function webServ_telform(tel)
{
	//value_session_telform = document.form_adduser.session_telform.value;
	value_session_telform = jQuery('#session_telform').val();
	// The full path to the proxy
	
	//PROD
	var url = 'http://www.cafe-privilege.com/speedform/accesServiceSpeedForm.php?Tel='+tel+'&session_telform='+value_session_telform;

	//DEV
	//var url = 'http://cafeprivilege/speedform/accesServiceSpeedForm.php?Tel='+tel+'&session_telform='+value_session_telform;
	
	jQuery.ajax({
		url:url,
		type: "GET",
		success: function(data)
		{
			console.log(data);
			
			var valSplit = data.split('#');

			//Aucune adresse n'a été trouvé
			if (valSplit[0] == '-2') 
			{
				document.getElementById("msg_telform2").style.display = "block";
				document.getElementById("msg_telform").style.display = "none";  
			}
			//Une adresse est remonté par le webservice
			else if (valSplit[0] == '1') 
			{
				document.getElementById("msg_telform2").style.display = "none";
				document.getElementById("msg_telform").style.display = "block";
				
				// script suppression des espaces devant et dérrière
				String.prototype.trim = function () {
				   return this.replace(/^\s*|\s*$/,"");
				}
				// script suppression des doubles espaces dans la chaine
				String.prototype.simplify= function () {
				   return this.replace(/\s+/g," ");
				}
				//civilité:Radio
				if (valSplit[22]=='Mle'){
				document.form_adduser.gender[0].checked=1;}
				else{document.form_adduser.gender[0].checked=0;}
				if (valSplit[22]=='Mme'){
				document.form_adduser.gender[1].checked=1;}
				else{document.form_adduser.gender[1].checked=0;}
				if (valSplit[22]=='M.'){
				document.form_adduser.gender[2].checked=1;}
				else{document.form_adduser.gender[2].checked=0;}
				
				
				//Nom : simple textbox
				document.form_adduser.lastname.value = valSplit[2].simplify();
					  
				//PrÃ©nom : simple textbox
				document.form_adduser.firstname.value = valSplit[3].simplify();
				
				//Raison Sociale
				//document.form_adduser.societeFacturation.value = valSplit[4].simplify();
				
				Adresse =valSplit[7] + '' + valSplit[8] + ' ' + valSplit[6] + ' ' +valSplit[9];
				AdressePropre = Adresse.trim();
				document.form_adduser.voie.value = Adresse.trim().simplify();
				//document.form_adduser.suburb.value = valSplit[10] + ' ' + valSplit[11] + ' ' + valSplit[12];
				
				// complement adresse
				AdresseCplt = valSplit[10] + ' ' + valSplit[11] + ' ' + valSplit[12];
				AdresseCpltPropre = AdresseCplt.trim();
				document.form_adduser.etage.value = AdresseCpltPropre.simplify();
				
				// Le code postal
				document.form_adduser.postcode.value = valSplit[13];
				
				villa = noaccent(valSplit[14]);
				var VilleMaj= villa.toUpperCase();
				// La ville
				document.form_adduser.city.value = VilleMaj.simplify();
				// Le Pays
				if (valSplit[15] == 'France') 
				{					  
				  selObj = document.form_adduser.country;					  
				  selObj.selectedIndex = 1; //Le 1er de la liste des pays est la France !!ATTENTION!! Bien vérifier la cohérence avec le fichier 'fra_pays.csv'
				}
			}
		}
	});	        
}

function noaccent(chaine) 
{
	temp = chaine.replace(/[\300\301\302\303\304\305\306\340\341\342\343\344\345ÀÁÂÃÄÅÆàâä]/gi,"a")
	temp = temp.replace(/[\350\351\352\353\340\310\311\312\313éèêë]/gi,"e")
	temp = temp.replace(/[\314\315\316\317\354\355\356\357îï]/gi,"i")
	temp = temp.replace(/[\322\323\324\325\326ôö]/gi,"o")
	temp = temp.replace(/[\331\332\333\334ùûü]/gi,"u")

	temp = temp.replace(/[\307\347]/gi,"c")
	return temp
}

function validatePhone(phone)
{
	return phone.replace(/\D/g,"");
}

function isTel() 
{
	document.getElementById("msg_telform2").style.display = "none";
	document.getElementById("msg_telform").style.display = "none";

	value_tel = jQuery('#telephone_sf').val();	
	jQuery('#telephone').val(value_tel);
	
	tel = validatePhone(value_tel);
	length_tel = tel.length;
	if ( length_tel >= 10 )
	{
		webServ_telform(tel);
	}
}

jQuery(document).ready(function(){	
								
	jQuery('#telephone_sf').bind("keydown", function(l) {

		if (l.keyCode == 13) 
		{
			isTel();	
		}	
	});
});


