//
//	tanitcmnfncs.js : common javascript functions used in this site
//

// --------- code common to all pages ---------------
  function domInits() {  	 
	   ajaxGetBasketPrdCount();
	   center_inits();  
  }
  
  jQuery( domInits );
  
	// fix png problems in MS-IE 5.x and 6
	 if (document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) ) {
		jQuery(document).ready(function(){ 
			jQuery(document).pngFix(); 
		});
	}

// --------- code from topMenu.php -----------
function  doAskDevis() { 
  
  var lurl ="/site/askDevis/askDevis.php?keepThis=true&TB_iframe=true&height=700&width=600";
  var titre ="Saisissez vos d&eacute;tails"

  //show in new browser window
  window.open(lurl,"Devis","scrollbars=yes");
}  

// --------- code from TopBanner.php --------------
function validateEmail(eml){
  var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  str = eml;
  if(str.match(emailRegEx)){
    return true;
  }else{
    return false;
  }
}

function xfaireDevis() {
  msg = "Veuillez entrer votre adresse Email :";
  eml = prompt(msg);
    //1.valider l'adresse email
      if (!validateEmail(eml) ) {
          alert("Veuillez saisir un compte Email valide!");
      } else {       
        //2.generer le devis
        alert("Vous allez recevoir prochainement votre devis.");
        //document.location.href="/Devis/?eml="+eml;
     }
}


function faireDevis() {
  msg = "Pour recevoir un devis, connectez-vous sur notre site. \n\n Allez dans l'espace client et identifiez-vous; Sinon ouvrez un nouveau compte client afin de vous inscrire.";
  
      //1.validate client connectd
          //       if ( ( ClntDenom == "" ) && ( ClntId == "" ) ) {
          //           alert(msg);
          //           return;
          //       }  
  
      msg = "Pour faire un devis vous devez ajouter des articles dans votre panier.";
      //2. validate none empty basket
      if ( nbrElemsInOrder == 0) {
        alert(msg);
        return;
      }
  
      //3. If role is  "admin" then ask for final client name ex. M. Malek Fallouh
      if (usrRole == "admin") {
        msg = "Veuillez entrer (Civilité Nom Prénom) du client final) :";
        ClntDenom = prompt(msg);
      }
      
      //4. otherwise : generate  devis
      ////alert("Vous allez recevoir prochainement votre devis.");
      if (ClntDenom == "") ClntDenom="Inconnu";
      lurl = "/Devis/"+ClntDenom+"/";//points to /site/devis.php
      window.open(lurl);
    
}

// Code de la page : produits.php
 
	function initPageTitles(Categ){ // init corresponding PageTitle Image
		var el = document.getElementById('PageTitle');		
		el.style.display='none';//alert(Categ);		
		var img_du_titre = "";
		Catg = Categ;
		if (Catg == "") Catg = window.location.pathname;
		tCatg = ""; //alert(Catg);
		if (Catg.indexOf('Shirt') > -1) tCatg = 'Tee-Shirt';
		if (Catg.indexOf('Polo') > -1)  tCatg = 'Polo';
		if (Catg.indexOf('Enfant') > -1) tCatg = 'Enfant';
		if (Catg.indexOf('Chemise') > -1) tCatg = 'Chemise';
		if (Catg.indexOf('Polaire') > -1) tCatg = 'Polaire';
		if (Catg.indexOf('Sweatshirt') > -1) tCatg = 'Sweatshirt';
		if (Catg.indexOf('Blouson') > -1) tCatg = 'Blouson';
		if (Catg.indexOf('Parka') > -1) tCatg = 'Parka';
		if (Catg.indexOf('Coupe-vent') > -1) tCatg = 'Coupe-vent';
		if (Catg.indexOf('Body-warmer') > -1) tCatg = 'Body-warmer';
		
		switch (tCatg) {
			case "Tee-Shirt" : 
				img_du_titre = "TeeShirtsPageTitle.png";
				break;
				
			case "Polo" : 
				img_du_titre = "PolosPageTitle.png";
				break;

			case "Enfant" : 
				img_du_titre = "EnfantsPageTitle.png";			
				break;
										
			case "Chemise" : 
				img_du_titre = "ChemisesPageTitle.png";			
				break;
				
			case "Polaire" : 
				img_du_titre = "PolairesPageTitle.png";			
				break;				
				
			case "Sweatshirt" : 
				img_du_titre = "PolairesPageTitle.png";			
				break;
				
			case "Blouson" : 
				img_du_titre = "PolairesPageTitle.png";			
				break;

			case "Parka" : 
				img_du_titre = "OutDoorsPageTitle.png";			
				break;				
				
			case "Coupe-vent" : 
				img_du_titre = "OutDoorsPageTitle.png";			
				break;
				
			case "Body-warmer" : 
				img_du_titre = "OutDoorsPageTitle.png";			
				break;
				
			case "Casquette" : 
				img_du_titre = "";			
				break;
				
			case "Bags" : 
				img_du_titre = "";			
				break;																								
		}
		
		if (img_du_titre != "") {
			el.style.backgroundImage = 'url(/image/'+img_du_titre+')';
			el.style.display='block';
		}
	}

// Code de la page : saisiecoordcmde.php

function saisiecoordcmde_frmSubmit(lfrm){
 var frm = document.frm1;
 if(!isFormValid()) { 
	alert('Veuillez remplir les champs obligatoires!');
	return false; 
 } else {
	///alert('Merci, nous allons répondre dans le plus bref délai.');
	document.frm1.action="/CnfrmInfoCmde/";
	document.frm1.method="post";
	document.frm1.submit();	
 } 
 return false;
};

function saisiecoordcmde_ajaxChkEmail(emailobj) {
  if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = saisiecoordcmde_showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte Email est invalide!"); 
    }
  }
};

function saisiecoordcmde_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; //  alert('showEmailEval :'+rep);	
	if (rep == "invalide") {
		alert("L&#039;adresse Email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	} else { // email ok but can be already in clnt db tbl ==> cant be used or visiter should authenticate
		delete ajax;
		ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/ajaxIsEmailInClntTbl.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = saisiecoordcmde_ctrledClntEmailEntry;	//func. that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function			
	}
};

function saisiecoordcmde_ctrledClntEmailEntry() {// if email already in clnt db tbl ==> cant be used or visiter should authenticate
	var emailobj = document.getElementById('email');
	rep = ajax.response; // alert('Rep:isEmailInClntDbTbl :'+rep);	
	/* Eviter le ctrl clnt deja existant ; demande par tanit le 05/06/2009
	if (rep == "invalide") {
		alert("L'adresse e-mail << " + emailobj.value + " >> nécessite une authentification dans l'espace client,\n\n veuillez vous authentifier, puis reprenez le panier afin de valider votre commande.\n\n\n Sinon, utilisez un autre compte email pour cette commande.");
		emailobj.value = "";
		emailobj.focus();
	}
	*/
	delete ajax;
}

// Code de la page : cnfrminfocmde.php

//var IE = navigator.appVersion.indexOf("MSIE")!=-1;
var ajax = null;

function cnfrminfocmde_frmSubmit(lfrm,payingMethod){
 var frm = document.frm1;
 if(frm.acceptCndVnt.checked != true) { 
	alert('Veuillez accepter nos conditions de vente afin de finaliser votre commande!');
	return false; 
 } else {
	///alert('Merci, nous allons répondre dans le plus bref délai.');
    document.frm1.payMethod.value = payingMethod; //Cb or check	
	document.frm1.action= "/ClntCmdeValidee/";   ; //for tests : "/site/Nclntcmdevalidee.php";   
	document.frm1.method="post";
	document.frm1.submit();		
 } 
 return false;
};

function cnfrminfocmde_ajaxChkEmail(emailobj) {
  if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte Email est invalide!"); 
    }
  }
};

function cnfrminfocmde_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; //  alert('showEmailEval :'+rep);	
	if (rep == "invalide") {
		alert("L&#039;adresse Email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	}
	delete ajax;
};

// Code de la page : votreprofile.php

function votreprofile_frmSubmit(lfrm){
 var frm = document.frm1;
 if(!isFormValid()) { 
	alert('Veuillez remplir les champs obligatoires!');
	return false; 
 } else {
  document.frm1.abonn.value = ( document.frm1.news.checked? 1:0);
	alert('Merci, nous allons r&eacute;pondre dans le plus bref d&eacute;lai.');
	document.frm1.action="/site/modifprofile.php"
	document.frm1.method="post";
	document.frm1.submit();	
 } 
 return false;
};

function votreprofile_ajaxChkEmail(emailobj) {
  if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset();       		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to call
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = votreprofile_showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte Email est invalide!"); 
    }
  }
};

function votreprofile_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; 
	if (rep == "invalide") {
		alert("L&#039;adresse Email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	}
	delete ajax;
};

function votreprofile_deleteClientProfile(){
    msg = "Etes-vous s&ucirc;r de vouloir supprimer votre profil/compte client?\n\n" +
          "Sachez que pour tout nouvel achat sur notre site,\n vous devez ouvrir un nouveau compte client."
    res = confirm(msg);
    if (res == true) {
    	document.frm1.action="/site/deleteprofile.php"
    	document.frm1.method="post";
    	document.frm1.submit();        
    }
	return false;
}

// Code de la page : pwdlost.php

function pwdlost_frmSubmit(lfrm){
 var frm = document.frm1;
 if(!isFormValid()) { 
	alert('Veuillez remplir les champs obligatoires!');
	return false; 
 } else {
  alert('Merci, nous allons r&eacute;pondre dans le plus bref d&eacute;lai.');
	document.frm1.action="/PWDLOST/"; //"/PWDLOST/mdpperdu.php"
	document.frm1.method="post";
	document.frm1.submit();	
 } 
 return false;
};

function pwdlost_ajaxChkEmail(emailobj) {
   if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset();    		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to call
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = pwdlost_showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte Email est invalide!"); 
    }
  }
};

function pwdlost_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; 	
	if (rep == "invalide") {
		alert("L&#039;adresse Email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	}
	delete ajax;
};

// Code de la page : suivicmdes.php

function suivicmdes_ajaxChkCmdeStatus(numCmde){
  if (numCmde.value != ""){
  	if (numCmde.value.match(formValidationMasks['numeric']) ) { // we can now check status
  		ajax = new sack();
  		ajax.reset();
  		ajax.requestFile = '/site/ajaxChkCmdeStatus.php';// Specifying which file to get
  		ajax.setVar("numCmde",numCmde.value);
  		ajax.onCompletion = suivicmdes_showCmdeStatus;// Specify function that will be executed after file has been found
  		ajax.runAJAX();// Execute AJAX function
  	}
  	else {
      alert("Le format du num&eacute;ro de commande/devis est invalide!");
    }
  }
};

function suivicmdes_showCmdeStatus(){
var frm = document.frm1;
	var cmdeStatus = frm.statuscmde;
	cmdeStatus.value = "";
	rep = ajax.response;
	if (rep == "invalide") {
		alert("Le num&eacute;ro << " + frm.numcmde.value + " >> est introuvable, veuillez recommencer!");
		cmdeStatus.value = "";
		frm.numcmde.focus();
	} else {
        cmdeStatus.value = rep;
		var btn = document.getElementById('btnCnfrmDevis');
		if (rep.indexOf('Devis') > 0){
 			btn.style.visibility = 'visible';
		} else btn.style.visibility = 'hidden';// not a Devis
       }
	delete ajax;
};

function suivicmdes_frmSubmit(lfrm){
var frm = document.frm1;
if (frm.numcmde.value == ""){
 	alert('Veuillez entrer le num&eacute;ro de commande/devis en question!');
 	return false;
 }
if(!isFormValid()){
	alert('Veuillez remplir les champs obligatoires!');
	return false;
 } else { // alert('Merci, nous allons répondre dans le plus bref délai.');
    //make the ajax call to get cmde status
    suivicmdes_ajaxChkCmdeStatus(frm.numcmde);
 } 
 return false;
};
var ajax = null;

// Code de la page : creecompteclnt.php

function creecompteclnt_frmSubmit(lfrm){
 var frm = document.frm1;
 if(!isFormValid()) { 
	alert('Veuillez remplir les champs obligatoires!');
	return false; 
 } else {
  msg = "Merci de votre inscription.\n\n Veuillez consulter votre boite email pour les d&eacute;tails\n\n"+
        " de votre nouveau compte sur Tanit eBoutique.";
	alert(msg);
	document.frm1.action="/NOUVEAUCOMPTE/nouveaucompte.php"
	document.frm1.method="post";
	document.frm1.submit();	
 } 
 return false;
};

function creecompteclnt_ajaxChkEmail(emailobj) {
  if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = creecompteclnt_showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte email est invalide!"); 
    }
  }
};

function creecompteclnt_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; //  alert('showEmailEval :'+rep);	
	if (rep == "invalide") {
		alert("L&#39;adresse email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	} else { // email ok but can be already in clnt db tbl ==> cant be used or visiter should authenticate
		  delete ajax;
		  ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/ajaxIsEmailInClntTbl.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = creecompteclnt_ctrledClntEmailEntry;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function			
	}	
};

function creecompteclnt_ctrledClntEmailEntry() {// if email already in clnt db tbl ==> cant be used or visiter should authenticate
	var emailobj = document.getElementById('email');
	rep = ajax.response; // alert('Rep:isEmailInClntDbTbl :'+rep);	
	if (rep == "invalide") {
		alert("L&#039;adresse email << " + emailobj.value + " >> n&eacute;cessite une authentification dans l&#039;espace client,\n\n veuillez vous authentifier, puis reprenez vos achats.\n\n\n Sinon, utilisez un autre email pour ouvrir un nouveau compte Tanit eBoutique.");
		emailobj.value = "";
		emailobj.focus();
	}
	delete ajax;
}

// ////////////////////Poser Votre Question //////////////////////////


function poservotrequestion_frmSubmit(lfrm){
 var frm = document.frm1;
 if (frm.question.value == "") {
 	alert('Veuillez formuler votre question!');
 	return false; 
 }
 if(!isFormValid()) { 
	alert('Veuillez remplir les champs obligatoires!');
	return false; 
 } else {
	alert('Merci, nous allons r&eacute;pondre dans les plus brefs d&eacute;lais.');
	document.frm1.action="/NOUVEAUQUEST/nouveauquestion.php"
	document.frm1.method="post";
	document.frm1.submit();	
 } 
 return false;
};



function poservotrequestion_ajaxChkEmail(emailobj) {
  if (emailobj.value != "") {      	
  	if (emailobj.value.match(formValidationMasks['email']) ) { // we can now check real email existance
  		ajax = new sack();
  		ajax.reset(); //   alert('ajaxChkEmail');       		
  		ajax.requestFile = '/site/evalemail.php';	// Specifying which file to get
  		ajax.setVar("eml",emailobj.value);
  		ajax.onCompletion = poservotrequestion_showEmailEval;	// Specify function that will be executed after file has been found
  		ajax.runAJAX();		// Execute AJAX function		
  	}
  	else { 
      alert("Le format du compte email est invalide!"); 
    }
  }
};

function poservotrequestion_showEmailEval(){
	var emailobj = document.getElementById('email');
	rep = ajax.response; //  alert('showEmailEval :'+rep);	
	if (rep == "invalide") {
		alert("L&#39;adresse email << " + emailobj.value + " >> est inconnue, veuillez recommencer!");
		emailobj.value = "";
		emailobj.focus();
	}
	delete ajax;
};



function poservotrequestion_initFormValues() {
var frm = document.frm1;
    slctval = slctvalue;
    if ( slctval != "") {
      for (i=0; i < frm.civilite.options.length; i++) {
        if (frm.civilite.options[i].value == slctval)
            frm.civilite.options[i].selected = true;
      }
    }
    frm.nom.value= usrNom;
    frm.prenom.value= usrPrenom;
    frm.email.value= usrEmail;
    frm.adresse.value= usrAdr;
    frm.codepostal.value= usrCp;    
    frm.ville.value= usrVille; 
	//validate filled fields
	for (i=0; i < frm.elements.length; i++) {
	  if ((frm.elements[i].parentNode.className=='invalidInput' ) && 
	  	( (frm.elements[i].value.length >= 2) || (frm.elements[i].selectedIndex > 0) )   )
           				frm.elements[i].parentNode.className='validInput';
    }	         
}






// ////////////////////////////////////////////////////////////////////////////
/*
  $(document).ready(  function(){
    var browser;
    if($.browser.mozilla)
      browser = "Firefox";
    else if($.msie)
      browser = "MSIE";
    else if($.browser.opera)
      browser = "Opera";
    else if($.browser.safari)
      browser = "Safari";
    else {
      browser =  "Unknown";
      }
	  if (browser == "Safari") 
		browser = navigator.userAgent.match(/Chrome/)? "Chrome" : "Safari";
	  if (browser == "Unknown") 
		browser = navigator.userAgent.match(/MSIE/)? "MSIE" : "Unknown";
	  if (browser == "Unknown") 
		browser = navigator.userAgent.match(/Firefox/)? "Firefox" : "Unknown";
    //alert("Browser : " +browser + " Real name : " + navigator.userAgent);
  });
*/