//The root context for the web application
var rootContext = "/csiro-login/";

//The prompt message to save the form changes
var message = "You have made changes on the page that you have not saved. \nClick Cancel to return to the page and then click the Save button \nor click OK to leave the page and lose your changes."

//The number of the form on the page to be checked
var formNumber = 1;

// The page timeout warning interval, in milliseconds
var pageTimeoutWarnInterval = (30-5)*60*1000;

// The page timeout interval, in milliseconds
var pageTimeoutInterval = 5*60*1000;

//Function to set the change flag to true.
function setModified()
{
	document.forms[formNumber].modifiedForm.value = true;		
}

function setFormNumber(num)
{
	formNumber = num;
}

function warnem()
{
	var currentTime = new Date().getTime();
	
	alert("Timeout Warning\n\n" +
		"Your session will timeout in 5 minutes. Please " + 
		"save the page now if you wish to retain your changes.");
		
	var timeElapsed = new Date().getTime() - currentTime;
		
	if (timeElapsed >= pageTimeoutInterval)
	{	
		timeout();
	}
	else
	{
		window.setTimeout('timeout()', pageTimeoutInterval - timeElapsed);
	}
}

function timeout()
{
	alert("OzClim Timeout \n\n" +
		"Your session has timed out. Please login again.");
}

//Function to set a timeout warning before the page expires.
function timer()
{
	window.setTimeout('warnem()', pageTimeoutWarnInterval);
}

//Function to check if the modified form flag has been set before changing 
//the window's location
function checkModified()
{
    if (typeof(document.forms[formNumber].modifiedForm)=="undefined")
	{
		return true;				
	}	

	if (document.forms[formNumber].modifiedForm.value == 'true')
	{
		return confirm(message);
		
	} else {
		return true;				
	}	
}

// Processing which must be run at the start of each page.
// This is called by the onload event of each page.
function start()
{
  //setFocus(0);
  if (document.forms[formNumber]!=null && typeof(document.forms[formNumber].scrollTarget)!="undefined")
  {
    scrollToAnchor(document.forms[formNumber].scrollTarget.value);
  }
  
  //timer();
}

//Function will set the page focus (cursor) to the first non-readonly element on the page, 
//usually a text field. 
function setFocus()
{
	//stores the numbers of elements available on the form
	var numberOfElements = document.forms[1].length;
	
	//store the type of each element
	var elementType;
	for(var i=0;i<numberOfElements;i++)
	{		
		//catch null. usually caught when there are NO non-readonly fields on page
		if(document.forms[formNumber].elements[i].type ==null)					
		{
			continue;
		}

		//set y to be the type of the form, eg "hidden", "textarea", etc.
		elementType = document.forms[1].elements[i].type;
		
		//set the focus to the first form on the field
		//do NOT set focus on hidden, checkbox or radio fields.
		if(document.forms[formNumber].elements[i].readOnly == false && (elementType !="hidden" && elementType!= "checkbox" && elementType!= "radio" && elementType!="submit"))
		{						
			document.forms[formNumber].elements[i].focus();			
			break;
		}
	}		
}

function scrollToAnchor(anchorname) {
  if (anchorname == '')
  {
    return;
  }
  var o = findObj(anchorname);
  var yPosition = getPageOffsetTop(o);
  window.scroll(0,yPosition);
}

/*
 Retrieve an object from the document.
*/
function findObj( oName, oFrame, oDoc ) {
	if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else { oDoc = window.document; } }
	if( oDoc[oName] ) { return oDoc[oName]; } if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }
	if( oDoc.getElementById && oDoc.getElementById(oName) ) { return oDoc.getElementById(oName); }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; x < oDoc.anchors.length; x++ ) { if( oDoc.anchors[x].name == oName ) { return oDoc.anchors[x]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
		var theOb = findObj( oName, null, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	if( !oFrame && window[oName] ) { return window[oName]; } if( oFrame && oFrame[oName] ) { return oFrame[oName]; }
	for( var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++ ) {
		var theOb = findObj( oName, oFrame.frames[x], oFrame.frames[x].document ); if( theOb ) { return theOb; } }
	return null;
}

// Function to get position of an object
function getPageOffsetTop (el) 
{
  if (el == undefined)
  {
    return 0;
  }
  var ot=el.offsetTop;
  while((el=el.offsetParent) != null) { ot += el.offsetTop; }
  return ot;
}

function checkSubmit()
{
    if (typeof(document.forms[formNumber].hasBeenSubmitted)=="undefined")
	{		
		return true;				
	}	

	if (document.forms[formNumber].hasBeenSubmitted.value == 'true')
	{				
		return false;
		
	} 
	else 
	{		
		document.forms[formNumber].hasBeenSubmitted.value = 'true';
		return true;				
	}	
}