/********************************************************************************
*																				*
*	PLATTFORM FORM VALIDATION SCRIPT											*
*	Written by Brandon Gregory, with some code by Jacob Greenberg				*
*																				*
*	USAGE																		*
*	Add a field to your form in this format:									*
*																				*
*		<input type="hidden" name="hidden"										*
*			value="Field name|Display value,Field name|Display value" />		*
*																				*
*	The Field name has to be the name of the field in your HTML. This is 		*
*	case-sensitive. The Display value is what gets displayed to the user in		*
*	the error message when a field is not properly filled in. This should		*
*	match what the user sees on the form.										*
*																				*
*	With that field added, add this JavaScript call to your form tag:			*
*																				*
*		onsubmit="return validateForm(this)"									*
*																				*
*	That should implement the form. If you have any other JavaScript in the		*
*	onsubmit, make sure to place it before the return.							*
*																				*
********************************************************************************/

function validateForm(f) {
	if (f.required == undefined) {
		alert("'Required' field missing on form. For the validation script to function properly, you must have a hidden field named 'required' with a comma separated list of fields in the format 'HTML Field Name|Field Label'");
		return false;
	}
	var input;
	var errors = '';
	var required = f.required.value.split(",");
	for (i=0; i<required.length; i++) {
		input = eval("f." + required[i].split("|")[0]);
		if (input == undefined) { // If input doesn't exist, alert and return false
			alert('Error! ' + required[i].split("|")[1] + ' is not a valid field on your form.');
			return false;
		}
		if (input.type == "select-one") { // Select box
			if (input.length > 1 && input.selectedIndex < 1) errors += "- Please select a value for " + required[i].split("|")[1] + "\n";
		} else if (input.type == "select-multiple") { // Multiple select box
			var selected = false;
			for (j=0; j<input.options.length; j++) {
				if (input.options[j].selected) {
					selected = true;
					j = input.options.length;
				}
			}
			if (!selected) errors += "- Please select a value for " + required[i].split("|")[1] + "\n";
		} else if (input.length) { // Radio button
			var checked = false;
			for (j=0; j<input.length; j++) {
				if (input[j].checked) {
					checked = true;
					j = input.length;
				}
			}
			if (!checked) errors += "- Please select a value for " + required[i].split("|")[1] + " \n";
		} else if (input.type == "text") { // Text box
			if (required[i].split("|")[0].indexOf("phone") > -1) errors += phoneIsValid(input,required[i].split("|")[1]);
			else if (required[i].split("|")[0] == "confirmemail" && input.value != f.email.value) errors += "- Confirm e-mail address does not match\n";
			else if (required[i].split("|")[0] == "email" && (input.value.length < 1 || input.value.indexOf("@") < 1 || input.value.indexOf(".") == -1)) errors += '- ' + required[i].split("|")[1] + " is missing or not valid\n";
			else if (!input.value.length) errors += '- ' + required[i].split("|")[1] + " is missing\n";
		} else if (input.type == "textarea" || input.type == "password") { // Text area or password
			if (!input.value.length) errors += '- ' + required[i].split("|")[1] + " is missing\n";
		} else if (input.type == "checkbox") { // Checkbox
			if (!input.checked) errors += "- " + required[i].split("|")[1] + " must be checked.\n";
		}
	}
	if (errors.length) { // If there's an error, alert the user and return false
		errors = "The following errors were found:\n\n" + errors;
		alert(errors);
		return false;
	} else return true;
}

function phoneIsValid(Obj,sLabel) {
	var sPhone = '';
	var sPhoneTemp = Obj.value;
	var sNumbers = '1234567890';
	for (var i=0;i<sPhoneTemp.length;i++) {
		if (sNumbers.indexOf(sPhoneTemp.charAt(i)) > -1) sPhone += sPhoneTemp.charAt(i);
	}
	if (sPhone.charAt(0) == '1' || sPhone.charAt(0) == 1) sPhone = sPhone.substring(1,(sPhone.length));
	if (sPhone.length != 10) return '- ' + sLabel + ' must be a 10-digit number\n';
	else Obj.value = sPhone;
	var sAreaCode = sPhone.substring(0,3);
	var sPrefix = sPhone.substring(3,6);
	var sNumber = sPhone.substring(6,10);
	ary7NotAllowed = new Array('1234567','4567890','0000000','1111111','2222222','3333333','4444444','5555555','6666666','7777777','8888888','9999999','3456789','4567890')
	ary3NotAllowed = new Array('000','911','555','012','123');
	for (var i=0;i<ary7NotAllowed.length;i++) {
		if (sPrefix.toString() + sNumber.toString() == ary7NotAllowed[i]) return '- ' + sLabel + ' must be a valid phone number\n';
	}
	for (var i=0;i<ary3NotAllowed.length;i++) {
		if (sPrefix.toString() == ary3NotAllowed[i].toString()) return '- ' + sLabel + ' must be a valid phone number\n';
	}
	return '';
}