/**
 * @author BOUKADIDA
 */
Event.observe(window,'load',Myinit);




/*Pour la page prepare_aller.php*/
function DataCheck()
{ 
  if (document.CMCICPaymentTesting.reference.value.length == 0)
  {
   alert("Order Reference is a mandatory field, numeric maxlength 12 ");
   document.CMCICPaymentTesting.reference.focus();
   return false;
  }  
  if (document.CMCICPaymentTesting.reference.value.length > 12)
  {
   alert("Order Reference is a mandatory field, , numeric maxlength 12 !");
   document.CMCICPaymentTesting.reference.focus();
   return false;
  } 
 return true;
}

function PopupCentrer(page,largeur,hauteur,options) {

var top=(screen.height-hauteur)/2;

var left=(screen.width-largeur)/2;

window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);

}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function valider_qty_panier(){
	
	if(document.getElementById('input_qty')){
		
		input_=document.getElementById('input_qty');
		if(input_.value != parseInt(input_.value))
		alert('Veuilez saisir un nombre entier SVP !');
		return false;
	}
	
	return true;
	
}




window.onload="MM_preloadImages('../i/deconnecter-over.jpg','../i/votre_profil-over.jpg','../i/last_visited-over.gif','../i/flech-bas2.gif','../i/flech-haut2.gif','../i/choisissez_motif-over.gif','../i/finalisercommande-over.gif','../i/payement_securise-over.gif','../i/choisissez_housse-over.gif','../i/demande_devis-over.jpg','../i/devis_client-over.gif')";

//=======prototip=======
if (document.observe) {
	document.observe('dom:loaded', function(){
		$$('a[rel]').each(function(element){
			new Tip(element, element.rel, {
				className: 'tooltipClass'
			});
		});
	});
	
}


/*=========Partie choix housse================
  ============================================
*/




function Myinit(){

if ($("table_choix_housse")) {
		
		//Pour chaque morceau du housse
		$$("tr.tr_choix_housse>td select").each(function(selectHousse){
		
			var len = selectHousse.name.length;
			var code = selectHousse.name.charAt(len - 1);//code de la partie A,B,C,...	
			
			//$("liste_couleur_"+code).hide();	
			
			
			Event.observe(selectHousse, 'change', function(){
			
				//Récuperer la valeur selectionné:			
				
				var matiere = selectHousse.options[selectHousse.selectedIndex].value;
				
				new Ajax.Request('../includes/ajax.inc.php', {
					method: 'post',
					postBody: 'action=listeCouleur&matiere=' + escape(matiere) + '&code=' + escape(code),
					onComplete: function(request){
					
					//$("liste_couleur_"+code).innerHTML=request.responseText;
					new Insertion.Bottom($("id_style_"+code), request.responseText);
					
								
					
					}
				});
				
				
			});		
			
			
			
		});//Pour chaque morceau du housse
		
		
		//submit de la form:
		
		//Event.observe( $('submit_send'),'click',submit_choix_housse);
		
}
	
//Adresse de livraison
if($('check_adresse_livraison') && $('div_adresse_livraison')){
	
	var div_adresse_livraison=$('div_adresse_livraison');
	
	Event.observe("check_adresse_livraison",'change',function(){
		
		if(div_adresse_livraison.getStyle('display')=='none') div_adresse_livraison.setStyle({display: 'block'});
		else div_adresse_livraison.setStyle({display: 'none'});
	});
	
}	
	
//FormCheckOut :   detecter le mail du l'acheter pendant le checkout

if($("FormCheckOut") && $("EmailCheck") && $('MdpCheck')){
	
	
	Event.observe($("EmailCheck"), 'blur', function(){
			
				//Récuperer la valeur selectionné:			
				
				var mail= $F('EmailCheck');
				
		if(isValidEmail(mail))
		{
				new Ajax.Request('../includes/ajax.inc.php', {
					method: 'post',
					postBody: 'action=CheckEmail&mail=' + escape(mail) ,
					onComplete: function(request){						
							
							if(request.responseText==1){							
								new Effect.Highlight('EmailCheck', {startcolor:'#ff99ff', endcolor:'#999999'});
								
								alert("Vous êtes déjà un client, vous devez vous connecter SVP");
								$('erreur_inscription_checkout').innerHTML='Vous êtes déjà un client, vous devez vous connecter SVP';
															
							}
				
						}
				});
				
	    }else alert("Vous venez de saisir une Email invalide \n Veuillez resaisir votre Email SVP.");	
		
				
	});		
	
	
	//Detection du mot de passe	du client	
	Event.observe($("VerifMdpCheck"), 'blur', function(){
			
				//Récuperer la valeur selectionné:			
				var mail= $F('EmailCheck');
				var mdp= $F('MdpCheck');
				var verif=$F('VerifMdpCheck');
				if (mail != '' && mdp != '' && verif != '') {
					if (isValidEmail(mail) && isValidMdp(mdp) && verif==mdp)
					{
						new Ajax.Request('../includes/ajax.inc.php', {
							method: 'post',
							postBody: 'action=CheckMdp&mdp=' + escape(mdp) + '&mail=' + escape(mail),
							onComplete: function(request){
							
								if (request.responseText == 1) {
								
									new Effect.Highlight('MdpCheck', {
										startcolor: '#307830',
										endcolor: '#98BF0C'
									});
									new Effect.Highlight('VerifMdpCheck', {
										startcolor: '#307830',
										endcolor: '#98BF0C'
									});
									
									
									document.location = document.location;
									
									$("MdpCheck").innerHTML=mdp;
									
									$("VerifMdpCheck").innerHTML=verif;
									
									
								}
								
							}
						});
						
					}else{
						new Effect.Highlight('MdpCheck', {
										startcolor: '#FF6600',
										endcolor: '#FF0000'
									});
									new Effect.Highlight('VerifMdpCheck', {
										startcolor: '#FF6600',
										endcolor: '#FF0000'
									});
						
						
					}
										
				}	
					
				
	});		
	
	
	
	
	
	
}
	
	
}//fin init

submit_choix_housse=function(){
	var instance=this;
	//verifier si le client est connecté:
	instance.bien_connected="oui";
	var requete=new Ajax.Request('../includes/ajax.inc.php', {
					method: 'post',
					postBody: 'action=verifierConnection',
					onComplete: function(request){
					
					//$("liste_couleur_"+code).innerHTML=request.responseText;
					//new Insertion.Bottom($("liste_couleur_"+code), request.responseText);
					var reponse=request.responseText;
					alert("reponse="+reponse);
						if (reponse == 'ko') {
							
							instance.bien_connected ="non"
							alert('Vous devez être connecter pour demander un devis.');
						
						}
									
					
					}
				});
		if(instance.bien_connected =="non")	{	
		alert('Vous devez être connecter pour demander un devis.');
		return false;
	
		}
	
	//si tout va bien on continue:	
	
	alert('bien_connected ='+instance.bien_connected);
	
	arraycode=new Array('A','B','C','D','E','F');
	
	var	codeSelected='';
	var idSelected='';
		for(var i=0;i<arraycode.length;i++){
			var code=arraycode[i];
			
			if($('id_style_'+code))
			{ 
				var id_style=$('id_style_'+code);
				if (id_style.selectedIndex>-1) {
					var selection = id_style.options[id_style.selectedIndex].value;
					codeSelected += code + ';';
					idSelected += selection + ';';
				}
			}
			
			
		}
		
		//recuperer prodid
		var prodid=document.form_choix_housse.id;
		//Envoyer les id des style selectonnées, au serveur
		new Ajax.Request('../includes/ajax.inc.php', {
					method: 'post',
					postBody: 'action=demandeDevis&codeSelected=' + escape(codeSelected) +
					 '&idSelected=' + escape(idSelected)+
					 '&prodid='+escape(prodid),
					onComplete: function(request){
					var reponse=request.responseText;
					//$("liste_couleur_"+code).innerHTML=request.responseText;
					//new Insertion.Bottom($("liste_couleur_"+code), request.responseText);
					if(reponse.substring(0,3)=="not")
						alert('Vous devez être connecter pour demander un devis.');
					else if(reponse=='ok')
						$('message').innerHTML="Merci pour votre demande , <br>	un Email de confirmation vous a été envoyé";
					else if(reponse=='ko')			
						$('message').innerHTML="Votre demande n'a pas été prise en compte, <br>	vous devez réessayer.";
					
					}
				});
				
		
}

function valider_inscription(){
//Si la form est chargé	
var toReturn="";
if (document.inscription) {


	if (document.inscription.nom.value.length == 0) {
		toReturn += "Nom saisie invalide<br>";
	}
	
	if (document.inscription.prenom.value.length == 0) {
		toReturn += "Prenom saisie invalide<br>";
		
	}
	
	if (document.inscription.adresse.value.length < 7) {
		toReturn += "Adresse saisie invalide<br>";
	}
	if (document.inscription.cp.value.length == 0 || !isValidCp(document.inscription.cp.value)) {
	
		toReturn += "CP saisie invalide<br>";
	}
	if (document.inscription.ville.value.length < 3 || !isValidVille(document.inscription.ville.value)) {
		toReturn += "ville saisie invalide<br>";
	}
	if (document.inscription.email.value=="" || !isValidEmail(document.inscription.email.value)) {
		toReturn += "Adresse E-mail saisie invalide<br>";
	}
	if (document.inscription.phone.value=="" || !verif_numero_inter(document.inscription.phone.value)) {
		toReturn += "Numéro de téléphone saisie invalide<br>";
		
	}
	//===type_custom
	if(!document.inscription.type_custom[0].checked && !document.inscription.type_custom[1].checked  && !document.inscription.type_custom[2].checked )
	toReturn +="Merci de choisir votre type de client<br>";
	//==
	
	//====mot de passe
	var passe1=document.inscription.password.value;
	var passe2=document.inscription.verifpassword.value;
	
	if( ! isValidMdp(document.inscription.password.value) )	
		toReturn +="Mot de passe saisie invalide<br>";	
				
	if( passe1 != passe2 )	
		toReturn +="Confirmation saisie invalide<br>";	
	//====	
		
	
}	

		if(toReturn=="") return true;
		else{
			
			document.getElementById("erreur_inscription").innerHTML=toReturn;
			
			return false;
			
		}
	
}

function valider_checkout(){
	
	
//Si la form est chargé	
var toReturn="";
if (document.form_ckeckout) {


	if (document.form_ckeckout.nom.value.length == 0) {
		toReturn += "Nom saisie invalide<br>";
	}
	
	if (document.form_ckeckout.prenom.value.length == 0) {
		toReturn += "Prenom saisie invalide<br>";
		
	}
	
	if (document.form_ckeckout.adresse.value.length < 7) {
		toReturn += "Adresse saisie invalide<br>";
	}
	if (document.form_ckeckout.cp.value.length == 0 || !isValidCp(document.form_ckeckout.cp.value)) {
	
		toReturn += "CP saisie invalide<br>";
	}
	if (document.form_ckeckout.ville.value.length < 3 || !isValidVille(document.form_ckeckout.ville.value)) {
		toReturn += "Ville saisie invalide<br>";
	}
	if (document.form_ckeckout.email.value=="" || !isValidEmail(document.form_ckeckout.email.value)) {
		toReturn += "Adresse E-mail saisie invalide<br>";
	}
	if (document.form_ckeckout.phone.value=="" || !verif_numero_inter(document.form_ckeckout.phone.value)) {
		toReturn += "Numéro de téléphone saisie invalide<br>";
		
	}
	//===type_custom
	if(!document.form_ckeckout.type_custom[0].checked && !document.form_ckeckout.type_custom[1].checked  && !document.form_ckeckout.type_custom[2].checked )
	toReturn +="Merci de choisir votre type de client<br>";
	//==
	
	//====mot de passe
	var passe1=document.form_ckeckout.password.value;
	var passe2=document.form_ckeckout.verifpassword.value;
	
	if( ! isValidMdp(document.form_ckeckout.password.value) )	
		toReturn +="Mot de passe saisie invalide<br>";	
				
	if( passe1 != passe2 )	
		toReturn +="Confirmation saisie invalide<br>";	
	//====	
		
		
//=====Adresse de livraison======
/*
if (document.checkout.ship_name.value.length != 0 ||
document.checkout.ship_firstname.value.length != 0 ||
document.checkout.ship_address.value.length != 0 ||
document.checkout.ship_zip.value.length != 0 ||
document.checkout.ship_city.value.length != 0 ||
document.checkout.ship_country.value.length != 0 ||
document.checkout.ship_mail.value.length != 0 ||
document.checkout.ship_phone.value.length != 0) {
*/
if (document.form_ckeckout.check_adresse_livraison.checked){
	if (document.form_ckeckout.ship_name.value.length == 0) {
		toReturn += "Nom saisie invalide<br>";
	}
	
	if (document.form_ckeckout.ship_firstname.value.length == 0) {
		toReturn += "Prenom saisie invalide<br>";
		
	}
	
	if (document.form_ckeckout.ship_address.value.length < 7) {
		toReturn += "Adresse saisie invalide<br>";
	}
	if (document.form_ckeckout.ship_zip.value.length == 0 || !isValidCp(document.form_ckeckout.ship_zip.value)) {
	
		toReturn += "CP saisie invalide<br>";
	}
	if (document.form_ckeckout.ship_city.value.length < 3 || !isValidVille(document.form_ckeckout.ship_city.value)) {
		toReturn += "Ville saisie invalide<br>";
	}
	if (document.form_ckeckout.ship_country.value.length ==0) {
		toReturn += "Pays saisie invalide<br>";
	}
	if (document.form_ckeckout.ship_mail.value == "" || !isValidEmail(document.form_ckeckout.ship_mail.value)) {
		toReturn += "Adresse E-mail saisie invalide<br>";
	}
	if (document.form_ckeckout.ship_phone.value == "" || !verif_numero_inter(document.form_ckeckout.ship_phone.value)) {
		toReturn += "Numéro de téléphone saisie invalide<br>";
	}
}	
	//=================
}	

		if(toReturn=="") return true;
		else{
			
			document.getElementById("erreur_inscription_checkout").innerHTML=toReturn;
			
			return false;
			
		}
		
	
	
	
}

function verif_numero_inter(num_tel){
		// Definition du motif a matcher
		//var regex = new RegExp(/\b00[0-9]{2,3}[0-9]{6,}\b|\b(01|02|03|04|05|06|08|09)[0-9]{8}\b/);
		var regex = new RegExp(/\b[0-9]{10,}\b/);
		// Definition de la variable booleene match
		var match = false;
		
		// Test sur le motif
		if (regex.test(num_tel)) {
			match = true;
		}
		else {
			match = false;
		}
		
		// On renvoie match
		return match;
}	
	
function isValidEmail(str){
		var expr = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return expr.test(str);
}
	
function isValidMdp(str)
{
		
		if(str.length<4) return false;	
	
		var regex = new RegExp(/\b[a-zA-Z0-9]{4,8}\b/);
		return str.match(regex);
		
}

function isValidCp(str){
	if(str.length<5) return false;	
	
		var regex = new RegExp(/\b[a-zA-Z0-9]{5,}\b/);
		return str.match(regex);	
}

function  isValidVille(str){
	
	var regex = new RegExp(/\b[a-zA-Z0-9\-]{3,}\b/);
	return str.match(regex);
	
	
}
function isValideNumeroTva(str){
	
	var regex = new RegExp(/\b[0-9]{18,20}\b/);
	return str.match(regex);
	
	
}
function isValideNumeroCarteBancaire(str){
	
	var regex = new RegExp(/\b[0-9]{16}\b/);
	return str.match(regex);
	
	
}
function isValideCryptogramme(str){
	
	var regex = new RegExp(/\b[0-9]{3}\b/);
	return str.match(regex);
	
	
}
function isValideLicence(str){
	
	var regex = new RegExp(/\b[a-zA-Z0-9]{6,8}\b/);
	return str.match(regex);
	
	
}

function valider_application_pi_old(){
var valide="";		alert("valide1= " +valide);
	if(document.application_pi.nomSociete.value == "") {
		
		
			if (document.getElementById)
		    {
		   obj=document.getElementById("nomSociete");
		    }
		  else if (document.all)
		    {
		   obj=document.all["nomSociete"];
		    }
				
		
		if(obj)
		obj.innerHTML = "Vous devez saisir Vos Nom et Prénom";
		valide = 'non';
	}
	alert("valide2= " +valide);
	if(document.application_pi.adresse.value == "" ) {
		
			if (document.getElementById)
		    {
		   obj=document.getElementById("adresse");
		    }
		  else if (document.all)
		    {
		   obj= document.all["adresse"];
		    }
		if(obj)
		obj.innerHTML = "Vous devez saisir votre adresse";
		valide = 'non';
	}
	alert("valide3= " +valide);
	if(!verif_numero_inter(document.application_pi.tel.value)) {
		if (document.getElementById)
		    {
		   obj=document.getElementById("tel");
		    }
		  else if (document.all)
		    {
		   obj= document.all["tel"];
		    }
		if(obj)
		obj.innerHTML = "Votre numéro de téléphone semble être non valide";
		valide = 'non';
	}
	alert("valide4= " +valide);
	if(!verif_numero_inter(document.application_pi.portable.value)) {
		if (document.getElementById)
		    {
		   obj=document.getElementById("portable");
		    }
		  else if (document.all)
		    {
		   obj= document.all["portable"];
		    }
		if(obj)
		obj.innerHTML = "Votre numéro de Portable semble être non valide";
		valide = 'non';
	}
	alert("valide5= " +valide);
	if(!verif_numero_inter(document.application_pi.fax.value)) {
		if (document.getElementById)
		    {
		   obj=document.getElementById("fax");
		    }
		  else if (document.all)
		    {
		   obj= document.all["fax"];
		    }
		if(obj)
		obj.innerHTML = "Votre numéro de Fax semble être non valide";
		valide = 'non';
	}
	alert("valide6= " +valide);
	if(!isValidEmail(document.application_pi.email.value)) {
		if (document.getElementById)
		    {
		   obj=document.getElementById("email");
		    }
		  else if (document.all)
		    {
		   obj= document.all["email"];
		    }
		if(obj)
		obj.innerHTML = "Votre Email semble être non valide";
		valide = 'non';
	}
	alert("valide7= " +valide);
	if(document.application_pi.message_.value=="") {
		if (document.getElementById)
		    {
		   obj=document.getElementById("message_");
		    }
		  else if (document.all)
		    {
		   obj= document.all["message_"];
		    }
		if(obj)
		obj.innerHTML = "Vous devez saisir votre message";
		valide = 'non';
	}
	alert("valide8= " +valide);
	if(!isValideLicence(document.application_pi.numeroLicence.value)) {
		if (document.getElementById)
		    {
		   obj=document.getElementById("numeroLicence");
		    }
		  else if (document.all)
		    {
		   obj= document.all["numeroLicence"];
		    }
		if(obj)
		obj.innerHTML = "Votre Numéro de licence semble être non valide<br>";
		valide = 'non';
	}
	
	alert("valide9= " +valide);
	if(valide=="") return true;
	else return false;
}

function valider_application_pi(){
var valide="";		alert("valide1= " +valide);
	if(document.application_pi.nomSociete.value == "") {
		
		
			$('nomSociete').innerHTML= "Vous devez saisir Vos Nom et Prénom";
		valide = 'non';
	}
	alert("valide2= " +valide);
	if(document.application_pi.adresse.value == "" ) {
		
			$('adresse').innerHTML = "Vous devez saisir votre adresse";
		valide = 'non';
	}
	alert("valide3= " +valide);
	if(!verif_numero_inter(document.application_pi.tel.value)) {
		$("tel").innerHTML = "Votre numéro de téléphone semble être non valide";
		valide = 'non';
	}
	alert("valide4= " +valide);
	if(!verif_numero_inter(document.application_pi.portable.value)) {
		$('portable').innerHTML = "Votre numéro de Portable semble être non valide";
		valide = 'non';
	}
	alert("valide5= " +valide);
	if(!verif_numero_inter(document.application_pi.fax.value)) {
		$('fax').innerHTML = "Votre numéro de Fax semble être non valide";
		valide = 'non';
	}
	alert("valide6= " +valide);
	if(!isValidEmail(document.application_pi.email.value)) {
		$('email').innerHTML = "Votre Email semble être non valide";
		valide = 'non';
	}
	alert("valide7= " +valide);
	if(document.application_pi.message_.value=="") {
		$('message_').innerHTML = "Vous devez saisir votre message";
		valide = 'non';
	}
	alert("valide8= " +valide);
	if(!isValideLicence(document.application_pi.numeroLicence.value)) {
		$('numeroLicence').innerHTML = "Votre Numéro de licence semble être non valide<br>";
		valide = 'non';
	}
	
	alert("valide9= " +valide);
	if(valide=="") return true;
	else return false;
}



