/////
function myOpen(URL, nom, features) {
	window.open(URL, nom, features);
}
/////
function checkSelect(obj, nom, val) {
	if (obj.options[obj.selectedIndex].value == val) {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;	
	}
	return true;
}
/////
function testURL(obj) {
	var lien = obj.value;
	if (lien != "") {
		if (lien.substr(0, 7) != "http://") lien = "http://" + lien;
		window.open(lien);
	} else {
		alert("Vous devez préciser une URL");
		obj.focus();
	}
}
/////
function isString(obj, nom, obligatoire) {
	return isString(obj, nom, obligatoire, -1, false);
}
function isString(obj, nom, obligatoire, taille) {
	return isString(obj, nom, obligatoire, taille, false);
}	
function isString(obj, nom, obligatoire, taille, fixe) {
	if (obligatoire && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	}
	if (taille != -1 && obj.value.length > taille) {
		alert("Le champ '" + nom + "' est trop long.\nMaximum " + taille + " caractères.");
		obj.select();
		return false;		
	}
	if (taille != -1 && fixe && obj.value.length != taille) {
		alert("Le champ '" + nom + "' n\'a pas la bonne taille.\n" + taille + " caractères nécessaires.");
		obj.select();
		return false;		
	}
	return true;
}
/////
function isEmail(obj, nom) {
	return isEmail(obj, nom, false);
}
function isEmail(obj, nom, obligatoire) {
	if (obligatoire && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	} else if (obj.value == "") {
		return true;
	}
	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.select();
		return false;			
	}
	return true;	
}
/////
//function isInt(obj, nom) {
//function isInt(obj, nom, obligatoire) {
function isInt(obj, nom, obligatoire, min, max) {
	if (obligatoire && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	} else if (obj.value == "") {
		return true;
	}
	var temp = parseInt(obj.value,10);
	if (isNaN(temp)) {
		alert("Le champ '" + nom + "' n'est pas un nombre entier.");
		obj.select();
		return false;	
	}
	if (min && temp < min) {
		alert("La valeur du champ '" + nom + "' est trop petite.\nMinimum " + min + ".");
		obj.select();
		return false;		
	}
	if (max && temp > max) {
		alert("La valeur du champ '" + nom + "' est trop grande.\nMaximum " + max + ".");
		obj.select();
		return false;		
	}
	obj.value = temp
	return true;
}
/////
//function isFloat(obj, nom) {
//function isFloat(obj, nom, obligatoire) {

function isFloat(obj, nom, obligatoire, min, max) {
	if (obligatoire && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	} else if (obj.value == "") {
		return true;
	}
	var temp = replaceString(",", ".", obj.value);
	temp = parseFloat(temp);
	if (isNaN(temp)) {
		alert("Le champ '" + nom + "' n'est pas un nombre décimal.");
		obj.select();
		return false;	
	}
	if (min && temp < min) {
		alert("La valeur du champ '" + nom + "' est trop petite.\nMinimum " + min + ".");
		obj.select();
		return false;		
	}
	if (max && temp > max) {
		alert("La valeur du champ '" + nom + "' est trop grande.\nMaximum " + max);
		obj.select();
		return false;		
	}
	obj.value = temp
	return true;
}
/////
//function isDate(obj, nom) {}
//function isDate(obj, nom, obligatoire) {}
function isDate(obj, nom, obligatoire, min) {
	if (obligatoire && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	} else if (obj.value == "") {
		return true;
	}
	var ok = true;
	if ((obj.value.length != 10) || (obj.value.substring(2,3) != "/") || (obj.value.substring(5,6) != "/")) ok = false;
	var d = obj.value.substring(0,2);
	var m = obj.value.substring(3,5);
	var y = obj.value.substring(6,10);	
	
	if (m==1 || m==3 || m==5 || m==7 | m==8 || m==10 || m==12) {
		if (d > 31) ok = false;
	}
	else if (m==4 || m==6 || m==9 || m==11) {
		if (d > 30) ok = false;	
	}
	else if (m==2) {
		if (y % 4 == 0) {
			if (d > 29) ok = false;	
		}
		else {
			if (d > 28) ok = false;	
		}
	}
	else {
		ok = false;	
	}
	if (!ok) {
		alert("Le champ '" + nom + "' n'est pas une date valide.\nFormat : jj/mm/aaaa.");
		obj.focus();
		obj.select();
		return false;			
	}
	if (min) {
		var maDate = new Date(y, m - 1, d).getTime();
		var dateMin = new Date().getTime() + (min * 86400000);
		if (maDate < dateMin) {
			alert("Le champ '" + nom + "' doit être postérieur d'au moins " + min + " jours à aujourd'hui");
			obj.focus();
			obj.select();
			return false;				
		}	
	}
	return true;
}
/////
function DeplaceCritere(from, to) {
	index=0;
	while (index<from.options.length) {
		if (from.options[index].selected) {
			var newElem = new Option(from.options[index].text, from.options[index].value);
			to.options[to.length]=newElem;
			from.options[index]=null;
		}
		else {
			index++;
		}
	}
	Trie(to);
}
/////
function Trie(obj) {
	modif = true;
	while (modif) {
		modif = false;
  		for (var i=0; i<(obj.options.length-1); i++) {
			if (obj.options[i].text > obj.options[i+1].text) {
				tempTxt = obj.options[i].text;
        		tempVal = obj.options[i].value;
        		obj.options[i].text=obj.options[i+1].text;
        		obj.options[i].value=obj.options[i+1].value;
        		obj.options[i+1].text=tempTxt;
        		obj.options[i+1].value=tempVal;
		    	modif=true;
			}
		}
	}
}
/////
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;
}
/////
function chronologieDate(date1, champ1, date2, champ2) {
	var d1 = new Date(date1.value.substring(6,10), date1.value.substring(3,5) - 1, date1.value.substring(0,2));
	var d2 = new Date(date2.value.substring(6,10), date2.value.substring(3,5) - 1, date2.value.substring(0,2));

	if (d1>d2) {
		alert("Le champ '" + champ1 + "' est supérieur au champ '" + champ2 + "'");
		date2.focus();
		return false;	
	}
	return true;
}
/////
function setNow(obj, mois, annee) {
	var date = new Date();
	if (mois) { //date sous la forme de 3 select (ancien dev)
		obj.options[date.getDate() - 1].selected = true;
		mois.options[date.getMonth()].selected = true;
		annee.options[date.getYear()-2000].selected = true;	
	} else { //date sous la forme d'un input jj/mm/aaaa (nouveau dev)
		obj.value = (date.getDate() < 10) ? '0' + date.getDate() : date.getDate();
		var month = date.getMonth() + 1;
		obj.value += (month < 10) ? '/0' + month: '/' + month;
		var year = date.getYear() % 100;
		obj.value += '/200' + year;//marche jusqu'en 2009  ;-)
	}	
}
function setLater(obj, mois, annee) {
	if (mois) { //date sous la forme de 3 select (ancien dev)
		obj.options[obj.options.length-1].selected = true; 
		mois.options[mois.options.length-1].selected = true; 
		annee.options[annee.options.length-1].selected = true; 
	} else { //date sous la forme d'un input jj/mm/aaaa (nouveau dev)
		obj.value = '31/12/2010';
	} 	
}
/////



//
// ces fonctions sont utilisées dans SI et GU (premier dev)
// elles ne doivents plus etre utiliséés pour les devs futurs
// cedric@eolas.fr
//
function checkDate(jour, mois) {
	var m = mois.options[mois.selectedIndex].value;
	var j = jour.options[jour.selectedIndex].value;
	if ((m==4 || m==6 || m==9 || m==11) && j==31) {
		jour.options[29].selected = true;
	} 
	else if (m==2 && j>28) {
		jour.options[27].selected = true;
	}
}
function compareDate(jour1, mois1, annee1, jour2, mois2, annee2) {
	var d1 = new Date(annee1.options[annee1.selectedIndex].value, mois1.options[mois1.selectedIndex].value, jour1.options[jour1.selectedIndex].value);
	var d2 = new Date(annee2.options[annee2.selectedIndex].value, mois2.options[mois2.selectedIndex].value, jour2.options[jour2.selectedIndex].value);
	if (d1>d2) {
		alert("La première date est supérieur à la seconde");
		annee2.focus();
		return false;	
	}
	return true;
}
function estPresent(obj, nom) {
	return maFonction(obj, true, nom, -1, "", false);
}

function estPresent(obj, nom, taille) {
	return maFonction(obj, true, nom, taille, "", false);
}

function estPresent(obj, nom, taille, type) {
	return maFonction(obj, true, nom, taille, type, false);
}

function estPresent(obj, nom, taille, type, fixe) {
	return maFonction(obj, true, nom, taille, type, fixe);
}

function estValide(obj, nom) {
	return maFonction(obj, false, nom, -1, "", false);
}

function estValide(obj, nom, taille) {
	return maFonction(obj, false, nom, taille, "", false);
}

function estValide(obj, nom, taille, type) {
	return maFonction(obj, false, nom, taille, type, false);
}

function estValide(obj, nom, taille, type, fixe) {
	return maFonction(obj, false, nom, taille, type, fixe);
}

function maFonction(obj, presence, nom, taille, type, fixe) {
	if (presence && obj.value == "") {
		alert("Vous devez préciser le champ '" + nom + "'.");
		obj.focus();
		return false;
	}
	if(presence && obj.value != ""){
		if (taille != -1 && fixe && obj.value.length != taille) {
			alert("Le champ '" + nom + "' n\'a pas la bonne taille.\n " + taille + " caractères nécessaires.");
			obj.select();
			return false;		
		}
	}

	if (taille != -1 && obj.value.length > taille) {
		alert("Le champ '" + nom + "' est trop long.\nMaximum " + taille + " caractères.");
		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.select();
			return false;	
		}
		obj.value = temp
	}
	else if (obj.value != "" && type == "float") {
		var temp = parseFloat(obj.value);
		if (isNaN(temp)) {
			alert("Le champ '" + nom + "' n'est pas un nombre décimal.");
			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.select();
			return false;			
		}		
	}
	return true;
}


