﻿function GetCursorPosition(e) {

    var posx = 0;
    var posy = 0;

    if (!e) e = window.event;

    if (e) {
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
            algor = '[e.pageX]';
            if (e.clientX || e.clientY) algor += ' [e.clientX] '
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.documentElement.scrollLeft;
            posy = e.clientY + document.documentElement.scrollTop;
            algor = '[e.clientX]';
            if (e.pageX || e.pageY) algor += ' [e.pageX] '
        }
    }

    var posArray = new Array(posx, posy);

    return posArray;


}


function OpenWindow(mypage, myname, w, h, features) {
    if (screen.width) {
        var winl = (screen.width - w) / 2;
        var wint = (screen.height - h) / 2;
    } else { winl = 0; wint = 0; }
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += features;
    win = window.open(mypage, myname, settings);
    win.window.focus();
}


function ShowHideSitemap() {
    var sitemap = document.getElementById("divSitemap");
    if (sitemap.style.display == 'none')
        sitemap.style.display = '';
    else
        sitemap.style.display = 'none';
}

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function getScreenCenterY() {
    var y = 0;

    y = getScrollOffset() + (getInnerHeight() / 2);

    return (y);
}

function getScreenCenterX() {
    return (document.body.clientWidth / 2);
}

function getInnerHeight() {
    var y;
    if (self.innerHeight) // non IE
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight; // IE6
    }
    else if (document.body) // IE
    {
        y = document.body.clientHeight;
    }
    return (y);
}

function getScrollOffset() {
    var y;
    if (self.pageYOffset) // non IE
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        y = document.documentElement.scrollTop; // IE6
    }
    else if (document.body) // IE
    {
        y = document.body.scrollTop;
    }
    return (y);
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function SetCookie(name, value, expires, path, domain, secure) {

    document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

function GetCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function SaveVehicle(id, title, stock) {

    SaveVehicle(id, title, stock, true);
}

function SaveVehicle(id, title, stock, showList) {

    var ckName = 'hbi-dna-saved-vehicles';
    var ck = GetCookie(ckName);
    var val = "DNA" + "\|" + eval(id) + "\|" + title + "\|" + stock;
    var today = new Date();
    var exp = new Date(today.getTime() + 999 * 24 * 60 * 60 * 1000);

    if (SaveVehicleExists(id) != true) {
        if (ck != null)
            val = ck + "\|" + val;

        SetCookie(ckName, val, exp, '/');
    }

    var savelink = document.getElementById('SaveLink' + id);
    if (savelink)
        savelink.innerHTML = "<img src='/resources/images/check.jpg' alt='' border='0' style='margin-bottom:-5px;' />";

    if (showList)
        ShowSavedVehicles();

    ShowVehiclesCount();
}

function ShowVehiclesCount() {

    var ck = GetCookie('hbi-dna-saved-vehicles');
    var dv = document.getElementById('SavedVehiclesCount');
    if (dv) {
        if (ck != null) {
            var vehicles = ck.split("\|");
            if (vehicles.length > 3)
                dv.innerHTML = vehicles.length / 4;
        } else
            dv.innerHTML = "0";
    }
}


function openSavedVehicles() {

    ShowSavedVehicles();
}

function ShowSavedVehicles() {

    var divList = document.getElementById('divSavedVehiclesList');

    if (divList) {


        var ck = GetCookie('hbi-dna-saved-vehicles');
        if (ck != null) {

            var vehicles = ck.split("\|");

            var html = "<table cellpadding='0' cellspacing='0' border='0'>"
            var idx = 0;

            while (idx < vehicles.length) {
                html += "<tr><td style='font-size:14px; font-weight:bold;' nowrap='nowrap'><div style='margin-top:4px; margin-left:10px;'><a href='/Stock/" + vehicles[idx + 3] + ".aspx' >" + vehicles[idx + 2] + " (#" + vehicles[idx + 3] + ")</a> <span onclick='DeleteVehicle(" + vehicles[idx + 1] + ");' style='font-size:10px; color:red; font-weight:normal;' onmouseover='this.style.cursor=\"pointer\";' onmouseout='this.style.cursor=\"default\";'><img src='/resources/images/garbage_empty.gif' height='12' width='12' alt='Delete' /><span></div></td></tr>";
                idx = idx + 4;
            }
            html += "</table>"

            divList.innerHTML = html;
        }
        else
            divList.innerHTML = "<span style='font-size:11px; font-weight:italic;'>You haven't added any vehicles to your list.</span>";

        $("#SavedVehicles").dialog('open');
        //openSavedVehicles();
        //div.style.display = '';
    }
}

function HideSavedVehicles() {

    var div = document.getElementById('divSavedVehicles');

    if (div)
        div.style.display = 'none';
}

function AutoHideSavedVehicles(evt) {
    var div = document.getElementById('divSavedVehicles');
    var e = (window.event) ? window.event : evt;

    if (div && e.srcElement && e.srcElement.tagName == "DIV") {
        setTimeout('HideSavedVehicles()', 3000);
    }
    else if (div && e.target && e.target.tagName == "DIV") {
        setTimeout('HideSavedVehicles()', 3000);
    }
}

function DeleteVehicle(id) {

    var ck = GetCookie('hbi-dna-saved-vehicles');
    if (ck != null) {

        var vehicles = ck.split("\|");
        var idx = 0;
        var val = '';

        while (idx < vehicles.length) {
            if (vehicles[idx + 1] != id) {
                if (val.length > 0)
                    val += "\|";

                val += "DNA" + "\|" + vehicles[idx + 1] + "\|" + vehicles[idx + 2] + "\|" + vehicles[idx + 3];
            }
            idx = idx + 4;
        }

        var ckName = 'hbi-dna-saved-vehicles';
        var today = new Date();
        var exp = new Date(today.getTime() + 999 * 24 * 60 * 60 * 1000);

        SetCookie(ckName, val, exp, '/');
    }
    ShowVehiclesCount();
    ShowSavedVehicles();

}

function SaveVehicleExists(id) {

    var ck = GetCookie('hbi-dna-saved-vehicles');

    if (ck != null) {

        var vehicles = ck.split("\|");
        var idx = 0;
        var found = false;

        while (idx < vehicles.length) {
            if (vehicles[idx + 1] == id) {
                found = true;
            }
            idx = idx + 4;
        }

    }
    return found;
}

function CreateBookmark() {

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(document.title, document.location.href, "");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(document.location.href, document.title);
    } else if (window.opera && window.print) {  // Opera Hotlist
        return true;
    }
}

function PrintPage() {

    window.print();
}

function ShowCPOInfo(obj, cpoInfo) {

    var dv = document.getElementById(cpoInfo);
    if (dv) {
        dv.style.display = '';
        dv.style.left = (findPosX(obj) - 250) + 'px';
        dv.style.top = (findPosY(obj) + 15) + 'px';
    } else { alert('not found'); }
}

function HideCPOInfo(cpoInfo) {
    var dv = document.getElementById(cpoInfo);
    if (dv) {
        dv.style.display = 'none';
    }
}

function ShowBrochure(id) {

    window.open('/Brochure/' + id + '.aspx', 'brochure', 'height=600,width=750,toolbar=no,status=yes,scrollbars=yes');
}


function SetMenuActive(val) {
    switch (val) {
        case 'MenuHome':
            $('#MenuHome').attr('style', 'height:52px; width:65px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuInventory':
            $('#MenuInventory').attr('style', 'height:52px; width:100px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuLastChance':
            $('#MenuLastChance').attr('style', 'height:52px; width:165px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuExtra':
            $('#MenuExtra').attr('style', 'height:52px; width:125px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuFinance':
            $('#MenuFinance').attr('style', 'height:52px; width:90px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuService':
            $('#MenuService').attr('style', 'height:52px; width:145px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuCommunity':
            $('#MenuCommunity').attr('style', 'height:52px; width:115px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
        case 'MenuContact':
            $('#MenuContact').attr('style', 'height:52px; width:115px; float: left; background-color:black; filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3;');
            break;
    }


}

function SetMenuInActive(val) {
    switch (val) {
        case 'MenuHome':
            $('#MenuHome').attr('style', 'height:54px; width:65px; float: left;');
            break;
        case 'MenuInventory':
            $('#MenuInventory').attr('style', 'height:54px; width:100px; float: left;');
            break;
        case 'MenuLastChance':
            $('#MenuLastChance').attr('style', 'height:54px; width:165px; float: left;');
            break;
        case 'MenuExtra':
            $('#MenuExtra').attr('style', 'height:54px; width:125px; float: left;');
            break;
        case 'MenuFinance':
            $('#MenuFinance').attr('style', 'height:54px; width:90px; float: left;');
            break;
        case 'MenuService':
            $('#MenuService').attr('style', 'height:54px; width:145px; float: left;');
            break;
        case 'MenuCommunity':
            $('#MenuCommunity').attr('style', 'height:54px; width:115px; float: left;');
            break;
        case 'MenuContact':
            $('#MenuContact').attr('style', 'height:54px; width:115px; float: left;');
            break;
    }
}        
    
function SaveLocation(location) {
    var ckName = 'dna-location';
    var ck = GetCookie(ckName);
    var val = location;
    var today = new Date();
    var exp = new Date(today.getTime() + 999 * 24 * 60 * 60 * 1000);

    SetCookie(ckName, val, exp, '/');
}
function LoadLocation() {
    var ck = GetCookie('dna-location');
    return ck;
}
