
var xmlhttp = null;

function getXMLHttpRequest()
{
  	var httpRequest = null;

  	// Create the appropriate HttpRequest object for the browser.
  	if (window.XMLHttpRequest != null)
    	httpRequest = new XMLHttpRequest();
	else if(window.ActiveXObject) 
	{
		try 
		{
        	httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
     	} 
     	catch(e) 
     	{
      	try {
          		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		httpRequest = null;
        	}
		}
	}

	// Display an error if we couldn't create one.
  	if (httpRequest == null)
    	alert("Error in HttpRequest():\n\n"+ "Cannot create an XMLHttpRequest object.");

  	// Return it.
  	return httpRequest;
}

function searchReadyStateChange()
{
	// Check the ready state.
 	alert("State = "+xmlhttp.readyState);
  	if (xmlhttp.readyState == 4)
  	{
 	alert("Change");
 		if (xmlhttp.status == 200)
    	{
			try
			{
			  	var result = xmlhttp.responseText;
				var search = document.getElementById('search');
				search.innerHTML = result;
			}
			catch (ex)
			{	
				alert("Error...");
			}
    	}
    	else
    	{
    		alert("Search result coule not be displayed at the moment. Please try again later. "+ xmlhttp.statusText);
    	}
  	}
}

function checkSearch(e)
{
	var characterCode;	
	if (e && e.which)
	{
		e=e;
		characterCode=e.which;
	}
	else
	{
		e=event;
		characterCode=e.keyCode;
	}
	
	if(characterCode==13)
	{
		// setCookieForSearch();
		execSearch();
		return false;
	}
	else
	{
		return true;
	}
}

	function readCookie(name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}


function checkCookie_Search() //Name clash with checkCookie in the PD_UserRegForm.js 
{
 	var cookieVal=  readCookie("searchTextValue"); 
	var searchTextVal;
	var baynoteFlag = readCookie("baynote_activated");

//	alert(baynoteFlag);

	if(null != cookieVal)  
	{
		if (window.console)
		{
			window.console.log("cookie returned " + cookieVal);	
		}			

		// bnResourceManager.waitForResource("baynoteObserver_handler", "bnObserver.queryOccurred('"+cookieVal+"')");
		
		// Cookie was found
		var googleURL ="http://search.pillardata.com/search?ie=&site=my_collection&output=xml_no_dtd&client=my_collection&lr=&proxystylesheet=my_collection&oe=&q=" + cookieVal + "&restrict=USSite";

		// getSearchResults(googleURL);
		
//		if (baynoteFlag != null && baynoteFlag == "true")
//		{
//			alert("Baynote");
			googleURL ="/baynote/results.jsp?q=" + cookieVal+"&filter=us";
//		}

		var objSearchFrame = document.getElementById("searchFrame");
		if(null != objSearchFrame)
		{
			objSearchFrame.src = googleURL ;
			document.searchForm.searchtext.value =  cookieVal;
		}
		
		eraseCookie("searchTextValue"); 
	} 
	else
	{
		if (window.console)
		{
			window.console.log("cookieVal was null");
			window.console.log("googleURL was " + googleURL);
		}			 		
	}
}

	function createCookie(name,value,days)
	{
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function eraseCookie(name)
	{
	createCookie(name,"",-1);
	}

	function setCookieForSearch()
	{
		var exVal = document.searchForm.searchtext.value;
		if (window.console)
		{
			window.console.log("form returned " + exVal);	
		}

	   setCookie("searchTextValue",exVal ,1);
		 
		 window.location.href = "/search/index.shtm";
		
		 //window.location.href = "index_test.shtm";
	}

	function execSearch()
	{
		var exVal = document.searchForm.searchtext.value;
		var googleURL ="http://search.pillardata.com/search?ie=&site=my_collection&output=xml_no_dtd&client=my_collection&lr=&proxystylesheet=my_collection&oe=&q=" + exVal+ "&restrict=USSite";	
		googleURL ="/baynote/results.jsp?q=" + exVal+ "&filter=us";
		document.location=googleURL;
	}

	function setCookie(name,value,days)
	{
		if (window.console)
		{
			window.console.log("setCookie with " + name + " " + value);	
		}		
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
