﻿    function addtocart(ppid, qty, resultsid, errorID) {
        var error = document.getElementById(errorID);
        if (error != null) {
            error.innerHTML = "";
        }
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            data: "ppid=" + ppid + "&qty=" + qty + "&resultsid=" + resultsid + "&action=addtocart",
            success: function(msg) {
                if (msg == "1") {
                    //alert("The value of Qty is invalid.");
                    error.innerHTML = "The Value of Qty is invalid.";
                }
                else if (msg == "2") {
                    //alert("ProductId is invalid..");
                    error.innerHTML = "ProductId is invalid..";
                }
                else if (msg == "3") {
                    //alert("This item is out of stock.");
                    error.innerHTML = "This item is out of stock.";
                }
                else if (msg == "4") {
                    //alert("System error,please try agin later.");
                    error.innerHTML = "System error,please try again later.";
                }
                else {
                    document.getElementById(resultsid).innerHTML = msg;
                    //setBottom();
                }
            }
        });
    }
    function additemstocart(products, resultsid, flag) {
        if (flag == 2) {
            var productGroup = products.split('|');
            for (var i = 0; i < productGroup.length; i++) {
                var types = productGroup[i].split(',');
                var qtyID = types[1];
                var errorID = types[2];
                var qty = 0;
                if (document.getElementById(qtyID) != null) {
                    var qtyValue = document.getElementById(qtyID).value;
                    if (isNaN(qtyValue) || qtyValue < 1) {
                        document.getElementById(errorID).innerHTML = "Qty is invalid.";
                        continue;
                    }
                    else {
                        document.getElementById(errorID).innerHTML = "";
                    }
                    products = products.replace(qtyID, qtyValue);
                }
            }
        }
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            data: "products=" + products + "&resultsid=" + resultsid + "&flag=" + flag + "&action=additemstocart",
            success: function(msg) {
                if (msg == "1") {
                    alert("System error,please try agin later.");
                }
                else if (msg == "2") {
                    alert("This item is out of stock.");
                }
                else if (msg == "3") {
                    alert("The value of Qty is invalid.");
                }
                else {
                    if (flag == "2") {
                        var errormsg = msg.substring(0, 5);
                        if (errormsg == "ERROR") {
                            msg = msg.substring(6);
                            var errorGroup = msg.split('|');
                            var i = 0;
                            while (i < errorGroup.length) {
                                var types = errorGroup[i].split(',');
                                var msgID = types[0];
                                var msgError = types[1];
                                var lblMsg = document.getElementById(msgID);
                                if (lblMsg != null) {
                                    lblMsg.innerHTML = msgError;
                                }
                                i = i + 1;
                            }
                        }

                    }
                    document.getElementById(resultsid).innerHTML = msg;
                    //setBottom();
                }

            }
        });
    }
    function updateqty(cartlineitemid, qty, resultsid) {
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            data: "cartlineitemid=" + cartlineitemid + "&qty=" + qty + "&resultsid=" + resultsid + "&action=updateqty",
            success: function(msg) {
                if (msg == "0" || msg == "1") {
                    alert("System error,please try agin later.");
                }
                else if (msg == "2") {
                    alert("Update side cart line item error,please try agin later.");
                }
                else if (msg == "3") {
                    alert("The value of Qty is invalid,please enter agin.");
                }
                else {
                    document.getElementById(resultsid).innerHTML = msg;
                }
            }
        });
    }

    function remove(cartlineitemid, resultsid) {
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            data: "cartlineitemid=" + cartlineitemid + "&resultsid=" + resultsid + "&action=remove",
            success: function(msg) {
                if (msg == "0" || msg == "1") {
                    alert("System error,please try agin later.");
                }
                else if (msg == "2") {
                    alert("Update side cart line item error,please try agin later.");
                }
                else {
                    document.getElementById(resultsid).innerHTML = msg;
                }
            }
        });
    }

 
// ======== Count the shoppingcart postion ==========
//    var obj = document.getElementById("cart");
//    var tempPos = GetObjWHLT(obj);
//    var divtop = tempPos.top;
//    
//    jQuery(document).ready(function() {
//        obj = document.getElementById("cart");
//        tempPos = GetObjWHLT(obj);
//        divtop = tempPos.top;
//        setBottom();
//    });
   

//    var timer;
//    jQuery(document).ready(function() {
//        jQuery(window).scroll(function() {
//            var divhigh = GetObjWHLT(obj).top;
//            clearInterval(timer);
//            var topScroll = getScroll();
//            var topDiv = "200px";

//            setBottom();
//            
//            var top = 0;
//            var cartheigth = GetObjWHLT(obj).heigth;
//            var thisHeight = document.documentElement.clientHeight;
//            if (topScroll > divtop) {
//                if (cartheigth + parseInt("250px") > thisHeight) {
//                    top = topScroll + thisHeight - cartheigth - parseInt("90px");
//                }
//                else {
//                    top = topScroll + parseInt(topDiv);
//                }
//            }
//            else {
//                if (divhigh > divtop || cartheigth + parseInt("250px") > thisHeight) {
//                    top = divtop;
//                }
//            }
//            if (top > 0) {
//                timer = setInterval(function() {
//                    jQuery("#cart").animate({ "top": top }, 500);
//                }, 500)
//            }
//        })
//    })
// ======== Count the shoppingcart postion ==========

    function getScroll() {
        var bodyTop = 0;
        if (typeof window.pageYOffset != 'undefined') {
            bodyTop = window.pageYOffset;
        } else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
            bodyTop = document.documentElement.scrollTop;
        }
        else if (typeof document.body != 'undefined') {
            bodyTop = document.body.scrollTop;
        }
        return bodyTop
    }
    function setBottom() {

        var cart_height = GetObjWHLT(obj).heigth;
        var div_bottom = document.getElementById("divBottom");
        var div_bottom_top = GetObjWHLT(div_bottom).top;

        if (divtop + cart_height > div_bottom_top) {
            if (div_bottom != null) {
                div_bottom.style.height = cart_height + divtop - div_bottom_top + "px";
            }
        }    
    }
    
    // ============ Call addtocart based on new request by eiwing on 20110706 =============
     function addtocartJson(ppid, qty, resultsid, errorID, cartCountId) {
        var error = document.getElementById(errorID);
        if (error != undefined || error != null) {
            error.innerHTML = "";
        }
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            dataType:"json",
            data: "ppid=" + ppid + "&qty=" + qty + "&resultsid=" + resultsid + "&action=addtocart&jsonData=true&cartCountId=" + cartCountId,
            success: function(data) {
             
                switch(data.msg)
                {
                    case "1":
                        error.innerHTML = "The Value of Qty is invalid.";
                        break;
                    case "2":
                        error.innerHTML = "ProductId is invalid..";
                        break;
                     case "3":
                         error.innerHTML = "This item is out of stock.";
                         break;         
                     case "4":
                         error.innerHTML = "System error,please try again later.";
                         break;   
                     default:  
                     case "0":
                     case "":
                          document.getElementById(resultsid).innerHTML = HTMLDecode(data.Html);
                          updateCartCount(cartCountId, data.totalQty);
                         break;   
                }               
            }
        });
    }
    
    function additemstocartJson(products, resultsid, flag, cartCountId) {
        if (flag == 2) {
            var productGroup = products.split('|');
            for (var i = 0; i < productGroup.length; i++) {
                var types = productGroup[i].split(',');
                var qtyID = types[1];
                var errorID = types[2];
                var qty = 0;
                if (document.getElementById(qtyID) != null) {
                    var qtyValue = document.getElementById(qtyID).value;
                    if (isNaN(qtyValue) || qtyValue < 1) {
                        document.getElementById(errorID).innerHTML = "Qty is invalid.";
                        continue;
                    }
                    else {
                        document.getElementById(errorID).innerHTML = "";
                    }
                    products = products.replace(qtyID, qtyValue);
                }
            }
        }

        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            dataType: "json",
            data: "products=" + products + "&resultsid=" + resultsid + "&flag=" + flag + "&action=additemstocart&jsonData=true&cartCountId=" + cartCountId,
            success: function(data) {

                switch (data.msg) {
                    case "1":
                        alert("System error,please try agin later.");
                        break;
                    case "2":
                        alert("This item is out of stock.");
                        break;
                    case "3":
                        alert("The value of Qty is invalid.");
                        break;
                    default:
                    case "0":
                    case "":
                        var errormsg = "";
                        if (flag == "2") {                            
                            if (data.msg != "" && data.msg.length >= 5) {
                                errormsg = data.msg.substring(0, 5);
                                if (errormsg == "ERROR") {
                                    var _msg = data.msg.substring(6);
                                    var errorGroup = _msg.split('|');
                                    var i = 0;
                                    while (i < errorGroup.length) {
                                        var types = errorGroup[i].split(',');
                                        var msgID = types[0];
                                        var msgError = types[1];
                                        var lblMsg = document.getElementById(msgID);
                                        if (lblMsg != null) {
                                            lblMsg.innerHTML = msgError;
                                        }
                                        i = i + 1;
                                    }
                                }
                            }

                        }
                        document.getElementById(resultsid).innerHTML = HTMLDecode(data.Html);
                        if (errormsg == "") {
                            updateCartCount(cartCountId, data.totalQty);
                        }
                        break;

                }
            }
        });
    }
    
    function updateqtyJson(cartlineitemid, qty, resultsid, cartCountId) {
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            dataType:"json",
            data: "cartlineitemid=" + cartlineitemid + "&qty=" + qty + "&resultsid=" + resultsid + "&action=updateqty&jsonData=true&cartCountId=" + cartCountId,
            success:function(data)
            {
                switch(data.msg)
                {
                    case "1":
                        alert("System error,please try agin later.");
                        break;
                    case "2":
                        alert("Update side cart line item error,please try agin later.");
                        break;
                    case "3":
                        alert("The value of Qty is invalid,please enter agin.");
                        break;
                    default:
                    case "0":
                    case "":
                        document.getElementById(resultsid).innerHTML = HTMLDecode(data.Html);
                        updateCartCount(cartCountId, data.totalQty);
                        break;
                }
            }
        });
    }

    function removeJson(cartlineitemid, resultsid, cartCountId) {
        jQuery.ajax({
            type: "POST",
            url: "JqueryAddtoCart.ashx",
            dataType:"json",
            data: "cartlineitemid=" + cartlineitemid + "&resultsid=" + resultsid + "&action=remove&jsonData=true&cartCountId=" + cartCountId,            
            success: function(data) {
                if (data.msg == "0" || data.msg == "1") {
                    alert("System error,please try agin later.");
                }
                else if (data.msg == "2") {
                    alert("Update side cart line item error,please try agin later.");
                }
                else {
                    document.getElementById(resultsid).innerHTML = HTMLDecode(data.Html);
                    updateCartCount(cartCountId,data.totalQty);
                }
            }
        });
    }
    
    // =========== Updated the shopping cart cartcount value
    function updateCartCount(cartCountId, totoalQty)
    {
        if(cartCountId != null)
        {
            document.getElementById(cartCountId).innerHTML = totoalQty;
        }
    }    
    
     function HTMLEncode(input) 
    { 
            var converter = document.createElement("DIV"); 
            converter.innerText = input; 
            var output = converter.innerHTML; 
            converter = null; 
            return output; 
    } 
    
    function HTMLDecode(input) 
    { 
        var converter = document.createElement("DIV"); 
        converter.innerHTML = input;
        var output = document.all ? converter.innerText : converter.textContent;
        converter = null; 
        return output; 
    } 
    
    // =================== |  Ended by eiwing on 20110706 | ===========================
    
