﻿/*****************************************************************************************/
// Save the scroll position to the ScrollX and ScrollY hidden form elements
function ScrollPosition_GetCoords()
{
    var ScrollX, ScrollY;
    if(document.all)
    {
	    if(!document.documentElement.scrollLeft)
		    ScrollX = document.body.scrollLeft;
	    else
		    ScrollX = document.documentElement.scrollLeft;
			
	    if(!document.documentElement.scrollTop)
		    ScrollY = document.body.scrollTop;
	    else
		    ScrollY = document.documentElement.scrollTop;
    }
    else
    {
	    ScrollX = window.pageXOffset;
	    ScrollY = window.pageYOffset;
    }
        
    GetElement("ScrollX").value = ScrollX;
    GetElement("ScrollY").value = ScrollY;
}

function ScrollPosition_ScrollIt()
{
    // Only "AutoScroll" if value for that hidden variable with that name is set to "true"
    var AutoScroll = GetElement("AutoScroll");
    if(AutoScroll.value == "true")
    {
        var x = GetElement("ScrollX").value;
        var y = GetElement("ScrollY").value;
        window.scrollTo(x, y);
    }
}


// Event bindings
// These check to see if the event is already bound and if so, they append the ScrollPosition code 
// to the existing event binding.  We don't want to hijack the browser events if, for example,
// a developer has specified an "onload" attribute for the "body" tag.
if(window.onload != null)
{
    var xOnLoad = String(window.onload);
    xOnLoad = xOnLoad.substr(xOnLoad.indexOf("{") + 1);
    xOnLoad = xOnLoad.substr(0,xOnLoad.lastIndexOf("}") - 1);
    window.onload = new Function(xOnLoad + "; ScrollPosition_ScrollIt();");
}
else
{
    window.onload = ScrollPosition_ScrollIt;
}

if(window.onscroll != null)
{
    var xOnScroll = window.onscroll.toString();
    xOnScroll = xOnScroll.substr(xOnScroll.indexOf("{") + 1);
    xOnScroll = xOnScroll.substr(0,xOnScroll.lastIndexOf("}") - 1);
    window.onscroll = new Function(xOnScroll + "; ScrollPosition_GetCoords();");
}
else
{
    window.onscroll = ScrollPosition_GetCoords;
}

if(window.onclick != null)
{
    var xOnClick = window.onclick.toString();
    xClick = xClick.substr(xClick.indexOf("{") + 1);
    xClick = xClick.substr(0,xClick.lastIndexOf("}") - 1);
    window.onclick = new Function(xClick + "; ScrollPosition_GetCoords();");
}
else 
{
    window.onclick = ScrollPosition_GetCoords;
}

if(window.onkeypress != null)
{
    var xKeyPress = window.onkeypress.toString();
    xKeyPress = xKeyPress.substr(xKeyPress.indexOf("{") + 1);
    xKeyPress = xKeyPress.substr(0,xKeyPress.lastIndexOf("}") - 1);
    window.onkeypress = new Function(xKeyPress + "; ScrollPosition_GetCoords();");
}
else
{
    window.onkeypress = ScrollPosition_GetCoords;
}