// ***************************************
// * Class de vérification de formulaire *
// *                                     *
// *               V 1.0                 *
// *        Pix&Log - 11/02/2004         *
// ***************************************
function checkForm(formulaire) {
	this.form = formulaire;
	
	this.dayOfWeek = 1; // le jour de la semaine rempli apres appel de IsDateValide (samedi=6, dimanche=0)
	
	// motifs de verification
	this.regCodeArticle = /^[0-9]{2}\.[0-9]{3}\.[0-9]{6}\.[0-9]{2}\.[0-9]$/;
	this.regEmail = /^(([^<>;()[\]\\.,;:@"]+(\.[^<>()[\]\\.,;:@"]+)*)|(".+"))@((([a-z0-9]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))\.)*(([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))$/;
	this.regDecimal = /^(([0-9]+\.[0-9]+)|([0-9]+)|([0-9]+\,[0-9]+))$/;
	this.regNumeric = /^[0-9]+$/;
	this.regAlpha = /^[A-Za-z]+$/;
	this.regAlphaNum = /^[A-Za-z0-9]+$/;
	
	// méthodes de l'objet
	this.Exist = Exist;
	this.GetValue = GetValue;
	this.IsVide = IsVide;
	this.IsReg = IsReg;
	this.IsCodeArticle = IsCodeArticle;
	this.IsEmail = IsEmail;
	this.IsDecimal = IsDecimal;
	this.IsNumeric = IsNumeric;
	this.IsAlpha = IsAlpha;
	this.IsAlphaNum = IsAlphaNum;
	this.IsDateValide = IsDateValide;
	this.BoxCombienCoche = BoxCombienCoche;
}

// verification de l'existence de l'element
function Exist(champ) {
	if(this.form.elements[champ]) {
		// le champ existe
		return true;
	}
	else {
		// info de débuggage
		if(debug)  alert("Alerte !!! Le champ \""+champ+"\" n'existe pas!");
		// le champ n'existe pas
		return false;
	}
}

// recupere la valeur d'un champ
function GetValue(champ) {
	if(this.Exist(champ)) {
		switch(this.form.elements[champ].type) {
			// les boites de textes
			case "text":
			case "password":
			case "hidden":
			case "textarea":
				return this.form.elements[champ].value;
				break;
			
			// les cases à cocher (mais ayant un nom unique !!!)
			case "checkbox":
				// checkbox unique
				return this.form.elements[champ].value;
				break;
			case "radio":
				// radio unique
				return this.form.elements[champ].value;
				break;
			
			// les liste de selections
			case "select-one":
				if(this.form.elements[champ].selectedIndex > -1) {
					// un choix a bien été selectionné
					return this.form.elements[champ].options[this.form.elements[champ].selectedIndex].value;
				}
				else {
					// aucune option selectionné -> retourne vide
					return "";
				}
				break;
			case "select-multiple":
				var returnArray = new Array();
				for(var i=0; i<this.form.elements[champ].length; i++) {
						if(this.form.elements[champ].options[i].selected) {
							returnArray.push(this.form.elements[champ].options[i].value);
						}
				}
				return returnArray;
				break;
				
			// les champs "parcourir"
			case "file":
				return this.form.elements[champ].value;
				break;
			
			// les boutons
			case "reset":
			case "submit":
			case "button":
				return this.form.elements[champ].value;
				break;
			
			default:
				// cas liste de checkbox et de radio ayant le même nom !!!!!!!!!
				if(this.form.elements[champ].length > 0) {
					var returnArray = new Array();
					for(var i=0; i<this.form.elements[champ].length; i++) {
						// cas tableau radio
						if(this.form.elements[champ][i].type == "radio") {
							if(this.form.elements[champ][i].checked == true) {
								// si radio checked, pas la peine de verifier plus avant ...
								return this.form.elements[champ][i].value;
							}
						}
						
						// cas tableau checkbox
						if(this.form.elements[champ][i].type == "checkbox") {
							if(this.form.elements[champ][i].checked == true) {
								returnArray.push(this.form.elements[champ][i].value);
							}
						}
					}
					return returnArray;
				}
				else {
					return "type inconnu";
				}
		}
	}
	else {
		// n'existe pas -> retourne faux
		return false;
	}
}

// le champ est t'il vide ?
function IsVide(champ) {
	if(this.Exist(champ)) {
		var champVal = this.GetValue(champ);
		
		// si retour d'une string , on verifie != ""
		if(typeof(champVal) == "string") {
			return champVal == "";
		}
		
		// si retour d'un array, on verifie length > 0
		if(typeof(champVal) == "object") {
			return champVal.length == 0;
		}
	}
	else {
		// le champ n'existe pas --> on fait comme si il était pas vide
		return false;
	}
}

// le champ verifie t'il l'expression regulière
function IsReg(champ, regexp) {
	if(this.Exist(champ)) {
		if(!this.IsVide(champ)) {
			// recupération de la valeur du champ
			var champVal = this.GetValue(champ);
			
			if(typeof(champVal == "string")) {
				return champVal.search(regexp) != -1;
			}
			else {
				if(debug) alert("La fonction IsReg ne peut fonctionner pour le champ "+champ+" (not string)");
				// c prévu que pour les strings -->  on fait comme si c'etait bon
				return true;
			}
		}
		else {
			// c'est vide -> c'est pas bon
			return false;
		}
	}
	else {
		// le champ n'existe pas --> on fait comme si c'etait bon
		return true;
	}
}

// le champ contient bien t'il un code article
function IsCodeArticle(champ) { return this.IsReg(champ, this.regCodeArticle); }
// le champ contient bien t'il un email
function IsEmail(champ) { return this.IsReg(champ, this.regEmail); }
// le champ contient bien t'il un nombre decimal
function IsDecimal(champ) { return this.IsReg(champ, this.regDecimal); }
// le champ contient bien t'il un nombre numerique
function IsNumeric(champ) { return this.IsReg(champ, this.regNumeric); }
// le champ contient bien t'il un nombre numerique
function IsAlpha(champ) { return this.IsReg(champ, this.regAlpha); }
// le champ contient bien t'il un nombre numerique
function IsAlphaNum(champ) { return this.IsReg(champ, this.regAlphaNum); }

// verifie si une date est valide
function IsDateValide(champAnnee, champMois, champJour) {
	if(this.Exist(champAnnee) && this.Exist(champMois) && this.Exist(champJour)) {
		var annee = parseInt(this.GetValue(champAnnee));
		var mois = parseInt(this.GetValue(champMois));
		var jour = parseInt(this.GetValue(champJour));
		
		var myDate = new Date(annee,mois-1,jour);
		this.dayOfWeek = myDate.getDay();
		
		var isBissextile = (annee%400==0 || (annee%4==0 && annee%100!=0));
		
		if ( ( mois == 1 || mois == 3 || mois == 5 || mois == 7 || mois == 8 || mois == 10 || mois == 12 ) && jour >= 1 && jour <= 31)
		{ return true;}
		else
		{
			if ( ( mois == 4 || mois == 6 || mois == 9 || mois == 11 ) && jour >= 1 && jour <= 30)
			{ return true;}
			else
			{
				if (isBissextile)
				{ return ( jour >= 1 && jour <= 29 && mois == 2);}
				else
				{ return ( jour >= 1 && jour <= 28 && mois == 2);}
			}
		}
	}
	else {
		// un des champs n'existe pas --> on fait comme si c'etait bon
		return true;
	}
}

// pour verifier combien de checkbox sont coché (nommage séquentiel)
function BoxCombienCoche(racinechamp, totalchamp) {
	var nb = 0;
	for (var i=1; i<=totalchamp; i++)
	{
		if(this.Exist(racinechamp+i)) {
			if (this.form.elements[racinechamp+i].checked == true)  nb=nb+1;
		}
	}
	return nb;
}