﻿/// <reference path="/Scripts/jquery-1.6.4-vsdoc.js" />


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));
}

function setCookie(name, value, path) {
    var today = new Date();
    today.setTime(today.getTime() + 1);
    var expires_date = new Date(today.getTime() + (1 * 24 * 60 * 60 * 1000));
    var c = name + "=" + escape(value) + ";expires=" + expires_date.toGMTString() + //expires.toGMTString()
   ((path) ? ";path=" + path : "");
    document.cookie = c;
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) document.cookie = name + "=" +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ";expires=Thu, 01-Jan-2000 00:00:01 GMT";
}

//---------------------------------------------------------------------------------------------------

function doCompare(item) {
    //returns -1:delete 0:nothing 1:added
    var currentList = getCookie("q", "/");
    var products = [];
    var maxItems = 4;

    if (currentList == null) {
        //add to compare
        setCookie("q", "'" + item + "'", "/");
        return 1; //Item Added
    } else {
        if (currentList.indexOf("'" + item + "'") != -1) { //Found item so delete
            products = currentList.split(",");
            if (products.length == 1) {
                deleteCookie("q", "/");
                document.location = "/Products";
            } else {
                var newItemCount = 0;
                var newItems = "";
                for (i = 0; i < products.length; i++) {
                    if (products[i] != ("'" + item + "'")) {
                        newItemCount++;
                        if (newItemCount == 1) {
                            newItems =  products[i] ;
                        } else { newItems += "," + products[i] ;}
                    }
                } //for
               setCookie("q", newItems, "/");
            }
            return -1; //Item Deleted
        } else {
            //count items, if 4 then cant add
            products = currentList.split(",");
            if (products.length == maxItems) {
                //max allowed
                alert("You can only compare a maximum of 4 items.");
                return 0; // Do Nothing
            } else {
                setCookie("q", (currentList + ",'" + item + "'"), "/");
                return 1; //Item Added
            }
        };
    }
}

function getProductsCookie() {
    var productFound = false;
    var products = [];
    var currentList = getCookie("q", "/");

    if (currentList == null) {
    } else {
        //var filtered = currentList.replace("'", "");
        var filtered = currentList.replace(/'/gi, "");
        var products = filtered.split(",");
    } //if
    return products;
}

function resetCompare() {
    deleteCookie("q", "/");
    $('#navigationCompare').hide();
    $('[data-compare]').html("Add To Compare");
    document.location("/Products");
}

