/**
Dynamic XMLHttp lookups based on Google Suggest XMLRPC code. See:
http://serversideguy.blogspot.com/2004/12/google-suggest-dissected.html
http://www.fastbugtrack.com/misc/google/ac.js
http://www.google.com/webhp?complete=1&hl=en
version 1.2 - January 5, 2005 Julian Robichaux, http://www.nsftools.com
*/


/*
function get_dom(dom_to_get)
{
	if(document.layers)// Netscape 4+
	{ dom_obj = document.layers[dom_to_get];
	dom_style = document.layers[dom_to_get].style; }
	else if(document.getElementById)// Netscape 6+, gecko, IE 5+
	{ dom_obj = document.getElementById(dom_to_get);
	dom_style = document.getElementById(dom_to_get).style; }
	else if(document.all)// IE 4+
	{ dom_obj = document.all[dom_to_get];
	dom_style = document.all[dom_to_get].style;	}
	else// Browser unknown; do nothing
	{ return; }
}
*/

function hide_dropdowns(show_type) {
	selects = document.getElementsByTagName("select");
	for (var i = 0; i < selects.length; i++) {
		selects[i].style.visibility = show_type; } }




var div_font = "Verdana";
var div_font_size = "11";
var div_padding = "0px";
var div_border = "1px solid black";
var div_bg_color = "#c0c4e4";
var div_highlight_color = "#b4b4b4";


var query_field;
var lookup_url;
var div_name;
var last_val = "";
var val = ""
var xmlHttp;
var cache = new Object();
var searching = false;
var global_div;
var div_formatted = false;


/* Expected text response should be a single line of text that calls the show_query_div function, in a format like:
show_query_div("smi", new Array("John Smith", "Mary Smith"), new Array("555-1212", "555-1234")); */
//removed passed in div name(, hidden_div_name) if (hidden_div_name) div_name = hidden_div_name;
function initial_lookup_code(query_field_name, lookup_url_passed)
{
	query_field = document.getElementsByName(query_field_name).item(0);
	query_field.onblur = _show_div; //function() { show_div(false) };
	query_field.onkeydown = keypress_handler;
	query_field.autocomplete = "off"; //need to manually set for firefox 1.0
	
	lookup_url = lookup_url_passed;
	div_name = "querydiv";
	//iframe_name = "queryiframe";
	
	/* add a blank value to the cache and start checking for changes to the field */
	add_to_cache("", new Array(), new Array());
	setTimeout("main_loop()", 100);
}


/* This is the function that should be returned by the XMLHTTP call. It will format and display the lookup results. */
function show_query_div(query_string, result_array_1, result_array_2)
{
	var div = get_div(div_name);
	
	// remove any results that are already there
	while (div.childNodes.length > 0)
	div.removeChild(div.childNodes[0]);
	
	// add an entry for each of the results in the resultArray
	for (var i = 0; i < result_array_1.length; i++)
	{
		// each result will be contained within its own div
		var result = document.createElement("div");
		result.style.cursor = "pointer";
		result.style.borderBottom = "1px solid #000000";
		result.style.padding = "3px 3px 3px 3px";
		unhighlight_result(result);
		result.onmousedown = _select_result; //function() { select_result(this) };
		result.onmouseover = _highlight_result; //function() { highlight_result(this) };
		result.onmouseout = _unhighlight_result; //function() { unhighlight_result(this) };
		
		var result_1 = document.createElement("span");
		/* result_1.className = "result_1";
		if(custom_result == 0)
		{ result_1.style.textAlign = "left";
		result_1.style.fontWeight = "bold";
		result_1.style.fontSize = div_font_size; } */
		result_1.className = "result_1";
		result_1.style.textAlign = "left";
		result_1.style.fontWeight = "bold";
		result_1.style.fontSize = div_font_size;
		result_1.innerHTML = result_array_1[i];
		
		var result_2 = document.createElement("span");
		/* result_2.className = "result_2";
		if(custom_result == 0)
		{ result_2.style.textAlign = "right";
		result_2.style.paddingLeft = "20px";
		result_2.style.fontSize = div_font_size; } */
		result_2.className = "result_2";
		result_2.style.textAlign = "right";
		result_2.style.paddingLeft = "12px";
		result_2.style.fontSize = div_font_size;
		result_2.innerHTML = result_array_2[i];
		
		
		
		result.appendChild(result_1);
		result.appendChild(result_2);
		div.appendChild(result);
	}
	
	// if this resultset isn't already in our cache, add it
	var is_cached = cache[query_string];
	if (!is_cached)
		add_to_cache(query_string, result_array_1, result_array_2);
	
	// display the div if we had at least one result
	show_div(result_array_1.length > 0);
}

/* This is a helper function that just adds results to our cache, to avoid repeat lookups. */
function add_to_cache(query_string, result_array_1, result_array_2)
{ cache[query_string] = new Array(result_array_1, result_array_2); }

/* create div for results and format */
function get_div(div_id)
{
	if(!global_div)
	{ // create div
		if(!document.getElementById(div_id))
		{ var new_node = document.createElement("div");
		new_node.setAttribute("id", div_id);
		document.body.appendChild(new_node); }
		// global_div reference
		global_div = document.getElementById(div_id);
		// input field offset
		var x = query_field.offsetLeft;
		var y = query_field.offsetTop + query_field.offsetHeight;
		var parent = query_field;
		while (parent.offsetParent)
		{ parent = parent.offsetParent;
		x += parent.offsetLeft;
		y += parent.offsetTop; }
		// add some formatting to the div, if we haven't already
		if (!div_formatted)
		{
			global_div.style.backgroundColor = div_bg_color;
			global_div.style.fontFamily = div_font;
			global_div.style.padding = div_padding;
			global_div.style.border = div_border;
			global_div.style.fontSize = div_font_size;
			
			global_div.style.position = "absolute";
			global_div.style.left = x + "px";
			global_div.style.top = y + "px";
			global_div.style.visibility = "hidden";
			global_div.style.zIndex = 10000;
			
			div_formatted = true;
		}
	}
	return global_div;
}

/* highlighting */
function _highlight_result() { highlight_result(this); }
function highlight_result(item) { item.style.backgroundColor = div_highlight_color; }
function _unhighlight_result() { unhighlight_result(this); }
function unhighlight_result(item) { item.style.backgroundColor = div_bg_color; }

/* show div */
function _show_div() { show_div(false); }
function show_div(show)
{
	var div = get_div(div_name);
	if(show)
	{ div.style.visibility = "visible";
	hide_dropdowns("hidden"); }
	else
	{ div.style.visibility = "hidden";
	hide_dropdowns("visible"); }
	//adjustiFrame(); removed and added code for hide selects
}

/* Enters selected result in field and hides div */
function _select_result() { select_result(this); }

function select_result(item)
{ var spans = item.getElementsByTagName("span");
	if(spans) {
		for (var i = 0; i < spans.length; i++) {
			if (spans[i].className == "result_1") {
				query_field.value = spans[i].innerHTML;
				last_val = val = escape(query_field.value);
				searching = false;
				main_loop();
				query_field.focus();
				show_div(false);
				return;
			}
		}
	}
}

/* Get the number of the results that's currently selected/highlighted */
function get_selected_span(div)
{
	var count = -1;
	var spans = div.getElementsByTagName("div");
	if(spans) {
		for (var i = 0; i < spans.length; i++) {
			count++;
			if (spans[i].style.backgroundColor != div.style.backgroundColor)
			return count;
		}
	}
	return -1;
}

/* Select/highlight the result at the given position */
function set_selected_span(div, span_num)
{
	var count = -1;
	var this_span;
	var spans = div.getElementsByTagName("div");
	if(spans) {
		for (var i = 0; i < spans.length; i++) {
			if (++count == span_num)
			{ highlight_result(spans[i]);
			this_span = spans[i]; }
			else
			{ unhighlight_result(spans[i]); }
		}
	}
return this_span;
}

/* This is the key handler function, for when a user presses the up arrow, down arrow, tab key, or enter key from the input field. */
function keypress_handler(evt)
{
	// if div is hidden return
	var div = get_div(div_name);
	if (div.style.visibility == "hidden")
	return true;
	
	// check for valid event variable
	if(!evt && window.event)
	{ evt = window.event; }
	var key = evt.keyCode;
	
	// if this key isn't one of the these return
	var KEYUP = 38;
	var KEYDOWN = 40;
	var KEYENTER = 13;
	var KEYTAB = 9;
	
	if((key != KEYUP) && (key != KEYDOWN) && (key != KEYENTER) && (key != KEYTAB))
	return true;
	
	// get the span that's currently selected, and perform an appropriate action
	var sel_num = get_selected_span(div);
	var sel_span = set_selected_span(div, sel_num);
	
	if((key == KEYENTER) || (key == KEYTAB))
	{
		if(sel_span)
		{ select_result(sel_span);
		evt.cancelBubble=true;
		return false; }
	}
	else
	{
		if(key == KEYUP)
			{ sel_span = set_selected_span(div, sel_num - 1); }
		if(key == KEYDOWN)
			{ sel_span = set_selected_span(div, sel_num + 1); }
		if(sel_span)
			{ highlight_result(sel_span); }
	}
	show_div(true);
	return true;
}


/* This is the function that monitors the query_field, and calls the lookup functions when the query_field value changes. */
main_loop = function() {
	val = escape(query_field.value);
	/* if the field value has changed show cached or lookup result */
	if(last_val != val && searching == false)
	{
	var cache_result = cache[val];
	if (cache_result)
		{ show_query_div(val, cache_result[0], cache_result[1]); }
	else
		{ do_remote_query(val); }
		last_val = val;
	}
	setTimeout("main_loop()", 100);
	return true;
}
;







/* This sets up the XMLHTTP object we're using for the dynamic lookups. */
function getXMLHTTP()
{
	var A = null;
	
	try
	{
		A = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
		
		try
		{
			A = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(oc){
				A = null;
		}
	}
	
	if(!A && typeof XMLHttpRequest != "undefined")
	{ A = new XMLHttpRequest(); }
	
	return A;
}


/* This actually sends the lookup request (as a URL with a query string) to a server in the background. 
When a response comes back from the server, the function attached to the onReadyStateChange event is fired off. */
function do_remote_query(query_string)
{
	searching = true;
	if(xmlHttp && xmlHttp.readyState != 0)
	{ xmlHttp.abort() }
	
	xmlHttp=getXMLHTTP();
	if(xmlHttp)
	{
		xmlHttp.open("GET", lookup_url + query_string, true);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.responseText && searching)
			{
			eval(xmlHttp.responseText);
			searching = false;
			}
		};
	xmlHttp.send(null);
	}
}

