function formCheck(mode, description)
{ 
	if (document.forms['qualform'].RECEIVE_YES_NO[0].checked == false && document.forms['qualform'].RECEIVE_YES_NO[1].checked == false)
	{
		alert("Please specify whether you wish to receive " + description +"?");
        	document.forms['qualform'].RECEIVE_YES_NO[0].focus();
		return false;
	}

	if (document.forms['qualform'].RECEIVE_YES_NO[1].checked == true)
	// stop validation when "not to receive" response
	{
		return true;
	}

	if (mode == "ABBR")
	{
		return true;
	}
	if (mode == "SHORT")
	{
		if (document.forms['qualform'].RECEIVE_YES_NO[0].checked == false && document.forms['qualform'].RECEIVE_YES_NO[1].checked == false)
		{
			alert("Please specify whether you wish to receive " + description +"?");
        		document.forms['qualform'].RECEIVE_YES_NO[0].focus();
			return false;
		}

		if (document.forms['qualform'].RECEIVE_YES_NO[1].checked == true)
		// stop validation on "not to receive" response
		{
			return true;
		}
		if (!CheckUnselects(document.forms['qualform'].REQUESTED_VERSION))	
		{
			alert("Please select the preferred format.");
        		document.forms['qualform'].REQUESTED_VERSION[0].focus();
			return false;
		}
        	if (document.forms['qualform'].EMAIL_ADDRESS.value == "")
		{
			alert("Please specify your Email Address.");
	        	document.forms['qualform'].EMAIL_ADDRESS.focus();
			return false;
		}

		if ((document.forms['qualform'].EMAIL_ADDRESS.value != "") && (!validEmail(document.forms['qualform'].EMAIL_ADDRESS.value)))
		{
			alert("You have entered an invalid email address. Please re-enter it.");
			document.forms['qualform'].EMAIL_ADDRESS.focus();
			return false;
		}
		if (document.forms['qualform'].PERSONAL_ID.selectedIndex == 0)
		{
			alert ("Please specify the color of your eyes.")
			document.forms['qualform'].PERSONAL_ID.focus();
			return false;
		}
       		return true;
	}

	if (document.forms['qualform'].REQUESTED_VERSION[0].checked == false && document.forms['qualform'].REQUESTED_VERSION[1].checked == false)
	{
		alert("Please specify your preferred format.");
        	document.forms['qualform'].REQUESTED_VERSION[0].focus();
		return false;
	}

	if (mode == "ADD")
	{
		if (document.forms['qualform'].FNAME.value == "")
		{
			alert("Please specify your First name.");
        		document.forms['qualform'].FNAME.focus();
			return false;
		}
		if (document.forms['qualform'].LNAME.value == "")
		{
			alert("Please specify your Last name.");
        		document.forms['qualform'].LNAME.focus();
			return false;
		}
	}
	if (mode != "RENEW")
	{
		if (document.forms['qualform'].TITLE.value == "")
		{
			alert("Please specify your Title.");
	        	document.forms['qualform'].TITLE.focus();
			return false;
		}
		if (document.forms['qualform'].COMPANY.value == "")
		{
			alert("Please specify your Company name.");
        		document.forms['qualform'].COMPANY.focus();
			return false;
		}
		if (document.forms['qualform'].ADDRESS2.value == "")
		{
			alert("Please specify your Street Address.");
	        	document.forms['qualform'].ADDRESS2.focus();
			return false;
		}
		if (document.forms['qualform'].CITY.value == "")
		{
			alert("Please specify your City.");
	        	document.forms['qualform'].CITY.focus();
			return false;
		}

		if (document.forms['qualform'].STATE.selectedIndex == 0)
		{
			alert("Please specify your State/Province or select 'I am not in the U.S. or Canada'.");
	        	document.forms['qualform'].STATE.focus();
			return false;
		}

		if (document.forms['qualform'].COUNTRY.selectedIndex < 2)
		{	// N.America
		
			// validate Canadian state/postal code combination
			if (!ValidCanPostal(document.forms['qualform'].STATE[document.forms['qualform'].STATE.selectedIndex].value,document.forms['qualform'].ZIPCODE.value))
			{
				alert ("Invalid Canadian province/postal code combination.");
        			document.forms['qualform'].STATE.focus();
				return false;
			}

			if (document.forms['qualform'].ZIPCODE.value.length < 5)
			{
				alert("Please specify your Zip/Postal Code.");
	        		document.forms['qualform'].ZIPCODE.focus();
				return false;
			}
		}
	}

	if (document.forms['qualform'].EMAIL_ADDRESS.value == "")
	{
		alert("Please specify your E-mail Address.");
	        document.forms['qualform'].EMAIL_ADDRESS.focus();
		return false;
	}


	if ((document.forms['qualform'].EMAIL_ADDRESS.value != "") && (!validEmail(document.forms['qualform'].EMAIL_ADDRESS.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms['qualform'].EMAIL_ADDRESS.focus();
		return false;
	}
	//if (document.forms['qualform'].PERSONAL_ID.selectedIndex == 0)
	//{
	//	alert ("Please specify the color of your eyes.")
	//	document.forms['qualform'].PERSONAL_ID.focus();
	//	return false;
	//}
}  

// 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.");
			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.");
		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 CheckDropdown(selectBox)
{

	if(selectBox.selectedIndex == "0")
	{
		return false;
	}
	else
	{
		return true;
	}
}
function CheckUnselects(radioItem)
{
	for(i=0;i<radioItem.length;i++)
	{
		if(radioItem[i].checked)
		{
			return true;
		}//end if
	}//end for
	return false;
}

function PopulateCountry(form)
{
	if (form.STATE[form.STATE.selectedIndex].value == "53")
	{return}
	if (form.STATE[form.STATE.selectedIndex].value <= "66")
	{
		form.COUNTRY.selectedIndex = 1;
	}
	else
	{
		form.COUNTRY.selectedIndex = 0;
	}

}
function checkEmailValuePA(emailValue,emailName)
{   //runs the validate script and returns error box or nonerror
	if (emailValue == "")
	{
		alert("You have not entered email address. Please re-enter it.");
		document.getElementById(emailName).focus();
		
		return false;
	}

	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 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 email address. Please re-enter it.");
		document.getElementById(emailName).focus();		
		return false;
	}
	return true;		
}
function doHref(myObj)
{

        var myHref = myObj.href;
	if (document.forms['qualform'].TestMode.value == "1")
	{
		if (myHref.indexOf("?",0) == -1) 
		{
			myObj.href = myHref + "?t=1";
		}
		else
		{
			myObj.href = myHref + "&t=1";
		}
	}
}
function ValidNum(NumObj)
{
	var myNum, cleanNum;
	
	myNum = NumObj.value
	cleanNum = parseInt(myNum);

	if ( myNum == "")
	{
		return true;
	}
       	if(myNum != cleanNum)
	
	{
		return false;
	}
	return true;
}  
