// JavaScript Document
var cartbody = Object;
var types = {original: "Skatelite Original", pro: "Skatelite Pro", occ: "Skatelite OCC"};

function body_onload() {
	//initialize the cart
	BSCart.initialize();
	
	preload_images(	"images/button_skatlite_original_over.png",
				   	"images/button_skatlite_pro_over.png",
					"images/button_skatlite_occ_over.png",
					"images/header_skatelitelogo_over.png",
					"images/button_updatecart_over.png",
					"images/button_updatecart_click.png",
					"images/button_loading.png",
					"images/button_remove_over.png",
					"images/button_remove_click.png",
					"images/button_cart_over.gif",
					"images/button_continue_over.png",
					"images/button_login_over.png",
					"images/button_orderstatus_over.png",
					"images/button_vieworder_over.png",
					"images/button_printableversion_over.png",
					"images/header_outletsale_over.png",
					"images/header_shippingquote_over.png");
	
	if (typeof(content_onLoad) == "function")
		content_onLoad();
	
	if (typeof(trackOrder) == "function")
		trackOrder();
	

	if (document.getElementById("cartContents_original"))
	{
		for (var key in types)
		{
			cartbody[key] = document.getElementById("cartContents_"+key);
		}
		
		updateCart();
		updateQuantities();
	} else {
		updateMinicart();
	}

}

function updateQuantities()
{
	
	for(var i = 0; document.getElementById("product_"+i+"_quantity") != null; i++)
	{
		id = "product_"+i;
		count = howMany(document.getElementById(id+"_type").value,
						document.getElementById(id+"_color").value,
						document.getElementById(id+"_size").value);
		
		document.getElementById("product_"+i+"_quantity").value = count;
	}	
}

var img = [];
function preload_images() {
	for (var i=0; i<arguments.length;i++)
	{
		url = arguments[i];
		if (!img[url])
		{
			img[url] = new Image;
			img[url].src = url;
		}
	}
}

function addToCart(id)
{
	if (document.getElementById(id+"_quantity").value > 0)
	BSCart.addItem(document.getElementById(id+"_sku").value, Number(document.getElementById(id+"_quantity").value));
}

function updateCart()
{
	//empty the cart contents div
	for (var key in cartbody)
	{
		while (cartbody[key].childNodes.length > 0) {
			cartbody[key].removeChild(cartbody[key].childNodes[0]);
		}
	}
	
	for (var key in BSCart.items)
	{
		var thisitem = BSCart.items[key];
		var newitem = createLineItem(key, thisitem);
		
		cartbody[thisitem.type].appendChild(newitem);
	}

	for (key in cartbody)
	{
		if (cartbody[key].childNodes.length == 0)
		cartbody[key].appendChild(createLineItem(-1,{none:"none"}));
	}

	if (BSCart.getShipping() === null) shippingCost = "...";
	else shippingCost = "$"+Number(BSCart.getShipping()).toFixed(2);
	document.getElementById("cartShipping").childNodes[0].nodeValue = shippingCost;
	
	if (BSCart.getTaxAmount() === null) taxCost = "...";
	else taxCost = "$"+Number(BSCart.getTaxAmount()).toFixed(2);
	document.getElementById("cartTax").childNodes[0].nodeValue = taxCost;
	
	document.getElementById("cartTotal").childNodes[0].nodeValue = "$"+Number(BSCart.getTotal()).toFixed(2);

	updateMinicart();
}

function updateMinicart()
{
	document.getElementById("infobarTotal").childNodes[0].nodeValue = "$"+Number(BSCart.getTotal()).toFixed(2);
	document.getElementById("infobarQty").childNodes[0].nodeValue = BSCart.getQuantity();
}

function updateType(type)
{
	//remove all items of this type
	for(var index in BSCart.items)
	{
		thisitem = BSCart.items[index];
		if (thisitem.type == type) BSCart.removeItem(index);
	}
	
	for(var i = 0; document.getElementById("product_"+i+"_quantity") != null; i++)
	{
		addToCart("product_"+i);
	}

	updateCart();
}

function howMany(type, color, size)
{
	var count = 0;
	for(index in BSCart.items)
	{
		var thisitem = BSCart.items[index];
		if (thisitem.type == type &&
			thisitem.size == size &&
			thisitem.color == color)
			count += Number(thisitem.quantity);
	}
	return count;
}

function removeFromCart(id)
{
	BSCart.removeItem(id.substr(7));
	updateCart();
	updateQuantities();
	if (btn = document.getElementById("confirmButton"))
	{
		btn.style.display = "block";
	}
}

function createLineItem(id, data)
{
	var newitem = document.createElement("tr");
	newitem.className = "cartitem";
	for(key in data)
	{
		if (key == "type" || key == "sku") continue;
		
		if (key == "price") {
			if (typeof(noRemoveButtons) == "undefined" || !noRemoveButtons) {
				//add a remove button before price
				cell = document.createElement("td");
				cell.className = "removeButton";
				//clone sample removeButton, change id
				var removeButton = document.getElementById("removeButton").cloneNode(true);
				removeButton.id = "remove_"+id;
				removeButton.className = "removeButton";
				cell.appendChild(removeButton);
				newitem.appendChild(cell);
			}
			
			//text for price includes a dollar sign
			text = "$" + Number(data[key]).toFixed(2);
		} else text = data[key];
		
		newtext = document.createTextNode(text);
		cell = document.createElement("td");
		cell.className = "cartCell_" + key;
		cell.appendChild(newtext);
		newitem.appendChild(cell);
	}
	
	
	return newitem;
}

function addListener(elem, type, fn){
	if (elem.addEventListener) elem.addEventListener(type, fn, false);
	else elem.attachEvent('on' + type, fn);
}

function strrev(inp) {
 var outp = "";
 for (i = 0; i <= inp.length; i++)
	 outp = inp.charAt (i) + outp;
 return outp;
} 

function writemail(user, domain, tld, innerElement)
{
 var garbage = "!Fjji68@Mfmki#"
 var at = garbage.charAt(7);
 var nothing = "";
 var eadd = user + at + domain;
 eadd = eadd + "." + strrev(tld);
 document.write("<a href='mai");
 document.write(nothing + "lto");
 document.write(":"+eadd+"' class='emailLinks'>");
 document.write((innerElement||eadd) + "</a>")
}
