/***************************************************************
* ValidationScript
* Version 1.76a
* 9/29/2004
* mrb1778.com © 2004
* 
* Updated by Joe Kwon 09/29/2004 with Mike's Help
*
* Usage:
*  In header: addRequiredField("fieldName", "displayName");
*  ...
*  In form tag: return validateForm(this);
* 
***************************************************************/

var formFields = new Array();
var currentForm = null;

function validateForm(submitForm) {
	
	var alertMessage = "";
	
	currentForm = submitForm; 

	for(var i=0; i<formFields.length; i++) {
		var item = formFields[i];
		if(item != null && item.isRequired && item.getValue() == item.defaultValue) {
			alertMessage += item.displayName + "\n" ;
		}
	}
	
	if (alertMessage !="") {
		message = "The following required fields were not filled out:\n\n";
		message += alertMessage + "\n";
		message += "Please fill them out before proceeding.";
		alert(message);
		return false;
	}
	 for(var i=0; i<formFields.length; i++) {
	 	var item = formFields[i];
	 	if(item != null && !item.isValid()) {
			alert(item.preValidateText + " " + item.displayName + " " + item.validateText);
			return false;
		}
	}
	return true;
}


//CLASS FUNCTIONS
function getValue() {
	return getValueFromForm(this.name);
}

function getValueFromForm(name) {
	if(currentForm[name].length > 0 && (currentForm[name].type == "select" || currentForm[name].type == "select-multiple")) {
		var formValue = currentForm[name].options[currentForm[name].selectedIndex].value;
		if(formValue == null || formValue.length == 0) {
			formValue = currentForm[name].options[currentForm[name].selectedIndex].text
		}
		
		return formValue;
	} else if(currentForm[name].length > 1 && currentForm[name][0].type == "radio") {
		return(getSelectedText(currentForm[name]));
	} else if(currentForm[name].type == "checkbox") {
		return currentForm[name].checked?"checked":"";
	} else {
		return currentForm[name].value;
	}
}
//END CLASS FUNCTIONS


//HELPER FUNCTIONS
function isAllNumeric(text) {
	for(var i=0; i<text.length; i++) {
	  if(isNaN(text.charAt(i))) {
			 return false;
	  }
	}
	return true;
}


function getSelectedText(radio) {
    for(var i=0; i<radio.length; i++) {
        if(radio[i].checked) return radio[i].value;
    }
    return "";
}

function getCircularNumber(num, maxNumber) {
    var circleNum = (maxNumber + num) % maxNumber;
    return circleNum;
}
//END HELPER FUNCTIONS


//ITEM FUNCTIONS
function addField(name, displayName, defaultValue) {
	if(displayName == null) displayName = name;
	if(defaultValue == null) defaultValue = "";
	
	var item = new Object();
	item.name = name;
	item.displayName = displayName;
	item.defaultValue = defaultValue;
	item.getValue = getValue;
	item.isRequired = false;
	item.preValidateText = "";
	
	item.validationLocation = formFields.length;
	formFields[formFields.length] = item;
	
	item.isValid = function() {
		return true; 
	};
	
	return item;
}

function removeRequiredField(item) {
	formFields[item.validationLocation] = null;
	return item;
}


function addRequiredField(name, displayName, defaultValue) {
	var item = addField(name, displayName, defaultValue);
	item.isRequired = true;
	return item;
}

function addRequiredEmail(name, displayName, defaultValue) {
	if(displayName == null) {
		displayName = name;
		name = "email";
	}
	
	if(name == null) {
		name = displayName = "email";
	}
	
	
	var item = addRequiredField(name, displayName, defaultValue);
	
	item.validateText = "is not valid.";
	
	item.isValid = function() {
		var email = this.getValue();
		return !(email.indexOf("@") < 1  || email.lastIndexOf(".") < email.indexOf("@"));
	};
	
	return item;
}

function addRequiredNumber(name, displayName, defaultValue, minValue, maxValue) {
	var item = addRequiredField(name, displayName, defaultValue);
	
	item.validateText = "must be a number.";
	item.minValue = minValue;
	item.maxValue = maxValue;
	
	item.isValid = function() {
		return isAllNumeric(this.getValue());
	};
	
	return item;
}

//strictly validators
function addMinRangeValidation(name, displayName, minValue) {
	var item = addField(name, displayName);
	
	item.validateText = "must be greater than or equal to " + minValue + ".";
	item.minValue = minValue;
	
	item.isValid = function() {
		return this.getValue() >= this.minValue;
	};
	
	return item;
}

function addMaxRangeValidation(name, displayName, maxValue) {
	var item = addField(name, "", defaultValue);
	
	item.validateText = "must be less than or equal to " + maxValue + ".";
	item.maxValue = maxValue;
	
	item.isValid = function() {
		return this.getValue() <= this.maxValue;
	};
	
	return item;
}


function addRangeValidation(name, displayName, minValue, maxValue) {
	var item = addField(name, displayName, null);
	
	item.validateText = "must be between " + minValue + " and " + maxValue + ".";
	item.minValue = minValue;
	item.maxValue = maxValue;
	
	item.isValid = function() {
		return this.getValue() >= this.minValue && this.getValue() <= this.maxValue; 
	};
	
	return item;
}



function addLengthValidation(name, displayName, maxLength) {
	var item = addField(name, displayName, null);
	
	item.validateText = "must be less than or equal to " + maxLength + " characters.";
	item.maxLength = maxLength;
	
	item.isValid = function() {
		return this.getValue().length <= this.maxLength; 
	};
	
	return item;
}


function addMinLengthValidation(name, displayName, minLength) {
	var item = addField(name, displayName, null);
	
	item.validateText = "must be greater than or equal to " + minLength + " characters.";
	item.minLength = minLength;
	
	item.isValid = function() {
		return this.getValue().length >= this.minLength; 
	};
	
	return item;
}

function addfixLengthValidation(name, displayName, fixLength) {
	var item = addField(name, displayName, null);
	
	item.validateText = "must be " + fixLength + " characters.";
	item.fixLength = fixLength;
	
	item.isValid = function() {
		return this.getValue().length == this.fixLength; 
	};
	
	return item;
}

function addSpecialCharacterValidation(name, displayName) {
	var item = addField(name, displayName, null);
	
	item.validateText = "can not contain special characters.";
	item.isValid = function () {
		return this.getValue().indexOf(" ") == -1 && 
		       this.getValue().indexOf("`") == -1 &&
		       this.getValue().indexOf("!") == -1 &&
		       //this.getValue().indexOf("@") == -1 &&
		       this.getValue().indexOf("#") == -1 &&
		       this.getValue().indexOf("$") == -1 &&
		       this.getValue().indexOf("%") == -1 &&
		       this.getValue().indexOf("^") == -1 &&
		       this.getValue().indexOf("&") == -1 &&
		       this.getValue().indexOf("*") == -1 &&
		       this.getValue().indexOf("(") == -1 &&
		       this.getValue().indexOf(")") == -1 &&
		       this.getValue().indexOf("-") == -1 &&
		       this.getValue().indexOf("<") == -1 &&
		       this.getValue().indexOf(">") == -1 &&
		       this.getValue().indexOf("/") == -1 &&
		       this.getValue().indexOf("+") == -1;
	};
	
	return item;
}

function addCurrencyValidation(name, displayName) {
	var item = addField(name, displayName, null);
	
	item.validateText = "can only contain dollar amounts."
	
	item.isValid = function() {
			var valid = "0123456789$.,";
			var temp;
			for(var i=0; i< this.getValue().length; i++) {
				temp = "" +  this.getValue().substring(i, i+1);
				if(valid.indexOf(temp) == -1) return false;
			}
			return true;
	};
	
}

//END ITEM FUNCTIONS

//COMPOUND ITEMS
function addDuplicatePassword(name, passwordField) {
	var item = addField(name, "Password confirmation", null);
	
	item.validateText = "must match your password.";
	item.duplicate = passwordField;
	
	item.isValid = function() {
		return this.getValue() == currentForm[this.duplicate].value;
	};
	
	return item;
}

function addRequiredDateOfBirth(month, day, year, monthText, dayText, yearText) {
	if(monthText == null) { 
		monthText = "month of birth"; 
	}
	
	if(dayText == null) { 
		dayText = "day of birth"; 
	}
	
	if(yearText == null) { 
		yearText = "year of birth"; 
	}
	
	addRequiredNumber(month, monthText);
	addRequiredNumber(day, dayText);
	addRequiredNumber(year, yearText);

	addRangeValidation(month, monthText, 1, 12);
	addRangeValidation(day, dayText, 1, 31);
	addRangeValidation(year, yearText, 1890, 2004);
}

function addRequiredSendToAFriend(fromName, fromEmail, toName, toEmail, message, defaultFromName, defaultFromEmail, defaultToName, defaultToEmail, defaultMessage) {
	
	addRequiredField(fromName, "Your name", defaultFromName);
	addRequiredField(toName, "Your friend's name", defaultToName);
	
	addRequiredEmail(fromEmail, "Your email", defaultFromEmail);
	addRequiredEmail(fromEmail, "Your friend's email", defaultToEmail);
	
	if(message != null) {
		addRequiredField(message, "Your message", defaultMessage);
	}
}


function addConditionalRequiredField(conditionalField, conditionalValue, requiredField, displayName) {
	var item = addField(conditionalField, displayName, null);
	
	item.preValidateText = "You must fill out"
	item.validateText = "before proceding.";
	item.conditionalValue = conditionalValue;
	item.requiredField = requiredField;
	
	item.isValid = function() {
		if(this.getValue() == this.conditionalValue) {
			if(getValueFromForm(this.requiredField) == null  || getValueFromForm(this.requiredField) == "") {
				return false;
			}
		}
		
		return true;
	};
	
	return item;
}

function addCurrencyGreaterThanValidation(name, displayName, minValue) {
	var item = addField(name, displayName);
	
	item.validateText = "must be greater than or equal to $" + minValue + " USD.";
	item.minValue = minValue;
	
	item.isValid = function() {
		if(this.getValue().substring(0, 1) == "$") {
			return parseFloat(this.getValue().substring(1)) >= this.minValue;
		} else {
			return parseFloat(this.getValue()) >= this.minValue;
		}
	};
	
	return item;
}
//END COMPOUND ITEMS
