﻿var Common_Ajax_Error = '<div class="error"><strong>Oops!</strong> Looks like we encountered a problem. We\'ll get that fixed as soon as possible.</div>';

function Common_Search(o)
{
    var query = o.value;
    var url = Common_Search_Url + '&q=' + Common_UrlEncode(query);
    document.location.href = url;
}

function Common_Search_KeyPress(o, e)
{
    if (Common_EnterPressed(o, e))
    {
        Common_Search(o);
        return false;
    }
    else
    {
        return true;
    }
}

function Common_AddClickSource(a, source)
{
    if (a.href.indexOf("source=") == -1)
    {
        var query = a.href.indexOf("?") > -1 ? "&" : "?";
        a.href += query + "source=" + source;
    }
}

function Common_EndRequest(sender, args)
{
    if (args.get_error() != undefined)
    {
        alert('Oops!\n\nLooks like we encountered a problem.\n\nWe\'ll get the problem fixed soon so it won\'t happen again.\n\nThanks for using the site.\n\nWhistleOut Development Team');
        args.set_errorHandled(true);
    }
}

function Common_EnterPressed(o, e)
{
    var keycode;
    if (window.event)
    {
        keycode = window.event.keyCode;
    }
    else if (e)
    {
        keycode = e.which;
    }
    else
    {
        return false;
    }
    if (keycode == 13)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function Common_UrlEncode(input)
{
    var output = '';
    var x = 0;
    input = input.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < input.length)
    {
        var match = regex.exec(input.substr(x));
        if (match != null && match.length > 1 && match[1] != '')
        {
            output += match[1];
            x += match[1].length;
        } else
        {
            if (input[x] == ' ')
                output += '+';
            else
            {
                var charCode = input.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}

function Common_CancelBubble(e)
{

    if (window.event)
    {
        window.event.cancelBubble = true;
    }
    else if (e)
    {
        e.cancelBubble = true;
    }

}

function Common_LoadingMessage_Big(message)
{
    return '<div class="loading-message"><div><img src="/Images/ajax-loader.gif" align="absmiddle" />' + message + '</div></div>';
}

function Common_LoadMap(controlID, latitude, longitude, title, description)
{

    // Load map
    var map = new VEMap(controlID);
    map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap(new VELatLong(latitude, longitude), 15, 'r', false);
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);

    // Add pin
    var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));
    pin.SetTitle(title);
    pin.SetDescription(description);
    pin.SetCustomIcon('<img src=\"/Images/map-icon.png"/>');
    map.AddShape(pin);

}

function Common_Pws(u, a, v, p)
{
    var src = '/pws.gif?u=' + u + '&a=' + a + '&v=' + v + '&p=' + p;
    Common_PwsImage(src);
}

function Common_GC(i, l)
{
    var src = 'http://www.googleadservices.com/pagead/conversion/' + i + '/?value=1.0&guid=ON&script=0&label=' + l;
    Common_PwsImage(src);
}

function Common_PwsImage(src)
{
    var r = Math.floor(Math.random() * 9999999999);
    var image = new Image(1, 1);
    image.src = src + '&r=' + r;
    image.onload = function() { return; };
}

function Common_ValidateCurrencyWithSymbol(value)
{
    var re = new RegExp(__reCurrencyWithSymbol);
    return re.test(value);
}

function Common_AddTooltip(imageID, htmlID)
{

    if (wo$(imageID) != null)
    {
        if (htmlID == null || htmlID == undefined || htmlID == '')
        {
            wo$(imageID).tooltip(
            {
                showURL: false,
                track: false,
                delay: 0
            });
        }
        else
        {
            wo$(imageID).tooltip(
            {
                bodyHandler: function()
                {
                    return (wo$(htmlID).html());
                },
                showURL: false,
                track: false,
                delay: 0
            });
        }
    }
}

function Common_CompareChecked(id)
{
    return wo$('#' + id).find('input:checkbox[checked=true]');
}

function Common_CompareClick(id, topID, cb, e)
{
    var count = Common_CompareChecked(id).length;

    if (count == 0)
    {
        wo$('#' + topID).hide("fast");
    }
    else
    {
        wo$('#' + topID).show("fast");
        if (count > 3)
        {
            cb.checked = false;
            alert('You can only select a maximum of 3 items to compare side-by-side');
        }
    }

    Common_CancelBubble(e);
}

function Common_Compare(id)
{
    var count = Common_CompareChecked(id).length;
    if (count < 2)
    {
        alert('Please select at least 2 items to compare side-by-side');
        return false;
    }
    else
    {
        var urls = [];
        Common_CompareChecked(id).each(function()
        {
            urls.push(wo$(this).val());
        });
        var url = __compareBase + '/' + urls.join('-compared-');
        if (__compareQuery != '')
        {
            url += '?' + __compareQuery;
        }
        window.setTimeout(function() { location.href = url; }, 200)
        return false;
    }
}

function Common_AddToggle(toggleID, targetID)
{
    wo$(toggleID).click(function()
    {
        wo$(targetID).toggle();
        var toggle = wo$(toggleID);
        if (toggle.is('.icon-maximise'))
        {
            toggle.removeClass('icon-maximise');
            toggle.addClass('icon-minimise');
        }
        else
        {
            toggle.removeClass('icon-minimise');
            toggle.addClass('icon-maximise');
        }
    });
}

var panes = new Array();

function setupPanes(containerId, defaultTabId)
{
    panes[containerId] = new Array();
    var maxHeight = 0; var maxWidth = 0;
    var container = document.getElementById(containerId);
    var paneContainer = document.getElementById(containerId + "-panes");
    var paneList = paneContainer.childNodes;
    for (var i = 0; i < paneList.length; i++)
    {
        var pane = paneList[i];
        if (pane.nodeType != 1 || pane.tagName != "DIV") continue;
        if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
        if (pane.offsetWidth > maxWidth) maxWidth = pane.offsetWidth;
        panes[containerId][pane.id] = pane;
    }
}

function showPane(paneId, activeTab)
{
    for (var con in panes)
    {
        activeTab.blur();
        activeTab.className = "tab-active";
        if (panes[con][paneId] != null)
        {
            var pane = document.getElementById(paneId);
            pane.style.display = "block";
            var container = document.getElementById(con);
            var tabs = container.getElementsByTagName("ul")[0];
            var tabList = tabs.getElementsByTagName("a")
            for (var i = 0; i < tabList.length; i++)
            {
                var tab = tabList[i];
                if (tab != activeTab) tab.className = "tab-disabled";
            }
            for (var i in panes[con])
            {
                var pane = panes[con][i];
                if (pane == undefined || pane.tagName != "DIV") continue;
                if (pane.id == paneId) continue;
                pane.style.display = "none"
            }
        }
    }
    return false;
}

function Common_MegaHoverOver()
{
    if (wo$.browser.msie && parseInt(wo$.browser.version) == 6)
    {
        wo$('select').css("visibility", "hidden");
        wo$(this).find(".nav-wo-sub").show();
    }
    else
    {
        wo$(this).find(".nav-wo-sub").stop().fadeTo('fast', 1).show();
    }
}

function Common_MegaHoverOut()
{
    if (wo$.browser.msie && parseInt(wo$.browser.version) == 6)
    {
        wo$(this).find(".nav-wo-sub").hide();
        wo$('select').css("visibility", "visible");
    }
    else
    {
        wo$(this).find(".nav-wo-sub").stop().fadeTo('fast', 0, function() { wo$(this).hide(); });
    }
}

function Common_SliderTabs(headerID, bodyID)
{

    //Get the height of the first item
    wo$(bodyID + ' #mask').css({ 'height': wo$(bodyID + ' #panel-1').height() });

    //Calculate the total width - sum of all sub-panels width
    //Width is generated according to the width of #mask * total of sub-panels
    wo$(bodyID + ' #panel').width(parseInt(wo$(bodyID + ' #mask').width() * wo$(bodyID + ' #panel div').length));

    //Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
    wo$(bodyID + ' #panel div').css('visibility', 'visible').width(wo$(bodyID + ' #mask').width());

    //Get all the links with rel as panel
    wo$(headerID + ' a[rel=panel]').click(function()
    {

        //Get the height of the sub-panel
        var panelheight = wo$(wo$(this).attr('href')).height();

        //Set class for the selected item
        wo$(headerID + ' a[rel=panel]').removeClass('tab-active');
        wo$(this).addClass('tab-active');

        //Resize the height
        wo$(bodyID + ' #mask').animate({ 'height': panelheight }, { queue: false, duration: 500 });

        //Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
        wo$(bodyID + ' #mask').scrollTo(wo$(this).attr('href'), 800);

        //Discard the link default behavior
        return false;
    });
}
