/*
Fonctions Javascript pour Site Internet

Copyright (c) YZEO 2005,2006,2007
Créé le 10/05/2005 par Flavien BUCHETON

Dernière modification le 05/10/2007

David cartier :

rajout d'une fonction de détection de numéros de tel valide: is_tel_or_not(mon_tel,message);
*/

function is_tel_or_not(obj,message,usage){
	mon_tel = obj.value;
	var number_ok = false;
	if(usage == "facultatif" && mon_tel==""){
		return true;
	}
	long_mon_tel = mon_tel.length;
	var tab_caractaire_autorised = ['0','1','2','3','4','5','6','7','8','9'," "];
	for (var i =0;i<long_mon_tel;i++){
		var caractaire_en_cours = mon_tel.substr(i,1);
		
		var test_c = false;
		for (var j=0;j<tab_caractaire_autorised.length;j++){
			if(tab_caractaire_autorised[j] == caractaire_en_cours){
				test_c = true;
				j = tab_caractaire_autorised.length;
			}
		}		
		
		if(i == 0 && caractaire_en_cours == "+"){
			test_c = true;
		}
		
		if(test_c == false){
			number_ok = false;
			alert(message);
			return number_ok;
		}else{
			number_ok = true;
		}
	}
	return number_ok;
}

function myOpen(theURL,winName,features) {
  w=window.open(theURL,winName,features);
}

function estPresent(obj, nom, taille, type) {
	return maFonction(obj, nom, taille, type, true);
}

function estValide(obj, nom, taille, type) {
	return maFonction(obj, nom, taille, type, false);
}

function maFonction(obj, nom, taille, type, presence) {
	
	if (presence && obj.value == "") {
		alert(nom);
		obj.focus();
		return false;
	}
	if (taille != -1 && obj.value.length > taille) {
		alert("Le champ '" + nom + "' est trop long.\nMaximum " + taille + " caractères.");
		obj.focus();
		obj.select();
		return false;		
	}
	if (obj.value != "" && type == "int") {
		var temp = parseInt(obj.value);
		if (isNaN(temp)) {
			alert("Le champ '" + nom + "' n'est pas un nombre entier.");
			obj.focus();
			obj.select();
			return false;
		}
		obj.value = temp
	}
   else if (obj.value != "" && type == "intPositif") {
		var temp = parseInt(obj.value);
		if (isNaN(temp)) {
			alert(nom);
			obj.focus();
			obj.select();
			return false;
		}
		else if (temp < 0) {
			alert(nom);
			obj.focus();
			obj.select();
			return false;
		}
		obj.value = temp
	}
	else if (obj.value != "" && type == "float") {
		var reg = RegExp(" ","gi");
		var temp = obj.value.replace(reg,"");
		temp = Remplace(temp,",",".");
		temp = parseFloat(temp);
		if (isNaN(temp)) {
			alert("Le champ '" + nom + "' n'est pas un nombre décimal.");
			obj.focus();
			obj.select();
			return false;
		}
		obj.value = temp			
	}
	else if (obj.value != "" && type == "floatPositif") {
		var temp = replaceString(",", ".", obj.value);
		temp = parseFloat(temp);
		if (isNaN(temp)) {
			alert("Le champ '" + nom + "' n'est pas un nombre décimal.");
			obj.focus();
			obj.select();
			return false;
		}
		else if (temp < 0) {
			alert("Le champ '" + nom + "' n'est pas un nombre décimal POSITIF.");
			obj.focus();
			obj.select();
			return false;
		}
		obj.value = temp
	}
	else if (obj.value != "" && type == "email") {
		var i = obj.value.indexOf("@",2);
		var j = obj.value.indexOf(".", i + 3);
		if ((i == -1) || (j == -1) || (j + 3 > obj.value.length)) {
			alert(nom);
			obj.focus();
			obj.select();
			return false;			
		}		
	}
	else if (obj.value != "" && type == "url") {
		var i = obj.value.indexOf("www.",0);
		
		if (i == -1) {
			alert("Le champ '" + nom + "' n'est pas une url valide.");
			obj.focus();
			obj.select();
			return false;			
		}		
	}
	else if (obj.value != "" && type == "date") {
		var ok = true;
		if ((obj.value.length != 10) || (obj.value.substring(2,3) != "/") || (obj.value.substring(5,6) != "/")) ok = false;
		var i = obj.value.substring(0,2);
		if ((i < 1) || (i >31)) ok = false;
		i = obj.value.substring(3,5);
		if ((i < 1) || (i >12)) ok = false;	
		i = obj.value.substring(6,10);
		if ((i < 1900) || (i >2100)) ok = false;		
		if (!ok) {
			alert("Le champ '" + nom + "' n'est pas une date valide.\nFormat : jj/mm/aaaa.");
			obj.focus();
			obj.select();
			return false;			
		}
	}	
	else if (obj.value != "" && type == "ip") {
		var ipReg = new RegExp("[0-9]{3}[\.][0-9]{3}[\.][0-9]{1,3}[\.][0-9]{1,3}");
		if (!ipReg.test(obj.value)) {
			alert("Le champ '" + nom + "' n'est pas une adresse ip valide.");
			obj.focus();
			obj.select();
			return false;
		}
	}
	
	return true;
}

function IsMotcle(str) {
	nb = 0;
	exReg = /\r/;
	while (exReg.test(str)) {
		str = str.replace(exReg, "");
		nb++;
	}
	
	if (nb>50) {
	result = nb -50;
		alert("Vous ne pouvez pas saisir plus de 50 mots clés par commerce ! veuillez en enlever " + result + " ");
		return false;
	}
	return true;
}

function EstDateValide(d1S, d2S, txt1S, txt2S) {
 // Indique si la date d1S qui correspond au champ txt1S
 // est inférieure à la date d2S qui correspond au champ txt2S
 // Tout ces champs sont des objets JavaScripts !

   // alert((d1S.value).substring(0,2) + "/" +  (d1S.value).substring(3,5) + "/" + (d1S.value).substring(6,10));

   var d1 = new Date((d1S.value).substring(6,10), (d1S.value).substring(3,5), (d1S.value).substring(0,2));
   var d2 = new Date((d2S.value).substring(6,10), (d2S.value).substring(3,5), (d2S.value).substring(0,2));

   if (d1>=d2) {
      alert(txt1S + " (" + d1S.value + ")" + " doit etre inferieure a " + txt2S + " (" + d2S.value + ")");
      return false;
   }
   else {
      // alert(txt1S + " (" + d1S.value + ")" + " est OK vis a vis de " + txt2S + " (" + d2S.value + ")");
      return true;
   }
}

function replaceString(oldS, newS, fullS) {
 // Remplace oldS avec newS dans la chaine fullS
    for (var i=0; i<fullS.length; i++) {
       if (fullS.substring(i,i+oldS.length) == oldS) {
          fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
       }
    }
    return fullS
}

//Affiche une image (img) dans un popup
function PopupImage(img) {
	titre="Image agrandie";
	w=open("",'image','top=0,left=200,width=400,height=400,toolbar=no,scrollbars=no,resizable=yes');	
	w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>");
	w.document.write("<SCRIPT language=javascript>function checksize()  { if (document.images[0].complete) {  window.resizeTo(document.images[0].width+12,document.images[0].height+40); window.focus();} else { setTimeout('check()',250) } }</"+"SCRIPT>");
	w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><a href='window.close();'><IMG src='"+img+"' border=0></a>");
	w.document.write("");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

function IsNumber(is,txt) {
	for (var i=0;i<is.value.length;i++) {
		var c = is.value.substring(i,i+1);
		if (c < "0" || c > "9") {
   			alert(txt);
			return false;  
		}
	}
	return true;
}

//Verifie un champ prix
function Verifprix(obj,txt){
var reg = RegExp(",","gi");
obj.value = obj.value.replace(reg,".");
var reg1 = RegExp(" ","gi");
obj.value = obj.value.replace(reg1,"");
if(!isNaN(obj.value)){
	if(obj.value!=""){
	obj.value = parseFloat(obj.value);
	return true;
	}
	else{
	alert(txt);
	return false;
	}
}
else{
alert(txt);
return false;
}
}

//Verifie une promotion
function Verifpromo(obj){
if(obj.promo.checked==true){
if(!Verifprix(obj.prixpromo,"Prix promotionnel")){ return false; }
}
return true;
}

//Verifie si l'élément (elem) se trouve dans le tableau (tab)
function existin_tab(elem,tab){
for(i in tab){
	if(tab[i]==elem) { return true;}
}
return false;
}

function Remplace(expr,a,b) {
      var i=0
      while (i!=-1) {
         i=expr.indexOf(a,i);
         if (i>=0) {
            expr=expr.substring(0,i)+b+expr.substring(i+a.length);
            i+=b.length;
         }
      }
      return expr
}

//Ouvrir Popup
function OpenFen(page,titre,x,y,w,h,scrollbar,resize){
	var option='top='+x+',left='+y+',width='+w+',height='+h+',';
	option+='toolbar=no,scrollbars='+scrollbar+',resizable='+resize+',status=no';
	window.open(page,titre,option);
}

function estIdentique(obj, nom, obj2, nom2) {
	if(obj.value==obj2.value){ return true; }
	else{ alert("Les champs '"+nom+"' et '"+nom2+"' ne correspondent pas !");
	return false; }
}

function DateCorrecte(obj, obj2, obj3, lage, obj4){
	var nbJourMax=31;
	var jNaissance=obj.options[obj.selectedIndex].value*1;
	var mNaissance=obj2.options[obj2.selectedIndex].value*1;
	var aNaissance=obj3.options[obj3.selectedIndex].value*1;

	if (jNaissance>0 && mNaissance>0)
	{
		if (mNaissance==2)
		{
			if (aNaissance%4==0)
				nbJourMax=29;
			else
				nbJourMax=28;
		}	
		else if (mNaissance==4 || mNaissance==6 || mNaissance==9 || mNaissance==11)
		{
			nbJourMax=30;
		}

		var todayMonth=10;
		var todayDay  =19;
		var todayYear =2005;

		var age=todayYear-aNaissance;
		if(lage){ obj4.value=age; }
		if (age>=18)
		{
			if (todayMonth<mNaissance || (todayMonth==mNaissance && todayDay<jNaissance))
				age=age-1;
		}

		if (age<18)
		{
			alert("Nous sommes désolés mais A Tout Coeur est réservé aux personnes âgées de plus de 18 ans.");
			return false;
		}
		else if (jNaissance>nbJourMax)
		{
			
			alert("Veuillez saisir une date valide !");
			return false;
		}
		else	
		{
			return true;
		}
	}
	else
	{
		alert("Veuillez saisir une date valide !");
		return false;
	}
}

function estCocher(obj,nom){
	if(obj.checked==true){ return true; }
	else{ alert(nom);
	return false; }
}

function estCocherM(obj,nom){
	for(i = 0; i<obj.length; i++){
	if (obj[i].checked == true) { return true; }
	} 
	alert(nom);
	return false;
}

function estCocherMD(obj,len,nom){
	for(i = 1; i<=len; i++){
	if (document.getElementById(obj+i).checked == true) { return true; }
	} 
	alert(nom);
	return false;
}

function ValidePasse(obj,nom){
	ctl = /^[A-Za-z0-9]{6,255}$/;
    if (obj.value.search(ctl) != -1) return true;
    else { alert(nom);
	return false; }
}

function controlerCP(obj,nom) {
    ctl = /^([A-Z]+\-)?[\d]{5}$/;
    if (obj.value.search(ctl) != -1) return true;
    else { alert(nom);
	return false; }
}

function estSelectionner(obj, nom, val) {
	if(obj.options[obj.selectedIndex].value!=val){ return true; }
	else{ alert(nom);
	return false; }
}

function estSelectionnerM(obj, nom, val) {
	var taille = obj.options.length;
	var i=0;
	for(i;i<taille;i++){
	if((obj.options[i].selected==true) && (obj.options[i].value!=-1)){ return true; }
	}
    alert(nom);
	return false;
}

function MemoListeM(liste1,obj){
	var taille = liste1.options.length;
	var val ="";
	var i=0;
	for(i;i<taille;i++){
		if((liste1.options[i].selected==true) && (liste1.options[i].value!=-1)){
			val += liste1.options[i].value+";";
		}
	}
	obj.value = val;
}
