
/**********************************************************************
*	Functions used to alter any links to www.cato.org or cato.org	  *
*	to specify an onclick event handler for Google Analytics.		  *
*	AGS(RH) - 6/18/08												  *
**********************************************************************/
	function alterCatoLinks(){
		for(var i=0;i<document.links.length;i++){
			if (isCatoURL(document.links[i].href)) {
				/*
				In IE, the 'return false' seems to prevent the link from working - AGS(RH) - 6/18/08 
				document.links[i].onclick = "javascript:pageTracker._link(this.href); return false;"; */
				document.links[i].onclick = "javascript:pageTracker._link(this.href);";
				
				/* 
				Add onchange and onclick handlers using addEventListener below.  See the following url for more info. - AGS(RH) - 10/19/07
				http://developer.mozilla.org/en/docs/DOM:element.addEventListener */
				var onClickHandler = new Function(document.links[i].onclick); 
				if (document.links[i].addEventListener) { 
				    document.links[i].addEventListener('click', onClickHandler, false ); 
				} else if (document.links[i].attachEvent) { 
				    document.links[i].attachEvent('onclick', onClickHandler); 
				} 
			}
		}
	}
	
	function isCatoURL(sURL){
		var sURLToTest = sURL;
		var rexp = /^https?:\/\/([a-z0-9\-]+\.)?cato\.org/i;
		var b = rexp.test(sURLToTest);
		return b;
	}

/**********************************************************************
*	The function fillShipping fills the Shipping fields with the	  *
*	values from the Billing fields.									  *
**********************************************************************/

	function fillShipping(f){

		if(f.bShipBillSame.checked){
			f.sShipTo.value = f.sFirstname.value + ' ' + f.sLastname.value
			f.sShipAddress1.value = f.sBillAddress1.value
			f.sShipAddress2.value = f.sBillAddress2.value
			f.sShipCity.value = f.sBillCity.value
			f.sShipState.selectedIndex = f.sBillState.selectedIndex
			f.sShipRegion.value = f.sBillRegion.value
			f.sShipCountry.selectedIndex = f.sBillCountry.selectedIndex
			f.sShipZip.value = f.sBillZip.value
			toggleStateRegion(f, "sShip");	// change the colors of the shipping state/region fields if necessary
		}
	}
	
	function fillCheckboxIfSame(f){
		if(
			f.sShipTo.value == f.sFirstname.value + ' ' + f.sLastname.value &&
			f.sShipAddress1.value == f.sBillAddress1.value &&
			f.sShipAddress2.value == f.sBillAddress2.value &&
			f.sShipCity.value == f.sBillCity.value &&
			f.sShipState.selectedIndex == f.sBillState.selectedIndex &&
			f.sShipRegion.value == f.sBillRegion.value &&
			f.sShipCountry.selectedIndex == f.sBillCountry.selectedIndex &&
			f.sShipZip.value == f.sBillZip.value
		  ){
			f.bShipBillSame.checked = true;
			return true;
		}
		return false;
	}
		
/**********************************************************************
*	The function unCheckShipBox unchecks the bShipBillSame checkbox  *
*	if the Shipping textfield and Billing textfield become different. *
*	This is checked everytime a Shipping or Billing field has been	  *
*	changed.  The function first determines which of the fields has	  *
*	called the function (Shipping/Billing).  Then, if the respective  *
*	Shipping/Billing fields are different, the checkbox is unchecked. *
*	The sShipTo/sFirstname-sLastname & the sShipstate/sBillstate are also  *
*	tested because they are handled differently.					  *
**********************************************************************/
	
	function unCheckShipBox(fld){
		if(fillCheckboxIfSame(fld.form)) return false;
		chkBox = fld.form.bShipBillSame
		noPfx = false;
		if(fld.name.substring(0,5) == "sShip"){
			thatPfx = "sBill";
		} else if(fld.name.substring(0,5) == "sBill"){
			thatPfx = "sShip";
		} else {
			// sFirstname or sLastname field has been selected
			noPfx = true;
		}
		
		fldRemainder = fld.name.substr(5)
		
		if(fldRemainder == "To" || noPfx == true){
			if(chkBox.checked){
				billFullname = fld.form.sFirstname.value + ' ' + fld.form.sLastname.value
				shipFullname = fld.form.sShipTo.value
				if(billFullname != shipFullname){
					uncheck(chkBox);
				}
			}
		} else {
			thisfldname = fld.name
			thisfld = eval("fld.form." + thisfldname)
			thatfldname = thatPfx + fldRemainder;
			thatfld = eval("fld.form." + thatfldname)

			if(chkBox.checked){
				if(fldRemainder == "State" || fldRemainder == "Country") {
					if(thatfld.selectedIndex != thisfld.selectedIndex){
						uncheck(chkBox);
					}				
				} else {			
					if(thatfld.value != thisfld.value){
						uncheck(chkBox);
					}				
				}
			}
		}
	}
	
/**********************************************************************
*	The function uncheck will uncheck a checkbox.  It takes the		  *
*	argument chkBox, which is the checkbox object.					  *
**********************************************************************/	

	function uncheck(chkBox){
		chkBox.checked = false;
	}

	
/**********************************************************************
*	The function openWin will open a new window given various		  *
*	parameters.														  *
**********************************************************************/	
	
	
function openWin(mylink, w, h, otherparams, targetName){
	if(!targetName){
		targetName = "win1";
	}
	mywin = window.open(mylink, targetName, 'width='+w+',height='+h + otherparams);
	mywin.focus()
}


function toggleStateRegion(f, sPfx, sAction){
	theStateField = eval("f." + sPfx + "State");
	theRegionField = eval("f." + sPfx + "Region");
	theCountryField = eval("f." + sPfx + "Country");
	
	bUSAIsSelected = false;
	bCanadaIsSelected = false;
	determineSelectedCountry(theCountryField);
	
	if(bUSAIsSelected || bCanadaIsSelected){
		if(sAction == "testfocus"){
			toggleFocus(theStateField, 1);
			toggleFocus(theRegionField, 0);
		} else {
			theStateField.className = "enabled-field";
			theRegionField.className = "disabled-field";
			theRegionField.value = "";
		}
	} else{
		if(sAction == "testfocus"){
			toggleFocus(theStateField, 0);
			theStateField.selectedIndex = 0;
			toggleFocus(theRegionField, 1);
		} else {
			theStateField.className = "disabled-field";
			theRegionField.className = "enabled-field";
			theStateField.selectedIndex = 0;
		}
	}
}

function toggleFocus(sFld, bAllowFocus){
	if(!bAllowFocus){
		sFld.blur();
	}
}