// returns the form object of the management form
function __getForm() {
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		theform = document.masterForm;
	} else {
		theform = document.forms["masterForm"];
	}
	return theform;
}

// this is set to true if a postback is being executed
var __doingPostback = false;

// executes a form postback
function __doPostBack(eventTarget, eventArgument) {
	if (__doingPostback)
		return false;
	
	__doingPostback = true;
	
	var theform = __getForm();

	// check for an onsubmit event handler
	if (theform.onsubmit != null) {		
		var res = theform.onsubmit();
		if (res == null || !res) {
			__doingPostback = false;
			return;
		}
	}

	theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
	theform.__EVENTARGUMENT.value = eventArgument;
	__saveScrollPosition();

	theform.submit();
}

function __saveScrollPosition() {
	var theform = __getForm();
	var scrollX = 0;
	var scrollY = 0;

	if (window.pageYOffset) {
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	} else if (document.body.scrollTop) {
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}
	if (theform.__SCROLLPOSX && theform.__SCROLLPOSY) {
		theform.__SCROLLPOSX.value = scrollX;
		theform.__SCROLLPOSY.value = scrollY;
	}
}

function __restoreScrollPosition() {
	var theform = __getForm();

	if (theform.__SCROLLPOSX && theform.__SCROLLPOSY) {
		var scrollX = theform.__SCROLLPOSX.value;
		var scrollY = theform.__SCROLLPOSY.value;
		window.scrollTo(scrollX, scrollY);
	}
}

// creates a new hidden form variable
function __registerHiddenField(name, value) {
	var theform = __getForm();
	var ih = document.createElement("INPUT");
	ih.type = "hidden";
	ih.id = name;
	ih.name = name;
	ih.value = value;
	theform.appendChild(ih);
}

// sets the value of a hidden form variable
function __setHiddenField(name, value) {
	var hid = document.getElementById(name);
	hid.value = value;
}

// gets the value of a hidden form variable
function __getHiddenField(name) {
	var hid = document.getElementById(name);
	return hid.value;
}

function __attachRestoreScrollPosition() {
	var user_agent = navigator.userAgent;
	var isOpera = user_agent.indexOf("Opera") >= 0;
	var isIE = !isOpera && user_agent.indexOf("MSIE") >= 0;
	
	// for internet explorer and opera, run the script here
	if (isIE || isOpera || true)
		__restoreScrollPosition();
	else {
		// attach the method to restore the scroll position to the onload event
		if (window.addEventListener) {
			window.addEventListener("load", __restoreScrollPosition, false);
		} else if(window.attachEvent) {
			window.attachEvent("onload", __restoreScrollPosition);
		}
	}
}

