// JavaScript Document

var curLocation = self.location //current, global URI variable


//***************Cancel form submission and redirect**********************//

function cancelRec(){
if(document.regForm.Cancel.value == "Cancel")
	{
	location.href="/default.htm";
	}
}

//***************BEGIN MAIN VALIDATION function**********************//


function checkUpdateForm()
{	
	var type_info = "";
	var fFld = document.regForm;
	//var nameTest=/[^a-z]/i;
	var nameTest=/[^A-Z\-\.\'\s\,]/i;
	var miTest=/[^a-z]/i;
	var charDigTest=/[^A-Z\-\.\'\,\;\:\/\&\\\s\d]/i; //restricted character, special characters, whitespace, digit entry 
	var emailTest=/^.+@.+\..{2,3}$/;
	var phoneTest=/\d{3}-\d{3}-\d{4}/;


//Question 1: First Name
	if(fFld.First_Name.value == "" || fFld.First_Name.value.length > 20 || nameTest.test(fFld.First_Name.value))
	{
	type_info += "Question 1: Enter your first name.\n";
    fFld.First_Name.style.backgroundColor="ffcc99";
	fFld.First_Name.focus();
	}
		else
		{
		fFld.First_Name.style.backgroundColor="ffffff";
		}

//Question 2: Middle Initial
	if(fFld.Middle_Initial.value == "" || fFld.Middle_Initial.value.length > 1 || miTest.test(fFld.Middle_Initial.value))
	{
	type_info += "Question 2: Enter your middle initial.\n";
    fFld.Middle_Initial.style.backgroundColor="ffcc99";
	fFld.Middle_Initial.focus();
	}
		else
		{
		fFld.Middle_Initial.style.backgroundColor="ffffff";
		}
		
//Question 3: Last Name
	if(fFld.Last_Name.value == "" || fFld.Last_Name.value.length > 30 || nameTest.test(fFld.Last_Name.value))
	{
	type_info += "Question 3: Enter your last name.\n";
    fFld.Last_Name.style.backgroundColor="ffcc99";
	fFld.Last_Name.focus();
	}
		else
		{
		fFld.Last_Name.style.backgroundColor="ffffff";
		}
		
//Question 5: Employer
	if(fFld.Employer.value == "" || fFld.Employer.value.length > 100 || charDigTest.test(fFld.Employer.value))
	{
	type_info += "Question 5: Enter the name of the company for which you work.\n";
    fFld.Employer.style.backgroundColor="ffcc99";
	fFld.Employer.focus();
	}
		else
		{
		fFld.Employer.style.backgroundColor="ffffff";
		}
//Question 6: Job Title
	if(fFld.Title.value == "" || fFld.Title.value.length > 100 || charDigTest.test(fFld.Title.value))
	{
	type_info += "Question 6: Enter your job title.\n";
    fFld.Title.style.backgroundColor="ffcc99";
	fFld.Title.focus();
	}
		else
		{
		fFld.Title.style.backgroundColor="ffffff";
		}

//Question 7: Phone

	if(!(phoneTest.test(fFld.Phone.value))){
	type_info += "Question 7: Enter your 10-digit work phone number separated by dashes: ###-###-####.\n";
	fFld.Phone.style.backgroundColor="ffcc99";
	fFld.Phone.focus();
		}
		else
		{
		fFld.Phone.style.backgroundColor="ffffff";
		}


//Question 8: Email Address
	if(fFld.Email.value == "" || fFld.Email.value.length > 75 || !(emailTest.test(fFld.Email.value)))
	{
	type_info += "Question 8: Enter your work email address.\n";
	fFld.Email.style.backgroundColor="ffcc99";
	fFld.Email.focus();
	}
		else
		{
		fFld.Email.style.backgroundColor="ffffff";
		}

//Question 9: Mailing Address
	if(fFld.Mailing_Address.value.length < 15 || fFld.Mailing_Address.value.length > 200 || charDigTest.test(fFld.Mailing_Address.value))
	{
	type_info += "Question 9: Enter your work mailing address.\n";
	fFld.Mailing_Address.style.backgroundColor="ffcc99";
	fFld.Mailing_Address.focus();
	}
		else
		{
		fFld.Mailing_Address.style.backgroundColor="ffffff";
		}


//Question 10: Tri-State member? is a radio button selected?
	var memberRadio = "no";
	for(var loop = 0; loop < fFld.Member.length; loop++)
	{
		if(fFld.Member[loop].checked == true)
		{
		memberRadio = "yes";
		fFld.radioError.style.backgroundColor="#ffffff"; //make sure flag field style is set to default if a radio button selected
		fFld.radioError.value = "";
		}
	}
	if (memberRadio == "no")
	{
	type_info += "Question 10: Select a radio button indicating whether or not you are a Tri-State Stroke Network member.\n";
	fFld.radioError.style.backgroundColor="#ffcc99"; //flag the question prompt for user attention if no radio button selected
	fFld.radioError.value = "SELECT \"Yes\" or \"No\"!"
	}
	if (fFld.Member[0].checked==true) //if TSSN member "Yes" radio button is selected then is a state selected from the "State" drop down menu?
	{
	var stateSelection = document.regForm.State[document.regForm.State.selectedIndex].value;
		if(stateSelection == "" || stateSelection == "Select State")
			{
			type_info += "Question 10: Select the Tri-State Stroke Network member state from the drop-down menu.\n";
			fFld.State.style.backgroundColor="#ffcc99"; //flag the question prompt for user attention if no state selected
			}
			else
			{
			fFld.State.style.backgroundColor="#ffffff"; //white background/no color to flag the drop-down if a state is selected
			}
	}
	

//Question 11: CE Credit Radio - check to see if a radio button is selected; if so, then are checkboxes also selected

	var cmeCreditRadio = "no"; //hold state of radio button selection; default state is no radio button selected, so if default remains the case then an alert is necessary on submit
	for(var loop = 0; loop < fFld.CME_CreditRad.length; loop++)
	{
		if(fFld.CME_CreditRad[loop].checked == true) //a radio button is selected so no alert
		{
		cmeCreditRadio = "yes"; //  a radio button is selected
		fFld.flagCreditError.style.backgroundColor="#ffffff"; //make sure flag field style is set to default if a radio button selected
		fFld.flagCreditError.value = "";
		}
	}
	if (cmeCreditRadio == "no") //neither radio button is selected so show alert and flag field
	{
	type_info += "Question 11: Select a radio button indicating whether or not you are requesting Continuing Education Credits.\n";
	fFld.flagCreditError.style.backgroundColor="#ffcc99"; //flag the question prompt for user attention if no radio button selected
	fFld.flagCreditError.value = "SELECT \"Yes\" or \"No\"!"
	}
	

//reCAPTCHA
	if(fFld.recaptcha_response_field.value == "")
	{
	type_info += "A response to the reCAPTCHA challenge question.";
	fFld.recaptcha_response_field.style.backgroundColor="ffcc99";
	fFld.recaptcha_response_field.focus();
	}
		else
		{
		fFld.recaptcha_response_field.style.backgroundColor="ffffff";
		}
	
	//if the typ_info variable is empty then the form entries pass validation and the form should be submitted unless
	if (type_info == "")
	{
	return true;
	}
	else
	{		
		type_info = "Please go back and enter at least the following information:\n" + type_info;
		alert (type_info);	
		return false;
	}
}


//***************END MAIN VALIDATION function**********************//


//************count down allowed characters in text box****************//
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}


//***************** TOGGLE and RESET sub controls on user click ********************//

//Question 10: toggle the state drop-down if yes or no TSSN state member radio button selected
// handled by onclick event rather than onsubmit, which is event triggering most of the validation (i.e., checkUpdateForm()) on the page
function radioButtonAction(){

/*block for state drop-down display; dependency on member radio buttons*/
if(document.regForm.Member[0].checked == true)
	{
	//alert("show it");
	document.getElementById("stateDrop").style.display="block"; //show div containing drop-down menu of TSSN member states if user checks that "Yes" they are a member of the TSSN Network
	}

if(document.regForm.Member[1].checked == true)
	{
	document.getElementById("State").selectedIndex = 0; //reset State drop-down menu to default if no is checked
	document.getElementById("stateDrop").style.display="none"; //hide div containing drop down menu if no is checked
	}

}


//***************** END TOGGLE and RESET sub controls on user click ********************//
