// Definicion del objecto por un campo solo
function champ() 
{
	this.id="";
	this.label="";
	this.type_affichage="";
	this.type_donnee="";
	this.colonnes=0;
	this.lignes=0;
	this.longueur_min=0;
	this.longueur_max=0;
	this.valeur_min=0;
	this.valeur_max=0;
	this.obligatoire=0;
}


function validation_formulaire() {
	
	this.num_champs=0;
	this.champs=Array();
	this.num_erreurs=0;
	this.mode="";
	this.traduction=Array();
	this.formulaire='';

	this.ajouter_champ = function (obj_champ) {
		this.num_champs++;
		this.champs[this.num_champs]=obj_champ;
	}
	
	this.valider = function () {
		// Buscar el campo
		var str_msg_erreur="";
		var str_msg_erreur_tmp="";
		var boo_pas_erreur=true;
		var str_RC="";
		var str_id_focus="";

		this.num_erreurs=0;
		// Buscar cada campo
		for(int_un_champ in this.champs) 
		{
			str_msg_erreur_tmp="";
			str_msg_erreur_tmp=this.verifier_un_champ(this.champs[int_un_champ],true);

			if(str_msg_erreur_tmp!="") 
			{		
				str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
				str_RC="\n";
				boo_pas_erreur=false;
				if(str_id_focus == '') {
					str_id_focus = this.champs[int_un_champ].id;
				}
			}
		}
		//boo_pas_erreur=false;
		if(!boo_pas_erreur) 
		{
			str_msg_erreur = this.traduction["js_validation_formulaire_titre"] + "\n\n" + str_msg_erreur;
			alert(str_msg_erreur);
			try {
				//document.getElementById(str_id_focus).focus();
				eval('document.' + this.formulaire + '.' + str_id_focus + '.focus();');
			}
			catch(e) {
			}
		}
		return(boo_pas_erreur);
	}


	this.verifier_un_champ = function (obj_champ, boo_montrer_msg_erreur) {

		var str_msg_erreur="";
		var str_msg_erreur_tmp="";
		var boo_champ_valide=true;
		var str_RC="";

		// If the object exists
		//if(document.getElementById(obj_champ.id)) 
		eval('var obj_id = document.' + this.formulaire + '.' + obj_champ.id + ';');
		if(obj_id) 
		{
			// obligatoire
			if(obj_champ.obligatoire==1 && obj_champ.type_donnee != "password") 
			{
				str_msg_erreur_tmp=this.champ_obligatoire(obj_champ);
				if(str_msg_erreur_tmp!="") 
				{
					boo_champ_valide=false;
					str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
					str_RC="\n";
				}
			}

			if(obj_champ.obligatoire==0 || (obj_champ.obligatoire==1 && boo_champ_valide)) 
			{
				switch(obj_champ.type_affichage) {
					case "text":
						if(obj_champ.longueur_min != 0 && obj_champ.longueur_max != 0 && obj_champ.type_donnee != "password" ) {
							str_msg_erreur_tmp=this.champ_longueur_min(obj_champ);
							if(str_msg_erreur_tmp!="") 
							{
								boo_champ_valide=false;
								str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
								str_RC="\n";
							}
							str_msg_erreur_tmp=this.champ_longueur_max(obj_champ);
							if(str_msg_erreur_tmp!="") 
							{
								boo_champ_valide=false;
								str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
								str_RC="\n";
							}
						}
						switch(obj_champ.type_donnee) {
							case "title":
								break;
							case "int":
								// Verify that the value is an integer : [0-9]
								str_msg_erreur_tmp=this.champ_int(obj_champ);
								if(str_msg_erreur_tmp!="") 
								{
									boo_champ_valide=false;
									str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
									str_RC="\n";
								}
								if(boo_champ_valide && (obj_champ.valeur_min != obj_champ.valeur_max)) {
									// Verify that the value is the minimum required
									str_msg_erreur_tmp=this.champ_valeur_min(obj_champ);
									if(str_msg_erreur_tmp!="") 
									{
										boo_champ_valide=false;
										str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
										str_RC="\n";
									}
									// Verify that the value is the maximum required
									str_msg_erreur_tmp=this.champ_valeur_max(obj_champ);
									if(str_msg_erreur_tmp!="") 
									{
										boo_champ_valide=false;
										str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
										str_RC="\n";
									}
								}
								break;
							case "float":
								// Verify that the value is an float : [0-9] and , and .
								str_msg_erreur_tmp=this.champ_float(obj_champ);
								if(str_msg_erreur_tmp!="") 
								{
									boo_champ_valide=false;
									str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
									str_RC="\n";
								}
								if(boo_champ_valide && (obj_champ.valeur_min != obj_champ.valeur_max)) {
									// Verify that the value is the minimum required
									str_msg_erreur_tmp=this.champ_valeur_min(obj_champ);
									if(str_msg_erreur_tmp!="") 
									{
										boo_champ_valide=false;
										str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
										str_RC="\n";
									}
									// Verify that the value is the maximum required
									str_msg_erreur_tmp=this.champ_valeur_max(obj_champ);
									if(str_msg_erreur_tmp!="") 
									{
										boo_champ_valide=false;
										str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
										str_RC="\n";
									}
								}
								break;
							case "date":
								// Verify that the value is a date : aaaa-mm-dd
								str_msg_erreur_tmp=this.champ_date(obj_champ);
								if(str_msg_erreur_tmp!="") 
								{
									boo_champ_valide=false;
									str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
									str_RC="\n";
								}
								break;
							case "datetime":
								// Verify that the value is a date : aaaa-mm-dd hh:mm:ss
								str_msg_erreur_tmp=this.champ_datetime(obj_champ);
								if(str_msg_erreur_tmp!="") 
								{
									boo_champ_valide=false;
									str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
									str_RC="\n";
								}
								break;
							case "email":
								eval('var valeur = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
								if(obj_champ.obligatoire==1 || (obj_champ.obligatoire==0 && valeur != '')) {
									// Verify that the value is an email : xxxxxx@xxxx.xxxx
									str_msg_erreur_tmp=this.champ_email(obj_champ);
									if(str_msg_erreur_tmp!="") 
									{
										boo_champ_valide=false;
										str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
										str_RC="\n";
									}
								}
								break;
							case "password":							
								//var str_password = document.getElementById(obj_champ.id).value;
								eval('var str_password = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
								//var str_password_confirm = document.getElementById(obj_champ.id + "_confirm").value;
								eval('var str_password_confirm = document.' + this.formulaire + '.' + obj_champ.id + '_confirm.value;');
								if(this.mode == "ajout" || (this.mode == "modif" && (str_password != "" || str_password_confirm != ""))) {							
									if(boo_champ_valide) {
										if(obj_champ.longueur_min != 0 && obj_champ.longueur_max != 0 ) {
											str_msg_erreur_tmp=this.champ_longueur_min(obj_champ);
											if(str_msg_erreur_tmp!="") 
											{
												boo_champ_valide=false;
												str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
												str_RC="\n";
											}
											str_msg_erreur_tmp=this.champ_longueur_max(obj_champ);
											if(str_msg_erreur_tmp!="") 
											{
												boo_champ_valide=false;
												str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
												str_RC="\n";
											}
										}
									}
									if(boo_champ_valide) {
										if(str_password == "") {
											this.num_erreurs++;
											str_msg_erreur_tmp = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_password_vide"];
											boo_champ_valide=false;
											str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
											str_RC="\n";
										}
										if(str_password_confirm == "") {
											this.num_erreurs++;
											str_msg_erreur_tmp = this.num_erreurs + " - " + obj_champ.label + " (" + this.traduction["js_confirmation"] + ") : " + this.traduction["js_obligatoire_password_conf_vide"];
											boo_champ_valide=false;
											str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
											str_RC="\n";
										}
									}
									if(boo_champ_valide) {
										// Verify that password and password_confirm are the same
										str_msg_erreur_tmp=this.champ_password(obj_champ);
										if(str_msg_erreur_tmp!="") 
										{
											boo_champ_valide=false;
											str_msg_erreur=str_msg_erreur+str_RC+str_msg_erreur_tmp;
											str_RC="\n";
										}
									}
								}
								break;
						}
						break;
					default:
				}

			}

			//str_msg_erreur = obj_champ.id;
			// Send erreur message if requested
			if(boo_montrer_msg_erreur) 
			{
				return(str_msg_erreur);
			}
			else 
			{
				return("");
			}
		} 
		else 
		{
			// Object no exists => no message
			return("");
		}			
	}


	// Verify if champ password is valid
	this.champ_password = function (obj_champ) 
	{
		var str_msg_erreur="";
		//var str_password = document.getElementById(obj_champ.id).value;
		eval('var str_password = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		//var str_password_confirm = document.getElementById(obj_champ.id + "_confirm").value;
		eval('var str_password_confirm = document.' + this.formulaire + '.' + obj_champ.id + '_confirm.value;');
		if (str_password != str_password_confirm) 
		{
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " (" + this.traduction["js_confirmation"] + ") : " + this.traduction["js_obligatoire_password_err_conf"];
		}
		return(str_msg_erreur);
	}

	// Verify if champ value is an email
	this.champ_email = function (obj_champ) 
	{
		var str_msg_erreur="";
		//var x = document.getElementById(obj_champ.id).value;
		eval('var x = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(x)) 
		{
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_email"];
		}
		return(str_msg_erreur);
	}

	this.is_array = function (obj)
	{
		try {
	   if (obj.constructor.toString().indexOf("Array") == -1)
		  return false;
	   else
		  return true;
		}
		catch(e) {
			return false;
		}
	}

	// Verify if champ value is a date with time
	this.champ_datetime = function (obj_champ) 
	{
   		var boo_resultat = true;
   		var boo_bisextil = false;
		var tab_mois = new Array();
		tab_mois[1] = 31;
		tab_mois[2] = 28;
		tab_mois[3] = 31;
		tab_mois[4] = 30;
		tab_mois[5] = 31;
		tab_mois[6] = 30;
		tab_mois[7] = 31;
		tab_mois[8] = 31;
		tab_mois[9] = 30;
		tab_mois[10] = 31;
		tab_mois[11] = 30;
		tab_mois[12] = 31;
		var str_msg_erreur="";
		//var x = document.getElementById(obj_champ.id).value;
		eval('var x = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		//var filter  = /([1-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]) [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/;
		var filter  = /([0-9][0-9])\/([0-9][0-9])\/([1-9][0-9][0-9][0-9]) [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/;
		var res = filter.exec(x);
		//if (res == null) 
		if(!this.is_array(res))
		{
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_datetime"];
		} else {
			if(res[3] < 1900 || res[3] > 2200) {
				boo_resultat = false;
			}
			if(boo_resultat) {
				if(res[2] < 1 || res[2] > 12) {
					boo_resultat = false;
				}
				if(boo_resultat) {
					if(this.isBi(res[3])) {
						tab_mois[2] = 29; 
					}
					if(res[1] < 1 || res[1] > tab_mois[parseInt(res[2],10)]) {
						boo_resultat = false;
					}
				}
			}
			if(!boo_resultat) {
				this.num_erreurs++;
				str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_datetime"];
			}
		}
		return(str_msg_erreur);
	}

	// Verify if champ value is a date with time
	this.champ_date = function (obj_champ) 
	{
   		var boo_resultat = true;
   		var boo_bisextil = false;
		var tab_mois = new Array();
		tab_mois[1] = 31;
		tab_mois[2] = 28;
		tab_mois[3] = 31;
		tab_mois[4] = 30;
		tab_mois[5] = 31;
		tab_mois[6] = 30;
		tab_mois[7] = 31;
		tab_mois[8] = 31;
		tab_mois[9] = 30;
		tab_mois[10] = 31;
		tab_mois[11] = 30;
		tab_mois[12] = 31;
		var str_msg_erreur="";
		//var x = document.getElementById(obj_champ.id).value;
		eval('var x = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		//var filter  = /([1-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]) [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/;
		var filter  = /([0-9][0-9])\/([0-9][0-9])\/([1-9][0-9][0-9][0-9])/;
		var res = filter.exec(x);
		if (!this.is_array(res)) 
		{
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_date"];
		} else {
			if(res[3] < 1900 || res[3] > 2200) {
				boo_resultat = false;
			}
			if(boo_resultat) {
				if(res[2] < 1 || res[2] > 12) {
					boo_resultat = false;
				}
				if(boo_resultat) {
					if(this.isBi(res[3])) {
						tab_mois[2] = 29; 
					}
					if(res[1] < 1 || res[1] > tab_mois[parseInt(res[2],10)]) {
						boo_resultat = false;
					}
				}
			}
			if(!boo_resultat) {
				this.num_erreurs++;
				str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_date"];
			}
		}
		return(str_msg_erreur);
	}

	this.isBi = function (str_year) 
	{
		var int_dif = str_year-2000;
		if(int_dif<0) {
			int_dif*=-1;
		}
		if(int_dif%4==0) {
			return true;
		} else {
			return false;
		}
	} 
	// Verify if value is the minimum required
	this.champ_valeur_max = function (obj_champ) 
	{
		var str_msg_erreur="";
		//var str_value = document.getElementById(obj_champ.id).value;
		eval('var str_value = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		if(str_value > obj_champ.valeur_max) {
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_valeur_max"] + " " + obj_champ.valeur_max;
		}
		return(str_msg_erreur);
	}


	// Verify if value is the minimum required
	this.champ_valeur_min = function (obj_champ) 
	{
		var str_msg_erreur="";
		//var str_value =document.getElementById(obj_champ.id).value;
		eval('var str_value = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		if(str_value < obj_champ.valeur_min) {
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_valeur_min"] + " " + obj_champ.valeur_min;
		}
		return(str_msg_erreur);
	}


	// Verify if champ value is an integer
	this.champ_int = function (obj_champ) 
	{
		var str_msg_erreur="";
		var str_valid_chars = "0123456789-";
   		var str_char;
   		var boo_resultat = true;
		//var str_string=document.getElementById(obj_champ.id).value;
		eval('var str_string = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		if (str_string.length > 0) 
		{
			//  test strString consists of valid characters listed above
			for (i = 0; i < str_string.length; i++) 
			{
				str_char = str_string.charAt(i);
				if (str_valid_chars.indexOf(str_char) == -1 && boo_resultat) 
				{
					boo_resultat = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_entier"];
				}
			}
		}
		return(str_msg_erreur);
	}

	// Verify if champ value is a float
	// Verify if champ value is a float
	this.champ_float = function (obj_champ) 
	{
		var str_msg_erreur="";
		var str_separateur_decimal = ',';
		var str_valid_chars = "0123456789-" + str_separateur_decimal;
   		var str_char;
   		var boo_resultat = true;
		//var str_string=document.getElementById(obj_champ.id).value;
		eval('var str_string = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		//alert(str_string);
		if (str_string.length > 0) 
		{
			//  test strString consists of valid characters listed above
			for (i = 0; i < str_string.length; i++) 
			{
				str_char = str_string.charAt(i);
				if (str_valid_chars.indexOf(str_char) == -1 && boo_resultat) 
				{
					boo_resultat = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_decimal"];
				}
			}
			if(str_string.indexOf(str_separateur_decimal) < 0) {
					boo_resultat = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_decimal"];
			}
		}
		return(str_msg_erreur);
	}


	// Verify if text length is valid
	this.champ_longueur_min = function (obj_champ) 
	{
		var str_msg_erreur="";
		var boo_champ_valide=true;
		//var str_value = new String(document.getElementById(obj_champ.id).value);
		eval('var str_value = document.' + this.formulaire + '.' + obj_champ.id + '.value;');

		if(str_value.length < obj_champ.longueur_min) {
			boo_champ_valide = false;
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_longueur_min"] + obj_champ.longueur_min + this.traduction["js_obligatoire_caractere"];
		}
		return(str_msg_erreur);
	}

	// Verify if text length is valid
	this.champ_longueur_max = function (obj_champ) 
	{
		var str_msg_erreur="";
		var boo_champ_valide=true;
		//var str_value = new String(document.getElementById(obj_champ.id).value);
		eval('var str_value = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
		if(str_value.length > obj_champ.longueur_max) {
			boo_champ_valide = false;
			this.num_erreurs++;
			str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_longueur_max"] + obj_champ.longueur_max + this.traduction["js_obligatoire_caractere"];
		}
		return(str_msg_erreur);
	}


	// Verify if a obligatoire champ had a value
	this.champ_obligatoire = function (obj_champ) 
	{
		var str_msg_erreur="";
		var boo_champ_valide=true;
		

		// El tratamiento depende del tip de campo
		switch(obj_champ.type_affichage) 
		{
			case "hidden":
			case "text":
			case "file":
			case "password":
			case "textarea":
				eval('var str_value = document.' + this.formulaire + '.' + obj_champ.id + '.value;');
				// Verify if there is a value in the champ
				if(str_value=="") 
				{
					boo_champ_valide = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_texte"];
				}
				break;
			
			case "select":
				eval('var int_selected_index = document.' + this.formulaire + '.' + obj_champ.id + '.selectedIndex;');
				// Verify if there is a value selected
				if(int_selected_index < 0) {
					boo_champ_valide = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_liste"];
				}
				break;
			case "select_none":
				eval('var int_selected_index = document.' + this.formulaire + '.' + obj_champ.id + '.selectedIndex;');
				// Verify if there is a value selected (not None)
				if(int_selected_index < 1) {
					boo_champ_valide = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_liste_aucun"];
				}
				break;
			case "radio":
				// Verify if it is a table
				eval('var obj_bouton = document.' + this.formulaire + '.' + obj_champ.id + ';');
				//if(document.getElementsByName(obj_champ.id)[0]) 
				if(obj_bouton[0])
				{
					var boo_one_selected=false;
					// Verify if one element has been selected
					//for(i=0;i<document.getElementsByName(obj_champ.id).length;i++) 
					for(i=0;i<obj_bouton.length;i++)
					{
						//if(document.getElementsByName(obj_champ.id)[i].checked)
						if(obj_bouton[i].checked)
						{
							boo_one_selected=true;
						}
					}
					// No element selected => erreur
					if(!boo_one_selected) 
					{
						boo_champ_valide=false;
						this.num_erreurs++;
						str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_radio"];
					}
				} 
				else 
				{
					// It is not a table => verify if element has been selected
					if (document.getElementsByName(obj_champ.id).checked) 
					{
						// No erreur
					} 
					else 
					{
						// Element not selected => erreur
						boo_champ_valide=false;
						this.num_erreurs++;
						str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_radio"];
					}
				}
				break;
			case "external":
				// Verify if there is an element in the list
				if(document.getElementById(obj_champ.id).options.length <= 0) {
					boo_champ_valide = false;
					this.num_erreurs++;
					str_msg_erreur = this.num_erreurs + " - " + obj_champ.label + " : " + this.traduction["js_obligatoire_externe"];
				}
				break;
			case "key":
				break;
		}
		return(str_msg_erreur);
	}
	
}
