/* 
---------------------
STORE FUNCTIONS
01. Global Variables
02. Navigation Functions
03. Cart Functions
--------------------- 
*/

/*
----------------------------------------------------------
01. Global Variables
---------------------------------------------------------- */
var initialLoad = true;

var ActiveSubCat = false;
var ActiveCat = true;
var ActiveCatCheck = false;


/*
----------------------------------------------------------
02. Navigation Functions
---------------------------------------------------------- */


startList = function() {
	
	if (document.all&&document.getElementById) {
		
		navRoot = document.getElementById("categories");
		if (navRoot)
		{
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
			
				
				if (node.nodeName=="LI" && node.className != "active") {
					node.onmouseover=function() { 					
						for (i=0; i<this.childNodes.length; i++) {
							if ( this.childNodes[i].nodeName=="UL" ) { this.childNodes[i].className="listStoreSubCatOpen"; }
						}
					};
					node.onmouseout=function() { 
						
						for (i=0; i<this.childNodes.length; i++) {
							if ( this.childNodes[i].nodeName=="UL" ) { this.childNodes[i].className="listStoreSubCat"; }
						}
						
					};
				}
			}
		}
	}
}
window.onload=startList;



/*
----------------------------------------------------------
03. Cart Functions
---------------------------------------------------------- */

function getSkuInfo(skuid) {
	if (initialLoad && document.getElementById('store_addtocart_activeskuID').value.length > 0) {

	} else {
	
		// get Sku Info
		var getSkuInfo2 = function(oXML) {
			
			var skuinfo_array=oXML.responseText.split("|*|");
			
			// Set image
			if ( document.getElementById('storeitem_image').src != skuinfo_array[1] ) {
				document.getElementById('storeitem_image').src = skuinfo_array[1];
			}
			
			// Set price
			document.getElementById('storeitem_price').innerHTML = skuinfo_array[2];
			
			// Set title
			// document.getElementById('storeItem_title').innerHTML = skuinfo_array[3];
			
			// Set on hand
			if ( skuinfo_array[4] == '0' ) {
				document.getElementById('store_product_addtocart_container').style.display = 'none';
				document.getElementById('store_product_addtocart_notavailable_container').style.display = 'block';
			} else {
				document.getElementById('store_product_addtocart_container').style.display = 'block';
				document.getElementById('store_product_addtocart_notavailable_container').style.display = 'none';
			}

		}

		var myConn = new XHConn();
		var random_num = Math.random();
		var pageToConnect = '/assets/includes/ajax_store.aspx';
		var pageVars = 'function=getSkuInfo&rnum=' + random_num + '&skuID=' + skuid ;
		myConn.connect(pageToConnect, 'POST', pageVars, getSkuInfo2);
	}
	initialLoad = false;
}

function setSkuInfoOLD(skuselect) {
	document.getElementById('store_addtocart_skuID').value = $("#storeItem_sku_select").linkselect("val");
	getSkuInfo($("#storeItem_sku_select").linkselect("val"));
}

function setSkuInfo(skuselect) {
	document.getElementById('store_addtocart_skuID').value = skuselect;
	getSkuInfo(skuselect);
}


/*  Sets the active product shown to be based upon the chosen color   */
function setSkuInfo_color(skuselect) {
	document.getElementById('store_addtocart_skuID').value = skuselect;
	getSkuInfo(skuselect);
}

/*  Draws the select box for color variants of SKU's  */
function setSkuInfo_colors() {
	document.getElementById('storeitem_pricebox_color').innerHTML = '<img src="/assets/images/ajax-loader.gif" />';

	// Retrieve colors available for shoe size
	var setSkuInfoShoes2 = function(oXML) {
		//alert('testing...' + oXML.responseText);
		document.getElementById('storeitem_pricebox_color').innerHTML = oXML.responseText;
		document.getElementById('store_addtocart_skuID').value = document.getElementById('cart_select_color').options[document.getElementById('cart_select_color').selectedIndex].getAttribute('skuid');
		getSkuInfo( document.getElementById('cart_select_color').options[document.getElementById('cart_select_color').selectedIndex].getAttribute('skuid') );		
	}
	
	var myConn = new XHConn();
	var random_num = Math.random();
	var pageToConnect = '/assets/includes/ajax_store.aspx';
	var pageVars = 'function=product_getColors&rnum=' + random_num + '&productID=' + document.getElementById('store_addtocart_productID').value + '&skusize=' + document.getElementById('cart_select_size').options[document.getElementById('cart_select_size').selectedIndex].text + '&activeskuID=' + document.getElementById('store_addtocart_activeskuID').value ;
	//alert('testing...' + pageVars);
	myConn.connect(pageToConnect, 'POST', pageVars, setSkuInfoShoes2);
}

function addItemToCart() {
	var addItemToCart2 = function(oXML) {
		var addItemToCart_array=oXML.responseText.split("|*|");
		if (  addItemToCart_array[1] == 'success'  ) {
			// Show added to cart message
			alertAddedToCart();
			// Update Cart total
			getCartTotal();
		} else {

		}
	};
	var myConn = new XHConn();
	var random_num = Math.random();
	var pageToConnect = '/assets/includes/ajax_store.aspx';
	var pageVars = 'function=addItemToCart&rnum=' + random_num + '&productID=' + document.getElementById('store_addtocart_productID').value + '&skuID=' +  document.getElementById('store_addtocart_skuID').value + '&quantity=' + document.getElementById('quantity').value;
	myConn.connect(pageToConnect, 'POST', pageVars, addItemToCart2);
}

function alertAddedToCart() {
	$("h3 span#alert").animate({opacity: "show"}, "slow");
	$("h3 span#alert").animate({opacity: 1.0}, 2500).fadeOut('slow');
}
	
function updateQuantity(cartid,skuid,quantity) {

	var updateQuantity2 = function(oXML) {

		var oArray = oXML.responseText.split("|*|");
		if ( oArray[1] == 'success'  ) { 
			if ( oArray[2].length > 0 ) {
				if ( oArray[2] == '0' ) {
					// Item quantity is 0....remove row
					$("#cartrow_" + cartid).remove();
				} else {			
					document.getElementById('quantity_'+skuid).value = oArray[2]; 
					// Update cartrow total
					document.getElementById("cartrow_total_" + cartid).innerHTML = '$' + (roundNumber((parseInt(oArray[2])) * ( parseFloat(document.getElementById("cartrow_price_" + cartid).innerHTML.split("$")[1]) ), 2))
					if ( document.getElementById("cartrow_total_" + cartid).innerHTML.split('.')[1].length == 1 ) { document.getElementById("cartrow_total_" + cartid).innerHTML += '0'; }
				}
				// Update cart total
				getCartTotal();
			}
		}
	};
	var myConn = new XHConn();
	var pageVars = 'function=updateQuantity&rnum=' + Math.random() + '&skuID=' + skuid + '&quantity=' +  quantity;
	myConn.connect('/assets/includes/ajax_store.aspx', 'POST', pageVars, updateQuantity2);
}

function updateQuantities() {
	for (var i=0;i<$(".quantity").length;i++) {
		if ( $(".cartrow")[i].id.split("_")[1].length > 0 &&  $(".quantity")[i].name.split("_")[1].length > 0 && $(".quantity")[i].value.length > 0 ) {
			updateQuantity( $(".cartrow")[i].id.split("_")[1], $(".quantity")[i].name.split("_")[1], $(".quantity")[i].value );
		}
	}

}

function getCartTotal() {
	var getCartTotal2 = function(oXML) {
		var oArray = oXML.responseText.split("|*|");
		if (  oArray.length > 1  ) { 
		
			if ( document.getElementById('cartTotal') ) { document.getElementById('cartTotal').innerHTML = oArray[1];   }
			if ( document.getElementById('cartSubTotal') ) { document.getElementById('cartSubTotal').innerHTML = oArray[1];   }			
			
		}
	};
	var myConn = new XHConn();
	var pageVars = 'function=getCartTotal&rnum=' + Math.random();
	myConn.connect('/assets/includes/ajax_store.aspx', 'POST', pageVars, getCartTotal2);
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
