function doFocus(elemID) {
	document.getElementById(elemID).focus();	
}

// FORM VALIDATION //
function FailSubmit(myAlert,myAction) {
	alert(myAlert);
	eval(myAction);
	return false;
}

function CheckFields(myForm)
{
	  var numOfElements = myForm.elements.length;
	  var forbiddenIntChars = new Array('.','+','-'); // characters that will still evaluate to true in an isNaN function
	  var foundIntChar = false;
	  var forbiddenFloatChars = new Array('+','-');
	  var foundFloatChar = false;
	  var myElem = new Object();
	  
	  // loops through all the elements in the submitted form
      for(var i=0; i<numOfElements; i++)
      {
            myElem = myForm.elements[i];
			// checks to see if the field is required
			if(myElem.getAttribute("required") == "required" && myElem.value == "")
            {
				//alert(myElem.name);
				return FailSubmit(myElem.getAttribute("alertname") + " is a required field.",myElem.focus());
            }
			
			// looks for the custom attribute 'Email'
			if(myElem.getAttribute("customtype") == "Email" && myElem.value != "")
			{
				// checks to see if the inputted value is formatted as an email should be
				var isOK = true;
				if (myElem.value.length<5) 
				{
					isOK = false;
				}
				else if (myElem.value.indexOf("@") < 1)
				{
					isOK = false;
				}
				else if (myElem.value.length - myElem.value.indexOf("@") < 4)
				{
					isOK = false;
				}
				
				// alerts the user if the field is not a valid email address
				if (!isOK)
				{
					return FailSubmit("The entered email address is invalid.",myElem.select());
				}
			}
			
			// looks for the custom attribute 'Integer'
			if(myElem.getAttribute("customtype") == "Integer" && myElem.value != "")
			{
				// isNaN will allow various non numbers to pass, this checks the forbidden
				// characters array to see if any of those characters are in the input
				for(var l=0; l<forbiddenIntChars.length; l++) {
					if(myElem.value.indexOf(forbiddenIntChars[l]) >= 0) {
						foundIntChar = true;
					}
				}
				// checks the input to see if the usual isNaN invalid chars are there
				// and also checks to see if the custom loop found anything as well
				if(isNaN(myElem.value) || foundIntChar)
				{
					return FailSubmit(myElem.getAttribute("alertname") + " must be numbers only.",myElem.select());
				}
			}
			
			// looks for the custom attribute 'Float' or 'Money'
			if((myElem.getAttribute("customtype") == "Float" || myElem.getAttribute("customtype") == "Money") && myElem.value != "")
			{
				// isNaN will allow various non numbers to pass, this checks the forbidden
				// characters array to see if any of those characters are in the input
				// THIS ONE WILL ALLOW '.' TO PASS
				for(var l=0; l<forbiddenFloatChars.length; l++) {
					if(myElem.value.indexOf(forbiddenFloatChars[l]) >= 0) {
						fountFloatChar = true;	
					}
				}
				// checks the input to see if the usual isNaN invalid chars are there
				// and also checks to see if the custom loop found anything as well
				if(isNaN(myElem.value) || foundFloatChar)
				{
					return FailSubmit(myElem.getAttribute("alertname") + " must be numbers only.",myElem.select());
				}
				
				// looks for the custom attribute 'Money'
				if(myElem.getAttribute("customtype") == "Money") {
					if(myElem.value.indexOf('.') < 0) { // there is no decimal point
						if(myElem.value.length < 12) {
							myElem.value += ".00"; // add .00 at the end
						} else {
							// if there are more than 12 characters in this field it will extend past the
							// char(15) restriction in the database
							return FailSubmit(myElem.getAttribute("alertname") + " is too long.",myElem.select());
						}
					}
					else if(myElem.value.indexOf('.') != (myElem.value.length - 3)) { // check to see where decimal is
						// if it's not at xx.xx then inform the user
						return FailSubmit(myElem.getAttribute("alertname") + " is not a monetary value.",myElem.select());
					}
				}

			}
			
			// looks for the custom attribute 'Authorize' that is a check box and unchecked
			if(myElem.getAttribute("customtype") == "Authorize" && myElem.type == "checkbox" && !myElem.checked) {
				// if this element is a checkbox and unchecked alert them that it must be
				// checked to proceed - similar to a terms & conditions authorization
				
				return FailSubmit(myElem.getAttribute("alertname"),myElem.focus());
			}
			
			// look for custom attribute 'Date'
			/*if(myElem.getAttribute("customtype") == "Date" && myElem.value != "")
			{
				// isNaN will allow various non numbers to pass, this checks the forbidden
				// characters array to see if any of those characters are in the input
				for(var l=0; l<forbiddenIntChars.length; l++) {
					if(myElem.value.indexOf(forbiddenIntChars[l]) > 0) {
						foundIntChar = true;
					}
				}
				// checks the input to see if the usual isNaN invalid chars are there
				// and also checks to see if the custom loop found anything as well
				if(isNaN(myElem.value) || foundIntChar)
				{
					return FailSubmit(myElem.getAttribute("alertname") + " is not correctly formatted. The formatting is YYYYMMDD.",myElem.select());
				} else {
					var now = new Date();
					var thisYear = now.getFullYear();
					var inputYear = myElem.value.substr(0,4);
					var inputMonth = myElem.value.substr(4,2);
					var inputDay = myElem.value.substr(6,2);
					// format of YYYYMMDD
					
					if (myElem.getAttribute("alertname").match('Birth') && inputYear < (thisYear - 120)) { 
					// highly unlikely someone will be more than 120 yrs old
						return FailSubmit("The date you entered is beyond the acceptable range.",myElem.select());
					}
					else if(myElem.getAttribute("alertname").match('Birth') && inputYear >= (thisYear - 17)) {
						return FailSubmit("You must be at least 18 years old to complete this application.",myElem.select());
					}
					else if (inputMonth < 1 || inputMonth > 12) {
						return FailSubmit("The month you entered is not formatted properly.",myElem.select());
					}
					else if (inputDay < 1 || inputDay > 31) {
						return FailSubmit("The day you entered is not formatted properly.",myElem.select());
					}
				}
			}*/
			
			// looks for password fields and checks their input length
			if(myElem.type=="password" && myElem.value != "") {
				if(myElem.length < 6 || myElem.length > 12) {
					return FailSubmit("Your password must be no less than 6 characters long and no more than 12 characters long.",myElem.focus());
				}
			}
			
			// looks for the custom attribute "ConfirmPassword" to verify previous field equals current field
			if(myElem.getAttribute("customtype") == "ConfirmPassword" && myElem.value != "") {
				if(myElem.value != myForm.elements[i-1].value) { // current field value != previous field value
					return FailSubmit("The passwords do not match.",myForm.elements[i-1].select());
				}
			}
			
	    	// looks for textareas in the form and replaces \r\n with <br>
			/* NOT USED IN THIS APPLICATION
			if(myElem.type=="textarea" && myElem.value != "")
			{
				  myElem.value = myElem.value.split('\r\n').join('<br />');
			}*/
			
			// looks for the custom attribute 'reqlen' and ensures that the user input for this field
			// meets the required length requirements - mainly used for phone numbers, SSNs, and zip codes
			if(myElem.getAttribute("reqlen") && myElem.value != "") {
				if(myElem.value.length	!= myElem.getAttribute("reqlen")) {
					return FailSubmit(myElem.getAttribute("alertname") + " must be " + myElem.getAttribute("reqlen") + " characters long.",myElem.focus());
				}
			}
			
			// looks for the custom attribute 'minlen' and ensures that the user input for this field
			// meets the required length requirements - mainly used for passwords
			if(myElem.getAttribute("minlen") && myElem.value != "") {
				if(myElem.value.length < myElem.getAttribute("minlen")) {
					return FailSubmit(myElem.getAttribute("alertname") + " must be between 6 - 12 characters long.",myElem.focus());
				}
			}
	
      }

// let the form pass if everything passed validation
return true;

/*
EXAMPLE OF FORM TAG
<form action="somepage.xxxx" method="post" name="form1" id="form1" onSubmit="return CheckFields(this);">

EXAMPLE OF FORM FIELD
<input name="name" type="text" class="input" id="name" required="required" reqlen="4" alertname="CUSTOM ALERT" size="30" value="" />
*/

}

function hearAbout(myVal) {
	if(myVal == "Other") {
		document.getElementById('hear_about_other_row').style.display = 'block';
		document.getElementById('hear_about_other').focus();
		document.getElementById('referral_info_row').style.display = 'none';
		document.getElementById('referral_info').value='';
	} 
	else if (myVal == "Referral") {
		document.getElementById('referral_info_row').style.display = 'block';
		document.getElementById('referral_info').focus();
		document.getElementById('hear_about_other_row').style.display = 'none';
		document.getElementById('hear_about_other').value = '';
	} else {
		document.getElementById('referral_info_row').style.display = 'none';
		document.getElementById('referral_info').value = '';
		document.getElementById('hear_about_other_row').style.display = 'none';
		document.getElementById('hear_about_other').value = '';
	}
}

function showHide(doAction,showHideID) {
	if (doAction == 'show') {
		document.getElementById(showHideID).style.display = '';
	} else {
		document.getElementById(showHideID).style.display = 'none';
	}
}