/*
* Add event by object
*
* @param <object> element - object for witch created event
* @param <string> eventType - type of event (without prefix 'on')
* @param <function> fn - function witch was process event
*/
function addEvent(obj, eventType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(eventType, fn, false);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent('on' + eventType, fn);
		return r;
	} else {
		obj['on' + eventType] = fn;
	}
}

/*********************************** Begin of definitions of CheckInput ************************************/

/*
* Constructor of CheckInput
*
* @param <array> oForm - array of [formName, after_submit_function_name, before_submit_fuction_name]
* @param <array> oInput - array of arrays of
		@item <string> - obligatory param, element name;
		@item <string> - optional parametr (default 'er_'+{element name}), specifire error message block by id;
		@item <array> - optional parametr, array of ['check_function', 'error_msg'], can be a few;
		@item <array> - optional parametr, array of ['modify_function'], can be a few;
* @param <array> oParams - array of
		[0] - what use to show/hide errors - display (0 - default value) or visibility (1)
*/
function CheckInput(oForm, oInput, oParams) {
	this.form            = '';
	this.before_submit_function = '';
	this.after_submit_function  = ''; 
	this.ItemsObj        = new Array();
	this.ItemsErrObj     = new Array();
	this.ItemsStates     = new Array();
	this.CheckFunctions  = new Array();
	this.CheckMessages   = new Array();
	this.ModifyFunctions = new Array();

	this.parseData(oForm, oInput, oParams);

	var oThis = this;
	this._onBlur   = function (e) { oThis.onBlur(e);   };
	this._onKeyup  = function (e) { oThis.onKeyup(e);  };
	this._onSubmit = function (e) { oThis.onSubmit(e); };

	for (i in this.ItemsObj) {
		obj = this.ItemsObj[i];
		addEvent(obj, "blur", this._onBlur);
		addEvent(obj, "keyup", this._onKeyup);
	}
	addEvent(this.form, 'submit', this._onSubmit);
}

/*
* Parse all input data
*
* @param - the same as for constractor
*/
CheckInput.prototype.parseData = function(oForm, oInput, oParams) {
	eval("this.form=document."+oForm[0]+";");
	if ("undefined" != typeof oForm[1]) {
		this.before_submit_function = oForm[1];
	}
	if ("undefined" != typeof oForm[2]) {
		this.after_submit_function  = oForm[2];
	}

	index = 0;
	oInput = oInput || [];
	for (var i in oInput) {
		if ('object' == typeof oInput[i]) {
			this.CheckFunctions[index]  = new Array();
			this.CheckMessages[index]   = new Array();
			this.ModifyFunctions[index] = new Array();
			this.ItemsStates[index]     = true;
			c_index = 0;
			m_index = 0;
			for (var j in oInput[i]) {
				if ('string' == typeof oInput[i][j]) {
					if ('undefined' == typeof this.ItemsObj[index]) {
						eval("this.ItemsObj[index]=this.form."+oInput[i][j]+";");
						this.ItemsErrObj[index] = document.getElementById("er_"+oInput[i][j]);
					} else {
						this.ItemsErrObj[index] = document.getElementById(oInput[i][j]);
					}
				} else {
					if (2 == oInput[i][j].length) {
						this.CheckFunctions[index][c_index] = oInput[i][j][0];
						this.CheckMessages[index][c_index]  = oInput[i][j][1];
						c_index++;
					} else if (1 == oInput[i][j].length) {
						this.ModifyFunctions[index][m_index] = oInput[i][j][0];
						m_index++;
					}
				}
			}
			index++;
		}
	}

	this.show_mode = ((null == oParams) || (oParams[0] == null) || (oParams[0] > 1)) ? 0 : oParams[0];
}
/*
* onSubmit process function
* Run 'before_submit_function', run checkForm: if success - submit form, run 'after_submit_function'; else - stop submiting
*/
CheckInput.prototype.onSubmit =  function(e) {
	if ('' != this.before_submit_function) {
		eval(this.before_submit_function+"();");
	}
	if (!this.checkForm()) {
		if (e.preventDefault) {
			e.preventDefault();		
		} else {
			e.returnValue=false;
		}	
		
	} else {
		if ('' != this.after_submit_function) {
			eval(this.after_submit_function+"();");
		}
	}
}

/*
* Check form before submit
*
* @return - true or false
*/
CheckInput.prototype.checkForm = function() {
	global_result = true;
	for (var i in this.ItemsObj) {
		obj = this.ItemsObj[i];
		element_result = true;
		for (var j in this.CheckFunctions[i]) {
			eval('result = '+this.CheckFunctions[i][j]+'(obj.value);');
			if (!result) {
				global_result = false;
				element_result = false;
				this.showErrorBlock(i, j);
				this.ItemsStates[i] = false;
				break;
			}
		}
		if (element_result) {
			this.hideErrorBlock(i);
		}
	}
	return global_result;
}

/*
* get element Index, if known or -1 if unknown
*/
CheckInput.prototype.getItemId = function(el) {
	for (i in this.ItemsObj) {
		if (this.ItemsObj[i] == el) return i;
	}
	return -1;
}

/*
* onBlur process function - check all validation functions for current element.
* If some validation function return 'false' - show error messages for this function and change ItemStates flag to 'false' for this element.
* If all validation function return 'true' - hide error message;
*/
CheckInput.prototype.onBlur = function(e) {
	var el = e.target || e.srcElement;
	var i = this.getItemId(el);
	if (-1 != i) {
		obj = this.ItemsObj[i];
		element_result = true;
		for (var j in this.CheckFunctions[i]) {
			eval('result = '+this.CheckFunctions[i][j]+'(obj.value);');
			if (!result) {
				element_result = false;
				this.showErrorBlock(i, j);
				this.ItemsStates[i] = false;
				break;
			}
		}
		if (element_result) {
			this.hideErrorBlock(i);
		}
	}
}

/*
* onKeyup process function - apply all modification functions to value of current element
* If ItemStates flag set to 'false' for this element - check all validation functions - run onBlur method
*/
CheckInput.prototype.onKeyup = function(e) {
	var el = e.target || e.srcElement;
	i = this.getItemId(el);
	if (-1 != i) {
		obj = this.ItemsObj[i];
		beforeModifyValue = obj.value;
		result = obj.value;
		for (var j in this.ModifyFunctions[i]) {
			eval('result = '+this.ModifyFunctions[i][j]+'(result);');
		}
		if (beforeModifyValue != result) {
			obj.value=result;
		}
		if (this.ItemsStates[i] == false) {
			this.onBlur(e);
		}
	}
}

/*
* Show error block function
*/
CheckInput.prototype.showErrorBlock = function(itemId, errMsgId) {
	if (obj = this.ItemsErrObj[itemId]) {
		obj.innerHTML = this.CheckMessages[itemId][errMsgId];
		obj.style.display = (this.show_mode == 0) ? 'block' : 'visible';
	}
}

/*
* Hide error block function
*/
CheckInput.prototype.hideErrorBlock = function(itemId) {
	if (obj = this.ItemsErrObj[itemId]) {
		obj.innerHTML = "";
		obj.style.display = (this.show_mode == 0) ? 'none' : 'hidden';
	}
}

/*********************************** End of definitions of CheckInput ************************************/


function is_not_empty(s) {
	for(i = 0; i < s.length; i++) {
		if(s.charAt(i) != ' ') {
			return true;
		}
	}
	return false;
}

function check_url(s) {
	var reg = new RegExp("[^a-zA-Z_0-9/\]+");
	if (s=='' || reg.test(s)) {
		return false;
	} else {
		return true;
	}
}

function check_urlFull(s) {
	var reg = new RegExp("[^a-zA-Z_0-9/\.\+\%&?=]+");
	if (s=='' || reg.test(s)) {
		return false;
	} else {
		return true;
	}
}

function removeChars(s) {
	filteredValues = "1234567890";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) != -1) {
				returnString += c;
		}
	}
	return returnString;
}

function check_email(s) {
	if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-zA-Z]{2,6}(?:\.[a-zA-Z]{2})?)$/.test(s)) {
		return true;
	}
	return false;
}