﻿function updateCartTotal() {
    return;
//    var count = getCookie("cartcount");
//    if(count != null) {
//        $("#cartCount").html(count);
//    }
}


function addToCart(id,quantity) {
    if( quantity == "" || quantity == undefined ) {
        quantity = 1;
    }
    var queryString = "Id=" + id + "&Quantity=" + quantity;

    showProgressDialog("Adding item");

    $.ajax(
    {
        cache: false,
        url: "/catalog/addtocart.aspx?" + queryString,
        success: function (x) {
            //updateCartTotal();
            closeProgressDialog();
        }
    });
}

var $dialog;

// dialog progress
function showProgressDialog(title) {
    if ($dialog) {
        $dialog.remove();
    }

    $dialog = $(getProgressDialogContent())
        .dialog({
        minHeight: 0,
        width: 250,
        resizable: false,
        modal: true,
        closeOnEscape: false,
        title: title
    });
}

function getProgressDialogContent() {
    var progressMessage = "<p>Adding item to cart...</p>";
    var progressImage = "<center><img src='/images/addtocartwait.gif' /></center>";

    var dialogText = "<div><center>" + progressMessage + progressImage + "</center></div>";

    return dialogText;
}

function closeProgressDialog() {
    $dialog.dialog('close');
}


function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) &&
        (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;

    return unescape(document.cookie.substring(len, end));
    //return value;
}

function shoppingCrumbs() {
    $("#AlternateBreadCrumb").show();
    $("#AlternateBreadCrumb").append(generateCrumb($("#tmpBreadCrumb").text()));
}

function generateCrumb(toJson) {
    var result = "";
    if (toJson.length > 0) {
        var json = eval('(' + toJson + ')');
        for (var i = 0; i < json.length; i++) {
            var text = "<a href='" + json[i].link + "'>" + json[i].name + "</a>";
            result = result.concat(text);
            if (i < json.length - 1) {
                result = result.concat(" > ");
            }
        }
    }
    return result;
}

function generateCrumbFromArray(json) {
    var result = "";
    if (json.length > 0) {
        for (var i = 2; i < json.length; i++) {
            var text = "";
            if (i + 1 == json.length)
                text = "<a href='" + json[i].link + "' style='font-weight:bold;'>" + json[i].name + "</a>";
            else
                text = "<a href='" + json[i].link + "'>" + json[i].name + "</a>";
            result = result.concat(text);
            if (i < json.length - 1) {
                result = result.concat(" > ");
            }
        }
    }
    return result;
}
