/*
*	@package  CheckInput
*	@author    vovan
*	
*	@modified 25.01.2005 by aubergine
*	@tDate was added and hide error tag also was added to the init function
*	@oParams[1] - was added
*	setMessage was added

*	@modified 21.02.2005 by aubergine
*	CheckInput.tPorts was added



oInput array of (input, type, div_for_error, error_message(optional))
oParams - array of parameters
	[0] - what use to show/hide errors - display (0 - default value) or visibility (1)
	[1] - new className for error tags
*/
function CheckInput(oForm, oInput, oParams) {
	this.Input = oInput || [];
	
	this.initParams(oParams);
	
	var oThis = this;
	this._iOnblur = function (e) {
		oThis.iOnblur(e);
	};
	if (oInput) {
		this.initInput(oForm, oInput);
		this.document = oInput.ownerDocument || oInput.document;
	}
	else {
		this.document = document;
	}
}

CheckInput.gecko = navigator.product == "Gecko";
CheckInput.msie = /msie/i.test(navigator.userAgent);

CheckInput.prototype._checkTypeInfo = {};

CheckInput.prototype.initParams = function (oParams) {
	this.params = oParams || [];
	
	this.params[0] = (this.params[0] == null || this.params[0] > 1) ? 0 : this.params[0];
	this.params[1] = (this.params[1] == null) ? 'errors_disable' : this.params[1];
}

CheckInput.prototype.initInput = function (oForm, oInput) {
	var i, inp, form;
	for (i=0; i<oInput.length; i++) {
		eval('form = document.'+oForm);
		eval('inp = form.'+oInput[i][0]);
		this.Input[i][0] = inp;
		
		if (typeof inp.addEventListener != "undefined"){
			inp.addEventListener("blur", this._iOnblur, false);
		} else if (typeof inp.attachEvent != "undefined") {// for IE
			inp.attachEvent("onblur", this._iOnblur, false);
		} else {
			inp.onblur = this._iOnblur;
		}
		
		this.Input[i][1] = this.Input[i][1].toLowerCase();
		this.Input[i][2] = document.getElementById(this.Input[i][2]);
		this.Input[i][2].className = this.params[1];
		
		if (this.setMessage(i, true)) {
			this.hide(i);
		}
	}
};

CheckInput.prototype.iOnblur = function (e) {
	var el = e.target || e.srcElement;
	var i = this.getCheckId(el);
	if (i != -1 || this.Input[i][1] == "none") {
		var f = this.getCheckFunction(this.Input[i][1]);
		this.setMessage(i);
		if (f(el.value)) {
			this.hide(i);
			return true;
		} else {
			this.show(i);
			return false;
		}
	}
	return true;
};

CheckInput.prototype.iOnsubmit = function (e) {
	var i,f;
	for (i=0;i<this.Input.length;i++) {
		f = this.getCheckFunction(this.Input[i][1]);
		this.setMessage(i);
		if (!f(this.Input[i][0].value)) {
			this.show(i);
			this.Input[i][0].focus();
			return false;
		}
	}
	return true;
};

CheckInput.prototype.show = function (i) {
	if (this.params[0] == 0) {
		this.Input[i][2].style.display='block';
	} else {
		this.Input[i][2].style.visibility = "visible";
	}
}

CheckInput.prototype.hide = function (i) {
	if (this.params[0] == 0) {
		this.Input[i][2].style.display='none';
	} else {
		this.Input[i][2].style.visibility = "hidden";
	}
}
CheckInput.prototype.getCheckId = function (el) {
	var i;
	for (i=0; i<this.Input.length;i++) {
		if (this.Input[i][0] == el) {
			return i;
		}
	}
	return -1;
};

// specify second param to true if you what to show js error only if they 
// didnt specified by php
CheckInput.prototype.setMessage = function (i) {
	var text;
	var live_existing_error = (arguments[1]) ? true : false;
	
	if (live_existing_error == true && this.Input[i][2].innerHTML != '') {
		return false;
	}
	
	this.Input[i][2].innerHTML = '';
	if (this.Input[i][3] != null) {
			text = document.createTextNode(this.Input[i][3]);
		} else {
			text = document.createTextNode(this.getMessage(this.Input[i][1]));
		}
	this.Input[i][2].appendChild(text);
	
	return true;
}

CheckInput.prototype.getMessage = function (sType, element) {
	if (this._checkTypeInfo[sType] && this._checkTypeInfo[sType].message)
		return this._checkTypeInfo[sType].message;
}

CheckInput.prototype.getCheckFunction = function (sType) {
	if (this._checkTypeInfo[sType])
		return this._checkTypeInfo[sType].check;
	return CheckInput.basicCheck;
};

CheckInput.prototype.destroy = function () {
	this._onunload = null;
	this.element = null;
	this.document = null;
	this._iOnblur = null;
	this.checkTypes = null;
};

CheckInput.prototype.addCheckType = function (sType, fCheckFunction, sMessage) {
	sType=sType.toLowerCase();
	this._checkTypeInfo[sType] = {
		type:			sType,
		check:			fCheckFunction || SortableTable.basicCheck,
		message:		sMessage
	};
};

// this removes the check type from all instances of CheckInput
CheckInput.prototype.removeCheckType = function (sType) {
	delete this._checkTypeInfo[sType];
};

// check functions
// s - value of input
// return true - all OK
//	false - Errors
CheckInput.basicCheck = function check(s) {
	return true;
};

CheckInput.tName = function (s) {
	for(i = 0; i < s.length; i++) {
		if(s.charAt(i) != ' ') {
			return true;
		}
	}
	return false;
};
CheckInput.tUrl = function (s) {
	var reg = new RegExp("[^a-zA-Z_0-9/\]+");
	if (s=='' || reg.test(s)) {
		return false;
	} else {
		return true;
	}
}
CheckInput.tUrlFull = function (s) {
	var reg = new RegExp("[^a-zA-Z_0-9/\.\+\%&?=]+");
	if (s=='' || reg.test(s)) {
		return false;
	} else {
		return true;
	}
}
CheckInput.tDate = function (s) {
	
	var date_correct = true;
	var date_now = new Date();
	var dateStr = s;
	
	dateStr = dateStr.replace('.','-');
	dateStr = dateStr.replace('.','-');
	
	var datePat = /^(\d{1,2})-(\d{1,2})-(\d{1,2})$/;
	var datePat1 = /^(\d{1,2})-(\d{1,2})-(\d{4})$/;

	matchArray = new Array();

	var matchArray = dateStr.match(datePat); // is the format ok?
	var matchArray1 = dateStr.match(datePat1); // is the format ok?

	if (matchArray == null) {
		matchArray = matchArray1;
	}
	
	if (matchArray == null) {
	
		date_correct = false;
		
	} else {

		day = matchArray[1]; // parse date into variables
		month = matchArray[2];
		year = matchArray[3];
	
		if (year.length != 4) {
			year_delta = 4 - year.length;
			now_year = '' + date_now.getYear();
			year = now_year.substring(0, year_delta) + year;
		}
	
		var date_tc = new Date(month + '/' + day + '/' + year);
	
		c_day = date_tc.getDate();
		c_month = date_tc.getMonth() + 1;
		c_year = date_tc.getYear();
		
		if (c_year<1000) {
			c_year += 1900;
		}
	
		if ((c_day != day) || (c_month != month) || (c_year != year)) {
			date_correct = false;
		}
	}

	if ((dateStr == '') || (!date_correct)) {
		return false;
	} else {
		return true;
	}
}

CheckInput.tPorts = function (s) {
	
	var str = s;
	var f = false;
	var num = '';
	var i = 0;
	while(i < str.length) {
		while (str.substring(i, i + 1) == ' ') i++;
		while(str.substring(i, i + 1) <= '9' && str.substring(i, i + 1) >= '0') {
			num += str.substring(i, i + 1);
			i++;
		}
		while (str.substring(i, i + 1) == ' ') i++;
		if((str.substring(i, i + 1) == ',' || i == str.length) && num != '') {
			num = '';
		} else {
			f = true;
			break;
		}
		i++;
	}
	
	return !f;

}




// add check type (type_name, check_fuction_name, defaul_error_message)
CheckInput.prototype.addCheckType("Name", CheckInput.tName, 'This field can\'t be empty');
CheckInput.prototype.addCheckType("Url", CheckInput.tUrl, 'Wrong url');
CheckInput.prototype.addCheckType("UrlFull", CheckInput.tUrlFull, 'Wrong url');
CheckInput.prototype.addCheckType("Date", CheckInput.tDate, ' Wrong Date (DD.MM.YYYY).');
CheckInput.prototype.addCheckType("Ports", CheckInput.tPorts, ' Wrong ports');