﻿


    //////////
    // Tabs //

function MenuTabs( tabs )
{
    this.tabs = tabs;
    this.Init();
}

MenuTabs.prototype.Init = function()
{
    var inx;
    for (inx = 0; inx < this.tabs.length; inx++)
    {
        var td_id   = this.tabs[inx][0];
        var mainTD = document.getElementById( td_id );
   
        var t = document.createElement("TABLE");
        t.cellPadding = "0";
        t.cellSpacing = "0";

        var tr = t.insertRow();

        var jnx;
        for (jnx = 0; jnx < this.tabs.length; jnx++)
        {
            var td_id   = this.tabs[jnx][0];
            var tabName = this.tabs[jnx][1];

            var td = tr.insertCell();
            td.noWrap = true;
            td.innerHTML = "&nbsp;"+tabName+"&nbsp;";
            if (inx == jnx)
                this._makeSelectedTd( td );
            else
                this._makeUnselectedTd( td, jnx );

            // If there are two consecutive tabs, not selected.
            if ( jnx+1 < this.tabs.length && jnx != inx && jnx+1 != inx )
                // Then but a divider between them
                td.style.borderRight = "ridge 1px black";
        }

        var td = tr.insertCell();
        td.width = "100%";
        td.innerHTML = "&nbsp;";
        td.style.borderBottom = "ridge 1px black";

        mainTD.insertBefore( t, mainTD.firstChild );
    }
}

MenuTabs.prototype._makeSelectedTd = function( td )
{
    td.style.backgroundColor = "lightsteelblue";
    td.style.borderBottom = "ridge 0px black";
    td.style.borderLeft = "ridge 1px black";
    td.style.borderTop = "ridge 1px black";
    td.style.borderRight = "ridge 1px black";

    td.onmouseover = null;
    td.onmouseout = null;
    td.onclick = null;

}
MenuTabs.prototype._makeUnselectedTd = function( td, selectedInx )
{
    td.style.backgroundColor = "";
    td.style.borderBottom = "ridge 1px black";
    td.style.borderLeft = "ridge 0px black";
    td.style.borderTop = "ridge 0px black";
    td.style.borderRight = "ridge 0px black";

    td.onmouseover = MouseOver( td );
    td.onmouseout = MouseOut( td );
    
    var _this = this;
    td.onclick = function ()
                 {
                    _this.Select( selectedInx );
                 };
}

MenuTabs.prototype.Select = function(selectedInx)
{
    var inx;
    for (inx = 0; inx < this.tabs.length; inx++)
    {
        var td_id   = this.tabs[inx][0];
        var td = document.getElementById(td_id);
        td.style.display = (inx == selectedInx) ? "block" : "none";
    }
}




function MouseOver( cell )
{
    return function()
    {
        cell.style.textDecoration = "underline";
        cell.style.cursor = "pointer";
        cell.style.color = "blue";
    };
}
function MouseOut( cell )
{
    return function()
    {
        cell.style.textDecoration = "none";
        cell.style.cursor = "auto";
        cell.style.color = "black";
    };
}








    ////////////
    // Pop-Up //

function PopUp( w, h, onOpen, onClose, color )
{
    if ( typeof(color)+"" == "undefined" )
	color = "#FAF2FF";
    this.onOpen = onOpen?onOpen:function(){};
    this.onClose = onClose?onClose:function(){};
    
    var div = document.createElement("DIV");
    this.div = div;

    div.style.position = "absolute";
    div.style.display = "none";

    var t = document.createElement("TABLE");
    this.t = t;
    div.appendChild(t);
    t.cellPadding = "0px";
    t.cellSpacing = "0px";
    t.border = "0px";
    t.width = w+"px";
    
    // Title & Close Button
    var tr = t.insertRow();
    var td = tr.insertCell();
    var b = document.createElement("B");
    this.b = b;
    td.appendChild(b);
    b.style.backgroundColor = color ;
    b.style.filter = "alpha(opacity=100)";

    var td = tr.insertCell();
    td.align="right";
    td.vAlign="bottom";
    var span = document.createElement("SPAN");
    td.appendChild(span);
    span.innerHTML = "&nbsp;X&nbsp;";
    span.style.cursor = "hand";
    span.onclick = (function(pUp){
        	return function()
        	{
        	    pUp.Close();
        	}
	})(this);
    span.style.backgroundColor = color ;
    span.style.filter = "alpha(opacity=100)";

    // Middle Row - left/right border and contents
    var tr = t.insertRow();
    
    var td = tr.insertCell();
    td.colSpan=2;
    td.style.verticalAlign = "top";
    td.width="100%";
    var div2 = document.createElement("DIV");
    td.appendChild(div2);
    div2.style.height=h+"pt";
    this.contentArea = div2;
    td.style.backgroundColor = color ;
    td.style.filter = "alpha(opacity=100)";

    document.body.appendChild(div);
}


PopUp.prototype.SetPosition = function (pp)
{
    this.div.style.left = pp.x+"px";
    this.div.style.top = pp.y+"px";
}
PopUp.prototype.SetTitle = function (title)
{
    this.b.innerHTML = title;
}
PopUp.prototype.SetBody = function (body)
{
    this.contentArea.innerHTML = body;
}
PopUp.prototype.IsOpen = function ()
{
    return (this.div.style.display != "none");
}

PopUp.prototype.Open = function ()
{
    list_push( popupStack, this );
    this.div.style.display = "block";
    MakeModal( this.div );
    this.onOpen();
}
PopUp.prototype.Close = function ()
{
    if ( !this.IsOpen() )
        return;

    popupStack = list_remove( popupStack, this );
    if ( popupStack.length == 0 )
        UndoModal();
    this.div.style.display = "none";
    this.onClose();
}

var popupStack = new Array();

function CurrentPopup()
{
    if (popupStack.length == 0)
        return null;
    return popupStack[popupStack.length-1];
}




    ///////////////////////
    // X & Y Positioning //

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function GetCellWidth( cell )
{
	var t = document.createElement("TABLE");
	document.body.appendChild( t );

	var tr = t.insertRow();

	var cCopy = tr.insertCell();
	cCopy.innerHTML = cell.innerHTML;
	cCopy.className = cell.className;

	var other = tr.insertCell();
	var greatestWidth = other.offsetLeft;

	// Put it back under its original parent at the same index.
	document.body.removeChild( t );

	return greatestWidth;
}

function PagePosition()
{
    this.x = 0;
    this.y = 0;
}
PagePosition.Relative = function ( e, xOffset, yOffset )
{
    if (!e)
        e = event.srcElement;

    var pp = new PagePosition();
    pp.x = findPosX(e);
    pp.y = findPosY(e);

    if (typeof(xOffset) != "undefined")
        pp.x += xOffset;
    if (typeof(yOffset) != "undefined")
        pp.y += yOffset;

    return pp;
}
PagePosition.Absolute = function ( x, y )
{
    var pp = new PagePosition();
    pp.x = x;
    pp.y = y;

    return pp;
}


// Gets the full width/height because it's different for most browsers.
function getViewportHeight() 
{
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() 
{
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}





    ///////////
    // Modal //

function MakeModal( e )
{
    _modalElement = e;

	disableTabIndexes();

    _modalElement.style.zIndex = 201;
	_ModalMask.style.display = "block";

    // Set the Mask Size - use either the scroll's H/W or viewport's H/W.
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	var theBody = document.getElementsByTagName("BODY")[0];
	var popHeight = Math.max(fullHeight, theBody.scrollHeight);
	var popWidth = Math.max(fullWidth, theBody.scrollWidth);
	_ModalMask.style.height = popHeight + "px";
	_ModalMask.style.width = popWidth + "px";

	if (_HideSelects)
		hideSelectBoxes();
}

function UndoModal() 
{
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";

	restoreTabIndexes();

    _modalElement.zIndex = 1;
	_ModalMask.style.display = "none";

	// display all select boxes
	if (_HideSelects)
		displaySelectBoxes();
}


    var _modalElement;

    // If using Mozilla or Firefox, use Tab-key trap.
    if (!document.all)
	    document.onkeypress = 
            function (e) 
            {   // iff in modal mode and key was [TAB], suppress it.
                if (_inModalMode() && e.keyCode == 9)  return false;
            };

    function _InitModal() 
    {
	    // Add the HTML to the body
	    var theBody = document.getElementsByTagName('BODY')[0];

	    _ModalMask = document.createElement('div');
	    theBody.appendChild(_ModalMask);
	    _ModalMask.style.position = "absolute";
	    _ModalMask.style.zIndex = 200;
	    _ModalMask.style.top = "0px";
	    _ModalMask.style.left = "0px";
	    _ModalMask.style.width = "100%";
	    _ModalMask.style.height = "100%";
	    _ModalMask.style.opacity = ".4";
	    _ModalMask.style.filter = "alpha(opacity=70)";
	    _ModalMask.style.backgroundColor = "#999999";
	    _ModalMask.style.display = "none";

	    _HideSelects = (window.navigator.userAgent.indexOf("MSIE") > -1)
    }
    var _ModalMask = null;
    var _HideSelects;
    addEvent(window, "load", _InitModal);

    function _inModalMode()
    {
        return _ModalMask.style.display == "block";
    }

    // Pre-defined list of tags we want to disable/enable tabbing into
    var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	
    var gTabIndexes = new Array();

    // For IE.  Go through predefined tags and disable tabbing into them.
    function disableTabIndexes() {
	    if (document.all) {
		    var i = 0;
		    for (var j = 0; j < gTabbableTags.length; j++) {
			    var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			    for (var k = 0 ; k < tagElements.length; k++) {
				    gTabIndexes[i] = tagElements[k].tabIndex;
				    tagElements[k].tabIndex="-1";
				    i++;
			    }
		    }
	    }
    }

    // For IE. Restore tab-indexes.
    function restoreTabIndexes() {
	    if (document.all) {
		    var i = 0;
		    for (var j = 0; j < gTabbableTags.length; j++) {
			    var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			    for (var k = 0 ; k < tagElements.length; k++) {
				    tagElements[k].tabIndex = gTabIndexes[i];
				    tagElements[k].tabEnabled = true;
				    i++;
			    }
		    }
	    }
    }


    function hideSelectBoxes() 
    {
	    var tagElements = document.getElementsByTagName("SELECT");
	    for(var inx = 0; inx < tagElements.length; inx++)
	    {
	        var e = tagElements[inx];

            // Do not hide SELECT's that are in the pop-up.
            var skipIt = false;
	        var p = e.parentNode;
	        while ( p != null )
	        {
	            if (p == _modalElement)
	            {
	                skipIt = true;
	                break;
	            }
		        p = p.parentNode;
		    }
	        if (skipIt)
	            continue;

            // Remember the original visibility value, so that we do not show SELECT's in UndoModal that were initially hidden.
		    e.style._origVisibility=tagElements[inx].style.visibility;

            // Hide the SELECT.
		    e.style.visibility="hidden";
		}
    }

    function displaySelectBoxes() 
    {
	    var tagElements = document.getElementsByTagName("SELECT");
	    for(var inx = 0; inx < tagElements.length; inx++)
	    {
	        var e = tagElements[inx];
		    e.style.visibility=e.style._origVisibility;
		}
    }




    //////////
    // List //

function list_push(l, i) { l[l.length] = i; }
function list_pop (list) { var lastInx = list.length-1; var item = list[lastInx]; list.splice(lastInx, 1); return item; }
function list_index( list, item ) {	var inx; for (inx = 0; inx < list.length; inx++) { if ( item == list[inx] ) return inx; } return -1; }
function list_remove( list, item ) { var inx = list_index( list, item ); if (inx == -1) return list; return list_removeAtIndex( list, inx ); }
function list_removeAtIndex( list, index ) 
{
    // split
	var nlist = list.slice(0,index);
	var list2 = list.slice(index+1);

	// merge
	var inx;
	for (inx = 0; inx < list2.length; inx++)
		list_push(nlist, list2[inx]);

	return nlist;
}




    ////////////
    // Events //

function addEvent( obj,         // the object to attach event to
                   evType,      // name of the event - DONT ADD "on", pass only "mouseover", etc
                   fn)          // function to call
{
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
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");
  }
}


function AppendEvent( control, eventName, func )
{
    control[eventName] = AppendFunction( control[eventName], func );
}

// This is really a function-chain.  We need to modify the function-chain, and
// since we cannot do that directly to the passed in argument, we must return
// the new function-chain.
function AppendFunction( functionChain, func )
{
	if (functionChain)
	{
	    functionChain = (function(func1, func2){
                return function()
                {
                    func1();
                    func2();
                }
            })(functionChain, func);
	}
	else
		functionChain = func;
    return functionChain;
}