//	stripSpaces(inputStr)
function stripSpaces(inputStr) {
	//Strip leading and trailing spaces
	x = inputStr;
	while (x.substring(0,1) == ' ') {
		x	= x.substring(1);
	}
	while (x.substring(x.length-1,x.length) == ' ') {
		x = x.substring(0,x.length-1);
	}
	return x;
}
// cleanCurrency(str)
// - Removes unneccessary currency characters $ and ,
function cleanCurrency(str) {
	str = stripSpaces(str);
	while (str.indexOf(',') > -1) {
		var idx = str.indexOf(',');
		var strA = str.substring(0, idx);
		var strB = str.substring( (idx + 1), str.length);
		str = strA + '' + strB;
	}
	// Remove Dollar Sign
	while (str.indexOf('$') > -1) {
		var idx = str.indexOf('$');
		var strA = str.substring(0, idx);
		var strB = str.substring( (idx + 1), str.length);
		str = strA + '' + strB;
	}
	return str;
}
function checkEmail(strValue) {
	var invalidChars = " /:,;";
	for (var i=0;i<invalidChars.length;i++) {
		var InvalidChar = invalidChars.charAt(i);
		if(strValue.indexOf(InvalidChar,0) > -1) {
			//alert("The email address contains an invalid character");
			return false;
		}
	}
	var atPosition = strValue.indexOf("@",1);
	if (atPosition == -1) {
		return false;
	}
	if (strValue.indexOf("@", atPosition+1) > -1) {
		return false;
	}
	dotPosition = strValue.indexOf(".", atPosition);
	if ((dotPosition == -1)||(dotPosition <= atPosition + 1)) {
		return false;
	}
	if (dotPosition + 3 > strValue.length) {
		return false;
	}
	return true;
}



//	FormManager
function FormManager(frm) {
	this.subject = frm;
	this.elements = new Array();
	this.allowSubmit = false;
	this.validatedOkay = false;
	this.submitCount = 0;
	//
	// Adds a standard validated element.
	function addElement(fieldName, fieldTitle) {
		//alert("Subject: " + this.subject);
		elem = new FieldElement(this.subject, fieldName, fieldTitle, fieldName, false, "", 0, 0, false);
		this.elements[this.elements.length] = elem;
	}
	this.addElement = addElement;
	// Adds a compound validated element
	function addCompoundElement(fieldName, fieldTitle, selectName) {
		elem = new FieldElement(this.subject, fieldName, fieldTitle, selectName, false, "", 0, 0, false);
		this.elements[this.elements.length] = elem;
	}
	this.addCompoundElement = addCompoundElement;
	// Adds a field that requires a value if another field has a certain value.
	function addLinkedElement(fieldName, fieldTitle, dependsOn, withValue) {
		elem = new FieldElement(this.subject, fieldName, fieldTitle, fieldName, true, dependsOn, withValue, 0, false);
		this.elements[this.elements.length] = elem;
	}
	this.addLinkedElement = addLinkedElement;
	// Adds a field that requires a value if another field doesnt have a certain value.
	function addAntiLinkedElement(fieldName, fieldTitle, dependsOn, withoutValue) {
		elem = new FieldElement(this.subject, fieldName, fieldTitle, fieldName, true, dependsOn, withoutValue, 0, true);
		this.elements[this.elements.length] = elem;
	}
	this.addAntiLinkedElement = addAntiLinkedElement;
	
	// Adds a standard validated element.
	function addTypedElement(fieldName, fieldTitle, fieldType) {
		//alert("Subject: " + this.subject);
		elem = new FieldElement(this.subject, fieldName, fieldTitle, fieldName, false, "", 0, fieldType, false);
		this.elements[this.elements.length] = elem;
	}
	this.addTypedElement = addTypedElement;
	// Adds a typed linked element.
	function addTypedLinkedElement(fieldName, fieldTitle, dependsOn, withValue, fieldType) {
		if (addTypedLinkedElement.arguments.length > 5) {
			elem = new FieldElement(this.subject, fieldName, fieldTitle, addTypedLinkedElement.arguments[5], true, dependsOn, withValue, fieldType, false);
		} else {
			elem = new FieldElement(this.subject, fieldName, fieldTitle, fieldName, true, dependsOn, withValue, fieldType);
		}
		this.elements[this.elements.length] = elem;
	}
	this.addTypedLinkedElement = addTypedLinkedElement;
	
	
	
	// Validates the form.
	function validate() {
		this.validatedOkay = true;
		
		for (var i=0; i < this.elements.length; i++) {
			if (this.elements[i].checkSelf() == false) {
				this.validatedOkay = false;
				break;
			}
		}
	}
	this.validate = validate;
	
	// Check to see whether this form can be submitted
	function checkAllowSubmit() {
		//alert("checkAllowSubmit is " + this.allowSubmit);
		return this.allowSubmit;
	}
	this.checkAllowSubmit = checkAllowSubmit;
	
	// Sets that the form can be submitted.
	function enableSubmit() {
		this.allowSubmit = true;
		//alert('AllowSubmit:(enable) ' + this.allowSubmit);
	}
	this.enableSubmit = enableSubmit;
	
	// Sets that the form cannot be submitted.
	function disableSubmit() {
		this.allowSubmit = false;
		//alert('AllowSubmit:(disable) ' + this.allowSubmit);
	}
	this.disableSubmit = disableSubmit;
	
	//
	function getSubmitCount() {
		return this.submitCount;
	}
	this.getSubmitCount = getSubmitCount;
	
	// Does all of the checking to see if the form is to be submitted.
	function submitForm() {
		//alert('AllowSubmit: ' + this.allowSubmit);
		if (this.allowSubmit == true) {
			if (this.submitCount == 0) {
				this.validate();
				if (this.validatedOkay == true) {
					this.submitCount++;
					this.disableSubmit();
					return true;
				} else {
					return false;
				}
			} else {
				//alert('sc: ' + this.submitCount);
				var c = confirm("This form has already been submitted, are you sure you want to submit it again?");
				if (c) {
					this.enableSubmit();
					return true;
				} else {
					this.disableSubmit();
					//this.subject.document.location.reload();
					return false;
				}
			}
		} else {
			return false;
		}
	}
	this.submitForm = submitForm;
	
	//
	function reduceSubmitCount() {
		this.submitCount--;
	}
	this.reduceSubmitCount = reduceSubmitCount;
	
	function resetForm() {
		this.submitCount = 0;
	}
	this.resetForm = resetForm;
	
	function displayIndicators() {
		for (var i=0; i < this.elements.length; i++) {
			if (this.elements[i]) {
				this.setReqInd(this.elements[i], this.elements[i].isLinked);
			} else {
				alert('Field Checking Fault On: ' + this.elements[i]);
			}
		}
	}
	this.displayIndicators = displayIndicators;
	
	function setReqInd(fldelem, linked) {
		if (linked) {
			fldelem.validateField.style.backgroundColor = '#EEEEEE';
		} else {
			fldelem.validateField.style.backgroundColor = '#CCCCCC';
		}
	}
	this.setReqInd = setReqInd;
	
	function update(fld) {
		for (var i=0; i < this.elements.length; i++) {
			if (this.elements[i].isLinked && this.elements[i].dependsOn == fld.name) {
				if (fld.value == this.elements[i].withValue && (! this.elements[i].negateCondition)) {
					this.elements[i].validateField.style.backgroundColor = '#CCCCCC';
				} else if (fld.value != this.elements[i].withValue && this.elements[i].negateCondition) {
					this.elements[i].validateField.style.backgroundColor = '#CCCCCC';
				} else {
					this.elements[i].validateField.style.backgroundColor = '#FFFFFF';
				}
			}
		}
	}
	this.update = update;
}

//	FieldElement
function FieldElement(subjectForm, fieldName, fieldTitle, selectName, isLinked, dependsOn, withValue, fieldType, negateCondition) {
	// Standard Field Types
	//this.NOT_NULL = 0; // standard type.
	//this.CURRENCY = 1;
	//this.EMAIL = 2;
	this.fieldName = fieldName;
	this.fieldTitle = fieldTitle;
	this.selectName = selectName;
	this.subjectForm = subjectForm;
	this.dependsOn = dependsOn;
	this.withValue = withValue;
	this.isLinked = isLinked;
	this.fieldType = fieldType;
	this.validateFieldString = "this.subjectForm." + this.fieldName;
	this.selectFieldString = "this.subjectForm." + this.selectName;
	this.validateField = eval(this.validateFieldString);
	this.selectField = eval(this.selectFieldString);
	this.negateCondition = negateCondition;
	// Checks the field to see if it is populated.
	function checkSelf() {
		var doCheck = false;
		if (this.validateField.disabled) {
			return true;
		}
		// Check to see if this field depends on another field.
		//alert("Checking: " + this.fieldName);
		if (this.isLinked == true) {
			var dependsFieldString = "this.subjectForm." + this.dependsOn;
			var dependsField = eval(dependsFieldString);
			var depType = dependsField.type;
			var val = "";
			if ((depType == 'text')||(depType == 'textarea')||(depType == 'password')||(depType == 'hidden')) {
				val = dependsField.value;
			} else if ((depType == 'select-one')||(depType == 'select-multiple')) {
				val = dependsField.options[dependsField.selectedIndex].value;
				if (val == "") {
					val = -1;
				}
			} else if (depType == 'checkbox') {
				val = 0;
				if (dependsField.checked == true) {
					val = 1;
				}
			}
			if (val == this.withValue && ! this.negateCondition) {
				doCheck = true;
			} else if (val != this.withValue && this.negateCondition) {
				doCheck = true;
			}
		} else {
			doCheck = true;
		}
		if (doCheck == true) {
			var strFieldType = "";
			if (this.validateField) {
				strFieldType = this.validateField.type;
			} else {
				alert('Invalid field specified for validation: ' + this.fieldName);
			}
			if ((strFieldType == 'text')||(strFieldType == 'textarea')||(strFieldType == 'password')||(strFieldType == 'hidden')) {
				var strEnteredValue = escape(stripSpaces(this.validateField.value));
				if (strEnteredValue.length < 1) {
					alert(this.fieldTitle + " must be entered");
					var strSelectFieldType = this.selectField.type;
					this.selectField.focus();
					if (!((strSelectFieldType == 'select-one')||(strSelectFieldType == 'select-multiple'))) {
						this.selectField.select();
					}
					return false;
				} else if (this.fieldType == 1) {
					if (this.checkCurrency(strEnteredValue) == false) {
						alert(this.fieldTitle + " must contain a value greater than zero.");
						this.selectField.focus();
						return false;
					} else {
						return true;
					}
				} else if (this.fieldType == 2) {
					if (this.checkEmail(strEnteredValue) == false) {
						alert(this.fieldTitle + " appears to be invalid.");
						this.selectField.focus();
						return false;
					} else {
						return true;
					}
				} else if (this.fieldType == 3) {
					if (this.checkNumeric(strEnteredValue) == false) {
						alert(this.fieldTitle + " must be a number.");
						this.selectField.focus();
						return false;
					} else {
						return true;
					}
				} else if (this.fieldType == 4) {
					if (strEnteredValue != 1) {
						alert(this.fieldTitle + " must be filled in.");
						this.selectField.focus();
						return false;
					} else {
						return true;
					}
				}
			} else if ((strFieldType == 'select-one')||(strFieldType == 'select-multiple')) {
				var intSelIndex = this.validateField.selectedIndex;
				var strSelectedValue = this.validateField.options[intSelIndex].value;
				if (isNaN(strSelectedValue)) {
					if (stripSpaces(strSelectedValue) == "") {
						alert("Please select a value in " + this.fieldTitle);
						this.selectField.focus();
						return false;
					} else {
						return true;
					}
				} else {
					if (parseInt(strSelectedValue) > -2) {
						return true;
					} else {
						alert("Please select a value in " + this.fieldTitle);
						this.selectField.focus();
						return false;
					}
				}
			} else if (strFieldType == 'checkbox') {
				if (this.validateField.checked) {
					return true;
				} else {
					alert("Please place a check in " + this.fieldTitle);
					this.selectField.focus();
					return false;
				}
			}
		} else {
			return true;
		}
	}
	this.checkSelf = checkSelf;
	// Checks a currency for a valid value.
	function checkCurrency(strValue) {
		strAmount = cleanCurrency(strValue);
		var flAmount = parseFloat(strAmount);
		if (isNaN(flAmount)) {
			//alert("The value is not a valid currency amount.");
			return false;
		}
		if (flAmount == 0) {
			//alert("The field must contain a value greater than zero.");
			return false;
		} else {
			return true;
		}
	}
	this.checkCurrency = checkCurrency;
	// check an email address
	function checkEmail(strValue) {
		var invalidChars = " /:,;";
		for (var i=0;i<invalidChars.length;i++) {
			var InvalidChar = invalidChars.charAt(i);
			if(strValue.indexOf(InvalidChar,0) > -1) {
				//alert("The email address contains an invalid character");
				return false;
			}
		}
		var atPosition = strValue.indexOf("@",1);
		if (atPosition == -1) {
			return false;
		}
		if (strValue.indexOf("@", atPosition+1) > -1) {
			return false;
		}
		dotPosition = strValue.indexOf(".", atPosition);
		if ((dotPosition == -1)||(dotPosition <= atPosition + 1)) {
			return false;
		}
		if (dotPosition + 3 > strValue.length) {
			return false;
		}
		return true;
	}
	this.checkEmail = checkEmail;
	// check number
	function checkNumeric(strValue) {
		var nval = parseInt(strValue);
		if (isNaN(nval)) {
		   return false;
		}
		return true;
	}
	this.checkNumeric = checkNumeric;
}

