﻿
/**
 * Supprime les vides a gauche
 */
function leftTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	while (s.length > 0 && whitespace.indexOf(s.charAt(0)) != -1) s = s.substring(1);
	
	return s;
}

/**
 * Supprime les vides a droite
 */
function rightTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	while ((l = s.length) > 1 && whitespace.indexOf(s.charAt(l - 1)) != -1) s = s.substring(0, l - 1);

	return s;
}

/**
 * Supprime les vides a droite et a gauche (pas au milieu!)
 */
function trim(str)
{
    return rightTrim(leftTrim(str));
}

/**
 * Fonction, utilisee dans doPays(), permettant d'activer (confirmation = true)
 * ou de desactiver (confirmation = false) les layers Etat/Province
 */
function activate(item, confirmation)
{
	var cache = (confirmation) ? "" : "none";
	if (document.all) {
		document.all('Layer1').style.display = cache;
		document.all('Layer'+item).style.display = cache;
	} else {
		document.getElementById('Layer1').style.display = cache;
		document.getElementById('Layer'+item).style.display = cache;
	}
}

/**
 * Fonction permettant d'afficher l'etat ou province
 * lorsque le pays choisi est USA ou CANADA
 */
function doPays(myform)
{
    if (typeof(myform.state) == "undefined")
    	return;

	var selectedArray = myform.pays.options[myform.pays.selectedIndex].value;

	// Efface tous les Layers
	for (i=2; i<4; i++ )
		activate(i, false);

	if ((selectedArray)=='PUS') {
		activate(2, true);
		myform.cp.value = myform.state.options[myform.state.selectedIndex].value
	}
	else if ((selectedArray)=='PCA')
		activate(3, true);
	else {
		if (myform.cp.value == myform.state.options[myform.state.selectedIndex].value)
			myform.cp.value = "";

		myform.state.selectedIndex = 0;
		myform.province.selectedIndex = 0;
	}
}

function enableTextbox(myform, opt) {
  if (document.layers) {
	// handle NS4
	if (opt.value=="professionnelle" || opt.value=="S")
		myform.raison_sociale.onfocus = null;
	else
		myform.raison_sociale.onfocus = function(){this.blur();};
  } else {
	// handle IE and NS6 
	myform.raison_sociale.disabled = !(opt.value=="professionnelle" || opt.value=="S");
  }
}

function Teste_numero(numero) {
	return true;
}

function getCivilite(myform) {
	return getCiviliteByLangue(myform,'FR');
}

function getCiviliteByLangue(myform, langue) {
	var nom_prenom = "";

	with (myform) {
		var ci = "";

		for (i = 0; i < civilite.length; i++)
			if (civilite[i].checked) ci = civilite[i].value;

		if (ci == "R"){
			if(langue=="FR")nom_prenom = "M ";
			else if(langue=="DE")nom_prenom = "HERR. ";
			else nom_prenom = "MR ";
		}
		else if (ci == "A"){
			if(langue=="FR")nom_prenom = "MME ";
			else if(langue=="DE")nom_prenom = "FRAU. ";
			else nom_prenom = "MRS ";
		}
		else if (ci == "M"){
			if(langue=="FR")nom_prenom = "MLLE ";
			else if(langue=="DE")nom_prenom = "FRAU ";
			else nom_prenom = "MISS ";
		}
		else if (ci == "D"){
			nom_prenom = "DR ";
		}
		nom_prenom += nom.value + " " + prenom.value;
	}
	return nom_prenom;
}

var nbClic = 0;
function compteClic(myform)
{
	++nbClic;
	var verifForm = verif_form(myform);
	if (verifForm && nbClic == 1)
		return true;

	if (!verifForm)
		nbClic = 0;

	return false;
}

var nbClic = 0;
function firstClic()
{
	++nbClic;
	if (nbClic == 1)
		return true;
	return false;
}

function replaceSubstring(inputString, fromString, to_String) {
   // Goes through the inputString and replaces every occurrence of fromString with to_String
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (to_String.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + to_String + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "to_String"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + to_String + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}

function cleanTel(tel){
	var newTel = "";
	newTel = replaceSubstring(tel,"/","");
	newTel = replaceSubstring(newTel," ","");
	newTel = replaceSubstring(newTel,".","");
	newTel = replaceSubstring(newTel,"(","");
	newTel = replaceSubstring(newTel,")","");
	newTel = replaceSubstring(newTel,"-","");
	newTel = replaceSubstring(newTel,"+","");
	return newTel;	
}

function verif_form(myform)
{
	re_numeric = /^[0-9]+$/i;
	var exp_email 	   = new RegExp("^[A-Za-z0-9\-\._]+[@][a-zA-Z0-9\-_]+[A-Za-z0-9\-\._]*[\.][A-Za-z]{2,}$","g");
	var exp_nom_prenom = new RegExp("^[-'\. A-Z]{1,25}$","g");
	var exp_carte_mouv = new RegExp("^925001[0-9]{10}$","g");
	var exp_annee	   = new RegExp("^(1|2)(9|0)[0-9][0-9]$","g");
	var exp_tel	   = new RegExp("^[+ (./)0-9-]*$","g");
	var exp_re_cp	   = new RegExp("^[-'\. 0-9a-zA-Z]*$","g");	
	var exp_re_raison  = new RegExp("^[-'\.ÉÛ()+ &/,0-9A-Z]{1,25}$","g");

	
	with (myform) {
		if (nom) {
			nom.value = trim(nom.value).toUpperCase();
			if (nom.value.length > 25){alert(nom_v);nom.focus();return false;}
			if (!exp_nom_prenom.test(nom.value)) {alert(nom_l);nom.focus();return false;}
		}

		if (prenom) {
			prenom.value = trim(prenom.value).toUpperCase();
			if (prenom.value.length > 25){alert(prenom_v);prenom.focus();return false;}
			if (prenom.value.match(exp_nom_prenom) == null) {alert(prenom_l);prenom.focus();return false;}
		}
        	
		if(soc_adresse) {
			soc_adresse.value = trim(soc_adresse.value)
			if(soc_adresse.value.length > 32)	{alert(adresse_l);soc_adresse.focus();return false;}
		}

        if (codepostal) {
			codepostal.value = trim(codepostal.value)
			if (codepostal.value == "") {alert(cp_v); codepostal.focus(); return false;}
			if (codepostal.value.match(exp_re_cp) == null) {alert(cp_t); codepostal.focus(); return false;}
			if (codepostal.value.length > 32) {alert(cp_l); codepostal.focus(); return false;}
			if (codepostal.value.length < 3 ) {alert(cp_c); codepostal.focus(); return false;}
		}
		
		if(soc_ville){
			soc_ville.value = trim(soc_ville.value)
			if(soc_ville.value.length > 32)	{alert(ville_l);soc_ville.focus();return false;}
		}
		
		if(pays) {
			if(pays.options[pays.selectedIndex].value==""){
				alert(pays_s);
				pays.focus();
				return false;
			}
		}

		if (telephone) {
			if(telephone.value!=""){
				telephone.value = trim(telephone.value);
				if (telephone.value.match(exp_tel) == null) {alert(tel_pro_t);telephone.focus();return false;}
				if(telephone.value.length > 20) {alert(tel_pro_l);telephone.focus();return false;}
				telephone.value = cleanTel(telephone.value);
			}
		}
		
		if (fax) {
			if(fax.value!=""){
				fax.value = trim(fax.value);
				if (fax.value.match(exp_tel) == null) {alert(fax_t);fax.focus();return false;}
				if(fax.value.length > 20)	       {alert(fax_l);fax.focus();return false;}
				fax.value = cleanTel(fax.value);
			}
		}

		if (email) {
			email.value = trim(email.value);
	            	if (!exp_email.test(email.value)) {alert(email_t);email.focus();return false;}
	            	if (email.value.length > 111)	 {alert(email_l); email.focus(); return false;}
		}
  	}
  return true;
}

// Fonction qui va tester si des caracteres interdits
// se trouvent dans une chaine de caractere
function check_textarea(target, message) {
    //verbotten = "<,>,&,\""
    var exp = new RegExp("((<|>|&|\"))","g");
    mystring = target.value;
    if( exp.test(mystring) ) {
        alert( message ) ;
        target.focus();
    }
} 

// Test la cle Luhn d'un numero de carte
function verifCleLuhn(numCarte){
	  var numero = numCarte.value;
	  var totalcarte=0;
	  var dnum=0;
	  var test=0; 
	  if (numero.length < 13) {
		alert(nbreChiffreIncorrect);
		numCarte.focus();
		return false;
	  }
	  else{
		  for ( i = numero.length; i >= 1 ;  i--){
		    	test=test+1;
			num = numero.charAt(i-1);
		    	if ((test % 2) != 0) totalcarte=totalcarte+parseInt(num)
		    	else {
		       		dnum=parseInt(num)*2;
		       		if (dnum >= 10) totalcarte=totalcarte+1+dnum-10
		       		else totalcarte=totalcarte+dnum;
		    	}
	   
	  	 }
	  	 if ((totalcarte % 10) != 0){
	       		alert(numeroIncorrect);
	       		numCarte.focus();
	       		return false;
	  	 }
	  	 else{
	       		return true;
	  	 }
	 }
}

//Fidelisation conversion (AFG ibis+AFG) : Fonction qui actualise le formulaire de conversion en fonction de la recompense selectionnee
function updateConvForm(){
	
	var currentForm = document.cli_conv_acc; 
	var nbElement = 0;
	if(currentForm.typeRecompense.length!=undefined){
		for (var i=0; i<currentForm.typeRecompense.length;i++) {
			var currentRecompense = currentForm.typeRecompense[i];
	        	if (currentRecompense.checked) {
	            		eval('document.cli_conv_acc.nbreCheque_'+currentRecompense.value+'.disabled = false;');
	         	}
	         	else{
	         		eval('document.cli_conv_acc.nbreCheque_'+currentRecompense.value+'.selectedIndex = 0;');
	         		eval('document.cli_conv_acc.nbreCheque_'+currentRecompense.value+'.disabled = true;');
	         	}
	         	nbElement++;
	        }
        }
        else{
        	eval('document.cli_conv_acc.nbreCheque_'+currentForm.typeRecompense.value+'.disabled = false;'); 	
        }
}

//Fidelisation conversion (AFG ibis+AFG) : Fonction qui actualise le formulaire de conversion en fonction de la recompense selectionnee
function updateConvForm(myform){
	
	var nbElement = 0;
	if(myform.typeRecompense.length!=undefined){
		for (var i=0; i<myform.typeRecompense.length;i++) {
			var currentRecompense = myform.typeRecompense[i];
	        	if (currentRecompense.checked) {
	            		eval('myform.nbreCheque_'+currentRecompense.value+'.disabled = false;');
	         	}
	         	else{
	         		eval('myform.nbreCheque_'+currentRecompense.value+'.selectedIndex = 0;');
	         		eval('myform.nbreCheque_'+currentRecompense.value+'.disabled = true;');
	         	}
	         	nbElement++;
	        }
        }
        else{
        	eval('myform.nbreCheque_'+myform.typeRecompense.value+'.disabled = false;'); 	
        }
}

//Fidelisation mouvango : Fonction qui actualise grise ou degrise le choix de la langue des newsletter
function updateMouvangoAdhForm(myform){
	if(myform.demande_communication.checked == 1){
		for (var i=0; i<myform.communication_lang.length;i++) {
			myform.communication_lang[i].disabled = false;
		}
	}
	else{	
		for (var i=0; i<myform.communication_lang.length;i++) {
			myform.communication_lang[i].disabled = true;
		}		
	}
}

