var count;
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 checkYear(year)
{
	if (year == "")
	{
		alert("You have not entered your year of birth. Please re-enter it.");
	        document.qualform.BIRTH_YEAR.focus();
		return false;
	}

	if ((year < '1800') || (year > '2001'))
	{
		alert("You have entered invalid year of birth. Please re-enter it.");
	        document.qualform.BIRTH_YEAR.focus();
		return false;
	}
	return true;
}

function formCheck(mode, description)
{ 	var myFocus;

	if (mode=="LOGIN")
	{
		var myElem = document.getElementById('have_rdr1');
		if (myElem.checked)
		{
			if (document.login.RDRNUM.value=="")
			{
				alert ('Please enter your Account Number, or choose "New Subscription" if you do not have an Account Number.');
				return false;
			}
		}
		return true;
	}
	
	if (document.qualform.RECEIVE_YES_NO[1].checked == true)
	// stop validation when "not to receive" response
	{
		return true;
	}

	if (mode == "ADD")
	{
		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.");
		        document.qualform.STATE.focus();
			return false;
		}

		if (document.qualform.ZIPCODE.value.length < 5)
		{
			alert ("Please specify your Zip Code.");
	        	document.qualform.ZIPCODE.focus();
			return false;
		}
		
	}
        if (document.qualform.EMAIL_ADDRESS.value == "")
	{
		alert ("Please specify your E-mail Address.");
       		document.qualform.EMAIL_ADDRESS.focus();
		return false;
    	}

	if ((document.qualform.EMAIL_ADDRESS.value != "") && (!validEmail(document.qualform.EMAIL_ADDRESS.value)))
	{
		alert ("You have entered an invalid email address. Please re-enter it.");
		document.qualform.EMAIL_ADDRESS.focus();
		return false;
	}
	
	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.BIRTH_YEAR.value == "")
	{
		alert ("You have not entered your year of birth. Please re-enter it.");
	        document.qualform.BIRTH_YEAR.focus();
		return false;
	}

	if ((document.qualform.BIRTH_YEAR.value < '1800') || (document.qualform.BIRTH_YEAR.value > '2001'))
	{
		alert ("You have entered invalid year of birth. Please re-enter it.");
	        document.qualform.BIRTH_YEAR.focus();
		return false;
	}
     	count=0;
     	checker(0,ENEWS_,"ENEWS_");
     	if (count > 4)
	{
		alert ("You can only select 4 of the newsletters.");
		return false;
	}
	
	// check the SHORT template due to diff. design
	if (ObjectExists(document.qualform, "template"))
	{
		//gender
		if (!checkUnselects(document.qualform.GENDER))
		{
			alert ("Please specify your gender.");
			document.qualform.GENDER[0].focus();
			return false;
		}
		//conditions
		if (!CheckboxChecked(document.qualform,'HAVE_CONDITION'))
		{ 	
			alert("Please specify your conditions.");
			document.qualform.GENDER[0].focus();
			return false;
		}
		//personal ID
		 if (document.qualform.PERSONAL_ID.value == "")
		{
			alert("Please specify the Personal ID.");
	        document.qualform.PERSONAL_ID.focus();
			return false;
		}
	}// end of short template demo validation
	
 }  

function SendFormCheck()
{ 
	if (!checkEmail1(document.SendEmail.send_to.value))
	{
		return false;
	}

	if (!checkEmail1(document.SendEmail.sent_from.value))
	{
		return false;
	}
	return true;
}  

function CheckMiralax()
{
	if (!checkUnselects(document.forms[0].yesno))
	{
		alert ("Please answer to the question 1: Have you ever heard of MiraLax?");
		document.forms[0].yesno[0].focus();
		return false;
	}

	if (document.forms[0].yesno[0].checked)
	{
		if ((document.forms[0].text1.value=="")||(document.forms[0].text1.value=="(please fill in the blank)"))
		{
			alert ("Please answer to the question 2: MiraLax™ is a..");
			document.forms[0].text1.focus();
			return false;
		}
	}
	return true;
}

function EmailCheck()
	{
		if (document.qualform.EMAIL_ADDRESS.value == "")
		{
			alert( "Please specify your Email Address.");
        		document.qualform.EMAIL_ADDRESS.focus();
			return false;
		}
	}

function ClearMrlxDesc()
{
	if (document.forms[0].text1.value=="(please fill in the blank)")
	{
		document.forms[0].text1.value="";
	}
}

function checkUnselects(radioItem)
{
	for(i=0;i<radioItem.length;i++)
	{
		if(radioItem[i].checked)
		{
			return true;
		}//end if
	}//end for
	return false;
}

// declare global variables
var i;
var ENEWS_;

function InitIndex(formindex)
{
	
	for ( i=0; i < document.forms[formindex].elements.length; i++)
	{
		GetIndex(formindex, "ENEWS_");
		ENEWS_ = i;
		i++;
				
		break;
	}
}

function GetIndex(formindex, GroupName)
{
	var namelen = GroupName.length;

	while (document.forms[formindex].elements[i].name.substring(0,namelen) != GroupName)
	{
		i++;
	}
}


// Start index must be calculated when document is being loaded using InitIndex() function 

function checker(formindex,start,groupname)
			// formindex - index of the form within current document, starts with 0
			// start - start index for group of checkboxes
			// generic name of group of checkboxes (Example: "QUES_5")
			
{
	var namelen = groupname.length;
	
	for (var j=start; j < document.forms[formindex].elements.length; j++)
	{
		if (document.forms[formindex].elements[j].name.substring(0,namelen) != groupname)
		{

			break;
		}
		else
		{
 			if (document.forms[formindex].elements[j].checked==true)
 			{
				count=count+1;
			}
		}
	}
}

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 CheckDropdown(selectBox)
{
	if(selectBox.selectedIndex == "0")
	{
		return false;
	}
	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;
}
	