var isNav, isIE
var range	= ""
var style 	= ""
var outerDiv 	= ""

if (navigator.appVersion.indexOf("MSIE") != -1) 
{
	isIE		= true
	var range 	= "all."
	var style 	= ".style"
} 
else 
{
	isNav 		= true
	outerDiv	= ""
}


function makeObj(obj) 
{
	var theObj
	
	if (typeof obj == "string") 
	{
		theObj = eval(outerDiv + "document." + range + obj + style)
	} 
	else 
	{
		theObj = obj
	}
	return theObj
}

function doVisible(obj, visible) 
{
	var theObj = makeObj(obj)
	
	if (visible) 
	{
		theObj.visibility  = "visible"
	} 
	else 
	{
		theObj.visibility = "hidden"
	}
}

function doImage(imgName, newImg, divName) 
{
	if (isIE) 
	{
		var theObj = eval("document.all." + imgName)
	} 
	else 
	{
		var theObj = eval("document." + divName + ".document." + imgName)
	}
	theObj.src = newImg
}
function LoginFormCheck()
{ 
    //change to this line if newsletter added
	//if (document.forms[0].HAVE_RDRNUM[0].checked == false && document.forms[0].HAVE_RDRNUM[1].checked == false && document.forms[0].HAVE_RDRNUM[2].checked == false)

    if (document.forms[0].HAVE_RDRNUM[0].checked == false && document.forms[0].HAVE_RDRNUM[1].checked == false)
	{
		alert ("Please select one of the options.");
        	document.forms[0].HAVE_RDRNUM[0].focus();
		return false;
	}
	if (document.forms[0].HAVE_RDRNUM[0].checked == true)
	{
		if  (document.forms[0].RDRNUM.value == "")
		{
			alert ("Please enter your account number.");
      				document.forms[0].RDRNUM.focus();
			return false;
		}
		if ( document.forms[0].LNAME_FIRST4.value == "")
		{
			alert ("Please enter first 4 letters of your last name.");
	       		document.forms[0].LNAME_FIRST4.focus();
			return false;
		}
	}
//  There is currently no AQI newsletter
//	if (document.forms[0].HAVE_RDRNUM[1].checked == true)
//	{
//		if  (document.forms[0].EMAIL_ADDRESS.value == "")
//		{
//			alert ("Please enter your email address.");
 //     				document.forms[0].EMAIL_ADDRESS.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;
//		}
//	}
	
	return true;
}

function checkEmail(emailValue)
{	//runs the validate script and returns error box or nonerror
	//allows blank email address

	if (emailValue == "")
	{
		return true;
	}

	if(!validEmail(emailValue))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		return false;
	}
	return true;		
}				

function checkEmail1(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 = " /:;"

// remove leading spaces
	while(email.substring(0,1)==" ")
	{
		email = email.substring(1,email.length);
	}

// remove trailing spaces
	while(email.substring(email.length-1,email.length)==" ")
	{

		email = email.substring(0,email.length-1);
	}

	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 doReset()
{
	document.qualform.reset();
}

function formCheck(mode, description)
{ 
	if (document.qualform.RECEIVE_YES_NO[0].checked == false && document.qualform.RECEIVE_YES_NO[1].checked == false)
	{
		alert ("Please specify whether you wish to receive " + description +"?");
        	document.qualform.RECEIVE_YES_NO[0].focus();
		return false;
	}
	if (document.qualform.RECEIVE_YES_NO[1].checked == true)
	// stop validation when "not to receive" response
	{
		return true;
	}
	
       	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;
		}	

	if (mode == "ADD")
	{
                if(document.forms[0].EMAIL_ADDRESS.value == "")
		{
			alert("Please specify your E-mail Address.");
       			document.forms[0].EMAIL_ADDRESS.focus();
			return false;
    		}

		if(!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;
		}
		if (document.qualform.FNAME.value == "")
		{
			alert("Please specify your First name.");
        		document.qualform.FNAME.focus();
			return false;
		}
		if (document.qualform.LNAME.value == "")
		{
			alert("Please specify your Last name.");
        		document.qualform.LNAME.focus();
			return false;
		}
	}
	if (mode != "RENEW")
	{
		if (document.qualform.ADDRESS2.value == "")
		{
			alert("Please specify your Street Address.");
	        	document.qualform.ADDRESS2.focus();
			return false;
		}
		if (document.qualform.CITY.value == "")
		{
			alert("Please specify your City.");
	        	document.qualform.CITY.focus();
			return false;
		}
		if (document.qualform.STATE.selectedIndex == 0)
		{
			alert("Please specify your State/Province or select 'I am not in the U.S. or Canada'.");
		        document.qualform.STATE.focus();
			return false;
		}

		if (document.qualform.COUNTRY.selectedIndex != 2)
		{
			if (document.qualform.ZIPCODE.value.length < 5)
			{
				alert("Please specify your Zip/Postal Code.");
	        		document.qualform.ZIPCODE.focus();
				return false;
			}
		}
		
	}
	if (!CheckDemos(document.forms[0]))
	{
		return false;
	}
 }  


function PaidformCheck(mode, description)
{ 
	if(!CheckUnselects(document.paidform.SUBSCRIPTION))	
	{
		alert("Please select your subscription.");
        	document.paidform.SUBSCRIPTION[0].focus();
		return false;
	}


	if (mode == "ADD")
	{
		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 (mode != "RENEW")
	{
		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.forms[0].STATE.selectedIndex].value,document.paidform.ZIPCODE.value))
		{
			alert ("Invalid Canadian province/postal code combination.");
        		document.paidform.STATE.focus();
			return false;
		}


		if (document.paidform.STATE.selectedIndex == 1)
		{

			if((document.paidform.COUNTRY.selectedIndex == 0)||(document.paidform.COUNTRY.selectedIndex == 1))
			{
				alert("Please specify your Country.");
	        		document.paidform.COUNTRY.focus();
				return false;
			}

			if(document.paidform.COUNTRY.selectedIndex == 3)
			{
				if(document.paidform.COUNTRY_DESC.value == "")
				{
					alert("Please specify your Country.");
	        			document.paidform.COUNTRY_DESC.focus();
					return false;
				}

			}
		}
		else
		{
			if (document.paidform.ZIPCODE.value.length < 5)
			{
				alert("Please specify your Zip/Postal Code.");
	        		document.paidform.ZIPCODE.focus();
				return false;
			}
		}
// Foreign ONLY apply with 1 year subscription
	if((document.paidform.COUNTRY.selectedIndex == 3) && (document.paidform.SUBSCRIPTION[0].checked == false))
    {
		alert("Foreign subscription is only avaliable for one year.");
       		document.paidform.SUBSCRIPTION[0].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;
	      }
	}

	if(!CheckDropdown(document.paidform.CARD_TYPE))
	{
		alert("Please specify credit card.");
        	document.paidform.CARD_TYPE.focus();
		return false;
	}

	if(document.paidform.CARD_NUMBER.value == "")
	{
		alert("Please specify credit card number.");
        	document.paidform.CARD_NUMBER.focus();
		return false;
	}

	if(CheckDropdown(document.paidform.CARD_TYPE))
	{
		// Visa
		if(document.paidform.CARD_TYPE.selectedIndex==1)
		{
			if (!isVisa(stripWhitespace(document.paidform.CARD_NUMBER.value)))
			{
				alert("Invalid credit card number. Please re-enter.")
        			document.paidform.CARD_NUMBER.focus();
				return false;
			}
		}

		// MasterCard
		if(document.paidform.CARD_TYPE.selectedIndex==2)
		{
			if (!isMasterCard(stripWhitespace(document.paidform.CARD_NUMBER.value)))
			{
				alert("Invalid credit card number. Please re-enter.")
        			document.paidform.CARD_NUMBER.focus();
				return false;
			}
		}

		// AmEx
		if(document.paidform.CARD_TYPE.selectedIndex==3)
		{
			if (!isAmericanExpress(stripWhitespace(document.paidform.CARD_NUMBER.value)))
			{
				alert("Invalid credit card number. Please re-enter.")
        			document.paidform.CARD_NUMBER.focus();
				return false;
			}
		}
	}
	
	if(!CheckDropdown(document.paidform.ExpMon))
	{
 		alert('Please select the expiration month.');
		document.paidform.ExpMon.focus();
 		return false;
	}

	if(!CheckDropdown(document.paidform.ExpYear))
	{
 		alert('Please select the expiration year.');
		document.paidform.ExpYear.focus();
 		return false;
	}
//	if(document.paidform.SCODE.value == "")
//	{
//		alert('Please enter security code.');
//		document.paidform.SCODE.focus();
//		return false;
//	}
 }  

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 CheckboxChecked(thisform,CheckBoxName)
{
var ChLength = CheckBoxName.length + 1;
var vName = CheckBoxName + "-";

var CBindex = 0;

	for ( i=0; i < thisform.elements.length; i++)
	{
		if (thisform.elements[i].type == "checkbox")
		{
			if (thisform.elements[i].name.substring(0,ChLength)==vName)
			{
				if (CBindex==0) {CBindex = i;}
				if (thisform.elements[i].checked)
				{
					return true;
					break;
				}
			}
		}

	}

	thisform.elements[CBindex].focus();
	return false;
}
function CheckedRadioValue(radioItem)
{
	for(i=0;i<radioItem.length;i++)
	{
		if(radioItem[i].checked)
		{
			return radioItem[i].value;
		}//end if
	}//end for
	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","66","61","62","63","64","65");
var CanLetters=new Array("T","UV","R","E","A","B","X","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 CopyAddr(form, Idx)
{
	var pslStart;
	var firstPslName = "CopyAddr" + Idx;

	// call ObjectExists to obtain index of the passalong section, it's returned in variable 'i'
	if (ObjectExists(form, firstPslName))
	{
		pslStart = i;

		if (form.elements[pslStart].checked)
		{ 
			if (form.elements[pslStart+4].value == "") {			
				form.elements[pslStart+4].value = form.COMPANY.value;}
			if (form.elements[pslStart+5].value == "") {
				form.elements[pslStart+5].value = form.ADDRESS2.value;}
			if (form.elements[pslStart+6].value == "") {
				form.elements[pslStart+6].value = form.ADDRESS1.value;}
			if (form.elements[pslStart+7].value == "") {
				form.elements[pslStart+7].value = form.CITY.value;}
			if (form.elements[pslStart+8].selectedIndex == 0) {
				form.elements[pslStart+8].selectedIndex = form.STATE.selectedIndex;}
			if (form.elements[pslStart+9].value == "") {
				form.elements[pslStart+9].value = form.ZIPCODE.value;}
			if (form.elements[pslStart+13].value == "") {
				form.elements[pslStart+13].value = form.FaxArea.value;}
			if (form.elements[pslStart+14].value == "") {
				form.elements[pslStart+14].value = form.FaxExch.value;}
			if (form.elements[pslStart+15].value == "") {
				form.elements[pslStart+15].value = form.Fax.value;}
		}
	}
}

function ClearAddrFlag(form,Idx)
{
	var pslStart;
	var firstPslName = "CopyAddr" + Idx;

	// call ObjectExists to obtain index of the passalong section, it's returned in variable 'i'
	if (ObjectExists(form, firstPslName))
	{
		pslStart = i;
		form.elements[pslStart].checked = false;
	}
}

function ValidatePassalong(form, PScnt)
{
	var pslStart;
	var PSLfocus;
	Idx = '0' + String(PScnt);
	if (Idx.length > 2) {Idx = Idx.slice(-1);}

	var firstPslName = "CopyAddr" + Idx;

	// call ObjectExists to obtain index of the passalong section, it's returned in variable 'i'
	if (ObjectExists(form, firstPslName))
	{
		pslStart = i;

		if (form.elements[pslStart+1].value+form.elements[pslStart+2].value+form.elements[pslStart+3].value+
			form.elements[pslStart+4].value+form.elements[pslStart+5].value+form.elements[pslStart+6].value+
			form.elements[pslStart+7].value+form.elements[pslStart+9].value+form.elements[pslStart+16].value == "")
		{
			return true;
		}


		PSLfocus = document.getElementById("PSL"+Idx);
		if (form.elements[pslStart+1].value == "")
		{
			alert ("Please specify your colleague's first name.");
			PSLfocus.scrollIntoView(true);
        		form.elements[pslStart+1].focus();
			return false;
		}
		if (form.elements[pslStart+2].value == "")
		{
			alert ("Please specify your colleague's last name.");
			PSLfocus.scrollIntoView(true);
        		form.elements[pslStart+2].focus();
			return false;
		}
		if (form.elements[pslStart+5].value == "")
		{
			alert ("Please specify your colleague's street address.");
			PSLfocus.scrollIntoView(true);
	        	form.elements[pslStart+5].focus();
			return false;
		}
		if (form.elements[pslStart+7].value == "")
		{
			alert ("Please specify your colleague's city.");
			PSLfocus.scrollIntoView(true);
	        	form.elements[pslStart+7].focus();
			return false;
		}
		if (form.elements[pslStart+8].selectedIndex == 0)
		{
			alert ("Please specify your colleague's state.");
			PSLfocus.scrollIntoView(true);
	        	form.elements[pslStart+8].focus();
			return false;
		}
		if (form.elements[pslStart+9].value == "")
		{
			alert ("Please specify your colleague's zip code.");
			PSLfocus.scrollIntoView(true);
	        	form.elements[pslStart+9].focus();
			return false;
		}
		if ((form.elements[pslStart+16].value != "") && (!validEmail(form.elements[pslStart+16].value)))
		{
			alert ("You have entered an invalid email address. Please re-enter it.");
			PSLfocus.scrollIntoView(true);
	        	form.elements[pslStart+16].focus();
			return false;
		}
		return true;
	}
	else
	{
		return true;
	}
}

function ObjectExists(form,fieldname)
{
	for ( i=0; i < form.elements.length; i++)
	{
		if (form.elements[i].name == fieldname)
		{
			return true;
		}
	}
	return false;
}



