/**
 * @version 0.0.0.11
 * @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
 * Właściwości / metody, które należy ustawiać dla każdego elementu formularza
 * @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. Należy podać nazwę klasy css
 * @property string _background_color_ok, określa kolor tła dla danego pola gdy walidacja przeszła prawidłowo. Należy podać nazwę klasy css
 * @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.
 * @property string _kontener_msg, nazwa znacznika, który posiada pojedyncze komunikaty wyświetlane przez formularz
 * -----------------------------------
 * @property string _kontener_msg_id, wartość atrybutu id znacznika: _kontener_msg
 * @property string _kontener_msg_class, wartość atrybutu class znacznika: _kontener_msg
 * Dwie powyższe właściwości można używać zamiennie i służą do jednoznaczej identyfikacji w drzewie DOM znacznika z komunikatami.
 * -----------------------------------
 * @property string _item_msg, nazwa znacznika zawierającego komunikat
 * @property string _item_msg_id, wartość znacznika id obiektu: _item_msg
 * @property string _message_ok, komunikat wyświetlany gdy pole na formularzu zostało prawidłowo zwalidowane
 * @property string _message_not, komunikat wyświetlany gdy pole na formularzu zostało nie prawidłowo zwalidowane
 * @method void cfg_item, metoda zawiera polecenia, wartość konfiguracyjne, dla danego pola umiejscowionego w formularzu.
 * @method int size_max_read() zwraca maksymalny rozmiar danego pola, wnętrze metody jest definiowana po za klasą.
 * @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
 * @method void _not_validate, metoda prywatna wykonywana 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)
 {
 	/*
 	 * @property string _id_form, wartość atrybutu id znacznika: form
 	 * @property string _background_color_not, nazwa klasy css dla pola, które zostało nie poprawnie zwalidowane
 	 * @property string _background_color_ok, nazwa klasy css dla pola, które zostało poprawnie zwalidowane
 	 * @property boolean _after, komunikat zostaną umieszczna za elementem form
 	 * @property boolean _before, komunikat zostaną umieszczna przed elementem form
 	 */
 	this._id_form = document.forms[p_id_form] != null ? p_id_form : document.forms[p_id_form];
 	this._background_color_not = "not_walidaes_input";
	this._background_color_ok = "ok_walidaes_input";
	this._after = null;
	this._before = null;
	this._list_item_or = new Array();
	
	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
			{
				return_value = parseInt(p_object.size_max_read());
			}
			catch (object_exception)
			{
				return_value = 255;
			}	
		}

		return (typeof return_value == 'number' ? return_value : 255);
	}
	
	this._not_validate = function(p_object)
	{
		var object_this = this;
		service_object = new Tools_html();
		try
		{
			service_object.set_siblings({
				_name: 'form',
				_list_attributes: new Array(
					{
						_name: 'id',
						_value: object_this._id_form,
						_operator: '='
					})});
			service_object.set_new_object({
				_name: p_object._kontener_msg,
				_object_child: {
					_name: p_object._item_msg,
					_txt: p_object._message_not,
					_list_attributes: new Array({
						_name: 'id',
						_value: p_object._item_msg_id,
						_operator: '='
					})},
				_list_attributes: new Array(
					{
						_name: service_object.get_name_attribute(p_object._kontener_msg_id, p_object._kontener_msg_class),
						_value: service_object.get_if_value(p_object._kontener_msg_id, p_object._kontener_msg_class),
						_operator: '='
					})
			});
			if (object_this._after == true)
			{
				service_object.add_after();
			}
			if (object_this._before == true)
			{
				service_object.add_before();
			}
		}
		catch (object_exception)
		{
			//window.alert(object_exception.message);
		}
	}
	
	this._ok_validate = function(p_object)
	{
		var object_this = this;
		service_object = new Tools_html();
		try
		{
			service_object.set_siblings({
				_name: 'form',
				_list_attributes: new Array(
					{
						_name: 'id',
						_value: object_this._id_form,
						_operator: '='
					})});
			service_object.set_new_object({
				_name: p_object._kontener_msg,
				_object_child: {
					_name: p_object._item_msg,
					_txt: p_object._message_ok,
					_list_attributes: new Array({
						_name: 'id',
						_value: p_object._item_msg_id,
						_operator: '='
					})},
				_list_attributes: new Array(
					{
						_name: service_object.get_name_attribute(p_object._kontener_msg_id, p_object._kontener_msg_class),
						_value: service_object.get_if_value(p_object._kontener_msg_id, p_object._kontener_msg_class),
						_operator: '='
					})
			});
			if (service_object.is_exists_element(service_object.get_string_siblings()) == true)
			{
				if (object_this._after == true)
				{
					service_object.add_after();
				}
				if (object_this._before == true)
				{
					service_object.add_before();
				}
			}
			service_object.delete_nothing(service_object.get_string_new_object());
		}
		catch (object_exception)
		{
			//window.alert(object_exception.message);
		}
	}
	
	this._walidate_or = function(p_list_item_or)
	{
		var i = 0;
		var v_return = 0;
		try
		{
			while (p_list_item_or.length > i)
			{
				if (this.is_validate_item(p_list_item_or[i]) == false)
				{
					v_return++;
				}
				i++;
			}
		}
		catch (object_exception)
		{
			v_return = p_list_item_or.length;
			window.alert(object_exception.message);
		}
		return v_return;
	}
	
	this.set_after = function()
	{
		this._after = true;
		this._before = false;
	}
	
	this.set_before = function()
	{
		this._after = false;
		this._before = true;
	}
	
	this.set_background_color = function(p_not, p_ok)
	{
		this._background_color_not = p_not || "not_walidaes_input";
		this._background_color_ok = p_ok || "ok_walidaes_input";
	}
	
	this.cmd_validate = function(p_validate, p_object)
	{
		if (p_validate == true)
		{
			if ($("#" + p_object._id).hasClass(this._background_color_not) == true)
			{
				$("#" + p_object._id).removeClass(this._background_color_not)
			}
			if ($("#" + p_object._id).hasClass(this._background_color_ok) == false)
			{
				$("#" + p_object._id).toggleClass(this._background_color_ok);
			}
			try
			{
				if (p_object._kontener_msg != null && p_object._kontener_msg != '')
				{
					if (this._walidate_or(this._list_item_or) == 0)
					{
						this._ok_validate(p_object);
					}
				}
				else
				{
					if (this._walidate_or(this._list_item_or) == 0)
					{
						p_object.ok_validate();
					}
				}
			}
			catch (object_exception)
			{
				//window.alert(object_exception.message);
			}
		}
		else
		{
			if ($("#" + p_object._id).hasClass(this._background_color_ok) == true)
			{
				$("#" + p_object._id).removeClass(this._background_color_ok)
			}
			if ($("#" + p_object._id).hasClass(this._background_color_not) == false)
			{
				$("#" + p_object._id).toggleClass(this._background_color_not);
			}
			try
			{
				if (p_object._kontener_msg != null && p_object._kontener_msg != '')
				{
					this._not_validate(p_object);
				}
				else
				{
					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;
	}
	
	this.set_list_item_or = function(p_object)
	{
		var i = 0;
		try
		{
			while (this._list_item_or.length > 0)
			{
				this._list_item_or.shift();
			}
			while (p_object.length > i)
			{
				if (typeof p_object[i]._or_item == "boolean" && p_object[i]._or_item == true)
				{
					this._list_item_or.push(p_object[i]);
				}
				i++;
			}
		}
		catch (object_exception)
		{
			//window.alert(object_exception.message);
		}
	}
	
	/**
	 * @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 return_function = false;
 		var count_or_item_validate = 0;
 		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)
					{
						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]) == "")
					{
						count_or_item_validate++;
					}
				}
				i++;
			}
			if (this._list_item_or.length > 0)
			{
				i = 0;
				return_value = licznik == 0 && this._list_item_or.length > count_or_item_validate;
				if (return_value == false)
				{
					while (this._list_item_or.length > i)
					{
						this.cmd_validate(false, this._list_item_or[i]);
						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);
 		}
 	}
 	
 }