// Rollovers are using CSS but need JS to prevent flicker on older comps
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// Preload Images
if (document.images) {
	nav_link = new Image();
	nav_link.src = "images/tree1.gif";
	nav_over = new Image();
	nav_over.src = "images/tree2.gif";
}

function docElement(strElement) {
	return document.getElementById(strElement);
}

function changeValue(strElement, strValue) {
	docElement(strElement).value = strValue;
}

function changeClass(strElement, strClass) {
	docElement(strElement).className = strClass;
}

function changeHTML(strElement, strHTML) {
	docElement(strElement).innerHTML = strHTML;
}

function changeDisplay(strElement, strDisplay) {
	docElement(strElement).style.display = strDisplay;
}

function redirectTo(strPage) {
	window.location.href = strPage;
}

function showElement(strElement) {
	if (docElement(strElement)) {
		docElement(strElement).style.visibility = "visible";
	}
}

function hideElement(strElement) {
	if (docElement(strElement)) {
		docElement(strElement).style.visibility = "hidden";
	}
}

function getAgent() {
	var sAgent = navigator.userAgent;
	if (sAgent.indexOf("MSIE 5.") >= 0) {
		return "IE5"
	} else if (sAgent.indexOf("MSIE 6.") >= 0) {
		return "IE6"
	} else if (sAgent.indexOf("MSIE 7.") >= 0) {
		return "IE7"
	} else if (sAgent.indexOf("Firefox") >= 0) {
		return "FF"
	} else if (sAgent.indexOf("Opera") >= 0) {
		return "Opera"
	} else {
		return "Unknown"
	}
}

function changePaymentSubmitted() {
	docElement("checkoutSubmit").disabled = "disabled";
	docElement("paymentSubmitted").innerHTML = "Processing payment, this may take several seconds...<br /><br />";
	docElement("paymentSubmitted").className = "bold red small";
}

function configureLoadingAnimation(strImage) {
	if (docElement(strImage).style.visibility != "visible") {
		docElement("loadingAnimation").style.display = "";
	}
}

function hideLoadingAnimation(strImage) {
	if (docElement("loadingAnimation")) {
		docElement("loadingAnimation").style.display = "none";
		strImage.style.visibility = "visible";
		if (docElement("sold_img")) {
			docElement("sold_img").style.visibility = "visible";
		}
	}
}

function toggleHover(strElement, strClass1, strClass2) {
	if (getAgent() == "IE5" || getAgent() == "IE6") {
		if (strElement.className == (strClass1+" "+strClass2)) {
			strElement.className = strClass1;
		} else {
			strElement.className = strClass1+" "+strClass2;
		}
	}
}

function getQS(strString) {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			if (strString == key) {
				return val;
			}
		}
	}
	return false;
} 

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function selectAll() {
	var action = "checked";
	if (docElement("removeItem").checked) { action = ""; }
	var aryElements = getElementsByClassName(document, "input", "removeProduct");
	for(var i=0; i<aryElements.length; i++) { aryElements[i].checked = action; }
}

function formatPhone(field) {
	var temp = field.value.replace(/[^0-9_]/g, "");
	var cursor = temp.length;
	temp = temp.replace(/^([0-9_]{0,3})([0-9_]{0,3})([0-9_]{0,4})([0-9_]*)/, "($1) $2-$3 x $4");
	if (cursor < 11)
		temp = temp.replace(/\s*x\s*$/, "");
	if (cursor < 7)
		temp = temp.replace(/\-\s*$/, "");
	if (cursor < 3)
		temp = temp.replace(/\)\s*$/, "");
	if (! cursor)
		temp = "";
	field.value = temp;
	return true;
}

function checkState(objSel) {
	var objText = objSel.form[objSel.name + "Other"];
	if (objText) {
		if (objSel[objSel.selectedIndex].value == "OTHER") {
			objText.style.display = "inline";
		} else {
			objText.style.display = "none";
		}
	}
}

function validateFormField(blnValid, objFormField, strAlert) {
	if (blnValid) {
		if (objFormField) {
			if (objFormField.value.replace(" ", "") == "") {
				if (strAlert != "") { alert(strAlert); }
				objFormField.focus();
				return false;
			}
		}
	} else { return false; }
	return true;
}

function validateFormMatch(blnValid, objFormField, objFormField2, strAlert) {
	if (blnValid) {
		if (objFormField) {
			if (objFormField.value.replace(" ", "") != objFormField2.value.replace(" ", "")) {
				if (strAlert != "") { alert(strAlert); }
				objFormField.value = "";
				objFormField2.value = "";
				objFormField.focus();
				return false;
			}
		}
	} else { return false; }
	return true;
}

function validateForm(theForm) {
	var blnValid = true;
	if (theForm.submit.value != "") {
		blnValid = validateFormField(blnValid, theForm._01_Name, "Please enter your name!");
		blnValid = validateFormField(blnValid, theForm._02_Address, "Please enter your address!");
		blnValid = validateFormField(blnValid, theForm._03_City, "Please enter your city!");
		blnValid = validateFormField(blnValid, theForm._04_State, "Please enter your state!");
		blnValid = validateFormField(blnValid, theForm._05_Zip, "Please enter your zip code!");
		blnValid = validateFormField(blnValid, theForm._06_Phone, "Please enter your phone number!");
		blnValid = validateFormField(blnValid, theForm._07_Email, "Please enter your email address!");
		blnValid = validateFormMatch(blnValid, theForm._07_Email, theForm.sender, "Your email addresses did not match!");
		blnValid = validateFormField(blnValid, theForm.str1, "Please enter the validation code!");
		blnValid = validateFormMatch(blnValid, theForm.str1, theForm.str2, "Please enter the validation code!");
	} return blnValid;
}

function validate(theForm) {
	var blnValid = true;
	if (theForm.submit.value == "cartContinue") {
		blnValid = validateFormField(blnValid, theForm.strFirstName, "Name is required!");
		blnValid = validateFormField(blnValid, theForm.strLastName, "Name is required!");
		blnValid = validateFormField(blnValid, theForm.strDayPhone, "Phone number is required!");
		blnValid = validateFormField(blnValid, theForm.strEmail, "Email address is required!");
		blnValid = validateFormField(blnValid, theForm.strCardNumber, "Credit card number is required!");
		blnValid = validateFormField(blnValid, theForm.strExpMonth, "Expiration date is required!");
		blnValid = validateFormField(blnValid, theForm.strExpYear, "Expiration date is required!");
		blnValid = validateFormField(blnValid, theForm.strBillAddress, "Billing address is required!");
		blnValid = validateFormField(blnValid, theForm.strBillCity, "Billing address is required!");
		blnValid = validateFormField(blnValid, theForm.strBillState, "Billing address is required!");
		blnValid = validateFormField(blnValid, theForm.strBillZip, "Billing address is required!");
		blnValid = validateFormField(blnValid, theForm.strShipFirstName, "Shipping name is required!");
		blnValid = validateFormField(blnValid, theForm.strShipLastName, "Shipping name is required!");
		blnValid = validateFormField(blnValid, theForm.strShipAddress, "Shipping address is required!");
		blnValid = validateFormField(blnValid, theForm.strShipCity, "Shipping address is required!");
		blnValid = validateFormField(blnValid, theForm.strShipState, "Shipping address is required!");
		blnValid = validateFormField(blnValid, theForm.strShipZip, "Shipping address is required!");
	}						
	return blnValid;
}

function useSameAddress(theForm) {
	if (theForm.blnSameAddress.checked) {
		theForm.strShipStateOther.style.display = theForm.strBillStateOther.style.display;
		theForm.strShipFirstName.value = theForm.strFirstName.value;
		theForm.strShipMiddleInitial.value = theForm.strMiddleInitial.value;
		theForm.strShipLastName.value = theForm.strLastName.value;
		theForm.strShipAddress1.value = theForm.strBillAddress1.value;
		theForm.strShipAddress2.value = theForm.strBillAddress2.value;
		theForm.strShipCity.value = theForm.strBillCity.value;
		theForm.strShipState.value = theForm.strBillState.value;
		theForm.strShipStateOther.value = theForm.strBillStateOther.value;
		theForm.strShipZip.value = theForm.strBillZip.value;
	}
}

function resizeHeight(strTarget, strElement, intProductID, intModify) {
	if (docElement(strTarget) && docElement(strElement)) {
		strTarget = docElement(strTarget);
		strElement = docElement(strElement);
		var intResize = strElement.offsetHeight + intModify;
		var sAgent = getAgent();
		if (sAgent == "IE6" || sAgent == "IE7") {
			intResize -= 2;
		} else if (sAgent == "IE5") {
			intResize += 18;
		} else {
			intResize = intResize;
		}
		if (strTarget.offsetHeight - 20 < intResize) {
			if (docElement("product"+intProductID+"Title").offsetHeight > 30) {
				intResize -= 13;
			}
			strTarget.style.height = intResize + "px";
		}
	}
}

$(document).ready(function() {
    $("div.nav_about").hide();
    $("li.nav2> a.navlink").click(function() {
		$(this).next("div").slideToggle("fast");
		if (docElement("nav_about").offsetHeight >= 15) {
			docElement("nav2_link").className = "navlink";
		} else {
			docElement("nav2_link").className = "navlink subactive";
		}
    });
});