﻿function comparenow() {
    var vidstr = GetCookie("comparevid");
    if (vidstr != null) {
        var vidarray = vidstr.split(',');
        if (vidarray.length > 2) {
            window.open("/compare.aspx?vidstr=" + vidstr, "Compare", "menubar=0, toolbar=0, status=1, resizable=1, width=" + screen.width + ", height=" + screen.height + ", scrollbars=1, top=0, left=0")
        }
        else {
            alert("Please select at least 2 vehicles to compare.");
        }
    }
    else {
        alert("Please select at least 2 vehicles to compare.");
    }
}

function clearCompare() {
    var vidStr = GetCookie("comparevid");
    if (vidStr != null) {
        var vidList = vidStr.split(",");
        for (var i in vidList) {
            try {
                document.getElementById("t" + Number(vidList[i]).toString()).className = "td_compare";
            }
            catch (oops) {
            }
            try {
                document.getElementById("c" + Number(vidList[i]).toString()).checked = false;
            }
            catch (oops) {
            }
        }
    }
    document.cookie = "comparevid=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    document.getElementById("totalvid").innerHTML = "<b>No Vehicles</b> in Compare List";
    document.getElementById("totalvid1").innerHTML = "<b>No Vehicles</b> in Compare List";
}

function changeCompare(id) {
    if (document.getElementById("c" + id).checked == true) {
        document.getElementById("t" + id).className = "td_compare_selected";
        AddVid(id);
    }
    else {
        document.getElementById("t" + id).className = "td_compare";
        RemoveVid(id);
    }
}

function RemoveVid(id) {
    var vidstr = GetCookie("comparevid");
    var vidstrnew = vidstr.replace(id + ",", "");
    if (vidstrnew.length > 0) {
        SetCookie(vidstrnew);
        ShowTotalVid();
    }
    else {
        clearCompare();
    }
}

function AddVid(id) {
    var vidstr = GetCookie("comparevid");
    if (vidstr == null) {
        vidstr = id + ",";
    }
    else {
        vidstr = vidstr + id + ",";
    }
    SetCookie(vidstr);
    ShowTotalVid();
}

function ShowTotalVid() {
    var vidstr = GetCookie("comparevid");
    if (vidstr != null) {
        var vidarray = vidstr.split(',');
        if (vidarray.length > 1) {
            if (vidarray.length > 2) {
                document.getElementById("totalvid").innerHTML = "<b>" + (vidarray.length - 1) + "</b> Vehicles in Compare List";
            }
            else {
                document.getElementById("totalvid").innerHTML = "<b>" + (vidarray.length - 1) + "</b> Vehicles in Compare List";
            }
            document.getElementById("totalvid1").innerHTML = document.getElementById("totalvid").innerHTML;
        }
        else {
            document.getElementById("totalvid").innerHTML = "<b>No Vehicles</b> in Compare List";
            document.getElementById("totalvid1").innerHTML = "<b>No Vehicles</b> in Compare List";
        }
    }
    else {
        document.getElementById("totalvid").innerHTML = "<b>No Vehicles</b> in Compare List";
        document.getElementById("totalvid1").innerHTML = "<b>No Vehicles</b> in Compare List";
    }
}

function CheckCompare() {
    var vidstr = GetCookie("comparevid");
    if (vidstr != null) {
        var vidarray = vidstr.split(',');
        for (var i = 0; i < vidarray.length; i++) {
            if (vidarray[i]) {
                if (document.getElementById("c" + vidarray[i])) {
                    document.getElementById("c" + vidarray[i]).checked = true;
                    document.getElementById("t" + vidarray[i]).className = "td_compare_selected";
                }
            }
        }
    }
}

function SetCookie(value) {
    if (value != 'none') {
        var today = new Date();
        var expires = 1000 * 60 * 30;
        var expires_date = new Date(today.getTime() + (expires));
        document.cookie = "comparevid=" + value + "; expires=" + expires_date.toGMTString();
    }
    else {
        var d = new Date();
        document.cookie = "comparevid=''; expires=" + d.toGMTString();
    }
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
