﻿var choseedItem = null;
var choseedColor = "";

function QtyDown_Function(_qtyObjName_Function) {

    var _qtyObj_Function = document.getElementById('' + _qtyObjName_Function + '');
    if (/^\d+$/.test($.trim(_qtyObj_Function.value)))
        if (parseInt(_qtyObj_Function.value, 10) >= 2) {
        _qtyObj_Function.value = parseInt(_qtyObj_Function.value, 10) - 1;
    }
};
function QtyUp_Function(_qtyObjName_Function) {

    var _qtyObj_Function = document.getElementById('' + _qtyObjName_Function + '');
    if (/^\d+$/.test($.trim(_qtyObj_Function.value)))
        _qtyObj_Function.value = parseInt(_qtyObj_Function.value, 10) + 1;
};
function addToCart_Mtech_Clkitchens_Function(productID, accessories, accessoriesMsg) {

    //andrew add start 2011-07- 29
    var msg = "Loading...";
    AlertMsg(msg);
    var thisfamily = document.getElementById("thisfamily").value;

    $.ajax({
        type: "POST",
        url: "AjaxProgramProcess.aspx",
        cache:"false",
        data: {ProductID:productID},
        success: function(msg) {
            if (!/\[NOEXISTS\]/.test(msg)) {
                var d = msg;
                d = d.replace(/&lt;/ig, "<").replace(/&gt;/ig, ">");
                $("#m_CartInfo").html(d);
                $("#ProgramCart").click();
                $('#hideMsg').hide();
                setTimeout(function() { $("#ProgramCart").click(); }, 500); //add delay time
            }
            else 
            {
                CallBackProcess(productID, accessories, accessoriesMsg);
            }
        },
        error: function() {
            alert("load failed!");
        }
    });

    //andrew add end 2011-07-29
   
}
//Andrew added Start 2011-08-02

function CallBackProcess(productID, accessories, accessoriesMsg) {
    var product_Function = document.getElementById('product_' + productID.toString(10)).value;

    var Quantity_Function = document.getElementById('Quantity_' + productID.toString(10)).value;
    var VariantID_Function = document.getElementById('VariantID_' + productID.toString(10)).value;
    var productModel_Function = document.getElementById('productModel_' + productID.toString(10)).value;
    var product_des_Function = document.getElementById('Desc_' + productID.toString(10)).value;

    var bodyClientHeight = 100;
    if ($.browser.msie) {
        bodyClientHeight = document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
        $('html').css('overflow-x', 'hidden');
    }
    else {
        bodyClientHeight = self.innerHeight;
    }


    var msg = "Adding Qty " + $('#Quantity_' + product_Function.toString(10)).val() + " of " + productModel_Function + ".";
    AlertMsg(msg);

    $.ajax({
        type: "POST",
        url: "ajaxAddToCart.aspx",
        cache: "false",
        data: { ProductID: product_Function, VariantID: VariantID_Function, Quantity: Quantity_Function, Accessories: accessories, AccessoriesMsg: accessoriesMsg, thisfamily: document.getElementById("thisfamily").value, thisfamilyName: document.getElementById("thisfamilyName").value }, // #region MTech cean mod for new select item
        error: function() {
            alert('error');
            Wait('100');
            // $('#hiddenDiv').hide();
            //$('#hiddenImgforAddToCart').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');
                Wait('100');

            }
            else if ($.trim(msg) == "Customer record can not be created!") {
                alert('Customer record can not be created!');
            }
            else {
                var _tempTdModel = $('#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>(' + msg + ' of ' + productModel_Function + ' added to cart)</b></span>';

                $(_tempTdModel).next().html(product_des_Function + _addHtml);



                var _dataType = '';
                if ($('#showMiniCart_title').size() > 0)
                    _dataType = 'GETCARTTABLE';
                else
                    _dataType = 'GETCARTALLTABLE';
                $.ajax({
                    type: "POST",
                    url: "AjaxMiniCart.aspx",
                    data: { type: _dataType },
                    error: function() {
                        alert('error');
                        $('#hiddenMiniCartDiv').hide();
                        $('#hiddenImgforMiniCart').hide();

                        Wait('100');


                    },
                    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();
                            $(".minicart_Total").remove();
                            $('#showMiniCart_title').remove();
                            $('#showMiniCart_main').remove();
                        } else {
                            if (_dataType == 'GETCARTTABLE') {
                                $("#minicartContent").replaceWith(msg);
                            }
                            else {

                                $("body").append(msg);

                            }
                            $("#minicart_total").html($("#hiddenTotal").val());
                        }
                        $('#hiddenMiniCartDiv').hide();
                        $('#hiddenImgforMiniCart').hide();
                        Wait('1000');

                        AlertMsg();

                        chageColor(_tempTdModel);



                    }
                });
            }



        }
    });


}

//Andrew Added end 2011-8-02


//ACC start
function addToCart4ViewCart(productID, accessories, accessoriesMsg, cartid) {
    var product_Function = document.getElementById('product_' + productID.toString(10)).value;

    var Quantity_Function = document.getElementById('Quantity_' + cartid.toString(10)).value;
    var VariantID_Function = document.getElementById('VariantID_' + productID.toString(10)).value;
    var productModel_Function = document.getElementById('productModel_' + productID.toString(10)).value;
    var product_des_Function = document.getElementById('Desc_' + productID.toString(10)).value;


    var bodyClientHeight = 100;
    if ($.browser.msie) {
        bodyClientHeight = document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
        $('html').css('overflow-x', 'hidden');
    }
    else {
        bodyClientHeight = self.innerHeight;
    }


    var msg = "Adding Qty " + $('#Quantity_' + cartid).val() + " of " + productModel_Function + ".";
    AlertMsg(msg);


    $.ajax({
        type: "POST",
        url: "ajaxAddToCart.aspx",
        cache: "false",
        data: { ProductID: product_Function, VariantID: VariantID_Function, Quantity: Quantity_Function, Accessories: accessories, AccessoriesMsg: accessoriesMsg, CartId: cartid, thisfamily: document.getElementById("thisfamily").value, thisfamilyName: document.getElementById("thisfamilyName").value },
        error: function() {
            alert('error');
            Wait('100');
            // $('#hiddenDiv').hide();
            //$('#hiddenImgforAddToCart').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');
                Wait('100');

            } else {
                var _tempTdModel = $('#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>(' + msg + ' of ' + productModel_Function + ' added to cart)</b></span>';

                $(_tempTdModel).next().html(product_des_Function + _addHtml);



                var _dataType = '';
                if ($('#showMiniCart_title').size() > 0)
                    _dataType = 'GETCARTTABLE';
                else
                    _dataType = 'GETCARTALLTABLE';


                $.ajax({
                    type: "POST",
                    url: "AjaxMiniCart.aspx",
                    data: { type: _dataType },
                    error: function() {
                        alert('error');
                        $('#hiddenMiniCartDiv').hide();
                        $('#hiddenImgforMiniCart').hide();

                        Wait('100');


                    },
                    success: function(msg) {

                        //                        var pq = $("#quantity_" + cartid).val();
                        //                        if (parseInt(pq) - parseInt(Quantity_Function) > 0) {
                        //                            $("#quantity_" + cartid).val(parseInt(pq) - parseInt(Quantity_Function));
                        //                        }
                        //                        else {
                        //                            $("#quantity_" + cartid).val("0");
                        //                        }


                        //$("#A_Update_" + cartid).click();
                        document.getElementById('loadData_Submit').click();
                        //$("#<%=btnChangeDeliveryZipCode.ClientID%>").click();
                    }
                });
            }



        }
    });


}
//ACC end
/*------------------------------for product acc by cean----------------------------*/



function loadAccForm(_ProductID, _CategoryID) {
    if ($("#popAcc_" + _ProductID).html() != "") {
        $("#aPopAcc_" + _ProductID).click();
    }
    else {
        var msg = "Loading...";
        AlertMsg(msg);

        $.ajax({
            type: "POST",
            url: "ShowAccessories.aspx",
            data: { ProductID: _ProductID, CategoryID: _CategoryID },
            error: function() {

            },
            success: function(msg) {
                $("#popAcc_" + _ProductID).html(msg);
                $("#aPopAcc_" + _ProductID).click();
                $('#hideMsg').hide(); //close the alert
            }
        });
    }
}

//ACC start
function addToCart4AccProductsViewCart(productID, cartid) {
    var msg = "Loading...";
    AlertMsg(msg);
    var accessories = "", accessoriesMsg = "";
    var objs = $(".productAcc_" + productID);
    for (var i = 0; i < objs.size(); ++i) {
        if (objs.eq(i).attr("checked")
        && !objs.eq(i).attr("noCount")) {
            accessories += objs.eq(i).val() + ",";
            accessoriesMsg += objs.eq(i).val() + ",";
        }
        //{{ Jacky Fix selections status
        if (objs.eq(i).attr("checked")) {
            objs.eq(i).attr("checked", false);
        }
        //}}
    }
    jQuery().colorbox.close();

    //andrew add

    var thisfamily = document.getElementById("thisfamily").value;

    $.ajax({
        type: "POST",
        url: "AjaxProgramProcess.aspx",
        cache: "false",
        data: {ProductID:productID},
        success: function(msg) {
            if (!/\[NOEXISTS\]/.test(msg)) {
                var d = msg;
                d = d.replace(/&lt;/ig, "<").replace(/&gt;/ig, ">");
                $("#m_CartInfo").html(d);
                $("#ProgramCart").click();
                $('#hideMsg').hide();
                setTimeout(function() { $("#ProgramCart").click(); }, 500); //add delay time

            }
            else {
                addToCart4ViewCart(productID, accessories, accessoriesMsg, cartid);
            }
        },
        error: function() {
            alert("load failed!");
        }
    });
    
    //andrew add end
    
    //alert(accessoriesMsg);
    //addToCart4ViewCart(productID, accessories, accessoriesMsg, cartid);
}

function addToCart4AccProducts(productID) {
    var msg = "Loading...";
    AlertMsg(msg);
    var accessories = "", accessoriesMsg = "";
    var objs = $(".productAcc_" + productID);
    for (var i = 0; i < objs.size(); ++i) {
        if (objs.eq(i).attr("checked")
        && !objs.eq(i).attr("noCount")) {
            accessories += objs.eq(i).val() + ",";
            accessoriesMsg += objs.eq(i).val() + ",";
        }
        //{{ Jacky Fix selections status
        if (objs.eq(i).attr("checked")) {
            objs.eq(i).attr("checked", false);
        }
        //}}
    }
    jQuery().colorbox.close();
    //alert(accessoriesMsg);

    //andrew add start 2011-07- 29

    var thisfamily = document.getElementById("thisfamily").value;

    $.ajax({
        type: "POST",
        url: "AjaxProgramProcess.aspx",
        cache: "false",
        data: {ProductID:productID},
        success: function(msg) {
            if (!/\[NOEXISTS\]/.test(msg)) {
                var d = msg;
                d = d.replace(/&lt;/ig, "<").replace(/&gt;/ig, ">");
                $("#m_CartInfo").html(d);
                $("#ProgramCart").click();
                $('#hideMsg').hide();
                setTimeout(function() { $("#ProgramCart").click(); }, 500); //add delay time
            }
            else {
                addToCart_Mtech_Clkitchens_Function(productID, accessories, accessoriesMsg);
            }
        },
        error: function() {
            alert("load failed!");
        }
    });
    
    //andrew add end 2011-07-29
}
function loadAccForm4ViewCart(_ProductID, _CategoryID, _cartid, _isShowModifyQty) {
    //    if ($("#popAcc_" + _ProductID).html() != "") {
    //        $("#aPopAcc_" + _ProductID).click();
    //    }
    //    else {
    var msg = "Loading...";
    AlertMsg(msg);
    $.ajax({
        type: "POST",
        url: "ShowAccessories.aspx",
        data: { ProductID: _ProductID, CategoryID: _CategoryID, Type: "editor", cartID: _cartid, isShowModifyQty: _isShowModifyQty },
        error: function() {

        },
        success: function(msg) {
            $("#popAcc_" + _ProductID).html(msg);
            $("#aPopAcc_" + _ProductID).click();
            $('#hideMsg').hide(); //close the alert
        }
    });
    //}
}
//ACC end

function CalculateAccPriceQty(productID) {
    var price = 0.00;
    var count = 0;
    var objs = $(".productAcc_" + productID);
    for (var i = 0; i < objs.size(); ++i) {
        if (objs.eq(i).attr("checked")
        && !objs.eq(i).attr("noCount")) {
            price += parseFloat(objs.eq(i).attr("price"));
            count += 1;
        }
    }
    $("#Price_" + productID).html(formatCurrency(price));
    $("#Count_" + productID).html(count);
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '' + num + '.' + cents);
}

/*------------------------------End-----------------------------------------------*/
var flagClose = 0;
function Wait(times) {

    if (flagClose >= 3) {
        flagClose = 0;
        $('#hideMsg').hide();
        return;
    }
    else {

        flagClose++;
    }

    setTimeout("Wait()", times)
}

function AlertMsg(msg) {


    $('#msgcontent').html(msg);

    $("#hideMsg").animate({ top: 400 + $(window).scrollTop() + "px" }, { duration: 0, queue: false });

    //document.getElementById("hideMsg").style.left = (document.body.offsetWidth / 2 - 130) + 'px';

    var menuYloc = 400;

    $(window).scroll(function() {
        var offsetTop = menuYloc + $(window).scrollTop() + "px";
        $("#hideMsg").animate({ top: offsetTop }, { duration: 60, queue: false });
    });

    $("#hideMsg").show();
    //toby add end
}

function chageColor(_tempTdModel) {
    if (choseedItem != null) {
        $(choseedItem).parent().css("background-color", choseedColor);
    }

    choseedColor = $(_tempTdModel).parent().css("background-color");
    choseedItem = _tempTdModel;
    $(_tempTdModel).parent().css("background-color", "#f6eca4");


    $(_tempTdModel).parent().fadeIn("slow")



}
//Acc Video
function showAccVideo() {

    $("#a_AccVideo").colorbox({ inline: true, display: '' });
    $("#a_AccVideo").click();
}


