var formFields = new Array();
var currentForm = null;

//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.isValid = validateText;
	item.getValue = getValue;
	item.isRequired = false;

	formFields[formFields.length] = item;
	
	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.isValid = validateEmail;
	item.validateText = "is not valid.";
	
	return item;
}

function addRequiredNumber(name, displayName, defaultValue, minValue, maxValue) {
	var item = addRequiredField(name, displayName, defaultValue);
	
	item.isValid = validateNumeric;
	item.validateText = "must be a number.";
	item.minValue = minValue;
	item.maxValue = maxValue;
	
	return item;
}

//strictly validators
function addMinRangeValidation(name, displayName, minValue) {
	var item = addField(name, displayName);
	
	item.isValid = validateMinRange;
	item.validateText = "must be greater than or equal to" + minValue + ".";
	item.minValue = minValue;
	
	return item;
}

function addMaxRangeValidation(name, displayName, maxValue) {
	var item = addField(name, "", defaultValue);
	
	item.isValid = validateMaxRange;
	item.validateText = "must be less than or equal to" + maxValue + ".";
	item.maxValue = maxValue;
	
	return item;
}


function addRangeValidation(name, displayName, minValue, maxValue) {
	var item = addField(name, displayName, null);
	
	item.isValid = validateRange;
	item.validateText = "must be between " + minValue + " and " + maxValue + ".";
	item.minValue = minValue;
	item.maxValue = maxValue;
	
	return item;
}



function addLengthValidation(name, displayName, maxLength) {
	var item = addField(name, displayName, null);
	
	item.isValid = validateLength;
	item.validateText = "must be less than or equal to " + maxLength + " characters.";
	item.maxLength = maxLength;
	
	return item;
}


function addMinLengthValidation(name, displayName, minLength) {
	var item = addField(name, displayName, null);
	
	item.isValid = validateMinLength;
	item.validateText = "must be greater than or equal to " + minLength + " characters.";
	item.minLength = minLength;
	
	return item;
}

function addfixLengthValidation(name, displayName, fixLength) {
	var item = addField(name, displayName, null);
	
	item.isValid = validateFixLength;
	item.validateText = "must be " + fixLength + " characters.";
	item.fixLength = fixLength;
	
	return item;
}

function addDuplicatePassword(name, passwordField) {
	var item = addField(name, "password confirmation", null);
	
	item.isValid = validateDuplicate;
	item.validateText = "must match your password.";
	item.duplicate = passwordField;
	
	return item;
}


//items

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);
	}
}



//END ITEM FUNCTIONS

function validateForm(submitForm) {
	var alertMessage = "";
	
	currentForm = submitForm; 

	for(var i=0; i<formFields.length; i++) {
		var item = formFields[i];
		
		if(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.isValid()) {
			alert(item.displayName + " " + item.validateText);
			return false;
		}
	}
	return true;
}


//CLASS FUNCTIONS
function getValue() {
	if(currentForm[this.name].length > 1 && currentForm[this.name][0].type == "radio") {
		return(getSelectedText(currentForm[this.name]));
	} else if(currentForm[this.name].type == "checkbox") {
		return currentForm[this.name].checked?"checked":"";
	} else {
		return currentForm[this.name].value;
	}
}
//END CLASS FUNCTIONS


//VALIDATE FUNCTIONS
function validateText() { return true; }

function validateNumeric() {
	return isAllNumeric(this.getValue());
}

function validateMinRange() {
	return this.getValue() >= this.minValue;
}

function validateMaxRange() {
	return this.getValue() <= this.maxValue;
}

function validateRange() {
	return this.getValue() >= this.minValue && this.getValue() <= this.maxValue; 
}

function validateLength() {
	return this.getValue().length <= this.maxLength; 
}

function validateMinLength() {
	return this.getValue().length >= this.minLength; 
}

function validateFixLength() {
	return this.getValue().length == this.fixLength; 
}

function validateDuplicate() {
	return this.getValue() == currentForm[this.duplicate].value;
}


function validateEmail() {
	var email = this.getValue();
	return !(email.indexOf("@") < 1  || email.lastIndexOf(".") < email.indexOf("@"));
}
//END VALIDATE 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 "";
}


//END HELPER FUNCTIONS
