/*
Fonctions Javascript pour Site Internet

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

Dernière modification le 20/12/2006
*/

//Vérifie si champ présent
function estPresent(obj, nom, taille, type) {
	return maFonction(obj, nom, taille, type, true);
}

//Vérifie si champ valide
function estValide(obj, nom, taille, type) {
	return maFonction(obj, nom, taille, type, false);
}

//Fonction de vérification des champs
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") {
		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") {
		temp = parseInt(obj.value);
		if (isNaN(temp)) {
			alert("Le champ '" + nom + "' n'est pas un nombre entier.");
			obj.focus();
			obj.select();
			return false;
		}
		else if (temp < 0) {
			alert("Le champ '" + nom + "' n'est pas un nombre entier POSITIF.");
			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("Le champ '" + nom + "' n'est pas un email valide.");
			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;
}

//Vérifie mots clés
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;
}

 // Indique si la date d1S est inférieure à la date d2S
function EstDateValide(d1S, d2S, nom) {
   var d1 = new Date((d1S.value).substring(0,4), (d1S.value).substring(5,7), (d1S.value).substring(8,10));
   var d2 = new Date((d2S.value).substring(0,4), (d2S.value).substring(5,7), (d2S.value).substring(8,10));
   if (d1>=d2) {
	  alert(nom);
      return false;
   }
   else {
	  return true;
   }
}

//Remplace oldS avec newS dans la chaine fullS
function replaceString(oldS, newS, 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,document.images[0].height); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>");
	w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><a href='javascript:window.close();'><IMG src='"+img+"' border=0></a>");
	w.document.write("");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

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);
}

//Affiche une image (img) dans un popup avec une légende
function PopupImagePlus(img,legende) {
	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,document.images[0].height+125); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>");
	w.document.write("<BODY bgcolor='#999898' onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><a href='javascript:window.close();'><IMG src='"+img+"' border=0></a><div style='padding-left:15px; font-size:24px; vertical-align:top'><p style='color:#D7D7D7'><img src='images/logo.gif' align='absmiddle' />   "+legende+"</p></div>");		
	w.document.write("");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

//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;
}

//Remplace tous les occurences de a par b dans expr
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);
}

//Vérification que 2 champs sont identiques
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; }
}

//Vérification que la date est correcte avec majorité
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 le site 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;
	}
}

//Verifie si radio ou checkbox cochée
function estCocher(obj,nom){
	if(obj.checked==true){ return true; }
	else{ alert(nom);
	return false; }
}

//Verifie si radio ou checkbox multiple coché
function estCocherM(obj,nom){
	for(i = 0; i<obj.length; i++){
	if (obj[i].checked == true) { return true; }
	} 
	alert(nom);
	return false;
}

//Permet de transformer des checkbox en effet bouton radio
function CB_Multiple(champ,idx){
var len = document.getElementsByName(champ).length;
for(i=0; i<len; i++){
	if (i != idx) { document.getElementsByName(champ)[i].checked=false; }
} 
}

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

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

//Verifie si liste sélectionnée
function estSelectionner(obj, nom, val) {
	if(obj.options[obj.selectedIndex].value!=val){ return true; }
	else{ alert(nom);
	return false; }
}

//Verifie si liste multiple sélectionnée
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;
}

//Récupérer un chiffre d'après une chaine
function GetChiffre(val){
var reg = RegExp(",","gi");
val = val.replace(reg,".");
var reg1 = RegExp(" ","gi");
val = val.replace(reg1,"");
val = parseFloat(val);
return val;
}

//Formater un chiffre avec ',' et ' ' pour les milliers
function SetChiffre(val){
val = Math.round(val*100)/100;
val = val.toString();
if(val.indexOf(".",0)==-1){
var deb = val;
var fin = "00";
}
else{
var deb = val.substring(0,val.indexOf(".",0));
var fin = val.substring((val.indexOf(".",0)+1),val.length);
if(fin.length==1){ fin+=0; }
}

var debinv = "";
for(i=(deb.length-1);i>=0;i--){
debinv += deb.charAt(i);
}
var cpt=1;
var inv = "";
for(i=0;i<debinv.length;i++){
inv += debinv.charAt(i);
if(cpt%3==0){ inv += " "; }
cpt++;
}
deb = "";
for(i=(inv.length-1);i>=0;i--){
deb += inv.charAt(i);
}
val = deb+","+fin+" &euro;";
return val;
}
