var frm = '';
var err = 0;
var errType = 'box'; // box , color
var isShowErr = 0;
var strFocus = '';
var ObjArray = new Array();
var ObjArrayCount = 0;
var errColor = 'red';
var errColorReset = 'white';

function setForm(formname){
	frm = formname;
	err = 0;
	isShowErr = 0;

	if (errType == 'color'){
		for(i=1;i<ObjArray.length;i++){
			strErrColor = 'document.all.' + frm + '.' + ObjArray[i] + '.style.background = errColorReset';
			eval(strErrColor);
		}
	}

	ObjArray = new Array();
	ObjArrayCount = 0;
}

function setErrType(types){
	errType = types;
}

function isErr(){
	if (err > 0){
		return true;
	}else{
		return false;
	}
}

function submitForm(){

	if (err == 0){
		frmSubmit = 'document.' + frm + '.submit()';
		eval(frmSubmit);
		return true;
	}
	return false;
}

function setErr(obj){
	ObjArrayCount++;
	ObjArray[ObjArrayCount] = obj;
	err++;
}

function isEmpty(obj){

	strCheck = "document." + frm + "." + obj + ".value == \"\"";
	strCheck2 = "document." + frm + "." + obj + ".value.charAt(0) == \" \"";

	if ( eval(strCheck) || eval(strCheck2)){
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function isNumeric(obj){

	var Pat = /^[0-9]{1,}$/;

	strCheck = 'document.' + frm + '.' + obj + '.value';
	strCheck = eval(strCheck);

	var matchArray = strCheck.match(Pat); // is the format ok?

	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function isNumber(obj){

	var valid = "-+,.0123456789";

	strCheck = 'document.' + frm + '.' + obj + '.value';

	strLength = eval(strCheck);

	for (var i=0; i<strLength.length; i++) {
		temp = "" + strLength.substring(i, i+1);
		if (valid.indexOf(temp) == "-1"){
			strFocus = 'document.' + frm + '.' + obj + '.focus()';
			setErr(obj);
			return true;
		}
	}
	return false;
}

function isInteger(obj){
	var Pat = /^[0-9]{1,}$/;

	strCheck = 'document.' + frm + '.' + obj + '.value';
	//strCheck = eval(strCheck);
	rExp = /,/gi;
	strCheck = eval(strCheck).replace(rExp,'');

	var matchArray = strCheck.match(Pat); // is the format ok?

	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}

	if (strCheck > 32767) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false
}

function isMoney(obj){
	var Pat = /^[0-9,]{1,}.{0,1}[0-9]{0,}$/;

	strCheck = 'document.' + frm + '.' + obj + '.value';
	strCheck = eval(strCheck);

	var matchArray = strCheck.match(Pat); // is the format ok?

	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function isDecimal(obj,inMin1,inMax1,inMin2,inMax2){

	strCheck = 'document.' + frm + '.' + obj + '.value';
	rExp = /,/gi;
	strCheck1 = eval(strCheck).replace(rExp,'');
	strPat = '/^[-+]?\\d{'+inMin1+','+inMax1+'}(\\.\\d{'+inMin2+','+inMax2+'})?$/.test(strCheck1)';
	if (eval(strPat)){
		eval(strCheck + ' = Number(strCheck1).toFixed(inMax2);');
		return false;
	}else{
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
}
function isDecimal(obj,inMin1,inMax1,inMin2,inMax2,from,to){

	strCheck = 'document.' + frm + '.' + obj + '.value';
	rExp = /,/gi;
	strCheck1 = eval(strCheck).replace(rExp,'');
	strPat = '/^[-+]?\\d{'+inMin1+','+inMax1+'}(\\.\\d{'+inMin2+','+inMax2+'})?$/.test(strCheck1)';
	if (eval(strPat)){
		if(parseFloat(strCheck1) > to || parseFloat(strCheck1) < from){
			strFocus = 'document.' + frm + '.' + obj + '.focus()';
			setErr(obj);
			return true;
		}
		eval(strCheck + ' = Number(strCheck1).toFixed(inMax2);');
		return false;
	}else{
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
}

function isPercent(obj){
	var Pat = /^[0-9]{1,}%{0,1}$/;

	strCheck = 'document.' + frm + '.' + obj + '.value';
	strCheck = eval(strCheck);

	var matchArray = strCheck.match(Pat); // is the format ok?

	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function isChar(obj){

	var valid = "0123456789";
	var isNum = 0;

	strCheck = 'document.' + frm + '.' + obj + '.value';

	strLength = eval(strCheck);

	for (var i=0; i<strLength.length; i++) {
		temp = "" + strLength.substring(i, i+1);
		if (valid.indexOf(temp) != "-1"){
			isNum = 1;
		}
	}
	if (isNum){
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function isPasswordMatch(obj1,obj2){
	strCheck = "document." + frm + "." + obj1 + ".value";
	strCheck2 = "document." + frm + "." + obj2 + ".value";

	if ( eval(strCheck) != eval(strCheck2)){
		strFocus = 'document.' + frm + '.' + obj1 + '.focus()';
		setErr(obj1);
		return true;
	}
	return false;

}

function isDateValid(obj,dateFormat){

	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	if (dateFormat == 'YYYY-MM-DD' || dateFormat == 'YYYY-DD-MM'){
		var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/;
	}else{
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	}

	strCheck = 'document.' + frm + '.' + obj + '.value';
	strDate = eval(strCheck);

	if (strDate == ''){
		return false;
	}

	var matchArray = strDate.match(datePat); // is the format ok?
	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}

	if (dateFormat == 'YYYY-MM-DD'){
		month = 3;
		day = 4;
		year = 1;
	}

	if (dateFormat == 'YYYY-DD-MM'){
		month = 4;
		day = 3;
		year = 1;
	}

	if (dateFormat == 'DD-MM-YYYY'){
		month = 3;
		day = 1;
		year = 4;
	}

	if (dateFormat == 'MM-DD-YYYY'){
		month = 1;
		day = 3;
		year = 4;
	}

	month = matchArray[month]; // parse date into variables
	day = matchArray[day];
	year = matchArray[year];

	if (month < 1 || month > 12) { // check month range
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}

	if (day < 1 || day > 31) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			strFocus = 'document.' + frm + '.' + obj + '.focus()';
			setErr(obj);
			return true;
		}
	}
	return false;
}

function isEmail(obj) {

	// checks if the e-mail address is valid
	//var emailPat = /^(\".*\"|[A-Za-z\._-]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	var emailPat = /^([\w-]+(?:\.[\w-]+)*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	strCheck = 'document.' + frm + '.' + obj + '.value';
	strEmail = eval(strCheck);

	if (strEmail == ''){
		return false;
	}

	var matchArray = strEmail.match(emailPat);
	if (matchArray == null) {
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	// make sure the IP address domain is valid
	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				strFocus = 'document.' + frm + '.' + obj + '.focus()';
				setErr(obj);
				return true;
			}
		}
	}
	return false;
}

function isDateBeforeToday(obj) {
	strCheck = 'document.' + frm + '.' + obj + '.value';
	inDate1 = eval(strCheck);

	today = new Date();
	cDate = today.getDate().toString();
	if (cDate.length == 1){
		cDate = '0' + cDate;
	}
	cMonth = today.getMonth()+1;
	cMonth = cMonth.toString();
	if (cMonth.length == 1){
		cMonth = '0' + cMonth;
	}
	cYear = today.getFullYear();

	inDate2 = cDate + '-' + cMonth + '-' + cYear;

	mDateArr1 = inDate1.split('-');
	mDateArr2 = inDate2.split('-');

	var strDay1 = mDateArr1[0] ;
	var strDay2 = mDateArr2[0] ;

	var strMth1 = mDateArr1[1] ;
	var strMth2 = mDateArr2[1] ;

	var strYear1 = mDateArr1[2] ;
	var strYear2 = mDateArr2[2] ;

	if ((strDay1.toString()).length < 2) {
		strDay1 = "0" + strDay1.toString() ;
	}
	if ((strDay2.toString()).length < 2) {
		strDay2 = "0" + strDay2.toString() ;
	}

	if ((strMth1.toString()).length < 2) {
		strMth1 = "0" + strMth1.toString() ;
	}
	if ((strMth2.toString()).length < 2) {
		strMth2 = "0" + strMth2.toString() ;
	}

	var date1 = strYear1+strMth1+strDay1 ;
	var date2 = strYear2+strMth2+strDay2 ;

	if (parseFloat(date1) < parseFloat(date2)){
		strFocus = 'document.' + frm + '.' + obj + '.focus()';
		setErr(obj);
		return true;
	}
	return false;
}

function showErr(msg){
	if (isShowErr == 0){
		if (errType == 'box')
		alert(msg);

		eval(strFocus);
	}

	if (errType == 'color'){
		for(i=1;i<ObjArray.length;i++){
			strErrColor = 'document.all.' + frm + '.' + ObjArray[i] + '.style.background = errColor';
			eval(strErrColor);
		}
	}
	isShowErr = 1;
	return true;
}

//Jimmy added 9/8/2007
//name:		limitTextarea(e1,maxLines,maxChar)
//description	e1		: textarea object
//				maxLines: maximum lines allow in the textarea. (usually represent number of rows in textarea)
//				maxChar	: maximum Char allow in any line of the textarea.
//						  (usually represent number of columns in textarea)
//						  (might be number of columns minus one in textarea)
//				example	:<textarea name="abc" cols="51" rows="5" onfocus="limitTextarea(this,5,50)" wrap="off">some text</textarea>

function limitTextarea(el,maxLines,maxChar){

	if(!el.x){
		el.x=uniqueInt();
		el.onblur=function(){clearInterval(window['int'+el.x])}
	}

	window['int'+el.x]=setInterval(function(){
		var lines=el.value.replace(/\r/g,'').split('\n'),
		i=lines.length,
		lines_removed,
		char_removed;

		if(maxLines&&i>maxLines){
			alert('You can not enter\nmore than '+maxLines+' lines');
			lines=lines.slice(0,maxLines);
			lines_removed=1
		}

		if(maxChar){
			i=lines.length;
			while(i-->0)if(lines[i].length>maxChar){
				lines[i]=lines[i].slice(0,maxChar);
				char_removed=1
			}

			if(char_removed)
			alert('You can not enter more\nthan '+maxChar+' characters per line')
		}

		if(char_removed||lines_removed)
		el.value=lines.join('\n')

	},50);

}

function uniqueInt(){
	var num,maxNum=100000;
	if(!uniqueInt.a||maxNum<=uniqueInt.a.length)uniqueInt.a=[];
	do num=Math.ceil(Math.random()*maxNum);

	while(uniqueInt.a.hasMember(num))
	uniqueInt.a[uniqueInt.a.length]=num;

	return num
}

Array.prototype.hasMember=function(testItem){
	var i=this.length;
	while(i-->0)if(testItem==this[i])return 1;
	return 0
};

