$(function(){
	$(".cartRemove").unbind("click").click(function(){
		var theParts = $(this).attr("id").split("_");
		removeFromCart(theParts[1]);
		return false;
	});
	$(".cartEmpty").unbind("click").click(function(){
		if(confirm("Are you sure?")){
			clearCart();
		}
		return false;
	});
});
function addToCart(){
	/*
	if(typeof $(".productOptions")[0] != "undefined"){
		var bMissing = false;
		$(".productOptions").each(function(){
			if($(this).val()==0 || $(this).val()==""){
				alert("Ooops! First choose an option.");
				$(this).focus();
				bMissing = true;
				return true;
			}
		});
		if(bMissing){
			return false;
		}
	}
	*/
	var formData = $("#frmAddToCart").fastSerialize();
	$.post(
		$("#scriptName").val(),
		formData,
		function(txt){
			if(txt!=""){
				var parts = txt.split("|");
				if(parts[4]!="0"){
					$('#cart_content').html(parts[0]+"<p class=\"error\">Ooops! This product became out of stock in meantime..</p>");
				}else{
					$("#cart_content").html(parts[0]);
				}
				$('#cartStatus').html(parts[1]);
				$('#totalCart').html(parts[2]);
				$('#prodOptions').empty();
				$('#prodSelectedOptions').empty();
				JT_init();
				$(".cartRemove").unbind("click").click(function(){
					var theParts = $(this).attr("id").split("_");
					removeFromCart(theParts[1]);
					return false;
				});
				$(".cartEmpty").unbind("click").click(function(){
					clearCart();
					return false;
				});
				if(!$("#ajax_cart").is(":visible")){
					$('#ajax_cart').fadeIn("slow");
					$('#advBox').fadeOut("slow");
					$('#slider')
						.height(120)
						.slideDown("slow");
				}
				JT_init;
				txt = null;
			}
		}
	);
	$("#frmAddToCart").resetForm();
	
	var thePrice = $("#thePrice").val();
	if(thePrice!="" && typeof $("#totalPrice")[0]!="undefined"){
		var formData = [];
		formData.push({name: "Option", value: "getFormattedPrice"});
		formData.push({name: "product", value: $("#product").val()});
		formData.push({name: "thePrice", value: thePrice});
		$.post(
			$("#scriptName").val(),
			formData,
			function(txt){
				if(txt!=""){
					$("#totalPrice").html(txt);
					$("#topPrice").html(txt);
					txt = null;
				}
			}
		);
	}
	return false;
}
function removeFromCart(id){
	var formData = [];
    formData.push({name: "isCart", value: "true"});
    formData.push({name: "action", value: "remove"});
    formData.push({name: "id", value: id});
    formData.push({name: "product", value: $('#product').val()});
    formData.push({name: "pageName", value: encodeURIComponent($('#pageName').val())});
    formData.push({name: "newsTitle", value: encodeURIComponent($('#newsTitle').val())});
    formData.push({name: "catName", value: encodeURIComponent($('#catName').val())});
    formData.push({name: "cplTitle", value: encodeURIComponent($('#cplTitle').val())});
	$.post(
		$("#scriptName").val(),
		formData,
		function(txt){
			if(txt!=""){
				if(txt.indexOf('emptyCartNote')!=-1){
					if($("#ajax_cart").is(":visible")){
						$("#ajax_cart").fadeOut("slow");
						$("#advBox").fadeIn("slow");
						$("#slider").css("height",40);
					}
				}else{
					var parts = txt.split("|");
					$("#cart_content").html(parts[0]);
					$('#cartStatus').html(parts[1]);
					if(!$("#ajax_cart").is(":visible")){
						$('#ajax_cart').fadeIn("slow");
						$('#advBox').fadeOut("slow");
					}
					if(!$("#slider").is(":visible")){
						$('#slider').height(40);
						$('#slider').slideDown("slow");
					}
					$('#totalCart').html(parts[2]).show();
				}
				JT_init();
				$(".cartRemove").unbind("click").click(function(){
					var theParts = $(this).attr("id").split("_");
					removeFromCart(theParts[1]);
					return false;
				});
				$(".cartEmpty").unbind("click").click(function(){
					clearCart();
					return false;
				});
				txt = null;
			}
		}
	);
	return false;
}
function clearCart(){
	var formData = [];
    formData.push({name: "isCart", value: "true"});
    formData.push({name: "action", value: "removeall"});
    formData.push({name: "product", value: $("#product").val()});
    formData.push({name: "prodName", value: encodeURIComponent($("#prodName").val())});
    formData.push({name: "pageName", value: encodeURIComponent($("#pageName").val())});
    formData.push({name: "newsTitle", value: encodeURIComponent($("#newsTitle").val())});
    formData.push({name: "catName", value: encodeURIComponent($("#catName").val())});
	$.post(
		$("#scriptName").val(),
		formData,
		function(){
			if($("#ajax_cart").is(":visible")){
				$("#ajax_cart").fadeOut("slow");
				$("#advBox").fadeIn("slow");
				$("#slider").css("height",40);
			}
		}
	);
	return false;
}