/* Author 	: Susantha Soysa, Sri Lanka.
   Contact	: susanthas@hotmail.com/ susanthas@gmail.com
   Created Date	: 11/Feb/2006
   Last Modified: 02/Apr/2006
   Copyright	: The code is freely distributable, but appriciate if the Author and Contact email is included in the
		  final source. The author will be delited with a short thanking email.
   Warranty	: This code is provided "AS IS" and the author bares no warranty what so ever.
   Description	: I was tired of finding a solution to save the scroll position 
      of a div element in the internet. Although there were few solutions, none of
      them was good enough to impress me, since I had to write lines 
      of code to get them functioning. So I came up with this solution which require 
      only one customization. I think you will feel lucky that I'm here to save 
      you. :D.
*/

// TO DO :
//	1. Set scrollingElements variable below 
//  2. Include the following in the form OnSubmit event
//      onsubmit="javascript:saveScrollPositions();"


// Set your div names (comma seperated) in which scroll position to be restored
//var scrollingElements = "div1,div3";
	
// DO NOT CHANGE
// scroll bar entry start and end tags
var scrollEntryStart = "scrollPos<!--";
var scrollEntryEnd   ="-->";
	
// DO NOT CHANGE
// Returns the HTML element correspond to the id.
function getElement(id)
{ 
	return document.getElementById ? document.getElementById(id) : document.all ? document.all(id) : null;
} 

// DO NOT CHANGE
// Restores the scroll box positions in all the scroll bars
window.onload = function(){
	var strCook = document.cookie;
	if(strCook.indexOf(scrollEntryStart)>= 0){
		var intS = strCook.indexOf(scrollEntryStart);
		var intE = strCook.indexOf(scrollEntryEnd);
		var strPos = "" + strCook.substring(intS+scrollEntryStart.length,intE) + "";
		var scrollEntries = strPos.split(":");
		var curElement;
		for (var i = 0; i < scrollEntries.length; i++){
			var scrollElement = scrollEntries[i].split(",");
			try{
				curElement = getElement(scrollElement[0]);
				curElement.scrollLeft = parseInt(scrollElement[1]);
				curElement.scrollTop = parseInt(scrollElement[2]);  
			}catch(e){
			}
		}
			
	}
	document.cookie = "";
}

// DO NOT CHANGE
// Saves the scroll positions
function saveScrollPositions(){
	var scrollElements = scrollingElements.split(",");
	var cookieData = "";
	var elementData = "";
	var curElement;
	var intY, intX;
	for (var i = 0; i < scrollElements.length; i++){
		curElement = getElement(scrollElements[i]);
		intX = curElement.scrollLeft;
		intY = curElement.scrollTop;
		elementData = scrollElements[i] + "," + intX + "," + intY;
		cookieData += cookieData != "" ? ":" + elementData: elementData;
		//cookieData += elementData + ":";
	}
	if (cookieData != "")
		document.cookie = scrollEntryStart + cookieData + scrollEntryEnd;
}
