//==================================================================================================================
//  Trim Whitespace
//==================================================================================================================

String.prototype.trim=function()
{ 
    return this.replace(/^\s*|\s*$/g,'');
}
String.prototype.ltrim=function()
{
    return this.replace(/^\s*/g,'');
}
String.prototype.rtrim=function()
{
    return this.replace(/\s*$/g,'');
}

//==================================================================================================================
//  Miscellaneous Functions
//==================================================================================================================

function ajaxRender(url,elem)
{
    Ext.Ajax.request({
       url: url,
       success: function(response, opts) {
          Ext.getDom(elem).innerHTML = response.responseText;
       },
       failure: function(response, opts) {
          Ext.getDom(elem).innerHTML = "Unable to retrieve data";
       }
    });
}

function setURLParameter(url,key,val)
{
    var newurl = url;
    if (url.indexOf("&" + key + "=" + val) != -1) return url;
    var regex = "/&" + key + "=[^&]*\\w/";
    newurl = url.replace(eval(regex),"&" + key + "=" + val);
    if (newurl == url) newurl +=  "&" + key + "=" + val;
    return newurl;
}

function showMessage(mesgText,elemName,mesgType)
{
    var elem = this.document.getElementById(elemName);
    var mesg = mesgText;
    
    if (elem)
    {
    if (mesgType == "error")
        mesg = '<span class="errorTitle">ERROR</span> &nbsp;' + mesg;
        elem.innerHTML = mesg;
        elem.style['display'] = "";
    }
    else
    {
        if (mesgType == "error")
            mesg = "ERROR: " + mesg;
        alert (mesg);
    }
}

//==================================================================================================================
//  Date Functions
//==================================================================================================================

function showDate(d)
{
    var m_names = new Array("January", "February", "March",
    "April", "May", "June", "July", "August", "September",
    "October", "November", "December");

    var dt_day   = d.getDate();
    var dt_month = d.getMonth();
    var dt_year  = d.getFullYear();
    document.write(dt_day + " " + m_names[dt_month] + " " + dt_year);
}

function showDate2(d)
{
    var str = d.toUTCString();
    str = str.substring(5,str.length);
    var a = str.split(" ");
    var dstr = a[0] + ' ' + a[1] + ', ' + a[2];
    document.write(dstr);
}

//==================================================================================================================
//  Row Selection Functions
//==================================================================================================================

var Selection = 
{
    SelectedIndex: -1,
    SelectColor: "#cc6666",
    SaveColor: "#ffffff",
    clear: function()
    {
        if (Selection.SelectedIndex < 0) return;
        document.getElementById('row' + Selection.SelectedIndex).style['backgroundColor'] = Selection.SaveColor;
    },
    select: function(idx)
    {
        if (Selection.SelectedIndex == idx) return;
        Selection.clear();
        Selection.SelectedIndex = idx;
        Selection.SaveColor = document.getElementById('row' + idx).style['backgroundColor'];;
        document.getElementById('row' + idx).style['backgroundColor'] = Selection.SelectColor;
    }
};

//==================================================================================================================
//  Event Functions
//==================================================================================================================

function makeDoubleDelegate(function1, function2) 
{
    return function() 
    {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}

function addEvent(obj, evType, fn, useCapture)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } 
    else if (obj.attachEvent)
    {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } 
    else 
    {
        alert("Handler could not be attached");
    }
} 

function removeEvent(obj, evType, fn, useCapture)
{
    if (obj.removeEventListener)
    {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } 
    else if (obj.detachEvent)
    {
        var r = obj.detachEvent("on"+evType, fn);
        return r;
    } 
    else 
    {
        alert("Handler could not be removed");
    }
} 

//==================================================================================================================
//  Form Functions
//==================================================================================================================

var Form = 
{
    post: function(name)
    {
        document.forms[name].submit();
    }
}

function getChecked(formName)
{
    var frm = document.forms[formName];
    var chkVals  = "";
    for (var i = 0; i < frm.elements.length; i++)
    {
        el = frm.elements[i];
        if (el.type != "checkbox") continue;
        if (!el.checked) continue;
        if (chkVals != "") chkVals += ",";
        chkVals += el.value;
    }
    return (chkVals);
}

//==================================================================================================================
//  Window Functions
//==================================================================================================================

var Window = 
{
    windows: new Object(),
    set: function(name,win)
    {
        Window.windows[name] = win;
    },
    get: function(name)
    {
        return Window.windows[name];
    },
    open: function(url,name,width,height,scroll,x,y,status,resize)
    {
        if (new String(url)  == 'undefined' || !url.length) return;
        if (new String(name) == 'undefined' || !name.length)
        {
            var dobj = new Date();
            name = 'win' + dobj.getSeconds();
        }

        //if (new String(width)  == 'undefined' || width  <= 0) width  = 525;
        //if (new String(height) == 'undefined' || height <= 0) height = 600;

        if (new String(x) == 'undefined' || x < 0)
        {
            xMax = 800;
            if (document.all) xMax = screen.width;
            else if (document.layers) xMax = window.outerWidth;
            x = (xMax - width ) / 2;
        }
        if (new String(y) == 'undefined' || y < 0)
        {
            yMax = 600;
            if (document.all) yMax = screen.height;
            else if (document.layers) yMax = window.outerHeight;
            y = (yMax - height ) / 2;
        }

        if (new String(scroll) == 'undefined') scroll = 1;
        if (new String(status) == 'undefined') status = 0;
        if (new String(resize) == 'undefined') resize = 0;

        var attr = 'resizable=' + resize;
        if (width)  attr += ',width='   + width;
        if (height) attr += ',height='  + height;
        if (width)  attr += ',screenX=' + x;
        if (height) attr += ',screenY=' + y;
        if (width)  attr += ',top='     + y;
        if (height) attr += ',left='    + x;
        attr += ',scrollbars=' + scroll;
        attr += ',status=' + status;
        attr += ',toolbar=0';
        attr += ',location=0';
        attr += ',menubar=0';
        attr += ',directories=0';

        var win = window.open(url,name,attr,true);
        Window.set(name,win);
        win.focus();        
    }    
};