/*
** Author: Lee Springer 
** HERE IS A QUICK EXAMPLE:

<html>
<head>

<!-- include this file -->
<script LANGUAGE="JavaScript" src="validate.js"></script>

<script LANGUAGE="JavaScript">

// Define how and what to look for in an array. 
// Make sure to wrap this as strings, if they are not wrapped in quotes the script
// will try to execute the function and error out.
// The standard format is;
// 'functionName('form id','label id','error style class')',
// Double check the function to see if there are other arguements

var validate = new Array(
	'checkBlank("your first name","label_1","red")',
	'checkBlank("your last name","label_2","red")'
	);
</script>

<!-- Make sure to define a highlighted and normal style class inline or in a CSS file -->
<style>
label
{
	color: #000000;
}
.red
{
	color: #FF0000;
}
</style>
</head>
<body>
	
<!-- Pass in the form name and array to checkForm make sure to return false -->
<form name="exampleForm" action="results.php" action="POST" onsubmit="checkForm(exampleForm,validate);return false;">

<!-- Wrap your form element labels with an id and a stylesheet class-->
<label for="your first name" id="label_1">Your First Name:</label>

<!-- Use readable ids on your elements to make the error messages user friendly -->
<input type="text" name="your first name" id="your first name" /><br />

<label for="your last name" id="label_2">Your Last Name:</label>

<!-- To prompt the user if they try to navigate away from this page call the navAway() function
	via an onchange arguement to the elements you wish to track.
	NOTE: This works in Firefox and IE, it does not work in Safari and has not been tested
	in any other browsers. -->
<input type="text" name="your last name" id="your last name" onchange="navAway();" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

*/

var error = new Array();
var origClassName = new Array();

function navAway () {
	window.onbeforeunload = function (oEvent) {
		if(!oEvent) oEvent = window.event;
		oEvent.returnValue = "Changes have been made to this page.";
	}
}

function highLight(labelName,style) {
	var flag = false;
	for(var x=0;x<origClassName.length;x++) {
		if(labelName == origClassName[x][0]) {
			flag = true;
		}
	}
	if(!flag) {
		origClassName.push(new Array(labelName,document.getElementById(labelName).className));
	}
	document.getElementById(labelName).className = style;
}

function dimDown(labelName) {
	for(var z=0;z<origClassName.length;z++) {
		if(origClassName[z][0] == labelName) {
			document.getElementById(labelName).className = origClassName[z][1];
		}
	}
}

// Use to check that a field is not blank. Can use on radio, text, and select fields.
function checkBlank(name,labelName,style) {
 	if (document.getElementById(name).value == "") {
    	error.push("You've left " + name + " blank.\n");
   		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
 }
 
// Use to check that an email address is in a valid format
function checkEmail(name,labelName,style) {
	var emailFormat = /[a-zA-Z0-9]+([_.-][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([.-][a-zA-Z0-9]+)*)+\.+[a-zA-Z]{2,}$/;
	if (!document.getElementById(name).value.match(emailFormat)) {
		error.push("The format of the email address entered is not valid.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that a US phone number is formatted correctly
function checkPhone(name,labelName,style) {
	var legalChars = /([0-9]{10} | [0-9]{3}[-.][0-9]{3}[-.][0-9]{4})/;
	if (document.getElementById(name).value.match(illegalChars)) {
		error.push("There is a problem with the phone number you entered.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that a field only contains numerals
function checkNumbers(name,labelName,style) {
	var illegalChars = /[^0-9]/;
	if (document.getElementById(name).value.match(illegalChars)) {
		error.push("Please enter only numbers in the " + name + " field.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that a field only contains numerals and/or letters
function checkAlphaNum(name,labelName,style) {
	var illegalChars = /[^0-9A-Za-z]/;
	if (document.getElementById(name).value.match(illegalChars)) {
		error.push("Please enter only numbers in the " + name + " field.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that a field is formatted as US currency
function checkCurrency(name,labelName,style) {
	var legalChars = /[0-9]*\.[0-9]{2}/;
	if (document.getElementById(name).value.match(legalChars)) {
		error.push("Please enter a dollar amount in currency format (e.g. 3.50) in the " + name + " field.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that 5 digit ZIP has been entered.
function checkZip(name,labelName,style) {
	var legalChars = /[0-9]{5}/;
	if (document.getElementById(name).value.match(legalChars)) {
		error.push("Please enter a valid zip code.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}

// Use to check that two fields have the same value.
function confirmFields(name,name2,labelName,labelName2,style) {
	if(document.getElementById(name).value != document.getElementById(name2).value){
		error.push("The email addresses you entered do not match.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		if(labelName2) {
 			highLight(labelName2, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		if(labelName2) {
 			dimDown(labelName2);
 		}
 		return false;
 	}
}

// Use to check that a checkbox is not blank 
function checkCheckbox(name,labelName,style) {
	if(!document.getElementById(name).checked) {
		error.push("The " + name + " checkbox has been left blank.\n");
		if(labelName) {
 			highLight(labelName, style);
 		}
 		return true;
 	}
 	else {
 		if(labelName) {
 			dimDown(labelName);
 		}
 		return false;
 	}
}


// Use with your form tag to run onsubmit. This loops through all the other
// functions that you have defined in an array.
function checkForm(formName,arrayName) {
	var flag=false;
	var flagText='';
	for(var t=0;t<arrayName.length;t++) {
		if (eval(arrayName[t])) {
			flag=true;
		}
	}
	if (flag) {
		for(var y=0;y<error.length;y++) {
			flagText = flagText + error[y];
		}
		alert(flagText);
		error = new Array();
	}
	else {
		document.eval(formName).submit();
	}
}	

function blankOut(field,setValue)
{
	if(document.getElementById(field).value == setValue)
	{
		document.getElementById(field).value = '';
	}
}	

function setValue(field,setvalue)
{
	if(document.getElementById(field).value == '')
	{
		document.getElementById(field).value = setvalue;
	}
}

function checkRegForm() {
firstname = document.getElementById("first_name").value;
lastname = document.getElementById("last_name").value;
email = document.getElementById("email").value;
company = document.getElementById("company").value;
var emailFilter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var legalChars=/^[a-zA-Z_-]+$/;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]0-9]/;
var errorCtr = 0;
 
if (firstname == "") {
	document.getElementById("firstnameError").style.display = "inline";
	errorCtr++;
	} else {
	document.getElementById("firstnameError").style.display = "none";
	if (firstname.match(illegalChars)) { 
		document.getElementById("firstnameValError").style.display = "inline";
		errorCtr++;
		}else{
		document.getElementById("firstnameValError").style.display = "none";
		} 
	}
if (lastname == "") {
	document.getElementById("lastnameError").style.display = "inline";
	errorCtr++;
	} else {
	document.getElementById("lastnameError").style.display = "none";
	if (lastname.match(illegalChars)) { 
		document.getElementById("lastnameValError").style.display = "inline";
		errorCtr++;
		}else{
		document.getElementById("lastnameValError").style.display = "none";
		} 
	}
if (email == "") {
	document.getElementById("emailError").style.display = "inline";
	errorCtr++;
	} else {
	document.getElementById("emailError").style.display = "none";
	if (!(emailFilter.test(email))) { 
		document.getElementById("emailValError").style.display = "inline";
		errorCtr++;
		}else{
		document.getElementById("emailValError").style.display = "none";
		} 
	}
if (company == "") {
	document.getElementById("companyError").style.display = "inline";
	errorCtr++;
	} else {
	document.getElementById("companyError").style.display = "none";
	}

if (errorCtr > '0'){
	return false;
	}
return true;
}

