
/*
    Scroll Postion Saver Logic: Randy Black - Apr 2009
    To use this, put initScrollSaver("key") in body onload for the page you want to remember the scroll postion in.
    This call will place a marker in cookies that stores the scroll positions using three identifiers:
    "key" value, page view, and pageno.  Upon returning to this page, a call to initScrollSaver("key")
    will see if the page view and pageno are the same as the last visit.  If so, the scroll position will
    be set to the value that existed when we were last on this page.  The page view is the query parameter "view".
    If that is not present, then the none-query part of the url is used instead (for Zend).  The pageno is also
    a query variable.
    
    Note: to reset a view, you need to include the following on click event: onclick="document.cookie='resetviewflag={KEYNAMEOFTARGETPAGE}'"
    
 */

var docscrollkeyname = "";

function formDataPageNo() {
	return ((FORM_DATA['pageno']==undefined) || FORM_DATA['pageno']=='') ? '' : FORM_DATA['pageno'];
}

function currentViewName() {
    return (typeof(FORM_DATA['view'])=='undefined') ? FORM_DATA['baselocation'] : FORM_DATA['view'];
}

function OnScrollPosSave() {
	document.cookie = "docscrollx" + docscrollkeyname + "=" + getScrollX();
	document.cookie = "docscrolly" + docscrollkeyname + "=" + getScrollY();
	document.cookie = "docscrollview" + docscrollkeyname + "=" + currentViewName(); 
	document.cookie = "docscrollpageno" + docscrollkeyname + "=" + formDataPageNo(); 
}

function scrollToCookiePositon() {
	window.scrollTo(cookies['docscrollx'+docscrollkeyname],cookies['docscrolly'+docscrollkeyname]);
}

function scrollToTopPosition() {
	window.scrollTo(0,0);
}

function initScrollSaver(keyname) {
	docscrollkeyname = keyname;
	document.cookie = "arecookiesenabled=yes";
	extractCookies();
	
	var forceresetview = false;
	if (FORM_DATA['resetview']=="1") forceresetview = true;
	if (cookies['resetviewflag']==keyname) {
		forceresetview = true;
		document.cookie = "resetviewflag=";
	}
	
	if ((cookies['docscrollview'+docscrollkeyname] == currentViewName()) && !forceresetview && (cookies['docscrollpageno'+docscrollkeyname] == formDataPageNo())) {
	    window.setTimeout("scrollToCookiePositon()",1); // needed to do this so the setfocus message didn't come after this.
	} else {
		window.setTimeout("scrollToTopPosition()",1);  // sometimes the setfocus scrolls the top out of view.  This beings it back
    }
	if (cookies['arecookiesenabled'] == 'yes') {
		window.onbeforeunload = function () {
			OnScrollPosSave();
		}
	}
	
}

function getScrollY() {
	if (window.pageYOffset)
	{
	  	pos = window.pageYOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop
	}
	else if (document.body)
	{
	  pos = document.body.scrollTop
	}
	return pos;
}

function getScrollX() {
	if (window.pageXOffset)
	{
	  	pos = window.pageXOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollLeft
	}
	else if (document.body)
	{
	  pos = document.body.scrollLeft
	}
	return pos;
}


function createRequestObject() {
	  FORM_DATA = new Object();
	    // The Object ("Array") where our data will be stored.
	  separator = ',';
	    // The token used to separate data from multi-select inputs
	  query = '' + this.location;
	  qu = query
	    // Get the current URL so we can parse out the data.
	    // Adding a null-string '' forces an implicit type cast
	    // from property to string, for NS2 compatibility.
	  var baselocation = (query.indexOf('?')==-1) ? query : query.substring(0, query.indexOf('?'));
	  query = query.substring((query.indexOf('?')) + 1);
	    // Keep everything after the question mark '?'.
	  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
	  keypairs = new Object();
	  numKP = 1;
	    // Local vars used to store and keep track of name/value pairs
	    // as we parse them back into a usable form.
	  while (query.indexOf('&') > -1) {
	    keypairs[numKP] = query.substring(0,query.indexOf('&'));
	    query = query.substring((query.indexOf('&')) + 1);
	    numKP++;
	      // Split the query string at each '&', storing the left-hand side
	      // of the split in a new keypairs[] holder, and chopping the query
	      // so that it gets the value of the right-hand string.
	  }
	  keypairs[numKP] = query;
	    // Store what's left in the query string as the final keypairs[] data.<
	  for (i in keypairs) {
	    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
	      // Left of '=' is name.
	    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
	      // Right of '=' is value.
	    while (keyValue.indexOf('+') > -1) {
	      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
	        // Replace each '+' in data string with a space.
	    }
	    keyValue = unescape(keyValue);
	      // Unescape non-alphanumerics
	    if (FORM_DATA[keyName]) {
	      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
	        // Object already exists, it is probably a multi-select input,
	        // and we need to generate a separator-delimited string
	        // by appending to what we already have stored.
	    } else {
	      FORM_DATA[keyName] = keyValue;
	        // Normal case: name gets value.
	    }
	  }
	  FORM_DATA['baselocation'] = baselocation;
	  return FORM_DATA;
	}

	FORM_DATA = createRequestObject();

	  // This is the array/object containing the GET data.
	  // FORM_DATA = createRequestObject();
	  // Retrieve information with 'FORM_DATA [ key ] = value'.

	var cookies = new Object();

	function extractCookies() {
		var name, value;
		var beginning, middle, end;
		for (name in value) {
			cookies = new Object;
		}
		beginning = 0;
		while (beginning < document.cookie.length) {
			middle = document.cookie.indexOf('=',beginning);
			end = document.cookie.indexOf(';',beginning);
			if (end == -1)
				end = document.cookie.length;
			if ( (middle > end) || (middle == -1)) {
				name = document.cookie.substring(beginning, end);
				value = "";
			} else {
				name = document.cookie.substring(beginning,middle);
				value = document.cookie.substring(middle+1, end);
			}
			cookies[name] = unescape(value);
			beginning = end + 2;
		}
	}



