function doReset()
{
	document.forms[0].reset();
}
function doSubmit()
{
	SubmitFlag = true;
	return;
}



function PaidformCheck(mode)
{ 
	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 (!CheckBusAddress())
		{
			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 (document.paidform.EMAIL_ADDRESS.value == "") 
	{
		alert("Please specify your email address.");
		document.paidform.EMAIL_ADDRESS.focus();
		return false;
	}
	
	if (!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 (!CheckPaymentInfo())
	{
		//alert(CheckPaymentInfo())
		return false;
	}
	
	
	return true;
}
function CheckBusAddress()
{
	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;
		}
	}
	return true;
}
			
function CheckPaymentInfo()
{
	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;
	}

	// 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 credid 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 credid card number. Please re-enter.")
        		document.paidform.CARD_NUMBER.focus();
			return false;
		}
	}
	
	// Discover
	if(document.paidform.CARD_TYPE.selectedIndex == 4)
	{
		if (!isDiscover(stripWhitespace(document.paidform.CARD_NUMBER.value)))
		{
			alert("Invalid credid 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;
	}
var d, month, year;
   	d = new Date();
    
	month = d.getMonth() + 1;
	year = d.getFullYear() - 2000;
	if (document.forms[0].ExpYear[document.forms[0].ExpYear.selectedIndex].value < year)
	{
 		alert('Invalid Expiration date.');
		document.forms[0].ExpYear.focus();
 		return false;
	}
	else
	{
		if (document.forms[0].ExpYear[document.forms[0].ExpYear.selectedIndex].value == year)
		{
			if (document.forms[0].ExpMon[document.forms[0].ExpMon.selectedIndex].value < month)
			{
 				alert('Invalid Expiration date.');
				document.forms[0].ExpMon.focus();
 				return false;
			}
		}
	}
	if(document.paidform.SCODE.value == "")
	{
		alert('Please enter security code.');
		document.paidform.SCODE.focus();
		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;
}
 
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 IsCanadian(StateCD)
{
	for(i=0;i<13;i++)
	{
		if (StateCD==CanCodes[i])
		{
			return true;
		}
	}
	return false;
}

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 CheckedRadioValue(radioItem)
{
	for(i=0;i<radioItem.length;i++)
	{
		if(radioItem[i].checked)
		{
			return radioItem[i].value;
		}//end if
	}//end for
	return false;
}
function ObjectExists(form,fieldname)
{
	for ( i=0; i < form.elements.length; i++)
	{
		if (form.elements[i].name == fieldname)
		{
			return true;
		}
	}
	return false;
}

function checkEmailValuePA()
{   //runs the validate script and returns error box or nonerror
	
	if ((document.forms[1].send_to_1.value != "") && (!validEmail(document.forms[1].send_to_1.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].send_to_1.focus();
		return false;
	}
	if ((document.forms[1].send_to_2.value != "") && (!validEmail(document.forms[1].send_to_2.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].send_to_2.focus();		
		return false;
	}
	if ((document.forms[1].send_to_3.value != "") && (!validEmail(document.forms[1].send_to_3.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].send_to_3.focus();		
		return false;
	}
	if ((document.forms[1].send_to_4.value != "") && (!validEmail(document.forms[1].send_to_4.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].send_to_4.focus();		
		return false;
	}
	if ((document.forms[1].send_to_5.value != "") && (!validEmail(document.forms[1].send_to_5.value)))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].send_to_5.focus();		
		return false;
	}
	
	
	
	return true;		
}

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 ValidNum(NumObj)
{
	var myNum, cleanNum;
	
	myNum = NumObj.value
	cleanNum = parseInt(myNum);

	if ( myNum == "")
	{
		return true;
	}
       	if(myNum != cleanNum)
	
	{
		return false;
	}
	return true;
}  

function formCheck(mode, description)
{ 

  	if (document.forms[0].formname.value == "QUALADDRESS")
  	{

	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].EMAIL_ADDRESS.value == "")
	{

		alert("Please specify your E-mail 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;
	}
	
		if (document.forms[0].EMAIL_ADDRESS.value != "") 
		{
			if (document.forms[0].EMAIL_ADDRESS2.value == "") 
			{
				alert("Please confirm your email address.");
				document.forms[0].EMAIL_ADDRESS2.focus();
				return false;
			}   
			if(document.forms[0].EMAIL_ADDRESS.value.toUpperCase() != document.qualform.EMAIL_ADDRESS2.value.toUpperCase())
			{
				alert("Confirmation email address does not match email address.");
		        	document.forms[0].EMAIL_ADDRESS.focus();
				return false;
			}
	 	}	

		if (document.forms[0].PERSONAL_ID.selectedIndex == 0)
		{
			alert ("Please select the day of month born.")
			document.forms[0].PERSONAL_ID.focus();
			return false;
		}
		
	
		if(document.forms[0].COMPANY.value == "")
		{
			alert("Please specify your Company.");
	        	document.forms[0].COMPANY.focus();
			return false;
		}
	

	if (document.forms[0].JOB_TITLE.selectedIndex == 0) 
	{ 
		alert("Please choose your Job title.");
		myFocus=document.getElementById("JOB_TITLE");
		myFocus.scrollIntoView(true); document.forms[0].JOB_TITLE[0].focus();
		return false;
	}

	if (document.forms[0].JOB_TITLE[document.forms[0].JOB_TITLE.selectedIndex].value == '98')
		{
		if (document.forms[0].JOB_TITLE_TXT.value == '') 
		{ 
			alert("Please specify your Job title - other.");
			myFocus=document.getElementById("JOB_TITLE");
			myFocus.scrollIntoView(true); 
			document.forms[0].JOB_TITLE_TXT.focus();
			return false;
		}
	
	}

//	if (mode == "ADD")
//	{
		if(document.forms[0].ADVERTISER[0].checked == false && document.forms[0].ADVERTISER[1].checked == false)
		{
			alert("Please specify whether you are a current or potential advertiser.");
        	document.forms[0].ADVERTISER[0].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 (ObjectExists(document.forms[0], "PHONE"))
		{
            if(document.forms[0].PHONE.value == "")
			{
				alert("Please specify your Phone Number.");
	        	document.forms[0].PHONE.focus();
				return false;
			}
         	if(!validPhone(document.forms[0].PHONE.value))
			{
				alert ("Only numeric values are allowed in the phone field, please re-enter.");
				document.forms[0].PHONE.focus();
				return false;
			} 
		}
//	}

	return true;
  }


  if (document.forms[0].formname.value == "QUALDEMO1")
  {

	if (document.forms[0].OCCUPATION.selectedIndex == 0) 
	{ 
		alert("Please select your Business.");
		myFocus=document.getElementById("OCCUPATION");
		myFocus.scrollIntoView(true); 
		document.forms[0].OCCUPATION[0].focus();
		return false;
	}
	
	if (document.forms[0].OCCUPATION.value == '98')

		{
		if (document.forms[0].OCCUPATION_DESC.value == '') 
		{ 
			alert("Please specify your Business - other.");
			myFocus=document.getElementById("OCCUPATION");
			myFocus.scrollIntoView(true); 
			document.forms[0].OCCUPATION_DESC.focus();
			return false;
		}
	
	}
	
	if (CheckedRadioValue(document.forms[0].CONTRACT_GROWER)=='1')
	{
		if (document.forms[0].CONTRACT_GROW_COMPANY.value == '') 
		{ 
			alert("Please specify who do you contract grow for.");
//			myFocus=document.getElementById("OCCUPATION");
//			myFocus.scrollIntoView(true); 
			document.forms[0].CONTRACT_GROW_COMPANY.focus();
			return false;
		}		
	}
		if(!ValidNum(document.forms[0].HOGS_SOLD))	
		{
			alert("Please enter numbers only.");
			document.forms[0].HOGS_SOLD.focus();
			return false;
		}


		if(!ValidNum(document.forms[0].YOUNG_SOLD))	
		{
			alert("Please enter numbers only.");
			document.forms[0].YOUNG_SOLD.focus();
			return false;
		
		}	
		if(!ValidNum(document.forms[0].BREEDING))	
		{
			alert("Please enter numbers only.");
			document.forms[0].BREEDING.focus();
			return false;
		
		}			
		

		if(!ValidNum(document.forms[0].SOWS))	
		{
			alert("Please enter numbers only.");
			document.forms[0].SOWS.focus();
			return false;
		
		}			
	
		if(!ValidNum(document.forms[0].EMPLOYEES))	
		{
			alert("Please enter numbers only.");
			document.forms[0].EMPLOYEES.focus();
			return false;
		
		}

	
		if(!ValidNum(document.forms[0].CORN_GRN_ACRES))	
		{
			alert("Please enter numbers only.");
			document.forms[0].CORN_GRN_ACRES.focus();
			return false;
		
		}


		if(!ValidNum(document.forms[0].CORN_SIL_ACRES))	
		{
			alert("Please enter numbers only.");
			document.forms[0].CORN_SIL_ACRES.focus();
			return false;
		
		}		
	
		if(!ValidNum(document.forms[0].SOYBEAN_ACRES))	
		{
			alert("Please enter numbers only.");
			document.forms[0].SOYBEAN_ACRES.focus();
			return false;
		
		}		
	
		if(!ValidNum(document.forms[0].TOTAL_ACRES))	
		{
			alert("Please enter numbers only.");
			document.forms[0].TOTAL_ACRES.focus();
			return false;
		
		}				
	
	return true;	
	
  } // end of the demo page	
		
	// page 3
  if (document.forms[0].formname.value == "QUALDEMO2")
  {
       			
	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[0].checked == true) && (document.forms[0].REQUESTED_VERSION[0].checked == false)&&(document.forms[0].REQUESTED_VERSION[1].checked == false))

	{
		alert("Please specify how you would like to receive your copy of Pork");
        document.forms[0].RECEIVE_YES_NO[0].focus();
		return false;
	}
//alert('hi: '+(document.forms[0].AGP_WEEKLY.checked))
if((document.forms[0].RECEIVE_YES_NO[1].checked == true)  &&
(document.forms[0].PORK_ISSUE_ANNOUN.checked== false) &&
 (document.forms[0].PORK_ALERTS_BREAK.checked== false) && (document.forms[0].PORK_ALERTS_DAILY.checked== false) && 
(document.forms[0].PORK_WIR.checked== false)) 

	{
		alert("Please choose a magazine and/or a newsletter.");
        document.forms[0].RECEIVE_YES_NO[0].focus();
		return false;
	
	}
	
//	if (document.forms[0].PAFLAG.value != '1')
//	{

		if(!ValidNum(document.forms[0].PEOPLE_READ_COPY))	
		{
			alert("Please enter numbers only.");
			document.forms[0].PEOPLE_READ_COPY.focus();
			return false;
		
		}		
	//}
	
	
	return true;		
		
  }		
	
 }
 
 function validPhone(phone)
{
	return phone.match(/^(\d|[-() .+])+$/);
} 
