var intervalSeconds = 60*5;
var messageBox;
//var lastFocus;
var theform;
var alreadyScheduled = false;

addLoadEvent( getTheForm );
addLoadEvent( cancelAutoSave );
addLoadEvent( loadAutoSave );

function addCancelAutoSave()
{
	theform.onsubmit = cancelAutoSave;
}

function cancelAutoSave()
{
	// well that's no good, onsubmit isn't fired by form.submit();
}

function getTheForm()
{
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
		theform = document.TheForm;
	else 
		theform = document.forms["TheForm"];
}
	
function loadAutoSave()
{


	if (theform!=null)
	{
		/*
		lastFocus = theform["LastFocusFieldName"];
		if (lastFocus!=null)
			positionCaretAtEnd(lastFocus.value);
		*/

		createMessageContainer();
		scheduleAutoSave();
	}
}

function createMessageContainer()
{
	messageBox = document.createElement('span');
	var bodyRef = document.getElementsByTagName("body").item(0);
	bodyRef.appendChild(messageBox);
	messageBox.style.background='#f0f0f0';
	messageBox.style.fontFamily='tahoma';
	messageBox.style.color='#990000';
	messageBox.style.position='absolute';
}

function scheduleAutoSave()
{
	if (alreadyScheduled==false)
	{
		setTimeout("autoSave()", intervalSeconds * 1000);
		alreadyScheduled = true;
	}
}


function autoSave()
{
	showAutoSaveWarningMessage(5);
	for (i=4; i>=1; i--)
		setTimeout("showAutoSaveWarningMessage(" + i + ")", (5-i)*1000);

	setTimeout("autoSavePostBack()", 5000);
}


function showAutoSaveWarningMessage(seconds) 
{
	messageText = "Auto-saving in " + seconds + " seconds.";
	fontSize = 12;
	padding = 0;
	messageBox.style.visibility = "visible";
	messageBox.innerHTML = "<nobr>" + messageText + "</nobr>";
	messageBox.style.fontSize = fontSize;
	messageBox.style.padding = padding;
	messageBox.style.top = (document.body.scrollTop);
	messageBox.style.left = (document.body.scrollLeft+document.body.clientWidth-fontSize*messageText.length*0.5-padding*2);
}


function autoSavePostBack()
{
	if (theform!=null)
	{
		messageBox.style.visibility = "hidden";

		oldTarget = theform.target;
		theform.target = "AutoSaveIFrame";

		theform.__EVENTTARGET.value = "AutoSave";
		/*
		if (lastFocus!=null)
		{
			theform.__EVENTARGUMENT.value = lastFocus.id;
		}
		*/
		theform.submit();

		theform.target = oldTarget;
		theform.__EVENTTARGET.value = "";

		setTimeout("autoSave()", intervalSeconds * 1000);
	}
}


/*
function autoSave()
{
	createMessageContainer();
	autoSaveStep1();
}
*/


/*
function autoSaveStep2()
{
	showAutoSaveMessage();
	autoSavePostBack();
}
*/


/*
function showAutoSaveMessage() 
{
	messageText = "Auto-saving, please wait...";
	fontSize = 30;
	padding = 10;
	message.innerHTML = messageText;
	message.style.fontSize = fontSize;
	message.style.padding = padding;
	message.style.top = (document.body.scrollTop+(document.body.clientHeight-fontSize)/2-padding);
	message.style.left = (document.body.scrollLeft+(document.body.clientWidth-fontSize*messageText.length*0.5)/2-padding);
}
*/


/*
function setLastFocus(obj)
{
	lastFocus = obj;
}
*/


/*
function positionCaretAtEnd (controlName) { 
	if (controlName!='')
	{
		var textControl = document.forms[1][controlName];
		if (textControl!=null)
		{
			if (textControl.createTextRange) { 
				var range = textControl.createTextRange(); 
				range.collapse(false); 
				range.select(); 
			} 
   }
  }
}
*/

