var end = new Array(false);
var cpt = 0;

function initialisePubs(url)
{
	if(window.XMLHttpRequest){ // Firefox
        xhr_object = new XMLHttpRequest();
    }else if(window.ActiveXObject){ // Internet Explorer
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    }else { // XMLHttpRequest non supporté par le navigateur
        return "";
    }
	
	rand=Math.floor(Math.random()*99999999);	
	
	xhr_object.open("GET", url+"&r="+rand, false);
    xhr_object.send(null);

    return xhr_object.responseText;
}


function OA_show(name) {
    if (typeof(OA_output[name]) == 'undefined') {
        return;
    } else {
        document.write(OA_output[name]);
    }
}




/**
 * Fonction permettant d'appeler le serveur afin d'ajouter les pubs manquantes avec un rayon plus éloigné
 * @param int num le numero de zone
 * @param string url l'url a appeler s'il n'y a pas de pub
 * @return void
 */
function checkPub(num, url, max)
{
	//initialisation de la valeur de retour
	text = "";
	
	//on incremente le compteur
	cpt++;
	//on remplit le tableau
	if(end[cpt]==undefined)
	{
		end[cpt] = true;
	}

	if(OA_output[num]=='' && ( (cpt<=max && end[cpt]) || (!end[cpt] && cpt==max) || max==0) )
	{
		//on appelle l'url car la zone n'a pas de pub
		if(window.XMLHttpRequest){ // Firefox
            xhr_object = new XMLHttpRequest();
	    }else if(window.ActiveXObject){ // Internet Explorer
	        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	    }else { // XMLHttpRequest non supporté par le navigateur
	        return;
	    }
		
		var regExp=/__bannerid=[0-9]+__/g;
		bannersId = String(document.getElementById('openX').innerHTML.match(regExp));
		var reg=new RegExp("__(bannerid=)?", "g");
		url += '&bannerId='+bannersId.replace(reg,"");

		if((!end[cpt] && cpt==max) || max==0)
		{
			url += '&all=1';
		}

		xhr_object.open("GET", url, false);
	    xhr_object.send(null);
	    
	    text = xhr_object.responseText;
	    eval(text);
	    
    	end[cpt] = false;
    	
	    if(OA_output[num]!="")
	    {
	    	document.write(OA_output[num]);
	    }
	    else if(cpt==max)
	    {
	    	//si les 3 recherches ne fonctionnent pas dès la première pub
	    	url += '&all=1';
	    	xhr_object.open("GET", url, false);
		    xhr_object.send(null);
		    
		    text = xhr_object.responseText;
		    eval(text);
		    
		    if(OA_output[num]!="")
		    {
		    	document.write(OA_output[num]);
		    }		    
	    }
	}
	
	//on reinitialise le compteur de rayon
	if(cpt==max)
	{
		cpt=0;
	}

    return text;
}

function getCookie(nomCookie, cleIdLieu, cleInsee, cleLieuEncode, cleCP, sep)
{
	
	if(document.cookie.length>0)
	{
		start_c=document.cookie.indexOf(nomCookie + "=");
		
		if (start_c!=-1)
		{
			start_c=start_c + nomCookie.length+1;
			end_c=document.cookie.indexOf(";",start_c);
			
			if (end_c==-1)
			{
				end_c = document.cookie.length;
			}
			
			cookie = unescape(document.cookie.substring(start_c, end_c));
			
			id = "";
			insee = "";
			encode = "";
			cp = "";
			
			tabParams = cookie.split(sep);
			
			for(i=0;i<tabParams.length;i++)
			{
				param = tabParams[i].split("=");
				
				if(param[0]==cleIdLieu)
				{
					id = param[1];
				}
				else if(param[0]==cleInsee)
				{
					insee = param[1];
				}
				else if(param[0]==cleLieuEncode)
				{
					encode = param[1];
				}
				else if(param[0]==cleCP)
				{
					cp = param[1];
				}
			}
			
			tabInfosLieu = new Array(id, insee, encode, cp);
			
			return tabInfosLieu;
		}
	}
}



function getPubs(nbPubs, rayons, typeBien, prixMin, prixMax, tabInfosLieu, url, divId)
{
    
	url += "?rayons="+rayons+"&nb="+nbPubs;
	
	if(typeBien!=""){
		url += "&typeBien="+typeBien;
	}
	if(prixMin!=""){
		url += "&prixMin="+prixMin;
	}
	if(prixMax!=""){
		url += "&prixMax="+prixMax;
	}
	
	if(tabInfosLieu!=null)
	{
	    url += "&id="+tabInfosLieu[0]+"&insee="+tabInfosLieu[1]+"&encode="+tabInfosLieu[2]+"&cp="+tabInfosLieu[3];
	}	
	
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
	}
	
	xhr.open("GET", url, false);
	xhr.send(null);
	
    document.write(xhr.responseText);
	
}
