


function showCartInfoTable()
{
	var cartTable = document.getElementById("cartInfoTable");
	if (cartTable) {
		cartTable.style.display = 'block';
	} else {
		alert ('Shopping cart table not found');
	}
}

function hideCartInfoTable()
{
	var cartTable = document.getElementById("cartInfoTable");
	if (cartTable) {
		cartTable.style.display = 'none';
	} else {
		alert ('Shopping cart table not found');
	}
}


function showCartMsg(message, status)
{
	var cartMsgDisplay = document.getElementById("cartMsgDisplay");
	if (cartMsgDisplay) {
		cartMsgDisplay.innerHTML = message;
		if (status == 1) {
			cartMsgDisplay.style.backgroundColor = '#336699';
		} else if (status == -1) {
			cartMsgDisplay.style.backgroundColor = '#CC3300';
		} else {
			cartMsgDisplay.style.backgroundColor = '#666666';
		}
	} else {
		alert ('Shopping cart message displaying element not found');
		return;
	}
	var cartMsg = document.getElementById("cartMsg");
	if (cartMsg) {
		cartMsg.style.display = 'block';
	} else {
		alert ('Shopping cart message displayer not found');
	}
}

function hideCartMsg()
{
	var cartMsgDisplay = document.getElementById("cartMsgDisplay");
	if (cartMsg) {
		cartMsgDisplay.innerHTML = '&nbsp;';
		cartMsgDisplay.style.backgroundColor = '#666666';
	} else {
		alert ('Shopping cart message displaying element not found');
		return;
	}
	var cartMsg = document.getElementById("cartMsg");
	if (cartMsg) {
		cartMsg.style.display = 'none';
	} else {
		alert ('Shopping cart message displayer not found');
	}
}


function disableCartForm()
{
	var frm = document.forms.cartForm;
	//frm.zipCode.disabled = true;
	frm.quantity.disabled = true;
	frm.couponCode.disabled = true;
}


function enableCartForm()
{
	var frm = document.forms.cartForm;
	//frm.zipCode.disabled = false;
	frm.quantity.disabled = false;
	frm.couponCode.disabled = false;
}


function addToCart()
{
	if (typeof (FormProcessing) == 'undefined')
		FormProcessing = false;
	if (!FormProcessing) {
		var frm = document.forms.cartForm;
		var zip_code = '' /*frm.zipCode.value*/;
		var quantity =  frm.quantity.value;
		var coupon_code = frm.couponCode.value;
		var tmp = readCookie('shuffle_up_playing_cards_client_id');
		var client_id = tmp ? tmp : '';
		var action = 'Add';
		var productId = frm.ProductId.value;
		//
		FormProcessing = true;
		disableCartForm();
		showCartMsg("Processing shopping cart data..", 0);
		showCartInfoTable();
		sendInfo(zip_code, quantity, coupon_code, client_id, productId,action);
	} else {
		return;
	}
}


function updateCart()
{
	if (typeof (FormProcessing) == 'undefined')
		FormProcessing = false;
	if (!FormProcessing) {
		var frm = document.forms.cartForm;
		var zip_code =  '' /* frm.zipCode.value */;
		var quantity =  frm.quantity.value;
		var coupon_code = frm.couponCode.value;
		var tmp = readCookie('shuffle_up_playing_cards_client_id');
		var client_id = tmp ? tmp : '';
		var action = 'Update';
		var productId = frm.ProductId.value;
		//
		FormProcessing = true;
		disableCartForm();
		showCartMsg("Processing shopping cart data..", 0);
		showCartInfoTable();
		sendInfo(zip_code, quantity, coupon_code, client_id, productId,action);
	} else {
		return;
	}
}

function sendInfo(zip_code, quantity, coupon_code, client_id, productId, action)
{
	   
	   var oXmlHttp = zXmlHttp.createRequest();
	   var sURL = 'update_cart.php?ClientId='+client_id+'&ZipCode='+escape(zip_code)+'&Quantity='+escape(quantity)+'&CouponCode='+escape(coupon_code)+ '&ProductId=' + productId +'&Action=' + action + '&requestId=' + createId();
		//alert(sURL);
		/*document.getElementById("debug").innerHTML = sURL;
		return ;*/
		oXmlHttp.open("get", sURL , true);
	    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
               receiveInfo(oXmlHttp.responseText);
            } else {
				
                showCartMsg("An error occurred: "+ oXmlHttp.statusText, -1);
			}
         }
 	   };
   	   oXmlHttp.send(null);
}


function receiveInfo(str)
{
	//alert(str);
	try {
		eval(str);
	} catch (e) {
		showCartMsg("An error occurred: "+ "could not parse processed data", -1);
	}
	if (typeof(receivedInfo) != 'undefined') {
		
		if (typeof (receivedInfo.Error) != 'undefined' && receivedInfo.Error) {
			showCartMsg("Internal database error:" + receivedInfo.Message, -1);
			enableCartForm();
			FormProcessing = false;
			return;
		}
		
		if (document.getElementById("showEach"))
			document.getElementById("showEach").innerHTML = receivedInfo.Each;
		else
			alert("'showEach' Div not found");

		if (document.getElementById("showSubtotal"))
			document.getElementById("showSubtotal").innerHTML = receivedInfo.Subtotal;
		else
			alert("'showSubtotal' Div not found");
		/*	
		if (document.getElementById("showTax"))
			document.getElementById("showTax").innerHTML = receivedInfo.Tax;
		else
			alert("'showTax' Div not found");
		*/	
		/*if (document.getElementById("showShipping"))
			document.getElementById("showShipping").innerHTML = receivedInfo.Shipping;
		else
			alert("'showShipping' Div not found");*/
		/*	
		if (document.getElementById("showTotal"))
			document.getElementById("showTotal").innerHTML = receivedInfo.Total;
		else
			alert("'showTotal' Div not found");
		*/
		var tmpi = parseInt(receivedInfo.Quantity);
		if (isNaN(tmpi)) {
			tmpi = 0;
		}
		var tmp =  (tmpi > 1) ? "SHOPPING CART: "+tmpi+" ITEMS" : "SHOPPING CART: "+tmpi+" ITEM";
		
		document.getElementById("showItemTotal").innerHTML = tmp;
		
		var frm = document.forms.cartForm;
		/*frm.zipCode.value = receivedInfo.ZipCode;*/
		frm.quantity.value = receivedInfo.Quantity;
		frm.couponCode.value = receivedInfo.CouponCode;
		
		showCartMsg("Shopping cart was updated", 1);
		enableCartForm();
		FormProcessing = false;
} else {
		showCartMsg("An error occurred: "+ "Processed data is invalid", -1);
		enableCartForm();
		FormProcessing = false;
	}
}

function checkout()
{
	window.location.href = "check_out.php?requestId=" + createId();
}

function createId()
{
	var cdate = new Date();
	var randomStr = '' + Math.random();
	var randomNo = parseInt(randomStr.replace("0.", "6")) + cdate.getHours() + cdate.getMinutes() + cdate.getSeconds();
	return randomNo;
}