﻿function SearchView(anchor) {
    var location = $(anchor).attr("href");
    location = location + "?sr=1";
    for (var s in _quickSearch_Values) {
        location += "&" + s + "=" + _quickSearch_Values[s];
    }
    document.location = location;
    return false;
}

function CompareDiv(div, lid, maxIds) {
    var cbox = $("input[type='checkbox']", $(div)).get(0);

    if (cbox) {
        cbox.checked = !cbox.checked;

        var ret = _Compare(cbox.checked, lid, maxIds);

        if (!ret)
            cbox.checked = false;
    }
}

function _Compare(isChecked, lid, maxIds) {
     try
        {
            var compareIds = new String();
            var cookieData = document.cookie.split(";");
            compareIds = getCompareData(cookieData);
            if( isChecked ) //add to the cookie
            {
                if( compareIds == null || compareIds.indexOf("=") < 0 )
                {
                    compareIds = "CompareIds=" + lid;
                }
                else
                {
                    var tmpIds = compareIds.split("=")[1].split(",");
                    if( tmpIds.length >= maxIds )
                    {
                        alert("You can only compare a maximum of " + maxIds + " Ads");
                        return false;
                    }
                    //Only add the value if it doesn't exist
                    if( compareIds.indexOf( lid ) < 0 )
                    {
                        if( compareIds.indexOf("=") == compareIds.length - 1 )
                        {
                            compareIds += lid;
                        }
                        else
                        {
                            compareIds += "," + lid;
                        }
                    }
                }
            }
            else //remove from cookie
            {
                if( compareIds != null )
                {
                    var tmpIds = compareIds.split("=")[1].split(",");
                    var newComparIds = new String();
                    newComparIds = "CompareIds=";
                    for( i=0; i<tmpIds.length; i++ )
                    {
                        if( tmpIds[i] != lid )
                        {
                            if( i > 0 )
                            {
                                newComparIds += ",";
                            }
                            newComparIds += tmpIds[i];
                        }
                    }
                    compareIds = newComparIds;
                }
            }
            if( compareIds != null )
            {
                if( compareIds.indexOf( "path" ) < 0 )
                {
                    compareIds += ";path=/";
                }
                document.cookie = compareIds;
            }
        }
        catch (e) { }
        return true;
    }
    function getCompareData( cookieData )
    {
        for( i=0; i<cookieData.length; i++ )
        {
            if( cookieData[i].indexOf("CompareIds") >= 0 )
            {
                return cookieData[i];
            }
        }
        return;
    }
    function removeCompare(lid) {
        var compareIds = new String();
        var cookieData = document.cookie.split(";");
        
        compareIds = getCompareData(cookieData);
        if (compareIds != null) {
            var tmpIds = compareIds.split("=")[1].split(",");
            var newComparIds = new String();
            newComparIds = "CompareIds=";
            for (i = 0; i < tmpIds.length; i++) {
                if (tmpIds[i] != lid) {
                    if (i > 0) {
                        newComparIds += ",";
                    }
                    newComparIds += tmpIds[i];
                }
            }
            compareIds = newComparIds;
            if (compareIds != null) {
                if (compareIds.indexOf("path") < 0) {
                    compareIds += ";path=/";
                }
                document.cookie = compareIds;
            }
        }
        document.location = document.location;
    }

    function isEnterKeyEvent(e) {
        var key;
        var event;

        if (window.event) {
            event = window.event;
            key = window.event.keyCode;     //IE
        }
        else {
            key = e.which;     //firefox
            event = e;
        }
        return key == 13;
    }