﻿
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function orderClick(inStrName){
	var thisObj=document.frmOrder[inStrName];
	thisObj.checked=!thisObj.checked;
	check(thisObj);
}

// common personalization strings
function writeFullName(){
	document.write(capitalize(getURLValue("fn") + " " + getURLValue("ln")));
}
function writeInitials(){
	document.write( (getURLValue("fn").charAt(0) + getURLValue("ln").charAt(0)).toUpperCase()  );	
}
function writeFirstName(){
	document.write(capitalize(getURLValue("fn")));
}
function writeLastName(){
	document.write(capitalize(getURLValue("ln")));
}
function writeAddress(){
	document.write(capitalize(getURLValue("a1") + " " + getURLValue("a2")));
}
function writeCityStateZip(){
	document.write(capitalize(getURLValue("ci") + ", " + getURLValue("st")) + getURLValue("zi"));
}

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 getURLValue(v){
	q = document.location.search.substring(1);
	if (q=="") return q;
	pairs = q.split("&");
	for (i=0; i<pairs.length; i++){
		if (pairs[i].split("=")[0].toLowerCase() == v.toLowerCase()) return unescape(pairs[i].split("=")[1]);
	}
	return "";
}


// cookie cart to maintain order information between two pages //

function nextPage(){
	order = [];
	for(i=0; i<document.frmOrder.elements.length; i++){
		if (document.frmOrder.elements[i].name.indexOf("OrderItem")==0 && document.frmOrder.elements[i].value!=""){
			order[order.length] = document.frmOrder.elements[i].value;
		}
	}
	setNameVal("cart", "order", order.toString());
	
	_globalSubmit = true;		
	document.location.href = document.location.href.replace("1.html", "2.html");
}

function prevPage(){
	theURL = stripQueryString(); // clear user info to allow for changes made in the forms fields
	// now add back info rfrom the form:
	if(theURL.indexOf("?")==-1) theURL+="?";
	theURL += "&ti=" + document.frmOrder.Title.options[document.frmOrder.Title.options.selectedIndex].value;
	theURL += "&fn=" + document.frmOrder.FName.value;
	theURL += "&ln=" + document.frmOrder.LName.value;
	theURL += "&a1=" + document.frmOrder.Address1.value;
	theURL += "&a2=" + document.frmOrder.Address2.value;
	theURL += "&ci=" + document.frmOrder.City.value;
	theURL += "&st=" + document.frmOrder.State.options[document.frmOrder.State.options.selectedIndex].value;
	theURL += "&zi=" + document.frmOrder.Zip.value;
	theURL += "&em=" + document.frmOrder.Email.value;
	
	theURL += "&mpRv=true" 
	
	theURL = theURL.replace("2.html", "1.html");

	_globalSubmit = true;		
	document.location.href = theURL;	
}
function initOrder(){
	order = getNameVal("cart", "order");
	if (!order) return;

	order = order.split(",");
	//counter = order.length;
	if (document.location.href.indexOf("2.html") !=-1){
		// if on page 2, write hidden order fields
		for (i=0; i<order.length; i++){
			order[i] = Trim(order[i]); // strip whitespace
			if (order[i]!="") {
				document.getElementById("orderFields").innerHTML += "<input type='hidden' name='OrderItem_" + order[i] + "' value='" + order[i] + "'>";
			}
		}
	}else{
		for(i=0; i<order.length; i++){
			order[i] = Trim(order[i]);
			if (order[i]!="") {
				for (j=0; j<arrOffers.length; j++){
					for (k=0; k<arrOffers[j].length; k++){
						tmpOfferCode = Trim(arrOffers[j][k].offerCode);
						if (tmpOfferCode == order[i]) {
							//alert("Found " + j);
							fCart(j, k, false);	
						}
					}
				}
			}
		}		
	}

}
// end of cookie cart //
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;
}

/* Compiled by Amy Jen */
/* Updated by hens, June'04 */
var noclick=0;
var counter = 0;
var counter2=0;
var tempID=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 items only.\nPlease review your selections.");
	}
	if (arguments.length == 2) document.frmOrder[duplicate].checked = box.checked;
}


function string_GetURLValue(string_name)    {
	var string_queryString = new String(document.location.search);
	if(string_queryString.charAt(0)=="?")   {
		string_queryString = string_queryString.substring(1)
	}
	if(string_queryString.length==0)return "";
	array_nameValuePairs = string_queryString.split("&");
	for(int_counter=0; int_counter < array_nameValuePairs.length; int_counter++)    {
		if(array_nameValuePairs[int_counter].split("=")[0].toUpperCase() == string_name.toUpperCase())  {
			return unescape(array_nameValuePairs[int_counter].split("=")[1]);
		}
	}
	return "";
}

/*****************************************************************************/
/* Prepop Info 											     */
/*****************************************************************************/
	var urlString = window.location.toString();
	urlStringarray = urlString.split("?");

	if (urlStringarray[1]){
		var pairs=urlStringarray[1].split("&");
	
		var key= new Array();
		for (i=0; i<pairs.length; i++) {
			key[i]=pairs[i].split("=");		
		}
	}else{
		urlStringarray[1]="No Query";
	}
	
/*******************************/
//GIFT
function giftLink(Gurl){
	Gurl=Gurl+"?ID_Type=515&Source=XX311&Segment_SourceKey=TY036"
	if (pairs){
		for (i=3; i<pairs.length;i++){
			Gurl=Gurl+"&"+pairs[i];
		}
	}
	newwindow=window.open(Gurl,"giftwin","scrollbars=yes,toolbar=yes,directories=yes,menubar=yes,resizable=yes,status=yes,width=800,height=600");
	newwindow.window.focus();
}
/*******************************/


	function Populate(targetfield,keyToFind) {
		targetfield.value="";
		for (i=0; i<pairs.length; i++) {
			if (key[i][0]==keyToFind){
				if (keyToFind=="ti"){				
					var gettikey = key[i][1].toLowerCase();	
					var gettikey2 = gettikey + ".";
					document.frmOrder.Title.selectedIndex = 0;//default
					for (q=0;q<document.frmOrder.Title.length;q++){
						var val = document.frmOrder.Title.options[q].value;
						if (gettikey == val.toLowerCase()){				
							document.frmOrder.Title.selectedIndex = q;
						}else if (gettikey2 == val.toLowerCase()){
							document.frmOrder.Title.selectedIndex = q;
						}				
					}
				}else if (keyToFind=="st"){				
					var getstkey = key[i][1].toUpperCase();	
					for (r=0;r<document.frmOrder.State.length;r++){
						var val = document.frmOrder.State.options[r].value;
						if (getstkey == val.toUpperCase()){				
							document.frmOrder.State.selectedIndex = r;
						}				
					}
				}else{										
					newVal=key[i][1];
					for (n=0;n<newVal.length;n++){							
						newVal=newVal.replace("%20"," ");
					}
					targetfield.value=newVal;		
				}
			}
		}
	}

	function writeMe(whatKey){
			for (i=0; i<pairs.length; i++) {
				if (key[i][0]==whatKey){
						return key[i][1];		
				}
			}
	}

	function onBodyLoad() {
		var winURL = window.location.toString();
		var fstart = winURL.indexOf("&fn=");
		if (fstart==-1){
			//don't do anything
		}else{
			Populate(document.frmOrder.Title,'ti');
			Populate(document.frmOrder.FName,'fn');
			Populate(document.frmOrder.LName,'ln');
			Populate(document.frmOrder.Address1,'a1');
			Populate(document.frmOrder.Address2,'a2');
			Populate(document.frmOrder.City,'ci');
			Populate(document.frmOrder.State,'st');
			Populate(document.frmOrder.Zip,'zi');
			Populate(document.frmOrder.Email,'em');
		}
		
	}

	function write_ifYouAreNot(blah){
		var temp = window.location.toString();
		var fisnot = temp.indexOf("&fn=");
		if (fisnot==-1){
			//don't do anything
		}else{
			if (blah=="1"){
				document.write(writeMe('fn')+" "+writeMe('ln'));	
			}
			if (blah=="4"){
				document.write("This communication has been officially prepared for <script language='javascript'> write_ifYouAreNot('1') </script>");
				document.write(". If you are not <script language='javascript'> write_ifYouAreNot('1') </script>");
				document.write(" please <a href='#' onclick='wrongPerson();'><span class='smalltext2'>click here</span></a>.");
			}	
		}
	}
	
	function wrongPerson(){
		var winURL = window.location.toString();
		var stopat = winURL.indexOf("&fn");
		var getNew = winURL.substring(winURL, stopat);

		window.location = getNew;
	}
/*****************************************************************************/

	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","assets/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():

		insert any added code needed on a page
		by page basis in this section here
		return true if your stuff is valid and OK to submit, 
		return false if your stuff is invalid and we should not allow submit
	*/
	function ValidateLocals() //bool
	{
		autofillfix();

		/*
			insert any added code needed on a page
			by page basis in this section here
		*/
		
		return true
	}

	var orderlimit = 3;
	var strSLP="";	//leave blank if the page does not have an SLP

	function autofillfix(){
		//explaination: if a user "orders" items via a plug in/toolbar we cannot track that via JS. so on submit we double check to make sure we enforce the orderlimit rule
		var 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){
				if(document.forms[0].elements[i].value != strSLP){ //ingore SLPs
					boxcount++;
				}
			}
			if (boxcount > orderlimit) break;
		}
		
		if (boxcount>orderlimit){
			var arrStrNums=[];
			arrStrNums[0]="zero";
			arrStrNums[1]="one";
			arrStrNums[2]="two";
			arrStrNums[3]="three";
			arrStrNums[4]="four";
			arrStrNums[5]="five";
			arrStrNums[6]="six";
			arrStrNums[7]="seven";
			arrStrNums[8]="eight";
			arrStrNums[9]="nine";

			
			alert ("You may order up to " + arrStrNums[orderlimit] +" items only.\nPlease review your selections.");
			counter = boxcount;
		}		
	}

	/*
		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
			}				       
		}
		setNameVal("cart", "order", "");
		DeleteCookie("cart");
		_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
	}

	/*
		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;
	}


