/********************
FormValidator performs client-side form field validation.
Copyright (C) 2003, Todd Cotton (todd.cotton@gmail.com)

FormValidator is free and may be redistributed and/or modified under the terms of the GNU GPL (http://www.gnu.org/copyleft/gpl.html).

FormValidator is provided WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*********************/

//-- GLOBALS --

var errors = 0;

//-- VALIDATION FUNCTIONS --

function IsNull(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetValue(fieldID) != "")){RaiseError(fieldID, fieldName +" must be null.");}
}

function IsNotNull(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetValue(fieldID) == "")){RaiseError(fieldID, fieldName +" cannot be null.");}
}

function IsSelected(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetSelectedIndex(fieldID) == 0)){RaiseError(fieldID, "A(n) "+ fieldName +" option must be selected.");}
}

function IsNotSelected(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetSelectedIndex(fieldID) != 0)){RaiseError(fieldID, "A(n) "+ fieldName +" option must not be selected.");}
}

function IsChecked(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(! GetObj(fieldID).checked)){RaiseError(fieldID, "The "+ fieldName +" checkbox must be checked.");}
}

function IsNotChecked(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetObj(fieldID).checked)){RaiseError(fieldID, "The "+ fieldName +" checkbox must not be checked.");}
}

function IsLength(fieldID, operator, length, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	var curFieldLength = GetValue(fieldID).length;
	if((errors == 0)&&(eval(curFieldLength +" "+ operator +" "+ length))){RaiseError(fieldID, fieldName +" cannot be "+ operator +" "+ length +" characters; the current field length is "+ curFieldLength +".");}
}

function IsInString(fieldID, target, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetValue(fieldID).indexOf(target) == -1)){RaiseError(fieldID, fieldName +" must contain \""+ target +"\".");}
}

function IsNotInString(fieldID, target, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	if((errors == 0)&&(GetValue(fieldID).indexOf(target) != -1)){RaiseError(fieldID, fieldName +" cannot contain \""+ target +"\".");}
}

function CompareValues(fieldID, operator, target, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	var testValue = GetValue(fieldID);
	if(testValue - testValue == 0){ testValue *= 1; }
	if((errors == 0)&&(eval(testValue +" "+ operator +" "+ target))){RaiseError(fieldID, fieldName +" must be "+ operator +" "+ target +".");}
}

function CompareFields(fieldOne, fieldTwo, fieldNameOne, fieldNameTwo) {
	if(fieldNameOne == ""){ fieldNameOne = fieldOne; }
	if(fieldNameTwo == ""){ fieldNameTwo = fieldTwo; }
	if((errors == 0)&&(GetValue(fieldOne) != GetValue(fieldTwo))) {RaiseError(fieldOne, fieldNameOne +" must match "+ fieldNameTwo +".");}
}

function IsEqual(fieldID, target, fieldName){
	CompareValues(fieldID, "==", target, fieldName);
}

function IsNotEqual(fieldID, target, fieldName){
	CompareValues(fieldID, "!=", target, fieldName);
}

function IsLess(fieldID, target, fieldName){
	CompareValues(fieldID, "<", target, fieldName);
}

function IsLessOrEqual(fieldID, target, fieldName){
	CompareValues(fieldID, "<=", target, fieldName);
}

function IsGreater(fieldID, target, fieldName){
	CompareValues(fieldID, ">", target, fieldName);
}

function IsGreaterOrEqual(fieldID, target, fieldName){
	CompareValues(fieldID, ">=", target, fieldName);
}

function IsEmail(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	IsNotNull(fieldID, fieldName);
	IsInString(fieldID, "@", fieldName);
	IsInString(fieldID, ".", fieldName);
}

function IsNumeric(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	var testValue = GetValue(fieldID);
	if((errors == 0)&&((testValue == "")||(testValue - testValue != 0))){RaiseError(fieldID, fieldName +" must be a number.");}
}

function IsNotNumeric(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	var testValue = GetValue(fieldID);
	if((errors == 0)&&((testValue == "")||(testValue - testValue == 0))){RaiseError(fieldID, fieldName +" must not be a number.");}
}

function IsDate(fieldID, fieldName){
	if(fieldName == ""){ fieldName = fieldID; }
	var strDate = GetValue(fieldID);
	var datDate = new Date(strDate);
	if((errors == 0)&&((datDate.getMonth() + 1) != strDate.substring(0, strDate.indexOf("/")))){RaiseError(fieldID, fieldName +" must be a valid date.");}
}

//-- HELPER FUNCTIONS --

function RaiseError(fieldID, errorTxt){
	errors++;
	alert("ERROR: "+ errorTxt);
	GetObj(fieldID).focus();
}

function SubmitForm(formID){
	if (errors == 0) {
		if(formID != ""){
			GetObj(formID).submit();
		}else{
			document.forms[0].submit();
		}
	}
}

function ErrorCount(){return errors;}

function ClearErrors(){errors = 0;}

//-- OBJECT FUNCTIONS --

function GetObj(objID){
	var thisObj;
	if(document.getElementById){
		thisObj = document.getElementById(objID);
	}else{
		if(document.all){ thisObj = eval("document.all[\"" + objID + "\"]"); }
	}
	return thisObj;
}

//-- GET/SET FUNCTIONS --

function GetValue(fieldID){ return GetObj(fieldID).value; }
function SetValue(fieldID, newValue){ GetObj(fieldID).value = newValue; }
function GetSelectedIndex(fieldID){ return GetObj(fieldID).selectedIndex; }
function SetSelectedIndex(fieldID, newValue){
	var myObj = GetObj(fieldID);
	for(i = 0; i < myObj.length; i++){
		if(myObj[i].value == newValue){
			myObj.selectedIndex = i;
			break;
		}
	}
}

function ReplaceValue(fieldID, target, result){
	var cStr = GetValue(fieldID);
	SetValue(fieldID, cStr.replace(/target/g, result));
}
