$("document").ready(function(){
    $("#ShowNum").hide();
    $("#LoadImg").show();
    $.post("../ajax_shoppingcart.php",{
        action:"show"
    },
    function(num){
        $("#ShowNum").html("<strong style='color:red'>"+num+"</strong>");
        $("#LoadImg").hide();
        $("#ShowNum").show();
    }
    ,"html");
}
);

function addtocart(Obj){
    var product_id = $(Obj).val();
    $(Obj).hide().next().show();
    $.ajax({
        type: "post",
        url : "../ajax_shoppingcart.php",
        dataType:'json',
        data: 'product_id=' + product_id + '&action=addtocart',
        error: function(msg){
            alert("Sorry,add to cart failed, please contact us.");
            return false;
        },
        success: function(json){
            $('#loading').show();
            //$('#addtocart').hide();
            switch (json.flag) {
                case 1:
                    alert("Sorry,add to cart failed, please contact us.");
                    break;
                case 2:
                    window.location.href = "../shoppingcart/";
                    break;
                default:
                    alert("Sorry,add to cart failed, please contact us.");
                    break;
            }
			$('#loading').hide();
        }
    });
}

function checkout(Obj){
    var product_id = $(Obj).val();
	$(Obj).hide().next().show();
    $.ajax({
        type: "post",
        url : "../ajax_shoppingcart.php",
        dataType:'json',
        data: 'product_id=' + product_id + '&action=addtocart',
        error: function(msg){
            window.location.reload();
			return false;
        },
        success: function(json){
            $('#loading').show();
            //$('#addtocart').hide();
            switch (json.flag) {
                case 1:
                    window.location.reload();
                    break;
                case 2:
                    window.location.href = "../checkout/";
                    break;
                default:
                    window.location.reload();
                    break;
            }
			$('#loading').hide();
        }
    });
}

function addOne(Obj){
    var product_id = $(Obj).val();
    $(Obj).hide().next().show();
    $.ajax({
        type: "post",
        url : "../ajax_shoppingcart.php",
        dataType:'json',
        data: 'product_id=' + product_id + '&action=addone',
        error: function(msg){
            window.location.reload();
            return false;
        },
        success: function(json){
            switch (json.flag) {
               case 1:
			       $(Obj).show().next().hide();
                   break;
               case 2:
			       $(Obj).show().next().hide();
				   break;
               case 3:
                   var amount=parseInt($("#num"+product_id).val());
                   amount=amount+1;
                   $("#num"+product_id).val(amount);
                   $("#total").html("$"+json.total);
				   $(Obj).show().next().hide();
                   break;
               case 4:
			       window.location.reload();
				   break;
               default:
                   break;
            }
        }
    });
}

function subOne(Obj){
    var product_id = $(Obj).val();
    $(Obj).hide().next().show();
    $.ajax({
        type: "post",
        url : "../ajax_shoppingcart.php",
        dataType:'json',
        data: 'product_id=' + product_id + '&action=subone',
        error: function(msg){
            alert("Sorry,sub cart failed, please contact us.");
            window.location.reload();
            return false;
        },
        success: function(json){
            switch (json.flag) {
               case 1:
                   alert("Sorry,sub cart failed, please contact us.");
			       $(Obj).show().next().hide();
                   break;
               case 2:
				   alert("Sorry,sub cart failed, please contact us.");
			       $(Obj).show().next().hide();
				   break;
               case 3:
                   $("#num"+product_id).val($("#num"+product_id).val()-1);
                   $("#total").html("$"+json.total);
				   $(Obj).show().next().hide();
                   break;
               case 4:
			       window.location.reload();
				   break;
               default:
                   break;
            }
        }
    });
}

function delOne(Obj){
  if(confirm("You will remove this product in your shoppingcart,confirm?")){
    var product_id = $(Obj).val();
    $(Obj).hide().next().show();
    $.ajax({
        type: "post",
        url : "../ajax_shoppingcart.php",
        dataType:'json',
        data: 'product_id=' + product_id + '&action=delone',
        error: function(msg){
            window.location.reload();
            return false;
        },
        success: function(json){
            switch (json.flag) {
               case 1:
			       $(Obj).show().next().hide();
                   break;
               case 2:
			       $(Obj).show().next().hide();
				   break;
               case 3:
			       window.location.reload();
                   break;
               case 4:
			       window.location.reload();
				   break;
               default:
                   break;
            }
        }
    });
  }
}