var lastWindowID	= 0;
var isIndexPage		= ( window.location.href.indexOf( 'index.sh' ) >= 0 );
var popups			= new Object();
var dragWin			= null;
var dragWiz			= null;
var dragOffX		= 0;
var dragOffY		= 0;
var maxDragX		= 0;
var maxDragY		= 0;

function falsefunc() { return false; } // used to block cascading events in FF

function bodyMouseMove( evt )
{
	if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
		
	if ( dragWin != null )
		winDrag( evt );
	else if ( dragWiz != null )
		wizDrag( evt );
	else
		sideDrag( evt );
	
	return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function bodyMouseUp( evt )
{
	if ( dragWin == null && dragWiz == null )
	{
		sideDragEnd();
		return;
	}
	
	winDragEnd();
}

function popupClick( evt )
{
	if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
	
	//close image click
	if ( tgt.nodeName == "IMG" )
	{
		switch ( tgt.className )
		{
			case "wix":
			    var altAction = tgt.parentNode.getAttribute(  'altCloseAction' );
			    
			    if ( altAction != null && altAction != undefined && altAction != "" )
			        eval( altAction + "( '" + tgt.parentNode.getAttribute('win') + "' )" );
			    else
				    winClose( tgt.parentNode.getAttribute('win') );
				break;
			case "widu":
				winToggle( tgt.parentNode.getAttribute('win'), null );
				break;
		}
	}
}

function popupMouseDown( evt )
{
	if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
		
	//close image click
	if ( tgt.nodeName == "IMG" )
		return;
	
	winDragStart( evt );
}

function winDragStart( evt )
{
	
	//TD.TR.TBODY.TABLE.DIV.DIV.DIV
	dragWin		= tgt.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
	
	dragOffX	= Number( evt.clientX ) - Number( dragWin.style.left.replace( 'px', '' ) );
	dragOffY	= Number( evt.clientY ) - Number( dragWin.style.top.replace( 'px', '' ) );

	document.getElementById( 'wgls' ).style.zIndex = 100000; //prevent iframes from stealing mouse focus
	
	document.onmousedown	= falsefunc; // in FF this prevents cascading of events, thus disabling text selection
	document.onmousemove	= bodyMouseMove;
	document.onmouseup		= bodyMouseUp;
	
	maxDragX = document.body.scrollWidth - dragWin.scrollWidth - 4;
	maxDragY = document.body.scrollHeight - dragWin.scrollHeight - 4;
	
	if ( dragWin.setCapture )
		dragWin.setCapture();
}

function winDragEnd()
{
	dragWin = null;
	document.getElementById( 'wgls' ).style.zIndex = -1;
	
	document.onmousedown	= null; // re-enables text selection on FF
	document.onmousemove	= null;
	document.onmouseup		= null;
	
	if ( document.releaseCapture )
		document.releaseCapture();
}

function winDrag( evt )
{
	var tx = ( evt.clientX - dragOffX );
	var ty = ( evt.clientY - dragOffY );
	
	if ( tx < 0 )
		tx = 0;
	if ( ty < 0 )
		ty = 0;
	if ( tx > maxDragX )
		tx = maxDragX;
	if ( ty > maxDragY )
		ty = maxDragY;
		
	dragWin.style.left	= tx + "px";
	dragWin.style.top	= ty + "px";
}

//
//Wizard dragging
//
function wizMouseDown( evt )
{
	if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
	
	wizDragStart( evt );
}

function wizDragStart( evt )
{
    dragWiz		= top.sesMain.document.getElementById('winWizard' );
	dragOffX	= Number( evt.screenX ) - Number( dragWiz.style.left.replace( 'px', '' ) );
	dragOffY	= Number( evt.screenY ) - Number( dragWiz.style.top.replace( 'px', '' ) );

    document.onmousedown	= falsefunc; // in FF this prevents cascading of events, thus disabling text selection
	document.onmousemove	= bodyMouseMove;
	document.onmouseup		= bodyMouseUp;
	
    tgt.setCapture();
}

function wizDragEnd()
{
	dragWiz = null;
	
	document.onmousedown	= null; // re-enables text selection on FF
	document.onmousemove	= null;
	document.onmouseup		= null;
	
	if ( document.releaseCapture )
		document.releaseCapture();
}

var counter = 0;

function wizDrag( evt )
{
	var tx = ( evt.screenX - dragOffX );
	var ty = ( evt.screenY - dragOffY );
	
	if ( tx < 0 )
		tx = 0;
	if ( ty < 0 )
		ty = 0;
		
	dragWiz.style.left	= tx + "px";
	dragWiz.style.top	= ty + "px";
}

//
//Upload handling
//
var uploadCount = 0;

function winOpenUpload( dest )
{
    var maxUploads = ( top == top.sesMain ? 1 : 3 );
    
	if ( uploadCount >= maxUploads )
	{
		alert( "Maximum number of uploads already in progress. Close any completed uploads and try again." );
		return;
	}
	
	uploadCount++;
	uploadWin = winOpen( 6, 96, "c", "HTML/UploadDocument.htm?content=" + dest + sessionString, '' );
	uploadWin.isUpload = true;
	
	return uploadWin;
}

function winClose( popupID )
{
	var winToClose	= popups[popupID];
	var baseID		= popupID.replace( 'popup', '' );
	
	if ( winToClose == null )
	{
		try
		{
			var altWinControl = document.getElementById( 'wb' + baseID ).parentNode;
			altWinControl.parentNode.removeChild( altWinControl );
			//altWinControl.contentWindow.close();
		}
		catch ( e ) {}
		return;
	}
		
	if ( winToClose.isUpload && uploadCount > 0 )
		--uploadCount;
	
	try
	{
		winToClose.winIFrame.contentWindow.location.href = "about:blank";
		winToClose.winIFrame.contentWindow.close();
	}
	catch ( e ) {}
	
	try
	{
		winToClose.winControl.parentNode.removeChild( winToClose.winControl );
	}
	catch ( e ) 
	{
		var altWinControl = document.getElementById( 'wb' + baseID ).parentNode;
		altWinControl.parentNode.removeChild( altWinControl );
	}
	
	popups[popupID] = null;
}

function setWinAnimation( popupID, filename )
{
	var base		= popupID.replace( 'popup', '' );
	var wlc			= document.getElementById( 'wlc' + base );
	wlc.innerHTML	= "<img src=Images/Anim/" + filename + ">";
}

function clearWinAnimation( popupID )
{
	var base		= popupID.replace( 'popup', '' );
	var wlc			= document.getElementById( 'wlc' + base );
	wlc.innerHTML	= "";
}

function winToggle( popupID, getSmall )
{
	var base	= popupID.replace( 'popup', '' );
	var wb		= document.getElementById( 'wb' + base );
	var wl		= document.getElementById( 'wl' + base );
	
	if ( getSmall == null )
		getSmall = ( wb.style.display == 'inline' );
		
	if ( getSmall == ( wb.style.display != 'inline' ) )
		return;
		
	if( getSmall ) //if currently normal size
	{
		var wbWidth			= wb.offsetWidth;
		wb.style.display	= 'none';
		wl.style.display	= 'inline';
		
		wl.parentNode.style.left = parseInt( wl.parentNode.style.left, 10 ) + ( wbWidth - 54 );
	}
	else
	{
		wb.style.visibility	= 'hidden';
		wb.style.display	= 'inline';
		wl.style.display	= 'none';
		var wbWidth			= wb.offsetWidth;
		
		var leftPos = parseInt( wl.parentNode.style.left, 10 ) - ( wbWidth - 54 );
		
		if ( leftPos < 0 )
			leftPos = 0;
			
		wb.parentNode.style.left = leftPos;
		
		var winHeight;

		if ( isIndexPage )
		{
			var mainWin		= document.getElementById( 'sesMain' );
			winHeight		= mainWin.offsetHeight + 91;
		}
		else
			winHeight		= document.body.scrollHeight;
		
		maxDragY = winHeight -wb.offsetHeight - 18;
		
		if ( maxDragY < 0 )
			maxDragY = 0;
			
		if ( parseInt( wb.parentNode.style.top, 10 ) > maxDragY )
			wb.parentNode.style.top = maxDragY + 'px';
			
		wb.style.visibility = 'visible';
	}
}

//
//Window handling
//
function getOuterDivFromIframeWin( iframeWin )
{
	var iframe = document.getElementById( iframeWin.name );
	
	//DIV.DIV.TD.TR.TBODY.TABLE.DIV
	var div = iframe.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling;
	
	return div.parentNode.parentNode; //outerDiv
}

//Window class definition
function cWin( winControl, winIFrame, isUpload )
{
	this.winControl		= winControl;
	this.winIFrame		= winIFrame;
	this.isUpload		= false;
	this.isHidden		= false;
}

function winOpenCenter( dest )
{
	return winOpen( 6, 96, "c", "r.sh?content=" + dest + sessionString, '' );
}

function winOpenAt( openCode, dest )
{
	return winOpen( 6, 96, openCode, "r.sh?content=" + dest + sessionString, '' );
}

function winOpenAtAltClose( openCode, dest, altCloseAction )
{
	return winOpen( 6, 96, openCode, "r.sh?content=" + dest + sessionString, altCloseAction );
}

function winOpen( x, y, openCode, dest, altCloseAction )
{
	var cWin = null;
	
	if ( isIndexPage || top == window )
		cWin = winOpenInternal( x, y, openCode, dest, altCloseAction );
	else
		cWin = top.winOpenInternal( x, y, openCode, dest, altCloseAction );
	
	if ( !isIndexPage )
	{
		nephews.push( cWin.winIFrame.name )
		cWin.winIFrame.setAttribute( 'uncle', window.name );
	}
	
	return cWin;
}

function winOpenInternal( x, y, openCode, dest, altCloseAction )
{
	var newWin;
	var style = "z-index:60000;position:absolute;top:" +
		y + ";left:" + x + ";";
	
	if ( window.createPopup ) //IE
		newWin = document.createElement( "<div style=\"" + style + "\">" );
	else //FF
	{
		newWin = document.createElement( "div" );
		newWin.setAttribute( "style", style );
	}
	
	lastWindowID++;
	
	newWin.innerHTML = 
		//the main window
		"<div id=wb" + lastWindowID + " style=display:inline>" +
		( window.createPopup ? //IE
		"<iframe frameborder=0 width=0 height=0 src=\"javascript:false;\" style=\"position:absolute;z-index:-1;\"></iframe>"
		: "" ) +  //FF
		"<div style=\"background-color:#495468;padding:1px;\">" +
			"<table cellpadding=0 cellspacing=0>" +
				"<tr height=15>" +
					"<td background=Images/FW/wiba.jpg nowrap align=right style=\"padding:1;cursor:hand;cursor:pointer;\" onclick=\"popupClick(event);\" onmousedown=\"popupMouseDown(event);\" altCloseAction=\"" + altCloseAction + "\" win=popup" + lastWindowID + "><img class=widu src=\"Images/FW/wid.gif\"><img class=wix src=\"Images/FW/wix.gif\"></td>" +
				"</tr>" +
				"<tr>" +
					"<td valign=top>" +
						"<div style=\"background-color:#CEC7B5;padding:0 1 1 1;\">" +
							"<div style=\"background-color:#293448;padding:1px 1px 1px 1px;\">" +
								"<iframe style=\"background-color:white;color:white;\" uncle='' openCode=" + openCode + " onfocus=\"childFocus();\" width=50 height=50 frameborder=0 src=\"" + dest + "\" id=popup" + lastWindowID + " name=popup" + lastWindowID + ">" +
								"</iframe>" +
							"</div>" +
						"</div>" +
					"</td>" +
				"</tr>" +
			"</table>" +
		"</div>" +
		"<div style=\"z-index:-1;position:absolute;left:10;top:10;width:46;height:52;background-color:#111133;filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;\">" +
		"</div>" +
		"</div>" +
		//the minimized window
		"<div id=wl" + lastWindowID + " style=\"display:none;\">" +
		( window.createPopup ? //IE
		"<iframe frameborder=0 width=0 height=0 src=\"javascript:false;\" style=\"position:absolute;z-index:-1;\"></iframe>"
		: "" ) +  //FF
		"<div style=\"background-color:#495468;padding:1px;\">" +
			"<table cellpadding=0 cellspacing=0>" +
				"<tr height=15>" +
					"<td background=Images/FW/wiba.jpg nowrap align=right style=\"padding:1;cursor:hand;cursor:pointer;\" onclick=\"popupClick(event);\" onmousedown=\"popupMouseDown(event);\" altCloseAction=\"" + altCloseAction + "\" win=popup" + lastWindowID + "><img class=widu src=\"Images/FW/wiu.gif\"><img class=wix src=\"Images/FW/wix.gif\"></td>" +
				"</tr>" +
				"<tr>" +
					"<td valign=top>" +
						"<div style=\"background-color:#CEC7B5;padding:0 1 1 1;\">" +
							"<div style=\"background-color:#293448;padding:1px 1px 1px 1px;\">" +
								"<div id=wlc" + lastWindowID + " style=\"background-color:white;width:50px;height:30px;text-align:center;padding-top:1px;\"></div>" +
							"</div>" +
						"</div>" +
					"</td>" +
				"</tr>" +
			"</table>" +
		"</div>" +
		"</div>";
	
	if ( isIndexPage )
		document.getElementById('toolcenter').appendChild( newWin );
	else
		document.body.appendChild( newWin );
	
	var newWinC = new cWin( newWin, document.getElementById( 'popup' + lastWindowID ) );
	popups['popup' + lastWindowID] = newWinC;
	
	return newWinC;
}

function popupLoad( iframeWin )
{
    if ( top != window )
	{
		top.popupLoad(window);
		window.focus();
		return;
	}
	
	if ( iframeWin == undefined )
	    return;
	
	var iframe = document.getElementById( iframeWin.name );
	
	//DIV.DIV.TD.TR.TBODY.TABLE.DIV
	var div = iframe.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling;
	
	if ( window.createPopup )
	{    
	    iframe.width	= iframe.contentWindow.document.body.scrollWidth;
	    iframe.height	= iframe.contentWindow.document.body.scrollHeight;
	}
	else
	{
	    iframe.width	= iframe.contentWindow.document.body.scrollWidth;
	    iframe.height	= iframe.contentWindow.document.height;
	}
	
	//if ( window.createPopup ) //causes page to load white in FF
		iframe.setAttribute( 'scrolling', 'no' );
	
	var outerDiv	= div.parentNode.parentNode;
	var outerIFrame = outerDiv.childNodes[0].childNodes[0];

	outerIFrame.width	= outerDiv.offsetWidth;
	outerIFrame.height	= outerDiv.offsetHeight;
	
	div.style.width		= ( Number( iframe.width ) ) + 'px';
	div.style.height	= ( Number( iframe.height ) + 15 ) + 'px';
	
	sendWindowToPosition( outerDiv, iframe.getAttribute( "openCode" ) );
	iframe.setAttribute( "openCode", "" );
}

function sendWindowToPosition( outerDiv, positionCode )
{
	var winWidth;
	var winHeight;
	
	if ( isIndexPage )
	{
		var mainWin		= document.getElementById( 'sesMain' );
		winWidth		= mainWin.offsetWidth + ( divSidebar == null ? 0 : divSidebarUn.offsetWidth );
		winHeight		= mainWin.offsetHeight + 91;
	}
	else
	{
		winWidth		= document.body.scrollWidth;
		winHeight		= document.body.scrollHeight;
	}
			
	switch ( positionCode )
	{
		case "nw" :
			break;
		case "c" :
			outerDiv.style.top	= ( ( winHeight - outerDiv.offsetHeight ) / 2 ) + 'px';
			outerDiv.style.left = ( ( winWidth - outerDiv.offsetWidth ) / 2 ) + 'px';
			break;
		case "ne" :
			outerDiv.style.left = ( winWidth - outerDiv.offsetWidth - 21 ) + 'px';
			break;
		case "sw" :
			outerDiv.style.top	= ( winHeight - outerDiv.offsetHeight - 21 ) + 'px';
			break;
		case "se" :
			outerDiv.style.top	= ( winHeight - outerDiv.offsetHeight - 21 ) + 'px';
			outerDiv.style.left = ( winWidth - outerDiv.offsetWidth - 21 ) + 'px';
			break;
		default :
			break;
	}
	
	if ( outerDiv.offsetTop < 0 )
	    outerDiv.style.top = "1px";
	if ( outerDiv.offsetLeft < 0 )
	    outerDiv.style.left = "1px";
}

function popupUnload( iframeWin )
{
	if ( top != window )
	{
		top.popupUnload(window);
		return;
	}
	
	try
	{
	    var iframe = document.getElementById( iframeWin.name );

	    //DIV.DIV.TD.TR.TBODY.TABLE.DIV
	    var div = iframe.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling;
    	
	    var outerDiv	= div.parentNode.parentNode;
	    var outerIFrame = outerDiv.childNodes[0].childNodes[0];

	    div.style.width		= 50
	    div.style.height	= 50;
	    outerIFrame.width	= 56;
	    outerIFrame.height	= 70;
    	
	    iframe.width	= 50;
	    iframe.height	= 50;
    	
	    iframe.setAttribute( 'scrolling', 'yes' );
	}
	catch ( err ) {}
}

function sizePopupForError( iframeWin )
{
	var iframe = document.getElementById( iframeWin.name );
	
	//DIV.DIV.TD.TR.TBODY.TABLE.DIV
	var div = iframe.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling;
	
	iframe.width	= 500;
	iframe.height	= 300;
	
	iframe.setAttribute( 'scrolling', 'yes' );
	
	var outerDiv	= div.parentNode.parentNode;
	var outerIFrame = outerDiv.childNodes[0].childNodes[0];

	outerIFrame.width	= outerDiv.offsetWidth;
	outerIFrame.height	= outerDiv.offsetHeight;
	
	div.style.width		= ( Number( iframe.width ) ) + 'px';
	div.style.height	= ( Number( iframe.height ) + 15 ) + 'px';

	sendWindowToPosition( outerDiv, 'c' );
}