// Fonctions Javascripts pour les formulaires
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") {
		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 == "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 == "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;			
		}		
	}
	return true;
}

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

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

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

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

function estSelectionner(obj, nom, val) {
	if(obj.options[obj.selectedIndex].value!=val){ return true; }
	else{ alert(nom);
	obj.focus();
	obj.select();
	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);
	obj.focus();
	obj.select();
	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;
}

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 Altitu'deux est réservé aux personnes âgées de plus de 18 ans.");
			return false;
		}
		else if (jNaissance>nbJourMax)
		{
			
			alert("Veuillez saisir une date valide !");
			obj.focus();
			obj.select();
			return false;
		}
		else	
		{
			return true;
		}
	}
	else
	{
		alert("Veuillez saisir une date valide !");
		obj.focus();
		obj.select();
		return false;
	}
}

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

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

function PopupImage(img) {
var titre="Image taille reelle";
var option='top=0,left=100,width=400,height=400,toolbar=no,scrollbars=no,resizable=yes,status=no';
w=open("",'image',option);	
w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>");
w.document.write("<SCRIPT language=javascript>function checksize(){");
w.document.write("if (document.images[0].complete){");
w.document.write("window.resizeTo(document.images[0].width+15,document.images[0].height+85);");
w.document.write("window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>");
w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
w.document.write("<a href='javascript:window.close()'><IMG src='"+img+"' border=0></a>");
w.document.write("</BODY></HTML>");
w.document.close();
}

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