function doReset()
{
	document.forms[0].reset();
}


function formCheck(mode, description)
{ 

	if(document.forms[0].RECEIVE_YES_NO[0].checked == false && document.forms[0].RECEIVE_YES_NO[1].checked == false)
	{
	alert("Please specify whether you wish to receive " + description +"?");
        document.forms[0].RECEIVE_YES_NO[0].focus();
		return false;
	}

	if(document.forms[0].RECEIVE_YES_NO[1].checked == true)
	// stop validation when "not to receive" response
	{
		return true;
	}

	if (mode == "SHORT")
	{
		return true;
	}
	

	if (mode == "ADD")
	{
		if(document.forms[0].FNAME.value == "")
		{
			alert("Please specify your First name.");
        		document.forms[0].FNAME.focus();
			return false;
		}
		if(document.forms[0].LNAME.value == "")
		{
			alert("Please specify your Last name.");
        		document.forms[0].LNAME.focus();
			return false;
		}
	}
	if (mode != "RENEW")
	{
		if(document.forms[0].TITLE.value == "")
		{
			alert("Please specify your Title.");
	        document.forms[0].TITLE.focus();
			return false;
		}
		if(document.forms[0].COMPANY.value == "")
		{
			alert("Please specify your Company name.");
        		document.forms[0].COMPANY.focus();
			return false;
		}
		if(document.forms[0].ADDRESS2.value == "")
		{
			alert("Please specify your Street Address.");
	        	document.forms[0].ADDRESS2.focus();
			return false;
		}
		if(document.forms[0].CITY.value == "")
		{
			alert("Please specify your City.");
	        	document.forms[0].CITY.focus();
			return false;
		}
		if (document.forms[0].STATE.selectedIndex == 0)
		{
			alert("Please specify your State.");
		        document.forms[0].STATE.focus();
			return false;
		}

		// validate Canadian state/postal code combination
		if (!ValidCanPostal(document.forms[0].STATE[document.forms[0].STATE.selectedIndex].value,document.forms[0].ZIPCODE.value))
		{
			alert ("Invalid Canadian province/postal code combination.");
        		document.forms[0].STATE.focus();
			return false;
		}

		if (document.forms[0].STATE[document.forms[0].STATE.selectedIndex].value == "53")
		{
			if (document.forms[0].COUNTRY.value == "")
			{
				alert("Please specify your Country.");
	        		document.forms[0].COUNTRY.focus();
				return false;
			}
		}
		else
		{
			if (document.forms[0].ZIPCODE.value.length < 5)
			{
				alert("Please specify your Zip Code.");
	        		document.forms[0].ZIPCODE.focus();
				return false;
			}
		}
	}
		
	if ((document.forms[0].EMAIL_ADDRESS.value != "") && (!validEmail(document.forms[0].EMAIL_ADDRESS.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[0].EMAIL_ADDRESS.focus();
		return false;
	}	

     //Check Question two "other" boxes when other selected and employer is Federal Government
    if (CheckedRadioValue(document.forms[0].EMPLOYER) == "01")
    {
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "05") && (document.forms[0].GRADE_RANK_DESC1.value == ""))
        {
		  AlertRankOther(1);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "41") && (document.forms[0].GRADE_RANK_DESC2.value == ""))
        {
		  AlertRankOther(2);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "54") && (document.forms[0].GRADE_RANK_DESC3.value == ""))
        {
		  AlertRankOther(3);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "60") && (document.forms[0].GRADE_RANK_DESC4.value == ""))
        {
		  AlertRankOther(4);
		  return false;
        }
    }  
}  

function formCheckAlt(mode, description)
{ 
	if(document.forms[0].RECEIVE_YES_NO[0].checked == false && document.forms[0].RECEIVE_YES_NO[1].checked == false)
	{
	alert("Please specify whether you wish to receive " + description +"?");
        document.forms[0].RECEIVE_YES_NO[0].focus();
		return false;
	}
if(document.forms[0].RECEIVE_YES_NO[1].checked == true)
	// stop validation when "not to receive" response
	{
		return true;
	}
	if (mode == "ADD")
	{
		if(document.forms[0].FNAME.value == "")
		{
			alert("Please specify your First name.");
        		document.forms[0].FNAME.focus();
			return false;
		}
		if(document.forms[0].LNAME.value == "")
		{
			alert("Please specify your Last name.");
        		document.forms[0].LNAME.focus();
			return false;
		}
	}
	if (mode != "RENEW")
	{
		if(document.forms[0].TITLE.value == "")
		{
			alert("Please specify your Title.");
	        document.forms[0].TITLE.focus();
			return false;
		}
		if(document.forms[0].COMPANY.value == "")
		{
			alert("Please specify your Company name.");
        		document.forms[0].COMPANY.focus();
			return false;
		}
		if(document.forms[0].ADDRESS2.value == "")
		{
			alert("Please specify your Street Address.");
	        	document.forms[0].ADDRESS2.focus();
			return false;
		}
		if(document.forms[0].CITY.value == "")
		{
			alert("Please specify your City.");
	        	document.forms[0].CITY.focus();
			return false;
		}
		if (document.forms[0].STATE.selectedIndex == 0)
		{
			alert("Please specify your State/Province.");
		        document.forms[0].STATE.focus();
			return false;
		}

		// validate Canadian state/postal code combination
		if (!ValidCanPostal(document.forms[0].STATE[document.forms[0].STATE.selectedIndex].value,document.forms[0].ZIPCODE.value))
		{
			alert ("Invalid Canadian province/postal code combination.");
        		document.forms[0].STATE.focus();
			return false;
		}

		if (document.forms[0].STATE[document.forms[0].STATE.selectedIndex].value == "53")
		{
			if (document.forms[0].COUNTRY.value == "")
			{
				alert("Please specify your Country.");
	        		document.forms[0].COUNTRY.focus();
				return false;
			}
		}
		else
		{
			if (document.forms[0].ZIPCODE.value.length < 5)
			{
				alert("Please specify your Zip/Postal Code.");
	        		document.forms[0].ZIPCODE.focus();
				return false;
			}
		}

		if ((document.forms[0].HOME_ADDRESS1.value != "")
			|| (document.forms[0].HOME_ADDRESS2.value != "")
			|| (document.forms[0].HOME_CITY.value != "")
			|| (document.forms[0].HOME_ZIPCODE.value != "")
                        || (document.forms[0].HOME_STATE.selectedIndex > 0))
		{
			if(document.forms[0].HOME_ADDRESS2.value == "")
			{
				alert("Please specify your optional delivery Street Address.");
	        		document.forms[0].HOME_ADDRESS2.focus();
				return false;
			}
			if(document.forms[0].HOME_CITY.value == "")
			{
				alert("Please specify your optional delivery City.");
	        		document.forms[0].HOME_CITY.focus();
				return false;
			}
			if (document.forms[0].HOME_STATE.selectedIndex == 0)
			{
				alert("Please specify your optional delivery State/Province.");
		        	document.forms[0].HOME_STATE.focus();
				return false;
			}

			// validate Canadian state/postal code combination
			if (!ValidCanPostal(document.forms[0].HOME_STATE[document.forms[0].HOME_STATE.selectedIndex].value,document.forms[0].HOME_ZIPCODE.value))
			{
				alert ("Invalid optional delivery Canadian province/postal code combination.");
        			document.forms[0].HOME_STATE.focus();
				return false;
			}

			if (document.forms[0].HOME_STATE[document.forms[0].HOME_STATE.selectedIndex].value == "53")
			{
				if (document.forms[0].HOME_COUNTRY.value == "")
				{
					alert("Please specify your optional delivery Country.");
	        			document.forms[0].HOME_COUNTRY.focus();
					return false;
				}
			}
			else
			{
				if (document.forms[0].HOME_ZIPCODE.value.length < 5)
				{
					alert("Please specify your optional delivery Zip/Postal Code.");
	        			document.forms[0].HOME_ZIPCODE.focus();
					return false;
				}
			}
		}
	}
    //Check Question two "other" boxes when other selected and employer is Federal Government
    if (CheckedRadioValue(document.forms[0].EMPLOYER) == "01")
    {
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "05") && (document.forms[0].GRADE_RANK_DESC1.value == ""))
        {
		  AlertRankOther(1);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "41") && (document.forms[0].GRADE_RANK_DESC2.value == ""))
        {
		  AlertRankOther(2);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "54") && (document.forms[0].GRADE_RANK_DESC3.value == ""))
        {
		  AlertRankOther(3);
		  return false;
        }
        if ((CheckedRadioValue(document.forms[0].GRADE_RANK) == "60") && (document.forms[0].GRADE_RANK_DESC4.value == ""))
        {
		  AlertRankOther(4);
		  return false;
        }
    }
 }  
 
 function PaidformCheck(mode, description)
{ 
        if(document.paidform.FNAME.value == "")
	{
			alert("Please specify your First name.");
        		document.paidform.FNAME.focus();
			return false;
	}
	if(document.paidform.LNAME.value == "")
	{
			alert("Please specify your Last name.");
        		document.paidform.LNAME.focus();
			return false;
	}
	if(document.paidform.COMPANY.value == "")
	{
			alert("Please specify your Company.");
        		document.paidform.COMPANY.focus();
			return false;
	}
	
	if(document.paidform.ADDRESS2.value == "")
	{
			alert("Please specify your Street Address.");
	        	document.paidform.ADDRESS2.focus();
			return false;
	}
	if(document.paidform.CITY.value == "")
	{
			alert("Please specify your City.");
	        	document.paidform.CITY.focus();
			return false;
	}
	
	if (document.paidform.STATE.selectedIndex == 0)
		{
			alert("Please specify your State/Province or select 'I am not in the U.S. or Canada'.");
		        document.paidform.STATE.focus();
			return false;
		}
	// validate Canadian state/postal code combination
		if (!ValidCanPostal(document.paidform.STATE[document.paidform.STATE.selectedIndex].value,document.paidform.ZIPCODE.value))
		{
			alert ("Invalid Canadian province/postal code combination.");
        		document.paidform.STATE.focus();
			return false;
		}
                if (document.paidform.STATE[document.paidform.STATE.selectedIndex].value == "53")
		{
			if (document.paidform.COUNTRY.value == "")
			{
				alert("Please specify your Country.");
	        		document.paidform.COUNTRY.focus();
				return false;
			}
		}
		else
		{
		if (document.paidform.ZIPCODE.value.length < 5)
		{
			alert("Please specify your Zip/Postal Code.");
	        	document.paidform.ZIPCODE.focus();
			return false;
		}
	}
        if ((document.paidform.EMAIL_ADDRESS.value != "") && (!validEmail(document.paidform.EMAIL_ADDRESS.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.paidform.EMAIL_ADDRESS.focus();
		return false;
	}
       return true;
}
 
 

// declare global variables
var i;

// two arrays for Canadian province/postal code validation
var CanCodes=new Array("54","55","56","57","58","59","60","61","62","63","64","65");
var CanLetters=new Array("T","UV","R","E","A","B","X","KLMNP","C","GHJ","S","Y");

// check if first letter of postal code matches selected Canadian province
// StateCD is 2-digit code that Omeda uses to identify Canadian provinces (value of STATE field)
// PostCD is entered postal code (value of ZIPCODE field)
function ValidCanPostal(StateCD, PostCD)
{
	var fletter=PostCD.substring(0,1);
	fletter = fletter.toUpperCase();

	for(i=0;i<13;i++)
	{
		if (StateCD==CanCodes[i])
		{
			if (CanLetters[i].indexOf(fletter,0) == -1)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	return true;
}

function checkEmailValue(emailValue)
{     //runs the validate script and returns error box or nonerror

	if (emailValue == "")
	{
		alert("You have not entered email address. Please re-enter it.");
		return false;
	}

	startPos = 0;

	commaPos = emailValue.indexOf(",",startPos+1);	//position of semicolomn
	while (commaPos != -1)
	{
		endPos = commaPos;
		emailElement = emailValue.substring(startPos,endPos);
		if(!validEmail(emailElement))
		{
			alert("You have entered an invalid email address. Please re-enter it.");
			document.qualform.EMAIL_ADDRESS.focus();
			return false;
		}
		startPos = endPos + 1;		
		commaPos = emailValue.indexOf(",",startPos);	//position of semicolomn
	}
	endPos = emailValue.length;
	emailElement = emailValue.substring(startPos,endPos);

	if(!validEmail(emailElement))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.qualform.EMAIL_ADDRESS.focus();
		return false;
	}
	return true;		
}				

function validEmail(email)
{
	invalidchars = "/:;"

	if(email == "")
	{			//checks to see if blank field
		return false
	}
			
	for(i=0;i<invalidchars.length;i++){ //checks for invalid chars
			
	badchars = invalidchars.charAt(i)
			
	if(email.indexOf(badchars,0)!= -1)
	{
		return false
	}	
													}
	
	atPos = email.indexOf("@",1)  //holds position of "@"
			
	if(atPos == -1) 
	{	//checks to see if "@" present
		return false
	}
							
	if(email.indexOf("@",atPos+1) != -1) 
	{ //checks for second "@"
		return false
	}
							
	periodPos = email.indexOf(".",atPos) //holds position of "."
			
	if(periodPos == -1)
	{ //checks for presence of "."
		return false
	}
	if(periodPos+3 > email.length)
	{//makes sure at least two chars after the period
		return false
	}
	return true 
}

function AnswerCheck(form)
{ 
	     if(form.EMAIL_ADDRESS.value == "")
		{
			alert("Please specify your E-mail Address.");
        		form.EMAIL_ADDRESS.focus();
			return false;
                } 
	  
       	if (!validEmail(form.EMAIL_ADDRESS.value))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		form.EMAIL_ADDRESS.focus();
		return false;
	}
	return true;
	
} 
function checkEmailValuePA(emailValue)
{   //runs the validate script and returns error box or nonerror
	
	if (emailValue == "")
	{
		//alert("You have not entered email address. Please re-enter it.");
		return true;
	}

	startPos = 0;

	commaPos = emailValue.indexOf(",",startPos+1);	//position of semicolomn
	while (commaPos != -1)
	{
		endPos = commaPos;
		emailElement = emailValue.substring(startPos,endPos);
		if(!validEmail(emailElement))
		{
			alert("You have entered an invalid colleague's email address. Please re-enter it.");
			return false;
		}
		startPos = endPos + 1;		
		commaPos = emailValue.indexOf(",",startPos);	//position of semicolomn
	}
	endPos = emailValue.length;
	emailElement = emailValue.substring(startPos,endPos);

	if(!validEmail(emailElement))
	{
		alert("You have entered an invalid colleague's email address. Please re-enter it.");
		return false;
	}
	return true;		
}

function AlertRankOther(textBox)
{
    //Error when Federal Government is selected for Q1 and other selected with "other" field empty for Q3
    if (CheckedRadioValue(document.forms[0].EMPLOYER) == "01")
    {
        alert("If your pay schedule or rank is not included in Question 3, please indicate what your GS equivalency may be.");
	    switch (textBox)
        {
            case 1:
	           document.forms[0].GRADE_RANK_DESC1.focus();
            break;
            case 2:
	           document.forms[0].GRADE_RANK_DESC2.focus();
            break;
            case 3:
	           document.forms[0].GRADE_RANK_DESC3.focus();
            break;
            case 4:
	           document.forms[0].GRADE_RANK_DESC4.focus();
            break;
        }
    }   
}	

function CheckedRadioValue(radioItem)
{
	for(i=0;i<radioItem.length;i++)
	{
		if(radioItem[i].checked)
		{
			return radioItem[i].value;
		}//end if
	}//end for
	return false;
}
function disable_enable()
{
	if ((CheckedRadioValue(document.forms[0].EMPLOYER)=="02")||(CheckedRadioValue(document.forms[0].EMPLOYER)=="09"))
	{ 
	
		//document.forms[0].elements("GRADE_RANK").length
	
		for (i=0; i<document.forms[0].elements("GRADE_RANK").length; i++)
		{
			//alert(document.forms[0].elements("GRADE_RANK")[i].value)
			document.forms[0].elements("GRADE_RANK")[i].disabled = true;
			document.forms[0].elements("GRADE_RANK")[i].checked = false;
			document.getElementById("GRADE_RANK_DESC1").disabled=true;
			document.getElementById("GRADE_RANK_DESC1").value='';
			document.getElementById("GRADE_RANK_DESC2").disabled=true;
			document.getElementById("GRADE_RANK_DESC2").value='';
			//document.getElementById("GRADE_RANK_DESC3").disabled=true;
			//document.getElementById("GRADE_RANK_DESC3").value='';
			//document.getElementById("GRADE_RANK_DESC4").disabled=true;
			//document.getElementById("GRADE_RANK_DESC4").value='';
		}
	
		return true;
	}
	else
	{
		for (i=0; i<document.forms[0].elements("GRADE_RANK").length; i++)
		{
			//alert(document.forms[0].elements("GRADE_RANK")[i].value)
			document.forms[0].elements("GRADE_RANK")[i].disabled = false;
			document.getElementById("GRADE_RANK_DESC1").disabled=false;
			document.getElementById("GRADE_RANK_DESC2").disabled=false;
			//document.getElementById("GRADE_RANK_DESC3").disabled=false;
			//document.getElementById("GRADE_RANK_DESC4").disabled=false;

		}		return true;
	}
}