/*
Includes code from ryan masuga, masugadesign.com
*/

function popupRatingAd(src, id) {
	window.open(src+'?id='+id, '', 'top=250, left=250, width=410, height=260, resizable=yes, toolbar=no, scrollbars=non, status=no, directories=no');
}

if (window.XMLHttpRequest) { // FIREFOX
	xmlhttp = new XMLHttpRequest();
} else if(window.ActiveXObject) { // IE
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	xmlhttp = false;
}

function post_comment(vote, annonce_id, comment, email, lang, ad_detail,isgold)
{

	if (ad_detail == 'true') {
		xmlhttp.open('get', '/modules/annonces/rating_bar/rpc.php?j='+vote+'&amp;q='+annonce_id+'&amp;comment='+comment+'&amp;mail='+email+'&amp;lang='+lang+'&amp;action=submit&amp;ad_detail=true&amp;gold=\''+isgold+'\'');
		xmlhttp.onreadystatechange = handleResponse;
		xmlhttp.send(null);
	}else{
	( function($) {
		// Utilisation de JQuery
		var divtoshow = "#unit_long" + annonce_id;
		$.get("/modules/annonces/rating_bar/rpc.php", {j: vote, q: annonce_id, comment: comment,mail : email, lang: lang, action : "submit", ad_detail : "false",gold: isgold}, function(data){
			if(data.length >0) {
				//$(divtoshow).show();
				$(divtoshow).html(data);
			}
		});
	} ) ( jQuery )
	
		//xmlhttp.open('get', '/rpc.php?j='+vote+'&q='+annonce_id+'&comment='+comment+'&mail='+email+'&lang='+lang+'&action=submit&ad_detail=false');
	}

}

function post_comment_abus(annonce_id, comment, lang, ad_detail)
{

	if (ad_detail == 'true') {
		xmlhttp.open('get', '/modules/annonces/rating_bar/rpc_abus.php?q='+annonce_id+'&comment='+comment+'&lang='+lang+'&action=submit&ad_detail=true');
		xmlhttp.onreadystatechange = handleResponse;
		xmlhttp.snd(null);
	}else{
	( function($) {
		// Utilisation de JQuery
		var divtoshow = "#unit_long" + annonce_id;
		$.get("/modules/annonces/rating_bar/rpc_abus.php", {q: annonce_id, comment: comment,lang: lang,action : "submit"}, function(data){
			if(data.length >0) {
				//$(divtoshow).show();
				$(divtoshow).html(data);
			}
		});
	} ) ( jQuery )
		//xmlhttp.open('get', '/rpc_abus.php?q='+annonce_id+'&comment='+comment+'&lang='+lang+'&action=submit');
	}
}


function hide_abus(annonce_id, lang, ad_detail){
	document.getElementById("abus_comment"+annonce_id).style.display = "none";
	if (ad_detail == 'true') {
		document.getElementById("abus-gif-"+annonce_id).innerHTML='<img src="/modules/annonces/images/ico_attention.png" onclick="sndReq_abus('+annonce_id+', 5, \''+lang+'\', \'true\')" alt="" />';
	}else{
		document.getElementById("abus-gif-"+annonce_id).innerHTML='<img src="/modules/annonces/images/ico_attention.png" onclick="sndReq_abus('+annonce_id+', 5, \''+lang+'\')" alt="" />';
	}
}

function com(form,log)
{
	if(form.commentaire.value=="")
	{
		alert('Aucun commentaire n\'a été saisi');
		return false;
	}
	return true;
}

function sndReq(vote, id_num, lang, ad_detail, ref, is_gold) {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	theUL.innerHTML = '<div class="loading"></div>';
	if (ad_detail == 'true') {
	
		( function($) {
		// Utilisation de JQuery
		var divtoshow = "#unit_long" + id_num;
		$.get("/modules/annonces/rating_bar/rpc.php", {j: vote, q: id_num,lang: lang, action : "vote", ad_detail : true, ad: ref, gold: is_gold}, function(data){
			if(data.length >0) {
				$(divtoshow).show();
				$(divtoshow).html(data);
			}
		});
		} )( jQuery )
	
	
		/*xmlhttp.open('get', '/rpc.php?j='+vote+'&q='+id_num+'&lang='+lang+'&action=vote&ad_detail=true');
		xmlhttp.onreadystatechange = handleResponse;
		xmlhttp.send(null);*/
	}else{
	( function($) {
		// Utilisation de JQuery
		var divtoshow = "#unit_long" + id_num;
		$.get("/modules/annonces/rating_bar/rpc.php", {j: vote, q: id_num,lang: lang, action : "vote", ad: ref, gold: is_gold}, function(data){
			if(data.length >0) {
				$(divtoshow).show();
				$(divtoshow).html(data);
			}
		});
		} )( jQuery )
	
	
		//xmlhttp.open('get', '/rpc.php?j='+vote+'&q='+id_num+'&lang='+lang+'&action=vote');
	}

}

function sndReq_abus(id_num, units, lang, ad_detail, ref)
{
	if (ad_detail == 'true') {
		document.getElementById("abus-gif-"+id_num).innerHTML='<img style="height:17px;" src="/modules/annonces/images/ico_attention_off.png" onclick="hide_abus('+id_num+', \''+lang+'\', \'true\')" alt="" />';
		xmlhttp.open('get', '/rpc_abus.php?q='+id_num+'&c='+units+'&lang='+lang+'&ad_detail=true');
    }else{
	( function($) {
		// Utilisation de JQuery
		var divtoshow = "#unit_long" + id_num;
		$.get("/modules/annonces/rating_bar/rpc_abus.php", {q: id_num, c: units,lang: lang,ad:ref}, function(data){
			if(data.length >0) {
				$(divtoshow).show();
				$(divtoshow).html(data);
			}
		});
	} ) ( jQuery )

    	// xmlhttp.open('get', '/rpc_abus.php?q='+id_num+'&c='+units+'&lang='+lang);
    }
}

function handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
	        var response = xmlhttp.responseText;
	        var update = new Array();
	        if(response.indexOf('|') != -1) {
	            update = response.split('|');
				changeText(update[0], update[1]);
	        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0;
    if (parseInt(navigator.appVersion) >=5) {DOM=1};
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

