// JavaScript Document

//Objet AJAX ( différent IE et Firefox )
function getXhr(){
  
  if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
  
  else if(window.ActiveXObject)
   {  
    try
     {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch (e)
     {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
     }
   }
  else 
   { 
    xhr = false; 
   }
}

function isLog(){
	document.location.reload();
}


//Envoi mail mot de passe oublié
function envoiMDP(){

	if ( isValidEmail ( document.forms["popupmotdepasse_form"].elements["popupmotdepasse_email"].value )  ){
	
		xhr = getXhr();
		
		if ( getXhr() != false ){
		
		xhr.onreadystatechange = function()
		{
		
			if(xhr.readyState == 4 && xhr.status == 200)
			 {
				
				var docXML= xhr.responseXML;
				var items = docXML.getElementsByTagName("reponse");
				
				if ( items.item(0).firstChild.data == "error" ){
					
					setError( "popupmotdepasse_erreur", "Erreur technique" );
					
				}else{
					
					closePopup( "popupmotdepasse" );
					affichPopup( "popupmotdepassevalid" );
					
				}
			
			 }
		}
		
		xhr.open("POST",'../php/produits/envoiMDP.php',true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send( "email=" + document.forms["popupmotdepasse_form"].elements["popupmotdepasse_email"].value );
	  
	  }
	
	}else{
		
		setError( "popupmotdepasse_erreur", "Merci d'indiquer une adresse e-mail valide" );
		
	}
	
	
}

function login(){
	//alert(document.url);
		if ( !isValidEmail ( document.forms["popuprituel_form"].elements["popuprituel_email"].value )  ){
			
			setError( "popuprituel_erreur", "Merci d'indiquer une adresse e-mail valide" );
			
		}else if ( document.forms["popuprituel_form"].elements["popuprituel_mdp"].value == "" ) {
			
			setError( "popuprituel_erreur", "Merci d'indiquer votre mot de passe" );
			
		}else{
				
				xhr = getXhr();
			
				if ( getXhr() != false ){
				
					xhr.onreadystatechange = function()
					{
					
					
						if(xhr.readyState == 4 && xhr.status == 200)
						 {
							
							var docXML= xhr.responseXML;
							var items = docXML.getElementsByTagName("reponse");
							
							if ( items.item(0).firstChild.data == "error" ){
								
								setError( "popuprituel_erreur", "Erreur technique" );
								
							}else if ( items.item(0).firstChild.data == "no" ){
								
								setError( "popuprituel_erreur", "E-mail ou mot de passe incorrect" );
								
							}else{
								
								isLog();

							}
						
						 }
					}
					
					xhr.open("POST",'../php/produits/login.php',true);
					xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					xhr.send( "email=" + document.forms["popuprituel_form"].elements["popuprituel_email"].value + "&mdp=" + document.forms["popuprituel_form"].elements["popuprituel_mdp"].value );
			  
			  }
			  
			  
			
		}

}





function loginAccesDirect( email, mdp ){
		
		if ( !isValidEmail ( email )  ){

			isLog();
			
		}else if ( mdp == "" ) {

			isLog();
			
		}else{

				xhr = getXhr();
			
				if ( getXhr() != false ){
				
					xhr.onreadystatechange = function()
					{
					
					
						if(xhr.readyState == 4 && xhr.status == 200)
						 {
							
							var docXML= xhr.responseXML;
							var items = docXML.getElementsByTagName("reponse");
							
							if ( items.item(0).firstChild.data == "error" ){
								
								isLog();
								
							}else if ( items.item(0).firstChild.data == "no" ){
								
								isLog();
								
							}else{
								
								location.reload() ;
								
							}
						
						 }
					}
					
					xhr.open("POST",'../php/produits/login.php',true);
					xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					xhr.send( "email=" + email + "&mdp=" + mdp );
			  
			  }
			  
			  
			
		}

}




//Envoi le diagnostic par mail

function envoiRituel(nom, email, mdp, typePeau, hydratation, preoccupation, contourYeux ){

		xhr = getXhr();
		
		if ( getXhr() != false ){
		
			xhr.onreadystatechange = function()
			{
			
				if(xhr.readyState == 4 && xhr.status == 200)
				 {
					
					var docXML= xhr.responseXML;
					var items = docXML.getElementsByTagName("reponse");
					
					if ( items.item(0).firstChild.data == "error" ){
						
					}else{
						
						validEnvoiRituel();
						
						
					}
				
				 }
			}
			
			xhr.open("POST",'../php/produits/envoiRituel.php',true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send("nom=" + nom + "&email=" + email + "&mdp=" + mdp + "&typePeau=" + typePeau +"&hydratation=" + hydratation +"&preoccupation=" + preoccupation +"&contourYeux=" + contourYeux );	
		}
}

function envoiRituelSolaire(nom, email, mdp, phototype, typePeau, protectionSolaire, enceinte, allergie){

		xhr = getXhr();
		
		if ( getXhr() != false ){
		
			xhr.onreadystatechange = function()
			{

				if(xhr.readyState == 4 && xhr.status == 200)
				 {
					
					var docXML= xhr.responseXML;
					var items = docXML.getElementsByTagName("reponse");
					
					if ( items.item(0).firstChild.data == "error" ){
						
					}else{
						
						validEnvoiRituel();
						
						
					}
				
				 }
			}
			
			xhr.open("POST",'../php/produits/envoiRituelSolaire.php',true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send("nom=" + nom + "&email=" + email + "&mdp=" + mdp + "&phototype=" + phototype +"&typePeau=" + typePeau +"&protectionSolaire=" + protectionSolaire +"&enceinte=" + enceinte +"&allergie=" + allergie);	
		}
}


//On affiche le popup de validation d'envoi du dignostic et uilise isLog.php pour recup le Civ Nom du client
function validEnvoiRituel(){

	xhr = getXhr();
  
	  if ( getXhr() != false ){
		  
		  xhr.onreadystatechange = function()
		   {
			
			
			if(xhr.readyState == 4 && xhr.status == 200)
			 {
				
				var docXML= xhr.responseXML;
				var items = docXML.getElementsByTagName("reponse");
				
				
				affichPopup( "popuprituelok" );
				
				if ( items.item(0).firstChild.data == "error" ){
					
					window.document.getElementById( "popuprituelok_titre" ).innerHTML = "";
				}else{
					
					window.document.getElementById( "popuprituelok_titre" ).innerHTML = items.item(0).firstChild.nextSibling.firstChild.data + ",";
				}
	
			 }
			 
		   }
		   
		  xhr.open("POST",'../php/produits/isLog.php',true);
		  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		  xhr.send(null);
		  
	  }

}



function setError( champs, ch )
{
	window.document.getElementById( champs ).style.visibility = 'visible';
	window.document.getElementById( champs ).innerHTML = ch;	
	//intV = setInterval( 'hideError(\'' + champs + '\')', 2000 );
}

function hideError( champs )
{ 
	var obj = window.document.getElementById( champs );
	obj.style.visibility = 'hidden';
	clearInterval( intV );
}

 