var xmlHttp
var flagemail=0;
var flagemail2=0;
var validn = "0123456789"
function validateNum2(field)
{ 
 var str=document.getElementById(field).value;
 var length=str.length;
	if(validn.indexOf(str.substring(str.length-1,str.length))== "-1"){
		//alert("Veuillez insérer un numero de téléphone correcte");
		document.getElementById(field).value=str.substring(0,str.length-1);
		return;
		}
	else if(length==1 && str.substring(0,1)!=0){
		//alert("Veuillez insérer un numero de téléphone correcte");
		document.getElementById(field).value="";
		return;
	}
	else if(length>10){
   		//alert("Nombre maximale de chiffre atteint!");
		document.getElementById(field).value=str.substring(0,str.length-1);
		return;
	}
}
function fill()

{

	 xmlHttp=GetXmlHttpObject();

 if (xmlHttp==null)

  {

  alert ("Browser does not support HTTP Request")

  return

  }

  var url="";

 url=url+"creer_clientP.php?q=3"

 url=url+"&sid="+Math.random()

 xmlHttp.onreadystatechange=returnFill

 xmlHttp.open("GET",url,true)

 xmlHttp.send(null);

}

 function returnFill(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

		var response=xmlHttp.responseText;
		if(response=="false")

			return;

		var attributes=response.split("?");

		if(attributes[0]!=null && attributes[0]!="")

			document.getElementById("nom_t").value=attributes[0];

		if(attributes[1]!=null && attributes[1]!="")

			document.getElementById("prenom_t").value=attributes[1];

		if(attributes[2]!=null && attributes[2]!="")

		{
			var dates=attributes[2].split("/");
			document.getElementById("select_day").value=dates[0];
			document.getElementById("select_month").value=dates[1];
			document.getElementById("select_year").value=dates[2];
		}

		if(attributes[3]!=null && attributes[3]!="")

			document.getElementById("cin_t").value=attributes[3];

		if(attributes[4]!=null && attributes[4]!="")

			document.getElementById("adresse_t").value=attributes[4];

		if(attributes[5]!=null && attributes[5]!="")

			document.getElementById("téléphone_professionel_t").value=attributes[5];

		if(attributes[6]!=null && attributes[6]!="")

			document.getElementById("gsm_t").value=attributes[6];

		if(attributes[7]!=null && attributes[7]!="")
{
			document.getElementById("select_ville").value=attributes[7];

	}

		}

		

}

function update()

{

	 xmlHttp=GetXmlHttpObject();

 if (xmlHttp==null)

  {

  alert ("Browser does not support HTTP Request")

  return

  }

  var url="";

 url=url+"creer_clientP.php?q=2"

 url=url+"&nom="+document.getElementById("nom_t").value

 url=url+"&prenom="+document.getElementById("prenom_t").value

var date_naiss=document.getElementById("select_day").value+"/"+document.getElementById("select_month").value+"/"+document.getElementById("select_year").value;
 url=url+"&ddn="+date_naiss

 url=url+"&cin="+document.getElementById("cin_t").value

 url=url+"&adresse="+document.getElementById("adresse_t").value

 url=url+"&ville="+document.getElementById("select_ville").value

 url=url+"&telephone_professionel="+document.getElementById("téléphone_professionel_t").value

 url=url+"&gsm="+document.getElementById("gsm_t").value


 url=url+"&sid="+Math.random()

 xmlHttp.onreadystatechange=returnUpdate

 xmlHttp.open("GET",url,true)

 xmlHttp.send(null);

}

 function returnUpdate(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

//alert(xmlHttp.responseText);
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var to="";
		if(xmlHttp.responseText==""){
			window.location.href="espace_client_home.php";
			//window.location="espace_client_home.php";
		}
		else

			
			document.getElementById("errorhandlingclient").innerHTML=xmlHttp.responseText;
			window.location.href="#errorstart";
			//window.location="#errorstart";

		}
	}
}

function create()

{


	 xmlHttp=GetXmlHttpObject();

 if (xmlHttp==null)

  {

  alert ("Browser does not support HTTP Request")

  return

  }

  

  var url="";

 url=url+"creer_clientP.php?q=1"

 url=url+"&nom="+document.getElementById("nom_t").value

 url=url+"&prenom="+document.getElementById("prenom_t").value

 url=url+"&ddn="+document.getElementById("select_day").value+"/"+document.getElementById("select_month").value+"/"+document.getElementById("select_year").value

 url=url+"&cin="+document.getElementById("cin_t").value

 url=url+"&adresse="+document.getElementById("adresse_t").value

 url=url+"&ville="+document.getElementById("select_ville").value

 url=url+"&telephone_professionel="+document.getElementById("téléphone_professionel_t").value

 url=url+"&gsm="+document.getElementById("gsm_t").value

 url=url+"&email="+document.getElementById("email_t").value
 
 url=url+"&email_conf="+document.getElementById("email_conf").value

 url=url+"&select_sitFam="+document.getElementById("select_sitFam").value

 url=url+"&select_enf="+document.getElementById("select_enf").value

 url=url+"&select_sexe="+document.getElementById("select_sexe").value

 url=url+"&select_profession="+document.getElementById("select_profession").value

 url=url+"&select_clienttype="+document.getElementById("select_clienttype").value

 url=url+"&select_client="+document.getElementById("select_client").value

 url=url+"&mot_de_passe="+document.getElementById("mot_de_passe_t").value

url=url+"&conf="+document.getElementById("confirmation_du_mot_de_passe_t").value
url=url+"&stateemail="+flagemail
url=url+"&existingemail="+flagemail2
 url=url+"&sid="+Math.random()

 xmlHttp.onreadystatechange=returnCreate

 xmlHttp.open("GET",url,true)

 xmlHttp.send(null);
 
 document.getElementById('confirm_div').innerHTML='<img src="images/clear_pix" height="1" width="100" border="0"><img src="images/ajax.gif" border="0" alt="En cours de chargement" />';

}
function isValidEmail2(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        flagemail=1;required=true;
		
    }
    if (email==null) {
        if (required) {
            flagemail=0;return false;
			
        }
        flagemail=1;return true;
		
    }
    if (email.length==0) {  
        if (required) {
           flagemail=0; return false;
			
        }
        flagemail=1;return true;
		
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        flagemail=0;return false;
		
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        flagemail=0;return false;
		
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        flagemail=0;return false;
		
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        flagemail=0;return false;
		
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	flagemail=0;return false;
    
	} else if (email.indexOf(".") == email.length) {  // . must not be the last character
	flagemail=0;return false;
    
	}
    flagemail=1;return true;
	
}

  function validateEmail2() {
	 if(document.getElementById("email_t").value=="")
	{
		flagemail=0;return false;
	}
    if (! isValidEmail2(document.getElementById("email_t").value)) {
        //alert("Veuillez insérer une adresse email correcte");
		document.getElementById("email_t").style.backgroundColor="white";
        flagemail=0;
    }
	else
		flagemail=1;
	checkEmail2(document.getElementById("email_t").value);
	//document.getElementById("email_t").style.backgroundColor="white";
  }
 function checkEmail2(str)
 { 
  type="email_t";
//alert(flagemail);
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="checkEmail.php"
 url=url+"?q="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=stateChanged2 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }
function stateChanged2() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
	   	if(xmlHttp.responseText=="true"){

				flagemail2=0;//alert("numéro patente existant! Veuillez choisir un autre");
		//document.getElementById(type).style.backgroundColor="white";
		}
		else{
		document.getElementById(type).style.backgroundColor="white";
		flagemail2=1;
		}
 	}
}
 function returnCreate(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var to="";
		if(xmlHttp.responseText==""){
			to=document.getElementById("destination").value+".php?q=1&flash_profil="+document.getElementById("flash_profil").value;
			if(document.getElementById("type_habitation").value!="" && document.getElementById("destination").value=="albaytProcessing"){
				to=to+"&type_habitation="+document.getElementById("type_habitation").value;
			
			}
				//alert('des : '+to);
				window.location.href=to;
				//window.location=to;
		}
		else
		{
				document.getElementById('confirm_div').innerHTML='<input type="Image" src="http://www.cniadirect.ma/images/form_confirm.gif" name="Button" onClick="create();" style="cursor:hand;">';
				document.getElementById("errorhandlingclient").innerHTML=xmlHttp.responseText;
				window.location.href="#errorstart";
			//window.location="#errorstart";

		}
	}
}

