// declare global variables
var i;
var IT_DECISION;
var PURCHASE_AUTHORITY;
var SubmitFlag;

// on load calculate indices for all group of checkboxes with 'None of the Above' option 
//function InitIndex(formindex)
//{
//	SubmitFlag = false;
//	for ( i=0; i < document.forms[formindex].elements.length; i++)
//	{
//		GetIndex(formindex, "IT_DECISION");
//		IT_DECISION = i;
//
//		GetIndex(formindex, "PURCHASE_AUTHORITY");
//		PURCHASE_AUTHORITY = i;
//		i++;

//		break;
//	}
//}

//function GetIndex(formindex, GroupName)
//{
//	var namelen = GroupName.length;

//	while (document.forms[formindex].elements[i].name.substring(0,namelen) != GroupName)
//	{
//		i++;
//	}
//}

// This function is running with assumption that 'None of the Above' option is the very last
// in the group of checkboxes.
// Start index must be calculated when document is being loaded using InitIndex() function 

function checker(formindex,start,groupname,none)
			// 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")
			// 'None of the above' option (Example: "99" if "QUES_5-99" is 
			//			a name of 'None of the above' checkbox
{
	var namelen = groupname.length;
	var OptionChecked = false;
	var LoopCount = parseInt(none);
	var NoneName = groupname + "-" + none;
	
	
	for ( var j=start; j < document.forms[formindex].elements.length; j++)
	{
		if (document.forms[formindex].elements[j].name.substring(0,namelen) == groupname)
		{	// group of check boxes located: continue loop inside the group
			while (LoopCount>=0)
			{ 
				if (document.forms[formindex].elements[j].name == NoneName)
 				{
 					if (OptionChecked == true)
 					{
 						document.forms[formindex].elements[j].checked = false;
 					}
 					LoopCount=0;	//break the loop when reach None option
 				}
 				else
 				{
					if (OptionChecked != true)
					{
 						if (document.forms[formindex].elements[j].checked==true)
 						{
 							OptionChecked = true;
 						}
					}
 				}
				j++;
				LoopCount--;
			}
			break;
		}
	}
}		

function validEmail(email)
{
	invalidchars = " /:;"

	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);
	}

	// remove leading spaces
	if(email == "")
	{			//checks to see if blank field
		return true;
	}

	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 ConfirmQuit(formindex)
{
	if(document.forms[0].RECEIVE_YES_NO[1].checked)
	{
		if ((ObjectExists("bslRECEIVE_YES_NO")) || (ObjectExists("ewkRECEIVE_YES_NO")))
		{
			if (ObjectExists("bslRECEIVE_YES_NO"))
			{
				// RECEIVE_YES_NO question for IST
				if (document.forms[0].bslRECEIVE_YES_NO[1].checked)
				{
	    				return "You answered 'No' to the question: I wish to receive CIO Insight FREE." + 
					       "\nYou answered 'No' to the question: I wish to receive Baseline FREE." +
					       "\n\nIf you made this choice in error, please press 'Cancel', return to the question and select 'Yes' for one or both of these publications.";
	  			}
			}
			if (ObjectExists("ewkRECEIVE_YES_NO"))
			{
				// RECEIVE_YES_NO question for eWEEK
				if (document.forms[0].ewkRECEIVE_YES_NO[1].checked)
				{
	    				return "You answered 'No' to the question: I wish to receive CIO Insight FREE." + 
					       "\nYou answered 'No' to the question: I wish to receive eWEEK FREE." +
					       "\n\nIf you made this choice in error, please press 'Cancel', return to the question and select 'Yes' for one or both of these publications.";
	  			}
			}
		}
		else
		{
    			return "You answered 'No' to the question: I wish to receive CIO Insight FREE." + 
			       "\n\nIf you made this choice in error, please press 'Cancel', return to the question and select 'Yes'\n\n";
		}
	}
	else
	{
		if ((SubmitFlag == true) || (document.forms[0].g.value == "1") || (document.forms[0].G.value == "1"))
		{
			return;
		}
		document.forms[formindex].returns.value = "1";
		return "The form should take under 5 minutes to complete.  That's just 5 minutes for a FREE 1-year \n" + "subscription to CIO Insight {$295 value for non-selected professionals}.";
	}
}

function doSubmit()
{
	SubmitFlag = true;
	return;
}

function formCheck(mode, description)
{ 
	SubmitFlag = true;
	var UCASEtemplate = document.forms[0].template.value.toUpperCase();

	var isEWK = ObjectExists("ewkRECEIVE_YES_NO");	
	var isBSL = ObjectExists("bslRECEIVE_YES_NO");	

	// stop validation when "not to receive" response
	if (document.forms[0].RECEIVE_YES_NO[1].checked == true)
	{
		if (!isBSL && !isEWK)
		{ return true; }
		else
		{
			if (isBSL)
			{
				if (document.forms[0].bslRECEIVE_YES_NO[1].checked == true)
				{return true;}
			}
			if (isEWK)
			{
				if (document.forms[0].ewkRECEIVE_YES_NO[1].checked == true)
				{return true;}
			}
		}

	}

	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(document.forms[0].TITLE.value == "")
	{
		alert("Please specify your Title.");
	        document.forms[0].TITLE.focus();
		return false;
	}
	if(document.forms[0].COMPANY.value == "")
	{
		alert("Please specify your Company name.");
        	document.forms[0].COMPANY.focus();
		return false;
	}
	if(document.forms[0].ADDRESS2.value == "")
	{
		alert("Please specify your Company 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/Province or select 'Outside U.S. or Canada'.");
		       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.selectedIndex == "")
		{
			alert("Please specify your Country.");
        		document.forms[0].COUNTRY.focus();
			return false;
		}
		else
		{
			if ((document.forms[0].COUNTRY[document.forms[0].COUNTRY.selectedIndex].value == "USA") || (document.forms[0].COUNTRY[document.forms[0].COUNTRY.selectedIndex].value == "CANADA"))
			{
				alert("Invalid State/Country combination.");
	        		document.forms[0].COUNTRY.focus();
				return false;
			}
		}
	}
	else
	{
		if (document.forms[0].ZIPCODE.value.length < 5)
		{
			alert("Please specify your Zip/Postal Code.");
	        	document.forms[0].ZIPCODE.focus();
			return false;
		}
	}
		
	//if EmailRequired is true then email will be required - as of 6/1/05 email is required
	//	if ((EmailRequired) && document.forms[0].EMAIL_ADDRESS.value=="")
	if (document.forms[0].EMAIL_ADDRESS.value=="")
	{
		alert ("Please specify your email address.");
    		document.forms[0].EMAIL_ADDRESS.focus();
    		return false;	
	}	

	//taken off from onblur
	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;
	}	      
    
	var MyFocus;	
	errMsg = "Every question must be answered in order to submit the form:\n\n";
	// RECEIVE_YES_NO question
	if (!checkUnselects(document.forms[0].RECEIVE_YES_NO))
	{
		alert (errMsg + "Please specify whether you wish to receive CIO Insight.");
		myFocus=document.getElementById("RECEIVE_YES_NO");
		myFocus.scrollIntoView(true);	
		return false;
	}
	
	// no need to ask this again due to implement yes/no page - as of 9/8/2008 per an email from Steve Plaskon
	//if ((document.forms[0].RECEIVE_YES_NO[1].checked == true) && (document.forms[0].NotToReceive.value != '1'))
	//{
	//	alert ("You answered 'No' to the question: I wish to receive/continue to receive CIO Insight FREE.\n\n" + "  If you made this choice in error, please answer 'Yes'. Click OK to return to the question.\n\n");
	//	myFocus=document.getElementById("RECEIVE_YES_NO");
	//	myFocus.scrollIntoView(true);	
		document.forms[0].NotToReceive.value = '1';
	//	return false;
	//}	
	
	// check the DUAL template for BSL's yes/no question
	if (ObjectExists("bslRECEIVE_YES_NO"))
	{
		// RECEIVE_YES_NO question for BSL
		if (!checkUnselects(document.forms[0].bslRECEIVE_YES_NO))
		{
			alert (errMsg + "Please specify whether you wish to receive/continue to receive Baseline FREE?");
			myFocus=document.getElementById("bslRECEIVE_YES_NO");
			myFocus.scrollIntoView(true);
			return false;
		}
		if ((document.forms[0].RECEIVE_YES_NO[1].checked == true) &&(document.forms[0].bslRECEIVE_YES_NO[1].checked == true))
		// stop validation when "not to receive" response
		{
			return true;
		}
}
	
	// check the DUAL template for IST's yes/no question
	if (ObjectExists("ewkRECEIVE_YES_NO"))
	{
		// RECEIVE_YES_NO question for IST
		if (!checkUnselects(document.forms[0].ewkRECEIVE_YES_NO))
		{
			alert (errMsg + "Please specify whether you wish to receive/continue to receive eWEEK for FREE?");
			myFocus=document.getElementById("ewkRECEIVE_YES_NO");
			myFocus.scrollIntoView(true);
			return false;
		}
		if ((document.forms[0].RECEIVE_YES_NO[1].checked == true) &&(document.forms[0].ewkRECEIVE_YES_NO[1].checked == true))
		// stop validation when "not to receive" response
		{
			return true;
		}
	}
	
	if ((ObjectExists("bslRECEIVE_YES_NO")) || (ObjectExists("ewkRECEIVE_YES_NO")))
	{
		//do nothing
	}
	else
	{
		if(document.forms[0].RECEIVE_YES_NO[1].checked == true)
		// stop validation when "not to receive" response
		{
			return true;
		}
	}
	
	//personal ID
	if (!checkDropdown(document.forms[0].PERSONAL_ID))
	{
		alert("Please specify your Personal ID.");
        	document.forms[0].PERSONAL_ID.focus();
		return false;
	}
	
	// validation for shortest template begin
	if (UCASEtemplate == "SHORTEST")
	{
		if (!checkUnselects(document.forms[0].REQUESTED_VERSION))
		{
			alert (errMsg + "Please check your Baseline subscription format.");
			document.forms[0].REQUESTED_VERSION[0].focus();
			return false;
		}
		if ((document.forms[0].REQUESTED_VERSION[0].checked == true)&&(document.forms[0].STATE.value=='53'))
		{
			alert ("International subscription to Baseline is available in Digital format only.");
			document.forms[0].REQUESTED_VERSION[0].focus();
			return false;
		}
		return true;
	}	// validation for shortest template end
	      
	// Question 1 "radio"
	if (!checkUnselects(document.forms[0].PURCHASE_IT))
	{
		alert (errMsg + "Do you authorize, recommend, evaluate and/or specify IT solutions, products and/or services for your organization?");
		document.forms[0].PURCHASE_IT[0].focus();
		return false;
	}
	

	
	// Question 2 job title
	if (!checkUnselects(document.forms[0].JOB_TITLE))
	{
		if (UCASEtemplate != "SHORT")
		{alert (errMsg + "Question 2: Job Title.");}
		else
		{alert (errMsg + "Question 2: Job Title.");}
		document.forms[0].JOB_TITLE[0].focus();
		return false;
	}

	var q2_resp = CheckedRadioValue(document.forms[0].JOB_TITLE);
	if ((q2_resp=="13") && (document.forms[0].OTHER_EXEC_VP_IT_TEXT.value==""))
	{
		if (UCASEtemplate != "SHORT")
		{alert (errMsg + "Question 2 - Other Exec VP/Sr VP/VP/Chief.");}
		else
		{alert (errMsg + "Question 2 - Other Exec VP/Sr VP/VP/Chief.");}
		document.forms[0].OTHER_EXEC_VP_IT_TEXT.focus();
		return false;
	}
	if ((q2_resp=="15") && (document.forms[0].JOB_TITLE_DESC.value==""))
	{
		if (UCASEtemplate != "SHORT")
		{alert(errMsg + "Question 2 - Other.");}
		else
		{alert(errMsg + "Question 2 - Other.");}
		document.forms[0].JOB_TITLE_DESC.focus();
		return false;
	}

	// Question 3 primary business
	if (!checkUnselects(document.forms[0].BUSINESS))
	{
		if (UCASEtemplate != "SHORT")
		{alert(errMsg + "Question 3 - Primary business.");}
		else
		{alert(errMsg + "Question 3 - Primary business.");}
		document.forms[0].BUSINESS[0].focus();
		return false;
	}

	var q3_resp = CheckedRadioValue(document.forms[0].BUSINESS);
	if ((q3_resp=="27") && (document.forms[0].BUSINESS_DESC.value==""))
	{
		if (UCASEtemplate != "SHORT")
		{alert(errMsg + "Question 3 - Other.");}
		else
		{alert(errMsg + "Question 3 - Other.");}
		document.forms[0].BUSINESS_DESC.focus();
		return false;
	}

	// question 4 RESELL
	if (ObjectExists("RESELL_IT"))
	{
		if (!checkUnselects(document.forms[0].RESELL_IT))
		{
			alert (errMsg + "Question 4: Resell IT products and services.");
			document.forms[0].RESELL_IT[0].focus();
			return false;
		}
	}
		
	// question 5 REQUESTED_VERSION
	if (ObjectExists("bslREQUESTED_VERSION"))
	{
		//start validation - if 'no' selected for RECEIVE_YES_NO 
		if (document.forms[0].RECEIVE_YES_NO[1].checked == true) 
		{
		//no need to check "REQUESTED_VERSION" field
		}
		else  //do the following validation
		{
			// REQUESTED_VERSION question
			if (!checkUnselects(document.forms[0].REQUESTED_VERSION))
			{
				alert (errMsg + "Question 5: Please check your CIO Insight subscription format.");
				document.forms[0].REQUESTED_VERSION[0].focus();
				return false;
			}                                
			
			if ((document.forms[0].REQUESTED_VERSION[0].checked == true)&&(document.forms[0].STATE.value=='53'))
			{
				alert ("International subscriptions are available in Digital format only.");
				document.forms[0].REQUESTED_VERSION[0].focus();
				return false;
			}
		}// end validation - if 'no' selected for RECEIVE_YES_NO
	}
	else //other then the dual form
	{
		// REQUESTED_VERSION question
		if (!checkUnselects(document.forms[0].REQUESTED_VERSION))
		{
			if (UCASEtemplate != "SHORT")
			{alert (errMsg + "Question 5: Please check your CIO Insight subscription format.");}
			else
			{alert (errMsg + "Question 6: Please check your CIO Insight subscription format.");}
			document.forms[0].REQUESTED_VERSION[0].focus();
			return false;
		}          
		if ((document.forms[0].REQUESTED_VERSION[0].checked == true)&&(document.forms[0].STATE.value=='53'))
		{
			alert ("International subscriptions are available in Digital format only.");
			document.forms[0].REQUESTED_VERSION[0].focus();
			return false;
		}
	}// end question 6 REQUESTED_VERSION
	
	// check the DUAL template for IST's REQUESTED_VERSION question
	if (ObjectExists("bslREQUESTED_VERSION"))
	{
		//start validation - if 'yes' selected for istRECEIVE_YES_NO 
		if (document.forms[0].bslRECEIVE_YES_NO[0].checked == true)
		{
			// REQUESTED_VERSION question for BSL
			if (!checkUnselects(document.forms[0].bslREQUESTED_VERSION))
			{
				alert (errMsg + "Question 6: Please check your Baseline subscription format.");
				document.forms[0].bslREQUESTED_VERSION[0].focus();
				return false;
			}
			if ((document.forms[0].bslREQUESTED_VERSION[0].checked == true)&&(document.forms[0].STATE.value=='53'))
			{
				alert ("International subscription to Baseline is available in Digital format only.");
				document.forms[0].bslREQUESTED_VERSION[0].focus();
				return false;
			}			
		}//end validation - if 'yes' selected for istRECEIVE_YES_NO 
	}
	
	// check the DUALEWK template for EWK's REQUESTED_VERSION question
	if (ObjectExists("ewkREQUESTED_VERSION"))
	{
		//start validation - if 'yes' selected for ewkRECEIVE_YES_NO 
		if (document.forms[0].ewkRECEIVE_YES_NO[0].checked == true)
		{
			// REQUESTED_VERSION question for EWK
			if (!checkUnselects(document.forms[0].ewkREQUESTED_VERSION))
			{
				alert (errMsg + "Question 6: Please check your eWEEK subscription format.");
				document.forms[0].ewkREQUESTED_VERSION[0].focus();
				return false;
			}
		}//end validation - if 'yes' selected for ewkRECEIVE_YES_NO 
	}
		
	SubmitFlag = true;
	return true;	

	}

// 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","GHJK","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 CheckboxChecked(CheckBoxName)
{
var ChLength = CheckBoxName.length + 1;
var vName = CheckBoxName + "-";

var CBindex = 0;
	for ( i=0; i < document.forms[0].elements.length; i++)
	{
		if (document.forms[0].elements[i].type == "checkbox")
		{
			if (document.forms[0].elements[i].name.substring(0,ChLength)==vName)
			{
				if (CBindex==0) {CBindex = i;}
				if (document.forms[0].elements[i].checked)
				{
					return true;
					break;
				}
			}
		}
	}
	//document.forms[0].elements[CBindex].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 ObjectExists(fieldname)
{
	for ( i=0; i < document.forms[0].elements.length; i++)
	{
		if (document.forms[0].elements[i].name == fieldname)
		{
			return true;
		}
	}
	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;
}

//Privacy Policy Link
function PopupPrivacyPolicy()
{
	window.open("http://www.ziffdavisenterprise.com/PrivacyPolicy.htm","","height=500, width=780,toolbar=yes,resizable=yes,scrollbars=yes");
}	

