/*
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.
History: Beth Budwig 11/12/08: added code to handle font-resize tool.  still need to add cookie for page-to-page persistence.
*/

// after page loads, call function to handle any javascript functionality
window.onload = events;

function events() {

	// be sure browser supports methods before running any more
	if(!document.getElementById || !document.getElementsByTagName) {return false;}

	// declare and attempt to assign global 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 referralBox = document.getElementById("referral");
	var emailUsForm = document.getElementById("emailUsForm");
	var emailAttyForm = document.getElementById("emailAtty");
	var contactForm = document.getElementById("contactUs");
	var simpleContactForm = document.getElementById("simpleContact");
	var onlineRegForm = document.getElementById("onlineRegForm");
	var tabs = document.getElementById("tabs");
	var moreTopicsOpen = document.getElementById("moreTopicsLinkOpen");
	var browseLink = document.getElementById("browseByLocLink");
	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");
	// for LDIR county select list ajax call
	var ldirSearchState = document.getElementById("ldirSearchState");
	var ldirSearchCounty = document.getElementById("ldirSearchCounty");
	var ldirWidget = document.getElementById("ldirWidget");
	var print_icon = document.getElementById("print");
	var ldirSearchForm = document.getElementById("LDIRSearchCommand");
	var hpLDIRLinks = document.getElementById("lawyerDirectoryBlockLinks");
	var orderForm = document.getElementById("checkout3Form");
	// adjust font size based on user preference
	setFontSize();
	// initialize page, sets functionality to specific tag classes
	initPage();
	// run other functions based on specific page element existence
	/* stopping javascript validation for forms 
	if(checkout1Form) {valCheck1();}
	if(checkout2Form) {valCheck2();}
	if(checkout3Form) {valCheck3();}
	if(emailUsForm) {valEmailUs();}
	if(emailAttyForm) {valEmailAtty();}
	if(contactForm) {valLDIRContact();}
	if(simpleContactForm) {valSimpleContact();}
	if(onlineRegForm) {valOnlineReg();} */
	if(print_icon) {printPage();}
	if(ldirWidget) {valLdirWidget();}
	if(ldirSearchForm) {valSearchLDIR();}
	if(referralBox) {checkout4();}
	if(checkout1Form) {billAddyToShip();}
	if(tabs) {handleTabs();}
	if(moreTopicsOpen) {openMoreTopics(); closeMoreTopics();}
	if(browseLink) {showBrowseLoc();}
	if(hasScroller) {callScrollers();}
	if(chkout1BillCountry && chkout1BillState) {billStateRefresh();}
	/* no country choice for shipping right now
	if(chkout1ShipCountry && chkout1ShipState) {
		updateElement(chkout1ShipState,'getStatesForCountry?country=USA&mode=ship');
		chkout1ShipCountry.onchange = function () {updateElement(chkout1ShipState,'getStatesForCountry?country='+this.value+'&mode=ship');}
	}*/
	// LDIR ajax call to get list of counties for a state
	if(ldirSearchState && ldirSearchCounty) {
		updateElement(ldirSearchCounty,'/ldir/countiesforstate.do?state='+this.value);
		ldirSearchState.onchange = function () {updateElement(ldirSearchCounty,'/ldir/countiesforstate.do?state='+this.value);}
	}
	if(sampleContent) {initPagination(1);}
	if(hpLDIRLinks) {hpLDIRLinksToggle();}
	if(orderForm) {submitFormOnce();}
	/***************************************************************************
	INITIALIZE PAGE
	***************************************************************************/
	/* 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;
			/* ASSIGN IMAGE ROLLOVERS
			images must have "switchImg" as class attribute
		  and images must be named like "whatever_off.gif" and "whatever_on.gif" */
			if(className.indexOf("switchImg") != -1) {
				allTags[i].onmouseover = function () {
					var imgSrc = this.getAttribute("src");
					var updateSrc = imgSrc.replace("_off","_on");
					this.setAttribute("src",updateSrc);
				}
				allTags[i].onmouseout = function () {
					var imgSrc = this.getAttribute("src");
					var updateSrc = imgSrc.replace("_on","_off");
					this.setAttribute("src",updateSrc);
				}
			}
			/* ASSIGN CLEARING DEFAULT TEXT FROM INPUT BOXES
			input fields must have a class of "clearTxt"*/
			if(className.indexOf("clearTxt") != -1) {
				allTags[i].onclick = function () {
					this.setAttribute('value', '');
				}
			}
			/* 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);	
				}
			}			
			/* SEARCH FOR PROD SCROLLER(S)
			will call code to include them if needed */
			if(className.indexOf("scroller") != -1) {
				hasScroller = true;
			}
			/* SET LINK TO CLOSE WINDOW
			using class of "jsClose"*/
			if(className.indexOf("jsClose") != -1) {
				allTags[i].onclick = function () {
					window.close();
					return false;
				}
			}
			/* ASSIGN HOPUPS
			if(className.indexOf("hopupLink") != -1) {
				allTags[i].onclick = function () {
					var linkID = '#' + this.id;
					callHopUp(linkID);
				}
			}*/
		} // end loop
	} // end initPage
	/***************************************************************************
	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','fontAdjustSmall',365);
			bodyArray[0].className = 'fontAdjustSmall';
		}
	}
	/***************************************************************************
	HOMEPAGE - SHOW/HIDE LDIR LINKS
	***************************************************************************/
	function hpLDIRLinksToggle() {
		var toggle = document.getElementById("viewldcatlabel");
		toggle.onclick = function () {
			var linksDiv = document.getElementById("lawyerDirectoryBlockLinks");
			var linksDivStatus = linksDiv.className;
			if (linksDivStatus == 'hide') {
				setToShow(linksDiv);
				toggle.innerHTML = "&uarr; Hide Categories";
			} 
			else {
				setToHide(linksDiv);
				toggle.innerHTML = "&darr; View Categories";
			}
			return false; //kill link action
		}
	}
	/***************************************************************************
	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;
			}
		}
	}
	/***************************************************************************
	CHECKOUT 4 - SHOW/HIDE REFERRAL BOX
	***************************************************************************/
	function checkout4() {
		referralBox.onchange = function () {
			var otherBox = document.getElementById("otherReferralSource");
			if(this.value == "other") {
				setToShow(otherBox);
			}
			else {
				setToHide(otherBox);
			}
			otherBox.focus();
		}
	}
	/***************************************************************************
	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.onsubmit = function () {
			var radioBtns = ldirSearchForm.specialtyId;
			var selected = false;
			for(var i=0; i<radioBtns.length; i++) {
				if(radioBtns[i].checked) {
					selected = true;
				}
			}
			if(!selected) {
				alert("Please select a legal topic.");
				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.attorney_name, 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.atty_email, 2, "Please provide your email address.")) {return false;}
			if(!validateEmail(this.atty_email, "Please enter a valid email.")) {return false;}
			if(!checkForMinValue(this.counties_served, 2, "Please specify the state(s) &amp; counties you serve.")) {return false;}
			if(!checkForMaxValue(this.comments, 1000, "Please limit your comments to 1,000 characters.")) {return false;}
			if(!checkCheckBoxes(this.practice_areas, 1, "Please specify one or more areas of practice.")) {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]);
					setToShow(divObj);
					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]);
					setToHide(divObj);
					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]);
				setToHide(divToHide);
			}
		}
	} // end global tabs show/hide and "tab selected" state-change handling
	/***************************************************************************
	FILTER NAV FUNCTIONS
	***************************************************************************/
	// More topics open
	function openMoreTopics() {
		var closeLink = document.getElementById("moreTopicsLinkClosed");
		var hideThis1 = closeLink.parentNode;  // default "show"
		var showThis1 = document.getElementById("moreTopicsLinkOpen").parentNode.parentNode; // default "subFilterList hide"
		closeLink.onclick = function () {
			setToHide(hideThis1);
			setToShow(showThis1);
			return false;
		}
	}
	// More topics close
	function closeMoreTopics() {
		var openLink = document.getElementById("moreTopicsLinkOpen");
		var hideThis = openLink.parentNode.parentNode;  // default "subFilterList hide"
		var showThis = document.getElementById("moreTopicsLinkClosed").parentNode; // default "show"
		openLink.onclick = function () {
			setToHide(hideThis);
			setToShow(showThis);
			return false;
		}
	}
	/***************************************************************************
	LDIR HOMEPAGE FUNCTIONS
	***************************************************************************/
	// show browse by location
	function showBrowseLoc() {
		browseLink.onclick = function () {
			var browseArea = document.getElementById("browseByLoc");
			setToShow(browseArea);
		}
	}
	/***************************************************************************
	INCLUDE JQUERY SCROLLERS IF NEEDED
	***************************************************************************/
	function callScrollers() {
		// function for single product scroller displaying 2 products
		$(function() {
			$(".prodScroller2").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				auto: 4000,
				speed: 800,
				visible:2
			});
		});
		// function for single product scroller displaying 1 product
		$(function() {
			$(".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
		$(function() {
			$(".prodScroller2_2").jCarouselLite({
				btnNext: ".next2",
				btnPrev: ".prev2",
				auto: 4000,
				speed: 800,
				visible:2
			});
		});
		// function for a single product scroller displaying 3 products on same page
		$(function() {
			$(".prodScroller3").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				auto: 4000,
				speed: 1100,
				visible:3
			});
		});
		// function for a single product scroller displaying 4 products
		$(function() {
			$(".prodScroller4").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				auto: 4000,
				speed: 1400,
				visible:4
			});
		});
	}
	/***************************************************************************
	HOP UP FUNCTIONS
	***************************************************************************/
		/* function callHopUp(contentID) {
			$(contentID).cluetip({
				showTitle: false, // hide the clueTip's heading
				sticky: true,
				activation: 'click',
				local: true,
				width: 300
		 	});
		} */
		/*function callHopUp(contentID) {
			$(contentID).cluetip({
			showTitle: false, // hide the clueTip's heading
			sticky: true,
			activation: 'click',
			local: true,
			width: 300
			});
			alert(contentID + " YO");
		} */
	/***************************************************************************
	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 setToHide(obj) {
		if(obj) {
			var initClass = obj.className;
			var newClass = initClass.replace("show","hide");
			obj.className = newClass;
		}
	}
	function setToShow(obj) {
		if(obj) {
			var initClass = obj.className;
			var newClass = initClass.replace("hide","show");
			obj.className = newClass;
		}
	}
	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 FUNCTIONS
	***************************************************************************/
	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(subDivs[i].className == 'show' || subDivs[i].className == 'hide') {
				if(divToShow == subDivs[i].id) {
					setToShow(subDivs[i]);
				}
				else {
					setToHide(subDivs[i]);
				}
			}
		}
		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 events function