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" );

/**
 * Init
 *
 * Event handler for the onload event.
 *
 * Launches all functions that should be started immediately on the page.<b> 
 * after loading the page.
 *
 * @author Ralf Siemon
 */ 
function Init()
{
  HideOwnerLanguageSubmit();
  //CheckOpenAreaLogin();
}

/**
 * reSubmitParentAndClose
 * 
 * resubmits parent without opening
 * a new window
 * and close current window
 *
 * @author Jan Hoffman
 */
function reSubmitParentAndClose(formName)
{
   if (window.opener)
   {
      var form= window.opener.document.forms[formName];
      form.target="";
      form.submit();
      window.close();
   }
}
/**
 * clearParentMessage
 * 
 * clears any errorMessage
 * in loginWindow
 *
 *
 * @author Jan Hoffman
 */
function clearParentMessage()
{
   if (window.opener)
   {
      var element= window.opener.document.getElementById("message");
      
      var psw=getElement("txtOpenAreaLoginPassword",window.opener.document);
      
      psw.value="";
      var usr=getElement("txtOpenAreaLoginUserName",window.opener.document);
      usr.value="";
 	  element.innerHTML="";
      element.style["backgroundColor"]="";
   }
}

/**
 * HideOwnerLanguageSubmit
 *
 * Hides the submit button for the owner and language form.
 *
 * The submit button is only needed in case there is no JavaScript
 * activated. Otherwise the comboboxes' onchange event will already
 * send the form.
 *
 * @author Ralf Siemon
 */ 
function HideOwnerLanguageSubmit()
{
  var objSubmit = document.getElementById( "ownerlanguagesubmit" );

  if ( objSubmit )
  {
    objSubmit.style.display = "none";
  }
}

/**
 * ClearInput
 *
 * Clears the given input field objInput in case its 
 * current text matches the given text strInfoText.
 *
 * @author Ralf Siemon
 */ 
function ClearInput( objInput, strInfoText )
{
    if ( objInput.value == strInfoText )
    { 
        objInput.value = '';
    }
}

/**
 * ResetInput
 *
 * Sets the text of the given input field objInput
 * to the given text strInfoText in case its current
 * text is empty.
 *
 * @author Ralf Siemon
 */ 
function ResetInput( objInput, strInfoText )
{
    if ( objInput.value == '' )
    {
        objInput.value = strInfoText;
    }
}

/**
 * StartResetLoginTimer
 *
 * Starts a timer which clears the login form 
 * after a short period of time.
 *
 * @author Ralf Siemon
 */ 
function StartResetLoginTimer()
{
	window.setTimeout( "ResetLogin()", 200 );
}

/**
 * ResetLogin
 *
 * Clears the login form.
 *
 * @author Ralf Siemon
 */ 
function ResetLogin()
{
	var objLoginForm = document.getElementById( "loginform" );
	
	if ( objLoginForm )
	{
		objLoginForm.reset();
	}
}

/**
 * DisableEverything
 *
 * Starts a timer which disables all forms and links
 * after a short period of time. Additionally starts
 * the progress display in the main prefix element.
 *
 * @author Ralf Siemon
 */ 
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.outerHTML.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;
}

/**
 *  StartProgress
 *
 *  Switches the main prefix element to a "Please wait" message
 *  and starts a timer which displays the progress.
 *
 *  @author Ralf Siemon
 */
function StartProgress()
{	
	var objPrefixDiv = document.getElementById( "prefix" );
	
	if ( ! objPrefixDiv )	
	{
		return;
	}
	
	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 ); 
}

/**
 * ShowProgress
 *
 * Increases the number of dots displayed after the "Please wait" message.
 * If the number of dots exceeds 15, the dots are removed and the 
 * progress display loop starts again.
 *
 * @author Ralf Siemon
 */ 
function ShowProgress()
{
	var objProgressSpan = document.getElementById( "waitprogress" );
	
	if ( objProgressSpan.firstChild.data.length >= 15 )
	{
		objProgressSpan.firstChild.data = "";
	}
	else
	{
		objProgressSpan.firstChild.data += ".";
	}
}

/**
 * SetMainPrefixMessage
 *
 * Replaces the main prefix message with a new one
 *
 * @author Nuno Simoes
 */ 
function SetMainPrefixMessage( message )
{
	var objPrefixDiv = getElement("prefix");
	
	if ( objPrefixDiv )
	{
		objPrefixDiv.className = "prefix infocolor";
		
		var objMessageText  = document.createTextNode( message );
	
		objPrefixDiv.replaceChild( objMessageText, objPrefixDiv.firstChild );
	}
}

/*
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.
 *			Tien Duc Nguyen     28-11-2006  Image button support - The ID of each imagebutton is equals the it's textvalueno
 *
 * 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 )
	    {
/*	        TODO: This event will be in conflict with the submit function in the login form - need to find a solution for this!!! 
			case 13:
	        {
	            // ENTER *** OKAY ***
	            objButton = document.getElementById( "70120");
	            break;
	        }
*/	        case 113:
	        {
	            // F2 *** INSERT ***
	            
	            objButton = document.getElementById( "70000");
	            
	            break;
	        }
	        case 118:
	        {
	            // F7 *** UPDATE ***
	            
	            objButton = document.getElementById( "70010");
	            
	            break;
	        }
	        case 119:
	        {
	            // F8 *** COPY ***
	            
	            objButton = document.getElementById( "70190");
	            
	            break;
	        }
	        case 120:
	        {
	            // F9 *** LOOKUP ***
	            
	            objButton = document.getElementById( "70250");
	            
	            break;
	        }
	        case 123:
	        {
	            // F12 *** OKAY ***
	            
	            objButton = document.getElementById( "70120");
	            
	            break;
	        }
	    }
	}
	
	// 
	// Shift.
	// 
	if ( objEvent.shiftKey &&
	     ! objEvent.ctrlKey  &&
	     ! objEvent.altKey )
	{
	    // Which key was pressed?
	    switch ( iKeyCode )
	    {
	        // not supported in the Portal
/*	        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 118:
	        {
	            // SHIFT F7 *** EDITCHANGE ***
	            
	            objButton = objForm.cmdEditChange;
	            
	            break;
	        }
	        case 120:
	        {
	            // SHIFT F9 *** LAST ***
	            
	            objButton = objForm.cmdLast;
	            
	            break;
	        }
	        
*/	        case 117:
	        {
	            // SHIFT F6 *** PREVIOUS ***
	            
	            objButton = document.getElementById( "70540");
	            
	            break;
	        }
			case 119:
	        {
	            // SHIFT F8 *** NEXT ***
	            
	            objButton = document.getElementById( "70550");
	            
	            break;
	        }
	        case 122:
	        {
	            // SHIFT F11 *** PRINT ***
	            
	            objButton = document.getElementById( "70070");
	            
	            break;
	        }
	        case 123:
	        {
	            // SHIFT F12 *** SEARCH ***
	            
	            objButton = document.getElementById( "70030");
	            
	            break;
	        }
	    }
	}

    // 
    // Ctrl.
    // 
	if ( ! objEvent.shiftKey &&
	     objEvent.ctrlKey  &&
	     ! objEvent.altKey )
	{
	    // Which key was pressed?
	    switch ( iKeyCode )
	    {
	        case 123:
	        {
	            // CTRL F12 *** DELETE ***

	            objButton = document.getElementById( "70020");
	            
	            // Is there no Delete button (or is it disabled)?
	            if ( ! objButton )
	            {
	                // Use Delete Selected button instead.
	            
	                objButton = document.getElementById( "70100");
	            }
                
                break;
            }
            case 122:
	        {
	            // CTRL F11 *** REPRINT ***
	            
	            objButton = document.getElementById( "70775");
	            
	            break;
	        }
	        case 113:
	        {
	            // CTRL F2 *** IMPORT ***
	            
	            objButton = document.getElementById( "70778");
	            
	            break;
	        }
	        case 118:
	        {
	            // CTRL F7 *** OVERVIEW ***
	            
	            objButton = document.getElementById( "70771");
	            
	            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;
	}
	clearParentMessage();
	
	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( ' ' );
}

/*
 * Set the Focus in the Input Field specified by the parameter 'fieldname'
 * To use this function we have to call this function in a javascript tag
 * in the end of the jsp page.
 *
 * author:	Tien Duc Nguyen	21/02/2006
 * 
 */
function setFocus(fieldname) 
{
	if(fieldname != null && fieldname != '')
  	{
    	var objElement	=	getElement( fieldname );	
	    var myDoc=document;
		if(objElement)
    	{
      		objElement.focus();
    	}
  	}
} 

/**
* Author: Tien Duc Nguyen	09/03/2006
*
* Function to get the element
* This function has been tested with MSIE6, MSIE 7 and FireFox
**/
function getElement(name,doc)
{
    var curDoc = doc;
    if (curDoc==null)
        curDoc=document;
	var element=null;
	if (!element) 
	{
    	element=curDoc.getElementById(name);
    }
	if (!element && curDoc.getElementsByName)
	{
	    var elements = curDoc.getElementsByName(name);
		if (elements && elements.length>0)
		{
		   element = elements[0];
		}
    }
	if (!element)
	{
	    for (var i=0;i<curDoc.forms.length;i++)
	    {
	        var form = curDoc.forms[i];
	        element = form.elements[name];
	        if (element)
	        {
	           return element;
	        }
	    }
	}
    return element;
}

/*
 * Set the Action and trigger the Submit from the Form
 *
 * author:	Tien Duc Nguyen	04-04-2006
 *  
 */
 function submitForm(strAction, strFormName)
{
	if( strAction != null && strAction != "null" && strFormName != null && strFormName != "null" )
	{
		var objForm = document.forms[ strFormName ];
 	
 		if ( objForm )
 		{
			objForm.txtAction.value = strAction;
		
			DisableEverything();
			objForm.submit();
		}
	}
}

