// The following is a validation script for the forms.

// Is the text field or text area blank
function isBlank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// Does the email field contain the 'at' symbol
function isEmail(s) {
	if (s.indexOf('@') >= 0) {
		return true;
	} else {
		return false;
	}
}

// Did the user enter an integer
function isNumber(s) {
	var sVal = parseFloat(s);
	if (isNaN(sVal)) {
		return false;
	} else {
		return true;
	}
}

// Did the user enter a correctly formatted phone number
function isPhoneNumber(s) {
	// var correctPhone = /\(\d{3}\)\d{3}-\d{4}/gi;
	
	// if (correctPhone.test(s)) {
	// 	return true;
	// } else {
	//	return false;
	//}
	
	return true;
}

// Did the user enter too many characters in a textarea
function isCorrectLength(s, maxChar) {
	if (s.length > maxChar) {
		return false;
	} else {
		return true;
	}
}

function isValidCompanyNo(s) {
	if (!isNumber(s)) {
		return false;
	}
	
	var sVal = parseFloat(s);
	
	if ((sVal >= 1) && (sVal <= 951)) {
		return true;
	} else if ((sVal >= 1410) && (sVal <= 1799)) {
		return true;
	} else if ((sVal >= 2000) && (sVal <= 2299)) {
		return true;
	} else if ((sVal >= 2309) && (sVal <= 2378)) {
		return true;
	} else if ((sVal >= 2447) && (sVal <= 3350)) {
		return true;
	} else if ((sVal >= 4000) && (sVal <= 4990)) {
		return true;
	} else if ((sVal >= 5110) && (sVal <= 5597)) {
		return true;
	} else if ((sVal >= 6001) && (sVal <= 6954)) {
		return true;
	} else if ((sVal >= 8151) && (sVal <= 8172)) {
		return true;
	} else if ((sVal >= 8176) && (sVal <= 8504)) {
		return true;
	} else if ((sVal >= 9602) && (sVal <= 9612)) {
		return true;
	} else if ((sVal >= 9900) && (sVal <= 9999)) {
		return true;
	} else if (sVal == 3555) {
		return true;
	} else if (sVal == 8175) {
		return true;
	} else {
		return false;
	}
}

function validateForm(form) {
	var msg;
	var empty_fields = "";
	var errors = "";

for(var i = 0; i < form.length; i++) {
	var e = form.elements[i];
	if (e.validType == "notBlank") {
		if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
			empty_fields += "\n	" + e.label;
			continue;
		}
	}
	
	if (e.validType == "email") {
		if (!isEmail(e.value)) {
			errors += "\n\n- The Email address you have entered is incorrect.";
			continue;
		}
	}
	
	if (e.validType == "phoneNumber") {
		if (!isPhoneNumber(e.value)) {
			errors += "\n\n- The " + e.label + " you entered is formatted incorrectly.";
			errors += "\n   Please format phone numbers as (xxx)xxx-xxxx."; 
			continue;
		}
	}
	
	if (e.validType == "length") {
		if (!isCorrectLength(e.value, e.lengthLimit)) {
			errors += "\n\n- The " + e.label + " field is too long.";
			errors += "\n   Please limit this field to " + e.lengthLimit + " characters or less.";
			errors += "\n   If your description is longer, please use an attachment.";
			continue;
		}
	}
	
	if (e.validType == "selectReq") {
		var choice = e[e.selectedIndex].value;
		if (choice == "none") {
			errors += "\n\n- You failed to make a selection for " + e.label;
			continue;
		}
	}
	
	if (e.validType == "validCompanyNo") {
		if (!isValidCompanyNo(e.value)) {
			errors += "\n\n- The " + e.label + " you entered is invalid.";
			errors += "\n   Please enter a valid " + e.label + ".";
			continue;
		}
	}
}

msg = "The form was not submitted because of the following error(s).\n";
msg += "Please correct these error(s) and re-submit.\n\n";

if (empty_fields) {
	msg += "- The following required field(s) are empty:\n" + empty_fields + "\n";
	if (errors) msg += errors;
	alert(msg);
	return false;
}

if (errors) {
	msg += errors;
	alert(msg);
	return false;
}

return true;
}