
// *.js			import System;		package myLayer { ... }

// *.asax; *.aspx		<%@ Import Namespace="myLayer" %>
// *.cs		using System;		using System.Collections;	namespace myLayer { public class } 

// *.vb			Imports System		Namespace myLayer

//C:/Develop/JavaTutor/uiswing/components/example-swing/index.html#TableSorterDemo

var a_windows=new Array(); 
var activeWin=-1;
var JSPath="../Image"


function SetImageJSPath(dir)
{
	JSPath = dir
}

function trim( key1 )
{
	var key = key1;
	while ( key.length && key.charAt( key.length - 1 ) == " "  ) key = key.slice(0, -1);
	while ( key.length && key.charAt( 0 ) == " " ) key = key.substring( 1 ) ;		
	
	return key;
}

function lib_resize_win(i,w,h){if(a_windows[i]!=null)a_windows[i].Resize(w,h);}

function lib_set_title_text(title,tid)
{
	var id=-1;										     
	for(var i=0;i<a_windows.length && id==-1;i++)
	{
		if (a_windows[i] != null)
		{
			if ( a_windows[i].getTradeID().length==0 && tid.length>0 )
			{	
				var obj = document.getElementById("winName"+i);
				a_windows[i].TradeID=tid;		
				obj.innerText=title;
				windowCallbackOpen(i,title);
				SetActiveWin(i,1);
				id=i;
			}
		}
	}
	return id;
}


function getHandle(){var id = -1;for(var h=0;id==-1 && h<a_windows.length;h++){if(a_windows[h]==null)id=h;}if(id==-1)id=a_windows.length;return id;}
function lib_set_status(i,t){var obj=document.getElementById("divWinStatusBar"+i);if(obj!=null)obj.innerText=t;}
function lib_win_counter(){var n=0;for(var i=0;i< a_windows.length;i++){if(a_windows[i]!=null)n++;}return n;}

function lib_bwcheck()
{ 
	this.dom=document.getElementById?1:0;
	this.ver=navigator.appVersion;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom)||(this.ver.indexOf("MSIE 7")>-1&&this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ie =(this.ie4||this.ie5||this.ie6);
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;

	return this;
}

function overlap(r1,r2){return(r1[0]+r1[2]>r2[0]&&r1[1]+r1[3]>r2[1]&&r1[0]<r2[0]+r2[2]&&r1[1]<r2[1]+r2[3]?1:0);}

function lib_bg(color){this.css.backgroundColor=color;}
function lib_moveIt(x,y){this.cX=x;this.cY=y;this.css.left=x;this.css.top=y;}
function lib_moveBy(x,y){this.moveIt(this.cX+x,this.cY+y);}
function lib_showIt(){this.css.visibility="visible";}
function lib_hideIt(){this.css.visibility="hidden";}

function lib_clipTo(r,b)
{ 
var t=0;var l=0;if(r<0)r=0;if(b<0)b=0;
this.ct=t; this.cW=r; this.cH=b; this.cl=l;
this.css.clip="rect("+t+","+r+","+b+","+l+")"; // LINE_KILL
this.css.width=r; this.css.height=b; 		
}

function lib_obj(obj,num)
{
this.evnt  =document.getElementById(obj);
this.css   =this.evnt.style;  
this.clipTo=lib_clipTo;	
this.moveIt=lib_moveIt; 
this.moveBy=lib_moveBy; 
this.showIt=lib_showIt; 
this.hideIt=lib_hideIt;
this.bg    =lib_bg; 
this.winNumber=num; 

return this;
}


function windows_init(arg)
{
if(arg)JSPath=arg;

a_windows.dragObj     = -1; 
a_windows.sizeObj   = -1; 
a_windows.zIndex      = 100; 
					
a_windows.bgcolor     = "silver"; 
a_windows.bgcoloron   = "#cdceff";  
a_windows.buttonsw    = 63;
a_windows.bordersize  = 1;	
 
a_windows.headh       = 18;  
a_windows.bottomh     = 18; 

a_windows.resizew     = 22;  
a_windows.resizeh     = 18; 
a_windows.MarginX     =  5; 

a_windows.MenuHeight  = 25; 

a_windows.defwidth    = 300; 
a_windows.defheight   = 300;	



a_windows.between     = 15; 
a_windows.logow       = 19;

a_windows.minWinNum=0; 
a_windows.lastMinWinX=0; 
a_windows.lastMinWinY=0; 
a_windows.minStateHeight=20; 
a_windows.minStateWidth=150;

a_windows.clickedX=0; 
a_windows.clickedY=0;

a_windows.mouseX=0;
a_windows.mouseY=0;

document.onmousemove = mmove;
document.onmousedown = mouseDown;	//mdown;

//document.onmouseup   = mup;

}


function create_window(i,tid,x,y,w,h,bg,bga)
{
if(!x) x=a_windows.MarginX; if(!y) y=a_windows.MenuHeight; if(!w) w=a_windows.defwidth; if(!h) h=a_windows.defheight;

a_windows[i]            = new lib_obj('divWin'         +i,  i)
a_windows[i].oWindow    = new lib_obj('divWindow'      +i,  i)


a_windows[i].oCaption   = new lib_obj('divWinHead'     +i,  i)
a_windows[i].oStatusBar = new lib_obj('divWinStatusBar'+i,  i)
a_windows[i].oResize    = new lib_obj('divWinResize'   +i,  i)
a_windows[i].oButtons   = new lib_obj('divWinButtons'  +i,  i)

a_windows[i].oStatusBar.evnt.onmousedown = new Function("SetActiveWin(" + i + ",0)");

a_windows[i].oCaption.evnt.ondblclick = new Function("wm_dbclick("+i+")")
a_windows[i].oCaption.evnt.onmouseout = new Function("wm_mout()")
a_windows[i].oCaption.evnt.onmouseover= new Function("wm_mover("+i+")")

a_windows[i].oResize.evnt.onclick     = new Function("wm_mover("+i+",1,1)")
a_windows[i].oResize.evnt.onmouseover = new Function("wm_mover("+i+",1)")
a_windows[i].oResize.evnt.onmouseout  = new Function("wm_mout()")	
	


a_windows[i].oCaption.css.cursor      = "move"; 
a_windows[i].oResize.css.cursor       = "nw-resize";

a_windows[i].oWindow.css.overflow     = "hidden"; 
a_windows[i].css.overflow             = "hidden";

a_windows[i].cX=x;
a_windows[i].cY=y;
a_windows[i].cW=w; 
a_windows[i].cH=h;
a_windows[i].stdX=x;
a_windows[i].stdY=y;
a_windows[i].stdW=w; 
a_windows[i].stdH=h;
a_windows[i].defbg =bg  || a_windows.bgcolor; 
a_windows[i].defbga=bga || a_windows.bgcoloron;
a_windows[i].state="reg";

a_windows[i].TradeID=tid;
a_windows[i].menuBind="";



a_windows[i].oCaption.bg(a_windows[i].defbg)
a_windows[i].oStatusBar.bg(a_windows[i].defbg)

a_windows[i].Resize  = w_resize;	
a_windows[i].Close   = w_close; 
a_windows[i].Maximize= w_maximize;
a_windows[i].Minimize= w_minimize;	
a_windows[i].Regular = w_regular; 
a_windows[i].UpdateIndex=w_SetzIndex;

a_windows[i].getTradeID=w_getTradeID;
a_windows[i].makeSize=w_makeSize;
												
a_windows[i].UpdateIndex();						//alert( 'values: ' + x + ' ' + y );
a_windows[i].moveIt(x,y); 						//alert( 'values: ' + w + ' ' + h );
a_windows[i].Resize(w,h); 
												
a_windows[i].oWindow.moveIt(a_windows.bordersize,a_windows.headh);
a_windows[i].showIt();

}

 
 // [ oActive = ] document.activeElement

function frm_mmove(w)
{
//	var id=a_windows.sizeObj;
	
//	if (id > -1)									  
//	{	
//		a_windows[id].cW += (event.clientX - a_windows[id].cW); 
//		a_windows[id].cH += (event.clientY - a_windows[id].cH);
//		a_windows[id].Resize(a_windows[id].cW,a_windows[id].cH)
//	}
//	return true;
	return false;
}


function addWindow(title,content,tid,x,y,w,h,bg,bga)
{
var dID = getHandle();

var str="<div id='divWin"+dID+"' class='clWin'>\n"

+"<div id='divWinHead" + dID + "' class='clCaption'><img src='" + JSPath + "/icon.gif' width='19' height='19' alt='' border='0' align='top'>&nbsp;"
+"<span id='winName" + dID + "'>" + title + "</span></div>\n"

+"<div id='divWinButtons"+dID+"' class='clWinButtons'>\n"
+"<img src='" + JSPath + "/min.gif'   width='19' height='19' alt='' border='0' align='top' href='#' onclick='a_windows["+dID+"].Minimize(); return false'>\n"
+"<img src='" + JSPath + "/max.gif'   width='19' height='19' alt='' border='0' align='top' href='#' onclick='a_windows["+dID+"].Maximize(); return false'>\n"
+"<img src='" + JSPath + "/close.gif' width='19' height='19' alt='' border='0' align='top' href='#' onclick='a_windows["+dID+"].Close();    return false'>\n"
+"</div>\n"

+"<IFRAME class='clWindow' id='divWindow" + dID 
+ "' name='divWindow" + dID 
+ "' scrolling=auto frameBorder=0 src=" + content
+ " onmouseover='frm_mmove("+dID+");' " 
+ " onmousemove='frm_mmove("+dID+");' " 

+ " onFocus='SetActiveWin("+dID+",1);' "
/*+ " onmouseup='frm_mup("+dID+");' " */

+ "></IFRAME>\n"

+"<div id='divWinStatusBar"+dID+"' class='clStatusBar'>Status</div>" 
+"<div id='divWinResize"+dID+"'    class='clResize'><img src='" + JSPath + "/size.gif' border='0'></div>"

+"</div>"; 
/*alert(str);*/


if (document.getElementById&&!document.all)
{
	var Obj = document.getElementById("docBody");

	var range = document.createRange();
	range.setStartBefore(document.body.lastChild);
	var docFrag = range.createContextualFragment(str);
	//document.body.appendChild(docFrag)
	Obj.parentNode.insertBefore(docFrag,Obj);

	var IFrameObj = document.getElementById('divWindow'+dID)
	var IFrameDoc=IFrameObj.contentDocument;
	IFrameDoc.location.replace(content);

	
	/* This Does Not Work. See jswindowCallBack.js::bindHandlers */
	//IFrameObj.contentWindow.document.onclick=new Function("SetActiveWin("+dID+",1)");
	//IFrameDoc.onclick=new Function("SetActiveWin("+dID+",1)");
	parent.frames[parent.frames.length-1].document.onclick=new Function("SetActiveWin("+dID+",1)");
}
else			   
{
	docBody.insertAdjacentHTML("beforeEnd",str);
}                        

create_window(dID,tid,x,y,w,h,bg,bga);

if (tid.length>0) windowCallbackOpen(dID,title);

SetActiveWin(dID,1);
return dID;
}


function MakeArray(n){this.length=n;return this;}

function wm_dbclick(num){ if(num>-1) a_windows[num].Maximize(); else if(a_windows.dragover>-1) a_windows[a_windows.dragover].Maximize();}
function wm_mout()           { a_windows.dragover=-1; a_windows.sizeover=-1 }

function wm_mover(n,resize,clk)
{ 
	if (!resize) 
	{	
		if (a_windows[n]!=null /*&&!a_windows[n].bNoMove*/ )
		if(a_windows.dragObj==-1)	a_windows.dragover=n; 
	}
	else
	{ 
		if(a_windows.sizeObj==-1)a_windows.sizeover=n;
		if (clk) SetActiveWin(n);
	}
//alert(n + ' ' + resize)
}
							
function mouseDown(e)
{
	var x = a_windows.mouseX;
	var y = a_windows.mouseY;
	var idd=a_windows.dragObj=a_windows.dragover; 
	var ids=a_windows.sizeObj=a_windows.sizeover;
	var acting = false;
	var theWindow, exOnEnd;
	e = e||window.event;
	function mouseUp(e)
	{
		globalEventListener.mouseup.removeItem(mouseUp);
		globalEventListener.selectstart.removeItem(stopSelectionOnM_Move);
		acting = false;
		exOnEnd();
		theWindow.oWindow.css.display = '';
		if(a_windows.dragObj!=-1){
			id=a_windows.dragObj;	
			if((a_windows[id])&&(a_windows[id].state == "reg")){
				a_windows[id].stdX=a_windows[id].cX;
				a_windows[id].stdY=a_windows[id].cY;
			}
			a_windows.dragover=a_windows.dragObj=-1;		  // release
		}else if(a_windows.sizeObj!=-1){
			id=a_windows.sizeObj;
			a_windows[id].stdW=a_windows[id].cW;
			a_windows[id].stdH=a_windows[id].cH;

//lib_set_status(id,a_windows[id].cW + ' ' + a_windows[id].cH )

			a_windows.sizeover=a_windows.sizeObj=-1;		  // release
		}
	}
	function atIntervalsSize()
	{
		if(acting){
			var	x=a_windows.mouseX;
			var y=a_windows.mouseY;
			theWindow.cW += x-a_windows.clickedX;	
			theWindow.cH += y-a_windows.clickedY; 
			if (theWindow.cW<120) {
				theWindow.cW=120;
				mouseUp();
			}
			if (theWindow.cH<120) {
				theWindow.cH=120;
				mouseUp();
			}
			a_windows.clickedX=x;
			a_windows.clickedY=y;
			if(TimedQue.lag < 0){
				theWindow.oWindow.css.display = 'none';
			}
			theWindow.Resize(theWindow.cW,theWindow.cH);
		}
		return acting;
	}
	function atIntervalsDrag()
	{
		var	x=a_windows.mouseX;
		var y=a_windows.mouseY;
		if((theWindow.cX = (x - a_windows.clickedX)) < a_windows.MarginX){
			theWindow.cX = a_windows.MarginX
		}
		if((theWindow.cY = (y - a_windows.clickedY)) < a_windows.MenuHeight){
			theWindow.cY =  a_windows.MenuHeight
		}
		if(TimedQue.lag < 0){
			theWindow.oWindow.css.display = 'none';
		}


//top.status = ''+top.application.document.documentElement.scrollWidth;
//document.body.style.width = Math.max(document.body.offsetWidth , document.documentElement.offsetWidth)+'px'
			theWindow.moveIt(theWindow.cX,theWindow.cY) 
			return acting;
	}
	if (ids>-1)	
	{ 
		theWindow = a_windows[ids]
		a_windows.clickedX = x;	
		a_windows.clickedY = y;
		acting = true;
		TimedQue((exOnEnd = atIntervalsSize));
		globalEventListener.mouseup.addItem(mouseUp);
		globalEventListener.selectstart.addItem(stopSelectionOnM_Move);
		SetActiveWin(ids);
	}
	else if ((idd>-1) && ((e.target||e.srcElement).tagName != 'IMG' ))
	{ 
		theWindow = a_windows[idd];
		if(theWindow){
			a_windows.clickedX=x-theWindow.cX;	
			a_windows.clickedY=y-theWindow.cY; 
			acting = true;
			TimedQue((exOnEnd = atIntervalsDrag));
			globalEventListener.mouseup.addItem(mouseUp);
			globalEventListener.selectstart.addItem(stopSelectionOnM_Move);
			SetActiveWin(idd);
		}
	}

}

function stopSelectionOnM_Move(e){			/* is this junk ? */
	e.returnValue = false;
}

/* 
	Local pages invoke winlibCallBack.js  for function wm_move(e)
	See function createIFrame((src,nTab,url_tree) in jstab.js
*/

function mmove(e,f)  //Mousemove parent.mmove
{ 
	a_windows.mouseX = arguments.length==0?event.screenX:(document.all)?e.screenX:e.pageX;
	a_windows.mouseY = arguments.length==0?event.screenY:(document.all)?e.screenY:e.pageY;
}


/* -------------------------------------------------[KILL]
function mdown(e)	//Mousedown
{ 
	var id=-1;
	var x=(window.event.x||event.clientX)
    var y=(window.event.y||event.clientY) + document.body.scrollTop;
	
	if (a_windows.sizeover>-1)	  {	id=a_windows.sizeObj=a_windows.sizeover; 	}
	else if(a_windows.dragover>-1) {	id=a_windows.dragObj=a_windows.dragover; a_windows.clickedX=x-a_windows[id].cX;	a_windows.clickedY=y-a_windows[id].cY; }
	if (id>-1) { SetActiveWin(id,0); }
		
	return false;    
}

function mmove(e)	//Mousemove
{ 
	var x=(window.event.x||event.clientX)
    var y=(window.event.y||event.clientY) + document.body.scrollTop;
	var idd=a_windows.dragObj
	var ids=a_windows.sizeObj

	if     (ids>-1)	{ if(a_windows[ids]==null) {	a_windows.sizeObj=a_windows.sizeover=-1; } 
	                  else { a_windows[ids].cW = x - a_windows[ids].cX < 120 ? 120 : x - a_windows[ids].cX; 
					         a_windows[ids].cH = y - a_windows[ids].cY < 100 ? 100 : y - a_windows[ids].cY; 
							 a_windows[ids].Resize(a_windows[ids].cW,a_windows[ids].cH) }	}
	else if(idd>-1) { if(a_windows[idd]==null) { a_windows.dragObj=a_windows.dragover=-1; } 
	                  else { a_windows[idd].cX=x-a_windows.clickedX                                                        ; 
					         a_windows[idd].cY=y-a_windows.clickedY < a_windows.MenuHeight ?  a_windows.MenuHeight :  y-a_windows.clickedY; 
							 a_windows[idd].moveIt(a_windows[idd].cX,a_windows[idd].cY) }	}

	return false;
}											 

function mmove(e,f)
{ 
	var x = arguments.length==0?event.screenX:(document.all)?e.screenX:e.pageX;
	var y = arguments.length==0?event.screenY:(document.all)?e.screenY:e.pageY;

	var idd=a_windows.dragObj
	var ids=a_windows.sizeObj

	if (ids>-1)	
	{ 
		if (a_windows[ids]==null) 
		{	
			a_windows.sizeObj=a_windows.sizeover=-1; 
		} 
	    else 
		{ 
			a_windows[ids].cW += x-a_windows.clickedX;	
			a_windows[ids].cH += y-a_windows.clickedY; 
			if (a_windows[ids].cW<120) {a_windows[ids].cW=120; a_windows.sizeObj=a_windows.sizeover=-1;}
			if (a_windows[ids].cH<120) {a_windows[ids].cH=120; a_windows.sizeObj=a_windows.sizeover=-1;}
			a_windows.clickedX=x;
			a_windows.clickedY=y;
		
			a_windows[ids].Resize(a_windows[ids].cW,a_windows[ids].cH) 
		}	
	}
	else if (idd>-1) 
	{ 
		if (a_windows[idd]==null) 
		{ 
			a_windows.dragObj=a_windows.dragover=-1; 
		} 
	    else 
		{ 
			a_windows[idd].cX=x-a_windows.clickedX < a_windows.MarginX    ?  a_windows.MarginX    : x-a_windows.clickedX; 
			a_windows[idd].cY=y-a_windows.clickedY < a_windows.MenuHeight ?  a_windows.MenuHeight : y-a_windows.clickedY; 
			a_windows[idd].moveIt(a_windows[idd].cX,a_windows[idd].cY) 
		}	
	}

	return false;
}											 


[KILL]------------------------------------------------- */

/* -------------------------------------------------

function mup(e)		//Mouseup
{ 
	var id;
	if(a_windows.dragObj!=-1) { id=a_windows.dragObj; a_windows[id].stdX=a_windows[id].cX;  a_windows[id].stdY=a_windows[id].cY; a_windows.dragover=a_windows.dragObj=-1; }
	if(a_windows.sizeObj!=-1) { id=a_windows.sizeObj; a_windows[id].stdW=a_windows[id].cW;	a_windows[id].stdH=a_windows[id].cH; a_windows.sizeover=a_windows.sizeObj=-1; }

	return false;
}
 ------------------------------------------------- */

//function frm_mdown(w) {	return true; }


//function frm_mup(w)
//{
//	var id;
//	if(a_windows.dragObj!=-1) { id=a_windows.dragObj; a_windows[id].stdX=a_windows[id].cX;  a_windows[id].stdY=a_windows[id].cY; a_windows.dragover=a_windows.dragObj=-1; }
//	if(a_windows.sizeObj!=-1) { id=a_windows.sizeObj; a_windows[id].stdW=a_windows[id].cW;	a_windows[id].stdH=a_windows[id].cH; a_windows.sizeover=a_windows.sizeObj=-1; }
//	return true;
//}






function w_resize(w,h)
{
this.oCaption.clipTo(w,a_windows.headh); 						//alert( 'oCaption.clipTo( '     + w + ', '+ a_windows.headh + ' )' );
this.oButtons.moveIt(  w-a_windows.buttonsw,0); 				//alert( 'oButtons.moveIt(  '    + (w-a_windows.buttonsw)    +', 0)' );
this.oStatusBar.moveIt(0, h-a_windows.bottomh);					//alert( 'oStatusBar.moveIt(0, ' + (h-a_windows.bottomh)     + ' )' );
this.oStatusBar.clipTo( w-a_windows.resizew+2,a_windows.bottomh); //alert( 'oStatusBar.clipTo( '   + (w-a_windows.resizew) +', ' + a_windows.bottomh + ' )' );
this.oResize.moveIt(w-a_windows.resizew+2, h-a_windows.resizeh);	//alert( 'oResize.moveIt( ' + (w-a_windows.resizew) + ', ' + (h-a_windows.resizeh) + ' )' );
this.oWindow.clipTo( w-a_windows.bordersize*2, h-a_windows.bottomh-a_windows.headh ); 
this.clipTo(w,h);												//alert( 'this.clipTo( ' +w+', '+h+ ' )' );
}


function w_makeSize(r,x,y,w,h,between)
{
this.UpdateIndex(100+r);
this.state="reg";
this.stdX=this.cX=x; // across
this.stdY=this.cY=y; // down
this.stdW=this.cW=(w-between); 
this.stdH=this.cH=(h-between);

this.Resize(this.stdW,this.stdH); 
this.moveIt(this.cX,this.cY)
this.oResize.showIt()
this.showIt(); 
}


function w_regular()
{
this.oStatusBar.css.visibility="inherit"



this.Resize(this.stdW,this.stdH)
this.moveIt(this.stdX,this.stdY)

this.oResize.showIt() ;
this.state="reg";
this.UpdateIndex(); 



}



function w_maximize()
{
if (this.state!="max")
{

if(this.state!="min")
{
this.stdX = this.cX; 
this.stdY = this.cY;
if(a_windows.lastMinWinX >0) a_windows.minWinNum--;
}
this.moveIt(a_windows.MarginX,a_windows.MenuHeight)	
this.Resize(document.body.offsetWidth-30,document.body.offsetHeight-40); 
this.oStatusBar.showIt(); 
this.state="max";
this.UpdateIndex();
}

else 
{
this.Regular()
}

}



function w_minimize()
{
var define_width=200;

if(this.state!="min")
{ 
if(a_windows.minWinNum == 0)
{
a_windows.lastMinWinY=document.body.offsetHeight - a_windows.minStateHeight;
a_windows.lastMinWinX = 0;
}
a_windows.minWinNum++;

if (document.body.offsetWidth < a_windows.lastMinWinX+a_windows.minStateWidth)
{
a_windows.lastMinWinY -= a_windows.minStateHeight;
a_windows.lastMinWinX = 0;
}

this.stdX=this.cX; 
this.stdY=this.cY;
this.cX   = a_windows.lastMinWinX;
this.cY   = a_windows.lastMinWinY;
this.stdW  = this.cW;
this.stdH = this.cH;    
this.cW  = a_windows.minStateWidth;
this.cH = a_windows.minStateHeight;

a_windows.lastMinWinX += a_windows.minStateWidth;

this.moveIt(this.cX,this.cY); 

this.oStatusBar.hideIt()
this.oResize.hideIt()

this.Resize(define_width,a_windows.headh)
this.UpdateIndex(100);
this.state="min";
}
else
{ 
this.Regular()
}
}



/*

function w_close()
{
	var el;
	var dID = arguments.length == 1 ? arguments[0] : this.winNumber;

	if(dID&&a_windows[dID]!=null)
	{
		var node = document.getElementById( "divWin" + dID );
		var doc = top.application.getDocument(dID);
		if ( (( (doc.forms)&&
				(doc.forms[0])&&
				(el = doc.forms[0].elements['QUIT.STDPANEL.PRESENTATION'])
			  )||
				(el = doc.getElementById('QUIT.STDPANEL.PRESENTATION'))
			)&&	(el) )
		{
			el.click();
		}
		else
		{
			a_windows[dID]=null;	
			if (document.getElementById&&!document.all)
			{
				var Obj = document.getElementById("docBody");
				Obj.parentNode.removeChild(node);
				//document.body.removeChild(node);
			}
			else
			{
				node.removeNode(true);
			}
		
			if (top.application.removeElement) top.application.removeElement("CM"+dID);

			if (lib_win_counter()==0&&a_windows[0].state=="min")
			{
				a_windows[0].Origin();	
			}
			else
			{
				dID=0;
				for(var h=a_windows.length-1;dID==0&&h>=0;h--)
				{
					if(a_windows[h]!=null) dID=h;
				}
				SetActiveWin(dID);
			}
		}
	}
}
*/

function w_close()
{
	var args = w_close.arguments;
	var dID = args.length == 1 ? args[0] : this.winNumber;

	if(a_windows[dID]!=null)
	{
		if (a_windows[dID].getTradeID().length>0)
			windowCallbackClose(dID);

		var node = parent.document.getElementById( "divWin" + dID );
		if(node==null) node = document.getElementById( "divWin" + dID );

		a_windows[dID]=null;	    
		if (document.getElementById&&!document.all)
		{
			var Obj = document.getElementById("docBody");
			Obj.parentNode.removeChild(node);
			//document.body.removeChild(node);
		}
		else
		{
			node.removeNode(true);
		}
	}
}





function w_SetzIndex(reset)
{
if (reset){this.css.zIndex=reset;a_windows.zIndex=100+a_windows.length;}
else{a_windows.zIndex++;this.css.zIndex=a_windows.zIndex;}
}

function w_getTradeID(){return this.TradeID;}

function getActiveWin(){return activeWin;}

function SetActiveWin(w,ds)
{
	if (ds && ds==1){a_windows.sizeObj=a_windows.sizeover=a_windows.dragObj=a_windows.dragover=-1;}

	for ( var i = 0; i < a_windows.length ; i++ )
	{
		if ( a_windows[i] != null )
		{
			if (i==w)
			{
				a_windows[i].oCaption.bg  ( a_windows[i].defbga )
				a_windows[i].oStatusBar.bg( a_windows[i].defbga )								  
				a_windows[i].UpdateIndex();
			}
			else
			{
				a_windows[i].oCaption.bg  ( a_windows[i].defbg )
				a_windows[i].oStatusBar.bg( a_windows[i].defbg )
			}
			//alert(a_windows[i].oStatusBar.bg)
		}
	}
	activeWin = w;
}

function getActiveWindow(){return activeWin;}

/* -------------------------------- */

if(window.addEventListener){
	window.addEventListener('load', function(){
		window.frameElement.style.overflow = 'auto'
		document.body.style.overflow = 'auto';
		parent.document.body.style.overflow = 'auto';
	}, false);
}else if(window.attachEvent){
	window.attachEvent('onload', function(){document.body.style.overflow = 'auto';});
}

/* ---------------------------------------------------------------------------------------------
globalEventListener
------------------

A page local object goes to the top.application frame and reads
a list of events that want reporting to the global object. It
then attaches event handlers (document and window). Probably;
onmousedown, onmouseup and onkey..? for the document and
onresize and onscroll for the window. The event handlers call
a method of this object in the global frame, passing on the
event object (and the window/document that they are attached
to).

The global object maintains a stack of call-back functions
that it executes whenever the corresponding method is called
in response to an event in a contained frame.

The call-back function must follow the contract of: accepting
the event object as its first argument, and (optionally)
accepting the window/document (by reference) as its second.
In addition the return value form the call-back function is
used to determine whether the function stays in the stack and
so is called on the next event. A - true - return value keeps
the function in the stack and a - false - return value means
that it will be removed. Thus a call-back function that is only
interested in the next event can be coded to return false, a
function that is intended to monitor all events of one type
may always return true, and a more complex function might
return a variable value to be dropped from the queue at
whatever point it chooses. (only call-back functions
originating in the top.application frame should be hard coded
to return true else they may be called after their frame has
unloaded, or they should actively employ a remove method on
the global object).

The global object provides both add and remove methods.
The add method should not allow the addition of the same
function to the same queue more than once (Queues are
distinct for each event handled.)


The remove method should be safe when the method it is asked
to remove is not in the queue (Quietly return without acting).


Use:-
   Registering a call-back function to be executed in response
   to any (but potentially just the next, depending on its
   return value) - mouseup - event originating in any frame
   in the browser:-

globalEventListener.mouseup.addItem(callBackFnc);

  Explicitly removing a call-back function that has previously
  been associated with mouseup events:-

globalEventListener.mouseup.removeItem(callBackFnc);

  The other events listed may be used by substituting an
  identifier that corresponds with the event name (without
  the "on" prefix) for mouseup in the above example.

globalEventListener.scroll.removeItem(callBackFnc);
                    ^^^^^^
--------------------------------------------------------------------------------------------- */


var globalEventListener = (function(){
	var interFc = {
		events:{ document:[
					'mousedown',
					'mouseup',
					'keydown',
					'keypress',
					'keyup',
					'selectstart'
				 ],
				 window:[
					'scroll',
					'resize'
				 ]
			   }
	};
	function getFncQueItem(fnc){
		var next = null;
		function p(event, orginator){
			next = (next&&next(event, orginator));
            if(fnc(event, orginator)){
                return p;
            }else{
                fnc = null;
                return next;
            }
		}
		p.addItem = function(d){
			if(fnc != d){ // don't add duplicates
				if(next){
					next.addItem(d);
				}else{
					next = getFncQueItem(d);
				}
			}
		};
		p.removeItem = function(d){
			if(fnc == d){
				fnc = null;
				return next;
			}else if(next){
				next = next.removeItem(d);
			}
			return this;
		};
		return p;
	}
	function getFncQue(){
		var base = null;
		function stackRoot(event, orginator){
			base = base&&base(event, orginator);
		};
		stackRoot.addItem = function(fnc){
			if(base){
				base.addItem(fnc)
			}else{
				base = getFncQueItem(fnc);
			}
		};
		stackRoot.removeItem = function(d){
			base = base&&base.removeItem(d);
		};
		return stackRoot;
	}
	function addHandlers(obj, namesArray){
		for(var c = namesArray.length;c--;){
			obj[namesArray[c]] = getFncQue();
		}
	}
	addHandlers(interFc, interFc.events.window);
	addHandlers(interFc, interFc.events.document);
	return interFc;
})();


if(typeof pageYOffset == 'number'){
	globalEventListener.scroll.addItem(function(){
top.status = '1 '+(innerWidth+pageXOffset)
		document.body.style.heigth = (innerHeight+pageYOffset)+'px';
		document.body.style.width = ((innerWidth-16)+pageXOffset)+'px';
		return true;
	});
}

var TimedQue = (function(){
	var base, timer;
	var interval = 70; // lower (min=40[25 framespersecond]) then number more likely to 'blank' [smoother animate]
	var newFncs = null;
	function addFnc(next, f){
		function t(){
			next = next&&next();
			if(f()){
				return t;
			}else{
				f = null;
				return next;
			}
		}
		t.addItem = function(d){if(next){next.addItem(d);}else{next = d;}return this;};t.finalize = function(){return ((next)&&(next = next.finalize())||(f = null));};return t;}
	function tmQue(fc){
		if(newFncs){
			newFncs = newFncs.addItem(addFnc(null, fc));
		}else{
			newFncs = addFnc(null, fc);
		}
		tmQue.lag = interval;
		if(!timer){
			timer = setTimeout(tmQue.act, interval);
		}
	}
	tmQue.act = function(){
		var t, fn = newFncs, strt = new Date().getTime();
		if(fn){
			newFncs = null;
			if(base){
				base.addItem(fn);
			}else{
				base = fn;
			}
		}
		base = base&&base();
		if(base||newFncs){
			tmQue.lag = (t = interval - (new Date().getTime() - strt));
			timer = setTimeout(tmQue.act, ((t > 0)?t:1));
		}else{
			timer = null;
		}
	};
	tmQue.lag = interval;
	tmQue.act.toString = function(){return 'TimedQue.act()';};
	tmQue.finalize = function(){timer = timer&&clearTimeout(timer);base = base&&base.finalize();newFncs = [];};
	return tmQue;
})();


/* -------------------------------- */


function closeWindows(){for(var i=0;i< a_windows.length;i++){if(a_windows[i]!=null){a_windows[i].Close(i);}}}

function lib_tile_vertical()
{
	var realWinNum = lib_win_counter();
	var r=0;
	var z=1;

	var nCols = realWinNum < 4 ? realWinNum : Math.ceil(Math.sqrt(realWinNum));
	var nRows = realWinNum < 4 ? 1 : Math.ceil( realWinNum / nCols ) ;

	var nCount = nCols
	var x=a_windows.MarginX; 
	var y=a_windows.MenuHeight;
	var w=(document.body.offsetWidth  - 30) / nCols;
	var h=(document.body.offsetHeight - a_windows.MarginX - y) / nRows ;

	for ( var i = 0 ; i < a_windows.length ; i++ )
	{
		if (a_windows[i] != null )
		{
			if ( r == nCount ) 
			{
				x = a_windows.MarginX;
				y += h;
										 
				/* nCols assumes say 3x3 If say 7 adjust as 3,2,2 */
				if ((nRows-z)>0)
					nCols = Math.ceil( (realWinNum - r) / (nRows - z) ) ;
				else
					nCols = Math.ceil( (realWinNum - r) ) ;
				z++;
				w = (document.body.offsetWidth - 30) / nCols; 
				nCount += nCols
			}
			a_windows[i].makeSize(r,x,y,w,h,0);
			x += w;
			r++;
		}
	}

}

function lib_tile_horizontal()
{
	var realWinNum = lib_win_counter();
	var r=0;
	var z=1;

	var nRows = realWinNum < 4 ? realWinNum : Math.ceil(Math.sqrt(realWinNum));
	var nCols = realWinNum < 4 ? 1 : Math.ceil( realWinNum / nRows ) ;
	
	var nCount = nRows;
	var x=a_windows.MarginX; 
	var y=a_windows.MenuHeight;
	var w=(document.body.offsetWidth  - 30) / nCols ;
	var h=(document.body.offsetHeight - a_windows.MarginX - y) / nRows;

	for ( var i = 0 ; i < a_windows.length ; i++ )
	{
		if (a_windows[i] != null )
		{
			if ( r == nCount ) 
			{
				x += w;					// accross
				y = a_windows.MenuHeight;	// down
										 
				/* nRows assumes say 3x3 If say 7 adjust as 3,2,2 */
				
				if ((nCols-z)>0)
					nRows = Math.ceil( (realWinNum - r) / (nCols - z) ) ;
				else
					nRows = Math.ceil( (realWinNum - r) ) ;
				
				
				z++;
				w=(document.body.offsetWidth  - 30) / nCols ;
				h=(document.body.offsetHeight - a_windows.MarginX - y) / nRows;
				nCount += nRows
			}	
			a_windows[i].makeSize(r,x,y,w,h,a_windows.between);

			y += h
			r++;
		}
	}

}

function lib_cascade_win()
{
	var r = 0;
	var x = a_windows.MarginX; 
	var y = a_windows.MenuHeight;
	var w = a_windows.length ? (a_windows[0] != null && a_windows[0].stdW ? a_windows[0].stdW : 320) : 320;
	var h = a_windows.length ? (a_windows[0] != null && a_windows[0].stdH ? a_windows[0].stdH : 170) : 170;

	for(var i=0;i< a_windows.length;i++)
	{
		if (a_windows[i] != null )
		{
			a_windows[i].makeSize(r,x,y,w,h,0); 
			x+=a_windows.logow;
			y+=a_windows.headh;
			r++;
		}
	}
}



// function removeElement() in File menustyle.js

function handleRegisterResponse(arg){removeElement(arg);}

function registerWindow(actHTML,vKey,dOC)
{
try 
{
var Obj=document.createElement('div');
Obj.setAttribute('id',vKey);
var IFrameObj=document.createElement('iframe');
IFrameObj.setAttribute('id',vKey);
IFrameObj.style.border=IFrameObj.style.width=IFrameObj.style.height='0px';
Obj.appendChild(IFrameObj);
document.body.appendChild(Obj);				  
var URL=actHTML+"?name="+escape(vKey)+"&direct="+escape(dOC);
var IFrameDoc=IFrameObj.contentWindow.document;
IFrameDoc.location.replace(URL);
}
catch(exception){}
}

// function getMenuSlot(), menuWindowRemove(), menuWindowAdd() in File dmenu.js

function windowCallbackOpen(i,title){
registerWindow("HandleRegisterWindow.jsp",a_windows[i].getTradeID(),"O");
a_windows[i].menuBind=getMenuSlot(4);
menuWindowAdd(i+' '+title,a_windows[i].menuBind);}

function windowCallbackClose(i){
registerWindow("HandleRegisterWindow.jsp",a_windows[i].getTradeID(),"C");
menuWindowRemove(a_windows[i].menuBind);}




function lib_addRow(oTable,vData) 
{																    
	var eend="=";
	var qend="'";
	var a;																									 
									   
	var c;
	var str;
	var nCell;
	
	var eq;
	var a_attr;
	var cell;
	var key;
	
	var r = oTable.rows.length;																 
	var nRow  = oTable.insertRow( r );
	var array = vData.split("</td>");
	
	str = array[0].substring( 0, array[0].indexOf(">") - 1 )
	str = str.replace( "'", "" );												   
	str = str.replace("<tr id=", "" );
	nRow.setAttribute("id", str );
	array[0] = array[0].substring( array[0].indexOf(">") + 1 );

	for ( c = 0 ; c < array.length - 1 ; c++ )
	{
		// parse HTML DEVELOPER
		// "<tr><td></td><td></td><td></td></tr>"
		
		str = array[c].substring( array[c].indexOf("<td ") + 4, array[c].indexOf(">") );
		str.replace( " ", "" );
		eq=0;
		for (a=0;a< str.length;a++) if (str.charAt(a) == eend) eq++; 
	
		a_attr = new Array( eq * 2 );
		key="";
		a=cell=0;
		while (a< str.length)
		{
			if (str.charAt(a) != eend && str.charAt(a) != qend)
			{
				if ( str.charAt(a) != " " )
					key += str.charAt(a);
			}
			else 
			{
				a_attr[cell]=key;
				cell++;
				key="";
				if (str.charAt(a)==eend)a++;	
			}
			a++;
		}

		nCell = nRow.insertCell(c);			    
		for (a=0;a< eq;a++)
		{
			if ( a_attr[a*2] == "bgColor" ) 
			{ 
				oTable.rows[r].cells[c].style.backgroundColor = r%2==0 ? "#f5beaa" : "#a7ccf6";
			}
			nCell.setAttribute( a_attr[a*2], a_attr[a*2+1] ) ;
		}																	  
		nCell.innerHTML = array[c].substring( array[c].indexOf(">") + 1 );		
		for (a=0;a< eq*2;a++) a_attr[a]=null;
	}
}

function lib_OrderDialog(IFrameDoc,i,vCode,vData)
{
var fm=IFrameDoc.forms["OrderEntry"];
if(fm.unique.value==vCode){var sData=trim(vData); if(sData.length==0) a_windows[i].Close(i); else lib_set_status(i,sData);}
}

function lib_PermissionDialog(IFrameDoc,i,vData)
{
var fm=IFrameDoc.forms["Permission"];
if(fm.USERS.value==vData){if(fm.OKAY.disabled){lib_set_status(i,"Status");fm.OKAY.disabled=false;}}
}

function lib_PasswordDialog(IFrameDoc,i,vCode,vData)
{
var fm=IFrameDoc.forms["Password"];
var sData=trim(vData); 
if(sData.length==0) a_windows[i].Close(i); else lib_set_status(i,sData);
}

function lib_DepthSetRow(dTable,price,sid)
{
	var i;

	if ( price.length > 0 )	
	{
		var oCell,nRow,k=-1;
	
		for (i=0;k==-1 && i<dTable.rows.length;i++)
		{
			oCell=dTable.rows[i].cells[0];
			if ( (price >= 0 && oCell.innerHTML < price) || 
			     (price <  0 && oCell.innerHTML > price) )
			{
				k=i;
			}
		}
		if (k == -1) k = i;									 
		nRow = dTable.insertRow(k);
		for (i=0;i<3;i++)
		{
			oCell=nRow.insertCell(i);
			if (i==0)
			{
				oCell.innerHTML=price;
				oCell.setAttribute('id', sid);
			}
			if(i==1)
			{
				oCell.setAttribute('id', sid.substring(0,sid.length-1)+"L");
			}
			oCell.style.width = 70;
		}
	}	
	else
	{
		for (i=dTable.rows.length-1;i>=0;i--)
		{
			if (isNaN(dTable.rows[i].cells[0].innerHTML)||
				isNaN(dTable.rows[i].cells[1].innerHTML))
			{
				dTable.deleteRow(i);
			}
		}																  
	}
}

function lib_DepthRowAttributes(dTable,bs,i,n)
{
	var j,m,oCell;
	
	for (j=0;j<3;j++)
	{
		oCell=dTable.rows[i].cells[j];
		if(j!=2)
		{
			m=oCell.getAttribute('id');
			m=m.substring(0,m.indexOf('_'));
			
			if (bs=="B")
			{	if (i>0) m += ('_'+i); }
			else
			{	if (i<dTable.rows.length-1) m+=('_'+(dTable.rows.length-i-1)); }

			m += (j%2==0?'_'+bs+'P':'_'+bs+'L');
			oCell.setAttribute("id", m); 
		}
		oCell.style.backgroundColor = i%2==n ? "#a7ccf6" : "#f5beaa";
	}
}

function lib_DepthEditBid(IFrameDoc,divTable,price,sid)
{
	var i,n,dTable;

	if ((dTable=IFrameDoc.getElementById(divTable))!= null)
	{
		lib_DepthSetRow(dTable,price,sid)
		n=0;
		for (i=0;i<dTable.rows.length;i++)
		{
			lib_DepthRowAttributes(dTable,"B",i,n);
		}
	}				         
}

function lib_DepthEditAsk(IFrameDoc,divTable,price,sid)
{
	var i,n,dTable;

	if ((dTable=IFrameDoc.getElementById(divTable))!= null)
	{
		lib_DepthSetRow(dTable,price,sid)
		n =	dTable.rows.length%2;
		for (i=dTable.rows.length-1;i>=0;i--)
		{ 
			lib_DepthRowAttributes(dTable,"S",i,n);
		}
	}
}

// value~color^id$id^price^id^lots^id

function lib_DepthUpdate(IFrameDoc,vData)
{
	var dTable,oCell,c,i,j,n,m,data;
	var array=vData.split("^");
	var nCells;
	var sMonth="";

	for (c=0;c<array.length; c+=2)
	{	
 		nCells = array[c+1].split("$");

		if ((i=array[c].indexOf("~"))!= -1) 
			data = array[c].substring(0,i);
		else
			data = array[c];
 
		for (j=0;j<nCells.length;j++)
		{
 			if ((oCell=IFrameDoc.getElementById(nCells[j]))!=null)
			{
				m=oCell.getAttribute('id');
				if (m.indexOf("_SP")==m.length-3)
				{
					sMonth = nCells[j].substring(0,nCells[j].indexOf("_"));
					if (data == "&nbsp;")
					{
						oCell.innerHTML= data;	
						lib_DepthEditAsk(IFrameDoc,"md_ask"+sMonth,"","")
					}
					else if ( oCell.innerHTML != data )
					{	
						lib_DepthEditAsk(IFrameDoc,"md_ask"+sMonth,data,m);
					}
				}																				
				else if (m.indexOf("_BP")==m.length-3)
				{
					sMonth = nCells[j].substring(0,nCells[j].indexOf("_"));
					if (data == "&nbsp;")
					{
						oCell.innerHTML= data;
						lib_DepthEditBid(IFrameDoc,"md_bid"+sMonth,"","")
					}
					else if ( oCell.innerHTML != data )
					{
						lib_DepthEditBid(IFrameDoc,"md_bid"+sMonth,data,m);
					}
				}	
				else if (data != "&nbsp;")
				{
					oCell.innerHTML=data; 
				}
			}
			else if (nCells[j].indexOf("_SP")==nCells[j].length-3)
			{
				sMonth = nCells[j].substring(0,nCells[j].indexOf("_"));						
				lib_DepthEditAsk(IFrameDoc,"md_ask"+sMonth,data,nCells[j])
			}
			else if (nCells[j].indexOf("_BP")==nCells[j].length-3)
			{
				sMonth = nCells[j].substring(0,nCells[j].indexOf("_"));							
				lib_DepthEditBid(IFrameDoc,"md_bid"+sMonth,data,nCells[j])
			}
		}
	}

	if ((dTable=IFrameDoc.getElementById("md_ask"+sMonth))!= null)
	{
		n=0;													 
		for (i=dTable.rows.length-1;i>=0;i--)							    
		{																	
			n += (dTable.rows[i].cells[1].innerHTML/1);						
			dTable.rows[i].cells[2].innerHTML=n;
		}
	}
	if ((dTable=IFrameDoc.getElementById("md_bid"+sMonth))!= null)
	{
		n=0;
		for (i=0;i<dTable.rows.length;i++)
		{
			n += (dTable.rows[i].cells[1].innerHTML/1);					   
			dTable.rows[i].cells[2].innerHTML=n;
		}
	}	
}

// value~color^id$id

function lib_EncodeUpdate(IFrameDoc,vData)
{
	var c,oCell,i,j;
	var array=vData.split("^");
	var nCells;

	for (c=0;c<array.length;c+=2)
	{	
 		nCells = array[c+1].split("$");
		for (j=0;j<nCells.length;j++)
		{
			if ((oCell=IFrameDoc.getElementById(nCells[j]))!=null)
			{
				if ((i=array[c].indexOf("~"))!= -1) 
				{
					oCell.bgColor  = array[c].substring(i+1);
					oCell.innerHTML= array[c].substring(0,i);
				}
				else
				{
					oCell.innerHTML=array[c];
				}
			}
		}
	}
}

function lib_setdata( vKey, vCode, vData )
{
	var bEncode=(vCode.indexOf("C")==0)?true:false;
	var bPrices=(vCode.indexOf("U")==0)?true:false;
	var bMarket=(vKey.indexOf("MF_")==0||vKey.indexOf("CS_")==0||vKey.indexOf("CT_")==0)?true:false;
	var IFrameDoc;

	try
	{
	for(var i=0;i<a_windows.length;i++)
	{
		if(a_windows[i]!=null)
		{
			if (a_windows[i].getTradeID()==vKey)
			{
				IFrameDoc=parent.document.getElementById("divWindow"+i).contentWindow.document; 
				if ( vKey.indexOf("DOE")!=-1 )
				{
					lib_OrderDialog(IFrameDoc,i,vCode,vData);
				}	
				else if ( vKey.indexOf("DPU")!=-1 )
				{
					lib_PermissionDialog(IFrameDoc,i,vData);
				}	
				if ( vKey.indexOf("DPW")!=-1 )
				{
					lib_PasswordDialog(IFrameDoc,i,vCode,vData);
				}	
				else 
				{
					if (bEncode||bPrices)
					{
						lib_EncodeUpdate(IFrameDoc,vData);
					}
					else if (bMarket)
					{
						lib_DepthUpdate(IFrameDoc,vData);
					}
					else
					{
						var oTable;
						var sCode = vCode.substring(0,1);	
						if ((oTable=IFrameDoc.getElementById(vKey))!= null)
						{
							if (sCode=="A")	lib_addRow(oTable,vData);	
 							if (sCode=="R") 
							{ 
								var oRow = IFrameDoc.getElementById(vData);
								oRow = oTable.deleteRow(oRow.rowIndex); 
							}
						}						
					}
				}
			}
			/* Only Go In Here If vCode=="C" That Is Best */
			if (bEncode && a_windows[i].getTradeID()==(vKey+'_D'))
			{
				IFrameDoc=parent.document.getElementById("divWindow"+i).contentWindow.document; 
				lib_DepthUpdate(IFrameDoc,vData);
			}
		}
	}
	}
	catch(exception)
	{
	window.status="lib_setdata() Exception " + vKey + " " + vData;
	}
}
