// variable globale pour connaître le code postal en cours de recherche
var agencyCP;
var micrositeId;

function getAgenciesList(id)
{
	if (arguments.length==1) {
		micrositeId=id;
	}
	var element = document.getElementById('boxAgencyCP');
	if (element==null)
	{
		element = document.getElementById('agencyCP');
	}
	agencyCP = element.value;
	if (agencyCP.length==5 && parseInt(agencyCP)) 
	{
		updateAgencyList(setAgenciesList, agencyCP, micrositeId);
	}
	else
	{
		element.value = "code postal";
	}
}

function removeLightbox()
{
	lightboxBG = document.getElementById("lightboxbg");
	lightbox = document.getElementById("TB_ajaxContent");
	document.body.removeChild(lightboxBG);
	document.body.removeChild(lightbox);
}

function reloadMap(length,id,url)
{
	for (var i = 0; i < length; i++)
	{
		document.getElementById('agence'+i).className="";
	}
	document.getElementById(id).className="active";
	document.getElementById("mygooglemap").innerHTML='<iframe id="mygoogleframe" width="344" height="472" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.selectour.com/common/googleMap.jsp' + url + '&width=344px&height=472px"></iframe><br /><small><a href="http://maps.google.fr/maps' + url + '" style="color:#0000FF;text-align:left" target="_blank">Agrandir le plan</a></small>';
}

function setAgenciesList(data)
{
	var lg = data.length;
	var lightBoxBgDiv = document.getElementById("lightboxbg");
    if (lightBoxBgDiv==null) 
	{
		var bgDiv = document.createElement("div");
		bgDiv.setAttribute("id","lightboxbg");
		bgDiv.onclick = function() {
			removeLightbox();
		};
		document.body.appendChild(bgDiv);
	} else {
	    lightBoxBgDiv.style.display = "block";
	    lightBoxBgDiv.style.visibility = "visible";
	    lightBoxBgDiv.onclick = function() {
            removeLightbox();
        };
	}

    var agenciesSearchActionPageReturn;
	var agenciesSearchActionPageReturnObj = document.getElementById("agenciesSearchActionPageReturn");
	if (agenciesSearchActionPageReturnObj != null){
		agenciesSearchActionPageReturn = agenciesSearchActionPageReturnObj.value;
	}else{ //aucune valeur renseignée, par défaut on dirige vers hotels-search (en prenant le risque de perdre la session si cookies désactivés)
		agenciesSearchActionPageReturn= "hotels-search.htm"  
	}
    
	var beginHTML = "<a style='float:right; margin:10px 30px 10px 10px' onclick='javascript:removeLightbox();'>fermer</a>"+
					"<div id='sa' ";
	if (!document.all) {
		beginHTML+="style='margin-top:30px'";
	}	
	beginHTML+="><div class='sa-header'>"+
			"<h1>Recherche d'agence</h1>"+
			"<h2>En choisissant votre agence, vous pouvez réserver en ligne ou à tout moment faire appel à tous les services et conseils d'un expert Selectour.</h2>"+
			"<div class='agency'>"+
				"<form id='boxSearchAgency' method='POST' name='searchAgency' action='javascript: getAgenciesList();'>"+
					"<span> Votre code postal : </span>"+
					"<input id='boxAgencyCP' type='text' maxlength='5' name='agencyCP' value='"+agencyCP+"' onfocus=\"this.value=''\"/>"+
					"<button class='button-ok' onclick='javascript: getAgenciesList(); return false;'>"+
					"</button>"+
				"</form>"+
			"</div>"+
		"</div>"+
		"<div class='sa-body'>"+
			"<div class='left'>"+
				"<ul class='results'>";

	var cpt;
	var middleHTML="";
	var endHTML;
	var test = true;
	
	if (lg==0) 
	{
		middleHTML="</ul><br/>" +
						"<h4>Il n'y a pas de résultat correspondant <br/>"+
						"à votre recherche. Noux vous conseillons <br/>"+
						"d'en modifier certains critères.</h4>";
		endHTML ="</div></div></div>";
	}
	else
	{
		for (cpt=0;cpt<lg;cpt++) 
		{
			if (test && data[cpt].zipCode!=agencyCP) {
				middleHTML+="<li><h2>Les agences de votre département</h2></li>"
				test=false;
			}
			middleHTML+="<li id='agence"+cpt+"'";
			if (cpt==0)
			{
				middleHTML+=" class='first active'";
			}
			middleHTML+="><div class='name'><h3>"+
							"<a onclick=\"javascript:reloadMap("+lg+",'agence"+cpt+"','?hl=fr&f=q&iwloc=addr&t=m&q="+data[cpt].address+" "+data[cpt].zipCode+" "+data[cpt].city+"+("+data[cpt].nameFre+")@"+data[cpt].latitude+","+data[cpt].longitude+"&ie=UTF-8&output=embed');\" href='javascript:void(0);'>"+data[cpt].nameFre+"</a>"+
							"</h3><p class='agence'><span class='address'>"+data[cpt].address+" "+data[cpt].zipCode+" "+data[cpt].city+"</span></p>"+
						"</div>"+
								"<div class='get'>"+
									"<p class='functions'>"+	
										"<span class='telephone'>"+data[cpt].phone+"</span>"+
										"<button class='button' onclick=\"javascript:document.getElementById('agencyChoice"+cpt+"').submit();\"></button>"+
										"<a href='mailto:"+data[cpt].mail+"'>"+data[cpt].mail+"</a>"+		
									"</p>"+						
								"</div>"+
								"<form id='agencyChoice"+cpt+"' method='POST' action='../"+agenciesSearchActionPageReturn+"'>"+
									"<input name='agencyChoice' type='hidden' value='"+data[cpt].id+"'>"+
								"</form></li>";			
		}	
		url="?hl=fr&f=q&iwloc=addr&t=m&q="+data[0].address+" "+data[0].zipCode+" "+data[0].city+"+("+data[0].nameFre+")@"+data[0].latitude+","+data[0].longitude+"&ie=UTF-8&output=embed";
		endHTML = "</ul></div></div><div id='mygooglemap'>"+
						'<iframe id="mygoogleframe" width="344" height="472" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.selectour.com/common/googleMap.jsp' + url + '&width=344px&height=472px"></iframe><br /><small>'+
							'<a href="http://maps.google.fr/maps' + url + '" style="color:#0000FF;text-align:left" target="_blank">Agrandir le plan</a></small></div></div>';
	}
	var element = document.getElementById("TB_ajaxContent"); 
	if (element==null)
	{
		var ajaxDiv = document.createElement("div");
		ajaxDiv.setAttribute("id","TB_ajaxContent");
		ajaxDiv.innerHTML = beginHTML + middleHTML + endHTML;
		document.body.appendChild(ajaxDiv);
	}
	else
	{
		element.innerHTML = beginHTML + middleHTML + endHTML;
	}
}
