// JavaScript Document
var validateForm=true;
function Form_Validator(theForm)
{
	//start login form validation
	if (theForm.formNamer.value == "loginForm"){
		if (theForm.loginName.value == "")
		{
			alert("Please enter a value for the \"EMAIL\" field.");
			theForm.loginName.focus();
			return (false);
		}
		if (!(isEmail(theForm.loginName.value)))
		{
			alert("Please enter a valid email address");
			theForm.loginName.focus();
			return (false);
		}
		if (theForm.loginPass.value == "")
		{
			alert("Please enter a value for the \"PASSWORD\" field.");
			theForm.loginPass.focus();
			return (false);
		}
	}
	//start newsletter form validation
	if (theForm.formNamer.value == "newsForm"){
		if (theForm.requestUpdates.value == "")
		{
			alert("If you would like to receive the newsletter you must first enter your email address.");
			theForm.requestUpdates.focus();
			return (false);
		}
		if (!(isEmail(theForm.requestUpdates.value)))
		{
			alert("Please enter a valid email address");
			theForm.requestUpdates.focus();
			return (false);
		} 
	}
	
	//start update password form validation
	if (theForm.formNamer.value == "updatePass"){
		if (theForm.curPass.value == "")
		{
			alert("Enter Your current password");
			theForm.curPass.focus();
			return (false);
		}
		
	  if (theForm.newPass.value == "")
		{
			alert("Please enter a value for the \"New Password\" field.");
			theForm.newPass.focus();
			return (false);
		}
		
	if (theForm.newPass.value.length < 6)
		{
			alert("Please enter a password at least 6 characters in length.");
			theForm.newPass.focus();
			return (false);
		}
		
	if (theForm.newPass.value.length > 12)
		{
			alert("Please enter a password no more than 12 characters in length.");
			theForm.newPass.focus();
			return (false);
		}

	  if (theForm.newPass.value != theForm.confPass.value)
		{
			alert("Your new password and new password confirmation do not match.  Please check these fields.");
			theForm.newPass.focus();
			return (false);
		}
	}
	
	//start register form validation
	if (theForm.formNamer.value == "register"){
	  if (theForm.fname.value == "")
		{
			alert("Please enter a value for the \"First Name\" field.");
			theForm.fname.focus();
			return (false);
		}
	 
	  if (theForm.lname.value == "")
		{
			alert("Please enter a value for the \"Last Name\" field.");
			theForm.lname.focus();
			return (false);
		}
	  if (theForm.degree.selectedIndex == 0)
		{
			alert("Please enter a value for the \"Degree\" field.");
			theForm.degree.focus();
			return (false);
		}
		
		if (theForm.degree.value == 'RT')
		{
			if (theForm.rtNum.value == ""){
			alert("Please enter a value for the \"AARC Member Number\" field.");
			theForm.rtNum.focus();
			return (false);
			}
		}
		
	  if (theForm.addr.value == "")
		{
			alert("Please enter a value for the \"Address\" field.");
			theForm.addr.focus();
			return (false);
		}
		
	  if (theForm.city.value == "")
		{
			alert("Please enter a value for the \"City\" field.");
			theForm.city.focus();
			return (false);
		}
		
	  if ((theForm.country.value == "U.S.A." || theForm.country.value == "United States") && theForm.state.selectedIndex == 0)
		{
			alert("Please enter a value for the \"State\" field.");
			theForm.state.focus();
			return (false);
		}
	
	  if (theForm.zip.value == "")
		{
			alert("Please enter a value for the \"Postal Code\" field.");
			theForm.zip.focus();
			return (false);
		} 
		
	  if (theForm.country.selectedIndex == 0)
		{
			alert("Please select one of the \"Country\" options.");
			theForm.country.focus();
			return (false);
		}
		
	  if (theForm.phone.value != "")
		{
			if (!(checkPhoneNum(theForm.phone.value)))
			{
			alert("Please enter only digit characters, \"+\", \"-\" or \"()\" in the \"Phone\" field.");
			theForm.phone.focus();
			return (false);
			}
		} 
		
	  if (theForm.fax.value != "")
		{
			if (!(checkPhoneNum(theForm.fax.value)))
			{
			alert("Please enter only digit characters, \"+\", \"-\" or \"()\" in the \"Fax\" field.");
			theForm.fax.focus();
			return (false);
			}
		} 
		
	  if (!(isEmail(theForm.email.value)))
		{
			alert("Please enter a valid email address");
			theForm.email.focus();
			return (false);
		}
		
	  if (theForm.confemail.value == "")
		{
			alert("Please enter a value for the \"Confirm Email\" field.");
			theForm.confemail.focus();
			return (false);
		}
		
	  if (theForm.email.value != theForm.confemail.value)
		{
			alert("Your email and email confirmation do not match.  Please check these fields.");
			theForm.email.focus();
			return (false);
		}
		
	  if (theForm.pass.value == "")
		{
			alert("Please enter a value for the \"Password\" field.");
			theForm.pass.focus();
			return (false);
		}
		
	  if (theForm.pass.value.length < 6)
		{
			alert("Please enter a password at least 6 characters in length.");
			theForm.pass.focus();
			return (false);
		}
		
	if (theForm.pass.value.length > 12)
		{
			alert("Please enter a password no more than 12 characters in length.");
			theForm.pass.focus();
			return (false);
		}
		
	  if (theForm.confPass.value == "")
		{
			alert("Please enter a value for the \"Confirm Password\" field.");
			theForm.confPass.focus();
			return (false);
		}

	  if (theForm.pass.value != theForm.confpass.value)
		{
			alert("Your password and password confirmation do not match.  Please check these fields.");
			theForm.pass.focus();
			return (false);
		}
	}
	
	//start updateuser form validation
	if (theForm.formNamer.value == "updateUser"){
	  if (theForm.fname.value == "")
		{
			alert("Please enter a value for the \"First Name\" field.");
			theForm.fname.focus();
			return (false);
		}
	 
	  if (theForm.lname.value == "")
		{
			alert("Please enter a value for the \"Last Name\" field.");
			theForm.lname.focus();
			return (false);
		}
		/*if (theForm.degree.selectedIndex == 0)
		{
			alert("Please enter a value for the \"Degree\" field.");
			theForm.degree.focus();
			return (false);
		}*/
		
		if (theForm.degree.value == 'RT')
		{
			if (theForm.rtNum.value == ""){
			alert("Please enter a value for the \"AARC Member Number\" field.");
			theForm.rtNum.focus();
			return (false);
			}
		}
		
		if (!(isEmail(theForm.email.value)))
		{
			alert("Please enter a valid email address");
			theForm.email.focus();
			return (false);
		}
		
	  if (theForm.confemail.value == "")
		{
			alert("Please enter a value for the \"Confirm Email\" field.");
			theForm.confemail.focus();
			return (false);
		}
		
	  if (theForm.email.value != theForm.confemail.value)
		{
			alert("Your email and email confirmation do not match.  Please check these fields.");
			theForm.email.focus();
			return (false);
		}
		
	  if (theForm.addr.value == "")
		{
			alert("Please enter a value for the \"Address\" field.");
			theForm.addr.focus();
			return (false);
		}
		
	  if (theForm.city.value == "")
		{
			alert("Please enter a value for the \"City\" field.");
			theForm.city.focus();
			return (false);
		}
		
	  if ((theForm.country.value == "U.S.A." || theForm.country.value == "United States") && theForm.state.selectedIndex == 0)
		{
			alert("Please enter a value for the \"State\" field.");
			theForm.state.focus();
			return (false);
		}
	
	  if (theForm.zip.value == "")
		{
			alert("Please enter a value for the \"Zip Code\" field.");
			theForm.zip.focus();
			return (false);
		} 
		
	  if (theForm.phone.value != "")
		{
			if (!(checkPhoneNum(theForm.phone.value)))
			{
			alert("Please enter only digit characters, \"+\", \"-\" or \"()\" in the \"Phone\" field.");
			theForm.phone.focus();
			return (false);
			}
		} 
		
	  if (theForm.fax.value != "")
		{
			if (!(checkPhoneNum(theForm.fax.value)))
			{
			alert("Please enter only digit characters, \"+\", \"-\" or \"()\" in the \"Fax\" field.");
			theForm.fax.focus();
			return (false);
			}
		} 
	}
	
	//start password assist form validation
	if (theForm.formNamer.value == "passassist"){
		if (theForm.email.value == "")
		{
			theForm.email.focus();
			return (false);
		}
		if (!(isEmail(theForm.email.value)))
		{
			alert("Please enter a valid email address");
			theForm.email.focus();
			return (false);
		}
	}
	
	//start addCourse form validation
	if (theForm.formNamer.value == "addCourse"){
		if (theForm.courseCategory.options[theForm.courseCategory.selectedIndex].value == "Select...")
		{
			alert("Please Select a Course Category");
			theForm.courseCategory.focus();
			return (false);
		}
		/*if (theForm.title.value == "")
		{
			alert("Please enter a course title");
			theForm.title.focus();
			return (false);
		}*/
		if (theForm.idNumber.value == "")
		{
			alert("Please enter a course number");
			theForm.idNumber.focus();
			return(false);
		}
	/*	if (theForm.acpeNumber.value == "")
		{
			alert("Please enter an ACPE number");
			theForm.acpeNumber.focus();
			return(false);
		}
		if (theForm.rtNumber.value == "")
		{
			alert("Please enter a RT number");
			theForm.rtNumber.focus();
			return(false);
		}*/
		if (theForm.lecturer.value == "")
		{
			alert("Please enter lecturer name");
			theForm.lecturer.focus();
			return (false);
		}
	/*	if (theForm.biography.value == "")
		{
			alert("Please enter a biography for the lectrurer.");
			theForm.biography.focus();
			return (false);
		}
		if (theForm.disclosure.value == "")
		{
			alert("Please enter a disclosure statement.");
			theForm.disclosure.focus();
			return (false);
		}
		if (theForm.overviewStatement.value == "")
		{
			alert("Please enter a lecture overview.");
			theForm.overviewStatement.focus();
			return (false);
		}
		if (theForm.affiliation.value == "")
		{
			alert("Please enter lecturer's affiliation");
			theForm.affiliation.focus();
			return (false);
		}
		if (theForm.audience.value == "")
		{
			alert("Please enter target audience info");
			theForm.audience.focus();
			return (false);
		}
		if (theForm.accred.value == "")
		{
			alert("Please enter accreditation info");
			theForm.accred.focus();
			return (false);
		}
		if (theForm.creditvalue.value == "")
		{
			alert("Please enter credit value");
			theForm.creditvalue.focus();
			return (false);
		}
		if (theForm.creditTypes.value == "")
		{
			alert("Please enter credit type value");
			theForm.creditTypes.focus();
			return (false);
		}
		if (theForm.objectives.value == "")
		{
			alert("Please enter objectives");
			theForm.objectives.focus();
			return (false);
		}
		if (theForm.expiredate.value == "")
		{
			alert("Please enter expiration date");
			theForm.expiredate.focus();
			return (false);
		}*/
		if (theForm.addQs.checked == true)
		{
			if (theForm.q1.value == "" || theForm.q1a.selectedIndex == 0 || theForm.q1opta.value == "" || theForm.q1optb.value == "" || theForm.q1optc.value == "" || theForm.q1optd.value == "" || theForm.q2.value == "" || theForm.q2a.selectedIndex == 0 || theForm.q2opta.value == "" || theForm.q2optb.value == "" || theForm.q2optc.value == "" || theForm.q2optd.value == "" || theForm.q3.value == "" || theForm.q3a.selectedIndex == 0|| theForm.q3opta.value == "" || theForm.q3optb.value == "" || theForm.q3optc.value == "" || theForm.q3optd.value == "" || theForm.q4.value == "" || theForm.q4a.selectedIndex == 0 || theForm.q4opta.value == "" || theForm.q4optb.value == "" || theForm.q4optc.value == "" || theForm.q4optd.value == "" || theForm.q5.value == "" || theForm.q5a.selectedIndex == 0 || theForm.q5opta.value == "" || theForm.q5optb.value == "" || theForm.q5optc.value == "" || theForm.q5optd.value == "" || theForm.q6.value == "" || theForm.q6a.selectedIndex == 0 || theForm.q6opta.value == "" || theForm.q6optb.value == "" || theForm.q6optc.value == "" || theForm.q6optd.value == "" || theForm.q7.value == "" || theForm.q7a.selectedIndex == 0 || theForm.q7opta.value == "" || theForm.q7optb.value == "" || theForm.q7optc.value == "" || theForm.q7optd.value == "" || theForm.q8.value == "" || theForm.q8a.selectedIndex == 0 || theForm.q8opta.value == "" || theForm.q8optb.value == "" || theForm.q8optc.value == "" || theForm.q8optd.value == "" || theForm.q9.value == "" || theForm.q9a.selectedIndex == 0 || theForm.q9opta.value == ""|| theForm.q9optb.value == "" || theForm.q9optc.value == "" || theForm.q9optd.value == "" || theForm.q10.value == "" || theForm.q10a.selectedIndex == 0 || theForm.q10opta.value == "" || theForm.q10optb.value == "" || theForm.q10optc.value == "" || theForm.q10optd.value == "") {
			alert("Please be sure to check your quiz Elements - You are no longer required to fill in all of the fields for the quiz portion of this form.  Empty fields and missing data will not be checked.  You need to do so manually.");
        	theForm.q1.focus();
		    }
		}
	}
    return (true);
}

/*function isEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}*/

function isEmail(incoming) {
	var emailstring = incoming;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
		//old regex did not allow subdomains and dots in names
		//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 0) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
			//alert("Please check your email address!");
			return false;
	}
}


function checkPhoneNum(theNumber){
	//alert(theNumber)
    var checkOK = "0123456789-()+";
	var checkStr = theNumber;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
		return (false);
		break;
		}
		allNum += ch;
		//alert(allNum);
	}
	//alert("all is good")
	return (true);
}

function displayConditionalDiv(theValue)
{
//alert(theValue.checked);
 if(theValue.value == "MD" || theValue.value == "DO" )
 	{
		document.getElementById('rtNum').value='';
		visi('residentFellow', true);
		visi('rtNumber', false);
	}else if(theValue.value == "RT"){
		visi('rtNumber', true);
		visi('residentFellow', false);
	}else{
		document.getElementById('rtNum').value='';
		visi('rtNumber', false);
		visi('residentFellow', false);
	}
}


function displayOther(theValue)
{
//alert(theValue.checked);
 if(theValue.checked == true)
 	{
		visi("otherStuff", true);
	}else{
		visi("otherStuff", false);
	}
}

function visi(nr, bool)
{
	//alert(nr + " " + bool);
	if (document.layers)
	{
		alert("layers");
		if (bool){
			vista = 'show';
			show = 'block';
		}else{
			vista = 'hide';
			show = 'none';
		}
		document.layers[nr].visibility = vista;
		document.layers[nr].display = show;
	}
	else if (document.all)
	
	{
		//IE
		if (bool){
			vista = 'visible';
			show = 'block';
		}else{
			vista = 'hidden';
			show = 'none';
		}
		document.all[nr].style.visibility = vista;
		document.all[nr].style.display = show;
	}
	else if (document.getElementById)
	{
		//Firefox
		if (bool){
			vista = 'visible';
			show = 'block';
		}else{
			vista = 'hidden';
			show = 'none';
		}
		document.getElementById(nr).style.visibility = vista;
		document.getElementById(nr).style.display = show;
	}
}