//////////////////////////////////////////////////////////
// Copyright (C) 2007 ALBAtel Inc. All rights reserved.
//////////////////////////////////////////////////////////

var KeyCode =
{
    SPACE : 32,
    NUM_0 : 48,
    NUM_1 : 49,
    NUM_2 : 50,
    NUM_3 : 51,
    NUM_4 : 52,
    NUM_5 : 53,
    NUM_6 : 54,
    NUM_7 : 55,
    NUM_8 : 56,
    NUM_9 : 57,

    BACKSPACE       : 8,
    DELETE          : 127,
    SPACE           : 32,
    PLUS            : 43,
    MINUS           : 45,
    ASTERISK        : 42,
    SLASH_FORWARD   : 47,
    BRACKET_LEFT    : 40,
    BRACKET_RIGHT   : 41,
    PERIOD          : 46,
    EQUALS          : 61,
    ENTER           : 13,
    ESCAPE          : 27,
    TAB             : 9
}


String.prototype.trim  = function() { return this.replace(/^\s+|\s+$/g, ""); }
String.prototype.ltrim = function() { return this.replace(/^\s+/,""); }
String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }

String.prototype.startsWith = function(sValue)
{
    return (this.indexOf(sValue) == 0);
}

Array.prototype.indexOf = function(oValue)
{
    for (var i = 0; i < this.length; i++) if(this[i] == oValue) return i;
    return -1;
}

Array.prototype.lastIndexOf = function(oValue)
{
    for (var i = this.length-1; i >= 0; i--) if(this[i] == oValue) return i;
    return -1;
}

Array.prototype.contains = function(oValue)
{
    for (var i = 0; i < this.length; i++) if(this[i] == oValue) return true;
    return false;
}

Array.prototype.remove = function(iIndex)
{
    var oValue = this[iIndex];
    this.splice(iIndex, 1);
    return oValue;
}

Array.prototype.getMap = function(oValue)
{
    for (var i = 0; i < this.length; i+=2) if(this[i] == oValue) return this[i+1];
    return null;
}

/**
 * A set of helpful UI functions.
 */
var UI =
{
    image: "styles/images/",

    defaultValue:function(pParam, pDefault)
    {
        return (typeof(pParam) == "undefined" ? pDefault : pParam);
    },


    /* Events */

    cancelBubble:function(oEvent)
    {
        oEvent=(oEvent?oEvent:event);
        oEvent.cancelBubble = true;
        return false;
    },

    eventFire:function(oParent, sEvent, oEvent)
    {
        var oCurrent = oParent;
        while ((oCurrent = XML.next(oParent, oCurrent)) != null) if (eval("oCurrent."+sEvent) != null) eval("oCurrent."+sEvent+"(oEvent)");
    },

    /* Positions, sizes, bounds and dimensions */

    position:function(oComponent)
    {
        var pPosition = {x: 0, y: 0};

        while (oComponent != null)
        {
            pPosition.x += oComponent.offsetLeft - (oComponent.scrollLeft ? oComponent.scrollLeft : 0);
            pPosition.y += oComponent.offsetTop - (oComponent.scrollTop ? oComponent.scrollTop : 0);
            oComponent   = oComponent.offsetParent
        }

        return pPosition;
    },

    dimension:function(pComponent)
    {
        return { width: pComponent.clientWidth, height: pComponent.clientHeight};
    },

    bounds:function(pComponent)
    {
        var pPosition = UI.position(pComponent);

        return {x: pPosition.x, y: pPosition.y, width: pComponent.clientWidth, height: pComponent.clientHeight};
    },

    setBounds:function(pComponent, oBounds)
    {
        pComponent.style.top = oBounds.y;
        pComponent.style.left = oBounds.x;
        pComponent.style.width = oBounds.width;
        pComponent.style.height = oBounds.height;
    },

    inBounds:function(iValue, iMin, iMax)
    {
        if      (iValue < iMin) iValue = iMin;
        else if (iValue > iMax) iValue = iMax;
        return iValue;
    },


    /* Windows, popups */

    centerWindow:function(oWindow)
    {
        document.body.appendChild(oWindow);

        var oWindowSize = UI.dimension(oWindow);
        var oBrowserSize = UI.dimension(document.body);

        var iTop = (oBrowserSize.height - oWindowSize.height)/2;
        var iLeft = (oBrowserSize.width - oWindowSize.width)/2;

        oWindow.style.top = (iTop < 1 ? 1 : iTop);
        oWindow.style.left = (iLeft < 1 ? 1 : iLeft);
    },

    showLoader:function(oParent, bShow)
    {
        if (!bShow && oParent.loader != null)
        {
            oParent.loader.parentNode.removeChild(oParent.loader);
            oParent.loader = null;
        }
        else if (bShow)
        {
            var oBounds = UI.bounds(oParent);
            var oPanel = (oParent.loader != null ? oParent.loader : document.createElement("DIV"));

            oPanel.className = "ui_loader";
            oPanel.style.top = oBounds.y + document.body.scrollTop;
            oPanel.style.left = oBounds.x + document.body.scrollLeft;
            oPanel.style.width = oBounds.width;
            oPanel.style.height = oBounds.height;

            oParent.loader = oPanel;
            document.body.appendChild(oPanel);
        }
    },

    showRelativeTo:function(oComponent, oRelativeTo, iOffsetX, iOffsetY, bEnsureWithinWindow)
    {
        bEnsureWithinWindow = UI.defaultValue(bEnsureWithinWindow, false);

        document.body.appendChild(oComponent);

        var pPosition = UI.position(oRelativeTo);

        var iX = pPosition.x + iOffsetX;
        var iY = pPosition.y + iOffsetY;

        if (bEnsureWithinWindow)
        {
            if (iX + oComponent.clientWidth > document.body.clientWidth) iX = document.body.clientWidth - oComponent.clientWidth;
            if (iY + oComponent.clientHeight > document.body.clientHeight) iY = document.body.clientHeight - oComponent.clientHeight;
            if (iY < 0) iY = 0;
            if (iX < 0) iX = 0;
       }

        oComponent.style.top = iY;
        oComponent.style.left = iX;
    },

    showPopupRelativeTo:function(oComponent, oRelativeTo, iOffsetX, iOffsetY, bEnsureWithinWindow)
    {
        oComponent.hideFunction = function(oEvent)
        {
            oEvent = (oEvent?oEvent:event);
            if      (oEvent.type == "scroll") UI.hide(oComponent);
            else if (oEvent.type == "resize") UI.hide(oComponent);
            else if (oEvent.type == "mousedown") UI.hide(oComponent);
            else if (oEvent.type == "keypress" && oEvent.keyCode == KeyCode.ESCAPE) UI.hide(oComponent);
        };

        window.onscroll = oComponent.hideFunction;
        window.onresize = oComponent.hideFunction;
        document.body.keypress = oComponent.hideFunction;
        document.body.onmousedown = oComponent.hideFunction;

        //Show the component.
        UI.showRelativeTo(oComponent, oRelativeTo, iOffsetX, iOffsetY, bEnsureWithinWindow);
    },

    hide:function(oComponent)
    {
        if (oComponent.parentNode != null) oComponent.parentNode.removeChild(oComponent);
        if (oComponent.hideFunction != null)
        {
            window.onscroll = null;
            window.onresize = null;
            document.body.keypress = null;
            document.body.onmousedown = null;

            oComponent.hideFunction = null;
        }
    },

    /* General UI functions */

    expandCollapse:function(oImage, sBody)
    {
        var oBody = document.getElementById(sBody);

        var bExpanded = (oBody.style.display == "");

        oImage.expanded = bExpanded;
        oImage.src = UI.image + "icon=" + (bExpanded ? "open.png" : "close.png");
        oBody.style.display = (bExpanded ? "none" : "");
    },

    expand:function(oImage, bExpand)
    {
        if (oImage.pane == null) oImage.pane = document.getElementById(oImage.getAttribute("panel"));

        var bExpand = (typeof(bExpand) == "undefined" ? (oImage.pane.style.display != "") : bExpand);

        oImage.expanded = bExpand;
        oImage.src = UI.image + "icon=" + (bExpand ? "close.png" : "open.png");
        oImage.pane.style.display = (bExpand ? "" : "none");
    },


    display:function(sId, bShow)
    {
        var oComponent = document.getElementById(sId);
        if (oComponent != null) oComponent.style.display = (bShow ? "" : "none");
    },

    /* Form */

    setValue:function(oInput, sValue, bForce)
    {
        if (typeof(oInput) == "string") oInput = document.getElementById(oInput);
        if (oInput == null) return;

        var sSet = oInput.getAttribute("set");
        var sName = oInput.nodeName.toUpperCase();

        if      (oInput.set)          oInput.set(sValue);
        else if (sSet != null)        eval(sSet+"(oInput, sValue)");
        else if (sName == "TEXTAREA") oInput.value = sValue;
        else if (sName == "SELECT")   UI.setDropDown(oInput, sValue, sValue, bForce);
        else if (sName == "INPUT")
        {
            var sType = oInput.type.toUpperCase();
            if (sType == "CHECKBOX" || sType == "RADIO") UI.setCheckbox(oInput, sValue);
            else                                         oInput.value = sValue;
        }
        else oInput.innerHTML = sValue;

        if(oInput.onchange != null) oInput.onchange();
    },

    setCheckbox:function(oInput, sValue)
    {
        if      (typeof(sValue) == "string") sValue = sValue.toLowerCase();
        else if (typeof(sValue) == "boolean") sValue = (sValue ? "true" : "false");
        else if (typeof(sValue) == "number") sValue = (sValue > 0 ? "true" : "false");

        oInput.checked = (sValue == "1" || sValue == "true");
    },

    setDropDown:function(oSelect, sValue, sText, bForce)
    {
        bForce = UI.defaultValue(bForce, false);

        if (!bForce) oSelect.value = sValue;
        else
        {
            for (var i = 0; i < oSelect.options.length; i++)
            {
                if (oSelect.options[i].value == sValue)
                {
                    oSelect.options[i].selected = true;
                    return;
                }
            }

            var oOption = oSelect.appendChild(document.createElement("OPTION"));
            oOption.value = sValue;
            oOption.innerHTML = sText;
            oOption.selected = true;
        }
    },

    getValue:function(oInput)
    {
        if (typeof(oInput) == "string") oInput = document.getElementById(oInput);
        if (oInput == null) return null;

        var sValue = null;
        var sGet = oInput.getAttribute("get");
        var sName = oInput.nodeName.toUpperCase();

        if      (oInput.get)          sValue = oInput.get();
        else if (sGet != null)        sValue = eval(sGet+"(oInput)");
        else if (sName == "SELECT")   sValue = oInput.value;
        else if (sName == "TEXTAREA") sValue = oInput.value;
        else if (sName == "INPUT")
        {
            var sType = oInput.type.toUpperCase();
            if (sType == "CHECKBOX" || sType == "RADIO") sValue = (oInput.checked ? "1" : "0");
            else                                         sValue = oInput.value;
        }
        else sValue = oInput.innerHTML

        return sValue;
    },

    populateDropDown:function(oDropDown, oOptions, sSelect, sValueAttr, sTextAttr)
    {
        if (typeof(oDropDown) == "string") oDropDown = document.getElementById(oDropDown);

        XML.clear(oDropDown);

        oOption = (oOptions != null ? oOptions.firstChild : null);
        while(oOption != null)
        {
            if(oOption.nodeType == 1)
            {
                var oValue = oDropDown.appendChild(document.createElement("OPTION"));
                var sValue = oOption.getAttribute(sValueAttr);
                var sText  = oOption.getAttribute(sTextAttr);

                if (sValue == sSelect) oValue.selected = true;

                oValue.object     = oOption;
                oValue.value      = sValue;
                oValue.innerHTML  = sText;
            }

            oOption = oOption.nextSibling;
        }

        if(oDropDown.onchange) oDropDown.onchange();

        return oDropDown;
    }

};
