function changeObjetMessage(oSelect,cas){
  //on change de page si on est dans qconso, et qu on choisit autre que qconso
  if (oSelect[oSelect.selectedIndex].value != "qconso" & cas == "qconso" ){
    switchToContact(oSelect[oSelect.selectedIndex].value);
  }
  //on change de page si on est dans contact, et qu on choisit qconso
  if (oSelect[oSelect.selectedIndex].value == "qconso" && cas =="contact" ){
    switchToQuestionConso();
  }
}
function switchToContact(cas){

  var str = 'pages-connexes/contact.html?tx_uges_pi3[action]=main&tx_uges_pi3[cas]=' + cas + '';

  if ( navigator.appName=="Microsoft Internet Explorer" )
    str = 'contact.html?tx_uges_pi3[action]=main&tx_uges_pi3[cas]=' + cas + '';

  window.location.replace(str);
}
function switchToQuestionConso(){
  var str = 'pages-connexes/contact.html?tx_uges_pi3[action]=qconso';

  if ( navigator.appName=="Microsoft Internet Explorer" )
    str = 'contact.html?tx_uges_pi3[action]=qconso';

  window.location.replace(str);
}


function verif(form)
{
 var f = form;

  if (f.email.value == "")
  { alert("Merci de préciser votre email pour obtenir une réponse dans les plus brefs délais");
    f.email.focus();
	return false;
  } else
  { if (!checkEmail(f.email)) return false;
  }

  var objet = f.objet;
  if (objet.options[objet.selectedIndex].value == "0")
  { alert("Veuillez sélectionner un objet pour obtenir une réponse dans les plus brefs délais");
    objet.focus();
	return false;
  }

  var localisation = f.localisation;
  if (objet.options[objet.selectedIndex].value == "1")
  { // sélection obligatoire du magasin
     if (localisation.options[localisation.selectedIndex].value == "0")
    { alert("Veuillez sélectionner une localisation pour obtenir une réponse dans les plus brefs délais");
	  localisation.focus();
	  return false;
    }
  }

  for (var j=0; j < f.elements.length; j++)
  { var formAct = f.elements[j];
    if (formAct.type == "textarea")
	{ valeur = formAct.value;
	  taille = valeur.length;
	  //alert ('nom ' + formAct.name + ', value ' + valeur + ', taille ' + taille + ', max/min ' + formAct.maxi + '/' + formAct.mini );
	  if (Number(taille) > Number(formAct.maxi))
	  { alert("La zone '" + formAct.nom + "' est trop longue (" + taille + " au lieu de " + formAct.maxi + ")");
	    return false;
	  }
	  if (Number(taille) < Number(formAct.mini))
	  { alert("Merci de préciser votre " + formAct.nom + " pour obtenir une réponse dans les plus brefs délais");
	    formAct.focus();
	    return false;
	  }
	}
  }

  // taille des champs adresse, ville, complements, nom du produit au  moins égale à 2
  if ( f.adresse.value.length < 2) {
	 alert("L'adresse n'est pas valide.");
	 f.adresse.focus();
	 return false;
  }

  if ( f.complement.value.length > 0 && f.complement.value.length < 2) {
	 alert("Le complement d'adresse n'est pas valide.");
	 f.complement.focus();
	 return false;
  }

  if ( f.ville.value.length < 2) {
	 alert("La ville n'est pas valide.");
	 f.ville.focus();
	 return false;
  }
  if ( f.design_produit.value.length < 2) {
	 alert("La désignation du produit n'est pas valide.");
	 f.design_produit.focus();
	 return false;
  }

  // on rajoute un check du message
  if ( f.message.value.length < 10) {
	 alert("Merci de préciser votre message de manière la plus explicite possible, pour obtenir une réponse dans les plus brefs délais");
	 f.message.focus();
	 return false;
  }

//  return false;
}

function valideEan(ean)
{
  var reg=new RegExp("( )", "g");
  ean = ean.replace(reg,'');

  if (ean.length == 0)
  {
  	return true;
  }

  if (ean.length != 13)
  {
    return false;
  }


  // Contrôle EAN poids/prix
  if (ean.indexOf('02') == 0
      || ean.indexOf('2040') == 0 || ean.indexOf('2041') == 0 || ean.indexOf('2042') == 0 || ean.indexOf('2044') == 0 || ean.indexOf('2045') == 0 || ean.indexOf('2046') == 0 || ean.indexOf('2047') == 0 || ean.indexOf('2049') == 0
      || ean.indexOf('205') == 0
      || ean.indexOf('2070') == 0 || ean.indexOf('2071') == 0 || ean.indexOf('2072') == 0 || ean.indexOf('2073') == 0 || ean.indexOf('2076') == 0 || ean.indexOf('2077') == 0 || ean.indexOf('2078') == 0 || ean.indexOf('2079') == 0
      || ean.indexOf('21') == 0 || ean.indexOf('22') == 0 || ean.indexOf('23') == 0 || ean.indexOf('24') == 0 || ean.indexOf('25') == 0 || ean.indexOf('26') == 0 || ean.indexOf('27') == 0 || ean.indexOf('28') == 0 || ean.indexOf('29') == 0
      )
  {
    return true;
  }

  // calcule du dernier caractère.
  var somme = 0;
  var c;
  for(i=0; i<12; i++)
  { somme += (ean.charCodeAt(i) - 48);
    i++;
    somme += 3 * (ean.charCodeAt(i) - 48);
  }
  somme = new String(somme);
  somme = somme.charAt(somme.length-1);
  somme = parseInt(somme);
  if (somme != 0)
    somme = 10 - somme;
  var clef = new String(somme);
  if (clef != ean.charAt(12))
  {
    return false;
  } else {
    return true;
  }
  return false;
};

function checkEmail(oInput) {
  var val = oInput.value;
  var navigateur = new String(navigator.appName);
//  if (navigateur.toLowerCase() != 'microsoft internet explorer')
//    val = val.replace(/[&$% ;,+*/]/gi, "");
  oInput.value = val;
  var newString = val.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (val.length > 0) {
     if (!newString) {
	   alert("l'adresse email renseignée est invalide !");
  	   oInput.style.color = "#FF0000";
	   oInput.focus();
	   return false;
	 }
  }
  oInput.style.color = "#818181";
  return true;
}

function affMag()
{ var f = document.formu;
  if (f.objet.selectedIndex == 1)
  { f.localisation.style.visibility = 'visible';
    document.all.info.innerHTML = 'Magasin Cora concern&eacute; par votre demande';
  } else
  { f.localisation.style.visibility = 'hidden';
    document.all.info.innerHTML = '';
  }
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
};
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
};
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
};
function verifEmail(mail) {
    expression = new RegExp("[a-zA-Z0-9-.]+\@[a-zA-Z0-9-.]+");
    return expression.test(mail);
};
function verifTelephone(tel) {
    expression = new RegExp("[0-9]{10}");
    return expression.test(tel);
};
function verifCp(cp) {
    expression = new RegExp("[0-9]{5}");
    return expression.test(cp);
};
//=======================================================================================//
//= function isEan13(ean)                                                               =//
//=   params : ean -> ean &agrave; contrôler                                                   =//
//=   retour : true si l'ean est valide                                                 =//
//=======================================================================================//
// version 1.00.00
function isEan13(ean)
{
  // taille de l'ean (13 caractères)
  if (ean.length != 13)
    return false;

  // calcule du dernier caractère.
  var somme = 0;
  var c;
  for(i=0; i<12; i++)
  { somme += (ean.charCodeAt(i) - 48);
    i++;
    somme += 3 * (ean.charCodeAt(i) - 48);
  }
  somme = new String(somme);
  somme = somme.charAt(somme.length-1);
  somme = parseInt(somme);
  if (somme != 0)
    somme = 10 - somme;
  var clef = new String(somme);
  if (clef != ean.charAt(12))
    return false;
  else
    return true;
}

function checkFormulaire(oForm){
	try{
	var civilite = -1;
  	for (i=0;i<3;i++){
       if (oForm['civilite'][i].checked){
        civilite = oForm['civilite'][i].value;
       }
    }
		var nom = trim(oForm['nom'].value);
		var prenom = trim(oForm['prenom'].value);
		var email = trim(oForm['email'].value);
		var adresse = trim(oForm['adresse'].value);
		var codepostal = trim(oForm['cp'].value);
		var ville = trim(oForm['ville'].value);
		var message = trim(oForm['message'].value);
		var ean = trim(oForm['code_barre'].value);
		var telephone = trim(oForm['telephone'].value);
		var complement = trim(oForm['complement'].value);
        var design_produit = trim(oForm['design_produit'].value);
		var erreur = '';
		var obligatoire = 'Merci de préciser :\n';
		var lngInitObligatoire = obligatoire.length;
		var incorrecte = "Merci de corriger :\n";
		var lngInitIncorrect = incorrecte.length;
		if (civilite == -1){
			obligatoire += '- votre titre\n';
		}
		if (nom == ''){
			obligatoire += '- votre nom\n';
		}
		if (prenom == ''){
			obligatoire += '- votre prénom\n';
		}
		if (email == ''){
			obligatoire += '- votre e-mail\n';
		} else{
			if (!verifEmail(email)){
				incorrecte += '- votre adresse email\n';
			}
		}
		if (adresse == ''){
			obligatoire += '- votre adresse\n';
		} else if ( adresse.length < 2) {
             incorrecte +="- votre adresse\n";
        }

        if ( complement.length > 0 && complement.length < 2) {
           incorrecte +="- votre complement d\'adresse\n";
        }

		if (codepostal == ''){
			obligatoire += '- votre code postal\n';
		} else{
			if (!verifCp(codepostal)){
				incorrecte += '- votre code postal\n';
			}
		}
		if (ville == ''){
			obligatoire += '- votre ville\n';
		}
        else if ( ville.length < 2) {
             incorrecte +="- votre ville\n";
        }
		if (telephone != '' && !verifTelephone(telephone)){
			incorrecte += '- votre numéro de téléphone\n';
		}
		if (message == ''){
			obligatoire += '- votre message\n';
		} else{
			var entrop = message.length - 512;
			if (entrop > 0){
				incorrecte += '- votre message est trop long. Limite à 512 caractères. Vous en avez '+entrop+' en trop.\n';
			}
		}
		if (ean != '' && !valideEan(ean)) {
			incorrecte +="- le code barre saisi\n";
		}

      if ( design_produit.length < 2) {
         incorrecte +="- la désignation du produit\n";
      }


		if (lngInitObligatoire != obligatoire.length){
      erreur = obligatoire + erreur;
    }
		if (lngInitIncorrect != incorrecte.length){
      erreur = erreur + incorrecte;
    }
		if (erreur != ''){
			alert(erreur);
			return false;
		}else{
			return true;
		}
	}catch(e){
		alert(e);
		return false;
	}
}
function voirDlc(){
	document.getElementById('aide').innerHTML = '<div style="width:225px;background-color:#ACD8E8">DLC est la Date Limite de Consommation, DLUO correspond à la Date Limite d\'Utilisation Optimale.</div>';
	document.getElementById('aide').style.visibility= 'visible';
//	return overlib('<div style="width:225px;background-color:#ACD8E8">DLC est la Date Limite de Consommation, DLUO correspond à la Date Limite d\'Utilisation Optimale.</div>', FULLHTML,ABOVE);
return;
}
function voirNumLot(){
	document.getElementById('aide').innerHTML = '<img src="fileadmin/portail/contact/num_de_lot.jpg" width="142" height="54"  />';
	document.getElementById('aide').style.visibility= 'visible';
//	return overlib('<img src="/images/general/num_de_lot.jpg" width="142" height="54" />', FULLHTML,ABOVE);
return;
}
function voirCodeBarres(){
	document.getElementById('aide').innerHTML = '<img src="fileadmin/portail/contact/code_a_barre.jpg" width="136" height="90" />';
	document.getElementById('aide').style.visibility= 'visible';
//	return overlib('<img src="/images/general/code_a_barre.jpg" width="136" height="90" />', FULLHTML,ABOVE);
return;
}
function voirFaq(){
  options = 'width=700,height=450,scrollbars=yes';

  if ( navigator.appName=="Microsoft Internet Explorer" )
	  window.open('../index.php?id=96','faq',options)
  else
          window.open('index.php?id=96','faq',options);
  return false;
}
function nd(){
	document.getElementById('aide').innerHTML = '';
	document.getElementById('aide').style.visibility= 'hidden';
}
