
// Definicion del objecto por un campo solo
function field() 
{
	this.id="";
	this.label="";
	this.display_type="";
	this.data_type="";
	this.cols=0;
	this.rows=0;
	this.min_length=0;
	this.max_length=0;
	this.min_value=0;
	this.max_value=0;
	this.mandatory=0;
}


function form_validation() {
	
	this.num_fields=0;
	this.fields=Array();
	this.num_errors=0;
	this.mode="";

	this.add_field = function (obj_field) {
		this.num_fields++;
		this.fields[this.num_fields]=obj_field;
	}
	
	this.validate = function () {
		// Buscar el campo
		var str_msg_error="";
		var str_msg_error_tmp="";
		var boo_no_error=true;
		var str_RC="";

		this.num_errors=0;
		// Buscar cada campo
		for(int_one_field in this.fields) 
		{
			str_msg_error_tmp="";
			str_msg_error_tmp=this.verify_one_field(this.fields[int_one_field],true);

			if(str_msg_error_tmp!="") 
			{		
				str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
				str_RC="\n";
				boo_no_error=false;
			}
		}
		//boo_no_error=false;
		if(!boo_no_error) 
		{
			str_msg_error = app["error_title_js"] + "\n\n" + str_msg_error;
			alert(str_msg_error);
		}
		return(boo_no_error);
	}

	this.set_mandatory = function (str_id, int_value) {
		for(int_one_field in this.fields) 
		{
			if(this.fields[int_one_field].id == str_id) 
			{		
				this.fields[int_one_field].mandatory = int_value;
			}
		}
	}

	this.verify_one_field = function (obj_field, boo_show_msg_error) {

		var str_msg_error="";
		var str_msg_error_tmp="";
		var boo_field_valid=true;
		var str_RC="";

		// If the object exists
		if(document.getElementById(obj_field.id)) 
		{
			// Mandatory
			if(obj_field.mandatory==1 && obj_field.data_type != "password") 
			{
				str_msg_error_tmp=this.field_mandatory(obj_field);
				if(str_msg_error_tmp!="") 
				{
					boo_field_valid=false;
					str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
					str_RC="\n";
				}
			}

			if(obj_field.mandatory==0 || (obj_field.mandatory==1 && boo_field_valid)) 
			{
				switch(obj_field.display_type) {
					case "text":
						if(obj_field.min_length != 0 && obj_field.max_length != 0 && obj_field.data_type != "password" ) {
							str_msg_error_tmp=this.field_min_length(obj_field);
							if(str_msg_error_tmp!="") 
							{
								boo_field_valid=false;
								str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
								str_RC="\n";
							}
							str_msg_error_tmp=this.field_max_length(obj_field);
							if(str_msg_error_tmp!="") 
							{
								boo_field_valid=false;
								str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
								str_RC="\n";
							}
						}
						switch(obj_field.data_type) {
							case "title":
								break;
							case "int":
								// Verify that the value is an integer : [0-9]
								str_msg_error_tmp=this.field_int(obj_field);
								if(str_msg_error_tmp!="") 
								{
									boo_field_valid=false;
									str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
									str_RC="\n";
								}
								if(boo_field_valid && (obj_field.min_value != obj_field.max_value)) {
									// Verify that the value is the minimum required
									str_msg_error_tmp=this.field_min_value(obj_field);
									if(str_msg_error_tmp!="") 
									{
										boo_field_valid=false;
										str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
										str_RC="\n";
									}
									// Verify that the value is the maximum required
									str_msg_error_tmp=this.field_max_value(obj_field);
									if(str_msg_error_tmp!="") 
									{
										boo_field_valid=false;
										str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
										str_RC="\n";
									}
								}
								break;
							case "float":
								// Verify that the value is an float : [0-9] and , and .
								str_msg_error_tmp=this.field_float(obj_field);
								if(str_msg_error_tmp!="") 
								{
									boo_field_valid=false;
									str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
									str_RC="\n";
								}
								if(boo_field_valid && (obj_field.min_value != obj_field.max_value)) {
									// Verify that the value is the minimum required
									str_msg_error_tmp=this.field_min_value(obj_field);
									if(str_msg_error_tmp!="") 
									{
										boo_field_valid=false;
										str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
										str_RC="\n";
									}
									// Verify that the value is the maximum required
									str_msg_error_tmp=this.field_max_value(obj_field);
									if(str_msg_error_tmp!="") 
									{
										boo_field_valid=false;
										str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
										str_RC="\n";
									}
								}
								break;
							case "datetime":
								// Verify that the value is a date : aaaa-mm-dd or aaaa-mm-dd hh:mm:ss
								str_msg_error_tmp=this.field_datetime(obj_field);
								if(str_msg_error_tmp!="") 
								{
									boo_field_valid=false;
									str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
									str_RC="\n";
								}
								break;
							case "email":
								// Verify that the value is an email : xxxxxx@xxxx.xxxx
								str_msg_error_tmp=this.field_email(obj_field);
								if(str_msg_error_tmp!="") 
								{
									boo_field_valid=false;
									str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
									str_RC="\n";
								}
								break;
							case "password":							
								var str_password = document.getElementById(obj_field.id).value;
								var str_password_confirm = document.getElementById(obj_field.id + "_confirm").value;
								if(this.mode == "add" || (this.mode == "edit" && (str_password != "" || str_password_confirm != ""))) {							
									if(boo_field_valid) {
										if(obj_field.min_length != 0 && obj_field.max_length != 0 ) {
											str_msg_error_tmp=this.field_min_length(obj_field);
											if(str_msg_error_tmp!="") 
											{
												boo_field_valid=false;
												str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
												str_RC="\n";
											}
											str_msg_error_tmp=this.field_max_length(obj_field);
											if(str_msg_error_tmp!="") 
											{
												boo_field_valid=false;
												str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
												str_RC="\n";
											}
										}
									}
									if(boo_field_valid) {
										if(str_password == "") {
											this.num_errors++;
											str_msg_error_tmp = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_password_empty"];
											boo_field_valid=false;
											str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
											str_RC="\n";
										}
										if(str_password_confirm == "") {
											this.num_errors++;
											str_msg_error_tmp = this.num_errors + " - " + obj_field.label + " (" + app["confirmation"] + ") : " + app["mandatory_password_confirm_empty"];
											boo_field_valid=false;
											str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
											str_RC="\n";
										}
									}
									if(boo_field_valid) {
										// Verify that password and password_confirm are the same
										str_msg_error_tmp=this.field_password(obj_field);
										if(str_msg_error_tmp!="") 
										{
											boo_field_valid=false;
											str_msg_error=str_msg_error+str_RC+str_msg_error_tmp;
											str_RC="\n";
										}
									}
								}
								break;
						}
						break;
					default:
				}

			}

			//str_msg_error = obj_field.id;
			// Send error message if requested
			if(boo_show_msg_error) 
			{
				return(str_msg_error);
			}
			else 
			{
				return("");
			}
		} 
		else 
		{
			// Object no exists => no message
			return("");
		}			
	}


	// Verify if field password is valid
	this.field_password = function (obj_field) 
	{
		var str_msg_error="";
		var str_password = document.getElementById(obj_field.id).value;
		var str_password_confirm = document.getElementById(obj_field.id + "_confirm").value;
		if (str_password != str_password_confirm) 
		{
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " (" + app["confirmation"] + ") : " + app["mandatory_password_confirm"];
		}
		return(str_msg_error);
	}

	// Verify if field value is an email
	this.field_email = function (obj_field) 
	{
		var str_msg_error="";
		var x = document.getElementById(obj_field.id).value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(x)) 
		{
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_email"];
		}
		return(str_msg_error);
	}


	// Verify if field value is a date with time
	this.field_datetime = function (obj_field) 
	{
   		var boo_result = true;
   		var boo_bisextil = false;
		var tab_months = new Array();
		tab_months[1] = 31;
		tab_months[2] = 28;
		tab_months[3] = 31;
		tab_months[4] = 30;
		tab_months[5] = 31;
		tab_months[6] = 30;
		tab_months[7] = 31;
		tab_months[8] = 31;
		tab_months[9] = 30;
		tab_months[10] = 31;
		tab_months[11] = 30;
		tab_months[12] = 31;
		var str_msg_error="";
		var x = document.getElementById(obj_field.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 res = filter.exec(x);
		if (!res) 
		{
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_datetime"];
		} else {
			if(res[1] < 2000 || res[1] > 2200) {
				boo_result = false;
			}
			if(boo_result) {
				if(res[2] < 1 || res[2] > 12) {
					boo_result = false;
				}
				if(boo_result) {
					if(this.isBi(res[1])) {
						tab_months[2] = 29; 
					}
					if(res[3] < 1 || res[3] > tab_months[parseInt(res[2],10)]) {
						boo_result = false;
					}
				}
			}
			if(!boo_result) {
				this.num_errors++;
				str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_datetime"];
			}
		}
		return(str_msg_error);
	}


	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.field_max_value = function (obj_field) 
	{
		var str_msg_error="";
		var str_value = document.getElementById(obj_field.id).value;
		if(str_value > obj_field.max_value) {
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_max_value"] + " " + obj_field.max_value;
		}
		return(str_msg_error);
	}


	// Verify if value is the minimum required
	this.field_min_value = function (obj_field) 
	{
		var str_msg_error="";
		var str_value =document.getElementById(obj_field.id).value;
		if(str_value < obj_field.min_value) {
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_min_value"] + " " + obj_field.min_value;
		}
		return(str_msg_error);
	}


	// Verify if field value is an integer
	this.field_int = function (obj_field) 
	{
		var str_msg_error="";
		var str_valid_chars = "0123456789-";
   		var str_char;
   		var boo_result = true;
		var str_string=document.getElementById(obj_field.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_result) 
				{
					boo_result = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_int"];
				}
			}
		}
		return(str_msg_error);
	}

	// Verify if field value is a float
	this.field_float = function (obj_field) 
	{
		var str_msg_error="";
		var str_valid_chars = "0123456789-.";
   		var str_char;
   		var boo_result = true;
		var str_string=document.getElementById(obj_field.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_result) 
				{
					boo_result = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_float"];
				}
			}
			if(str_string.indexOf(".") < 0) {
					boo_result = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_float"];
			}
		}
		return(str_msg_error);
	}

	// Verify if text length is valid
	this.field_min_length = function (obj_field) 
	{
		var str_msg_error="";
		var boo_field_valid=true;
		var str_value = new String(document.getElementById(obj_field.id).value);
		if(str_value.length < obj_field.min_length) {
			boo_field_valid = false;
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_min_length"] + obj_field.min_length + app["mandatory_character"];
		}
		return(str_msg_error);
	}

	// Verify if text length is valid
	this.field_max_length = function (obj_field) 
	{
		var str_msg_error="";
		var boo_field_valid=true;
		var str_value = new String(document.getElementById(obj_field.id).value);
		if(str_value.length > obj_field.max_length) {
			boo_field_valid = false;
			this.num_errors++;
			str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_max_length"] + obj_field.max_length + app["mandatory_character"];
		}
		return(str_msg_error);
	}


	// Verify if a mandatory field had a value
	this.field_mandatory = function (obj_field) 
	{
		var str_msg_error="";
		var boo_field_valid=true;
		
		// El tratamiento depende del tip de campo
		switch(obj_field.display_type) 
		{
			case "hidden":
			case "text":
			case "textarea":
			case "file":
			case "password":
				// Verify if there is a value in the field
				if(document.getElementById(obj_field.id).value=="") 
				{
					boo_field_valid = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_text"];
				}
				break;
			case "select":
				// Verify if there is a value selected
				if(document.getElementById(obj_field.id).selectedIndex < 0) {
					boo_field_valid = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_select"];
				}
				break;
			case "select_none":
				// Verify if there is a value selected (not None)
				if(document.getElementById(obj_field.id).selectedIndex < 1) {
					boo_field_valid = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_select_none"];
				}
				break;
			case "radio":
				// Verify if it is a table
				if(document.getElementsByName(obj_field.id)[0]) 
				{
					var boo_one_selected=false;
					// Verify if one element has been selected
					//alert(document.getElementsByName(obj_field.id).length);
					for(i=0;i<document.getElementsByName(obj_field.id).length;i++) 
					{
						if(document.getElementsByName(obj_field.id)[i].checked)
						{
							boo_one_selected=true;
						}
					}
					// No element selected => error
					if(!boo_one_selected) 
					{
						boo_field_valid=false;
						this.num_errors++;
						str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_radio"];
					}
				} 
				else 
				{
					// It is not a table => verify if element has been selected
					if (document.getElementsByName(obj_field.id).checked) 
					{
						// No error
					} 
					else 
					{
						// Element not selected => error
						boo_field_valid=false;
						this.num_errors++;
						str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_radio"];
					}
				}
				break;
			case "external":
				// Verify if there is an element in the list
				if(document.getElementById(obj_field.id).options.length <= 0) {
					boo_field_valid = false;
					this.num_errors++;
					str_msg_error = this.num_errors + " - " + obj_field.label + " : " + app["mandatory_external"];
				}
				break;
			case "key":
				break;
		}
		return(str_msg_error);
	}

}
