﻿var addToCart = function(pid, pnum, price) {
    var s = new CartJson();
    var newAdd = s.add(pid, pnum, price);
    alert("Add Product：" + newAdd.ProductNumber + " , Price：" + newAdd.Price + " , Quantity：" + newAdd.Quantity);
}
var addPackToCart = function (productNumber, price, id) {
    addToCart(id, productNumber, price);
}

//new cart script
var Discount = 0.65;
jQuery(function () {
    init();
    jQuery("select").change(function () { init() });
    jQuery(".Discount").html(Discount * 100 + "%");
});
var init = function () {
    var tb = jQuery(document).find(".search-listings");
    var tbCount = tb.length;
    for (j = 0; j < tbCount; j++) {
        var tr = jQuery(tb[j]).find("tr");
        var trCount = tr.length;
        var Gross_Amount = 0;
        for (i = 1; i < trCount - 1; i++) {
            Gross_Amount = Gross_Amount + Number(jQuery(jQuery(tr[i]).find("select")[0]).val());
        }
        var GA = jQuery(tb[j]).find(".GrossAmount");
        var NA = jQuery(tb[j]).find(".NetAmount");
        if (GA[0] != null)
            jQuery(GA[0]).html(Gross_Amount);
        if (NA[0] != null)
            jQuery(NA[0]).html(ForDight(Gross_Amount * Discount, 2));
    }
}
var addCart = function (obj, title, id) {
    var tr = jQuery(obj).parent().parent().parent().find("tr");
    var trCount = tr.length;
    var selectVal = "";
    var Gross_Amount = 0;
    for (i = 1; i < trCount - 1; i++) {
        var val = jQuery(jQuery(tr[i]).find("select")[0]).val();
        selectVal = selectVal + val + ",";
        Gross_Amount = Gross_Amount + Number(val);
    }

    if (arguments.length == 3) {
        //alert(Gross_Amount * Discount);
        addPackToCart(title, ForDight(Gross_Amount * Discount, 2), id);
    }else if (arguments.length == 4){
        addPackToCart(title, ForDight(Gross_Amount * arguments[3], 2), id);
    }
}

function ForDight(Dight, How) {
    Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How);
    return Dight;
} 
