﻿//Ready
$(document).ready(function() {
    $.ShoppingCart({ id: $('table[name="dataList"]').attr("id") });
});

$.ShoppingCart = function(config) {
    config = $.extend(
			{
			    id: null
			},
			config || {});

    init();

    function init() {
        new $('#head').TableSetter({ id: 'head', innerHtml: null });
        new $('#' + config.id).TableSetter({ id: 'dataList', innerHtml: null });

        //Check out
        if ($('#hidTotalShipFee')[0])
            $('#divCheckOut').show();

        addOneBtnEvent();
        subOneBtnEvent();
        proChkNumBlurEvent();
        proChkDelBtnEvent();
        
        adjustMain();
    }

    function addOneBtnEvent() {
        $('.proChkAddOne').click(function() {
            var catNo = $(this).attr('name');
            optCart(catNo, 'add', calNum($('#cart_chk_' + catNo).val(), 1), 1, setTotal);
        });
    }
    function subOneBtnEvent() {
        $('.proChkSubOne').click(function() {
            var catNo = $(this).attr('name');
            var willNum = calNum($('#cart_chk_' + catNo).val(), -1);
            if (willNum > 0) {
                optCart(catNo, 'del', willNum, -1, setTotal);

            }
            else if (willNum == 0 && confirm('Do you not need the one?')) {
                optCart(catNo, 'del', willNum, -1);
                //refresh
                window.location.href = window.location.href;
            }
        });
    }
    function proChkNumBlurEvent(obj) {
        $('.proChkNum').blur(function() {
            var catNo = $(this).attr('name');
            var obj = $(this);
            var oldv = $('#cart_chk_old_' + catNo).val();
            if (obj.val().match(/[^\d]/g)) {
                alert('Please input a number');
                obj.val($('#cart_chk_old_' + catNo).val());
                obj.focus();
            }
            else {
                if (obj.val() == "" || obj.val() == "0") {
                    if (confirm('Do you not need the one?')) {
                        optCart(catNo, 'del', oldv, -1);
                        window.location.href = window.location.href;
                    }
                    else
                        obj.val(oldv);
                }
                var incr = parseInt(obj.val()) - parseInt(oldv);
                if (incr != 0) {
                    optCart(catNo, (incr > 0) ? 'add' : 'del', obj.val(), incr, setTotal);
                }
            }
        });
    }

    function proChkDelBtnEvent() {
        $('.proChkDelBtn').click(function() {
            if (confirm('Are you sure to delete it?')) {
                var catNo = $(this).attr('name');
                var incNum = parseInt($('#cart_chk_' + catNo).val());
                if (incNum > 0) {
                    optCart(catNo, 'del', 0, incNum * -1);
                    //refresh
                    window.location.href = window.location.href;
                }
            }
        });
    }
    function setTotal() {
        var cArr = $.cookie('ptg_mini_cart_list');
        var tNum = 0;
        var tPrice = 0;
        if (cArr != null) {
            var itms = eval('[' + cArr + ']');
            $.each(itms, function(i, t) {
                tNum = tNum + parseInt(t.num);
                tPrice = tPrice + t.price * parseInt(t.num);
            });
        }
        if (tNum > 0) {
            $('.proChkTotalNum').html(tNum);
            $('.proChkTotalPrice').html($.cookie('ptg_user_currency_selected') + "&nbsp;" + tPrice.format('#.00'));
            $('.proChkEndPrice').html($.cookie('ptg_user_currency_selected') + "&nbsp;" + (tPrice + parseInt($('#hidTotalShipFee').val())).format('#.00'));

            //Check out
            $('#divCheckOut').show();
        }

    }


    //catNo,opration(add,del),willNum(final num),incNum(increment),callback
    function optCart(catNo, opt, willNum, incNum, cback) {
        $.MiniCart({ catNo: catNo, country: $('select[objname="country"]').val(), dowhat: opt, incNum: (incNum) ? incNum : 0, callback: (cback) ? cback : null });
        $('#cart_chk_' + catNo).val(willNum);
        $('#cart_chk_old_' + catNo).val(willNum);
    }

    function calNum(ov, inc) {
        ov = parseInt(ov);
        return ov + inc;
    }
}


function proCheckOutEvent(o) {
    //check ab
    if (checkHasAb()) {
        var chk = $('#chkAgree');
        if (chk.length > 0 && !chk[0].checked) alert("Please check on the free sample protocol!");

        else { location = "Order.aspx"; }
    }
}
//check ab and second ab
function checkHasAb() {
    var rtn = true;
    var abc = 0;
    var sabc = 0;
    var rtn = true;
    var cArr = $.cookie('ptg_mini_cart_list');
    if (cArr != null) {
        var itms = eval('[' + cArr + ']');
        $.each(itms, function(i, t) {
            if (t.t == 1)
                abc++;
            else if (t.t == 2)
                sabc++;
        });
    }
    if (abc == 0 && sabc > 0) {
        alert("Must add primary antibody to cart to purchase secondary antibody!"); rtn = false;
    }

    return rtn;
};
