var count;
var zero;
var totalitems;
var itemString = "";
var buttons = "<input type='button' class='dia_continueshopping ui-corner-all ui-widget-header' value='Continue Shopping' onclick='closedialog()' />&nbsp;<input type='button' class='dia_checkout ui-corner-all ui-widget-header' value='Checkout' onclick='window.location=\"/action/cart\"' />";

function cursor_wait() {
document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
document.body.style.cursor = 'default';
}

function closedialog(){
$(".ui-icon-closethick").click();
}

// ajax call to add item to cart...
function ajaxAddToCart(sku, itemSku, sizeId, colorId, qty, price, points, discount, discountflag){
	$("#add2Cart").css("opacity", "0.4");
	var color = (document.getElementById(colorId))?document.getElementById(colorId).value:"";
	var size = (document.getElementById(sizeId))?document.getElementById(sizeId).value:"";
	$.ajax({
		url: "/shopaction.php",
		data: "action=addtocart&sku=" + sku + "&itemSku=" + itemSku + "&size=" + size + "&color=" + color + "&qty=" + qty + "&price=" + price + "&points=" + points + "&discount=" + discount + "&discountflag=" + discountflag,
		success: function(msg){
		var test = msg+"";
			//if(test == "ok"){
				$("#dialog").attr("title", "Your cart has been updated");
				$("#dialog").html("Your product has been added to the cart");
				$("#dialog").dialog();
				//alert("Your product has been added to the cart");
				updateFloatingCart();
			/*}else{
				$("#dialog").attr("title", "Error...");
				$("#dialog").html("There has been an error adding this item to the cart:"+msg);
				$("#dialog").dialog();
				//alert("There has been an error adding this item to your cart.");
			}*/
			$("#add2Cart").css("opacity", "1");
		},error: function(err){
			$("#dialog").attr("title", "Cart Error");
			$("#dialog").html("There has been an error updating your cart");
			$("#dialog").dialog();
			$("#add2Cart").css("opacity", "1");
		}
	});
}

function deleteItemFromCart(id){
	$.ajax({
		url: "/shopaction.php",
		data: "action=deletefromcart&itemId=" + id,
		success: function(msg){
			updateFloatingCart(true);
			/*$("#dialog").attr("title", "Item Removed");
			$("#dialog").html("Your cart has been updated");
			$("#dialog").dialog();*/
		}
	});	
}

function updateFloatingCart(show){
	$.ajax({
		url: "/shop/icart.php",
		data: "",
		success: function(msg){
			$("#floatcart").html(msg);
			$(".msg_head").click(function(){
				$(this).next(".msg_body").slideToggle(600);
			});
			if(show){
				$(".msg_body").show();
			}
		}
	});	
}
function checkadd(count){
	//if((count==totalitems) || (count+zero)==totalitems){
		cursor_clear();
		//if(zero==totalitems){
		if(count < 1){
			//alert("fail");
			$("#dialog").attr("title", "Error");
			$("#dialog").html("Please enter a quantity greater than 0");
			itemString = "";
		}else{
			//alert("passed else");
			$("#dialog").attr("title", "Cart Updated");
			$("#dialog").html(itemString+"<br/>item has been added to your cart<br/><br/>"+buttons);
			itemString = "";
		}
		$("#dialog").dialog();
		updateCartInfo();
		updateFloatingCart(true);
	//}else{
	//	setTimeout("checkadd()", 500);
	//}
}

function updateCartInfo(){
	$.ajax({
		url:"/shopaction.php",
		data:"action=cartinfo",
		success: function(msg){
			$("#iteminfo").html(msg);
		}
	});
}

function addProductToCart(e){
	cursor_wait();
//	count = 0;
//	totalitems = 1;

	var sku = $(e).parent().find(".itemSku").val();
	var qty = $(e).parent().find(".qty").val();
	var qty = (qty)?qty:1;
	//alert(sku); //undefined
	//alert(qty);
	
	$.ajax({
		url:"/shopaction.php",
		data:"action=addtocart&itemSku="+sku+"&qty="+qty,
		success: function(msg){
			//var json = eval('('+unescape(msg)+')');
			//itemString+="<br/>"+json.title;
			//count++;
			//checkadd(qty);
		}

	});
	
	//location.href = "/shopaction.php?action=addtocart&itemSku=" + sku + "&qty=" + qty;
	checkadd(qty);
}
function addToCart(sku, itemSku, size, color, qty, price, points, discount, discountflag){
	
	var color = (document.getElementById(color))?document.getElementById(color).value:"";
	var size = (document.getElementById(size))?document.getElementById(size).value:"";

	location.href = "/shopaction.php?action=addtocart&sku=" + sku + "&itemSku=" + itemSku + "&size=" + size + "&color=" + color + "&qty=" + qty + "&price=" + price + "&points=" + points + "&discount=" + discount + "&discountflag=" + discountflag;

}

function updateCart(orderId, qty){
	
	location.href = "/shopaction.php?action=updatecart&orderId=" + orderId + "&qty=" + qty;

}

function emptyCart(orderId){
	
	location.href = "/shopaction.php?action=emptycart&orderId=" + orderId;

}

function loadProduct(id, category, parent){
	var sku = document.getElementById(id).value;
	location.href = "/product/" + parent + "/" + category + "/" + sku;

}

function loadProduct2(size, sku, category, parent){
	var size = document.getElementById(size).value;

	location.href = "/shopredirect.php?sku=" + sku + "&size=" + size + "&category=" + category + "&parent=" + parent;

}

function loadProduct3(size, sku, category, parent){
	var color = document.getElementById("color").value;
	location.href = "/shopredirect.php?sku=" + sku + "&size=" + size + "&color=" + color + "&category=" + category + "&parent=" + parent;

}

function loadProduct4(color, sku, category, parent){
	var size = document.getElementById("size").value;
	location.href = "/shopredirect.php?sku=" + sku + "&size=" + size + "&color=" + color + "&category=" + category + "&parent=" + parent;

}

function loadProduct5(size_id, color_id, sku, category, parent){
	var size = document.getElementById(size_id).value;
	var color = document.getElementById(color_id).value;
	location.href = "/shopredirect.php?sku=" + sku + "&size=" + size + "&color=" + color + "&category=" + category + "&parent=" + parent;

}



