﻿var __redirect = '';

function error(e, id)
{
    var msg;
    if (e.message)
    {
        msg = e.message;
    }
    else
    {
        msg = e;
    }

    if (id)
    {
        if (id.innerHTML)
        {
            id.innerHTML = msg;
        }
        else
        {
            $(id).innerHTML = msg;
        }
    }
    else
    {
        alert(msg);
    }
};

function show(id)
{
    var s = $(id).style;
    s.display = '';
    s.visibility = 'visible';
};

function hide(id,holdSpace)
{
    var s = $(id).style;
    if (holdSpace)
    {
        s.visibility = 'hidden';
    }
    else
    {
        s.display = 'none';
    }
};

function setPageTitle(s)
{
    $('page_title').innerHTML = s;
};

function viewAd(url)
{
    window.open(url, 'advertisement');
};

function getAjaxErrorHTML(transport)
{
    return 'An unexpected problem occurred while attempting to contact the PyD servers.  Please try again.';
};

function ajaxError(transport, id)
{
    var msg = getAjaxErrorHTML(transport);
    if (id)
    {
        if (id.innerHTML)
        {
            id.innerHTML = msg;
        }
        else
        {
            $(id).innerHTML = msg;
        }
    }
    else
    {
        alert(msg);
    }
};
        
function viewProfile(g)
{
    document.location.href = '/v/divers/' + g + '/x.aspx';
};

function setListItemClasses(parent)
{
    var i, c;
    var visible = [];
    var clean = [];

    try
    {
        // Put visible children into a new array
        for (i = 0; i < parent.childNodes.length; i++)
        {
            c = parent.childNodes[i];
            
            // Record text nodes
            if (3 == c.nodeType)
            {
                clean.push(c);
            }

            // Remember all visible items
            if (c.className && 5 < c.className.length && 'item_' == c.className.substring(0, 5) && (!c.style || 'none' != c.style.display))
            {
                visible.push(parent.childNodes[i]);
            }
        }

        // Set the CSS class names of visible children
        for (i = 0; i < visible.length; i++)
        {
            visible[i].className = getListItemClass(visible, i);
            visible[i].firstChild.className = visible[i].className + '_inner';
        }

        // Remove 'cleaned' nodes for browsers that use text nodes
        for (i = 0; i < clean.length; i++)
        {
            clean[i].parentNode.removeChild(clean[i]);
        }
    }
    catch (e)
    {
        error(e);
    }
};

function getListItemClass(ary, i)
{
    if (i == 0)
    {
        return 'item_first';
    }
    else if (i == (ary.length - 1))
    {
        return 'item_last';
    }
    else
    {
        return 'item_middle';
    }
};

//var PyDEvent = {
//    add: function(obj, type, fn)
//    {
//        if (obj.attachEvent)
//        {
//            obj['e' + type + fn] = fn;
//            obj[type + fn] = function() { obj['e' + type + fn](window.event); };
//            obj.attachEvent('on' + type, obj[type + fn]);
//        }
//        else
//        {
//            obj.addEventListener(type, fn, false);
//        }
//    },
//    remove: function(obj, type, fn)
//    {
//        if (obj.detachEvent)
//        {
//            obj.detachEvent('on' + type, obj[type + fn]);
//            obj[type + fn] = null;
//        }
//        else
//        {
//            obj.removeEventListener(type, fn, false);
//        }
//    },
//    createCallback: function(callbackObject, callbackFunction)
//    {
//        return function()
//        {
//            callbackFunction.apply(callbackObject, arguments);
//        };
//    }
//};

function findPos(o)
{
    if (o.substring)
    {
        o = $(o);
    }
	var x = y = 0;
	if (o.offsetParent)
	{
	    do
	    {
	        x += o.offsetLeft;
	        y += o.offsetTop;
	    } while (o = o.offsetParent);
	}
	return { x: x, y: y };
};


//
// Methods for working with lists
//

function emptyList(list)
{
    if (list.options)
    {
        list.options.length = 0;
    }
};

function getListValue(list)
{
    if (-1 == list.selectedIndex)
    {
        return null;
    }
    else
    {
        return list.options[list.selectedIndex].value;
    }
};

function setListValue(list, val)
{
    for (var i = 0; i < list.options.length; i++)
    {
        if (list.options[i].value == val)
        {
            list.selectedIndex = i;
            return true;
        }
    }

    return false;
};

function setList(list, val, matchText)
{
    for (var i = 0; i < list.options.length; i++)
    {
        if (!matchText && list.options[i].value == val)
        {
            list.selectedIndex = i;
            return true;
        }
        else if (true == matchText && list.options[i].text == val)
        {
            list.selectedIndex = i;
            return true;
        }
    }

    return false;
};

function getListText(list)
{
    if (-1 == list.selectedIndex)
    {
        return null;
    }
    else
    {
        return list.options[list.selectedIndex].text;
    }
};

function addListItem(list, val, text)
{
    var opt = document.createElement("option");
    opt.value = val;
    opt.text = text;
    list = $(list);
    list.options[list.options.length] = opt;
};


//
// String helper methods
//

String.prototype.trim = function() 
{
	var x = this;
	x = x.replace(/^\s*(.*)/, "$1");
	x = x.replace(/(.*?)\s*$/, "$1");
	return x;
};

String.prototype.startsWith = function(s) 
{
    if (s == this.substring(0, s.length))
    {
        return true;
    }
    else
    {
        return false;
    }
};

String.prototype.fixLB = function() 
{
	var x = this;
	x = x.replace(/\r/g, "").replace(/\n/g, "<br />");
	return x;
};

function logout()
{
    try
    {
        new Ajax.Request('/AJAX/Logout.ashx',
            {
                onSuccess: function(transport)
                {
                    document.location.reload(true);
                },
                on400: function(transport)
                {
                    var json = transport.responseText.evalJSON();
                    alert(json.message);
                },
                onFailure: function(transport)
                {
                    ajaxError(transport);
                }
            });
    }
    catch (e)
    {
        error(e);
    }
};

function newGuid()
{
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()).toUpperCase();
};

function S4()
{
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};