/*
 * 
 * ThrillNetwork.com Ride & Park Database Quicksearch Autocomplete
 * Programmed by: Graham Kelly <graham@thrillnetwork.com>
 *
 */
 

var popup_id = "";
var qbox_id = "";
var default_text = "";
var span_id = "";
var poptable_id = "";
var tnrpdbac_init_called = false;
var popup_open = false;
var curser_name = false;
var use_ac = true;
var select_htm = "";
var select_span_id = "";


function init_tnrpdbac(qid, pid, ptid, sid, ssid, dtext) {
  tnrpdbac_init_called = true;
	
  if (!document.getElementById) {
  	use_ac = false;
  }
  
  default_text = dtext;
  qbox_id = qid;
  
  if (use_ac) {
  	popup_id = pid;
  	span_id = sid;
  	select_span_id = ssid;
  	poptable_id = ptid;
  	
  	document.getElementById(qbox_id).value = default_text;
  	
  	var ssobj = get_select_spanobj();
  	select_htm = ssobj.innerHTML;
  	
  	var popupobj = get_popupobj();
    popupobj.style.position = "absolute";
  }
}

function query_change() {
  var qboxobj = get_qboxobj();
  q=qboxobj.value;
  
  if (use_ac && q != "" && q != default_text) {
  	send_query(q);
  } else {
  	hide_popup();
  }
}

function send_query(q) {
  if (use_ac) {
  	x_tndbac(q, get_ac_list);
  }
}

function get_ac_list (list) {
  if (use_ac) {
  	list = unpack_array(list);
  	
  	if (list != null) {
  	  htm = "";
  	  max_text_width = 0;
  	  if (list.length > 0) {
  	  	htm = "<table width=\"100%\">";
  	    for (i = 0; i < list.length; i++) {
  	  	  id_name = 'qac'+(i+1);
  	  	  l_code = list[i].replace("\"", "\\\"");
  	  	  l_code = list[i].replace('\'', '\\\'');
  	  	  
  	  	  // guess the pixel width
  	  	  //
  	  	  // this is for an IE error where it doesnt calculate
  	  	  // the pixel width of the text correctly
  	  	  cl = 12+(6.5*list[i].length);
  	  	  if (cl > max_text_width) {
  	  	  	max_text_width = cl;
  	  	  }
  	  	  
  	      htm += "<tr>";
  	      htm += "<td id=\""+id_name+"\" align=\"left\" onmouseover=\"tnrpdbac_list_rollover('"+id_name+"');\" onmouseout=\"tnrpdbac_list_rollout('"+id_name+"');\" onmousedown=\"tnrpdbac_list_click('"+id_name+"', '"+l_code+"');\">";
  	      htm += list[i];
  	      htm += "</td>";
  	      htm += "</tr>";
  	    }
  	  
  	    htm += "</table>";
  	  

  	    document.getElementById(poptable_id).width=Math.ceil(max_text_width);
  	    document.getElementById(span_id).innerHTML = htm;
  	  
  	    display_popup();
  	  } else {
  	    hide_popup();
  	  }
    } else {
  	  hide_popup();
    }
  }
}

function tnrpdbac_list_rollover(lid) {
  document.getElementById(lid).style.backgroundColor="#F1F1F1";
  
  try {
    document.getElementById(lid).style.cursor = "pointer";
  } catch(e) {
    document.getElementById(lid).style.cursor = "hand";
  }
}

function tnrpdbac_list_rollout(lid) {
  document.getElementById(lid).style.backgroundColor="";
}

function tnrpdbac_list_click(lid, val) {
  var qboxobj = get_qboxobj();
  qboxobj.value=val;
  hide_popup();
}

function display_popup() {
  if (!popup_open) {
    var qbox_boxobj = get_qbox_boxobj();
    var qboxobj = get_qboxobj();
    var popupobj = get_popupobj();
    var ssobj = get_select_spanobj();
    
    ssobj.innerHTML = "";
    
    popupobj.style.visibility="visible";
    
    var leftpx = fetch_object_posleft(qboxobj);
	var toppx = fetch_object_postop(qboxobj) + qboxobj.offsetHeight;
	
	popupobj.style.left= leftpx+"px";
    popupobj.style.top= toppx+"px";
    
    popup_open = true;
  }
}

function hide_popup() {
  if (popup_open) {
    var popupobj = get_popupobj();
    
    popupobj.style.visibility="hidden";
    popupobj.style.position = "absolute";
    
    var ssobj = get_select_spanobj();
    ssobj.innerHTML = select_htm;
    
    popup_open = false;
  }
}

function tnrpdbac_blur_handler() {
  var qboxobj=get_qboxobj();
  
  if (qboxobj.value == "") {
  	qboxobj.value=default_text;
  }
	
  window.setTimeout("hide_popup();",10);	
}

function tnrpdbac_focus_handler() {
  var qboxobj=get_qboxobj();

  if (qboxobj.value == "" || qboxobj.value == default_text) {
  	qboxobj.value="";
  }
}

function unpack_array(array_str) {
  if (array_str != "" && array_str.indexOf("[") == 0 && array_str.lastIndexOf("]") == (array_str.length-1)) {
  	array_str = array_str.slice(1);                       // remove the opening [
  	array_str = array_str.slice(0, (array_str.length-1)); // remove the closing ]
  	
  	a = new Array();
  	
  	if (array_str.indexOf("][") > -1) {
  	  a = array_str.split("][");
  	} else {
  	  a[0] = array_str;
  	}

  	return a;
  } else {
  	return null;
  }
}

function set_use_ac(v) {
  if (!tnrpdbac_init_called) {	
    use_ac = v;
  }
}

function get_popupobj() {
  return document.getElementById(popup_id);
}

function get_qboxobj() {
  return document.getElementById(qbox_id);
}

function get_qbox_boxobj() {
  return document.getElementById(qbox_id+"_box");
}

function get_select_spanobj() {
  return document.getElementById(select_span_id);
}


function fetch_object_posleft(elm)
{
	var left = elm.offsetLeft;
	while((elm = elm.offsetParent) != null)
	{
		left += elm.offsetLeft;
	}
	return left;
}

function fetch_object_postop(elm)
{
	var top = elm.offsetTop;
	while((elm = elm.offsetParent) != null)
	{
		top += elm.offsetTop;
	}
	return top;
}