/**
 * @author Pawel Lytko
 */

 function Lenght_validate(p_min, p_max)
 {
 	this._min = -1;
	this._max = -1;
	this._value = '';
	this._is_required = false;
	
	try
	{
		this._min = typeof p_min == "number" ? parseInt(p_min) : -1;
		this._max = typeof p_max == "number" ? parseInt(p_max) : -1;
	}
	catch (object_exception)
	{
		this._min = -1;
		this._max = -1;
	}
	
	this.set_value = function(p_value)
	{
		var return_value = true;
		try
		{
			this._value = typeof p_value == "undefined" ? "" : p_value;
		}
		catch (object_exception)
		{
			this._value = '';
			return_value = false;
		}
		return return_value;
	}
	
	this.set_required = function(p_value)
	{
		var return_value = true;
		try
		{
			this._is_required = typeof p_value == "boolean" ? p_value : false;
		}
		catch (object_exception)
		{
			return_value = false;
			this._is_required = false;
		}
		return return_value;
	}
	
	this.is_validate = function()
	{
		var return_value = false;
		try
		{
			if (typeof this._min == "number" && typeof this._max == "number" && typeof this._is_required == "boolean")
			{
				if (this._is_required == true)
				{
					if (parseInt(this._min) > -1 && parseInt(this._max) > -1)
					{
						return_value = this._value.length >= parseInt(this._min) && this._value.length <= parseInt(this._max);
					}
				}
				else
				{
					if (parseInt(this._max) > -1 && this._value.length > 0)
					{
						return_value = this._value.length >= 0 && this._value.length <= parseInt(this._max);
					}
					else
					{
						return_value = this._value.length == 0;
					}
				}
			}
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
 }

 function Reg_exp_validate(p_reg_exp_contents)
 {
 	this._reg_exp_contents = '';
	this._is_required = false;
	this._value = '';
	try
	{
		this._reg_exp_contents = typeof p_reg_exp_contents == "undefined" ? "" : p_reg_exp_contents;
	}
	catch (object_exception)
	{
		this._reg_exp_contents = '';
	}
	
	this.set_value = function(p_value)
	{
		var return_value = true;
		try
		{
			this._value = typeof p_value == "undefined" ? "" : p_value;
		}
		catch (object_exception)
		{
			this._value = '';
			return_value = false;
		}
		return return_value;
	}
	
	this.set_required = function(p_value)
	{
		var return_value = true;
		try
		{
			this._is_required = typeof p_value == "boolean" ? p_value : false;
		}
		catch (object_exception)
		{
			return_value = false;
			this._is_required = false;
		}
		return return_value;
	}
	
	this.is_validate = function()
	{
		var return_value = false;
		var walidacja = null;
		try
		{
			if (typeof this._is_required == "boolean" && typeof this._value != "undefined" && typeof this._reg_exp_contents != "undefined" && this._reg_exp_contents != "")
			{
				if (this._is_required == true)
				{
					walidacja = new RegExp(this._reg_exp_contents);
					return_value = walidacja.test(this._value);
				}
				else
				{
					if (this._value.length == 0)
					{
						return_value = true;
					}
					else
					{
						walidacja = new RegExp(this._reg_exp_contents);
						return_value = walidacja.test(this._value);
					}
				}
			}
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
 }

 function Validate_value_object()
 {
	this._background_color_ok = '';
	this._background_color_not = '';
	this._object_validate = null;
	
	this.set_background_color = function(p_background_color_ok, p_background_color_not)
	{
		try
		{
			this._background_color_ok = typeof p_background_color_ok == "string" ? p_background_color_ok : "";
			this._background_color_not = typeof p_background_color_not == "string" ? p_background_color_not : "";
		}
		catch (object_exception)
		{
			this._background_color_ok = '';
			this._background_color_not = '';
		}
	}
	
	this.set_object_lenght = function(p_min, p_max, p_is_required)
	{
		try
		{
			if (this._object_validate == null)
			{
				this._object_validate = new Lenght_validate(p_min, p_max);
				this._object_validate.set_required(p_is_required);
			}
		}
		catch (object_exception)
		{
			this._object_validate == null
		}
	}
	
	this.set_object_reg_exp = function(p_reg_exp_contents, p_is_required)
	{
		try
		{
			if (this._object_validate == null)
			{
				this._object_validate = new Reg_exp_validate(p_reg_exp_contents);
				this._object_validate.set_required(p_is_required);
			}
		}
		catch (object_exception)
		{
			this._object_validate == null
		}
	}
	
	this.is_validate = function(p_object)
	{
		var return_value = false;
		try
		{
			if (this._object_validate != null && typeof p_object == "object" && typeof this._background_color_ok == "string" && typeof this._background_color_not == "string")
			{
				this._object_validate.set_value(p_object.value);
				return_value = this._object_validate.is_validate();
			}
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
	
	this.validate = function(p_object)
	{
		var return_value = false;
		try
		{
			if (this._object_validate != null && typeof p_object == "object" && typeof this._background_color_ok == "string" && typeof this._background_color_not == "string")
			{
				this._object_validate.set_value(p_object.value);
				if (this._object_validate.is_validate())
				{
					p_object.style.backgroundColor = this._background_color_ok;
					return_value = true;
				}
				else
				{
					p_object.style.backgroundColor = this._background_color_not;
					return_value = false;
				}
			}
		}
		catch (object_exception)
		{
			return_value = false;
		}
		return return_value;
	}
 }
