﻿var _minicart_imgURL = "skins/Skin_1/images/";
var _minicart_ImgArr = new Array("ViewCart-go.png", "Checkout-go.png", "miniCart-b.png", "miniCart-bg.png", "miniCart-t.png", "remove.gif", "update-button.gif");
for (var i = 0; i < _minicart_ImgArr.length; i++) {
    var img = new Image();
    img.src = _minicart_imgURL + _minicart_ImgArr[i];
}
function clickMiniCartShow(isReplaceImg) {

    if ($('#showMiniCart_title').css("background-image").toLowerCase().indexOf("App_Themes/Skin_1/images/Checkout-go.png".toLowerCase()) > -1) {
        window.self.location.href = 'viewcart.aspx';
        return;
    }
    if (isReplaceImg == true)
        $('#showMiniCart_title').css("background-image", "url(App_Themes/Skin_1/images/Checkout-go.png)");
    $('#showMiniCart_main').stop().show().animate({ right: '+55px' }, '600');
}
function closeMiniCartWindowHidden(replaceURL) {
    $('#showMiniCart_main').stop().animate({ right: '-492px' }, '600');
    if (typeof (replaceURL) != "undefined") {
        $('#showMiniCart_title').css("background-image", replaceURL);
    }
}
function miniCartUpdate() {
    //class:minicartrow_tr

    var pID = "";
    var vID = "";
    var qty = "";
    var cartid = "";
    var jsonStr = "";
    var isOk = true;
   
    $("#minicartTable tr.minicartrow_tr").each(function() {
        var model = $(this).find("td:eq(0)").html();
        if (model.trim() != "Granite") {

            pID = $(this).find("td:eq(2)").find("input").eq(0).val();
            vID = $(this).find("td:eq(2)").find("input").eq(1).val();
            qty = $(this).find("td:eq(2)").find("input").eq(2).val();
            //Cean Mod for cart id
            cartid = $(this).find("td:eq(2)").find("input").eq(3).val();
            
            if (/^[1-9]\d*$/.test(qty) == false) {
                $(this).find("input[id^='minicart_Qty_']").select();
                $(this).find("input[id^='minicart_Qty_']").focus();
                alert('Qty error.');
                isOk = false;
                return false; 
            }
            var jsonS = '{"ProductID":"' + pID + '","VariantID":"' + vID + '","Qty":"' + qty.toString(10) + '","cartid":"' + cartid + '"}';
            jsonStr += $.trim(jsonStr) == "" ? jsonS : "," + jsonS;
        }
    });

    if (isOk == false || jsonStr == "") return;

    jsonStr = "[" + jsonStr + "]";



    $('#hiddenMiniCartDiv').css({ 'opacity': '0.0',
        'width': $('#minicartContent').width(),
        'height': $('#minicartContent').height(),
        'left': 25,
        'top': 50,
        'z-Index': '199'
    }).show();

    var _topheight = ($('#showMiniCart_main').height() * (2 / 3));
    if ($.browser.msie)
        _topheight -= 40;


    //    $('#hiddenImgforMiniCart').css({
    //      'top': _topheight,
    //      'left': ($('#minicartContent').width() * (1 / 3) + 40),
    //      'z-Index': 299
    //    }).show();
    $('#hiddenImgforMiniCart').show();
    $.ajax({
        type: "POST",
        url: "AjaxMiniCart.aspx",
        data: { JSONStr: jsonStr, type: "update" },
        error: function() {
            alert('error');
            $('#hiddenMiniCartDiv').hide();
            $('#hiddenImgforMiniCart').hide();
        },
        success: function(msg) {
            if (msg.indexOf('<div align="center"><b><font color="#FF0000;">') > -1) {
                msg = msg.substr(0, msg.lastIndexOf('<div align="center"><b><font color="#FF0000;">'));
            }
            if ($.trim(msg) == 'error') {
                alert('error');
            } else if ($.trim(msg) == 'empty') {
                //                $('#showMiniCart_title').hide();
                //                $('#showMiniCart_main').hide();
                $('#showMiniCart_title').remove();
                $('#showMiniCart_main').remove();
            } else {
                updateMinicartPanel();
                $("#minicartContent").html(msg);
                $("#minicart_total").html($("#hiddenTotal").val());
            }
            //            $('#hiddenMiniCartDiv').hide();
            //            $('#hiddenImgforMiniCart').hide();

        }
    });

}

function updateMinicartPanel() {
    var pID = "";
    var vID = "";
    var qty = "";
    var cartid = "";
    var jsonStr = "";
    var isOk = true;

    $("#minicartTable tr.minicartrow_tr").each(function() {
        var model = $(this).find("td:eq(0)").html();
        if (model.trim() != "Granite") {

            pID = $(this).find("td:eq(2)").find("input").eq(0).val();
            vID = $(this).find("td:eq(2)").find("input").eq(1).val();
            qty = $(this).find("td:eq(2)").find("input").eq(2).val();
            //Cean Mod for cart id
            cartid = $(this).find("td:eq(2)").find("input").eq(3).val();

            if (/^[1-9]\d*$/.test(qty) == false) {
                $(this).find("input[id^='minicart_Qty_']").select();
                $(this).find("input[id^='minicart_Qty_']").focus();
                alert('Qty error.');
                isOk = false;
                return false;
            }
            var jsonS = '{"ProductID":"' + pID + '","VariantID":"' + vID + '","Qty":"' + qty.toString(10) + '","cartid":"' + cartid + '"}';
            jsonStr += $.trim(jsonStr) == "" ? jsonS : "," + jsonS;
        }
    });

    if (isOk == false || jsonStr == "") return;

    jsonStr = "[" + jsonStr + "]";
    
    var _jsonObj;
    _jsonObj = eval('sJson=' + jsonStr + ';');

    var tmpproductID = new Array(_jsonObj.length);
    var tmpqty = new Array(_jsonObj.length);


    for (var i = 0; i < _jsonObj.length; ++i) {
        var tag = -1;
        for (var ii = 0; ii < tmpproductID.length; ++ii) {
            if (tmpproductID[ii] == _jsonObj[i].ProductID) {
                tag = ii;
            }
        }
        if (tag >= 0) {
            tmpqty[tag] = parseInt(tmpqty[tag]) + parseInt(_jsonObj[i].Qty);
        }
        else {
            tmpproductID[i] = _jsonObj[i].ProductID;
            tmpqty[i] = _jsonObj[i].Qty;
        }
        //updateSelectSectionPageInfo(_jsonObj[i].ProductID, _jsonObj[i].Qty);
    }
    for (var j = 0; j < tmpproductID.length; ++j) {

        if (tmpproductID[j] != undefined) {
            updateSelectSectionPageInfo(tmpproductID[j], tmpqty[j]);
        }
    }
}

function miniCartRemove(pid, vid,recid) {

    $('#hiddenMiniCartDiv').css({ 'opacity': '0',
        'width': $('#minicartContent').width(),
        'height': $('#minicartContent').height(),
        'left': 25,
        'top': 50,
        'z-Index': '100'
    }).show();

    var _topheight = ($('#minicartContent').height() * (2 / 3));
    if ($.browser.msie)
        _topheight -= 40;


    //    $('#hiddenImgforMiniCart').css({
    //        'top': _topheight,
    //        'left': ($('#minicartContent').width() * (1 / 3)),
    //        'z-Index': '200'
    //    }).show();
    $('#hiddenImgforMiniCart').show();
    $.ajax({
        type: "POST",
        url: "AjaxMiniCart.aspx",
        data: { pID: pid, vID: vid, type: "remove", cartid: recid },
        error: function() {
            alert('error');
            $('#hiddenMiniCartDiv').hide();
            $('#hiddenImgforMiniCart').hide();
        },
        success: function(msg) {


            if (msg.indexOf('<div align="center"><b><font color="#FF0000;">') > -1) {
                msg = msg.substr(0, msg.lastIndexOf('<div align="center"><b><font color="#FF0000;">'));
            }

            if ($.trim(msg) == 'error') {
                alert('error');
            } else if ($.trim(msg) == 'empty') {

                //Remove total
                $(".minicart_Total").hide();
                $('#showMiniCart_title').hide();
                $('#showMiniCart_main').hide();
                $('#showMiniCart_title').remove();
                $('#showMiniCart_main').remove();
                updateSelectSectionPageInfo(pid, 0);
                updateMinicartPanel();

            } else {

                $("#minicartContent").html(msg);
                $(".minicart_Total").show();
                $("#minicart_total").html($("#hiddenTotal").val());
                updateSelectSectionPageInfo(pid, 0);
                updateMinicartPanel();

            }
            $('#hiddenMiniCartDiv').hide();
            $('#hiddenImgforMiniCart').hide();




        }
    });




}


function updateSelectSectionPageInfo(productID, productQty) {
   
    if (document.getElementById("hiddenImgforAddToCart")) {
        //通过selectSection页面的ajax隐藏DIV判断是否处理此页面
        if (document.getElementById('productModel_' + productID.toString(10)) != null && document.getElementById('Desc_' + productID.toString(10)) != null) {
            var productDesc = document.getElementById('Desc_' + productID.toString(10)).value;
            var productModel = document.getElementById('productModel_' + productID.toString(10)).value;

            var _tempQtyTd = $('#Quantity_' + productID.toString(10)).parents('tr:eq(1)').find('td:first');
            var _addHtml = '<br/><span style=" font-weight:bold; font-size: 10px;color:green"><b>(' + productQty + ' of ' + productModel + ' added to cart)</b></span>';
         
            if (productQty > 0) {

                $(_tempQtyTd).next().html(productDesc + _addHtml);
            }
            else {
              
                $(_tempQtyTd).next().html(productDesc);
            }
        }





    } else if (document.getElementsByName("shoppingcartinput_12_RandId").length > 0) {
    alert(productQty);
        //表示在ShoppingCart页面,SHIPPING更新待续
        if (productQty > 0)
            $('#hiddenqutty_' + productID.toString(10)).next().val(productQty.toString(10));
        else
            $('#hiddenqutty_' + productID.toString(10)).parents("tr:eq(0)").remove();
    }




}

String.prototype.trim = function() {
    // 用正则表达式将前后空格  
    // 用空字符串替代。  
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
//minicartTable

