var xmlHttp
var valida = "aàbcçdeéèfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var validn = "0123456789ab,"
var validCIN = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var apos="éçèà";
var temp
var flag=false;
var flagn=false;
var ville="";
var type;

function devis(str)
{
	 xmlHttp=GetXmlHttpObject();
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  }
  var url="";
  if(str=="addamaneauto")
  url="addamaneAutoProcessing.php"
else if(str=="ousraalmouaddaf_eco")
  url="ousratAlMouaddafEco.php"
 else if(str=="ousrataalim_eco")
 url="ousraTaalimEco.php"
 else if(str=="ousrataalim_confort")
 url="ousraTaalimConfort.php"
 else if(str=="ousrataalim_liberte")
 url="ousraTaalimLiberte.php"
 else if(str=="ousraalmouaddaf_liberte")
 url="ousratAlMouaddafLiberte.php"
 else if(str=="alousra")
 url="alousra.php"
  else if(str=="albayt"  || str=="albayt_almoumtaz")
 url="albayt.php"
 url=url+"?q=1"
   if(str=="albayt"){
	   var habitation="albayt";
	url=url+"&type_habitation="+habitation   
   }
   else if(str=="albayt_almoumtaz"){
	   var habitation="albayt_almoumtaz";
	url=url+"&type_habitation="+habitation   
   }
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=confirmation
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null);
}
 function confirmation(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		window.location.href=xmlHttp.responseText;
		
}
function emptyField(sa){
	
	 var str=document.getElementById(sa).value;
	 if(str==""){
	// 	alert("Veuiller remplir le champ: "+sa.substring(0,sa.length-2));
		//document.getElementById(sa).style.backgroundColor="pink";
		return;
		}
	/*else
		document.getElementById(sa).style.backgroundColor="white";*/
	
	if(sa=="cin_t" && (str.length < 2 || str.length > 8) && str.length!=0){
	 	alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
		document.getElementById(sa).style.backgroundColor="pink";
		return;
		}
//	else if(sa=="cin_t"){
		//checkCIN(str);
	//}
	else
		document.getElementById(sa).style.backgroundColor="white";
	
	/*if(sa=="numéro_patente_t"){
		checkPatente(str);
	}
	else
		document.getElementById(sa).style.backgroundColor="white";*/
	
	if((sa=="téléphone_professionel_t" || sa=="gsm")&& str.length !=10){
		if(flagn==false){
			return;
		}
	 	alert("Veuillez insérer un numero de téléphone correcte");
		document.getElementById(sa).style.backgroundColor="pink";
		return;
		}
	else
		document.getElementById(sa).style.backgroundColor="white";
	
	if(sa=="confirmation_du_mot_de_passe_t" && str!="" && str!=document.getElementById("mot_de_passe_t").value){
		alert("Attention, mot de passe et confirmation sont différents!")
		document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor="pink";
	}
	else
		document.getElementById(sa).style.backgroundColor="white";
	
}
function validateText(sa)
{ 
 var str=document.getElementById(sa).value;
	if(valida.indexOf(str.substring(str.length-1,str.length))== "-1"){
		alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
		document.getElementById(sa).value=str.substring(0,str.length-1);
		}
}
function validateCIN(sa)
{ 
 var str=document.getElementById(sa).value;
 var length=str.length;
 
 	if(length==1 && validCIN.indexOf(str)== "-1"){
		alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
		document.getElementById(sa).value="";
		flag=false;
		return;
		}
 	if(length==2 && validCIN.indexOf(str.substring(1,2))<0 && validn.indexOf(str.substring(1,2))<0){
		alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
		document.getElementById(sa).value=str.substring(0,str.length-1);
		return;
		}
		
	if(length==2 && validn.indexOf(str)>=0){
	}
	if(length==3 && validCIN.indexOf(str)>=0){
		alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
		document.getElementById(sa).value=str.substring(0,str.length-1);
		return;
	}
	if(length>2 && validn.indexOf( str.substring(str.length-1, str.length))== "-1"){
	alert("Veuillez insérer un "+sa.substring(0,sa.length-2)+" correcte");
	document.getElementById(sa).value=str.substring(0,str.length-1);
	}
}
	function checkDropDown(str,sa){
		 if(sa=="select_ville"){
			 ville=str;
			 if(str=="ville"){
			 alert("Veuillez choisir une ville correcte");
			 document.getElementById(sa).style.backgroundColor="pink";
			 return;
		 	}
			else	
				document.getElementById(sa).style.backgroundColor="white";
		}
		/* else if(sa=="select_sitFam"){
			 if(str=="famille"){
			 alert("Veuillez choisir une situation familiale correcte");
			 document.getElementById(sa).style.backgroundColor="pink";
			 return;
		 	}
			else	
				document.getElementById(sa).style.backgroundColor="white";
		}
		 else if(sa=="select_enf"){
			 if(str=="enfant"){
			 alert("Veuillez choisir un nombre d'enfants correcte");
			 document.getElementById(sa).style.backgroundColor="pink";
			 return;
		 	}
			else	
				document.getElementById(sa).style.backgroundColor="white";
		}
		else if(sa=="select_sexe"){
			 if(str=="sexe"){
			 alert("Veuillez choisir un sexe correcte");
			 document.getElementById(sa).style.backgroundColor="pink";
			 return;
		 	}
			else	
				document.getElementById(sa).style.backgroundColor="white";
		}
		else if(sa=="select_profession"){
			 if(str=="profession"){
			 alert("Veuillez choisir une profession correcte");
			 document.getElementById(sa).style.backgroundColor="pink";
			 return;
		 	}
			else	
				document.getElementById(sa).style.backgroundColor="white";
		}*/
		else if(sa=="select_client"){
			 if(str=="client"){
			 //alert("Veuillez choisir une option");
			 //document.getElementById(sa).style.backgroundColor="pink";
			 document.getElementById("select_clienttype").style.visibility="hidden";
			
			 return;
		 	}
			else{	
				document.getElementById(sa).style.backgroundColor="white";
				createBox(str);
				document.getElementById("select_clienttype").style.visibility="visible";
				
				
			}
		}
}
function createBox(str){
	
	removeAllOptions(document.getElementById("select_clienttype"));
	
	if(str=="oui"){
		document.getElementById("labelclient").value=""; //Veuillez spécifier<br>le type d'assurance :
		var option=new Array("", "", "Accident du travail", "Responsabilité civile", "Addamane Al Mihani", "Autres multirisque", "Transport", "Addamane Chaabi", "Recore", "Capital fin de carrière", "Al Moustakbal Chaabi", "Education", "Soins de santé", "Décès", "Accidents corporels" );
		var label=new Array("Votre Type d'assurance", "-----------------------", "Accident du travail", "Responsabilité civile", "Addamane Al Mihani", "Autres multirisque", "Transport", "Addamane Chaabi", "Recore", "Capital fin de carrière", "Al Moustakbal Chaabi", "Education", "Soins de santé", "Décès", "Accidents corporels" );
	}
	
	else if(str=="non"){
		document.getElementById("labelclient").value=""; //Votre compagnie d'assurance actuelle
		var option=new Array("", "", "Atlanta", "Axa Assurance Maroc", "CAT", "Es Saada", "La Marocaine Vie", "MAMDA", "MCMA", "MATU", "RMA Watanya", "Sanad", "Wafa Assurance", "Zurich");
		var label=new Array("Compagnie d'assurance actuelle", "-----------------------", "Atlanta", "Axa Assurance Maroc", "CAT-Compagnie D'assurance Transport", "Es Saada", "La Marocaine Vie", "MAMDA-Mut. Agricole Marocaine D'ass.", "MCMA-Mut. Centrale Marocaine D'ass.", "MATU-Mut. D'ass. Des Transp. Unis", "RMA Watanya", "Sanad", "Wafa Assurance", "Zurich");
	}
	
	for(var i=0;i<option.length;++i){
		var optn = document.createElement("OPTION");
		optn.text = label[i];
		optn.value = option[i];
		document.getElementById("select_clienttype").options.add(optn);
	}
}
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}
function validateNum(sa)
{ 
flagn=false;
 var str=document.getElementById(sa).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(sa).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(sa).value="";
		return;
	}
	else if(length>10){
   		//alert("Nombre maximale de chiffre atteint!");
		document.getElementById(sa).value=str.substring(0,str.length-1);
		return;
	}
	flagn=true;
}
function validatePatente()
{
	 var str=document.getElementById("numéro_patente_t").value;
	if(validn.indexOf(str.substring(str.length-1,str.length))== "-1"){
		alert("Veuillez insérer un N° RC ou N° Patente correcte");
		document.getElementById("numéro_patente_t").value=str.substring(0,str.length-1);
		return;
		}
}
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
  function validateEmailField() {
	 if(document.getElementById("username").value=="")
		return false;
    if (! isValidEmail(document.getElementById("username").value)) {
        //alert("Veuillez insérer une adresse email correcte");
		alert("Email incorrect !");
        return false;
    }
	  }
  function validateEmail() {
	 if(document.getElementById("email_t").value=="")
		return false;
    if (! isValidEmail(document.getElementById("email_t").value)) {
        //alert("Veuillez insérer une adresse email correcte");
		document.getElementById("email_t").style.backgroundColor="pink";
        return false;
    }
	checkEmail(document.getElementById("email_t").value);
	//document.getElementById("email_t").style.backgroundColor="white";
  }
  function verifySession(){
	xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="verifySession.php"
 xmlHttp.onreadystatechange=ok
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
  }
  
  function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
  function checkPatente(str)
 { 
  type="numéro_patente_t";
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="checkPatente.php"
 url=url+"?q="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=stateChanged 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }

 function login()
 {
if(validateEmailField()==false)
return;
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="identification.php"
 url=url+"?q=1&username="+document.getElementById("username").value
 url=url+"&pass="+document.getElementById("pass").value
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=loginReturn 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }
  function loginReturn() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
	   	if(xmlHttp.responseText==""){
			alert("Adresse email ou mot de passe invalide.");

		}
		else{

			if(document.getElementById("destination").value=="home")
				window.location.href="espace_client_home.php";
				//window.location="espace_client_home.php";
			else{

                                        
                    if(document.getElementById("type_habitation").value!="")
						window.location.href=document.getElementById("destination").value+".php?q=1&type_habitation="+document.getElementById("type_habitation").value;
						//window.location=document.getElementById("destination").value+".php?q=1&type_habitation="+document.getElementById("type_habitation").value;
					else
						
						if(document.getElementById("type_forfait").value!=""){
							window.location.href=document.getElementById("destination").value+".php?q=1&type_forfait="+document.getElementById("type_forfait").value;
						}
						//window.location=document.getElementById("destination").value+".php?q=1";
						else{
							window.location.href=document.getElementById("destination").value+".php?q=1";
						}
							
			}
		}
 	}
}
 function login2()
 { 
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="identification.php"
 url=url+"?q=2&username="+document.getElementById("username2").value
 url=url+"&pass="+document.getElementById("pass2").value
 url=url+"&destination="+document.getElementById("destination2").value
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=loginReturn2 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }
  function loginReturn2() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{//alert(xmlHttp.responseText);return;
		//alert(document.getElementById("destination").value+".php?q=1");return;
	   	if(xmlHttp.responseText==""){
			alert("Adresse email ou mot de passe invalide.");

		}
		else if(xmlHttp.responseText=="false"){
			window.location.href="profil_entreprise.php";
			//window.location="profil_entreprise.php";
		}
		else{
			//alert(document.getElementById("destination").value+".php?q=1");return;
			if(document.getElementById("type_habitation").value!="" && document.getElementById("destination").value=="albaytProcessing")
			{
				//alert("url2 : "+document.getElementById("destination").value+".php?q=1&type_habitation="+document.getElementById("type_habitation").value+"&flash_profil="+document.getElementById("flash_profil").value);
				window.location.href=document.getElementById("destination").value+".php?q=1&type_habitation="+document.getElementById("type_habitation").value+"&flash_profil="+document.getElementById("flash_profil").value;
				//window.location=document.getElementById("destination").value+"Processing.php?q=1&type_habitation="+document.getElementById("type_habitation").value+"&flash_profil="+document.getElementById("flash_profil").value;
			}
			else
			{
				//alert("url : "+document.getElementById("destination").value+".php?q=1&flash_profil="+document.getElementById("flash_profil").value);
				window.location.href=document.getElementById("destination").value+".php?q=1&flash_profil="+document.getElementById("flash_profil").value;
				//window.location=document.getElementById("destination").value+"Processing.php?q=1&flash_profil="+document.getElementById("flash_profil").value;
			}
		}
	//setTimeout('devisIncomplet()',1);
 	}
}

 function checkEmail(str)
 { 
  type="email_t";
 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=stateChanged 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }
  function ok() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
	   	if(xmlHttp.responseText=="true"){
			window.location.href="login.html";
			//window.location="login.html";
		}
		setTimeout('devisIncomplet()',1);
		
 	}
}
 function stateChanged() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
	   	if(xmlHttp.responseText=="true"){
			if(type=="email_t")
				var x;//alert("L'adresse email existante! Veuillez choisir une autre");
			else if(type=="cin_t")
				alert("CIN existant! Veuillez choisir un autre");
			else if(type=="numéro_patente_t")
				alert("numéro patente existant! Veuillez choisir un autre");
		document.getElementById(type).style.backgroundColor="pink";
		}
		else
		document.getElementById(type).style.backgroundColor="white";
 	}
}
function checkDate()
{
	var currentDate = new Date()
	if(document.getElementById("ddn_t").value=="")
		return false;
	var col_array=document.getElementById("ddn_t").value.split("/");
	var myDate= new Date();
	myDate.setDate(col_array[0]);
	myDate.setMonth(col_array[1]-1);
	myDate.setFullYear(col_array[2]);
	//alert(currentDate+"   "+myDate);
	//alert(col_array[0]+" "+col_array[1]+ " "+ col_array[2]);
	if(myDate>currentDate){
		document.getElementById("ddn_t").style.backgroundColor="pink";
		return false;
	}
	else
		document.getElementById("ddn_t").style.backgroundColor="white";
}
function go_particulier()
{
	validateEmail();
	if(document.getElementById("mot_de_passe_t").value!=document.getElementById("confirmation_du_mot_de_passe_t").value)
	{




			document.getElementById("confirmation_du_mot_de_passe_t").value="";
			document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor="pink";
	}
	else
		document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor="white";
	if( document.getElementById("nom_t").style.backgroundColor=="pink" || document.getElementById("nom_t").value==""
		||document.getElementById("prenom_t").style.backgroundColor=="pink" || document.getElementById("prenom_t").value==""
		||document.getElementById("ddn_t").style.backgroundColor=="pink" || document.getElementById("ddn_t").value==""
		||document.getElementById("cin_t").style.backgroundColor=="pink" || document.getElementById("cin_t").value==""
		||document.getElementById("adresse_t").style.backgroundColor=="pink" || document.getElementById("adresse_t").value==""
		||document.getElementById("select_ville").style.backgroundColor=="pink" ||document.getElementById("select_ville").value=="ville"
		||document.getElementById("téléphone_professionel_t").style.backgroundColor=="pink" || document.getElementById("téléphone_professionel_t").value==""
		||document.getElementById("gsm_t").style.backgroundColor=="pink" || document.getElementById("gsm_t").value==""
		||document.getElementById("email_t").style.backgroundColor=="pink" || document.getElementById("email_t").value==""
		||document.getElementById("mot_de_passe_t").style.backgroundColor=="pink" || document.getElementById("mot_de_passe_t").value==""
		||document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor=="pink" || document.getElementById("confirmation_du_mot_de_passe_t").value=="")
	{

				alert("Veuillez corriger les champs incorrectes et remplir les champs manquants!");
		return false;
	}
}
function go_entreprise()
{
	validateEmail();
	if(document.getElementById("mot_de_passe_t").value!=document.getElementById("confirmation_du_mot_de_passe_t").value)
		{
			document.getElementById("confirmation_du_mot_de_passe_t").value="";
			document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor="pink";
		}
	else
		document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor="white";
		
	if(document.getElementById("nom_t").style.backgroundColor=="pink" || document.getElementById("nom_t").value==""
		||document.getElementById("prenom_t").style.backgroundColor=="pink" || document.getElementById("prenom_t").value==""
		||document.getElementById("raison_sociale_t").style.backgroundColor=="pink" || document.getElementById("raison_sociale_t").value==""
		||document.getElementById("numéro_patente_t").style.backgroundColor=="pink" || document.getElementById("numéro_patente_t").value==""
		||document.getElementById("adresse_t").style.backgroundColor=="pink" || document.getElementById("adresse_t").value==""
		||document.getElementById("select_ville").style.backgroundColor=="pink" || ville=="ville"
		||document.getElementById("téléphone_professionel_t").style.backgroundColor=="pink" || document.getElementById("téléphone_professionel_t").value==""
		||document.getElementById("gsm_t").style.backgroundColor=="pink" || document.getElementById("gsm_t").value==""
		||document.getElementById("email_t").style.backgroundColor=="pink" || document.getElementById("email_t").value==""
		||document.getElementById("mot_de_passe_t").style.backgroundColor=="pink" || document.getElementById("mot_de_passe_t").value==""
		||document.getElementById("confirmation_du_mot_de_passe_t").style.backgroundColor=="pink" || document.getElementById("confirmation_du_mot_de_passe_t").value=="")
	{
		

		alert("Veuillez corriger les champs incorrectes et remplir les champs manquants!");
		return false;
	}
}
function go_identification()
{
	if(document.getElementById("email_t").style.backgroundColor=="pink" || document.getElementById("email_t").value==""
		||document.getElementById("mot_de_passe_t").style.backgroundColor=="pink" || document.getElementById("mot_de_passe_t").value=="")
	{
		alert("Veuillez corriger les champs incorrectes et remplir les champs manquants!");
		return false;
	}
}

 function getList(str)
 { 
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 var url="getlist.php?q="+str
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=getlistres 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 }
  function getlistres() 
 { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
	   	if(xmlHttp.responseText!=""){
			document.getElementById("list").innerHTML=xmlHttp.responseText;
		}
		else
		document.getElementById("list").innerHTML="Aucun devis à signlaler!";
		
 	}
}