// JavaScript Document
var cseId = "015829576795886445944:iznddmmtqdo"; //shopperosity sites
var destinationurls = new Array();
function onResultsReady()
{
	var resultsHTML = new Array();
	var resultsURL = new Array();
	//empty our stored destinations url array
	destinationurls.length=0;
	for (var i = 0; i < searcher.results.length; i++) 
	{
		var thisResult = searcher.results[i];  
		//save the visible url to pass later to the routine that will find our aff link
		resultsURL.push(thisResult.visibleUrl);
		//save the destination page so we can redirect if possible
		destinationurls.push(thisResult.url);
	   	resultsHTML.push( "<p>" );
		resultsHTML.push( "<a class='searchtitle' href='' id=myid_"+i+">" );
	    resultsHTML.push( thisResult.title );
	    resultsHTML.push( "<\/a>" );
		resultsHTML.push( "<br \/>" );
	   	resultsHTML.push( thisResult.content )
	    resultsHTML.push( "<br \/>" );
	  	resultsHTML.push( "<span class='url'>" );
	    resultsHTML.push( thisResult.visibleUrl);
	    resultsHTML.push( "<\/span> - " );
		//put in a place holder for the 'shop here' link
		resultsHTML.push( "<span id=\"my_link_" +  i + "\"><\/span>" );
	   	resultsHTML.push( "<\/p>" );
  	}      
	show_results_message(resultsHTML.join( "" ));	
	findMerchant(resultsURL);
}              

var searcher = new GwebSearch();
searcher.setSiteRestriction(cseId);
searcher.setUserDefinedLabel("Shopperosity Search");
searcher.setNoHtmlGeneration();
searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
searcher.setSearchCompleteCallback( null, onResultsReady );

function performQuery(query)
{        
	show_results_message("Searching for '" + query + "'. Please wait...");
	var display_phrase = document.getElementById("srch_phrase");
	document.title = "Search results for "+query+" | Shopperosity.com";
	display_phrase.innerHTML = "<font color=\"red\">"+query+"<\/font>";
	
	searcher.execute( query );
}

function show_results_message(display_text)
{
	var panel = document.getElementById( "resultsPanel" );
	panel.innerHTML = display_text;
}
/**AJAX portion**/
var http = createRequestObject();
var m_name="";
var m_id="";

function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
	{
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
	{
        ro = new XMLHttpRequest();
    }
    return ro;    
}

function findMerchant(url) 
{
//url is an array of the urls we need to pass to the php script
  if(http)
  {
	http.open('get', 'find_merch.php?url='+url.join(","));
    http.onreadystatechange = handleResponse;
	http.send(null);
  }
  else
  {
  	show_results_message('Your browser does not support dynamic searching.<br />');
  }
}

function handleResponse() 
{
m_name="<i>Store not found in Shopperosity network</i>";
m_id=0;
	if(http.readyState == 4)
	{
		var results = http.responseText.split(","); //put comma delimited result into array
		for(var i=0; i < (results.length/2); i++)
		{
			m_name=results[i*2];//=merch name 
			m_id=results[(i*2)+1];//=mid
			//write out what we want in the proper place
			var link_spot = document.getElementById( "my_link_"+i );
			link_spot.innerHTML = "<b><a href='go_shopping.php?mid="+m_id+"'>Shop at "+m_name+"<\/a><\/b>";

			var title_link = document.getElementById( "myid_"+i );
			if(title_link)
			{
				title_link.href = "go_shopping.php?mid="+m_id;
				title_link.href += "&url="+destinationurls[i];				
			}
		}
    }
}

