﻿var addToCart = function(productNumber, price) {
    jQuery.ajax({
        url: "CartScript.ashx?action=add&PName=" + productNumber.replace(/&/g, "|") + "&Price=" + price,
        cache: false,
        success: function(html) {
            alert("ADD 1x " + productNumber + " TO BASKET"); //ADD 1x   TO BASKET
        }
    });
}

//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) {
    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 == 2)
        addToCart(title, ForDight(Gross_Amount * Discount, 2));
    else if (arguments.length == 3)
        addToCart(title, ForDight(Gross_Amount * arguments[2], 2));
}

function ForDight(Dight, How) {
    Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How);
    return Dight;
} 
