// project: client form validator
// author: Adrian Zaharia (Sylvester), adyzah@yahoo.com
// version: 0.1

function Rule(poTarget, psRule, psConditions, psMessage) {
	this.oTarget = poTarget;
	this.sRule = psRule;
	this.sConditions = psConditions;
	this.sMessage = psMessage;
	this.checkRule = Rule_checkRule;
	this.checkRuleNOTNULL = Rule_checkRuleNOTNULL;
	this.checkRuleDIGITS = Rule_checkRuleDIGITS;
	this.checkRuleNUMERIC = Rule_checkRuleNUMERIC;
	this.checkRuleALPHA = Rule_checkRuleALPHA;
	this.checkRuleEMAIL = Rule_checkRuleEMAIL;
	this.checkRuleEQ = Rule_checkRuleEQ;
	this.checkRuleGT = Rule_checkRuleGT;
	this.checkRuleGTE = Rule_checkRuleGTE;
	this.checkRuleLT = Rule_checkRuleLT;
	this.checkRuleLTE = Rule_checkRuleLTE;
	this.checkRuleBETWEEN = Rule_checkRuleBETWEEN;
	this.checkRuleMINCHAR = Rule_checkRuleMINCHAR;
	this.checkRuleMAXCHAR = Rule_checkRuleMAXCHAR;
	this.checkRuleLIST = Rule_checkRuleLIST;
	this.checkRuleREGEXP = Rule_checkRuleREGEXP;
	this.checkRuleSELECTED = Rule_checkRuleSELECTED;
	this.checkRuleJS = Rule_checkRuleJS;
	this.getValue = Rule_getValue;
}

function Rule_getValue() {
	if( this.oTarget.type == "text" || this.oTarget.type == "textarea" || this.oTarget.type == "password") {
		return this.oTarget.value;
	}
	if( this.oTarget.type == "select-one") {
		if(this.oTarget.options[this.oTarget.selectedIndex].value == "")
			return this.oTarget.options[this.oTarget.selectedIndex].text;
		else
			return this.oTarget.options[this.oTarget.selectedIndex].value;
	}
	if( this.oTarget.type == "select-multiple") {
		if(this.oTarget.length == 0)
			return "";
		else {
			if(this.oTarget.options[0].value == "")
				return this.oTarget.options[0].text;
			else
				return this.oTarget.options[0].value;
		}
	}
}

function Rule_checkRule(psColor) {
	if( !eval("this.checkRule" + this.sRule + "()") ) {
		if(this.oTarget != null) { 
			this.sOldBgColor = this.oTarget.style.backgroundColor;
			this.oTarget.style.backgroundColor = psColor;
		}
		return this.sMessage + "\n";
	} else {
		if(this.oTarget != null) { 
			this.oTarget.style.backgroundColor = "";
		}
		return "";
	}
}

function Rule_checkRuleNOTNULL() {
	var sValue = this.getValue();
	return (sValue.replace(/(\s)*/g, "") != "");
}

function Rule_checkRuleDIGITS() {
	var sValue = this.getValue();
	return (sValue.replace(/([0-9])*/g, "") == "");
}

function Rule_checkRuleNUMERIC() {
	var sValue = this.getValue();
	return (sValue.replace(/([0-9.])*/g, "") == "");
}

function Rule_checkRuleALPHA() {
	var sValue = this.getValue();
	return (sValue.replace(/([a-zA-Z])*/g, "") == "");
}

function Rule_checkRuleEMAIL() {
	var sValue = this.getValue();
	return (sValue.search(/^[a-zA-Z0-9]([a-zA-Z0-9\+\._-])*(@){1}([a-zA-Z0-9_-])+(\.){1}([a-zA-Z0-9\._-])+$/) != -1);
}

function Rule_checkRuleEQ() {
	var sValue = this.getValue();
	if( this.sConditions.substring(0, 2) == 'O:' ) {
		return ( sValue == eval(this.sConditions.substring(2)).value );
	} else {
		return ( sValue == this.sConditions );
	}
}

function Rule_checkRuleGT() {
	var sValue = this.getValue();
	if( this.sConditions.substring(0, 2) == 'O:' ) {
		return ( parseInt(sValue) > parseInt( eval(this.sConditions.substring(2)).value ) );
	} else {
		return (parseInt(sValue) > parseInt(this.sConditions));
	}
}

function Rule_checkRuleGTE() {
	var sValue = this.getValue();
	if( this.sConditions.substring(0, 2) == 'O:' ) {
		return ( parseInt(sValue) >= parseInt( eval(this.sConditions.substring(2)).value ) );
	} else {
		return (parseInt(sValue) >= parseInt(this.sConditions));
	}
}

function Rule_checkRuleLT() {
	var sValue = this.getValue();
	if( this.sConditions.substring(0, 2) == 'O:' ) {
		return ( parseInt(sValue) < parseInt( eval(this.sConditions.substring(2)).value ) );
	} else {
		return (parseInt(sValue) < parseInt(this.sConditions));
	}
}

function Rule_checkRuleLTE() {
	var sValue = this.getValue();
	if( this.sConditions.substring(0, 2) == 'O:' ) {
		return ( parseInt(sValue) <= parseInt( eval(this.sConditions.substring(2)).value ) );
	} else {
		return (parseInt(sValue) <= parseInt(this.sConditions));
	}
}

function Rule_checkRuleBETWEEN() {
	var iMin, iMax, aTemp;
	var sValue = this.getValue();
	aTemp = this.sConditions.split(",");
	if( aTemp.length == 2) {
		if( aTemp[0].substring(0, 2) == 'O:' )
			iMin = parseInt( eval(aTemp[0].substring(2)).value );
		else
			iMin = parseInt(aTemp[0]);
		if( aTemp[1].substring(0, 2) == 'O:' )
			iMax = parseInt( eval(aTemp[1].substring(2)).value );
		else
			iMax = parseInt(aTemp[1]);
		return (parseInt(sValue) >= iMin && parseInt(sValue) <= iMax );
	} else
		return false;
}

function Rule_checkRuleMINCHAR() {
	var sValue = this.getValue();
	return ( sValue.length >= parseInt(this.sConditions) );
}

function Rule_checkRuleMAXCHAR() {
	var sValue = this.getValue();
	return ( sValue.length < parseInt(this.sConditions) );
}

function Rule_checkRuleLIST() {
	var i;
	var sValue = this.getValue();
	for(i=0;i<sValue.length;i++) {
		if( this.sConditions.indexOf(sValue.charAt(i)) == -1 )
			return false;
	}
	return true;
}

function Rule_checkRuleREGEXP() {
	var oRE = new RegExp(this.sConditions);
	return ( oRE.test(this.oTarget.value) );
}

function Rule_checkRuleSELECTED() {
	var i;
	if(this.oTarget.type == "select-multiple") {
		for(i=0;i<this.oTarget.length;i++)
			if(this.oTarget.options[i].selected)
				return true;
	}
	return false;
}

function Rule_checkRuleJS() {
	return(eval(this.sConditions));
}

/*

*/

function Validator() {
	this.aRules = new Array();
	this.sMessage = "";
	this.bGroupMessages = true;
	this.sErrorBgColor = "#FFCC66";
	this.addRule = Validator_addRule;
	this.checkForm = Validator_checkForm;
	this.groupMessages = Validator_groupMessages;
	this.setErrorBgColor = Validator_setErrorBgColor;
}

function Validator_groupMessages(pbValue) {
	this.bGroupMessages = pbValue;
}

function Validator_setErrorBgColor(psColor) {
	this.sErrorBgColor = psColor;
}

function Validator_addRule(poRule) {
	this.aRules[this.aRules.length] = poRule;
}

function Validator_checkForm() {
	var i;
	if(this.bGroupMessages) {
		for(i=0;i<this.aRules.length;i++) {
			this.sMessage += this.aRules[i].checkRule(this.sErrorBgColor);
		}
		if( this.sMessage != "") {
			alert(this.sMessage);
			this.sMessage = "";
			return false;
		}
	} else {
		for(i=0;i<this.aRules.length;i++) {
			this.sMessage = this.aRules[i].checkRule();
			if( this.sMessage != "") {
				alert(this.sMessage);
				this.sMessage = "";
				return false;
			}
		}
	}
	this.sMessage = "";
	return true;
}

