﻿function reloadCart() {
    var timestamp = Number(new Date());

    //document.write("http://" + domainName + "/ajax.ashx?e=GC&ief=" + timestamp);

    $.getJSON("http://" + domainName + "/ajax.ashx?e=GC&ief=" + timestamp,
        function(data) {
            displayCart(data);
        }
    );
}

function displayCart(data) {
    if (data.cart != null) {
        var mycartid = '';
        var mywebsiteid = '';
        var subtotal = 0;
        var shippingcost = 0;
        $("#Cart").html("");

        $.each(data.cart.items, function(i, item) {
            $("#Cart").append("<div style='background-image:url(/images/footer/bg-dots-vertical.gif);background-repeat: repeat-x; background-position: center top; font-size: 5px'>&nbsp;</div>");

            $("#Cart").append("<table cellpadding='0' cellspacing='0' style='width: 100%; text-align: left'><tr>" +
                                "<td valign='top'>" +
                                    "<a href='/" + item.url + "'><img src='/images/products/tiny/" + item.productphotoid + ".png' width='50' border='0' alt='" + item.title + "' style='margin-right: 5px' /></a></td>" +
                                "<td valign='top' style='width: 90%'>" +
                                    "<a href='/" + item.url + "'>" + item.title + "</a>" +
                                    "<div class='cookie-crumbs' style='margin-top: 3px'>" + item.option1 + "<br>" + item.option2 + "<br>Quantity: <input type='text' id='qty" + item.id + "' value='" + item.qty + "' class='tinytxt' /><br>Price: <span id='line" + item.id + "'>" + formatCurrency(item.price) + "</span></div>" +
                                    "<div class='cookie-crumbs' style='text-align: right'>" +
                                    "<a href='javascript:void(\"updateitem\");' onclick='javascript:updateCartItem(\"" + item.id + "\");'>Update</a> | " +
                                    "<a href='javascript:void(\"removeitem\");' onclick='javascript:removeCartItem(\"" + item.id + "\");'>Remove</a>" +
                                    "</div>" +
                                "</td></tr></table>");
            subtotal = subtotal + (parseInt(item.qty) * Math.round(item.price * 100));

            if ((subtotal * 0.01) >= item.freeshippingminimum) {
                shippingcost = 0;
            } else {
                shippingcost = Math.round(item.shippingcost * 100);
            }

            mycartid = item.cartid
            mywebsiteid = item.websiteid
        });

        $("#Cart").append("<div style='background-image:url(/images/footer/bg-dots-vertical.gif);background-repeat: repeat-x; background-position: center top; font-size: 3px'>&nbsp;</div>");

        $("#Cart").append("<table cellpadding='2' cellspacing='0' style='float: right; margin-bottom: 5px'>" +
                              "<tr><td style='text-align: right'>Subtotal:</td><td id='cartsubtotal' style='text-align: right'>" + subtotal * 0.01 + "</td></tr>" +
                              "<tr><td style='text-align: right'>Shipping:</td><td id='shippingtotal' style='text-align: right'>" + shippingcost * 0.01 + "</td></tr>" +
                              "<tr><td style='text-align: right'>Total:</td><td id='carttotal' style='text-align: right; font-weight: bold'>" + ((subtotal + shippingcost) * 0.01) + "</td></tr>" +
                          "</table>");

        //$("#Cart").append('<div style="text-align: center"><a href="https://' + httpHost + '/checkout.aspx"><img src="/images/common/button-checkout.gif" width="155" height="28" alt="checkout" border="0" /></a></div>');

//        $("#Cart").append('<div style="text-align: center">' +
//                            '    <a href="javascript:void(\'checkout\')" onclick="document.location.href = \'https://www.performancefuel.com/checkout.aspx?cid=' + mycartid + '&wid=' + mywebsiteid + '\';">' +
//                            '    <img src="/images/common/button-checkout.gif" width="155" height="28" alt="checkout" border="0" />' +
//                            '    </a>' +
//                            '</div>');

//        $("#Cart").append('<div style="text-align: center">' +
//                            '    <input type="image" name="gotocheckout" value="gotocheckout" src="/images/common/button-checkout.gif" width="155" height="28" alt="checkout" border="0" />' +
//                            '</div>');

        $("#cartsubtotal").formatCurrency();
        $("#shippingtotal").formatCurrency();
        $("#carttotal").formatCurrency();
    }

    if (data.error != null) {
        $("#Cart").html(data.error);
    }

    if ($("#" + panelCart).hasClass("fadedout") == true) {
        $("#" + panelCart).removeClass("fadedout").fadeTo("fast", "1", function() {
            if (jQuery.browser.msie) {
                document.getElementById(panelCart).style.removeAttribute('filter');
            }
        });
    } else {
        $("#" + panelCart).animate({ opacity: 'show', height: 'show' }, "400", function() {
            if (jQuery.browser.msie) {
                document.getElementById(panelCart).style.removeAttribute('filter');
            }
        });
    }
}

function updateCartItem(cartItemId) {
    var timestamp = Number(new Date());

    var quantity = $("#qty" + cartItemId).val();

    if (quantity <= 99) {
        $.getJSON("http://" + domainName + "/ajax.ashx?e=UIQ&ciid=" + cartItemId + "&qty=" + quantity + "&ief=" + timestamp,
            function(data) {
                $("#" + panelCart).addClass("fadedout").fadeTo("fast", "0.1", function() {
                    displayCart(data);
                });
            }
        );
    } else {
        alert('Please enter a quantity below 100.');
    }
}

function removeCartItem(cartItemId) {
    var timestamp = Number(new Date());
    $.getJSON("http://" + domainName + "/ajax.ashx?e=RIFC&ciid=" + cartItemId + "&ief=" + timestamp,
            function(data) {
                $("#" + panelCart).animate({ opacity: 'hide', height: 'hide' }, "400", function() {
                    displayCart(data);
                });
            }
        );
}

function formatCurrency(nStr) {
    nStr += '';
    nStr = parseFloat(nStr).toFixed(2);
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';

    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }

    if (x1 + x2 == 0) {
        return '$0.00';
    } else {
        if (x1 + x2 == 'NaN') {
            return '$0.00';
        } else {
            return '$' + x1 + x2;
        }
    }
}

var win = null;
var ScreenWidth = null;
var ScreenHeight = null;

function openWindow(mypage, myname) {
    { ScreenWidth = 900 }
    { ScreenHeight = 700 }
    var TopPosition = (screen.height) ? ((screen.availHeight - ScreenHeight) / 2) - 15 : 0;
    var LeftPosition = (screen.width) ? (screen.availWidth - ScreenWidth) / 2 : 0;
    settings = "height=" + ScreenHeight + ",width=" + ScreenWidth + ",top=" + TopPosition + ",left=" + LeftPosition + ",toolbar=no,titlebar=yes,directories=no,fullscreen=no,status=yes,scrollbars=yes,resizable=yes,menubar=no"
    win = window.open(mypage, encodeName(myname), settings)
    win.window.focus();
}

function smallWindow(mypage, myname) {
    { ScreenWidth = 640 }
    { ScreenHeight = 480 }
    var TopPosition = (screen.height) ? ((screen.availHeight - ScreenHeight) / 2) - 15 : 0;
    var LeftPosition = (screen.width) ? (screen.availWidth - ScreenWidth) / 2 : 0;
    settings = "height=" + ScreenHeight + ",width=" + ScreenWidth + ",top=" + TopPosition + ",left=" + LeftPosition + ",toolbar=no,titlebar=yes,directories=no,fullscreen=no,status=no,scrollbars=yes,resizable=yes,menubar=no"
    win = window.open(mypage, encodeName(myname), settings)
    win.window.focus();
}

function tinyWindow(mypage, myname) {
    { ScreenWidth = 450 }
    { ScreenHeight = 350 }
    var TopPosition = (screen.height) ? ((screen.availHeight - ScreenHeight) / 2) - 15 : 0;
    var LeftPosition = (screen.width) ? (screen.availWidth - ScreenWidth) / 2 : 0;
    settings = "height=" + ScreenHeight + ",width=" + ScreenWidth + ",top=" + TopPosition + ",left=" + LeftPosition + ",toolbar=no,titlebar=no,directories=no,fullscreen=no,status=no,scrollbars=yes,resizable=no,menubar=no"
    win = window.open(mypage, encodeName(myname), settings)
    win.window.focus();
}

function encodeName(name) {
    name = name.replace(/\+/g, "");
    name = name.replace(/\./g, "");
    name = name.replace(/\-/g, "");
    name = name.replace(/\//g, "");
    name = name.replace(/\?/g, "");
    name = name.replace(/\=/g, "");
    name = name.replace(/\&/g, "");
    return escape(name);
}
