var objYourGlsWindow = null;

var arrEvents = new Array
( "onabort",
  "onblur",
  "onchange",
  "onclick",
  "onerror",
  "onfocus",
  "onkeydown",
  "onkeyup",
  "onload",
  "onmousedown",
  "onmousemove",
  "onmouseout",
  "onmouseover",
  "onmouseup",
  "onreset",
  "onselect",
  "onsubmit",
  "onunload" );

function Init()
{
  HideOwnerLanguageSubmit();
  CheckOpenAreaLogin();
}

function HideOwnerLanguageSubmit()
{
  var objSubmit = document.getElementById( "ownerlanguagesubmit" );

  if ( objSubmit )
  {
    objSubmit.style.display = "none";
  }
}

function ClearInput( objInput, strInfoText )
{
    if ( objInput.value == strInfoText )
    { 
        objInput.value = '';
    }
}

function ResetInput( objInput, strInfoText )
{
    if ( objInput.value == '' )
    {
        objInput.value = strInfoText;
    }
}

function StartResetLoginTimer()
{
	window.setTimeout( "ResetLogin()", 200 );
}

function ResetLogin()
{
	var objLoginForm = document.getElementById( "loginform" );
	
	if ( objLoginForm )
	{
		objLoginForm.reset();
	}
}

function DisableEverything()
{
	window.setTimeout( "DisableFormsAndLinks();", 0 );
	
	StartProgress();
}

/*
 * Disable all possibilities to use a link or send a form.
 *
 * author:	Ralf Siemon		24/08/2005
 */
function DisableFormsAndLinks()
{
	var arrElements = null;
	
	arrElements = document.getElementsByTagName( "a" );
	
	for ( iElementNo = arrElements.length - 1;
	      iElementNo >= 0;
	      -- iElementNo )
	{
		var objLink = arrElements[ iElementNo ];
		
		if ( ! objLink.getAttribute( "onclick" ) ||
			 objLink.getAttribute( "onclick" ).indexOf( "DisableEverything" ) < 0 )
		{
			continue;
		}
		
		DisableLink( objLink );
	}
	
	arrElements = document.getElementsByTagName( "input" );
	
	for ( iElementNo = 0;
	      iElementNo < arrElements.length;
	      ++ iElementNo )
	{
		var objInput = arrElements[ iElementNo ];
		var strType = objInput.getAttribute( "type" );
		
		if ( strType == "submit" ||
		     strType == "button" )
		{
			if ( ! objInput.getAttribute( "disabled" ) )
			{
				objInput.bWasEnabled = true;
			}
			
			objInput.setAttribute( "disabled", "disabled" );
		}
		
		DisableEvents( objInput );
	}

	arrElements = document.getElementsByTagName( "select" );
	
	for ( iElementNo = 0;
	      iElementNo < arrElements.length;
	      ++ iElementNo )
	{
		var objSelect = arrElements[ iElementNo ];
		
		DisableEvents( objSelect );
	}
}

/*
 * Disable all event handlers of the given form element.
 *
 * author:	Ralf Siemon		24/08/2005
 *
 * param:	objEvent		Form element
 */
function DisableEvents( objElement )
{	
	objElement.oldevents = new Array();
		
	for ( iEventNo = 0;
	      iEventNo < arrEvents.length;
	      ++ iEventNo )
	{
		strEvent = arrEvents[ iEventNo ];
		
		if ( objElement.getAttribute( strEvent ) )
		{
			objElement.oldevents[ iEventNo ] = objElement.getAttribute( strEvent );

			objElement.removeAttribute( strEvent );
		}
	}
}

/*
 * Disable the given link.
 * Replace the <a> tag by a <span> tag.
 *
 * author:	Ralf Siemon		24/08/2005
 */
function DisableLink( objLink )
{
	var objNonLink = document.createElement( "span" );
	
	if ( objLink.firstChild )
	{
		objNonLink.appendChild( objLink.firstChild );
	}
	
	var objLinkParent = objLink.parentNode;
	
	objLinkParent.replaceChild( objNonLink, objLink );
	
	objNonLink.oldlink = objLink;
}


function StartProgress()
{
	var objPrefixDiv = document.getElementById( "prefix" );
	
	objPrefixDiv.className = "prefix waitmessage";
	
	var objMessageText  = document.createTextNode( "Please wait" + " " );
	var objProgressSpan = document.createElement( "span" );
	var objProgressText = document.createTextNode( "" );
	
	objPrefixDiv.replaceChild( objMessageText, objPrefixDiv.firstChild );
	objPrefixDiv.appendChild( objProgressSpan );
	objProgressSpan.appendChild( objProgressText );
	
	objProgressSpan.id = "waitprogress";
	
	window.setInterval( "ShowProgress()", 300 ); 
}

function ShowProgress()
{
	var objProgressSpan = document.getElementById( "waitprogress" );
	
	if ( objProgressSpan.firstChild.data.length >= 15 )
	{
		objProgressSpan.firstChild.data = "";
	}
	else
	{
		objProgressSpan.firstChild.data += ".";
	}
}

/*
function SetButtonAsHiddenInput( objButton )
{
	var objForm = objButton.form;
	
	var objHiddenInput = document.createElement( "input" );
	
	objHiddenInput.setAttribute( "type", "hidden" );
	objHiddenInput.setAttribute( "name", objButton.getAttribute( "name" ) );
	objHiddenInput.setAttribute( "value", objButton.getAttribute( "value" ) );
	
	objForm.appendChild( objHiddenInput );
}
*/

/* Function Keys
 *
 * Determines the button corresponding to the pressed function key
 * and imitates a click on it.
 *
 * author:	Matthias Brueckmann	12-12-2002
 *								03-01-2003	New Function Keys
 *          Ralf Siemon         10-11-2004  Browser independent reimplementation
 *                                          TOP/BOTTOM mechanism not needed anymore.
 *
 * param :  objEvent            Event object from the calling event handler  
 */
function FunctionKey( objEvent )
{
    // Determine key.
	var iKeyCode = objEvent.keyCode;
	
	// Determine the form to be submitted.
	var objForm = document.getElementById( "mainform" );
	
	// Now determine the button 
	// corresponding to the pressed key.
	
	var objButton = null;

	// First determine whether Shift, Ctrl or Alt key was used:
	
	// 
	// Without Shift, Ctrl or Alt.
	// 
	if ( ! objEvent.shiftKey &&
	     ! objEvent.ctrlKey  &&
	     ! objEvent.altKey )
	{
	    // Which key was pressed?
	    switch ( iKeyCode )
	    {
	        case 113:
	        {
	            // F2 *** INSERT ***
	            
	            objButton = objForm.cmdInsert;
	            
	            break;
	        }
	        case 118:
	        {
	            // F7 *** UPDATE ***
	            
	            objButton = objForm.cmdUpdate;
	            
	            break;
	        }
	        case 119:
	        {
	            // F8 *** COPY ***
	            
	            objButton = objForm.cmdCopy;
	            
	            break;
	        }
	        case 120:
	        {
	            // F9 *** LOOKUP ***
	            
	            objButton = objForm.cmdLookup;
	            
	            break;
	        }
	        case 123:
	        {
	            // F12 *** OKAY ***
	            
	            objButton = objForm.cmdOkay;
	            
	            break;
	        }
	    }
	}
	
	// 
	// Shift.
	// 
	if ( objEvent.shiftKey &&
	     ! objEvent.ctrlKey  &&
	     ! objEvent.altKey )
	{
	    // Which key was pressed?
	    switch ( iKeyCode )
	    {
	        case 113:
	        {
	            // SHIFT F2 *** EDITADD ***
	            
	            objButton = objForm.cmdEditAdd;
	            
	            break;
	        }
	        case 114:
	        {
	            // SHIFT F3 *** HELP ***
	            
	            objButton = objForm.cmdHelp;
	            
	            break;
	        }
	        case 116:
	        {
	            // SHIFT F5 *** FIRST ***
	            
	            objButton = objForm.cmdFirst;
	            
	            break;
	        }
	        case 117:
	        {
	            // SHIFT F6 *** PREVIOUS ***
	            
	            objButton = objForm.cmdPrevious;
	            
	            break;
	        }
	        case 118:
	        {
	            // SHIFT F7 *** EDITCHANGE ***
	            
	            objButton = objForm.cmdEditChange;
	            
	            break;
	        }
	        case 119:
	        {
	            // SHIFT F8 *** NEXT ***
	            
	            objButton = objForm.cmdNext;
	            
	            break;
	        }
	        case 120:
	        {
	            // SHIFT F9 *** LAST ***
	            
	            objButton = objForm.cmdLast;
	            
	            break;
	        }
	        case 122:
	        {
	            // SHIFT F11 *** PRINT ***
	            
	            objButton = objForm.cmdPrint;
	            
	            break;
	        }
	        case 123:
	        {
	            // SHIFT F12 *** SEARCH ***
	            
	            objButton = objForm.cmdSearch;
	            
	            break;
	        }
	    }
	}

    // 
    // Ctrl.
    // 
	if ( ! objEvent.shiftKey &&
	     objEvent.ctrlKey  &&
	     ! objEvent.altKey )
	{
	    // Which key was pressed?
	    switch ( iKeyCode )
	    {
	        case 123:
	        {
	            // CTRL F12 *** DELETE ***

	            objButton = objForm.cmdDelete;
	            
	            // Is there no Delete button (or is it disabled)?
	            if ( ! objButton )
	            {
	                // Use Delete Selected button instead.
	            
	                objButton = objForm.cmdDeleteSelected;
	            }
                
                break;
            }
        }
    }
    
    // Does the button exist on the current screen,
    // and is it enabled?
    if ( objButton )
    {
        // Click the button.
        objButton.click();
    }
}

/**
 * CheckOpenAreaLogin
 *
 * Checks if the hidden form for the Open Area Login
 * exists and in case it does, sends it.
 *
 * author:	Ralf Siemon         20/12/2005
 */
function CheckOpenAreaLogin()
{
	var objHiddenLoginForm = document.getElementById( "hiddenloginform" );
	
	if ( ! objHiddenLoginForm )
	{
		return;
	}
	
	objHiddenLoginForm.submit();
}

/**
 * CloseWindow
 *
 * Closes the current window
 *
 * author:  Ralf Siemon		02/01/2006
 */
function CloseWindow()
{
	window.close();
}

/**
 * SendLoginForm
 *
 * Sends the login form
 *
 * author:  Ralf Siemon		03/01/2006
 */
function SendLoginForm()
{
 	var objForm = document.forms[ 'SCTT020' ];
 	
 	if ( objForm )
 	{
 		objForm.submit();
 	}
}
 
/**
 * HighlightTopLink
 *
 * Highlights the Toolbar Navigation Links (Home/News/Your GLS).
 *
 * @author Ralf Siemon	03/01/2006
 */ 
function HighlightTopLink( objTableCell, bHighlight )
{
    var aClasses = objTableCell.className.split( ' ' );

    if ( bHighlight )
    {
        aClasses.push( 'hover' );
    }
    else
    {
        aClasses.pop();        
    }
    
    objTableCell.className = aClasses.join( ' ' );
} 
