onBodyLoad = initForm;



function recordTCV(){
	TCVvalue = "tc=" + getURLValue("tc") + ";v=" + getURLValue("v");
	expires = new Date();
	expires.setFullYear(expires.getFullYear()+20);
	SetCookie("PCH_TCV", TCVvalue, expires, "/");
}
function restoreTCV(){
	document.frmOrder.tcIdA.value = getNameVal("PCH_TCV", "v") ;
	document.frmOrder.tcIdB.value = getNameVal("PCH_TCV", "tc") ;
}


function fMojoTrack(){
//03.28.2007 - ADD THE MOJO PIXEL ON SUBIT - markm
	var mpt = new Date();
	var mojoImg = document.getElementById("mojoImg");
	mojoImg.src = "http://altfarm.mediaplex.com/ad/bk/8290-45416-2054-9?Submitoptin=1&mpuid=" + mpt.getTime() + "-" + mpt.getTimezoneOffset();
	mojoImg.style.display="block";
}

function initForm(){
	f = document.forms[0]; // shortcut reference to order form

	prepop("Title", "ti", false);
	prepop("FName", "fn", true);
	prepop("LName", "ln", true);
	prepop("Address1", "a1", true);
	prepop("Address2", "a2", true);
	prepop("City", "ci", true);
	prepop("State", "st", false);
	prepop("Zip", "zi", false)
	prepop("Email", "em", false)
	
	if (f.ID_Type) f.ID_Type.value = getURLValue("ID_Type");
	if (f.Source) f.Source.value = getURLValue("Source");
	if (f.Segment_sourceKey) f.Segment_sourceKey.value = getURLValue("Segment_sourceKey");
	if((f.tcIdA) && (f.tcIdB) ) {
		f.tcIdA.value = getURLValue("v");
		f.tcIdB.value = getURLValue("tc");
	}
	if(f.destId) f.destId.value = getURLValue("destId");   
	if(f.foreign) f.foreign.value  = getURLValue("foreign"); 
	if (f.OSType) f.OSType.value = navigator.userAgent.toString().replace(/\;/g," ");
	if (f.BrowserType) f.BrowserType.value = navigator.appName.toString().replace(/\;/g," ");
}

function validateBillMe() {
	if(!ValidateLocals()) return false;
	if(!ValidateOrderInfo()) return false;
	if(!ValidateGiftOrderInfo()) return false;
	f.submit();
}

// retrieve the value from a pulldown menu
function getOption(pulldown){
	return pulldown.options[pulldown.options.selectedIndex].value;
}

// retrieve the text (label) from a pulldown menu
function getOptionText(pulldown){
	return pulldown.options[pulldown.options.selectedIndex].text;
}

// retrieve the value associated with a radio button group
function getRadio(radio){
	for (i=0; i<radio.length; i++){
		if (radio[i].checked) return radio[i].value;
	}
	return "";
}

// retrieve the value(s) associated with a check box (group)
function getCheck(check){
	if (check.length == undefined) return check.value;
	values = [];
	for (i=0; i<check.length; i++){
		if (check[i].checked) values[values.length] = check[i].value;
	}
	return values;
}

// parse a variable from the query string
function getURLValue(variable){
	queryString = document.location.search.substring(1);
	if (queryString == "") return "";
	pairs = queryString.split("&");
	for (i=0; i<pairs.length; i++){
		if (pairs[i].split("=")[0].toLowerCase() == variable.toLowerCase()) return unescape(pairs[i].split("=")[1]);
	}
	return "";
}

// write a query string variable to the page
function writeVar(varName){
	document.write(getURLValue(varName));
}


// prepop values in form
function prepop(field, value, caps){
	field = document.forms[0][field];
	if (field == undefined) return; // field not found in form? exit here!

	value = getURLValue(value).toLowerCase();
	if (value=="") return; //value not found in querystring? exit here!
	
	if (field.name=="Title" && value.indexOf(".")==-1) value+=".";
	
	if (field.type == "radio" || field.type == "select-one"){ //radios or pulldowns
		for (i=0; i<field.length; i++){
			if (field[i].value.toLowerCase() == value && field.type == "radio") {
				field[i].checked = true;
				return;
			}else if (field[i].value.toLowerCase() == value && field.type == "select-one"){
				field.selectedIndex = [i];
				return
			}
		}
	}else field.value = caps?capitalize(value):value; // text field
}

function capitalize(string){
	// capitalizes all words in a string
	tmp = "";
	string = string.split(/\s+/); // split on any white space
	for (i=0; i<string.length; i++){
		tmp += ((i==0)?"":" ") + string[i].charAt(0).toUpperCase() + string[i].substring(1).toLowerCase();
	}
	return tmp;
}


function wrongPerson(){
	if (getURLValue("fn") == "") return;
	identity = capitalize(getURLValue('fn') + " " + getURLValue('ln'));
	document.write("This communication has been officially prepared for " + identity + ". ");
	document.write("<b>If you are not " + identity + ", <a href='"+ stripQueryString() + "'>please click here.</a></b>");
}

function stripQueryString(){
	//strips the querystring from personalized info while leaving other data
	excluded = "fn,ln,a1,a2,ci,st,zi,ti,em";
	queryString = document.location.search.substring(1);
	if (queryString == "") return document.location.href; // no querystring: return current URL
	else url = document.location.href.substr(0, document.location.href.indexOf("?")) + "?";
	pairs = queryString.split("&");
	for (i=0; i<pairs.length; i++){
		if (excluded.indexOf( pairs[i].split("=")[0].toLowerCase()) == -1 ) url += ((i==0)?"":"&") + pairs[i].split("=")[0] + "=" + pairs[i].split("=")[1];
	}
	return url;
}

function popup(url, w, h){	
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	features = 'width='+w+',height='+h+',top='+top+',left='+left+',toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';
	newwindow=window.open(url,'newwin',features);
	newwindow.window.focus();
}

function popupScroll(url, w, h){	
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	features = 'width='+w+',height='+h+',top='+top+',left='+left+',toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
	newwindow=window.open(url,'newwin',features);
	newwindow.window.focus();
}
//////////////////////////////////////////////////////////////////////////////////////////
var counter = 0;
function check(box, duplicate){
	if (box.checked) counter++;
	else counter--;
	if (counter > 3){
		box.checked = false;
		counter--;
		alert ("You may order up to three magazines only.\nPlease review your selections.");
	}
	if (arguments.length == 2) duplicate.checked = box.checked;
}

/*****************************************************************************/

	function getImageOnly(imgPath)	{
		var img = parseInt(imgPath.lastIndexOf("/"))+1;
		return imgPath.substr(img);
	}

	function openit(sURL,w,h){
		if (urlStringarray[1]){
			sURL=sURL+"?"+urlStringarray[1];
		}else{
			urlStringarray[1]="No Query";
		}	
		newwindow=window.open(sURL,"newwin","scrollbars=no,toolbar=no,directories=no,menubar=no,resizable=yes,status=no,width="+w+",height="+h+"");
		newwindow.window.focus();
	}

	function openit2(sURL,w,h){
		newwindow=window.open(sURL,"nextwin","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=no,width="+w+",height="+h+"");
		newwindow.window.focus();
	}
	
	function ChangeIT(imgName,imgSrc){
		//alert(noclick);
		document.frmOrder[imgName].src=imgSrc;
	}	
	
	function go(){
		ChangeIT("load","http://a1708.g.akamai.net/7/1708/5303/456456/image.pch.com/opsimages/63fdb558-54a9-4c3a-a4eb-de52eee2fcbe/aq/06/35-52/bireport.gif");
	}	
	
/*
	Chris Donnan
	2003 09 24

	BillMe and Gifting support
	This file is a template for local.js includes for billme pages

	Requires library:
		email.js
*/

	// set this flag to true if your page will support gifts, otherwise - false
	// if this is set to true, we will attempt to validate 
	var IS_GIFT_PAGE = false

	/*
		ValidateLocals():
	*/
	function ValidateLocals() //bool
	{
		boxcount=0;
		for (i=0; i<document.forms[0].elements.length; i++){
			if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].checked && document.forms[0].elements[i].name.indexOf("OrderItem") != -1)
			boxcount++;
			if (boxcount > 3) break;
		}
		if (boxcount>3){
			for (i=0; i<document.forms[0].elements.length; i++){
				if (document.forms[0].elements[i].type=="checkbox")
					document.forms[0].elements[i].checked = false;
			}
		}
		/*
			insert any added code needed on a page
			by page basis in this section here
		*/

		return true
	}
	/*
		newCheck()
		called when a page is submitted
	*/
	function newCheck()//void
	{
		// validates localized page-by-page code
		if(!ValidateLocals())
		{
		      return
		}

		// validates bill me order
		if(!ValidateOrderInfo())
		{
			return
		}

		//if we allow gifts
		if(IS_GIFT_PAGE)
		{
			if(!ValidateGiftOrderInfo())
			{
				return
			}				       
		}
		fMojoTrack();
		_globalSubmit = true;
		document.frmOrder.submit();
	}


	/*
		ValidateGiftOrderInfo() 
		called to validate the information in a gift bill me order
	*/
	function ValidateGiftOrderInfo() //bool
	{
		document.frmOrder.GiftOrder.value = 0
		var TitleGift = document.frmOrder.TitleGift.selectedIndex
		var fnameGift = Trim(document.frmOrder.FNameGift.value)
		document.frmOrder.FNameGift.value = fnameGift
		var lnameGift = Trim(document.frmOrder.LNameGift.value)
		document.frmOrder.LNameGift.value = lnameGift
		var emailGift = Trim(document.frmOrder.EmailGift.value)
		document.frmOrder.EmailGift.value = emailGift 
		var address1Gift = Trim(document.frmOrder.Address1Gift.value)
		document.frmOrder.Address1Gift.value = address1Gift
		var address2Gift = Trim(document.frmOrder.Address2Gift.value)
		document.frmOrder.Address2Gift.value = address2Gift
		var cityGift = Trim(document.frmOrder.CityGift.value)
		document.frmOrder.CityGift.value = cityGift 
		var stateGift = document.frmOrder.StateGift.selectedIndex
		var zipGift = Trim(document.frmOrder.ZipGift.value)
		document.frmOrder.ZipGift.value = zipGift

		if(document.frmOrder.GiftOrderCB.checked)
		{
			document.frmOrder.GiftOrder.value = 1
			if(TitleGift==0)
			{
				alert("Please enter a valid Giftee Title")
				document.frmOrder.TitleGift.focus()
				return false
			}
			if(fnameGift=="" || (!strRegEx_FName.test(fnameGift)))
			{
				alert("Please enter a valid Giftee First Name")
				document.frmOrder.FNameGift.focus()
				return false
			}
			if(lnameGift==""|| (!strRegEx_LName.test(lnameGift)))
			{
				alert("Please enter a valid Giftee Last Name")
				document.frmOrder.LNameGift.focus()
				return false
			}
 			if(emailGift!="" && (!strRegEx_Email.test(emailGift)))
			{
				alert("Please enter a valid Giftee Email")
				document.frmOrder.EmailGift.focus()
				return false
			}
			if(address1Gift==""|| (!strRegEx_Address1.test(address1Gift)))
			{
				alert("Please enter a valid Giftee Address 1")
				document.frmOrder.Address1Gift.focus()
				return false
			}
			if(address2Gift !="" && (!strRegEx_Address2.test(address2Gift)))
			{
				alert("Please enter a valid Giftee Address 2")
				document.frmOrder.Address2Gift.focus()
				return false
			}

			if(cityGift==""|| (!strRegEx_City.test(cityGift)))
			{
				alert("Please enter a valid Giftee City")
				document.frmOrder.CityGift.focus()
				return false
			}
			if(stateGift==0)
			{
				alert("Please enter a valid Giftee State")
				document.frmOrder.StateGift.focus()
				return false
			}
			if(zipGift==""|| (!strRegEx_Zip.test(zipGift)))
			{
				alert("Please enter a valid Giftee Zip")
				document.frmOrder.ZipGift.focus()
				return false
			}
			return true
		}
		return true
	}

	/*
		ValidateOrderInfo() 
		called to validate the information in a standard bill me order
	*/
	function ValidateOrderInfo() //bool
	{

		var Title = document.frmOrder.Title.selectedIndex
		var fname = Trim(document.frmOrder.FName.value)
		document.frmOrder.FName.value = fname
		var lname = Trim(document.frmOrder.LName.value)
	        document.frmOrder.LName.value = lname
		var email = Trim(document.frmOrder.Email.value)
		document.frmOrder.Email.value = email
		var address1 = Trim(document.frmOrder.Address1.value)
		document.frmOrder.Address1.value = address1
		var address2 = Trim(document.frmOrder.Address2.value)
		document.frmOrder.Address2.value = address2
		var city = Trim(document.frmOrder.City.value)
		document.frmOrder.City.value = city
		var state = document.frmOrder.State.selectedIndex
		var zip = Trim(document.frmOrder.Zip.value)
		document.frmOrder.Zip.value =  zip

		if(Title==0)
		{
			alert("Please enter a valid Title")
			document.frmOrder.Title.focus()
			return false
		}
		if(fname=="" || !strRegEx_FName.test(fname))
		{
			alert("Please enter a valid First Name")
			document.frmOrder.FName.focus()
			return false
		}
		if(lname=="" || (!strRegEx_LName.test(lname)))
		{
			alert("Please enter a valid Last Name")
			document.frmOrder.LName.focus()
			return false
		}
 		if(email=="" || (!strRegEx_Email.test(email)))
		{
			alert("Please enter a valid Email")
			document.frmOrder.Email.focus()
			return false
		}
		if (!checkEmail(document.frmOrder.Email,"Email")) {
			return false;
		}	
		if(address1==""|| (!strRegEx_Address1.test(address1)))
		{
			alert("Please enter a valid Address 1")
			document.frmOrder.Address1.focus()
			return false
		}
		if(address2 !="" && (!strRegEx_Address2.test(address2)))
		{
			alert("Please enter a valid Address 2")
			document.frmOrder.Address2.focus()
			return false
		}

		if(city==""|| (!strRegEx_City.test(city)))
		{
			alert("Please enter a valid City")
			document.frmOrder.City.focus()
			return false
		}
		if(state==0)
		{
			alert("Please enter a valid State")
			document.frmOrder.State.focus()
			return false
		}
		if(zip==""|| (!strRegEx_Zip.test(zip)))
		{
			alert("Please enter a valid Zip")
			document.frmOrder.Zip.focus()
			return false
		}
		return true
	}

/*	
	// this is a modified version of the above validation function that 
	// does not use a checkbox, it in stead looks to see if any 'gift' fields
	// are filled out, if they are, then we will validate gift, else, will not require
	// the gift fields
	//
	// this can be uncommented and comment the above script to not use the checkbox based
	// ValidateGiftOrderInfo() function 
	
	function ValidateGiftOrderInfo() //bool
	{

		var TitleGift = document.frmOrder.TitleGift.selectedIndex
		var fnameGift = Trim(document.frmOrder.FNameGift.value)
		var lnameGift = Trim(document.frmOrder.LNameGift.value)
		var emailGift = Trim(document.frmOrder.EmailGift.value)
		var address1Gift = Trim(document.frmOrder.Address1Gift.value)
		var address2Gift = Trim(document.frmOrder.Address2Gift.value)
		var cityGift = Trim(document.frmOrder.CityGift.value)

		var stateGift = document.frmOrder.StateGift.selectedIndex
		var zipGift = Trim(document.frmOrder.ZipGift.value)

		if(TitleGift!=""||
			fnameGift !=""||
				lnameGift !=""||
					emailGift !=""||
						address1Gift !=""||
							address2Gift !=""||
								cityGift !=""||
									stateGift !=""||
										zipGift	)
		{
			if(TitleGift==0)
			{
				alert("Please enter a valid Giftee Title")
				document.frmOrder.TitleGift.focus()
				return false
			}
			if(fnameGift=="" || (!strRegEx_FName.test(fnameGift)))
			{
				alert("Please enter a valid Giftee First Name")
				document.frmOrder.FNameGift.focus()
				return false
			}
			if(lnameGift==""|| (!strRegEx_LName.test(lnameGift)))
			{
				alert("Please enter a valid Giftee Last Name")
				document.frmOrder.LNameGift.focus()
				return false
			}
 			if(emailGift!="" && (!strRegEx_Email.test(emailGift)))
			{
				alert("Please enter a valid Giftee Email")
				document.frmOrder.EmailGift.focus()
				return false
			}
			if(address1Gift==""|| (!strRegEx_Address1.test(address1Gift)))
			{
				alert("Please enter a valid Giftee Address 1")
				document.frmOrder.Address1Gift.focus()
				return false
			}
			if(address2Gift !="" && (!strRegEx_Address2.test(address2Gift)))
			{
				alert("Please enter a valid Giftee Address 2")
				document.frmOrder.Address2Gift.focus()
				return false
			}

			if(cityGift==""|| (!strRegEx_City.test(cityGift)))
			{
				alert("Please enter a valid Giftee City")
				document.frmOrder.CityGift.focus()
				return false
			}
			if(stateGift==0)
			{
				alert("Please enter a valid Giftee State")
				document.frmOrder.StateGift.focus()
				return false
			}
			if(zipGift==""|| (!strRegEx_Zip.test(zipGift)))
			{
				alert("Please enter a valid Giftee Zip")
				document.frmOrder.ZipGift.focus()
				return false
			}
			document.frmOrder.GiftOrder.value = 1
			return true
		}
		return true
	}
*/


	/*
		string editing function that trims any leading and trailing whitespace
	*/
	function Trim(str)//string	
	{
		return RTrim(LTrim(str));
	}

	/*
		string editing function that trims any leading whitespace
	*/
	function LTrim(str)//string	
	{
		var whitespace = new String(" \t\n\r");
		var s = new String(str);
		if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
		}
		return s;
	}

	/*
		string editing function that trims any trailing whitespace
	*/
	function RTrim(str)//string	
	{
		var whitespace = new String(" \t\n\r");
		var s = new String(str);
		if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
		}
		return s;
	}


// ************************************************************************************


///////////////////////////////////////// COOKIE LIB

//This function is used to retrieve a value from a name value pair
//stored in a cookie.  There are several assumptions being made
//each one is tested for and a false value is returned if the assumptions/requirements
//are not met
//1.  The cookie name value pairs are delimited by a semicolon ";"
//2.  Each name value pair is delimited by an equal sign "name=value"
function getNameVal(cookieName,valName) {	
	var cookieString;
	var cookieArray;
	var nameValArray;
	var nameValStr;
	var nameValLength;
	var i;
	
	cookieString = GetCookie(cookieName);
	if (cookieString == null) return false;
	
	cookieArray = getStringArr(cookieString,";");
	if (cookieArray.length == 0) return false;

	nameValStr = setArrayStr(cookieArray,"=");
	nameValArray = getStringArr(nameValStr,"=");
	if (nameValArray == 0) return false;
	
	nameValLength = nameValArray.length;
	for (i = 0; i < nameValLength;){
		if (nameValArray[i] == valName){
			return unescape(nameValArray[i+1]);
		}
		i=i+1; //increment every other array member to just get the key values
	}
}

//Purpose of this function is to create a "symbol" delimited
//string out of an array of values
function setArrayStr(strArray,sDelimiter){
	var arrLength;
	var sReturn = ""; 
	var i; 
	arrLength = strArray.length;
	if (arrLength == 0) return false;
	for (i=0;i<arrLength;i++){
		sReturn = sReturn + strArray[i] + sDelimiter; 
	}
	return sReturn
}   
	
	
	
//Purpose of this function is to create a string array
//out of a "symbol" delimited string
function getStringArr(strCookieVals,sDelimiter){
	var sReturn
	sReturn = (sDelimiter.length > 0)? strCookieVals.split(sDelimiter) : false;
	if (!sReturn) return false
	return sReturn
}
	
	
function checkCookieEnable(sRedirect){
	var sCheckCookieName = "check"
	var sCheckCookieVal = "000"
	var sCookieVal = ""
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
	SetCookie(sCheckCookieName,sCheckCookieVal,expdate);
	sCookieVal = GetCookie(sCheckCookieName)
	DeleteCookie(sCheckCookieName,"/")
	
	if (sCookieVal==null) document.location.href = sRedirect
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)  // Except on the Mac - ahead of its time
	date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//	name - String object containing the cookie name.
//	returns - String object containing the cookie value, or null if
//	  the cookie does not exist.
//
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)	return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}
//

//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//	  SetCookie ("myCookieName", "myCookieValue", null, "/");
function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//	name -   String object containing the cookie name
//	path -   String object containing the path of the cookie to delete.  This MUST
//			 be the same as the path used to create the cookie, or null/omitted if
//			 no path was specified when creating the cookie.
//	domain - String object containing the domain of the cookie to delete.  This MUST
//			 be the same as the domain used to create the cookie, or null/omitted if
//			 no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

checkCookieEnable("/errors/cookiesoff.html")




