// JavaScript Document
// This is the call to the function:
//      <form id="theForm" name="theForm" method="post" action="referralFormInput.asp" onKeyPress="fnFormKeyPressed(event,1)">
//      or fnFormKeyPressed(event,1)
//      The "1" in the params is for whether or not there is a password and re-type password section in the form. Use "0" if not.
// Use accessKey="1" (true) or accessKey="0" (false) for whether or not the <input> elements are required
// The "name" value for the <input> elements should be the same as the elements label like name="Company Name"
// You must use an "id" tag for all elements
// The <form> id and name MUST BE "theForm"
// The scrpt in the <head> should look like this: <scrpt src="formErrorCheck.js"></scrpt> (add the "i" in <scrpt>)

function fnFormKeyPressed(e,hasPassword) {
	var hasErrors = false;
	var keynum;
	var keychar;
	var numcheck;
	var myObject;
	var strTemp = "The following ERRORS have occurred:\n\n";
	var targ;

	if(e != "1") {
		if(!e) { var e=window.event; }
		if(e.target) { targ=e.target; }
		else if(e.srcElement) { targ=e.srcElement; }
		if(targ.nodeType==3) { targ = targ.parentNode; } // defeat Safari bug

		var tname;
		tname=targ.tagName;
	
		if(window.event) { // IE
			keynum = e.keyCode;
		} else if(e.which) { // Netscape/Firefox/Opera
			keynum = e.which;
		}
	
		if(keynum != 13) { return; }
		if(tname == "TEXTAREA") { return; }
	}

	for(intObjIterator = 0; intObjIterator < document.getElementById("theForm").length; intObjIterator++) {
		myObject = eval(document.getElementById("theForm").elements[intObjIterator]);
		if((myObject.accessKey == "1") && (myObject.value == "")) {
			strTemp += "     REQUIRED: " + myObject.name + "\n";
			hasErrors = true;
		}
		if(myObject.type != "password") {
			myObject.value = fnReplaceBadChars(myObject.value);
		}
	}
	strTemp += "\n \n";
	
	if((hasPassword == 1) && (document.getElementById("theForm").password.value != document.getElementById("theForm").password2.value)) {
		strTemp += "     PASSWORD: Doesn't match the re-type password entry.\n \n";
		hasErrors = true;
	}
	
	if(!hasErrors) {
		document.getElementById("theForm").submit();
	} else {
		alert(strTemp);
	}
}

function fnReplaceBadChars(myString) {
	var newString = myString.replace(/"/g, "'");
	newString = newString.replace(/%/g, " percent");
	newString = newString.replace(/!/g, ".");
	return newString;
}

