var nephews = new Array();

var isRunningStartaOptimized = ( window.external != undefined && 
    window.external.RunningStartaOptimized == true );
    
function getEventTarget( evt )
{
	if ( evt == undefined )
		return null;
	if ( evt.srcElement != null )
		return evt.srcElement;
	else
		return ( evt.target.nodeType == 3 ? evt.target.parentNode : evt.target );
}

function documentLoad()
{
	if ( message == 'True' )
		window.setTimeout( 'messageDisplay()', 200 );
    if ( top == window )
        top.sesMain = window;
	
	//this method works for fixing the frozen popup window error, but is less ideal than the
	//method which has been added to popupLoad. Uncomment this if the problem resurfaces. ~*~
	//if ( window.name.indexOf( 'popup' ) == 0 )
	//    window.setTimeout( 'window.focus()', 1000 );
	
	if ( window.name.indexOf( 'sesMain' ) == 0 )
	    top.closePopupCalendar();
	    
	if ( top == window ) //if in standalone window (no header frame), run pings here
	    window.setTimeout( 'checkMessages()', 120000 );
}

focusOnLoad = true;

function bodyLoad()
{
    if ( window.name.indexOf( 'sesMain' ) == 0 && focusOnLoad )
        window.focus();
}

function setPCC( count ) //setPersonCacheCount
{
    if ( top.setPersonCacheCount ) //this is in header.js only
        top.setPersonCacheCount( count );
}

function messageEvent()
{
    try
    {
        if ( this.getNodeValue( 'HasMessage' ) == 'true' )
            window.setTimeout( 'messageDisplay()', 200 );
        
        var cacheCount = Number( this.getNodeValue( 'PersonCacheCount' ) );
        setPCC( cacheCount );
    }
    catch ( e ){}
}

function checkMessages()
{
    if ( sessionString == "&aid=&sid=&cid=" )
        return;
    goAjax( 'Navigation/MessageChecker', messageEvent, true );
    window.setTimeout( 'checkMessages()', 120000 );
}

function pageUnload()
{
	closeNephews();
	
	if ( window.name.indexOf( "popup" ) == 0 )
		popupUnload();
	else if ( window.name == 'winWizard' )
		wizardUnload();
}

function scrollPageLoad()
{
	document.body.scrollTop		= scrY;
	document.body.scrollLeft	= scrX;
}

function navigate( location )
{	
	if ( location.indexOf( 'http://' ) != -1 )
	{
		window.open( location );
	}
	else
	{
		window.location.href = location.toString();
	}
}

function messageDisplay()
{
	top.winOpenCenter( "Navigation/Message&" + Math.random() +"=" + Math.random() );
}

var htmlSource;
var lastScript = '';

//shortcuts
document.onkeydown = function ( evt )
{
    if ( evt == null )
        evt = event;
    
    if ( testKeyCode( evt, 120 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F9
	{
		if ( document.getElementById( "ActionFrame" ) != null && document.getElementById( "ActionFrame" ) != undefined )
		{
			if ( document.getElementById( "ActionFrame" ).style.display == "none" )
				document.getElementById( "ActionFrame" ).style.display = "inline";
			else
				document.getElementById( "ActionFrame" ).style.display = "none";
		}
		else
			alert( "No debug windows available for this screen" );
	}
	else if ( testKeyCode( evt, 121 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F10
	{
		htmlSource	    = "";
		var length		= document.childNodes.length;
		var srcElement  = "";
		
		for ( var i = 0; i < length; i++ )
		{
		    srcElement = document.childNodes[i].innerHTML;
		    
		    if ( srcElement != undefined && srcElement != "undefined" )
			    htmlSource += srcElement;
		}
		
		winOpenCenter( 'Navigation/SourceView' );
	}
	else if ( testKeyCode( evt, 122 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F11
	{
		if ( document.getElementById('nevent') == null )
			alert( "No form rendered." );
		else
		{
			document.getElementById('nevent').value = "displayform";
			document.Form.submit();
		}
	}
	else if ( testKeyCode( evt, 119 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F8
		alert ( window.location.href );
	else if ( testKeyCode( evt, 118 )  ) // F7
	{
	    if ( evt.altKey && evt.ctrlKey ) // Control + Alt +  F7
	    {
		    var curScript = prompt( "Your Entry?", lastScript );
    		
		    if ( curScript != null )
		    {
			    lastScript = curScript;
			    eval( lastScript );
		    }
		}
		
		cancelKeyEvent( evt ); //don't allow carat to be turned on in FF
	}
	else if ( testKeyCode( evt, 116 ) || //F5
	    testKeyCode( evt, 82 ) && evt.ctrlKey )  //Ctrl-R
	{
	    cancelKeyEvent( evt ); //don't allow refresh by F5 or Ctrl-R
		window.location.href = window.location.href; //refresh the current page only
		return false;
	}
		
	if ( testKeyCode( evt, 122 ) ) //F11
	{
		cancelKeyEvent( evt ); //don't allow maximization by F11
		return false;
	}
	
	if ( testKeyCode( evt, 87 ) && evt.ctrlKey )  //Ctrl-W
	{
	    cancelKeyEvent( evt ); //don't allow window to be closed by Ctrl-W
	    return false;
	}
	
	if ( testKeyCode( evt, 69 ) && evt.ctrlKey )  //Ctrl-E
	{
	    cancelKeyEvent( evt ); //don't allow web search to open
	    try
	    {
	        top.search();
	    }   
	    catch ( e ) {}
	    return false;
	}
	
	if ( testKeyCode( evt, 112 ) ) //F1
	{
	    cancelKeyEvent( evt ); //don't allow default help popup by F1
	    
	    try
	    {
	        top.help();
	    }
	    catch ( e )
	    {
	        winOpenCenter( 'Global/Help' );
	    }
		return false;
	}
	else if ( testKeyCode( evt, 113 ) ) //F2
	{
	    cancelKeyEvent( evt );
	    
	    var curName = prompt( "Please Enter A New Window Title", top.document.title );
    		
	    if ( curName != null )
	    {
		    top.document.title = curName;
	    }
		return false;
	}
}

//
//START Cookies
//
function setCookie( name, value )
{
	document.cookie = name + "=" + value + "; expires=Thu, 7 Mar 2075 22:14:55 UTC; path=/";
}

function getCookie( name )
{
	var nameEQ	= name + "=";
	var ca		= document.cookie.split( ';' );
	
	for( var i = 0; i < ca.length; i++ )
	{
		var c = ca[i];
		
		//trim out any white space
		while ( c.charAt(0) == ' ' ) 
			c = c.substring( 1, c.length );
		
		if ( c.indexOf( nameEQ ) == 0 ) 
			return c.substring( nameEQ.length, c.length );
	}
	
	return null;
}
//
//END Cookies
//

function reloadWindow( winToReload, queryAdd, queryRemove )
{
	winToReload.location.href = winToReload.location.href.toString().replace( 
		queryRemove, '' ) + queryAdd;
}

function fireDeleteEvent( winForConfirm, ConfirmationQuestion )
{
	if ( winForConfirm.confirm( ConfirmationQuestion ) )
	{
		document.getElementById( "nevent" ).value = "delete";
		document.Form.submit();
	}
}

function fireConfirmedEvent( winForConfirm, ConfirmationQuestion, nevent )
{
	if ( winForConfirm.confirm( ConfirmationQuestion ) )
	{
		document.getElementById( "nevent" ).value = nevent;
		document.Form.submit();
	}
}

function fireNEvent( nevent )
{
	document.getElementById( "nevent" ).value = nevent;
	document.Form.submit();
}

function goC( location ) 
{ 
	showWait(); 
	window.location.href = "r.sh?content=" + location + sessionString; 
}

function goP( location ) 
{ 
	showWait(); 
	window.location.href = "r.sh?content=" + location + sessionString;
}

function goQ( location ) 
{ 
	showWait(); 
	window.location.href = "r.sh?content=" + location + sessionString + queryStub; 
}

function goAsync( location, doShowWait ) 
{ 
	if ( doShowWait != null && doShowWait ) 
		showWait(); 
	
	document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
		"r.sh?content=" + location + sessionString;
}

function goR( location ) 
{
    showWait(); 
	window.location.href = "r.sh?content=" + location + getQueryString();
}

function goAsyncR( location, doShowWait ) 
{ 
	if ( doShowWait != null && doShowWait ) 
		showWait(); 
	
	document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
		"r.sh?content=" + location + getQueryString();
}

function getQueryString() 
{
    var url = window.location.href;
    url     = url.substring( url.indexOf( '?content=' ) + '?content='.length );
    url     = url.substring( url.indexOf( '&' ) );
    
    return url;
}

function goMasterAsync( location, doShowWait ) 
{ 
	if ( doShowWait != null && doShowWait ) 
		showWait(); 
	
	top.document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
		"r.sh?content=" + location + sessionString;
}

function bodyMouseDown( evt, allHoversIncluded )
{
	if ( top != window && top.bodyMouseDown )
		top.bodyMouseDown( evt, allHoversIncluded );
		
	if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
		
	if ( tgt.nodeType != 1 ) //SELECT
		window.focus(); //prevents occasional lock-up in popup windows
	
	if ( top != window && top.closePopupCalendar )
	    top.closePopupCalendar();
	
	if ( taPop != null )
	{
	    if ( taCLast )
            taCLast = false;
	    else if ( tgt.nodeName != "TEXTAREA" && tgt != taPop && tgt.parentNode != taPop && tgt != lastTA )
            taPop.style.display		= "none";
    }
}

function closePopup()
{
	if ( top != window && top.winClose )
	{
		var popupID = window.name;
		
		if ( popupID.indexOf( 'popup' ) >= 0 )
			top.winClose( popupID );
	}
}

function closeNephews()
{
	var iter = nephews.length;
	var curNephew;
	
	while ( iter-- )
	{
		curNephew = top.popups[nephews[iter]];
		
		try
		{
			if ( curNephew != null )
				curNephew.winIFrame.contentWindow.closeNephews();
		}
		catch ( e ) {}
		
		try
		{	
			top.winClose( nephews[iter] );
		}
		catch ( e ) {}
	}
}

function hideNephews()
{
	var iter = nephews.length;
	var curNephew;
	
	while ( iter-- )
	{
		curNephew = top.popups[nephews[iter]];
		
		if ( curNephew == null )
		    continue;
		    
		try
		{
			if ( curNephew != null )
				curNephew.winIFrame.contentWindow.hideNephews();
		}
		catch ( e ) {}
		
		curNephew.isHidden = true;
		curNephew.winControl.style.display = 'none';
	}
}

function showNephews()
{
	var iter = nephews.length;
	var curNephew;
	
	while ( iter-- )
	{
		curNephew = top.popups[nephews[iter]];
		
		if ( curNephew == null )
		    continue;
		    
		try
		{
			if ( curNephew != null )
				curNephew.winIFrame.contentWindow.showNephews();
		}
		catch ( e ) {}
		
		curNephew.isHidden = false;
		curNephew.winControl.style.display = 'inline';
	}
}

function getUncle()
{
	if ( window.name == "sesMain" )
		return window;
	else
	{
		var uncleName = top.document.getElementById( window.name ).getAttribute( "uncle" );
		
		if ( uncleName == "sesMain" )
			return top.sesMain;
		else
			return top.popups[uncleName].winIFrame.contentWindow;
	}
}

function ueBut( entityIndex, buttonSize, headerText, labelText ) //Update Entity Button
{
	if ( top != window && top.ueBut )
		top.ueBut( entityIndex, buttonSize, headerText, labelText );
}

function heButs() //Hide Entity Buttons
{
	if ( top != window && top.heButs )
		top.heButs();
}

function ueSB( entityIndex, dest, selID ) //Update Entity Sidebar
{
	if ( top != window && top.ueSB )
		top.ueSB( entityIndex, dest, selID );
}

function umSB( selID, changeTo ) //Update Main Sidebar
{
	if ( top != window && top.umSB )
		top.umSB( selID, changeTo );
}

//
//Wizards
//
function wizOpen( dest )
{
	dest		= "r.sh?content=" + dest + sessionString;
	var oldWin	= document.getElementById( 'winWizard' );
	
	if ( oldWin != null )
	{
		oldWin.src = dest;
		return;
	}
	
	var newWin;
	var style = "z-index:90000;position:absolute;top:45;left:15;width:49px;height:50px;visibility:hidden;";
	
	if ( window.createPopup ) //IE
		newWin = document.createElement( "<iframe id=winWizard name=winWizard style=\"" + style + 
			"\" onfocus=\"childFocus();\" frameborder=0 src=\"" + dest + "\" scrolling=no>" );
	else //FF
	{
		newWin = document.createElement( "iframe" );
		newWin.setAttribute( "style", style );
		newWin.setAttribute( 'onfocus', 'childFocus();' );
		newWin.setAttribute( 'frameborder', 0 );
		newWin.setAttribute( 'id', 'winWizard' );
		newWin.setAttribute( 'name', 'winWizard' );
		newWin.setAttribute( 'scrolling', 'no' );
		newWin.setAttribute( 'src', dest );
	}
	
	document.body.appendChild( newWin );
	
	if ( newWin.contentWindow.location.href != dest )
		newWin.contentWindow.location = dest;
}

function wizardLoad()
{
	if ( window.name != "sesMain" )
	{
		if ( top.sesMain != null && top.sesMain.wizardLoad != undefined )
			top.sesMain.wizardLoad();
		return;
	}
	
	var winWizard = document.getElementById('winWizard');    
	
	if ( window.createPopup )
	{    
	    winWizard.style.width		= winWizard.contentWindow.document.body.scrollWidth + 'px';
	    winWizard.style.height		= winWizard.contentWindow.document.body.scrollHeight + 'px';
	}
	else
	{
	    winWizard.style.width		= winWizard.contentWindow.document.body.scrollWidth + 'px';
	    winWizard.style.height		= winWizard.contentWindow.document.height + 'px';
	}
	winWizard.style.top			= ( document.body.scrollTop + 45 ) + 'px';
	winWizard.style.left		= ( document.body.scrollLeft + 15 ) + 'px';
	winWizard.style.visibility	= "visible";
	winWizard.style.zIndex	    = "100000";
}

function wizardUnload()
{
	if ( window.name != "sesMain" )
	{
		if ( top.sesMain != null && top.sesMain.wizardUnload != undefined )
			top.sesMain.wizardUnload();
		return;
	}
	var winWizard				= document.getElementById('winWizard');
	
	winWizard.style.zIndex	    = "-4";
	winWizard.style.width		= "49px";
	winWizard.style.height		= "50px";
}

function hideWizard()
{
	if ( window.name != "sesMain" )
	{
		if ( top.sesMain != null && top.sesMain.hideWizard != undefined )
			top.sesMain.hideWizard();
		return;
	}
	
	var winWizard				= document.getElementById('winWizard');
	winWizard.style.visibility	= "hidden";
}

function wizardFinished()
{
	if ( window.name != "sesMain" && top.sesMain != null )
		top.sesMain.location.href = top.sesMain.location.href;
}

function wizardFinishedRemove( queryRemove )
{
	if ( window.name != "sesMain" && top.sesMain != null )
		top.sesMain.location.href = top.sesMain.location.href.toString().replace( queryRemove, '' );
}

function wizardFinishedAdd( queryAdd )
{
	if ( window.name != "sesMain" && top.sesMain != null )
		top.sesMain.location.href = top.sesMain.location.href + queryAdd;
}

function childFocus()
{
	if ( top != window && top.childFocus )
		top.childFocus( true );
}

function sizeWizardForError()
{
	var winWizard				= document.getElementById('winWizard');
	winWizard.style.width		= "500px";
	winWizard.style.height		= "300px";
	winWizard.style.top			= ( document.body.scrollTop + 45 ) + 'px';
	winWizard.style.left		= ( document.body.scrollLeft + 15 ) + 'px';
	winWizard.style.visibility	= "visible";
}

function doAct(){} //keeps pages from refreshing to root after errors

var loadFails = new Array();

function richEditorLoad( id )
{
    try
    {
        var richWin     = document.getElementById('rt_'+id).contentWindow;
        var richContent = richWin.document.getElementById('content' );
        var richHTML    = document.getElementById(id).value;
        
        richWin.document.body.style.margin  = "0 0 0 0";
        richWin.document.body.innerHTML     = richHTML;
    }
    catch ( e )
    {
        if ( loadFails[ id ] == undefined )
            loadFails[ id ] = 1;
        else
        {
            var failCount   = ( Number( loadFails[ id ] ) + 1 );
            loadFails[ id ] = failCount;
            
            if ( failCount > 10 )
                return;
        }
        
        window.setTimeout( "richEditorLoad( '" + id + "');" );
    }
}

var lastRichID = "";
function showRichEditor( richID )
{
    lastRichID          = richID;
    var editorIFRAME    = top.document.getElementById( 'sesEditor' );
    top.setEditTarget( window );
    
    if ( editorIFRAME.src != 'HTML/Editor.htm' )
        editorIFRAME.src = 'HTML/Editor.htm';
    else
    {
        try
        {
            top.sesEditor.setRichText();
        }
        catch ( e )
        {
            editorIFRAME.src = 'HTML/Editor.htm';
        }
    }
    
    top.document.getElementById( 'sesMain' ).style.display  = 'none';
    top.sesMain.hideNephews();
    editorIFRAME.style.display                              = 'inline';
}


function getRichText()
{
    return document.getElementById( lastRichID ).value;
}

function getRichTextTags()
{
    return document.getElementById( lastRichID ).getAttribute('tags');
}

function setRichText( text )
{
    document.getElementById( lastRichID ).value = text;
    richEditorLoad( lastRichID );
}

window.onbeforeunload = function ( evt )
{
    if ( window.name.indexOf('popup' ) < 0 && window.name != 'sesMain' )
        return;
        
    var sesEditor = top.document.getElementById('sesEditor');
    
    if ( sesEditor != null )
    {
        if ( sesEditor.style.display == "inline" )
        {
            var retVal = "WARNING: You will be unable to keep your changes if you click OK.";
            
            if ( window.showModelessDialog )
                event.returnValue = retVal;
            else
                return retVal;
        }
    }
}

onL();
function onL()
{
    if ( window.name.indexOf('popup' ) < 0 && window.name != 'sesMain' )
        return;
       
    var sesEditor = top.document.getElementById('sesEditor');
    
    if ( sesEditor != null )
    {
        if ( sesEditor.style.display == "inline" )
        {
            top.document.getElementById( 'sesMain' ).style.display      = 'inline';
            top.document.getElementById( 'sesEditor' ).style.display    = 'none';
            top.sesMain.showNephews();
            top.document.getElementById( 'sesMain' ).focus();
        }
    }
}

function clickElement( element )
{
    if ( window.createPopup )
        element.click();
    else
    {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);
        element.dispatchEvent(evt);
    }
}

function testKeyCode( evt, intKeyCode )
{
    if ( window.createPopup )
        return evt.keyCode == intKeyCode;
    else
        return evt.which == intKeyCode;
}

function cancelKeyEvent( evt )
{
    if ( window.createPopup )
        evt.keyCode = 0;
    else
	    evt.preventDefault();
}