//v8 - new pricelabels
var pageLoaded = false;
var ie = (navigator.appName.indexOf('Microsoft Internet Explorer')>-1);
var ns = (navigator.appName.indexOf('Netscape')>-1);
var catSelect = new Array();
var catPending = new Array();

function initCats() {
	for (i=0;i<topCatCnt;i+=1) {
		catSelect[i] = false;
		catPending[i] = false;
	}
}


if (document.images) {
	 
	 checkbox_checked = new Image;
	 checkbox_unchecked = new Image;
	 
	 checkbox_checked = '/images/checkboxchecked.gif';
	 checkbox_unchecked = '/images/checkboxunchecked.gif';
	 
	 selectedCat = new Image;
	 deselectedCat = new Image;
	 
	 selectedCat.src = "/images/selectedCat.gif";
	 deselectedCat.src = "/images/deSelectedCat.gif";
 }
 
var offmenuPending = "";
var onmenuPending = "";
var subMenuDisplayed = "";	// indicates the submenu that is currently displayed
var showPending = "";	// indicates that the specified subcat menu is being expanded.
var hidePending = "";   // indicates that the specified subcat menu is being hidden.
var enableSubMenu = true;	// flag used to turn off submenus of small display

function hiliteCat(cat) {
	document.getElementById("ci_"+cat).src = selectedCat.src;
}
function unhiliteCat(cat) {
	document.getElementById("ci_"+cat).src = deselectedCat.src;
}

function click_cat(catlnk,catCnt) {
	if(catSelect[catCnt] || !enableSubMenu) {window.location = catlnk+'?tcc='+catCnt; return false;}
	for (i=0;i<=topCatCnt;i+=1) {
		if(catSelect[i] || catPending[i]) {hide_menu(i);}
	}
	show_menu(catCnt);
	return false;
}

function clickSubCat(searchlnk,topcatcnt) {
	window.location = searchlnk + '?tcc='+topcatcnt;
	return false;
}

function showDebug(fn,cat) {
//console.log(fn+'('+cat+'):onp='+onmenuPending+',offp='+offmenuPending+',sm='+subMenuDisplayed+',sp='+showPending+',hp='+hidePending);
}
var extraHeight = 2;	//extra height for submenu container so it's not clipped at the bottom 12
function show_menu(catCnt) {
	if (!pageLoaded) {return;}
	catPending[catCnt] = true;
	containerElement = document.getElementById('container_'+catCnt);
	var clipID = 'clip_'+catCnt;
	subCatHeight = document.getElementById(clipID).offsetHeight + extraHeight;
	clipElement = document.getElementById(clipID);
	clipElement.style.marginTop = "0px";
	clipElement.currentMargin = 0;
	doBlindUp(containerElement,0,subCatHeight,animSteps,animTime,easePwr);
}

function hide_menu(catCnt) {
//	if (catPending[catCnt]) {setTimeout("hide_menu("+catCnt+");",100);showDebug('hide_menu_delay',catCnt);return;}
	//if (!catSelect[catCnt]) {showDebug('hide_menu_catNotDisplayed',catCnt);return;}	
//	console.log('hide_menu:'+cat);
	containerElement = document.getElementById('container_'+catCnt);
	var clipID = 'clip_'+catCnt;
	subCatHeight = document.getElementById(clipID).offsetHeight + extraHeight;
	clipElement = document.getElementById(clipID);
	clipElement.style.marginTop = "0px";
	clipElement.currentMargin = 0;
	doBlindUp(containerElement,subCatHeight,0,animSteps,animTime,easePwr);	
}
 
var easePwr = 0.7;var animSteps = 15;var animTime = 20; 

function doBlindUp(elem,startHeight,endHeight,steps,intervals,epwr) { 
    if (elem.blindUpInt) window.clearInterval(elem.blindUpInt);
    var actStep = 0;
    elem.blindUpInt = window.setInterval(
	function() { 
	  elem.currentHeight = stepGen(startHeight,endHeight,steps,actStep,epwr);
	  elem.style.height = elem.currentHeight + "px"; 
	  actStep++;
	  if (actStep > steps) {
	  	window.clearInterval(elem.blindUpInt);
		if (elem) {
			var ccnt = parseInt(elem.id.replace('container_',''));
			if (startHeight==0) {
				catPending[ccnt] = false;
				catSelect[ccnt] = true;
			} else {
				catPending[ccnt] = false;
				catSelect[ccnt] = false;
			}
		}
		
	  }
	} 
	,intervals)
}

function stepGen(minValue,maxValue,totalSteps,actualStep,epwr) {
	var delta = maxValue - minValue;
	var steptmp = minValue+(Math.pow(((1 / totalSteps)*actualStep),epwr)*delta);
	return Math.ceil(steptmp)
}	
	
function calcHeight() {
	var sh;
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		sh=16
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		sh=20
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		sh=20
	}
	if (myWidth<960){myHeight = myHeight-sh;}
}

function homepageLoad() {
	f('fixSpacer');
	setTimeout("f('runRotateBanner')",3000);
	if (document.getElementById("slidingBanner")) {setTimeout("f('slideBanner','auto')",2000);}
	if(/msie|MSIE 6/.test(navigator.userAgent)) {


	}
	if(/msie|MSIE 7/.test(navigator.userAgent)) {

	}
}



function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}


var sliding = true;
var slideOffset = 0;

function pauseSlide() {
	sliding = false;
}

function playSlide() {
	if (!sliding) {sliding = true;}
}

var slideLock = false;
function releaseSlideLock() {
	slideLock = false;}


function slideBanner(direction) {
	if(sliding || direction != "auto") {
		if(slideLock) {if(direction=="auto"){setTimeout("slideBanner('auto');",500);}return;}
		slideLock = true;
		setTimeout("releaseSlideLock()",1000);
//		load back images
		var offsetFix;
		document.getElementById("slidingBack").style.left="-230px";
		for (i=0;i<5;i++) {
			offsetFix = 0;
			if (slideOffset+i-1 > numSliding-1) {offsetFix = -numSliding;}
			if (slideOffset+i-1 < 0) {offsetFix = numSliding;}
			document.getElementById("slidingBack"+i).style.backgroundImage = "url(/productImages/promoImages/Image" + slidingBannerImage[slideOffset+offsetFix+i-1] +".jpg)";
			if (i >= 2) {
				document.getElementById("bannerform"+(i+4)).action = slidingBannerLink[slideOffset+offsetFix+i-1];
				document.getElementById("slidingbanner"+(i-1)).value = slidingBannerImage[slideOffset+offsetFix+i-1];
			}
		}
		document.getElementById("slidingBack").style.visibility = "visible";
		document.getElementById("slidingFront").style.visibility = "hidden";
		if (direction == "left" || direction=="auto") {
			slideOffset++
			if(slideOffset>=numSliding) {slideOffset = 0;}		
			doSlide(document.getElementById("slidingBack"),-230,-475,15,40,0.7)
			setTimeout("swapSlideFront('left')",1000);
		}
		if (direction == "right") {
			slideOffset--
			if(slideOffset<0) {slideOffset = numSliding-1;}		
			doSlide(document.getElementById("slidingBack"),-230,15,15,40,0.7)
			setTimeout("swapSlideFront('right')",1000);
		}
		if (direction == "auto") {setTimeout("slideBanner('auto');",3000);}
	} else {
		setTimeout("slideBanner('auto');",500);
	}	
}

function slideLeft() {
	doSlide(document.getElementById("slidingBack"),-230,-435,15,40,0.7)
}
function slideRight() {
	doSlide(document.getElementById("slidingBack"),-230,15,15,40,0.7)
}

function swapSlideFront(direction) {
	if(direction == "left" || direction=="auto"){
		for (i=0;i<3;i++) {document.getElementById("slidingFront"+(i+1)).style.backgroundImage = document.getElementById("slidingBack"+(i+2)).style.backgroundImage;}
	}
	if(direction == "right"){
		for (i=0;i<3;i++) {document.getElementById("slidingFront"+(i+1)).style.backgroundImage = document.getElementById("slidingBack"+i).style.backgroundImage;}
	}
	document.getElementById("slidingBack").style.visibility = "hidden";
	document.getElementById("slidingFront").style.visibility = "visible";
	for (i=0;i<3;i++) {
		offsetFix = 0;
		if (slideOffset+i-1 > numSliding-1) {offsetFix = -numSliding;}
		if (slideOffset+i-1 < 0) {offsetFix = numSliding;}
		document.getElementById("bannerform"+(i+5)).action = slidingBannerLink[slideOffset+offsetFix+i-1];
		document.getElementById("slidingbanner"+(i)).value = slidingBannerImage[slideOffset+offsetFix+i-1];
	}
}
function doSlide(elem,startLeft,endLeft,steps,intervals,epwr) { 
    if (elem.slideInt) window.clearInterval(elem.slideInt);
    var actStep = 0;
    elem.slideInt = window.setInterval(
	function() { 
	  elem.currentLeft = stepGen(startLeft,endLeft,steps,actStep,epwr);
	  elem.style.left = elem.currentLeft + "px"; 
	  actStep++;
	  if (actStep > steps) window.clearInterval(elem.slideInt);
	} 
	,intervals)
}

function runSlideBanner() {
	slideBanner("left");
}
function bannerSubmit(n) {
	document.getElementById("bannerform"+n).submit();
}
/* Gift Certificate */
function selectStyle(inum) {
	if (inum == "undefined") {
		inum = document.getElementById("gcstyle").selectedIndex;
	}
	document.getElementById("prodImage").src = "/images/gift"+inum+".jpg";
	document.getElementById("gcstyle").selectedIndex = inum;
	
}

/* Product Detail */
var pd_lowas = "<div class='pricetext3'>Low as&nbsp;</div>";
var pd_salelowas = "<div class='pricetext1'>Sale<br /><span style='font-size:11px;'>Low as:&nbsp;</span></div>";
var pd_sale = "<div class='pricetext2'>Sale:</div>";
var pd_reg = "";

function priceMatchLoad() {
	if (document.getElementById("tabs")) {document.getElementById("tabs").style.display = "block";}
	if(productCombo && comboCustomize) {
		updatedata();
		if(comboAltSku) {document.getElementById("comboconfignote").style.display = "inline-block";}
	}
	
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = updateSelect;
	xmlHttp.open("GET", "/ajax/stock?p="+manprodcode+"&vt="+varientType, true);		
	xmlHttp.send(null);
	
	xmlHttp2 = GetXmlHttpObject();
	xmlHttp2.onreadystatechange = updateRecentlyViewed;
	xmlHttp2.open("GET", "/ajax/recent?p="+manprodcode, true);		
	xmlHttp2.send(null);
	
	if (store != 'mad') {
		xmlHttp3 = GetXmlHttpObject();
		xmlHttp3.onreadystatechange = updateAlsoBought;
		xmlHttp3.open("GET", "/ajax/alsobought?p="+manprodcode, true);		
		xmlHttp3.send(null);
	}
	
//	document.getElementById("qty").selectedIndex = parseInt(document.getElementById("pminitqty").value);
	if (document.getElementById("partno")) {
		document.getElementById("partno").selectedIndex = parseInt(document.getElementById("pminitpartno").value);
	}
	fixSpacer();
	checkPM();
	discountPrice = ourprice;	//initialize discountPrice.
	if (dailySpecial) {countDown();}
}



function goPriceMatch(mpc) {
	var q = '?q='+document.getElementById("qty").selectedIndex;
	if (document.getElementById("partno")) {
		q = q + '&p='+document.getElementById("partno").selectedIndex;
	}
	window.location=document.getElementById("pricematchlink").href + q;
	return false;
}
function addToCart(sr) {
	document.getElementById("prodform").action = formAction;
	if (!skuInStock && !deposit) {outOfStock();return false;}
	
	if(document.getElementById("partno").value == "") {
		document.getElementById("partno").style.backgroundColor = "#ffff3f";
		alert("Please select " + varientSelectType);
		return false;
	}
	
	if(document.getElementById("qty").value == "" && skuInStock) {
		document.getElementById("qty").style.backgroundColor = "#ffff3f";
		alert("Please select a quantity");
		return false;
	}
	if (sr==3) {inStoreOnly();}
	document.getElementById("prodform").submit(); 
}



function addToCartPriceMatch() {
	document.getElementById("prodform").action = formAction;
	if (!skuInStock) {outOfStock();return false;}
	if(document.getElementById("partno").value == "") {
		document.getElementById("partno").style.backgroundColor = "#ffff3f";
		alert("Please select color and/or size");
		return false;
	}
	if(document.getElementById("qty").value == "" && skuInStock) {
		document.getElementById("qty").style.backgroundColor = "#ffff3f";
		alert("Please select a quantity");
		return false;
	}
	var pmurlo = document.getElementById("pmurl");
	pmurlo.value = pmurlo.value.toLowerCase();
	if(pmurlo.value.substr(0,3) == 'www') {pmurlo.value = 'http://'+pmurlo.value;}
	if(!isURL(pmurlo.value)) {
		pmurlo.style.backgroundColor = "#ffff3f";
		if (pmurlo.value.length > 0) {debugInfo('pmurl='+pmurlo.value);}
		alert("The competitor's website url appears to be invalid or is missing. Please make sure it starts with http:// or https://. ");
		return false;
	}
	var pmpriceo = document.getElementById("pmprice");
	if(!isCurrency(pmpriceo.value)) {
		pmpriceo.style.backgroundColor = "#ffff3f";
		if (pmpriceo.value.length > 0) {debugInfo('pmprice='+pmpriceo.value);}
		alert("The competitor's price appears to be invalid or is missing");
		return false;
	}
	
	for (i=0;i<=nonUSDomains.length;i++) {
		if(pmurlo.value.indexOf(nonUSDomains[i]) != -1 && (parseFloat(pmpriceo.value.replace("$","")) < mip || mip == 0)) {
			if (productCombo || mip == 0 || mip > calculatedPrice) {
				alert("We're sorry, we cannot price-match non-US competitors."); return false;
			} else {
				if (confirm("We cannot price-match non-US competitors. However, we can offer you this product for " + formatCurrency(mip) + ". Click OK to accept this price and add the item to the shopping cart.")) {
				pmpriceo.value = mip;
				checkPM();
				} else {return false;}
			}
		}
	}
	document.getElementById("prodform").submit(); 
}

/* Form inputs */
var currentFocus = '';
var currentOver = '';
function inputFocus(inputID) {
	document.getElementById(inputID).style.backgroundColor = "#ffffff";
	currentFocus = inputID;
	var formString = document.getElementById(inputID);
	
	if (inputID == 'pmprice') {
		pmprice = formString.value;
		checkQty();
		if(isCurrency(pmprice)) {document.getElementById("instockicon").src="/images/pricematchflag.gif";}
	}
}

function inputBlur(inputID) {
	if (currentFocus == inputID) {currentFocus = '';}
}
function checkPM() {
	formString = document.getElementById("pmprice");
	if (isCurrency(formString.value)) {
		pmprice = formString.value;
		checkQty();
		document.getElementById("instockicon").src="/images/pricematchflag.gif";
	}
}
function inputOver(inputID) {}
var fadeinIncrement = 25;
var holdIncrement = 10;
var fadeoutIncrement = 20;
var waitIncrement = 5;

function overlayOpacity(inputID,mode,percentDone){}
function inputOut(inputID) {}

/* Review */
var overallDescription = new Array('Click Star to Rate','Terrible','Mediocre','Average','Very Nice','Great!');
//var valueDescription = new Array('Click Star to Rate','Save Your Money','Kind of Pricey','Reasonable','Good Deal','A Steal!');

function checkRating() {
	starOutO();
	ratingO(overallRating);
}

function reviewSubmit() {
	window.resizeTo(520,300);
}

function starHoverO(rating) {
	for (i=1;i<=5;i++) {
		if (rating >= i) {
			document.getElementById("Overall"+i).src = "/images/fullstar.gif";
			
		} else {
			document.getElementById("Overall"+i).src = "/images/hollowstar.gif";
		}
	}
	document.getElementById("rv-OverallDescribe").innerHTML = overallDescription[rating];
}
function starOutO() {
	starHoverO(overallRating);
}
function ratingO(rating) {
	overallRating = rating;
	document.getElementById("overallratinginput").value=rating;
}

function validateReview() {
	if (overallRating == 0) {
		alert("Please Rate this product before submitting your review");
		return false;
	}	
	if (document.getElementById("rv-summarytext").value.length == 0){
		alert("Please include a Review Headline before submitting your review");
		return false;
	}
	if (document.getElementById("rv-reviewcomments").value.length == 0){
		alert("Please include Your Opinion before summitting your review");
		return false;
	}
}

/* Contact Us */
var bodydataInit = "Enter your questions or comments here"
function initContactUs() {
	document.getElementById("cs-bodydata").value = bodydataInit;
	document.getElementById("cs-subject").selectedIndex = 0;
	fixSpacer();
}

/*function clearCSBodyData() {
	if (document.getElementById("cs-bodydata").value == "Enter your question or comments here") {
		document.getElementById("cs-bodydata").value = ""
	}
}*/




function clearCSBodyData() {
	if (document.getElementById("cs-bodydata").value.search(bodydataInit) != -1) {
		document.getElementById("cs-bodydata").value = document.getElementById("cs-bodydata").value.replace(bodydataInit,"");
	}
	document.getElementById("cs-bodydata").style.backgroundColor = "#ffffff";
}

function checkBodyData() {
	if (document.getElementById("cs-bodydata").value.length < 4) {document.getElementById("cs-bodydata").value = bodydataInit;}
}

function checkSubject() {
	document.getElementById("cs-subject").style.backgroundColor = "#ffffff";
}

function submitContactUs() {
	var email=document.getElementById("email")
	if(email.value.length == 0) {
		email.style.backgroundImage="url(/images/email_3.gif)";
		alert ("Please enter your email")
		return false;
	}
	
	var re = new RegExp("^[a-zA-Z0-9&\+._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
	if (!email.value.match(re)) {
		email.style.backgroundImage="url(/images/email_3.gif)";
		debugInfo("email="+email.value);
		alert("The email address is not of a valid form for our software. Please check to make sure it was entered correctly.");
		return false;
	}
	
	
	var bodydata = document.getElementById("cs-bodydata");
	if ( bodydata.value == "" || bodydata.value == bodydataInit) {
		bodydata.style.backgroundColor = "#ffff3f";
		bodydata.value = bodydataInit;
		alert("Please enter a question or a comment.");
		return false;
	}
	
	var q = "?e="+email.value+"&s="+URLEncode(document.getElementById("cs-subject").value)+"&b="+URLEncode(bodydata.value);
	if (document.getElementById("cs-dealer")) {
		q = q + "&dn="+URLEncode(document.getElementById("cs-dealer").value);
	}
	if (document.getElementById("cs-contactname")) {
		q = q + "&dc="+URLEncode(document.getElementById("cs-contactname").value);
	}
	if (document.getElementById("cs-contactphone")) {
		q = q + "&dp="+URLEncode(document.getElementById("cs-contactphone").value);
	}
	if (document.getElementById("cs-contactemail")) {
		q = q + "&de="+URLEncode(document.getElementById("cs-contactemail").value);
	}
	if (document.getElementById("cs-location")) {
		q = q + "&dl="+URLEncode(document.getElementById("cs-location").value);
	}
	if (document.getElementById("cs-contacttype")) {
		q = q + "&dt="+URLEncode(document.getElementById("cs-contacttype").value);
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = emailResponse;
	xmlHttp.open("GET", "/ajax/sendcontact"+q, true);		
	// send the request
	xmlHttp.send(null);
	return false;
}

/* View Cart */
function hover(hdiv,mp) {
	if(mp == 'on'){document.getElementById(hdiv).style.textDecoration = "underline";}
	if(mp == 'off'){document.getElementById(hdiv).style.textDecoration = "none";}
}

/* Shipping */



