function createHttpRequestObject() 
{
	var xmlHttp;
	
	try
    {   
		// Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	}
  	catch (e)
    {    
		// Internet Explorer    
		try
    	{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
    	catch (e)
      	{
			try
        	{        
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
      		catch (e)
        	{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}      
		}    
	}
	
	return xmlHttp;
}

var navigatingCurrentResults = false;
var paginationParams = "";

function navigateResults(params) 
{
	//Set pagination params
	paginationParams = params;
	navigatingCurrentResults = true;
	
	//Do new AJAX search
	doAJAXSearch();
	
	//Clear params
	paginationParams = "";
	navigatingCurrentResults = false;
}

function doAJAXSearch(pathPrepend)
{  
	var searchingHTML = '<br><br><table cellpadding="20" cellspacing="0" width="75%" border="1" borderColor="#0153a5" align="center"><tr><td class="text" align="center"><b>Searching...</b><br><img src="' + pathPrepend + 'img/searching.gif" width="100" height="10"></td></tr></table>';
	
	//First set "Searching" message/image
	document.getElementById("resultsDiv").innerHTML = searchingHTML;
	
	var xmlHttp = createHttpRequestObject();
	
	if (xmlHttp != null) {
    
	xmlHttp.onreadystatechange=function()
    	{
      		if(xmlHttp.readyState == 4)
        	{
        		var fullPage = xmlHttp.responseText;
				var bodyStartIndex = fullPage.lastIndexOf("<body>");
				var bodyEndIndex = fullPage.indexOf("</body>");
				
				var justTheBody = xmlHttp.responseText.substring(bodyStartIndex + 6, bodyEndIndex);
				
				var searchHeader = "<table borderColor=\"#0153a5\" cellSpacing=\"0\" cellPadding=\"10\" width=\"100%\" bgColor=\"#eeeeff\" border=\"1\"><tr><td class=\"text\">Below you will find the results of your custom search.  <b>www.education-destination.org</b> is committed to utilizing our high-tech search technology to help you reach your goals.  Refine your search further by selecting different options to the right of the page.</td></tr></table>";
				
				document.getElementById("resultsDiv").innerHTML = searchHeader + justTheBody;
				
				reformatResults(pathPrepend);
        	}
      	}
		
	var hostName = (window.location.href.indexOf("http://www.education") != -1) ? "http://www.education-destination.org" : "http://education-destination.org";
    
	var requestUrl = hostName + "/search.aspx?url=" + buildSearchUrl();
	
	xmlHttp.open("GET", requestUrl, true);
	xmlHttp.send(null);
	
	}
	else 
	{
		alert("Unable to create AJAX connection");
	}
}

function reformatResults(pathPrepend) 
{
	//Hide "Refine Your Search" search fields included in results
	document.getElementById("search").style.display = "none";
	
	//Reformat links
	for (var i = 0; i < document.links.length; i++) 
	{
		var linkHref = document.links[i].href;
		
		if (linkHref.indexOf("results.jsp") != -1 && linkHref.indexOf("&aff=results.jsp") == -1)
		{
			var paramStartIndex = linkHref.indexOf("?");
			var params = linkHref.substring(paramStartIndex + 1); //don't include the '?'
			var newLink = "javascript:navigateResults('&" + params + "');"; //prepend a '&' to params
			
			document.links[i].href = newLink;
		}
	}
	
	//Reformat logo image paths
	for (var i = 0; i < document.images.length; i++) 
	{
		var imgSrc = document.images[i].src;
		
		if (imgSrc.indexOf("logos/") != -1) 
		{
			//alert("logo!");
			var logoPathIndex = imgSrc.indexOf("logos/");
			var newPath = imgSrc.substring(logoPathIndex);
			document.images[i].src = pathPrepend + newPath;
		}
	}
	
	do_onload();
}

function buildSearchUrl() 
{
	var onlineSelection = "either"; //default to either
		
	for (var i = 0; i < document.searchWidgetForm.ct.length; i++)
	{
		if (document.searchWidgetForm.ct[i].checked)
		{
			onlineSelection = document.searchWidgetForm.ct[i].value;
		}
	}

	//This is the generic url.  Don't use this unless we don't want to be paid. :)
	//var paramString = "http://www.guidetopremierschools.com/results.jsp";
	
	//This is the real url specific to us.
	var paramString = "http://v.vutyls.com/cgi/r?;n=203;c=249395;s=4854;x=7936;f=200701181049480;u=j;z=TIMESTAMP;k=http://www.guidetopremierschools.com/results.jsp";
	
	
	if (!navigatingCurrentResults) 
	{
		paramString += "&pc=" + document.searchWidgetForm.pc.value //use "&" since these are all params (including the url)
			+ "&sub=" + document.searchWidgetForm.sub.options[document.searchWidgetForm.sub.selectedIndex].value
			+ "&qual=" + document.searchWidgetForm.qual.options[document.searchWidgetForm.qual.selectedIndex].value
			+ "&ct=" + onlineSelection;
	} else {
		paramString += paginationParams;
	}
		
	return paramString;
}




