/*
'-------------------------------------------------------------------------------------------------
' Nom du fichier : TGI_validation.js
' Projet : Perso
' Auteur : LETURGEZ Olivier
' Date de modification : 26/04/2002
' Version : 2.5
'-------------------------------------------------------------------------------------------------
' Role : Permet une gestion simplifiee de la validation des formulaires....
'	
'-------------------------------------------------------------------------------------------------
*/

/*======================================================================================*
 * CONSTRUCTEUR DE FORMVALIDATION							*
 *======================================================================================*/
function formValidation() {
	this.Index = new Array();
	this.addInput = formValidation_addInput;
	this.validat = formValidation_validat;
}
/*======================================================================================*
 * RAJOUTE UN OBJET INPUTVALIDATION A L'OBJET FORMVALIDATION				*
 *======================================================================================*/
function formValidation_addInput(nom,input,contenu,longueur,obligatoire) {
	this.Index[this.Index.length] = new inputValidation(nom,input,contenu,longueur,obligatoire);
}
/*======================================================================================*
 * VALIDE L'ENSEMBLE DES OBJETS INPUTVALIDATION DE L'OBJET FORMVALIDATION		*
 *======================================================================================*/
function formValidation_validat() {
	for (i = 0; i < this.Index.length; i++) {
		valide = this.Index[i].checkEntry();
		if (valide) {
			continue;
		} else {
			break;
		}
	}
	return valide;
}
/*======================================================================================*
 * CONSTRUCTEUR DES OBJETS INPUTVALIDATION						*
 *======================================================================================*/
function inputValidation(nom,input,contenu,longueur,obligatoire) {
	this.name =nom;
	this.input =input;
	this.contentType = contenu;
	this.length = longueur;
	this.full = obligatoire;
	this.checkEntry = inputValidation_checkEntry;
	return this;
}
/*======================================================================================*
 * VALIDE LE CONTENU D'UN OBJET INPUTVALIDATION						*
 *======================================================================================*/
function inputValidation_checkEntry() {
	var inputToTest = eval(this.input);
	var valide=true;
	if ((inputToTest.type=="file")||(inputToTest.type=="password")||(inputToTest.type=="text")||(inputToTest.type=="textarea")) {
		var valueToTest=inputToTest.value;
		var strChar = "[abcdefghijklmnopqrstuvwxyz0123456789_\\-\\.]";
		var strNum = "[0123456789\\.]";
		var strInt = "[0123456789]";
		// NUMERIC
		if ((valide==true)&&(valueToTest!="")&&(this.contentType=="numeric")) {
			var regNumNo = /\..*\./;
			var regNum = new RegExp("^" + strNum + "+$");
			if ((!regNum.test(valueToTest)) || (regNumNo.test(valueToTest))) {
				valide=false;
				alert("A numeric value is requested for the field \""+this.name+"\".");
			}
		}
		// ENTIER
		if ((valide==true)&&(valueToTest!="")&&(this.contentType=="integer")) {
			var regNum = new RegExp("^" + strInt + "+$");
			if (!regNum.test(valueToTest)) {
				valide=false;
				alert("A whole value is requested for the field \""+this.name+"\".");
			}
		}
		// DATE
		if ((valide==true)&&(valueToTest!="")&&(this.contentType=="date")) {
			var dateContent =  new Array();
			var msg ,jj, mm, aaaa;
			dateContent = valueToTest.split("/");
			if (dateContent.length != 3) {
				msg = "A \"dd/mm/yyyy\" type of value is requested";
				valide = false;
			} else {
				jj = dateContent[0];
				mm = dateContent[1];
				aaaa = dateContent[2];
				msg = checkDate(jj,mm,aaaa);
				if (msg!="") {valide = false;}
			}
			if (valide == false) {
				alert(msg + " for the field \""+this.name+"\".");
			}
		}
		// URL
		if ((valide==true)&&(valueToTest!="")&&(this.contentType=="url")) {
			var regMailto = new RegExp("^mailto://" + strChar + "+@" + strChar +"+\." + strChar + "+$","i");
			var regHttp = new RegExp("^http://" + strChar + "+\."+ strChar + "+\." + strChar + "+$","i");
			var regMailtoNo = /([_\-\.]@)|(@[_\-\.])|([_\-\.][_\-\.])+|(^mailto:\/\/[_\-\.])|([_\-\.]$)/;
			var regHttpNo = /([_\-\.][_\-\.])+|(^http:\/\/[_\-\.])|([_\-\.]$)/;
			var valideMailto = true;
			var valideHttp = true;
			if ((!regMailto.test(valueToTest)) || (regMailtoNo.test(valueToTest))) {
				valideMailto = false;
			}
			if ((!regHttp.test(valueToTest)) || (regHttpNo.test(valueToTest))) {
				valideHttp = false;
			}
			if (!valideMailto && !valideHttp) {
				valide=false;
				alert("The value of the field \""+this.name+"\" is not a valid url.");
			}
		}
		// ADRESSE MAIL
		if ((valide==true)&&(valueToTest!="")&&(this.contentType=="mail")) {
			var regMail = new RegExp("^" + strChar + "+@" + strChar +"+\." + strChar + "+$","i");
			var regMailNo = /([_\-\.]@)|(@[_\-\.])|([_\-\.][_\-\.])+|(^[_\-\.])|([_\-\.]$)/;
			if ((!regMail.test(valueToTest)) || (regMailNo.test(valueToTest))) {
				valide=false;
				alert("The value of the field \""+this.name+"\" is not a valid mail address.");
			}
		}
		// TROP DE CARACTERES
		if ((valide==true)&&(valueToTest.length > this.length)) {
			valide=false;
			alert("There cannot be more than "+this.length+" characters for the field \""+this.name+"\".");
		}
		// CHAMP OBLIGATOIRE PAS MOYEN DE FINTER AVEC DES BLANCS
		if ((valide==true)&&(this.full=="yes")) {
			str = trim(valueToTest);
			if (str.length==0) {
				valide=false;
				alert("A value is requested for the field \""+this.name+"\".");





			}
		}
		if (valide==false) {inputToTest.select();inputToTest.focus();}
	}
	return valide;
}
/*======================================================================================*
 * SUPRIME L'ENSEMBLE DES CARACTERES BLANC (\f\n\r\t\v) EN DEBUT ET FIN DE CHAINE	*
 *======================================================================================*/
	function trim(str) {
		str = trimLeft(str);
		str = trimRight(str);
		return str;
	}
	function trimLeft(str) {
		var regTrimleft = /^\s+/g;
		str = str.replace(regTrimleft,"");
		return str;
	}
	function trimRight(str) {
		var regTrimright = /\s+$/g;
		str = str.replace(regTrimright,"");
		return str;	
	}
/*======================================================================================*
 * VALIDE DES DONNEES DE TYPE DATE							*
 *======================================================================================*/
function checkDate(jour, mois, annee) {
	var valide=true;
	var msg ="";
	var Calendar = new Array();
	Calendar[0] = 31;
	Calendar[1] = 31;
	Calendar[3] = 31;
	Calendar[4] = 30;
	Calendar[5] = 31;
	Calendar[6] = 30;
	Calendar[7] = 31;
	Calendar[8] = 31;
	Calendar[9] = 30;
	Calendar[10] = 31;
	Calendar[11] = 30;
	Calendar[12] = 31;
	// Validation du type entier
	var regInt = new RegExp("^[0123456789]+$");
	if (!regInt.test(jour)||!regInt.test(mois)||!regInt.test(annee)) {
		msg = "You have to type byte characters";
		valide = false;
	}
	// Validation de l'annee
	if ((valide==true)&&((annee <= 1900) || (annee == "") || (annee > 2100))) {
		msg = "You have to type a value \"year\" between 1900 and 2100";
		valide = false;
	
	}
	// Validation du mois
	if ((valide == true) && ((mois-0 < 1 || 12 < mois-0 || (mois == "")))) {
		msg = "You have to type a value \"month\" between 1 and 12";
		valide = false;
	}
	// Prise ne compte des ann‚es bi
	if ((valide == true) && ((!(annee % 4) && (annee % 100)) || !(annee % 400))) {
		Calendar[2] = 29;
	} else {
		Calendar[2] = 28;
	}
	// Validation du jour
	if ((valide==true)&&((jour-0 < 1 || (jour == "") || Calendar[Number(mois)] < jour-0))) {
		msg = "You have to type a value \"day\" between 1 and " + Calendar[Number(mois)];
		valide = false;
	}
	return msg;
}