function login( loginArguments )
{
	if ( loginArguments == undefined )
		loginArguments = "";
	
	if ( LUID != undefined && LOID != undefined && Number( LUID ) != -3 && Number( LOID ) != -3 )
		loginArguments = loginArguments + "&luid=" + LUID + "&loid=" + LOID;
		
	if ( willCallURL != undefined && willCallURL != "" )
	    loginArguments = loginArguments + "&willcall=" + willCallURL;
		
	sesMain.location.href = 'r.sh?content=Navigation/Login' + loginArguments + sessionString;
}

function back()
{
    if ( !editorCheck() )
        return;
        
	sesMain.history.back();
}

function forward()
{
    if ( !editorCheck() )
        return;
        
	sesMain.history.forward();
}

function stop()
{
    if ( !editorCheck() )
        return;
        
	sesMain.document.execCommand('Stop');
}

function refresh()
{
    if ( !editorCheck() )
        return;
        
    if ( sesMain.startingURL != null && sesMain.startingURL != undefined )
	    sesMain.location.href = sesMain.startingURL;
	else
	    sesMain.location.href = sesMain.location.href;
}

function home()
{
	sesMain.location.href = 'r.sh?content=Person/ViewStartPage' + sessionString;
}

function exportContent()
{
    sesMain.document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
        sesMain.location.href.toString() + '&rexm54=ok';
}

function goToPersonCache( personID, organizationID )
{
    sesMain.goP( 'Person/EditAttachments&personid=' + personID + 
        '&organizationid=' + organizationID + '&mo=0' );
}

function setPersonCacheCount( count )
{
    var cacheSpan = document.getElementById('personCache');
    
    if ( cacheSpan == null || cacheSpan == undefined )
        return;
    else
        cacheSpan.innerHTML = count;
}

function editorCheck()
{
    if ( document.getElementById( 'sesMain' ).style.display == 'none' )
    {
        alert( "This button cannot be used while the rich text editor is open." );
        return false;
    }
    
    return true;
}

var searchWin = null;

function search()
{
    if ( searchWin == null || searchWin == undefined )
	    searchWin = winOpenAtAltClose( 'c', 'Navigation/Search', 'hideSearch' );
	else
	    searchWin.winControl.style.display = 'inline';

	try 
	{
	    searchWin.winIFrame.contentWindow.document.getElementById('txtCriteria').select();
	    searchWin.winIFrame.contentWindow.document.getElementById('txtCriteria').focus();
	}
	catch (e) { }
}

function hideSearch( popupID )
{
    searchWin.winControl.style.display = 'none';
}

function print()
{
    if ( document.getElementById( 'sesMain' ).style.display == 'none' )
    {
        sesEditor.idContenteditor.focus();
	    sesEditor.idContenteditor.print();
    }
    else
    {
	    sesMain.focus();
	    sesMain.print();
	}
}

function favorites()
{
	changeToSidebarNoButton( 'Fav', 'Favorites', false );
}

function addFav()
{
	winOpenCenter( 'Navigation/AddFavorite' );
}

function manFav()
{
	winOpenCenter( 'Navigation/ManageFavorites' );
}

function help()
{
	winOpenCenter( 'Global/Help' );
}

function toggleShowing()
{
	var sBI = document.getElementById( 'sb' + lastSidebarID );
	
	if ( sBI == null )
		return;
	
	sBI.contentWindow.location.href = sBI.contentWindow.location.href + '&toggleshowing=1';
	
	//alert is in Sidebar.js
}

function rfMyAllToggle()
{
    var imgObj  = document.getElementById('imgMyAll');
    var imgSrc  = imgObj.getAttribute('src').replace( /\n/g, '' ).replace( /\r/g, '' );
	
	if ( imgSrc.indexOf( 'Images/FW/ba_my.gif' ) >= 0 )
	{
	    imgObj.setAttribute( 'src', 'Images/FW/ba_all.gif' );
	    goAsync( 'Person/ChangeMyMode' );
	    alert( 'You will now see all Projects you have access to.' );
	}
	else
	{
	    imgObj.setAttribute( 'src', 'Images/FW/ba_my.gif' );
	    goAsync( 'Person/ChangeMyMode&mymode=1' );
	    alert( 'You will no longer see Projects that you only have access to via administrative/superPerson rights.' );
	}
}

function rfSecurity()
{
	var sBI = document.getElementById( 'sb' + lastSidebarID );
	
	if ( sBI == null )
		return;
	
	sBI.contentWindow.location.href = sBI.contentWindow.location.href + '&refsec=1';
	
	alert( "Your Security Has Been Refreshed!\n\nYou may need to manually reload other sidebar menus and pages in order for these changes to be fully visible.\n(see the refresh button at the top of the menu, and on the naviation bar)" );
}

function openNewWin()
{
    var mainURL = top.sesMain.location.href;
    mainURL     = mainURL.substring( mainURL.indexOf( '?' ) );
    mainURL     = mainURL.replace( sessionString, '' );
    mainURL     = mainURL.replace( '?content=', '' );
    mainURL     = mainURL.replace( /&/g, '~' );
    
    goAsync( 'Global/OpenNewWindow&target=' + mainURL );
}

function openFavoriteInNewWin( favURL, windowName, portalID )
{
    favURL     = favURL.replace( /&/g, '~' );
    
    goAsync( 'Global/OpenNewWindow&target=' + favURL + '&winName=' + windowName + '&portalID=' + portalID );
}

//
//Start Zoom
//
function setZoom( size )
{
	document.getElementById('sesMain').style.zoom	= size;
}

function snapZoomToFit ( fitWidth )
{
	document.getElementById('sesMain').style.zoom = 1;
	
	var outsideSize = 0;
	var insideSize	= 0;
	
	if ( fitWidth )
	{
		outsideSize = document.getElementById('sesMain').scrollWidth;
		insideSize	= document.getElementById('sesMain').contentWindow.document.body.scrollWidth;
	}
	else
	{
		outsideSize = document.getElementById('sesMain').scrollHeight;
		insideSize	= document.getElementById('sesMain').contentWindow.document.body.scrollHeight;
	}
	
	document.getElementById('sesMain').style.zoom = ( outsideSize / insideSize );
}

function alterZoom ( zoomIn )
{
	var currentZoom = Number( document.getElementById('sesMain').style.zoom );
	if ( zoomIn )
	{
		if ( currentZoom != 0 )
		{
			document.getElementById('sesMain').style.zoom = currentZoom + 0.1;
			
			if ( document.getElementById('sesMain').style.zoom > 4 )
				document.getElementById('sesMain').style.zoom = 4;
		}
		else
			document.getElementById('sesMain').style.zoom = 1.1;
	}
	else
	{
		if ( currentZoom!= 0 )
		{
			document.getElementById('sesMain').style.zoom = currentZoom - 0.1;
			
			if ( document.getElementById('sesMain').style.zoom < 0.1 )
				document.getElementById('sesMain.style').zoom = 0.1;
		}
		else
			document.getElementById('sesMain.style').zoom = 0.9;
	}
}
//
//End Zoom
//

//
//Start Person Controls
//
function checkOut()
{
	if ( window.location.href.indexOf( "?" ) != -1 )
		window.location.href = window.location.href.toString() + "&action=checkout";
	else
		window.location.href = window.location.href.toString() + "?action=checkout";
}

//
//End User Controls
//

//
//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;
}

var Windows = new Array();
			
var doPageLoad = false;

var isRunningStartaOptimized = ( window.external != undefined && 
    window.external.RunningStartaOptimized == true );

function documentLoad()
{
	if ( location.search.indexOf( "opened" ) == -1 )
	{
		//prevents this window from prompting the Person about being closed.
		window.opener = window.onload = null;
		var openSelf = false;
		
		var windowName = "Starta" + windowSuffix;
		
		//if a will call in IE
		if ( hasWillCall && !isRunningStartaOptimized && window.createPopup )
		{
		    var lastWindowName  = getCookie( "lastWinName" );
		    
		    if ( lastWindowName == "" )
		        lastWindowName = "StartaWillCall";
		    
		    var methodName  = "javascript:try{parseWillCall('&content=" + willCallURL + "&luid=" +
		        LUID + "&loid=" + LOID + "&lpid=" + LPID + "');}catch(e){false;}"
		    var newWin      = window.open( methodName, lastWindowName );
           
            if ( newWin.location.href == methodName )
            {
                newWin.close();
                openSelf        = true;
                windowName      = lastWindowName;
            }
		}
		else
		    openSelf = true;
		
		if ( openSelf )
		{
		    setCookie( "lastWinName", windowName );
		    
//		    if ( window.createPopup )
		        window.open( location.href + "&opened=1", windowName,
			        "status=yes,toolbar=no,menubar=no,resizable=yes,location=no,scrollbars=no" );
//			else
//			    window.open( location.href + "&opened=1", windowName,
//			        "status=yes,toolbar=no,menubar=no,resizable=yes,location=yes,scrollbars=no" );
	    }
	    else
	        window.close();
	}
	else if ( location.search.indexOf( "sized" ) == -1 )
	{
		//close the calling window
		if ( window.opener != null )
			window.opener.close();
			
	    var width   = screen.availWidth;
	    var height  = screen.availHeight;
	    
	    if ( width / height >= 2 ) //keep a general aspect ratio of a standard screen
	        width = ( width / 2 );
		
		window.moveTo( 0,0 );
		window.resizeTo( width, height );
		window.location.replace( window.location.href + "&sized=1" );
	}
	else
		doPageLoad = true;
}

function parseWillCall( queryString )
{
    window.setTimeout( "gotoWillCall('" + queryString + "');", 100 );
}

function gotoWillCall( queryString )
{
    window.location.href = "index.sh?" + queryString + sessionString + "&opened=1&sized=1";
}

function pageLoad()
{
    if ( !doPageLoad )
		return;
		
	if ( getCookie( 'spin' ) != '0' && document.getElementById( 'divSidebar' ) != null )
	{
		showSidebar();
		sidePin( document.getElementById('imgPin') );
	}
	
	loadIframe( 'sesMain', sesMainSrc );
	loadIframe( 'sbMain', sbMainSrc );
	loadIframe( 'sbFav', sbFavSrc );
	window.setTimeout( 'checkMessages()', 120000 );
}

function pageLoadNoMainMenu()
{
    if ( !doPageLoad )
		return;
		
//	if ( getCookie( 'spin' ) != '0' && document.getElementById( 'divSidebar' ) != null )
//	{
//		showSidebar();
//		sidePin( document.getElementById('imgPin') );
//	}
	
	loadIframe( 'sesMain', sesMainSrc );
	loadIframe( 'sbMain', sbMainSrc );
	loadIframe( 'sbFav', sbFavSrc );
	window.setTimeout( 'checkMessages()', 120000 );
}

function loadIframe( iframeID, dest )
{
	if ( document.getElementById( iframeID ) != null )
		document.getElementById( iframeID ).contentWindow.location.href = dest;
}

function messageEvent()
{
    try
    {
        if ( this.getNodeValue( 'HasMessage' ) == 'true' )
            window.setTimeout( 'messageDisplay()', 200 );
            
        var cacheCount = Number( this.getNodeValue( 'PersonCacheCount' ) );
        setPersonCacheCount( cacheCount );
    }
    catch ( e ){}
}

function checkMessages()
{
    goAjax( 'Navigation/MessageChecker', messageEvent, true );
    window.setTimeout( 'checkMessages()', 120000 );
}

var htmlSource;
var lastScript = '';

//shortcuts
document.onkeydown = function ( evt )
{
    if ( evt == null )
        evt = event;
        
	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;
		}
		
		top.sesMain.htmlSource = htmlSource;
		winOpenCenter( 'Navigation/SourceView' );
	}
	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

		var mainWin		= document.getElementById('sesMain');
		mainWin.contentWindow.location.href = mainWin.contentWindow.location.href; //refresh the content win only
		return false;
	}
	else if ( testKeyCode( evt, 115 ) ) //F4
	{
		isListenerOn = !isListenerOn;
		
		alert( "isListenerOn: " + isListenerOn );
		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
	    search();
	    return false;
	}
	
	if ( testKeyCode( evt, 112 ) ) //F1
	{
	    cancelKeyEvent( evt ); //don't allow default help popup by F1
	    help();
		return false;
	}
}

var isListenerOn = false;

window.focus();

function addWindow( Window )
{
	Windows.push( Window );
}

window.onunload = closing;
function closing()
{
	for ( var i = 0; i < Windows.length; i++ )
	{
		var WindowChild = Windows.pop();
		
		if ( !WindowChild.closed )
		{
			try
			{
				WindowChild.parentClosing();
			}
			catch( e )
			{
				try
				{
					WindowChild.close();
				}
				catch( e2 )
				{
				
				}
			}
		}
	}
}

function setFavorite()
{
	external.AddFavorite( 'http://www.startadev.com/jericho/index.sh?portal=' + portalURL, 
		portalName + ' Portal');
}

var lastShownBut	= -1;
var lastEntityIndex = -1;

function ueBut( entityIndex, buttonSize, headerText, labelText ) //Update Entity Button
{
	var btnEn	= document.getElementById( 'btn' + entityIndex );
	var btnEnH	= document.getElementById( 'btnh' + entityIndex );
	var btnEnT	= document.getElementById( 'btnt' + entityIndex );
	
	if ( btnEn == null )
	    return;
	
	btnEnH.innerHTML	= htmlSafe( headerText );
	btnEnT.innerHTML	= htmlSafe( labelText );
	btnEnT.style.width  = ( buttonSize - 6 ) + "px";
	btnEn.style.width	= buttonSize + "px";
	
	if ( window.createPopup )
	    btnEn.style.display = 'inline';
	else
	    btnEn.style.display = 'table';
	
	if ( lastEntityIndex < entityIndex )
		lastEntityIndex = entityIndex;
	
	if ( lastShownBut < entityIndex )
		lastShownBut = entityIndex;
}

function heButs() //Hide Entity Buttons
{
	if ( lastShownBut < lastEntityIndex )
	{
		var btnEn;
		for ( var i = lastShownBut + 1; i <= lastEntityIndex; i++ )
		{
			btnEn				= document.getElementById( 'btn' + i );
			btnEn.style.display = 'none';
		}
		
		if ( isSidebarPinned && lastSidebarID != 'Main' && 
			Number( lastSidebarID ) > lastShownBut )
			changeToSidebar( 'Main', false );
	}
	
	lastEntityIndex = lastShownBut;
	lastShownBut	= -1;
}

function ueSB( entityIndex, dest, selID ) //Update Entity Sidebar
{
	var sbEn	= document.getElementById( 'sb' + entityIndex );
	
	if ( sbEn == null )
	    return;
	
	dest		= "r.sh?content=Navigation/Sidebar/" + dest + sessionString;
	var cur		= sbEn.contentWindow.location.href;
	cur			= "r.sh" + cur.substring( cur.indexOf('?content=' ) );
	
	if ( cur != dest )
	{
		sbEn.contentWindow.location.replace( dest );
		sbEn.setAttribute( 'selID', selID );
	}
	
	if ( isSidebarPinned && lastShownBut == entityIndex )
		changeToSidebar( entityIndex, true );
	
	if ( cur == dest )
		window.setTimeout( 'updateSidebarSelection( \'' + selID + '\', \'sb' + entityIndex + '\' );', 100 );
}

function updateSidebarSelection( selID, menuID )
{
	var sbEn	= document.getElementById( menuID );
	
	if ( sbEn.contentWindow.updateSelection )
		sbEn.contentWindow.updateSelection( selID );
}

function umSB( selID, changeTo ) //Update Main Sidebar
{
	var sbEn	= document.getElementById( 'sbMain' );
	
	if ( isSidebarPinned && changeTo )
		changeToSidebar( 'Main', false );
		
	window.setTimeout( 'updateSidebarSelection( \'' + selID + '\', \'sbMain\' );', 100 );
}

function rfSB( entityIndex, selID ) //Refresh Entity Sidebar
{
	var sbEn	= document.getElementById( 'sb' + entityIndex );
	sbEn.contentWindow.location.href = sbEn.contentWindow.location.href;
	sbEn.setAttribute( 'selID', selID );
}

function rfMain( selID ) //Refresh Entity Sidebar
{
	var sbEn	= document.getElementById( 'sbMain' );
	sbEn.contentWindow.location.href = sbEn.contentWindow.location.href;
	sbEn.setAttribute( 'selID', selID );
}

var previousURL = '';

function setPreviousURL( url )
{
	previousURL = url;
}

function getPreviousURL()
{
	return previousURL;
}

function goAsync( location, doShowWait ) 
{ 
	if ( doShowWait != null && doShowWait ) 
		showWait(); 
	
	document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
		"r.sh?content=" + location + sessionString;
}

function htmlSafe( text )
{
    return text.replace( /&/g, "&amp;" );
}

function getRandomQueryString()
{
    return "&" + new Date().getTime() + "=" + Math.random();
}

function messageDisplay()
{
	top.winOpenCenter( "Navigation/Message&" + Math.random() +"=" + Math.random() );
}