var autoscrollison = true;

function AutoScrollOnLoad(){
	try {
		if(document.getElementsByName('__ScrollTop')[0].value.length>0) document.body.scrollTop = document.getElementsByName('__ScrollTop')[0].value;
		if(document.getElementsByName('__ScrollLeft')[0].value.length>0) document.body.scrollLeft = document.getElementsByName('__ScrollLeft')[0].value;
	} catch (err) {
		// something is wrong. now just make sure a JS error doesn't show up
		DisableAutoScroll();
		return;
	}
	var f = document.forms[0];
	f.onsubmit_AutoScroll = f.onsubmit;
	f.onsubmit = function() {
		SetAutoScrollFields();
		if(f.onsubmit_AutoScroll) f.onsubmit_AutoScroll();
	}

	var links = document.links;
	for(var i=0; i<links.length; i++){
		if(links[i].autoscroll == 'false'){
			links[i].onclick_AutoScroll = links[i].onclick;
			links[i].onclick = DisableAutoScroll;
		}
	}

	var inputs = document.getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++){
		if(inputs[i].autoscroll == 'false'){
			inputs[i].onclick_AutoScroll = inputs[i].onclick;
			inputs[i].onclick = DisableAutoScroll;
		}
	}
}

function DisableAutoScroll(){
	autoscrollison = false;
	try {
		document.getElementsByName('__ScrollTop')[0].value = 'off';
		document.getElementsByName('__ScrollLeft')[0].value = 'off';
		if(this.onclick_AutoScroll)
			return this.onclick_AutoScroll;
		else
			return true;
	} catch (err) {
		// moving on
	}
}

function SetAutoScrollFields(){
	if (IsAutoScrollDisabled())
		return;
	document.getElementsByName('__ScrollLeft')[0].value = document.body.scrollLeft;
	document.getElementsByName('__ScrollTop')[0].value = document.body.scrollTop;
}

function IsAutoScrollDisabled(){
	return !autoscrollison;
}
