function doSubmit()
{
	SubmitFlag = true;
	return;
}
function LoginFormCheck()
{ 

	if (ObjectExists(document.forms[1], "RECEIVE_YES_NO"))
	{
		if(document.forms[1].RECEIVE_YES_NO[0].checked == false && document.forms[1].RECEIVE_YES_NO[1].checked == false)
		{
		alert("Please specify whether you wish to receive energyBiz Magazine?");
	        document.forms[1].RECEIVE_YES_NO[0].focus();
			return false;
		}
	
		if(document.forms[1].RECEIVE_YES_NO[1].checked == true)
		// stop validation when "not to receive" response
		{
			return true;
		}
	}
	if (!CheckUnselects(document.forms[1].REQUESTED_VERSION))
	{
	       alert("Please specify the subscription format.");
		document.forms[1].REQUESTED_VERSION[0].focus();
		return false;
	}
             
	return true;
}
function formCheck(mode, description)
{ 
	if ((mode == "RENEW") || (mode == "COA")) 	// revers
	{
		if(document.forms[1].RECEIVE_YES_NO[0].checked == false && document.forms[1].RECEIVE_YES_NO[1].checked == false)
		{
		alert("Please specify whether you wish to receive energyBiz Magazine?");
	        document.forms[1].RECEIVE_YES_NO[0].focus();
			return false;
		}
	
		if(document.forms[1].RECEIVE_YES_NO[1].checked == true)
		// stop validation when "not to receive" response
		{
			return true;
		}
		if (!CheckUnselects(document.forms[1].REQUESTED_VERSION))
		{
		       alert("Please specify the subscription format.");
			document.forms[1].REQUESTED_VERSION[0].focus();
			return false;
		}

		if((document.forms[1].REQUESTED_VERSION[0].checked == true)|| 		(document.forms[1].REQUESTED_VERSION[2].checked == true))
		// International Print or Both (print & digital)
		{
			if (document.forms[1].STATE[document.forms[1].STATE.selectedIndex].value <= "53")
			{
				return true;
			}
		}
	}
	
	if ((mode == "SHORT") || (mode == "ABBR"))
	{
		if ((document.forms[1].EMAIL_ADDRESS.value != "") && (!validEmail(document.forms[1].EMAIL_ADDRESS.value)))
		{
			alert("You have entered an invalid email address. Please re-enter it.");
			document.forms[1].EMAIL_ADDRESS.focus();
			return false;
		}	
		return true;
	}

	if (mode == "ADD")
	{
		if(document.forms[1].FNAME.value == "")
		{
			alert("Please specify your First name.");
        		document.forms[1].FNAME.focus();
			return false;
		}
		if(document.forms[1].LNAME.value == "")
		{
			alert("Please specify your Last name.");
        		document.forms[1].LNAME.focus();
			return false;
		}
	}
	if (mode != "RENEW")
	{
		if(document.forms[1].TITLE.value == "")
		{
			alert("Please specify your Title.");
	        document.forms[1].TITLE.focus();
			return false;
		}
		if(document.forms[1].COMPANY.value == "")
		{
			alert("Please specify your Company name.");
        		document.forms[1].COMPANY.focus();
			return false;
		}
		if(document.forms[1].ADDRESS2.value == "")
		{
			alert("Please specify your Street Address.");
	        	document.forms[1].ADDRESS2.focus();
			return false;
		}
		if(document.forms[1].CITY.value == "")
		{
			alert("Please specify your City.");
	        	document.forms[1].CITY.focus();
			return false;
		}
		
		if (document.forms[1].STATE.selectedIndex == 0)
		{
			if(document.forms[1].REQUESTED_VERSION.value == "2")
			// international digital
			{
				alert("Please specify your State/Province or select 'I am not in the US or Canada'.");
					document.forms[1].STATE.focus();
				return false;
			}
			else // U.S.
			{
				alert("Please specify your State.");
					document.forms[1].STATE.focus();
				return false;
			}
		}		

		// validate Canadian state/postal code combination
		if (!ValidCanPostal(document.forms[1].STATE[document.forms[1].STATE.selectedIndex].value,document.forms[1].ZIPCODE.value))
		{
			alert ("Invalid Canadian province/postal code combination.");
        		document.forms[1].STATE.focus();
			return false;
		}

		if (document.forms[1].STATE[document.forms[1].STATE.selectedIndex].value == "53")
		{
			if (document.forms[1].COUNTRY.value == "")
			{
				alert("Please specify your Country.");
	        		document.forms[1].COUNTRY.focus();
				return false;
			}
		}
		else
		{
			if (document.forms[1].ZIPCODE.value.length < 5)
			{
				if(document.forms[1].REQUESTED_VERSION.value == "2")
				// Canada digital
				{
					alert("Please specify your Zip/Postal Code.");
		        		document.forms[1].ZIPCODE.focus();
					return false;
				}
				else
				{
					alert("Please specify your Zip Code.");
	        			document.forms[1].ZIPCODE.focus();
					return false;
				}
			}
		}
	}
	
	if (document.forms[1].PHONE.value == "") 
	{
		alert("Please specify your phone number.");
		document.forms[1].PHONE.focus();
		return false;
	}
	
	if (document.forms[1].EMAIL_ADDRESS.value == "") 
	{
		alert("Please specify your email address.");
		document.forms[1].EMAIL_ADDRESS.focus();
		return false;
	}
	
	if (!validEmail(document.forms[1].EMAIL_ADDRESS.value))
	{
		alert("You have entered an invalid email address. Please re-enter it.");
		document.forms[1].EMAIL_ADDRESS.focus();
		return false;
	}
	
	if (!CheckDemos(document.forms[1]))
	{
		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 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 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 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 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;		
}

// below are the functions from the eBiz
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
