function addLoadEvent(func) {
				
  				var oldonload = window.onload;
 				if (typeof window.onload != 'function') {
					window.onload = func;
				} 
				else {
					window.onload = function() {
						if (oldonload) {
							oldonload();
						}
						func();
					}
  				}
			}
			
			function getCookie(name) {
				var dc = document.cookie;
				var prefix = name + "=";
				var begin = dc.indexOf("; " + prefix);
				if (begin == -1) {
					begin = dc.indexOf(prefix);
					if (begin != 0) return null;
				} 
				else {
					begin += 2;
				}
				var end = document.cookie.indexOf(";", begin);
				if (end == -1) {
					end = dc.length;
				}
				return unescape(dc.substring(begin + prefix.length, end));
			}
			
			function setCookie(name, value, expires, path, domain, secure) {
				document.cookie= name + "=" + escape(value) +
				((expires) ? "; expires=" + expires.toGMTString() : "") +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				((secure) ? "; secure" : "");
			}
			
			function deleteCookie(name, path, domain) {
				if (getCookie(name)) {
    				document.cookie = name + "=" +
     				((path) ? "; path=" + path : "") +
      				((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
				}
			}
			
			function init( ) {
				document.onmousedown = onMouseDown;
				document.onmouseup = onMouseUp;
				
				id = $("page_id").value;
								
				var fromBackButton = false;
				
				
				if (getCookie(id)) { 
					fromBackButton = true; 
				}
				else{ 
					setCookie(id, "1") ;
				}
				
				if( fromBackButton ) {
					$("spacer").style.height = getCookie(id+"_height") + "px";
					scroll(0, getCookie(id + "_scroll") );            
				}

				$("history_index").value = 1;
				setTimeout("updatePage()", 0);
			}	
			
			
			
			function updatePage(){
				
				//alert("isUpdating = " + isUpdating + ", mouseState = " + mouseState + ", PageHeight = " + getPageHeight() + ", ScrolHeight = " + getScrollHeight() + ", preloadDistance = " + preloadDistance);

				
				if (isUpdating == false && mouseState == "up" && getPageHeight() - getScrollHeight() < preloadDistance) {
					//alert('ok');
					setCookie(id, $("history_index").value );
					isUpdating = true;
					getMoreHistory();
				}
				if (parseInt($("history_index").value) <= parseInt($("total_pages").value) && parseInt($("total_pages").value) != 1) {				
					//alert("test 2");
					setTimeout("updatePage()", checkInterval);
				
				}
			}
			
			function getMoreHistory() {
				var indexVal = $("history_index").value;
				var params = "page=" + indexVal;
				var total_pg = ($("total_pages").value);
				//alert(indexVal + " :: " + params);
				//alert("indexVal = " + indexVal + ",  total_pages = " + total_pg);
				if (indexVal != total_pg) {
				//	alert("test pass");
					new Ajax.Request("results.php?search_str=" + document.myForm.myText.value + "&start=" + indexVal, {asynchronous:true, method: 'post', onComplete: addMoreHistory, onFailure: failure, parameters: params});
				}
			}
			
			
			function addMoreHistory(ajax){
				var response = ajax.responseText;
				
				
				if (response == "No results!") {
					//alert("No results!");
					Element.hide("contentLYR");
					//Element.show("no_results");
					//Element.hide("more_loading");
				} 
				else {
					if (parseInt($("total_pages").value) == 1) {
						//alert("total_pages = 1");
						$("contentLYR").innerHTML += response;
					//	Element.hide("more_loading");
					//	Element.hide("page_end");
					} 
					else {
						if (parseInt($("history_index").value) == parseInt($("total_pages").value)) {
							//alert("history_index = total_pages");
							$("contentLYR").innerHTML += response;
							//Element.hide("more_loading");
							//Element.show("page_end");
							$("history_index").value = parseInt($("history_index").value) + 1;
						} 
						else {
							//Element.show("more_loading");
							//alert(response);
							//alert(response.indexOf('nomoreresults'));
							if(response.indexOf('nomoreresults') == -1 && sHold != "1") {
								$("contentLYR").innerHTML += response;
								$("history_index").value = parseInt($("history_index").value) + 1;
							}
						}
					}
				}
	
				isUpdating = false;
			}
			
			
			
			function onMouseDown(e ){
				//mouseState = "down";
				setCookie(id+"_height", getScrollHeight());
				setCookie(id+"_scroll", getScrollHeight() - _getWindowHeight());
				var theScrollDistance = (getScrollHeight() - _getWindowHeight());
				//alert(theScrollDistance);
			}

			function onMouseUp(){
				mouseState = "up";
			}
			
			function getPageHeight(){
				var y;
				var test1 = document.body.scrollHeight;
				var test2 = document.body.offsetHeight
				if (test1 > test2) {
					y = document.body.scrollHeight;
				} 
				else {
	 				 y = document.body.offsetHeight;
 				}
  			return parseInt(y);
			}

			function _getWindowHeight(){
 				if (self.innerWidth) {
					frameWidth = self	.innerWidth;
					frameHeight = self.innerHeight;
				} 
				else if (document.documentElement && document.documentElement.clientWidth) {
    				frameWidth = document.documentElement.clientWidth;
					frameHeight = document.documentElement.clientHeight; 
				} 
				else if (document.body) {
					frameWidth = document.body.clientWidth;
					frameHeight = document.body.clientHeight;
				}
				return parseInt(frameHeight);
			}

			function getScrollHeight(){
				var y;
				// all except Explorer
				if (self.pageYOffset) {
					y = self.pageYOffset;
				} 
				else if (document.documentElement && document.documentElement.scrollTop) {
					y = document.documentElement.scrollTop;
				} 
				else if (document.body)	{
					y = document.body.scrollTop;
				}
				return parseInt(y)+_getWindowHeight();
			}
			
			
			var checkInterval = 200;
			var preloadDistance = 250;
			var isUpdating = false;
			var mouseState = "up";
			var id;
			
			
			//addLoadEvent(init);
			
			function failure() {
				$("contentLYR").innerHTML = "<p><strong>Could not contact the server.</strong><br />Please wait awhile and try again. <br /><br />We apologize for the inconvenience.</p>";
			}
	
	
	function checkAction(theKey) {
		var character = String.fromCharCode(theKey);
		alert(theKey);
		if(character == ' ' || character == "\r") {
			//swapGo(2);
			changeVal();
			//setTimeout("swapGo(1)",1);
		}
	}

	function swapGo(which) {
		if(which > 1) {
			document.goButton.src = 'images/button_1.gif';
		}
		else {
			document.goButton.src = 'images/button_0.gif';
		}
	}
	
	function changeVal() {
		swapGo(2);	
		locationSTR = "results.php?search_str=" + document.myForm.myText.value
		ajaxManager('load_page', locationSTR, 'contentLYR');
		//setTimeout("swapGo(1)",1);
		swapGo(1);
		
	}
	
	function moreEval(startVal) {
		swapGo(2);
		if(!startVal) {
			startVal = 0;
		}
		
		if(keyok < 1) { 
			openSeat('none');
		}
		
		locationSTR_1 = "results.php?search_str=" + document.myForm.myText.value + "&start=" + startVal;
		
		locationSTR_2 = "button.php?search_str=" + document.myForm.myText.value + "&start=" + startVal;
		
		ajaxManager('load_page', locationSTR_2, 'button');
		
		ajaxManager('load_page', locationSTR_1, 'contentLYR');
		
		//setTimeout("swapGo(1)",1);
		swapGo(1);
		
		sHold = "2"
		
		$("history_index").value = 1;
		$("total_pages").value = 100;
	}
	
	function getSims(content_id) {
		swapGo(2);
		locationSTR = "results.php?search_str=" + content_id + "&type=sims";
		ajaxManager('load_page', locationSTR, 'contentLYR');
		setTimeout("swapGo(1)",1200);
		sHold = "1"
		//$("history_index").value = 1;
		//$("total_pages").value = 1;
		
	}
	
	function addToLightbox(content_id) {
		//alert(content_id);
		locationSTR = "lightbox.php?cid=" + content_id;
		ajaxManager('load_page', locationSTR, 'lightboxWin');
	}
	
	function openLightBox() {
		locationSTR = "lightbox.php";
		ajaxManager('load_page', locationSTR, 'lightboxWin');
		win2.showCenter();
	}
	
	
	function removeFromLightbox(content_id) {
		//locationSTR = "lightbox.php?rcid=" + content_id;
		//ajaxManager('load_page', locationSTR, 'lightboxWin');
		alert(content_id);
	}
	
	
	function ajaxManager()
	{
	var args = ajaxManager.arguments;
	switch (args[0])
		{
		case "load_page":
			$("history_index").value = 1;
			$("total_pages").value = 100;
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)
				{
			x.onreadystatechange = function()
					{
				if (x.readyState == 4 && x.status == 200)
						{
						el = document.getElementById(args[2]);
						el.innerHTML = x.responseText;
					}
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;
		case "start_up":
			ajaxManager('load_page', 'basic.xml', 'contentLYR');
			break;
		}
	}
