//AJAX for Adding an item into shopping cart

var isWorking = false;
var http 	= getHTTPObject();
var test = 1

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
	
function AddToCart(sSKU)
{
	//alert("AddToCart");
	var sURL = "AddToCart.asp";
	var params = "Action=add" + "&SKU=" + sSKU;
	
	//check if the SKU comes w any dropdown option
	var hdnSKUQuals = document.getElementById('hdnSKUQuals' + sSKU);
	
	//if it does, pass it in
	if (hdnSKUQuals != null)
	{
		params = params + "&SKUQuals=" + hdnSKUQuals.value;
		//alert (sURL);
	}
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged;
		http.send(params);
		isWorking = true;
	}
}

function RemoveFromCart(sSKU)
{
	//alert("AddToCart");
	var sURL = "AddToCart.asp";
	var params = "Action=del" + "&SKU=" + sSKU;
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged_v2;
		http.send(params);
		isWorking = true;
	}
}

function EmptyCart()
{
	//alert("AddToCart");
	var sURL = 'AddToCart.asp?Action=emp';
	var params = "Action=emp";
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged_v2;
		http.send(params);
		isWorking = true;
	}
}

function UpdateSKUInCart(sSKU, iQty, iPgNum)
{
	var sURL = 'AddToCart.asp'; 
	var params = "Action=upd&SKU=" + sSKU + "&Qty=" + iQty + "&Page=" + iPgNum; 
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged_v2;
		http.send(params);
		isWorking = true;
	}
}

function GetShoppingCart(iPage)
{
	var sURL = 'AddToCart.asp';
	var params = "";

	if (iPage != null)
		params = "page=" + iPage;
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged;
		http.send(params);
		isWorking = true;
	}
}

function UpdateCart(iPageNum)
{
	var sSKUs = $('input:hidden[name=hdnSKU]').val();
	var aSKUs = sSKUs.split(',');
	var iNewQty = 0;
	var iOldQty = 0;
	var sQty = ''
	
	sSKUs = ''
	
	for (var i = 0; i < aSKUs.length; i++)
	{
		iOldQty = $('input:hidden[name=hdnOrigQty' + aSKUs[i] + ']').val();
		iNewQty = $('input:text[name=txtQty' + aSKUs[i] + ']').val(); 
		//update only when the qty has been changed
		if (iOldQty != iNewQty)
		{
			sSKUs = sSKUs + aSKUs[i] + ',';
			sQty = sQty + iNewQty + ',';
		}
	}
	
	UpdateSKUInCart(sSKUs, sQty, iPageNum)
}

function stateChanged()
{
	var oShopCart = document.getElementById("divShopCart");
	
	if (http.readyState == 4)
	{
		//if the shoppingcart is currently displayed, don't hide it
		//*Note: i think on the 1st load, the style.display is null, that's why i check if it's block
		if (oShopCart.style.display == 'block')
		{

		}
		else
		{
			oShopCart.style.display = 'block';
		}
		
		$('#divShopCart').html(http.responseText);
		//display iframe to fix IE6 dropdown issue
		ShowFrame();
		
		isWorking = false;
		
		SetFocusToHeader();
	}
}

function stateChanged_v2()
{
	//Doesn't toggle the shopping cart box
	
	if (http.readyState == 4)
	{
		$('#divShopCart').html(http.responseText);
		isWorking = false;
		
		SetFocusToHeader();
	}
}

function SetFocusToHeader()
{
	//to set the focus on the Shopping Cart
	location.href = "#aShoppingCart";
}

function ToogleShoppingCart()
{
	var sShopCart = $('#divShopCart').text();
	
	sShopCart = jQuery.trim(sShopCart);
	
	//if Shopping Cart has been set (not blank)
	if (sShopCart != "")
	{
		$('#divShopCart').toggle();
		$('#iframe').toggle();
	}
	else
	{
		//if the Shopping Cart has not been set (rendered?), need to grab the content first
		GetShoppingCart();
	}
}

//To fix IE6 dropdown render error, this is implemented. The Iframe is located in header09.txt
function ShowFrame() {
    var layer = document.getElementById('divShopCart');

    // show IFRAME
    var iframe = document.getElementById('iframe');
	if (iframe != null)
	{
		iframe.style.display = 'block';
		iframe.style.width = layer.offsetWidth;
		iframe.style.height = layer.offsetHeight;
		iframe.style.left = layer.offsetLeft;
		iframe.style.top = layer.offsetTop;
	}
}

function HideFrame() {
    // hide IFRAME
    var iframe = document.getElementById('iframe');
	if (iframe != null)
	{	
		iframe.style.display = 'none';
	}
}

function IntegerFilter(objectName, iLimit)
{
	var objectValue = $('input:text[name=' + objectName + ']').val(); 
	
	var cLastChar = objectValue.charAt(objectValue.length - 1);
	
	if (iLimit == null)
		iLimit = 0;
	
	//only accepts numeric (positive integer)
	if (!((cLastChar == "0") || (cLastChar == "1") || (cLastChar == "2") || (cLastChar == "3") || (cLastChar == "4") || (cLastChar == "5") || (cLastChar == "6") || (cLastChar == "7") || (cLastChar == "8") || (cLastChar == "9") || (cLastChar == "")))
	{
	   $('input:text[name=' + objectName + ']').val(objectValue.substr(0, objectValue.length - 1));
	}
	else
	{
		//make sure the qty entered is not greater than the limit qty (if available)
		if ((parseInt(iLimit) > 0) && (parseInt(objectValue) > parseInt(iLimit)))
		{
			$('input:text[name=' + objectName + ']').val(iLimit);
		}
	}
} 


//to set the SKUQuals string for dropdown menu options (if available for the SKU)
//the dropdowns are created in \ASPmodules\mod.functions.asp - ShowBundleOption  and  ShowBundleList
function OnDropdownChange(sSKU, oDDL, iID)
{
	//the SKUQuals format should be: ID - ID - ID
	//by default, taking the place of ID is *
	
	var iIndex = oDDL.selectedIndex;
	var iValue = oDDL.options[iIndex].value;
	var hdnSKUQuals = document.getElementById('hdnSKUQuals' + sSKU);
	var aSKUQuals = new Array();
	var sSKUQuals = "";
	
	if (hdnSKUQuals != null)
	{
		aSKUQuals = hdnSKUQuals.value.split("-");
		
		for (i = 0; i< aSKUQuals.length; i++)
		{
			if (i == iID)
			{
				//inserting the SSVID in the slot, according to the ID
				sSKUQuals += iValue + "-";
			}
			else
				sSKUQuals += aSKUQuals[i] + "-";
		}
		
		//to trim out the last '-'
		sSKUQuals = sSKUQuals.substr(0, sSKUQuals.length - 1)
		
		hdnSKUQuals.value = sSKUQuals;
		
		//alert("SKUQuals value: " + sSKUQuals);
	}
}

//to get all possible Shipping Methods for a given ZipCode
function GetShippingMethods()
{
	var sZipCode = $('input:text[name=txtZip]').val();

	//alert("ZIP:" + sZipCode);
	
	var sURL = 'AddToCart.asp';
	var params = "";
	
	if (sZipCode != "")
		params = 'ZipCode=' + sZipCode;
	
	if (!isWorking && http) {
		http.open('POST', sURL ,true);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http.setRequestHeader("Content-Length", params.length);
		http.onreadystatechange = stateChanged;
		http.send(params);
		isWorking = true;
	}	
}

function UpdateTotal()
{
	var dSalesTax = $('input:hidden[name=hdnSalesTax]').val();
	var dSubTotal = $('input:hidden[name=hdnSubTotal]').val();
	var dShipCost = $('select[name=ddlShipMethod]').val();
	var dTotal = 0;
	
	//sales tax may not always be available (for state other than CA)
	if (dSalesTax == null)
	{
		dSalesTax = 0;
	}
	
	//make sure ShipCost is valid, if not just set to 0
	if ((dShipCost == "") || (dShipCost == null))
	{
		dShipCost = 0;
	}
	
	dTotal = parseFloat(dSalesTax) + parseFloat(dSubTotal) + parseFloat(dShipCost);
	
	$('#divGrandTotal').html(dTotal.toFixed(2));
}

function onEnterZipCode(e)
{ 
	if(e.keyCode == 13 )
	{
		//alert("test");
		GetShippingMethods();
	}
}

function onEnterUpdateQty(e, iPgNum)
{ 
	if(e.keyCode == 13 )
	{
		//alert("test");
		UpdateCart(iPgNum);
	}
}
