// JScript File

/* The bind is declared here to just because it's so important for the Sys_Objectware class */
Function.prototype.bind = fnBind;
function fnBind(_this)
{
	var ptr = this;
	return function(args) { ptr.call(_this, args) };
}

/* Sys_Objectware */

function Sys_Objectware()
{
    this.onMouseMove = new Array();
    this.onMouseDown = new Array();
    this.onMouseUp = new Array();
    
    this.bDebugShowing = false;
    this.EnableDebugging = false;
    this.BodyLoaded = false;
    this.debugHTML = '';
    this.arActivateElems = new Array();
	this.arPopupElems = new Array();
	this.ModalCancel = null;
    
    this.Init();
}

Sys_Objectware.prototype.AddWindowOnload = fnAddWindowOnload;
Sys_Objectware.prototype.FireOnClick = fnFireOnClick;
Sys_Objectware.prototype.CopyValue = fnCopyValue;
Sys_Objectware.prototype.ShowElementById = fnShowElementById;
Sys_Objectware.prototype.HideElementById = fnHideElementById;
Sys_Objectware.prototype.ShowElement = fnShowElement;
Sys_Objectware.prototype.HideElement = fnHideElement;

Sys_Objectware.prototype.AddOnMouseMove = fnAddOnMouseMove;
Sys_Objectware.prototype.AddOnMouseDown = fnAddOnMouseDown;
Sys_Objectware.prototype.AddOnMouseUp = fnAddOnMouseUp;
Sys_Objectware.prototype.MaximizeObject = fnMaximizeObject;
Sys_Objectware.prototype.SetObjectTransparency = fnSetObjectTransparency;
Sys_Objectware.prototype.RemoveOnMouseMove = fnRemoveOnMouseMove;
Sys_Objectware.prototype.RemoveOnMouseDown = fnRemoveOnMouseDown;
Sys_Objectware.prototype.RemoveOnMouseUp = fnRemoveOnMouseUp;
Sys_Objectware.prototype.GrayScreen = fnGrayScreen;
Sys_Objectware.prototype.UnGrayScreen = fnUnGrayScreen;
Sys_Objectware.prototype.SetModalBackgroundColor = fnSetModalBackgroundColor;
Sys_Objectware.prototype.MakeElementDraggable = fnMakeElementDraggable;
Sys_Objectware.prototype.Debug = fnDebug;
Sys_Objectware.prototype.GetAbsoluteLeft = fnGetAbsoluteLeft;
Sys_Objectware.prototype.GetAbsoluteTop = fnGetAbsoluteTop;
Sys_Objectware.prototype.CloneObject = fnCloneObject;
Sys_Objectware.prototype.CopyValues = fnCopyValues;
Sys_Objectware.prototype.SetCookie = fnSetCookie;
Sys_Objectware.prototype.DeleteCookie = fnDeleteCookie;
Sys_Objectware.prototype.GetCookie = fnGetCookie;
Sys_Objectware.prototype.EnableDebug = fnEnableDebug;
Sys_Objectware.prototype.DisableDebug = fnDisableDebug;
Sys_Objectware.prototype.InitDebug = fnInitDebug;
Sys_Objectware.prototype.DisplayObject = fnDisplayObject;
Sys_Objectware.prototype.WindowTop = fnWindowTop;
Sys_Objectware.prototype.WindowLeft = fnWindowLeft;
Sys_Objectware.prototype.WindowRight = fnWindowRight;
Sys_Objectware.prototype.WindowBottom = fnWindowBottom;
Sys_Objectware.prototype.DocumentRight = fnDocumentRight;
Sys_Objectware.prototype.DocumentBottom = fnDocumentBottom;
Sys_Objectware.prototype.CenterObject = fnCenterObject;
Sys_Objectware.prototype.MaximizeObject = fnMaximizeObject;
Sys_Objectware.prototype.SetObjectTransparency = fnSetObjectTransparency;
Sys_Objectware.prototype.MoveObject = fnMoveObject;
Sys_Objectware.prototype.ObjectVisible = fnObjectVisible;
Sys_Objectware.prototype.ObjectHit = fnObjectHit;
Sys_Objectware.prototype.AddPopup = fnAddPopup;
Sys_Objectware.prototype.RemovePopup = fnRemovePopup;
Sys_Objectware.prototype.ShowPopup = fnShowPopup;
Sys_Objectware.prototype.CopyObject = fnCopyObject;
Sys_Objectware.prototype.NumberToString = fnNumberToString;
Sys_Objectware.prototype.Round = fnRound;
Sys_Objectware.prototype.ValidObject = fnValidObject;
Sys_Objectware.prototype.DoModal = doModal;
Sys_Objectware.prototype.GrayScreen = fnGrayScreen;
Sys_Objectware.prototype.UnGrayScreen = fnUnGrayScreen;
Sys_Objectware.prototype.RightAlignAndCenterToElement = fnRightAlignAndCenterToElement;
Sys_Objectware.prototype.ScrollToTop = fnScrollToTop;
Sys_Objectware.prototype.CreateFloatingIFrame = fnCreateFloatingIFrame;
Sys_Objectware.prototype.DestroyFloatingIFrame = fnDestroyFloatingIFrame;
Sys_Objectware.prototype.AddPopupClick = fnAddPopupClick;
Sys_Objectware.prototype.AddDefaultSubmit = fnAddDefaultSubmit;
Sys_Objectware.prototype.Init = fnInit;

function fnInit()
{
    var ptr = this;
    
    this.AddWindowOnload(function(){ ptr.BodyLoaded = true; });
    
    // Setup global events
    
    document.onmousemove = _handleMouseMove.bind(this);
    document.onmousedown = _handleMouseDown.bind(this);
    document.onmouseup = _handleMouseUp.bind(this);
    
    this.Debug('Sys_Objectware Initialized...');
}
var bPopUpVisible = false;
var bUseIfrm = false;
var ifrmPopup = null;

var loadIFRAME = function()
{
    ifrmPopup = document.createElement('iframe');
    if ( typeof ifrmPopup != 'undefined' && ifrmPopup != null )
    {
        ifrmPopup.style.backgroundColor = '#FFFFFF';
        ifrmPopup.style.position = 'absolute';
        ifrmPopup.frameBorder = 0;
        ifrmPopup.border = 0;
        ifrmPopup.style.border = 0;
        ifrmPopup.style.visibility = 'hidden';
        ifrmPopup.style.display = 'none';
        ifrmPopup.src = 'javascript:false;';
        
        document.body.appendChild(ifrmPopup);
        
        bUseIfrm = true;
    }
}

fnAddWindowOnload(loadIFRAME);

function _handleMouseMove(e)
{
    /*
    if ( !this.BodyLoaded )
        return;
        */
        
    var x = 0;
    var y = 0;
    
    if ( !e ) // ie!
    {
        e = window.event;
        
        x = e.clientX + document.body.scrollLeft;
        y = e.clientY + document.body.scrollTop;
        
        
    }
    else
    {
        x = e.pageX;
        y = e.pageY;       
    }
    
    this.CurrentMouseX = x;
    this.CurrentMouseY = y;
    
    // Send MouseMove Events
    
    for( var n=0; n < this.onMouseMove.length; n++ )
    {
        if ( this.onMouseMove[n](x,y) )
        {
			if ( e && typeof e.preventDefault != 'undefined' )
				e.preventDefault();
			
			if ( event )
			{
				event.returnValue = false;
				event.cancelBubble = true;
			}
				
			return false;
        }
    }
    
    // Check Popups
    for( var i=0; i < this.arActivateElems.length; i++ )
    {
        var activate = document.getElementById(this.arActivateElems[i]);
        var popup = document.getElementById(this.arPopupElems[i]);
        
        if ( Sys_Objectware.ValidObject(activate) )
        {
		    if ( Sys_Objectware.ObjectHit(activate, x, y) )
		    {
			    if ( !bPopUpVisible )
			    {
			        Sys_Objectware.ShowPopup(popup, x, y);
			        // check if need iframe and move if need be
    			    
			        if ( bUseIfrm )
			        {
			            ifrmPopup.style.left = popup.style.left;
			            ifrmPopup.style.top = popup.style.top;
			            ifrmPopup.style.width = popup.offsetWidth + 'px';
			            ifrmPopup.style.height = popup.offsetHeight + 'px';
			            
			            if ( !Sys_Objectware.ObjectVisible(ifrmPopup) )
			                Sys_Objectware.ShowElement(ifrmPopup);
			        }
    			    
			        bPopUpVisible = true;
			    }
		    }
		    else
		    {
			    if ( Sys_Objectware.ObjectVisible(popup) && !Sys_Objectware.ObjectHit(popup, x,y) )
			    {
				    Sys_Objectware.HideElement(popup);
				    Sys_Objectware.HideElement(ifrmPopup);
    				
				    bPopUpVisible = false;
			    }
		    }
		}
    }
}
var bList = 0;
var bPopUpVisible = false;
var bUseIfrm = false;
var ifrmPopup = null;
function _handleMouseDown( e )
{
    for( var n=0; n < this.onMouseDown.length; n++ )
    {
		if ( this.onMouseDown[n](this.CurrentMouseX,this.CurrentMouseY) )
		{
			if ( e && typeof e.preventDefault != 'undefined' )
				e.preventDefault();
			
			if ( event )
			{
				event.returnValue = false;
				event.cancelBubble = true;
			}
				
			return false;
		}
	}
}

function _handleMouseUp( e )
{
    for( var n=0; n < this.onMouseUp.length; n++ )
    {
		if ( this.onMouseUp[n](this.CurrentMouseX,this.CurrentMouseY) )
		{
			if ( e && typeof e.preventDefault != 'undefined' )
				e.preventDefault();
			
			if ( event )
			{
				event.returnValue = false;
				event.cancelBubble = true;
			}
			
			return false;
		}
	}
}

function fnAddPopup(activateElem, popupElem )
{
	this.arActivateElems.push(activateElem);
	this.arPopupElems.push(popupElem);
}

var grayDivID = null;
var grayDivBackgroundColor = '#999999';
function fnSetModalBackgroundColor(sColor)
{
    grayDivBackgroundColor = sColor;
}

function fnGrayScreen(transparencyLevel)
{
    if ( !transparencyLevel )
        transparencyLevel = 25;
	
	var div = document.getElementById(grayDivID);
	if ( Sys_Objectware.ValidObject(div) )
 		document.body.removeChild(div);
		
		
	div = document.createElement('div');
	div.id = 'graydiv';
	grayDivID = div.id;
	document.body.appendChild(div);

	div.style.zIndex = 500;
	div.style.backgroundColor = grayDivBackgroundColor;
	
	
	alert('50');
	Sys_Objectware.MaximizeObject(div);
	Sys_Objectware.SetObjectTransparency(div, transparencyLevel);		
	Sys_Objectware.ShowElement(div);
}

function fnSetObjectTransparency(elem, opacity)
{
	alert(elem.style.filter);
	if ( typeof elem.style.filter != 'undefined' )
		elem.style.filter = 'alpha(opacity = ' + ( opacity ) + ');';
	else if (typeof elem.style.mozOpacity != 'undefined')
		elem.style.mozOpacity = opacity / 100;
	else
		elem.style.opacity = opacity / 100;
}

function fnMaximizeObject(elem)
{
	elem.style.position = 'absolute';
	elem.style.top = 0
	elem.style.left = 0
	elem.style.width = Sys_Objectware.DocumentRight() + 'px';
	elem.style.height = Sys_Objectware.DocumentBottom() + 'px';
}

function fnUnGrayScreen()
{
	if ( grayDivID != null )
	{
		var div = document.getElementById(grayDivID);
		if ( Sys_Objectware.ValidObject(div) )
 			document.body.removeChild(div); 
	}
	
	if ( bUseIfrm && ifrmPopup != null )
		Sys_Objectware.HideElement(ifrmPopup);
}

function fnRemovePopup(activateElem, popupElem)
{
    var array1 = new Array();
    var array2 = new Array();
    
    for( var n=0; n < this.arActivateElems.length; n++ )
    {
        if ( this.arActivateElems[n] != activateElem )
        {
            array1.push(this.arActivateElems[n]);
            array2.push(this.arPopupElems[n]);
        }
    }
    
    this.arActivateElems = array1;
    this.arPopupElems = array2;
}

function fnInitDebug()
{
    if ( !this.EnableDebugging )
        return;
    
    if ( !this.bDebugShowing )
    { 
        // Create Div for doing debug            
        
        var divHeader = document.createElement('div');
        divHeader.style.backgroundColor = '#8080ff';
        divHeader.innerHTML = 'Debug Window';
        divHeader.style.width = '200px';
        divHeader.style.height = '20px';
        divHeader.style.position = 'relative';
        
        this.divDebug = document.createElement('textarea');
        // this.divDebug.style.wrap = 'off';
        // this.divDebug.style.whiteSpace = 'nowrap';
        this.divDebug.style.overflow = 'auto';
        this.divDebug.style.backgroundColor = '#ffffff';
        this.divDebug.style.width = '200px';
        this.divDebug.style.height = '400px';
        // this.divDebug.style.overflow = 'auto';
        this.divDebug.style.position = 'relative';
        
        var divDebugWindow = document.createElement('div');
        divDebugWindow.appendChild(divHeader);
        divDebugWindow.appendChild(this.divDebug);
        divDebugWindow.style.zIndex = 10000;
        divDebugWindow.style.position = 'absolute';
        
        divDebugWindow.style.height = '420px';
        divDebugWindow.style.width = divHeader.style.width;
        
        if ( this.GetCookie('DebugLocation') != null )
        {
            var cookie = this.GetCookie('DebugLocation');
            
            try
            {
                divDebugWindow.style.left = cookie.split(',')[0];
                divDebugWindow.style.top = cookie.split(',')[1];
                
                if ( divDebugWindow.style.left < 0 )
                    divDebugWindow.style.left = 0;
                
                if ( divDebugWindow.style.top < 0 )
                    divDebugWindow.style.top = 0;
            }
            catch(e)
            {
                divDebugWindow.style.left = 0;
                divDebugWindow.style.top = 0;
            }
        }
        else
        {
            divDebugWindow.style.left = 0;
            divDebugWindow.style.top = 0;
        }
                
        document.body.appendChild(divDebugWindow);
        
        var ptr = this;
        
        var funcMouseUp = function(elem, args) 
        {
            if ( elem.MoveParent )
            {
                args.x = elem.parentNode.style.left;
                args.y = elem.parentNode.style.top;
                
//                if ( args.x + elem.style.offsetWidth > window.screen.width )
//                    args.x = window.screen.width - elem.style.offsetWidth;
//                    
//                if ( args.y + elem.style.offsetHeight > window.screen.height )
//                    args.y = window.screen.Height - elem.style.offsetHeight;
            }
            else
            {
                args.x = elem.style.left;
                args.y = elem.style.top;
            }
            
            var sLocation = args.x + ',' + args.y;
            
            ptr.SetCookie('DebugLocation', sLocation);
        }
        
        this.MakeElementDraggable(divHeader, true, null, funcMouseUp,null);
       
        this.divDebug.innerHTML += this.debugHTML;
        
        this.bDebugShowing = true;
    }
}

function fnDebug(sDebug)
{
    if ( !this.divDebug )
    {
        this.debugHTML += sDebug + '\r\n';
        return;
    }
        
    this.divDebug.value += sDebug + '\r\n';
}

function fnEnableDebug()
{
    this.EnableDebugging = true;
    this.AddWindowOnload(this.InitDebug.bind(this));
    this.Debug('Debugging Enabled');
}

function fnDisableDebug()
{
    this.EnableDebugging = false;
}

function fnMakeElementDraggable(elem, bMoveParent, fBeginDragging, fEndDragging, fDragging)
{
    elem.style.cursor = 'move';
    elem.IsDragging = false;    
    elem.BeginDragging = fBeginDragging;
    elem.EndDragging = fEndDragging;
    elem.Dragging = fDragging;
    
    elem.MoveParent = bMoveParent;
    var ptr = this;
    var funcMouseMove = function(x,y) 
    { 
        if ( elem.IsDragging )
        {
            var dragElem = elem;
            
            if ( elem.MoveParent )
                dragElem = elem.parentNode;
                
            var newX = x - elem.HitOffsetX;
            var newY = y - elem.HitOffsetY;
            
            if ( newX < 0 )
                newX = 0;
            
            if ( newY < 0 )
                newY = 0;
                
            dragElem.style.left = newX + 'px';
            dragElem.style.top = newY + 'px';
            
            if ( elem.Dragging )
            {
                var args = new Object();
                
                args.x = x;
                args.y = y;
                elem.Dragging(elem, args);
            }
            
            return true;    
        }
        
        return false;
    };
    var funcMouseDown = function(x,y)
    {
        var elemLeft = Sys_Objectware.GetAbsoluteLeft(elem);
        var elemTop = Sys_Objectware.GetAbsoluteTop(elem);
        var elemWidth = elem.offsetWidth;
        var elemHeight = elem.offsetHeight;
        
        if ( x >= elemLeft && x <= elemLeft + elemWidth )
        {
            if ( y >= elemTop && y <= elemTop + elemHeight )
            {
                elem.IsDragging = true;
                elem.HitOffsetX = x - elemLeft;
                elem.HitOffsetY = y - elemTop;   
                
                if ( elem.BeginDragging )
                {
                    var args = new Object();
                    args.x = x;
                    args.y = y;
                    elem.BeginDragging(elem, args);
                }
                
                var dragElem = elem;
            
                if ( elem.MoveParent )
                    dragElem = elem.parentNode;
                
                if ( dragElem.style.position != 'absolute' )
                {
                    var elemX = Sys_Objectware.GetAbsoluteLeft(dragElem);
                    var elemY = Sys_Objectware.GetAbsoluteTop(dragElem);
                    dragElem.style.position = 'absolute';
                    dragElem.style.left = elemX;
                    dragElem.style.top = elemY;
                    dragElem.style.zIndex = 10000;
                }
                
                return true;
            }
        }
        
        return false;
    };
    
    var funcMouseUp = function(x, y)
    {
        if ( elem.IsDragging )
        {
            if ( elem.EndDragging != null )
            {
                var args = new Object();
                args.x = x;
                args.y = y;
                elem.EndDragging(elem, args);
            }
                
            elem.IsDragging = false;
        }
    };
    
    elem.OnMouseMove = funcMouseMove;
    elem.OnMouseDown = funcMouseDown;
    elem.OnMouseUp = funcMouseUp;
        
    this.AddOnMouseMove(funcMouseMove);
    this.AddOnMouseDown(funcMouseDown);
    this.AddOnMouseUp(funcMouseUp);
}

function fnAddWindowOnload(func) 
{
    if (window.addEventListener) 
        window.addEventListener("load",func,false);
    else if (window.attachEvent) 
        window.attachEvent("onload",func);
    else
    {
        var oldFunc = window.onload ? window.onload : new Function;

        var newFunc = function()
        {
            if ( oldFunc )
                oldFunc();
                
            func(); 
        }

        window.onload = newFunc;        
    }

}

function fnAddOnMouseMove(func)
{
    this.onMouseMove.push(func);
}

function fnRemoveOnMouseMove(func)
{
    var arNewMoves = new Array();
    for( var n=0; n < this.onMouseMove.length; n++ )
    {
        if ( this.onMouseMove[n] != func )
            arNewMoves.push(this.onMouseMove[n]);
    }    
    this.onMouseMove = arNewMoves;
}

function fnAddOnMouseDown(func)
{
    this.onMouseDown.push(func);
}

function fnRemoveOnMouseDown(func)
{
	var arNewDowns = new Array();
    for( var n=0; n < this.onMouseDown.length; n++ )
    {
        if ( this.onMouseDown[n] != func )
            arNewDowns.push(this.onMouseDown[n]);
    }    
    this.onMouseDown = arNewDowns;
}

function fnAddOnMouseUp(func)
{
    this.onMouseUp.push(func);
}

function fnRemoveOnMouseUp(func)
{
	var arNewUps = new Array();
    
    for( var n=0; n < this.onMouseUp.length; n++ )
    {
        if ( this.onMouseUp[n] != func )
            arNewUps.push(this.onMouseUp[n]);
    }   
    this.onMouseUp = arNewUps;
}

function fnFireOnClick(elem)
{
	/*
    if ( elem.fireEvent )
    {
        elem.fireEvent("onclick");   
    }
    else if ( elem.onclick )
    {
        elem.onclick();
    }
    else
    */ 
    
    if ( !Sys_Objectware.ObjectVisible(elem) )
	{
		Sys_Objectware.ShowElement(elem);
		elem.style.position = 'absolute';
		elem.style.left = Sys_Objectware.WindowLeft() - elem.style.offsetWidth + 'px';
	}
	
	if ( document.createEvent )
    {
		var evObj = document.createEvent('MouseEvents');
        evObj.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, elem);
        elem.dispatchEvent(evObj);
    }
    else if ( elem.click )
    {
        elem.click();
    }    
    else if ( elem.onclick )
    {
        elem.onclick();
    }
    
    
}

function fnCopyValue(elem1, elem2)
{
	document.getElementById(elem2).value = document.getElementById(elem1).value;
}

function fnHideElementById(elem)
{
    document.getElementById(elem).style.visibility = 'hidden';
    document.getElementById(elem).style.display = 'none';
}

function fnHideElement(elem)
{
	if ( Sys_Objectware.ValidObject(elem) )
	{
	    elem.style.visibility = 'hidden';
        elem.style.display = 'none';
    }
}

function fnShowElementById(elem)
{
    document.getElementById(elem).style.visibility = 'visible';
    document.getElementById(elem).style.display = 'block';
}

function fnShowElement(elem)
{
	elem.style.visibility = 'visible';
    elem.style.display = 'block';
}

function fnGetAbsoluteLeft(elem)
{
    var left = 0;

    while ( elem != null )
    {
        left += elem.offsetLeft;    
        
        if ( elem.offsetParent )
            elem = elem.offsetParent;
        else
            elem = null;
    }
    
    return left;
}

function fnGetAbsoluteTop(elem)
{
    var top = 0;
    
    while ( elem != null )
    {
        top += elem.offsetTop;    
        if ( elem.offsetParent != null )
            elem = elem.offsetParent;
        else
            elem = elem.offsetParent;
    }
    
    return top;
}

function fnCloneObject(object)
{
    return new _fnCloneObject(object);
}

function _fnCloneObject(object)
{
    for (i in object) 
    {
        if (typeof object[i] == 'object') 
            this[i] = new _fnCloneObject(object[i]);
        else
            this[i] = object[i];    
    }
}

function fnCopyValues(objectSrc, objectDest)
{
    for( i in objectSrc)
    {
        if ( typeof objectSrc[i] == 'object' )
        {
            //TODO: Verify the following will work when dest is not the same type of object as src
            // if ( typeof objectDest[i] == 'undefined' )  
            //     objectDest[i] = new Object();
                
            Sys_Objectware.CopyValues(objectSrc[i], objectDest[i]);
        }
        else
        {
            try
            {
                objectDest[i] = objectSrc[i];
            }
            catch(e)
            {
                Sys_Objectware.Debug(e);
            }
        }
    }
}

function _getCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
      endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function fnGetCookie (name) 
{
/*
    try
    {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) 
		{
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
				return _getCookieVal (j);
	            
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) 
				break; 
		}
    }
    catch (e)
    {
		return null;
    }
    
    return null;
    */
    var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
			
		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function fnSetCookie (name, value,days) 
{
/*
   var argv = fnSetCookie.arguments;
   var argc = fnSetCookie.arguments.length;
   var expires = (argc > 2) ? argv[2] : null;
   var path = (argc > 3) ? argv[3] : null;
   var domain = (argc > 4) ? argv[4] : null;
   var secure = (argc > 5) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
        */
        if (days) 
        {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else 
			var expires = "";
			
		document.cookie = name+"="+value+expires+"; path=/";
}

function fnDeleteCookie (name) 
{
	/*
    var exp = new Date();
    
    exp.setTime (exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
    var cval = Sys_Objectware.GetCookie (name);
    document.cookie =name + "=" + cval + "; expires=" + exp.toGMTString();
    */
    
    Sys_Objectware.SetCookie(name,"",-1);
}

function fnDisplayObject(object, prefix, arRecursedObjects)
{
    if ( arRecursedObjects == null )
    {
        arRecursedObjects = new Array();
        arRecursedObjects.push(object);
    }
    
    if ( prefix == null )
        prefix = '';
        
    var iCounter = 0;
    
    var names = new Array();
    
    for( p in object )
    {
		iCounter++;
		
		if ( iCounter > 1000 )
			return;
//        if ( typeof object[p] == 'object' )
//        {
//            var bRecursed = false;
//            for( var i=0; i < arRecursedObjects.length; i++ )
//            {
//                if ( arRecursedObjects[i] == object[p] )
//                    bRecursed = true;
//            }
//            if ( !bRecursed )
//            {
//                arRecursedObjects.push(object[p]);
//                // this.DisplayObject(object[p], prefix + '.' + p);
//            }
//            else
//                this.Debug(prefix + '.' + p + ' = ' + object[p]);
//        }
//        else
//        {
            try
            {
				names.push(prefix + '.' + p + ' = ' + object[p]);
			}
			catch( e )
			{
				names.push('Error on key: ' + p + ' --> ' + e);
			}
        //}        
    }    
    
    function sort(a,b)
    {
        var maxLength = a.length;
        if ( b.length < maxLength )
            maxLength = b.length;
            
        for( var n=0; n < maxLength; n++ )
        {
            var A=a.charAt(n);
            var B=b.charAt(n);
            
            if ( A != B )
            {
                return A>B ? 1: A < B ? -1 : 0;    
            }
        }
        
        return 1;
    }
    
    names.sort(sort);
    
    for( var n=0; n < names.length; n++ )
    {
	    this.Debug(names[n]);
    }
    
}

function fnWindowLeft()
{
	if ( typeof document.body.scrollLeft != 'undefined' )
	{
		return document.body.scrollLeft;
	}
	else
	{
		return  window.pageXOffset;
	}	
}

function fnWindowRight()
{
	if ( typeof document.body.scrollLeft != 'undefined' )
	{
		return document.body.scrollLeft + document.body.clientWidth;
	}
	else
	{
		return window.pageXOffset + window.innerWidth;
	}
}

function fnWindowTop()
{
	if ( typeof document.body.scrollLeft != 'undefined' )
	{
		return document.body.scrollTop;
	}
	else
	{
		return window.pageYOffset;
	}
}

function fnWindowBottom()
{
	if ( typeof document.body.scrollLeft != 'undefined' )
	{
		return document.body.scrollTop + document.body.clientHeight;
	}
	else
	{
		return window.pageYOffset + window.innerHeight;
	}
}

function fnDocumentRight()
{
	if ( document.body.scrollHeight )
		return document.body.scrollWidth;
	else
		return document.scrollWidth;
}

function fnDocumentBottom()
{
	if ( document.body.scrollHeight )
		return document.body.scrollHeight;
	else
		return document.scrollHeight;
}

function fnCenterObject(elem)
{
	elem.style.position = 'absolute';
	// elem.style.top = Sys_Objectware Sys_Objectware.WindowTop() + 
	// elem.style.left = Sys_Objectware.WindowRight() - elem.offsetWidth;
	
	var bVisible = Sys_Objectware.ObjectVisible(elem);
	Sys_Objectware.ShowElement(elem);	
		
	var iWidth = elem.offsetWidth;
	var iHeight = elem.offsetHeight;
	
	if ( !bVisible )
		Sys_Objectware.HideElement(elem);
	
	elem.style.left = Sys_Objectware.WindowLeft() + (  Sys_Objectware.WindowRight() - Sys_Objectware.WindowLeft() ) / 2 - iWidth / 2 + 'px';
	elem.style.top = Sys_Objectware.WindowTop() + ( Sys_Objectware.WindowBottom() - Sys_Objectware.WindowTop() ) / 2 - iHeight / 2 + 'px';
}

function fnSetObjectTransparency(elem, opacity)
{
	if ( typeof elem.style.filter != 'undefined' )
		elem.style.filter = 'alpha(opacity = ' + ( opacity ) + ');';
	else if (typeof elem.style.mozOpacity != 'undefined')
		elem.style.mozOpacity = opacity / 100;
	else
		elem.style.opacity = opacity / 100;
}

function fnMaximizeObject(elem)
{
	elem.style.position = 'absolute';
	elem.style.top = 0
	elem.style.left = 0
	elem.style.width = Sys_Objectware.DocumentRight() + 'px';
	elem.style.height = Sys_Objectware.DocumentBottom() + 'px';
}

function fnMoveObject(elem, x, y)
{
	elem.style.position = 'absolute';
	elem.style.left = x + 'px';
	elem.style.top = y + 'px';
}

function fnObjectVisible( elem )
{
	return elem.style.visibility != 'hidden';
}

function fnObjectHit(elem, x, y )
{
	if ( !Sys_Objectware.ObjectVisible(elem) )
		return false;
		
	var iLeft = Sys_Objectware.GetAbsoluteLeft(elem);
	var iTop = Sys_Objectware.GetAbsoluteTop(elem);
	
	var iWidth = elem.offsetWidth;
	var iHeight = elem.offsetHeight;
		
	if ( x <= iLeft + iWidth && x >= iLeft && y <= iTop + iHeight && y >= iTop )
		return true;
	else
		return false;
}

function fnCreateFloatingIFrame(zIndex, left, top, width, height, elemAttach)
{
	var frm = document.createElement('iframe');
	
	frm.style.zIndex = zIndex;
	frm.src = 'javascript:false;';
	frm.style.position = 'absolute';
    frm.frameBorder = 0;
    frm.border = 0;
    frm.style.border = 0;
    frm.style.visibility = 'visible';
    frm.style.display = '';
    
	if ( elemAttach )
		elemAttach.appendChild(frm);
	else
		document.body.appendChild(frm);
	
	Sys_Objectware.SetObjectTransparency(frm, 0);
	if ( left && top && width && height )
	{
		frm.style.left = left + 'px';
		frm.style.top = top + 'px';
		frm.style.width = width + 'px';
		frm.style.height = height + 'px';
	}
	else
		Sys_Objectware.MaximizeObject(frm);

	return frm;
}

function fnDestroyFloatingIFrame(iFrame, elemAttach)
{
	try
	{
		if ( elemAttach )
			elemAttach.removeChild(iFrame);
		else
			document.body.removeChild(iFrame);
	}
	catch(r)
	{
	}
}

var _currentPopups = 0;
var _currentPopupZ = 10;

function fnAddPopupClick(clickElem, clickTargetElem)
{
	clickElem = document.getElementById(clickElem);
	clickTargetElem = document.getElementById(clickTargetElem);
	
	var f = function(event)
	{
		if ( !Sys_Objectware.ObjectVisible(clickTargetElem) )
		{
			Sys_Objectware.ShowPopup(clickTargetElem);
			
			clickTargetElem.IFRAME = Sys_Objectware.CreateFloatingIFrame(_currentPopupZ - 1, Sys_Objectware.GetAbsoluteLeft(clickTargetElem), Sys_Objectware.GetAbsoluteTop(clickTargetElem), clickTargetElem.offsetWidth, clickTargetElem.offsetHeight );
			
			_currentPopups++;
		}
		
		clickTargetElem.style.zIndex = _currentPopupZ++;
		
		if ( event )
			event.returnValue = false;
			
		return false;	
	}
	
	clickElem.onclick = f;		
	
	fnAddPopupClickFunction(clickTargetElem, 'Close', function() { Sys_Objectware.HideElement(clickTargetElem); _currentPopups--; if ( _currentPopups == 0 ) _currentPopupZ = 10;  Sys_Objectware.DestroyFloatingIFrame(clickTargetElem.IFRAME);});
}

function fnAddPopupClickFunction(elem, value, func)
{
 if ( Sys_Objectware.ValidObject(elem.childNodes) )
  {
      for( var n=0; n < elem.childNodes.length; n++ )
	  {
	   	   if ( typeof elem.childNodes[n].getAttribute != 'undefined' && elem.childNodes[n].getAttribute('PopupFunction') && elem.childNodes[n].getAttribute('PopupFunction') == value )
		   {
		   	  elem.childNodes[n].onclick = func;
			  elem.style.cursor = 'pointer';
		   }		   
		   fnAddPopupClickFunction(elem.childNodes[n], value, func);
	  }
  }
}

function fnShowPopup(popupElem, mouseX, mouseY)
{
	if ( !mouseX )
		mouseX = this.CurrentMouseX;
		
	if ( !mouseY )
		mouseY = this.CurrentMouseY;
		
	Sys_Objectware.ShowElement(popupElem);

	var iWidth = popupElem.offsetWidth;
	var iHeight = popupElem.offsetHeight;
	
	var iScreenRight = Sys_Objectware.WindowRight();
	var iScreenLeft = Sys_Objectware.WindowLeft();
	var iScreenTop = Sys_Objectware.WindowTop();
	var iScreenBottom = Sys_Objectware.WindowBottom();
	
	var x = Sys_Objectware.GetAbsoluteLeft(popupElem);
	var y = Sys_Objectware.GetAbsoluteTop(popupElem);
	
	// 0 - Bottom Right
	// 1 - Top Right
	// 2 - Middle Right
	
	// 3 - Top Left
	// 4 - Bottom Left
	// 5 - Middle Left
	// 6 - Top Middle
	// 7 - Bottom Middle
	// 8 - Middle Middle 
	
	var iPopDirection = 0;
	
	if ( iScreenRight - mouseX - 1 > iWidth  ) // fits on the right
	{
		if ( iScreenBottom - mouseY - 1 > iHeight ) // fits under 
		{
			iPopDirection = 0;
		}
		else if ( mouseY - iScreenTop - 1 > iHeight ) // fits over
		{
			iPopDirection = 1;
		}
		else // fits in middle
		{
			iPopDirection = 2;
		}
	}
	else if ( mouseX - iScreenLeft - 1 > iWidth ) // fits on the left
	{
		if ( mouseY - iScreenTop - 1 > iHeight ) // fits over
		{
			iPopDirection = 3;
		}
		else if ( iScreenBottom - mouseY - 1 > iHeight ) // fits under 
		{
			iPopDirection = 4;
		}
		else // fits in middle
		{
			iPopDirection = 5;
		}
	}
	else // go in middle
	{
		if ( iScreenBottom - mouseY - 1 > iHeight ) // fits under 
		{
			iPopDirection = 6;
		}
		else if ( mouseY - iScreenTop - 1 > iHeight ) // fits over
		{
			iPopDirection = 7;
		}
		else // fits in middle
		{
			iPopDirection = 8;
		}
	}
	
	if ( iPopDirection == 0 )
	{
		x = mouseX + 1;
		y = mouseY + 1;
	}
	else if ( iPopDirection == 1 )
	{
		// x = mouseX - 1 - iWidth;
		x = mouseX + 1;
		y = mouseY - 1 - iHeight;
	}
	else if ( iPopDirection == 2 )
	{
		x = mouseX + 1;
		y = iScreenTop + ( iScreenBottom - mouseY ) / 2;
	}
	else if ( iPopDirection == 3 )
	{
		x = mouseX - 1 - iWidth;
		y = mouseY - 1 - iHeight;
	}
	else if ( iPopDirection == 4 )
	{
		x = mouseX - 1 - iWidth;
		y = mouseY + 1;
	}
	else if ( iPopDirection == 5 )
	{
		x = mouseX - 1 - iWidth;
		y = iScreenTop + ( iScreenBottom - mouseY ) / 2;
	}
	else if ( iPopDirection == 6 )
	{
		x = iScreenLeft + ( iScreenRight - iWidth ) / 2;
		y = mouseY - 1 - iHeight;
	}
	else if ( iPopDirection == 7 )
	{
		x = iScreenLeft + ( iScreenRight - iWidth ) / 2;
		y = mouseY + 1 
	}
	else if ( iPopDirection == 8 )
	{
		x = iScreenLeft + ( iScreenRight - iScreenLeft - iWidth ) / 2;
		y = iScreenTop + ( iScreenBottom - iScreenTop - iHeight ) / 2;
	}
	
	Sys_Objectware.MoveObject(popupElem, x,y);
}

function fnCopyObject(obj)
{
	return obj;
}

function fnNumberToString(num, arg)
{
	if ( typeof arg == 'undefined' )
		return '' + num;
	else if ( arg.toLowerCase() == 'c' )
	{
		num = fnRound(parseFloat(num),2);
		
		var strCurrency = num.toString();
		
		if ( strCurrency.lastIndexOf('.') == -1 )
			strCurrency = strCurrency + '.00';
		
		if ( strCurrency.lastIndexOf('.') == strCurrency.length - 2 )
			strCurrency += '0';
		
		return '$' + strCurrency;
	}
	else
	{
		throw 'Unknown formatting for float: ' + arg;
	}
}

function fnRound(num, arg)
{
	if ( isNaN(num) )
		throw num + ' is not a number.';
	var resolution = 2;
	
	if ( typeof arg != 'undefined' || arg != null && !isNaN(arg) )
		resolution = arg;
	
	var precision = 1;
	for( var n=0; n < resolution; n++ )
	{
		precision = precision * 10;
	}
	
	num = Math.round(num * precision) / precision;
	
	return num;
}

function fnValidObject(obj)
{
    if ( typeof obj != 'undefined' && obj != null )
        return true;
    
    return false;
}

function doModal(modalDiv, onok, oncancel, bLockScreen, posLeft, posTop)
{
	if ( Sys_Objectware.ModalCancel != null )
	{
		Sys_Objectware.ModalCancel();
		Sys_Objectware.ModalCancel = null;
	}

	if ( bLockScreen )
		Sys_Objectware.GrayScreen();
	
 	var oldCancelFunc = oncancel ? oncancel : Function;
 	
	oncancel = function()
	{
		if ( bUseIfrm )
			Sys_Objectware.HideElement(ifrmPopup);
		
		Sys_Objectware.HideElement(modalDiv); 
		
		if ( bLockScreen )
			Sys_Objectware.UnGrayScreen();
		
		oldCancelFunc();
		
		Sys_Objectware.ModalCancel = null;
	}
	
    oldOkFunc = onok ? onok : Function;
    
    onok = function()
    {
		if ( bUseIfrm )
			Sys_Objectware.HideElement(ifrmPopup);
		
		Sys_Objectware.HideElement(modalDiv); 
		Sys_Objectware.UnGrayScreen();
		
		oldOkFunc();
		
		Sys_Objectware.ModalCancel = null;
    }
		
	modalDiv.style.zIndex = 501;
	
			
	if ( !Sys_Objectware.ValidObject(posLeft ) )
		Sys_Objectware.CenterObject(modalDiv);
	else
		Sys_Objectware.MoveObject(modalDiv, posLeft, posTop);
		
	Sys_Objectware.ShowElement(modalDiv);
		
	if ( bUseIfrm )
	{
		Sys_Objectware.ShowElement(ifrmPopup);
		Sys_Objectware.MoveObject(ifrmPopup, posLeft, posTop);
		ifrmPopup.style.zIndex = 500;
		
		ifrmPopup.style.width = modalDiv.offsetWidth + 'px';
		ifrmPopup.style.height = modalDiv.offsetHeight + 'px';
	}
		
	fnModalAddOnOk(modalDiv, onok);
	fnModalAddOnCancel(modalDiv,oncancel);
		
	Sys_Objectware.ModalCancel = oncancel;
	
	return false;
}

var grayDivID = null;

function fnGrayScreen()
{
	var div = document.getElementById(grayDivID);
	if ( Sys_Objectware.ValidObject(div) )
 		document.body.removeChild(div);
		
		
	div = document.createElement('div');
	div.id = 'graydiv';
	grayDivID = div.id;
	document.body.appendChild(div);

	div.style.zIndex = 500;
	div.style.backgroundColor = '#999999';
	
	if ( bUseIfrm )
	{
		Sys_Objectware.ShowElement(ifrmPopup);
		ifrmPopup.style.zIndex = 499;
		Sys_Objectware.MaximizeObject(ifrmPopup);
		Sys_Objectware.SetObjectTransparency(div, 0);		
	}

	Sys_Objectware.MaximizeObject(div);
	Sys_Objectware.SetObjectTransparency(div, 50);		
	Sys_Objectware.ShowElement(div);
}

function fnUnGrayScreen()
{
	if ( grayDivID != null )
	{
		var div = document.getElementById(grayDivID);
		if ( Sys_Objectware.ValidObject(div) )
 			document.body.removeChild(div); 
	}
	
	if ( bUseIfrm )
		Sys_Objectware.HideElement(ifrmPopup);
}

function fnModalAddOnCancel(elem, oncancel)
{
 if ( Sys_Objectware.ValidObject(elem.childNodes) )
  {
      for( var n=0; n < elem.childNodes.length; n++ )
	  {
	   	   if ( elem.childNodes[n].id == 'tdCancel' )
		   {
		   	  elem.childNodes[n].onclick = oncancel;
			  elem.style.cursor = 'pointer';
		   }		   
		   fnModalAddOnCancel(elem.childNodes[n], oncancel);
	  }
  }
}

function fnModalAddOnOk(elem, onok)
{
	
  if ( Sys_Objectware.ValidObject(elem.childNodes) )
  {
      for( var n=0; n < elem.childNodes.length; n++ )
	  {
	   	   if ( elem.childNodes[n].id == 'tdOk' )
		   {
		   	  elem.childNodes[n].onclick = onok;
			  elem.style.cursor = 'pointer';
		   }
		   
		   fnModalAddOnOk(elem.childNodes[n], onok);
	  }
  }
}

function Redirect(url)
{
   window.location = url; 
}

function fnRightAlignAndCenterToElement(elemAnchor, elemToCenter, iPadding)
{
	if ( iPadding == null )
		iPadding = 0;
		
	var iLeft = Sys_Objectware.GetAbsoluteLeft(elemAnchor);
	var iTop = Sys_Objectware.GetAbsoluteTop(elemAnchor);
	var iWidth = elemAnchor.offsetWidth;
	var iHeight = elemAnchor.offsetHeight;
	
	var x = iLeft + iWidth + iPadding;
	var y = iTop + iHeight / 2 - elemToCenter.offsetHeight / 2;
	
	Sys_Objectware.MoveObject(elemToCenter, x, y );		
} 

function fnScrollToTop()
{
	window.scroll(0,0);
}

function fnAddDefaultSubmit(ctl, btn)
{
	if ( typeof ctl == 'string' );
		ctl = document.getElementById(ctl);
		
	if ( typeof btn == 'string' )
		btn = document.getElementById(btn);
		
	if ( ctl != null && btn != null )
	{
		ctl.onkeydown = function(event)
		{
			if ( !event )
				event = window.event;
				
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) 
			{
				Sys_Objectware.FireOnClick(btn);			
				
				if( typeof event.preventDefault != 'undefined' )
					event.preventDefault();
				
				event.returnValue = false;
				
				return false;
			} 
			else 
				return true;
		}
	}
}

/* Native Type prototypes */

String.prototype.LeftTrim = function leftTrim() 
{
	var sString = this;
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

String.prototype.RightTrim = function rightTrim() 
{
	var sString = this;
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

String.prototype.Trim = function trimAll() 
{
	var sString = this;
	sString = sString.LeftTrim();
	sString = sString.RightTrim();
	return sString;
}

/* Instantiate Global Object */

var Sys_Objectware = new Sys_Objectware();
//	Sys_Objectware.EnableDebug();