﻿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)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13)
    {
        Common_Search(o);
        return false;
    }
    else
        return true;
}
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 r = Math.floor(Math.random() * 9999999999);
    var image = new Image(1, 1);
    image.src = '/pws.gif?u=' + u + '&a=' + a + '&v=' + v + '&r=' + r + '&p=' + p;
    image.onload = function() { };
}

function Common_GC(i, l)
{
    var r = Math.floor(Math.random() * 9999999999);
    var image = new Image(1, 1);
    image.src = 'http://www.googleadservices.com/pagead/conversion/' + i + '/?value=1.0&guid=ON&script=0&label=' + l + '&r=' + r;
    image.onload = function() { };
}

function Common_AddTooltip(imageID, htmlID)
{

    if (jQuery(imageID) != null)
    {
        if (htmlID == null || htmlID == undefined || htmlID == '')
        {
            jQuery(imageID).tooltip(
            {
                showURL: false,
                track: false,
                delay: 0
            });
        }
        else
        {
            jQuery(imageID).tooltip(
            {
                bodyHandler: function()
                {
                    return (jQuery(htmlID).html());
                },
                showURL: false,
                track: false,
                delay: 0
            });
        }
    }
}

function Common_AddToggle(toggleID, targetID)
{
    jQuery(toggleID).click(function()
    {
        jQuery(targetID).toggle();
        var toggle = jQuery(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)
{
    // go through the DOM, find each tab-container
    // set up the panes array with named panes
    // find the max height, set tab-panes to that height
    panes[containerId] = new Array();
    var maxHeight = 0; var maxWidth = 0;
    var container = document.getElementById(containerId);
    var paneContainer = container.getElementsByTagName("div")[0];
    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;
        pane.style.display = "none";
    }
    //   paneContainer.style.height = maxHeight + "px";
    //   paneContainer.style.width  = maxWidth + "px";
    document.getElementById(defaultTabId).onclick();
}

function showPane(paneId, activeTab)
{
    // make tab active class
    // hide other panes (siblings)
    // make pane visible

    for (var con in panes)
    {
        activeTab.blur();
        activeTab.className = "tab-active";
        if (panes[con][paneId] != null)
        { // tab and pane are members of this container
            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;
}
