/** Common product detail functions **/

// A list of all of the categories that always display the beat it by a buck message.
var bbbCategories = new Array('10801', '10802', '10803', '10805', '10806',
	 '10103', '21304', '21306','20202','20705','21003','20808','30103', 
	 '40402','40403','40407','40103','50203','50107');

function displayBBB(categoryId, price){
	var correctCat = false;
	
	if(parseFloat(price) <= 100){
		for(var x = 0; x < bbbCategories.length; x++){
			if(categoryId == bbbCategories[x])
				correctCat = true;
		}
		
		if(correctCat){
			document.getElementById("bbb").style.display = "inline";
		} else {
			document.getElementById("bbb").style.display = "none";
		}
	} else {
		document.getElementById("bbb").style.display = "inline";
	}
}

// This function determines which BML table and text to display
function displayBML(price) {
	var replacementText;
	// Anything below $100 always gets CORE BML text
	if(parseFloat(price) > 100.00) {
		// When the promo is active and the total is over $250 display the 
		// promo text
		if(document.getElementById("bmlPromoTable") != null 
			&& parseFloat(price) > 250.00) {
			document.getElementById("bmlPromoTable").style.display = "block";
			document.getElementById("bmlTable").style.display = "none";
		} else if (document.getElementById("bmlPromoTable") == null){
			// When the promo isn't active display payments as low as text
			document.getElementById("payfast").style.display = "none";
			var payments = "$";
			var payment = price * .03;
			
			if(payment < 10)
				payment = 10.00
			
			payment = bmlRounding(payment);
			
			payments += payment;
			payments += "/month!";
			document.getElementById("payments").innerHTML = payments;
			document.getElementById("aslowas").style.display = "inline";
			
			document.getElementById("bmlTable").style.display = "block";
			document.getElementById("bmlLink").innerHTML = "Restrictions Apply";
			document.getElementById("bmlLink").href = "javascript:DK_openNewWindow('https://www.securecheckout.billmelater.com/paycapture-content/fetch?hash=G86303P5&content=/bmlweb/np90d0507iw.html','Restrictions','height=500,width=525,resizable=no,scrollbars=yes');"
			
			document.getElementById("bmlTable").style.display = "block";
		} else {
			// When the promo is active, but the amount is < $250 display CORE
			if(document.getElementById("bmlPromoTable") != null)
			document.getElementById("bmlPromoTable").style.display = "none";
			document.getElementById("aslowas").style.display = "none";
			replacementText = "<span class=\"bml_bffs\">";
			replacementText += "Buy Fast.<br/>Feel Secure.<sup>&reg;</sup>";
			replacementText += "</span>";
			document.getElementById("payfast").innerHTML = replacementText;
			document.getElementById("payfast").style.display = "inline";
			document.getElementById("bmlTable").style.display = "block";
			document.getElementById("bmlLink").innerHTML = "Details";
			if(document.getElementById("bmlPromoTable") != null)
				document.getElementById("bmlLink").href = "javascript:DK_openNewWindow('https://www.securecheckout.billmelater.com/paycapture-content/fetch?hash=G86303P5&content=/bmlweb/np90d0507iw.html','Restrictions','height=500,width=525,resizable=no,scrollbars=yes');"
		}
	} else {
		// Display CORE when under $100
		if(document.getElementById("bmlPromoTable") != null)
			document.getElementById("bmlPromoTable").style.display = "none";
		document.getElementById("aslowas").style.display = "none";
		replacementText = "<span class=\"bml_bffs\">";
		replacementText += "Buy Fast.<br/>Feel Secure.<sup>&reg;</sup>";
		replacementText += "</span>";
		document.getElementById("payfast").innerHTML = replacementText;
		document.getElementById("payfast").style.display = "inline";
		document.getElementById("bmlTable").style.display = "block";
		document.getElementById("bmlLink").innerHTML = "Details";
		if(document.getElementById("bmlPromoTable") != null)
			document.getElementById("bmlLink").href = "javascript:DK_openNewWindow('https://www.securecheckout.billmelater.com/paycapture-content/fetch?hash=G86303P5&content=/bmlweb/np90d0507iw.html','Restrictions','height=500,width=525,resizable=no,scrollbars=yes');"
	}
}

// converts the given number to double percision
function doublePercision(numb) {
	numb += "";
	if(numb.indexOf(".") == -1)
		numb = numb.concat(".00");
 	else if(numb.indexOf(".") == numb.length - 1)
		numb = numb.concat("00");
  	else if(numb.indexOf(".") == numb.length - 2)
		numb = numb.concat("0");
	// remove additional percision
	if(numb.indexOf(".") != numb.length - 3) {
		// round up when applicable
		if(numb.substring(numb.indexOf(".") + 3, numb.indexOf(".") + 4) >= 5) {
			numb = parseFloat(numb) + .01;
			numb += "";
		}
		numb = numb.substring(0, numb.indexOf(".") + 3);
	}
		
	return numb;
}

// This function rounds up to the nearest whole number per BML's current specs
function bmlRounding(numb) {
	numb += "";
	if(numb.indexOf(".") != -1){
	    var wholeNumbs = numb.substring(0, numb.indexOf("."));
		var decimals = numb.substring(numb.indexOf("."), numb.length);
		
		if(parseFloat(decimals) > 0){
			wholeNumbs = parseInt(wholeNumbs) + 1
			numb = wholeNumbs;
		}
	}
	numb = doublePercision(numb);
	
	return numb;
}

function determineMaxWidth(){
	var width = 0;
	var skuOpt = document.getElementById("skuOpt");
	for(var x = 0; x < skuOpt.options.length; x++){
		if(width < skuOpt.options[x].text.length){
			width = skuOpt.options[x].text.length;
		}
	}
	return width;
}

function skuListMouseLeave()
{
	if(!skuListOpen) {
		skuListBlur();
	}
}

function skuListClick()
{
	skuListOpen = !skuListOpen;
	if(!skuListOpen) {
		skuListBlur();
	}
}

function skuListBlur()
{
	if(navigator.userAgent.indexOf("MSIE")) {
		document.getElementById("skuOpt").style.width = "165px";
	}
}

//The following function disables the addToCart button, addToWishlist
//button, and the sku drop-down
function disableButtons(){
	document.getElementById("addToCart").disabled = true;
	document.getElementById("addToWishlist").disabled = true;
	document.getElementById("skuOpt").disabled = true;
}

//The following funciton re-enables the buttons and sku dropdown
function enableButtons(){
	document.getElementById("addToCart").disabled = false;
	document.getElementById("addToWishlist").disabled = false;
	document.getElementById("skuOpt").disabled = false;
}

function displayLoadingInSkus(){
	document.getElementById("skuOpt").options[0].text = "Loading...";
	document.getElementById("skuOpt").options[0].selected = "true";
	document.getElementById("productfeaturearea").innerHTML = "Loading...";
}

//This function is for when there are no products to display
function emptyPage(msg){
	var errorMsg;
		document.getElementById("productarea").style.visibility = "hidden";
    errorMsg = 
	    "<span style=\"color: #CC0000; font-weight: bold;\">Your Have Reached This Page In Error.<br/>"+
    "The product you requested does not exist.  Please go back and try again.";
	if(msg != null){
	    errorMsg += "<br/>" + msg;
    }
	errorMsg += "</span>";
	document.getElementById("errorMsg").innerHTML = errorMsg;
}
