$(function(){
    $('#dialog-shopping-cart').dialog({
        autoOpen:false,
        modal:true,
        dragable:true,
        resizable:true,
        minWidth:640,
        width:640,
        height:400,
        title: "Your shopping cart",
        buttons: {
            'Close': function() {
                $(this).dialog('close');
            },
            'Re-Calculate': function() {
//                $(this).dialog('close');
             check_update();
            },
            'Check out': function() {
                window.location = "action.php?do=register_and_login";
                $(this).dialog('close');
            }
        }
    });

   /*$("#btn-check-update").click(function() {
       checkUpdate();
   });*/

   $(".btn-cart").click(function() {
      $.post("action.php?do=shopping_cart&ajax=1&is_include=0",  function(data){
        $('#dialog-shopping-cart-text').html(data);
        $('#dialog-shopping-cart').dialog('open');    
      });
      return false;   
   });
});

function check_update(){
    actionUri = "action.php?do=update_cart_test&ajax=1";
    $("form#shopping-cart-form :input").each(function() {
        var type = $(this).attr('type');
        var name = $(this).attr('id');

        if (type == "checkbox") {
            tmp = name.split("del");
            iKey = tmp[1];
         
            quantityObj = $("input#quantity"+iKey);

            if ($(this).attr("checked") == false) {
                actionUri = actionUri + "&" + quantityObj.attr('id') + "=" + quantityObj.val(); 
            }
            else {
                actionUri = actionUri + "&" + name + "=" + $(this).val();
            }
        }
    });
    
    $.post(actionUri,  function(data){
        $('#dialog-shopping-cart-text').html(data);
        $('#dialog-shopping-cart').dialog('open');    
      });
    return false;   
  //  window.location = actionUri;
}
function checkUpdate()
{
    actionUri = "action.php?do=update_cart";
    $("form#update-cart-form :input").each(function() {
        var type = $(this).attr('type');
        var name = $(this).attr('id');

        if (type == "checkbox") {
            tmp = name.split("del");
            iKey = tmp[1];
         
            quantityObj = $("input#quantity"+iKey);

            if ($(this).attr("checked") == false) {
                actionUri = actionUri + "&" + quantityObj.attr('id') + "=" + quantityObj.val(); 
            }
            else {
                actionUri = actionUri + "&" + name + "=" + $(this).val();
            }
        }
    });
    window.location = actionUri;
}

function pay(actionUri){
   var theForm = $("checkOutForm");
   theForm.action = actionUri;
   theForm.submit();
}

