/*
 * Asynchronously grab the search results.
 */
 //  matchNumber($number, $trailing, $limit, $adult)
function full_search(number, trailing, limit, hasEnglish, hasAdult, hasLending, hasMortage, hasAutomotive, hasFinancial, hasMedical, hasOptical, hasDental, hasLegal,page, filter)
{

	// build the URL for the async call
    var url = build_url(number, trailing, limit, hasEnglish, hasAdult, hasLending, hasMortage, hasAutomotive, hasFinancial, hasMedical, hasOptical, hasDental, hasLegal, page, filter);
    
    // show the waiting message
    //show_waiting(false);
    
    // send the request asynchronously
    g_xmlHttp = getXmlHttpObjectEx();
    g_xmlHttp.onreadystatechange = search_results;
    g_xmlHttp.open("GET", url, true);
    g_xmlHttp.send(null);
}

/*
 * Handle the returned results from the async call.
 */
var g_FullSearchTimer = null;
function search_results()
{
    if(g_xmlHttp.readyState == 4 || g_xmlHttp.readyState == "complete")
    {
        
		document.getElementById("async_area").innerHTML = g_xmlHttp.responseText;
        
    }

}

/*
 * Do a search every time they enter any character
 * in the keyword box.
 */
var FULL_WAIT_TIME = 3000;  // 3 seconds to wait
var g_StartFullTimer = null;
var g_xmlHttpActive = null;
var g_Term = null;
var g_Category = null;
var g_Areas = null;
function active_search(term, form, do_full)
{
    // if not indicated, we're doing a full search after some time
    if(do_full == null) do_full = true;
    
}

function active_results()
{
    if(g_xmlHttpActive.readyState == 4 || g_xmlHttpActive.readyState == "complete")
    {   
        // just put the response in the async area
        document.getElementById("async_area").innerHTML = g_xmlHttpActive.responseText;
    }
}

/*
 * Bit of a hack to get parameters passed to the timer.
 */
function full_async_search()
{
    full_search(g_Term, g_Areas, g_Category, "0");
}

/*
 * Build the URL for an asynchronous search.
 */
function build_url(number, trailing, limit, hasEnglish, hasAdult, hasLending, hasMortage, hasAutomotive, hasFinancial, hasMedical, hasOptical, hasDental, hasLegal,page, filter)
{
    var url = "numspell_search_async.php?";
	url += "number=" + number;
    url += "&trailing=" + trailing;
    url += "&max_results=" + limit;
	if(hasEnglish>0)
		url += "&English=English";
	if(hasAdult>0)	
		url += "&Adult=Adult";
	if(hasLending>0)
		url += "&Lending=Lending";
	if(hasMortage>0)
		url += "&Mortage=Mortage";
    
	if(hasAutomotive>0)
		url += "&Automotive=Automotive";

	if(hasFinancial>0)
		url += "&Financial=Financial";

	if(hasMedical>0)
		url += "&Medical=Medical";
	if(hasOptical>0)
		url += "&Optical=Optical";
	if(hasDental>0)
		url += "&Dental=Dental";
	if(hasLegal>0)
		url += "&Legal=Legal";
	
	if(typeof(filter) != 'undefined')
	{
		// Trim the filter variable
		filter = filter.replace(/^\s*/, "").replace(/\s*$/, "")
		if (filter != '') url += "&filter=" + filter;
	}
	
	url+="&page="+page;
    return url;
}


/*
 * Show the appropriate waiting message on the screen.
 */
var g_WaitTimer = null;
function show_waiting(more)
{
    if(!g_WaitTimer)
    {
        // make sure the message is appropriate inside
        document.getElementById("waiting").innerHTML =
                    "Searching for more results <span id='please_wait'>.</span>";
                    
        // display the searching menu if we're not already
        document.getElementById("waiting").style.display = "block";
        g_WaitTimer = setInterval("waiting()", 500);
    }
}

/*
 * A function that makes the periods keep going on a
 * waiting message.
 */
var g_Dots = 1;
var MAX_DOTS = 10;
function waiting()
{
    var el = document.getElementById("please_wait");
    if(el)
    {
        // increase the number of dots by one
        g_Dots++;
        
        if(g_Dots == 10)
        {
            g_Dots = 1;
        }
        
        // create the dots string
        var str = "";
        for(var i=0; i<g_Dots; i++)
        {
            str += " . ";
        }
        
        // now make those dots visible
        el.innerHTML = str;
    }
}

