/**
 * @version 0.0.0.9
 * @author Paweł Łytko
 * @class Tools_form, klasa zawiera zestaw narzedzi do obsługi formularza umieszczonego na stronie Internetowej.
 * @param int p_id_form identyfikator formularza, kolejny numer dla danego formularza.
 *
 * Struktura obiektu konfiguracyjnego zawierającego dane określące dopuszczone wartości w danym polu
 * @class object_cfg
 * @property int _size_min, minimalny rozmiar danego pola, przeznaczone dla wartości typu string
 * @property int _size_max, maksymany rozmiar danego pola, przeznaczony dla wartości typu string
 * @property bool _read_maxlength, właściwość gdy przyjmuje wartość true wówczas w metodzie _get_size_max odczytuje wartość atrybutu: maxlength
 * @property RegExp (obiekt JavaScript, wyrażenie regularne) _reg_exp
 * @property bool _checked_element, sprawdzam czy dane pole jest zaznaczone, jesli true i item_requisite == true to pole musi być zaznaczone 
 * @property string _id, wartość atrybutu id znacznika. Właściwość jest obowiązkowe jeśli nie użyto właściwości: element_name.
 * @property string _name, wartość atrybutu name znacznika.
 * @property array _list_value, tablica zawiera listę wartość, które mogą wystąpić.
 * @property string _background_color_not, określa kolor tła dla danego pola gdy walidacja nie przeszła prawidłowo
 * @property string _background_color_ok, określa kolor tła dla danego pola gdy walidacja przeszła prawidłowo
 * @property bool _item_requisite, pole wymagane, true, pole nie wymagane, false.
 * @property bool _or_item, przykład użycia, numer telefonu, e-mail - jedno z tych pól musi wystąpić, należy ustawić na true. 
 * @method void cfg_item, metoda zawiera polecenia, wartość konfiguracyjne, dla danego pola umiejscowionego w formularzu.
 * @method int size_max_read() odczytuje wartość z atrybutu: maxlength
 * @method void not_validate metoda jest uruchamiana po walidacji, gdy walidacja się nie powiodła
 * @method void ok_validate metoda jest uruchamiana po walidacji, gdy walidacja się powiodła
 * 
 */

 function Tools_form(p_id_form)
 {
 	this._id_form = document.forms[p_id_form] != null ? p_id_form : document.forms[p_id_form];
 	this._background_color_not = "#B3ED98";
	this._background_color_ok = "#EEFFE6";
	this._object_html_message_ok = null;
	this._object_html_message_not = null;
	
	this._get_checked = function(p_object)
	{
		var return_value = false;
		try
		{
			return_value = document.getElementById(p_object._id).checked;
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
	
	this._get_value = function(p_object)
	{
		var return_value = "";
		try
		{
			if (p_object._id != null)
			{
				return_value = $("#" + p_object._id).val() != null ? $("#" + jQuery.trim(p_object._id)).val() : "";
			}
			else
			{
				if (p_object._name != null)
				{
					return_value = $("[name='" + p_object._name + "']:checked").val() != null ? $("[name='" + p_object._name + "']:checked").val() : "";
				}
			}
		}
		catch (object_exception)
		{
			return_value = "";
		}
		return return_value;
	}
	
	this._get_object_id = function(p_id, p_array_object)
	{
		var return_value = null;
		var i = 0;
		try
		{
			while (p_array_object.length > i)
			{
				if (p_array_object[i]._id == p_id)
				{
					return_value = p_array_object[i];
					i = p_array_object.length;
				}
				i++;
			}
		}
		catch (object_exception)
		{
			//window.alert(object_exception.message);
			return_value = null;
		}
		return return_value;
	}
	
	this._get_size_max = function(p_object)
	{
		var return_value = null;
		try
		{
			if (typeof p_object._read_maxlength == 'boolean')
			{
				return_value = p_object._read_maxlength == true ? parseInt($("#" + p_object._id).attr("maxlength")) : null
			} 
		}
		catch (object_exception)
		{
			return_value = null;
		}
		if (return_value == null)
		{
			try
			{
				if (typeof p_object._size_max == 'number')
				{
					return_value = parseInt(p_object._size_max);
				}
				else
				{
					return_value = p_object.size_max_read();
				}
			}
			catch (object_exception)
			{
				return_value = 255;
			}	
		}

		return (typeof return_value == 'number' ? return_value : 255);
	}
	
	this.set_background_color = function(p_not, p_ok)
	{
		this._background_color_not = p_not || "#C01515";
		this._background_color_ok = p_ok || "#E6E6E6";
	}
	
	this.set_object_html_message_ok = function(p_object)
	{
		this._object_html_message_ok = (typeof p_object == "object" ? p_object : null);
	}
	
	this.set_object_html_message_not = function(p_object)
	{
		this._object_html_message_not = (typeof p_object == "object" ? p_object : null);
	}
	
	this.cmd_validate = function(p_validate, p_object)
	{
		if (p_validate == true)
		{
			$("#" + p_object._id).css("background-color", this._background_color_ok);
			try
			{
				p_object.ok_validate();
			}
			catch (object_exception)
			{
				
			}
		}
		else
		{
			$("#" + p_object._id).css("background-color", this._background_color_not);
			try
			{
				p_object.not_validate();
			}
			catch (object_exception)
			{
				//window.alert(object_exception.message);
			}
		}
	}
	 	
 	/**
 	 * @method boolean is_load_form metoda zwraca wartość true, gdy został załadowany dany formularz, false gdy nie został załadowany dany formularz. 
 	 */
 	
 	this.is_load_form = function()
 	{
 		return this._id_form != null;
 	}
 	
 	/**
 	 * Metoda ładuje uruchamia metodę: cfg_item
 	 * @param object Array p_array_object,  
 	 */
 	
 	this.load_cfg = function(p_array_object)
 	{
 		var return_value = false;
 		var i = 0;
 		try
 		{
				while (p_array_object.length > i)
				{
					if (this.is_exists_element_id(p_array_object[i]._id) == true)
					{
						try
						{
							p_array_object[i].cfg_item();
						}
						catch (object_exception) {}
					}
				else
				{
					if (this.is_exists_element_name(p_array_object[i]._name) == true)
					{
						try
						{
							p_array_object[i].cfg_item();
						}
						catch (object_exception) {}
					}
				}
					i++;
				}
				return_value = true;
 		}
 		catch (object_exception)
 		{
 			return_value = false;
 		}
 		return return_value;
 	}
 	
 	/**
 	 * @method boolean is_exists_element_id, metoda sprawdza czy istnieje dany element 
 	 */
 	
	this.is_exists_element_id = function(p_id)
	{
		var return_value = false;
		try
		{
			return_value = $("#" + jQuery.trim(p_id)).length == 1;
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
	
	this.is_exists_element_name = function(p_name)
	{
		var return_value = false;
		try
		{
			return_value = $("[name='" + jQuery.trim(p_name) + "']").length > 0;
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
	
	this.is_validate_item = function(p_object)
	{
		var value_item = null;
		var return_value = false;
		var i = 0;
		try
		{
			value_item = this._get_value(p_object);
			if (p_object._size_min != null && this._get_size_max(p_object) != null && typeof p_object == "object")
			{
				if (p_object._item_requisite == true)
				{
					return_value = value_item.length >= p_object._size_min && value_item.length <= this._get_size_max(p_object);
				}
				else
				{
					return_value = value_item.length <= this._get_size_max(p_object);
				}
			}
			else
			{
				if (p_object._reg_exp != null)
				{
					if (p_object._item_requisite == true)
					{
						return_value = p_object._reg_exp.test(value_item);
					}
					else
					{
						return_value = value_item.length == 0 || p_object._reg_exp.test(value_item);
					}
					
				}
				else
				{
					if (p_object._list_value != null)
					{
						if (p_object._item_requisite == true)
						{
							while (p_object._list_value.length > i)
							{
								if (p_object._list_value[i].toLowerCase() == value_item.toLowerCase())
								{
									return_value = true;
									i = p_object._list_value;
								}
								i++;
							}
						}
						else
						{
							return_value = value_item.length == 0;
						}
					}
					else
					{
						if (p_object._checked_element != null)
						{
							if (p_object._item_requisite == true)
							{
								return_value = this._get_checked(p_object);
							}
							else
							{
								return_value = true;
							}
						}
					}
				}
			}
		}
		catch (object_exception)
		{
			//window.alert(object_exception.message);
			return_value = false;
		}
		return return_value;
	}
	
	/**
	 * @method boolean is_validate_item, metoda waliduje dany element z formularza.
	 * @param object p_list_object_cfg.
	 */
 	
 	this.set_submit = function(p_object)
 	{
 		var return_value = false;
 		var i = 0;
 		var licznik = 0;
 		var count_or_item = 0; //Ilość pól z ustawioną właściwością _or_item na true
 		var return_function = false;
 		var count_or_item_validate = 0;
 		var list_index = new Array();
 		try
 		{
			while (p_object.length > i)
			{
				return_function = this.is_validate_item(p_object[i]);
				this.cmd_validate(return_function, p_object[i]);
				if (return_function == false)
				{
					licznik++;
					if (typeof p_object[i]._or_item == "boolean" && p_object[i]._or_item == true)
					{
						list_index[count_or_item_validate] = i;
						count_or_item_validate++;
					}
				}
				else
				{
					if (typeof p_object[i]._or_item == "boolean" && p_object[i]._or_item == true && this._get_value(p_object[i]) == "")
					{
						list_index[count_or_item_validate] = i;
						count_or_item_validate++;
					}
				}
				if (typeof p_object[i]._or_item == "boolean")
				{
					if (p_object[i]._or_item == true)
					{
						count_or_item++;
					}
				}
				i++;
			}
			if (count_or_item > 0)
			{
				i = 0;
				return_value = licznik == 0 && count_or_item > count_or_item_validate;
				while (list_index.length > i)
				{
					if (count_or_item == count_or_item_validate)
					{
						if (typeof this._object_html_message_not == "object")
						{
							this.cmd_validate(false, p_object[list_index[i]]);
							this._object_html_message_not.add_before();
						}
					}
					else
					{
						if (typeof this._object_html_message_ok == "object")
						{
							this.cmd_validate(true, p_object[list_index[i]]);
							this._object_html_message_ok.add_before();
						}
					}
					i++;
				}
			}
			else
			{
				return_value = licznik == 0;
			}
 		}
 		catch (object_exception)
 		{
 			return_value = false;
 		}
 		return return_value;
 	}
 	
 	this.set_events_blur = function(p_array_object)
 	{
 		var i = 0;
 		var object_this = this;
 		var parametr_object = p_array_object;
 		try
 		{
			while (p_array_object.length > i)
			{
				$("#" + p_array_object[i]._id).blur(
					function()
					{
						var this_object = object_this._get_object_id(this.id, parametr_object);
						object_this.cmd_validate(object_this.is_validate_item(this_object), this_object);
					});
				i++;
			}
 		}
 		catch (object_exception)
 		{
 			//window.alert("Wartość 1: " + object_exception.message);
 		}
 	}
 	
 	this.set_events_change = function(p_array_object)
 	{
 		var i = 0;
 		var object_this = this;
 		var parametr_object = p_array_object;
 		try
 		{
			while (p_array_object.length > i)
			{
				$("#" + p_array_object[i]._id).change(
					function()
					{
						var this_object = object_this._get_object_id(this.id, parametr_object);
						object_this.cmd_validate(object_this.is_validate_item(this_object), this_object);
					});
				i++;
			}
 		}
 		catch (object_exception)
 		{
 			//window.alert("Wartość 1: " + object_exception.message);
 		}
 	}
 	
 }