/*
Author: Doug Varn
Date Created: 6/19/08
Notes: All code handwritten (except the regular expression) by authors.  This file is included on every page to call all javascript functionality unobtrusively making use of jQuery and Ajax when needed.
History: Beth Budwig 11/12/08: added code to handle font-resize tool.  still need to add cookie for page-to-page persistence.
*/
// after DOM loads, call function to handle any javascript functionality
$(document).ready(function() {
	
	// be sure browser supports methods before running any more
	if(!document.getElementById || !document.getElementsByTagName) {return false;}
	
	// declare and attempt to assign variables
	var hasScroller; // may be assigned a value in initPage()
	var allTags = document.getElementsByTagName("*");
	var checkout1Form = document.getElementById("checkout1Form");
	var checkout2Form = document.getElementById("checkout2Form");
	var checkout3Form = document.getElementById("checkout3Form");
	var emailUsForm = document.getElementById("emailUsForm");
	var emailAttyForm = document.getElementById("emailAtty");
	var contactForm = document.getElementById("GetListedCommand");
	var simpleContactForm = document.getElementById("simpleContact");
	var onlineRegForm = document.getElementById("onlineRegForm");
	var tabs = document.getElementById("tabs");
	var chkout1BillCountry = document.getElementById("billCountryGeoId");
	var chkout1BillState = document.getElementById("billStateProvinceGeoId");
	var chkout1ShipCountry = document.getElementById("shipCountryGeoId");
	var chkout1ShipState = document.getElementById("shipStateProvinceGeoId");
	var sampleContent = document.getElementById("sampleContent");
	var ldirWidget = document.getElementById("ldirWidget");
	var print_icon = document.getElementById("print");
	var ldirSearchForm = document.getElementById("ldirSearchForm");
	var orderForm = document.getElementById("checkout3Form");
	
	setFontSize(); // adjust font size based on user preference
	initPage(); // initialize page, sets functionality to specific tag classes
	
	// run other functions based on specific page element existence
	/* stopping javascript validation for most forms 
	if(checkout1Form) {valCheck1();}
	if(checkout2Form) {valCheck2();}
	if(checkout3Form) {valCheck3();}
	if(emailUsForm) {valEmailUs();}
	if(emailAttyForm) {valEmailAtty();}
	if(simpleContactForm) {valSimpleContact();}
	if(onlineRegForm) {valOnlineReg();} */
	if(contactForm) {valLDIRContact();}
	if(print_icon) {printPage();}
	if(ldirWidget) {valLdirWidget();}
	if(ldirSearchForm) {valSearchLDIR();populateSearch();}
	if(checkout1Form) {billAddyToShip();}
	if(tabs) {handleTabs();}
	if(chkout1BillCountry && chkout1BillState) {billStateRefresh();}
	if(sampleContent) {initPagination(1);}
	if(orderForm) {submitFormOnce(); checkCoupon();}
	
	/***************************************************************************
	INITIALIZE PAGE
	***************************************************************************/
	/* ASSIGN CLEARING DEFAULT TEXT REUSABLE CLASS
	element(s) must have a class of "clearTxt" */
	$('.clearTxt').click(function() {
			this.value = '';	
	});
	/* ASSIGN IMAGE ROLLOVERS
	element(s) must have a class of "switchImg"
	and must be named "[name]_off.xyz" and "[name]_on.xyz" */
	$('.switchImg').hover(
		function() {
			$(this).attr('src', $(this).attr('src').replace("_off","_on"));
		},
		function() {
			$(this).attr('src', $(this).attr('src').replace("_on","_off"));
		}
	);									
	/* this function will loop over all tags on the page searching for certain classes
	and assiging them their unique functionality */
	function initPage() {
		for (var i=1; i<allTags.length; i++) {
			var className = allTags[i].className;
			var tagName = allTags[i].nodeName;
			/* FONT-RESIZE TOOL */
			if( (className.indexOf("fontAdjust") != -1) && (allTags[i].parentNode.id == 'fontEmailPrint') ) {
				allTags[i].onclick = function () {
					var fontAdjustSize = this.getAttribute("id");
					var bodyArray = document.getElementsByTagName("body");
					bodyArray[0].className = fontAdjustSize;
					editCookie('fontSize',fontAdjustSize,365);	
				}
			}			
			/* SET LINK TO CLOSE WINDOW
			using class of "jsClose"*/
			if(className.indexOf("jsClose") != -1) {
				allTags[i].onclick = function () {
					window.close();
					return false;
				}
			}
		} // end loop		
	} // end initPage
	/***************************************************************************
	ASSIGN HOVER CLASS FOR SCROLLER BUTTONS
	***************************************************************************/
	//IE6 doesn't support :hover except on links
	$('.scroller button').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	/***************************************************************************
	PRINT PAGE
	***************************************************************************/
	function printPage() {
		var icon = document.getElementById("print");
		icon.onclick = function() {
			if(window.print) {
				window.print();
			}
			return false;
		}
	}
	/***************************************************************************
	USER FONT ADJUSTMENT
	***************************************************************************/
	function setFontSize() {
		var bodyArray = document.getElementsByTagName("body");
		if(readCookie('fontSize')) {
			var savedSize = readCookie('fontSize');
			bodyArray[0].className = savedSize;
		}
		else {
			createCookie('fontSize','fontAdjustMedium',365);
			bodyArray[0].className = 'fontAdjustMedium';
		}
	}
	/***************************************************************************
	HOMEPAGE - SHOW/HIDE LDIR LINKS
	***************************************************************************/
	$('#lawyerDirectoryBlockLinks').hide();
	$('#viewldcatlabel').toggle(
		function() {
			$('#lawyerDirectoryBlockLinks').slideDown(700, function() {
				$('#viewldcatlabel').text('\u2191 Hide Categories');																										
			});
		},
		function() {
			$('#lawyerDirectoryBlockLinks').slideUp(700, function() {
				$('#viewldcatlabel').text('\u2193 View Categories');
			});
		}
	);
	/***************************************************************************
	CHECKOUT 1 FORM - COPY BILLING ADDRESS TO SHIPPING
	***************************************************************************/
	function billAddyToShip() {
		// switch bill addy to ship addy, check for checkbox first which doesn't exist on ebook version of this form
		if(checkout1Form.billToShip) {
			checkout1Form.billToShip.onclick = function () {
				if(this.checked) {
					checkout1Form.shipFirstName.value = checkout1Form.billFirstName.value;
					checkout1Form.shipLastName.value = checkout1Form.billLastName.value;
					checkout1Form.shipCompany.value = checkout1Form.billCompany.value;
					checkout1Form.shipAddress1.value = checkout1Form.billAddress1.value;
					checkout1Form.shipAddress2.value = checkout1Form.billAddress2.value;
					checkout1Form.shipCity.value = checkout1Form.billCity.value;
					checkout1Form.shipStateProvinceGeoId.value = checkout1Form.billStateProvinceGeoId.value;
					checkout1Form.shipPostalCode.value = checkout1Form.billPostalCode.value.substr(0,5);  //limiting shipping zip to 5 chars
					checkout1Form.shipPhone.value = checkout1Form.billPhone.value;
				}
				else {
					checkout1Form.shipCountryGeoId.selectedIndex = 0;
					checkout1Form.shipFirstName.value = '';
					checkout1Form.shipLastName.value = '';
					checkout1Form.shipCompany.value = '';
					checkout1Form.shipAddress1.value = '';
					checkout1Form.shipAddress2.value = '';
					checkout1Form.shipCity.value = '';
					checkout1Form.shipStateProvinceGeoId.value = '';
					checkout1Form.shipPostalCode.value = '';
					checkout1Form.shipPhone.value = '';
				}
			}
		}
	}
	/***************************************************************************
	CHECKOUT 1 FORM - COPY BILLING ADDRESS TO SHIPPING
	***************************************************************************/
	function billStateRefresh() {
		var billStateValue = document.getElementById('billStateProvinceGeoIdDD').value;
		updateElement(chkout1BillState,'getStatesForCountry?country=USA&mode=bill', function() {
			if(billStateValue != '') {
				var billStateList = document.getElementById('billStateList');
				billStateList.value = billStateValue;
			}																																												 							
		}); //default state list to USA states	
		chkout1BillCountry.onchange = function () {updateElement(chkout1BillState,'getStatesForCountry?country='+this.value+'&mode=bill');} //load new states onchange
	}
	/***************************************************************************
	VALIDATE CHECKOUT 1 FORM - ADDRESSES
	***************************************************************************/
	/* the code below depends on a few things:
	1. form fields are named as given below
	2. the country value for United States is "US"
	3. the form value for no selected state or country is blank (ie <option value="">) */
	function valCheck1() {
		checkout1Form.onsubmit = function () {
			if(!checkForMinValue(this.billCountryGeoId, 1, "Please select a country for your billing address.")) {return false;}
			if(!checkForMinValue(this.billFirstName, 1, "Please enter a first name for your billing address.")) {return false;}
			if(!checkForMinValue(this.billLastName, 1, "Please enter a last name for your billing address.")) {return false;}
			if(!checkForMinValue(this.billAddress1, 3, "Please enter an address for your billing address.")) {return false;}
			if(!checkForMinValue(this.billCity, 2, "Please enter a city for your billing address.")) {return false;}
			if(this.billCountryGeoId.value == 'US') {
				if(!checkForMinValue(this.billStateProvinceGeoId, 1, "Please select a state for your billing address.")) {return false;}
			}
			if(!checkForMinValue(this.billPostalCode, 1, "Please enter a zip/postal code for your billing address.")) {return false;}
			if(!checkForMinValue(this.billPhone, 10, "Please enter a phone for your billing address.")) {return false;}
			if(!validateEmail(this.emailAddress, "Please enter a valid email.")) {return false;}

			// these won't show up on ebook version of this form, so check existence first
			if(this.shipCountryGeoId) {
				if(!checkForMinValue(this.shipCountryGeoId, 1, "Please select a country for your shipping address.")) {return false;}
			}
			if(this.shipFirstName) {
				if(!checkForMinValue(this.shipFirstName, 1, "Please enter a first name for your shipping address.")) {return false;}
			}
			if(this.shipLastName) {
				if(!checkForMinValue(this.shipLastName, 1, "Please enter a last name for your shipping address.")) {return false;}
			}
			if(this.shipAddress1) {
				if(!checkForMinValue(this.shipAddress1, 3, "Please enter an address for your shipping address.")) {return false;}
			}
			if(this.shipCity) {
				if(!checkForMinValue(this.shipCity, 2, "Please enter a city for your shipping address.")) {return false;}
			}
			if(this.shipCountryGeoId) {
				if(this.shipCountryGeoId.value == 'US') {
					if(!checkForMinValue(this.shipStateProvinceGeoId, 1, "Please select a state for your shipping address.")) {return false;}
				}
			}
			if(this.shipPostalCode) {
				if(!checkForMinValue(this.shipPostalCode, 1, "Please enter a zip/postal code for your shipping address.")) {return false;}
			}
			if(this.shipPhone) {
				if(!checkForMinValue(this.shipPhone, 10, "Please enter a phone for your shipping address.")) {return false;}
			}
		}
	} // end if checkout 1 form
	/***************************************************************************
	VALIDATE CHECKOUT 2 FORM - SHIPPING
	***************************************************************************/
	function valCheck2() {
		checkout2Form.onsubmit = function () {
			if(!checkForMinValue(this.shipCountry, 1, "Please select a country from the list.")) {return false;}
		}
	}
	/***************************************************************************
	VALIDATE CHECKOUT 3 FORM - PAYMENT
	***************************************************************************/
	function valCheck3() {
		checkout3Form.onsubmit = function () {
			var ccType = document.getElementById("ccType").value;
			var ccExpMonth = document.getElementById("ccExpMonth").value;
			var ccExpYear = document.getElementById("ccExpYear").value;
			if(!checkForMinValue(this.ccType, 1, "Please select a card type.")) {return false;}
			if(!checkForMinValue(this.ccNum, 16, "Please enter a valid credit card number.")) {return false;}
			if(!checkForMinValue(this.ccFirstName, 2, "Please enter the first name on your card.")) {return false;}
			if(!checkForMinValue(this.ccLastName, 2, "Please enter the last name on your card.")) {return false;}
			if(!ccExpMonth) {
				alert("Please enter the month of your card's expiration.");
				document.getElementById("ccExpMonth").focus();
				return false;
			}
			if(!ccExpYear) {
				alert("Please enter the year of your card's expiration.");
				document.getElementById("ccExpYear").focus();
				return false;
			}
			if (ccExpMonth && ccExpYear) {
				var ExpDate = new Date(ccExpYear, ccExpMonth-1);
				var results = compareDateToNow(ExpDate);
				if(results == "<") {
					alert("The expiration date for your credit card has already passed.");
					document.getElementById("ccExpMonth").focus();
					return false;
				}
			}
			/* need to revisit with logic for all supported credit cards
			//3 digit code for visa, mc, discover, 4 for ax
			if (ccType == 'vi' || ccType == 'mc' || ccType == 'ds') {
				if(!checkForMinValue(this.securityCode, 3, "Please enter the 3 digit security code found on the back of your credit card.")) {return false;}
				if(!checkForMaxValue(this.securityCode, 3, "Please enter the 3 digit security code found on the back of your credit card.")) {return false;}
			}
			else {
				if(!checkForMinValue(this.securityCode, 4, "Please enter the 4 digit security code found on the front of your American Express card.")) {return false;}
			}
			*/
		}
	}
	function submitFormOnce() {
		var formSubmit = false;
		orderForm.onsubmit = function () {
			if(formSubmit) {
				alert("Your order has already been submitted. Please wait while it processes.");
				return false;
			}
			else {
				formSubmit = true;
				return true;
			}
		}
	}
	function checkCoupon() {
		var ccType = document.getElementById('ccType');
		var couponCode = document.getElementById('promoCode');
		// check on click of CC type
		ccType.onclick = function() {
			if(couponCode.value.length > 0) {
				alert('Please click the "Apply Coupon" button to apply the coupon to your order before proceeding.');
				return false;
			}
		}
		// but check again as form submits in case they fill out coupon after cc info
		orderForm.onsubmit = function () {
			if(couponCode.value.length > 0) {
				alert('Please click the "Apply Coupon" button to apply the coupon to your order before proceeding.');
				return false;
			}
		}
	}
	/***************************************************************************
	VALIDATE LDIR WIDGET VALUES
	***************************************************************************/
	function valLdirWidget() {
		ldirWidget.onsubmit = function () {
			var cityStateZipcode = document.getElementById("searchNear").value;
			var legalTopic = document.getElementById("specialtyId");
			if(legalTopic.value.length == 0) { 
				if(!valCityStateZipcode(cityStateZipcode)) {return false};
			} else { // legal topic is selected
				if(cityStateZipcode.length == 0) {return true;}
				if(cityStateZipcode == "Enter your location") {return true;}
				if(!valCityStateZipcode(cityStateZipcode)) {return false};
			}
		}
	}
	function valCityStateZipcode(cityStateZipcodeValue) {
		if (cityStateZipcodeValue.length == 5 && !isNaN(cityStateZipcodeValue)) {return true;}
		var stringArray = cityStateZipcodeValue.split(",");
		if (stringArray.length != 2) {
			alert("Please enter a 5 digit zip code or city and state (e.g. Berkeley, CA) or leave it blank.");
			return false;
		}
		var city = stringArray[0];
		var stateCode = stringArray[1];
		stateCode = stateCode.replace(/^\s+/,"");
		stateCode = stateCode.replace(/\s+$/,"");
		if (city.length == 0 || stateCode.length != 2) {
			alert("Please enter a city and a two letter state code (e.g. Berkeley, CA) or a 5 digit zip code");
			return false;			
		}
		return true;
	}
	/***************************************************************************
	VALIDATE EMAIL US SIDEBAR FORM
	***************************************************************************/
	function valEmailUs() {
		emailUsForm.onsubmit = function () {
			if(!checkForMinValue(this.question, 1, "Please choose an email subject from the list.")) {return false;}
			if(!checkForMinValue(this.email, 2, "Please provide your email address.")) {return false;}
			if(!validateEmail(this.email, "Please enter a valid email.")) {return false;}
			if(!checkForMaxValue(this.comments, 500, "Please limit your comments to 500 characters.")) {return false;}
		}
	}
	/***************************************************************************
	VALIDATE LDIR SEARCH FORM
	***************************************************************************/
	function valSearchLDIR() {
		$('#ldirSearchForm').submit(function() {	
			var selectBox = document.getElementById('box1View');
			if ( (selectBox.selectedIndex == -1) && (selectBox.length != 0) ) {
				alert("Please select a legal issue from the list.");
				$('#hack').addClass('hackError');
				return false;
			}
		});
	}
	/***************************************************************************
	VALIDATE LDIR EMAIL ATTORNEY FORM
	***************************************************************************/
	function valEmailAtty() {
		emailAttyForm.onsubmit = function () {
			if(!checkForMinValue(this.your_name, 2, "Please provide your name.")) {return false;}
			if(!checkForMinValue(this.your_email, 2, "Please provide your email address.")) {return false;}
			if(!validateEmail(this.your_email, "Please enter a valid email.")) {return false;}
			if(!checkForMinValue(this.comments, 2, "Please enter a message.")) {return false;}
			if(!checkForMaxValue(this.comments, 2000, "Please limit your message to 2,000 characters.")) {return false;}
		}
	}
	/***************************************************************************
	VALIDATE LDIR CONTACT US FORM
	***************************************************************************/
	function valLDIRContact() {
		contactForm.onsubmit = function () {
			if(!checkForMinValue(this.attorneyName, 2, "Please provide your name.")) {return false;}
			if(!checkForMinValue(this.phone, 10, "Please provide your phone number, complete with area code.")) {return false;}
			if(!checkForMinValue(this.email, 2, "Please provide your email address.")) {return false;}
			if(!validateEmail(this.email, "Please enter a valid email.")) {return false;}
			if(!checkForMinValue(this.countiesServed, 2, "Please specify the state(s) & counties you serve.")) {return false;}
			if(this.practiceAreas.selectedIndex == -1) {alert("Please specify one or more areas of practice."); return false;}
			if(!checkForMaxValue(this.comments, 1000, "Please limit your comments to 1,000 characters.")) {return false;}
		}
	}
	/***************************************************************************
	VALIDATE SIMPLE CONTACT US FORM
	***************************************************************************/
	function valSimpleContact() {
		simpleContactForm.onsubmit = function () {
			if(!checkForMinValue(this.contactSubject, 1, "Please select a subject.")) {return false;}
			if(!checkForMinValue(this.contactEmail, 2, "Please provide your email address so we can respond to your message.")) {return false;}
			if(!validateEmail(this.contactEmail, "Please enter a valid email address.")) {return false;}
			if(!checkForMinValue(this.contactMessage, 2, "Please enter a message.")) {return false;}
			if(!checkForMaxValue(this.contactMessage, 2000, "Please limit your message to 2,000 characters.")) {return false;}
		}
	}
	/***************************************************************************
	VALIDATE ONLINE REGISTRATION FORM
	***************************************************************************/
	function valOnlineReg() {
		onlineRegForm.onsubmit = function () {
			if(!checkForMinValue(this.registerCountry, 1, "Please select a country for registering your product.")) {return false;}
			if(!checkForMinValue(this.registerFName, 1, "Please enter a first name for registering your product.")) {return false;}
			if(!checkForMinValue(this.registerLName, 1, "Please enter a last name for registering your product.")) {return false;}
			if(!checkForMinValue(this.registerAddy1, 3, "Please enter an address for registering your product.")) {return false;}
			if(!checkForMinValue(this.registerCity, 2, "Please enter a city for registering your product.")) {return false;}
			if(this.registerCountry.value == 'US') {
				if(!checkForMinValue(this.registerState, 1, "Please select a state for registering your product.")) {return false;}
			}
			if(!checkForMinValue(this.registerZip, 1, "Please enter a zip/postal code for registering your product.")) {return false;}
			if(!checkForMinValue(this.registerPhone, 10, "Please enter a valid phone number for registering your product.")) {return false;}
			if(!validateEmail(this.registerEmail, "Please enter a valid email registering your product.")) {return false;}
			if(!checkForMinValue(this.opsys, 1, "Please select an Operating System for registering your product.")) {return false;}
		}
	}
	/***************************************************************************
	SITE-WIDE TAB HANDLING
	***************************************************************************/
	// toggle tabs show/hide and "tab selected" state-change handling
	function handleTabs() {
		// create array of link IDs for all <a> tags under tabs list
		// then create array of <div> IDs (content containers to show/hide) by chopping off "link" from end of corresponding linkID
		var tabs = document.getElementById("tabs").getElementsByTagName("a");
		var tabLinkIDs = Array();
		var tabDivIDs = Array();
		var tabLinkID_len;
		for (var i=0; i<tabs.length; i++) {
			tabLinkIDs[i] = tabs[i].id;
			tabLinkID_len = tabLinkIDs[i].length;
			tabDivIDs[i] = tabLinkIDs[i].slice(0,tabLinkID_len-4);
		}
		// set each tab link to swapDisplay function onclick
		for (var i=0; i<tabLinkIDs.length; i++) {
			document.getElementById(tabLinkIDs[i]).onclick = function () {
				swapDisplay(this.id);
				return false;
			}
		}
		// function to swap show/hide class of content div as well as change tabSelected class to particular link being passed in
		function swapDisplay(linkID) {
			var divID = linkID.slice(0,linkID.length-4);
			for (var i=0; i<tabDivIDs.length; i++) {
				if (tabDivIDs[i] == divID) {
					// show text if match
					var divObj = document.getElementById(tabDivIDs[i]);
					$(divObj).removeClass('hide');
					var tabID = tabDivIDs[i] + 'Link';
					var tabObj = document.getElementById(tabID);
					tabObj.className = "tabSelected";
				}
				else {
					// hide text if not a match
					var divObj = document.getElementById(tabDivIDs[i]);
					$(divObj).addClass('hide');
					var tabID = tabDivIDs[i] + 'Link';
					var tabObj = document.getElementById(tabID);
					tabObj.className = "";
				}
			}
		} // end swapDisplay function

		// hide all but first tab initially
		for (var i=0; i<tabDivIDs.length; i++) {
			if (i>0) {
				var divToHide = document.getElementById(tabDivIDs[i]);
				$(divToHide).addClass('hide');
			}
		}
	} // end global tabs show/hide and "tab selected" state-change handling
	/***************************************************************************
	LDIR HOMEPAGE FUNCTIONS
	***************************************************************************/
	function populateSearch() {
		var searchBox = document.getElementById('box1Filter');
		var selectBox = document.getElementById('box1View');
		var optionList = selectBox.options;
		selectBox.onclick = function () {
			var selected = selectBox.selectedIndex;
			searchBox.value = optionList[selected].text;
		}
	}
	/***************************************************************************
	AJAX CALL AND RESPONSE
	***************************************************************************/
	// cross-browser attempt to create an XMLHttpRequest object - thanks Jeremy Keith!
	function getHTTPObject() {
		var xhr = false;
		if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					xhr = false;
				}
			}
		}
		return xhr;
	}
	/* calls for a new XMLHttpRequest and sends get command for file from server.
	also sets readystatechange of HTTP object to a function that will swap original
	element with response data*/
	function updateElement(element,file, fn) {
		var request = getHTTPObject();
		if (request) {
			request.onreadystatechange = function() {
				swapElementWithResponse(request,element);
				if(fn != null) {
					fn();
				}
			};
			request.open("GET", file, true);
			request.send(null);
		}
	}
	/* function gets passed the initially created XMLHttpRequest object and the
	ID of the element the request will update.  this function is called by
	updateElement() or any function that needs to display a response in this way */
	function swapElementWithResponse(request,element) {
		if (request.readyState == 4) {
			if (request.status == 200 || request.status == 304) {
		 		element.innerHTML = request.responseText;
			}
		}
	}
	function updateFormXML(file) {
		var request = getHTTPObject();
		if (request) {
			request.onreadystatechange = function() {
				if(request.readyState == 4 && (request.status == 200 || request.status == 304)) {
						var d = document.createElement("div");
						d.innerHTML = request.responseText;
						var divs = d.childNodes;
					for(var i = 0; i < divs.length; i++) {
						var div = divs[i];
						if(div.id != null)
							document.getElementById(div.id).innerHTML = div.innerHTML;
					}
			 }
			}
			request.open("GET", file);
			request.send(null);
		}
	}
	/***************************************************************************
	INCLUDE JQUERY PLUGINS
	***************************************************************************/
	// function for single product scroller displaying 2 products
	$(".prodScroller2").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		auto: 25000,
		speed: 800,
		visible:1
	});
	// function for single product scroller displaying 1 product
	$(".prodScroller1").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
//		auto: 4000,
		speed: 400,
		visible:1
	});
	// function for a second single product scroller displaying 2 products on same page
	$(".prodScroller2_2").jCarouselLite({
		btnNext: ".next2",
		btnPrev: ".prev2",
		auto: 25000,
		speed: 800,
		visible:1
	});
	// function for a single product scroller displaying 3 products on same page
	$(".prodScroller3").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
//		auto: 4000,
		speed: 1100,
		visible:3
	});
	// function for a single product scroller displaying 4 products
	$(".prodScroller4").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
//		auto: 4000,
		speed: 1400,
		visible:4
	});
	// CART SIDEBAR INCLUDE HOPUPS
	$('#shippingRatesLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#returnPolicyLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#internationalShippingLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#internationalShipping2Link').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#sendEmailLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	// CHECKOUT 3 HOPUPS
	$('#promoTipLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#debitGiftTipLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	$('#aboutSecurityCodeLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	// ONLINE LEGAL FORM HOP-UP
	$('#onlineFormsSafeLink').cluetip({
		showTitle: false, // hide the clueTip's heading
		sticky: true,
		activation: 'click',
		local: true,
		width: 300
	});
	// LDIR HOMEPAGE DUAL LIST BOX (only using one for filtering)
	$.configureBoxes();
	/***************************************************************************
	GENERAL USE FUNCTIONS
	***************************************************************************/
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	function editCookie(name,newValue,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";	
		document.cookie = name+"="+newValue+expires+"; path=/";
	}
	function checkForMinValue(obj, minChar, msg) {
		if(obj) {
			if(obj.value.length < minChar) {
				alert(msg);
				obj.focus();
				return false;
			}
			else {return true;}
		}
		else {
			// no one should see this unless they pass the function an object that doesn't exist
			alert("There has been an error. An object has been called that does not exist on this page.");
			return false;
		}
	}
	function checkIfNumeric(obj, msg) {
		if(obj) {
			if(isNaN(obj.value)) {
				alert(msg);
				obj.focus();
				return false;
			}
			else {return true;}
		}
		else {
			// no one should see this unless they pass the function an object that doesn't exist
			alert("There has been an error. An object has been called that does not exist on this page.");
			return false;
		}		
	}
	function checkForMaxValue(obj, maxChar, msg) {
		if(obj) {
			if(obj.value.length > maxChar) {
				alert(msg);
				obj.focus();
				return false;
			}
			else {return true;}
		}
		else {
			alert("There has been an error. An object has been called that does not exist on this page.");
			return false;
		}
	}
	function validateEmail(emailObj,msg) {
		var emailVal = emailObj.value;
		if((emailVal.search(/^[a-zA-Z0-9_=+-]+((-[a-zA-Z0-9_=+-]+)|(\.[a-zA-Z0-9_=+-]+))*\@[a-zA-Z0-9_=+-]+((\.|-)[a-zA-Z0-9_=+-]+)*\.[a-zA-Z0-9_=+-]+$/) == -1)) {
			alert(msg);
			emailObj.focus();
			return false;
    	}
		else {return true};
	}
	function checkCheckBoxes(obj, minChecked, msg) {
		var isChecked = false;
		for (var i=0; i < obj.length; i++) {
			if(obj[i].checked) {
				isChecked = true;
			}
		}
		if (isChecked == false) {
			alert(msg);
			return false;
		}
		else {return true};
	}
	function compareDateToNow(date) {
	// function compares date and "now" against simple month and year
	// returns either <, >, or =
		var compareResults;
		var today = new Date();
		var todayMonth = today.getMonth();
		var todayYear = today.getFullYear();
		var today = new Date(todayYear, todayMonth);
		if (date.toString == today.toString) {
			compareResults = "=";
		}
		if (date < today) {
			compareResults = "<";
		}
		if (date > today) {
			compareResults = ">";
		}
		return compareResults;
	}
	/***************************************************************************
	SAMPLE CHAPTER PAGINATION
	***************************************************************************/
	function initPagination(ch) { 
		// gets hit when page is first loaded or new chapter chosen
		var chapter = ch; // default chapter
		var section = 1; // default section
		var wysiwygDiv = document.getElementById('wysiwyg');
		var topPagDiv = document.getElementById('topPagination');
		var botPagDiv = document.getElementById('botPagination');
		var sections = returnSections(wysiwygDiv,chapter); // # of sections in current chapter
		if( (!wysiwygDiv) || (!topPagDiv) || (!botPagDiv) || (sections<2) ) {
			return false;
		}
		showSection(section,chapter,wysiwygDiv); // show/hide appropriate sections
		// build the pagination numbers
		buildNumLinks(topPagDiv,sections); 
		buildNumLinks(botPagDiv,sections);
		// add fwd arrows since starting at section 1
		addFwdArrows("top",topPagDiv);
		addFwdArrows("bot",botPagDiv);
		// set up pagination links
		setLinkURLs(topPagDiv,chapter,wysiwygDiv,section,sections); 
		setLinkURLs(botPagDiv,chapter,wysiwygDiv,section,sections);
		setChapterLinks(); // set chapter 1/2 link to switch and initPag onclick
	}
	function returnSections(parentDiv,chapter) {
		//figure out how many sections in chapter
		var subDivs = parentDiv.getElementsByTagName('div');
		var sections = 0; // initialize
		for(var i=0; i<subDivs.length; i++) {
			var idName = subDivs[i].getAttribute('id');
			var idLookUp = "section_" + chapter;
			if (idName) { // filter out the null ids
				if (idName.indexOf(idLookUp) > -1) {
					sections++;
				}
			}
		}
		return sections;
	}
	function showSection(which,chapter,parentDiv) {
		//shows chosen section and returns it
		var subDivs = parentDiv.getElementsByTagName('div');
		switch(which) {
			case '>>':
				section++;
				break;
			case '<<':
				section--;
				break;
			default:
				section = which;
		}
		var divToShow = 'section_' + chapter + '_' + section;
		for(var i=0; i<subDivs.length; i++) {
			if(divToShow == subDivs[i].id) {
				$(subDivs[i]).removeClass('hide');
			}
			else {
				$(subDivs[i]).addClass('hide');
			}
		}
		return section;
	}
	function buildNumLinks(pagDiv,sections) {
		// build brand new pagination list
		while(pagDiv.hasChildNodes()) {
			pagDiv.removeChild(pagDiv.lastChild);
		}
		for(var i=1; i<=sections; i++) {
			var numText = document.createTextNode([i]);
			var blank = document.createTextNode(' ');
			if (i === 1) {
				var strong = document.createElement('strong');
				strong.appendChild(numText);
				pagDiv.appendChild(strong);
				pagDiv.appendChild(blank);
			}
			else {
				var numLink = document.createElement('a');
				numLink.setAttribute('href','');			
				numLink.appendChild(numText);
				pagDiv.appendChild(numLink);
				pagDiv.appendChild(blank);
			}
		}
	}
	function setLinkURLs(pagDiv,chapter,wysiwygDiv,section,sections) {
		var links = pagDiv.getElementsByTagName('a'); // reset list of links
		for(var i=0; i<links.length; i++) {
			links[i].onclick = function () {
				paginate(this.firstChild.nodeValue,chapter,wysiwygDiv,section,sections);
				return false;
			}
		}
	}
	function paginate(next,chapter,wysiwygDiv,section,sections) {
		// runs the show after initPagination
		var topPagDiv = document.getElementById('topPagination');
		var botPagDiv = document.getElementById('botPagination');
		var nextSection = showSection(next,chapter,wysiwygDiv);
		updatePagination(topPagDiv,chapter,wysiwygDiv,section,sections,nextSection);
		updatePagination(botPagDiv,chapter,wysiwygDiv,section,sections,nextSection);
	}
	function updatePagination(pagDiv,chapter,wysiwygDiv,section,sections,next) {
		// replace bold with link, updating old section #
		var oldStrong = pagDiv.getElementsByTagName('strong')[0];
		var newLink = document.createElement('a');
		newLink.setAttribute('href','');
		var linkText = document.createTextNode(oldStrong.firstChild.nodeValue);
		newLink.appendChild(linkText);
		pagDiv.replaceChild(newLink,oldStrong);
		// replace link with bold, highlighting new section #
		var newStrong = document.createElement('strong');
		var strongText = document.createTextNode(next);
		newStrong.appendChild(strongText);
		// loop over links to find one to replace
		var links = pagDiv.getElementsByTagName('a'); // set list of numeric links after created
		for(var i=0; i<links.length; i++) {
			if(links[i].firstChild.nodeValue == next) {
				var oldLink = links[i]; 
			}
		}
		pagDiv.replaceChild(newStrong,oldLink);
		var topPagDiv = document.getElementById('topPagination');
		var botPagDiv = document.getElementById('botPagination');
		var topfwdArrows = document.getElementById('topfwdArrows');
		var botfwdArrows = document.getElementById('botfwdArrows');
		var topbackArrows = document.getElementById('topbackArrows');
		var botbackArrows = document.getElementById('botbackArrows');
		// add front arrows if needed
		if( (!topfwdArrows) && (next < sections) ) {addFwdArrows("top",topPagDiv);}
		if( (!botfwdArrows) && (next < sections) ) {addFwdArrows("bot",botPagDiv);}
		// add back arrows if needed
		if( (!topbackArrows) && (next > 1) ) {addBackArrows("top",topPagDiv);}
		if( (!botbackArrows) && (next > 1) ) {addBackArrows("bot",botPagDiv);}
		// remove forward arrows if needed
		if( (topfwdArrows) && (next == sections) ) {deleteFwdArrows("top",topPagDiv);}
		if( (botfwdArrows) && (next == sections) ) {deleteFwdArrows("bot",botPagDiv);}
		// remove back arrows if needed
		if( (topbackArrows) && (next == 1) ) {deleteBackArrows("top",topPagDiv);}
		if( (botbackArrows) && (next == 1) ) {deleteBackArrows("bot",botPagDiv);}
		// reset link actions since we made some new one(s)
		setLinkURLs(pagDiv,chapter,wysiwygDiv,section,sections); 
	}
	function addFwdArrows(loc,pagDiv) {
		var fwdArrowsId = document.getElementById(loc+'fwdArrows');
		if(!fwdArrowsId) {
			var links = pagDiv.getElementsByTagName('a');
			var forwardArrows = document.createTextNode('>>');
			var forwardLink = document.createElement('a');
			forwardLink.appendChild(forwardArrows);
			forwardLink.setAttribute('href','');
			forwardLink.setAttribute('id',loc+'fwdArrows');
			pagDiv.appendChild(forwardLink);
		}
	}
	function addBackArrows(loc,pagDiv) {
		var backArrowsId = document.getElementById(loc+'backArrows');
		if(!backArrowsId) {
			var links = pagDiv.getElementsByTagName('a');
			var backArrowsText = document.createTextNode('<<');
			var backLink = document.createElement('a');
			var blank = document.createTextNode(' ');
			backLink.setAttribute('href','');
			backLink.setAttribute('id',loc+'backArrows');
			backLink.appendChild(backArrowsText);
			pagDiv.appendChild(backLink);
			// this fixed IE bug where it counts empty text node
			if(pagDiv.firstChild.nodeType == 3) {
				pagDiv.removeChild(pagDiv.firstChild);
			}
			pagDiv.insertBefore(blank,pagDiv.firstChild);
			pagDiv.insertBefore(backLink,pagDiv.firstChild);
		}
	}
	function deleteFwdArrows(loc,pagDiv) {
		var fwdArrowsId = document.getElementById(loc+'fwdArrows');
		if(fwdArrowsId) {
			pagDiv.removeChild(fwdArrowsId);
		}
	}
	function deleteBackArrows(loc,pagDiv) {
		var backArrowsId = document.getElementById(loc+'backArrows');
		if(backArrowsId) {
			pagDiv.removeChild(backArrowsId);
		}
	}
	function setChapterLinks() {
		if(document.getElementById("chapterList").getElementsByTagName('a')[0]) {
			var chapLink = document.getElementById("chapterList").getElementsByTagName('a')[0];
			chapLink.onclick = function () {
				var id = this.parentNode.id;
				var chNum = id.substr(2,1);
				switchChapterLinks();
				initPagination(chNum);
				return false;
			}
		}
	}
	function switchChapterLinks() {
		var strong = document.getElementById('chapterList').getElementsByTagName('strong')[0];
		var strongTxt = strong.firstChild.nodeValue;
		var chLink = document.getElementById('chapterList').getElementsByTagName('a')[0];
		var chLinkTxt = chLink.firstChild.nodeValue;
		var newStrong = document.createElement('strong'); 
		var newStrongTxt = document.createTextNode(chLinkTxt);
		newStrong.appendChild(newStrongTxt);
		var newLink = document.createElement('a');
		newLink.setAttribute('href', '');
		var newLinkTxt = document.createTextNode(strongTxt);
		newLink.appendChild(newLinkTxt);
		chLink.parentNode.replaceChild(newStrong,chLink);
		strong.parentNode.replaceChild(newLink,strong);
	}	
}); // end parent function