



//instanciation des objets de communication Ajax
var xmlhttp1;




//fonction de creation d'un objet xmlHttpRequest necessaire a la communication Ajax
function createXmlHttpRequest(){

	var xmlhttp;

	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject){
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return xmlhttp;
}

//fonction chargement des villes propres au code postal
function loadAdressBookByNickName(urlAction, xmlhttp, nodeCAinput, choixDestinput, nodeOutput){
	loadAdressBookByNickNameAndCallBack(urlAction, xmlhttp, nodeCAinput,choixDestinput, nodeOutput, null);
}

//fonction chargement des villes propres au code postal avec appel d'une fonction au retour
/*function loadAdressBookByNickNameAndCallBack(urlAction, xmlhttp, nodeCAinput,choixDestinput, nodeOutput, fctCallBack){

	xmlhttp = createXmlHttpRequest();



	//verification qu'il existe une instance de l'objet xmlhttp
	if (xmlhttp != null && nodeCAinput != null && choixDestinput != null){

		var nickNameRecherche = nodeCAinput.value;

		var destination = choixDestinput;
				
		// d?finition de la fonction de call-back
		xmlhttp.onreadystatechange= function(){ processNickNameReply(xmlhttp, nodeOutput, fctCallBack); };

		//ecriture de l'url d'appel : action + variable code postal
		var url = urlAction + nickNameRecherche + "&destination="+destination;

		// param?trage de la requ?te (true = asynchrone)
		xmlhttp.open("GET",url,true);
		// envoi de la requ?te
		xmlhttp.send(null);
		
	} 
}*/

function loadAdressBookByNickNameAndCallBack(urlAction, xmlhttp, nodeCAinput,choixDestinput, nodeOutput, fctCallBack){
	var nickNameRecherche = nodeCAinput.value;

	var destination = choixDestinput;
	
	var url = urlAction + nickNameRecherche + "&destination="+destination;
	
	$.ajax({
		async: false,
	   	type: "GET",
 				url: url,
 				success: function(responseXml){
 					if(null != nodeOutput){
   						writeListNickName(responseXml, nodeOutput );
   					}
   					
		   			//appel de la fonction voulue
					if (fctCallBack != null){
						fctCallBack.apply();
					}
 				}
	});
}

//fonction call-back renvoyant vers la fonction de traitement de mise a jour de la liste des villes de la comboBox
function processNickNameReply (xmlhttp, nodeOutput, fctCallBack){
	// si xmlhttp est "loaded"
	if (xmlhttp.readyState==4){
		// si "OK"
		if (xmlhttp.status==200){

			//la reponse Ajax est arrive, on appelle la methode de MAJ de la liste des villes
			if(null != nodeOutput){
				writeListNickName(xmlhttp, nodeOutput );
			}

			//appel de la fonction voulue
			if (fctCallBack != null){
				fctCallBack.apply();
			}
		}
	}
}

//place la liste des villes dans la comboBox voulue
function writeListNickName(responseXml, nodeOutput ){
	
	var contenu="";
	contenu = '<table width="375" cellspacing="1" cellpadding="0" style="border: solid 1px grey">';
	contenu += '<tr><th width="375" cellspacing="1" cellpadding="0" colspan=2 style="background-color: #ffffff; vertical-align: center">&nbsp;&nbsp;&nbsp;Vos contacts</th></tr>'; 	
	//alert('contenu='+contenu);
	//while (nodeXmlNick!= null){
	$(responseXml).find("nickName").each(function() {
		//alert($(this).find("nick").text()+"\n"+$(this).find("firstName").text()+"\n"+$(this).find("name").text()+"\n"+$(this).find("id").text());

		var idNick = $(this).find("id").text();
		var labelNick = $(this).find("nick").text();
		var labelName	  = $(this).find("name").text();		
		var lablFirstName = $(this).find("firstName").text();
				
		
		//alert('idNick:'+idNick+' labelNick:'+labelNick);
				
		contenu += '<tr style="border: solid 1px grey; background-color:white; height:10px;">';
		contenu += '<td onmouseover="this.style.background=\'#ff6010\'" onmouseout="this.style.background=\'#ffffff\'" class="td_cal" style="padding:0px; height:10px;">';
		contenu += '<a class="a_tab" a href="javascript:chooseNickName(' + idNick + ')"><span style="font-size:9px;">'+labelNick+'</span></a></td></tr>';		
		
	});
	contenu += '</table>';
	//alert('contenu='+contenu);
	if (contenu.indexOf('td')<0) {
		contenu='<span class="texteErreur"><i>&nbsp;Aucun contact correspondant</i></span>';
	}

	//alert('contenu:'+contenu);

	nodeOutput.innerHTML=contenu;

	nodeOutput.style.display = "block";

}




