 // == globals ==
 var __LAST_QUERY_URL = "";
 // == build_result_link() ==
 function build_result_link( link_code_s, query_func ) {
	var ary = link_code_s.split("|");
	var end = ary.length;
	var html = "";
	if ( end > 1 ) {
		html = "Filter by Area Code: <SELECT onchange='"+query_func+"(select_get_selected(this).value,true)'>\n";
		for ( var i = 0; i < end; i++ ) {
			if ( html != "" ) {
				if ( (i%12) == 0 )
					html += "<br>";
				else	
					html += " | ";
			}				
			html += "<OPTION value='"+ary[i]+"'>"+ary[i]+"</OPTION>\n";
		}
		html += "</SELECT>\n";
	}
	return html;
 }
 // == build_export() ==
 function build_export() {
 	var html = "<A href='"+url_concat(__LAST_QUERY_URL,"export=print")+"' target='_blank'><IMG border='0' src='images/print2.gif'> print</A>";		
	html += "&nbsp;&nbsp;<A href='"+url_concat(__LAST_QUERY_URL,"export=excel")+"' target='_blank'><IMG border='0' src='images/excel.gif'> export</A>";		
	return html;
 }
 // == build_result() ==
 function build_result( response_text, query_func ) {
	var ary = response_text.split("\n\n");
	var result_obj = get_o("result");
	if ( ary[0] == "ERROR" ) {
		result_obj.innerHTML = ary[1];
	} else if ( ary[0] == "OK" ) {
		var link_html = build_result_link(ary[1],query_func);
		if ( link_html != "" )
			get_o("link").innerHTML = link_html+"&nbsp;&nbsp;&nbsp;";
		get_o("export").innerHTML = build_export();
		result_obj.innerHTML = ary[2];
	} else {
		result_obj.innerHTML = "Unspecified error.";				
	}	
 }
 // == show_loading() ==
 function show_loading( msg ) {
	if ( msg == undefined )
		msg = "Loading query results...";
	get_o("result").style.display = "none";		
    get_o("result").innerHTML = "<strong>"+msg+"</strong>";
	get_o("result").style.display = "";		
 }
 // == hide_loading() ==
 function hide_loading() {
    get_o("result").innerHTML = "";
 }
 // == query_all() ==
 function query_all( area_code, is_no_head ) {
	if ( is_no_head == undefined )
		get_o("link").innerHTML = "";
	show_loading("Loading ALL access numbers...");		
    var url = "query.aspx";
    if ( area_code != undefined ) {
		url = url_concat(url,"act=area_code&ac="+escape(area_code));
		if ( is_no_head != undefined )
			url += "&nh=1";
	}			
	query_ajax(url,"query_all");		
 }
 // == query_st() ==
 function query_st( area_code, is_no_head ) {
	var st = get_o("state").value;
	if ( st == "ALL" ) {
		query_all(area_code,is_no_head);
		return;
	}
	if ( is_no_head == undefined )
		get_o("link").innerHTML = "";
	show_loading("Loading access numbers for "+st+" state...");		
    var url = "query.aspx?act=state&st="+escape(st);
    if ( area_code != undefined )
		url += "&ac="+escape(area_code);
	if ( is_no_head != undefined )
		url += "&nh=1";
//	document.write(url); return;
	query_ajax(url,"query_st");		
 }
 // == query_ac() ==
 function query_ac( state_code, is_no_head ) {
    var obj = select_get_selected(get_o("area_code"));
    var ac = "";
    if ( trim(obj.value) != "ALL" )
		ac = obj.text;
	if ( ac == "" ) {
		query_all(state_code,is_no_head);
		return;
	}
	if ( is_no_head == undefined )
		get_o("link").innerHTML = "";
	show_loading("Loading access numbers for "+ac+" area code...");		
    var url = "query.aspx?act=area_code&ac="+escape(ac);
    if ( state_code != undefined )
		url += "&st="+escape(state_code);
	if ( is_no_head != undefined )
		url += "&nh=1";
	query_ajax(url,"query_ac");		
 }
 // == query_phone() ==
 function query_phone( ac ) {
	if ( ac == undefined ) 
		ac = get_o("phone_ac").value;
	if ( ac == undefined || ac == "" ) {
	   alert("Please enter your phone number.");
	   return;
	}
	get_o("link").innerHTML = "";
	show_loading("Loading access numbers for "+ac+" area code...");		
    var url = "query.aspx?act=area_code&ac="+escape(ac);
	query_ajax(url,"query_ac");		
 }
 // == query_ajax() == 
 function query_ajax( url, func ) {
	__LAST_QUERY_URL = url;		
    var http = ajax_http();
    http.open("GET",url);
    http.onreadystatechange = function () {
        if ( http.readyState == 4 )
			build_result(http.responseText,func);
        delete http;
    };
    http.send(null);
 }
 // == sort_asc() ===
 function sort_asc( col_name ) {
	var url = __LAST_QUERY_URL;
	url = set_var("sort",col_name,url);
	url = set_var("asc","1",url);
	get_o("link").innerHTML = "";
	show_loading("Sorting by "+col_name+" in ASCENDING order...");		
	query_ajax(url,"query_ac");
 }
  // == sort_desc() ===
 function sort_desc( col_name ) {
	var url = __LAST_QUERY_URL;
	url = set_var("sort",col_name,url);
	url = set_var("asc","0",url);
	get_o("link").innerHTML = "";
	show_loading("Sorting by "+col_name+" in DESCENDING order...");		
	query_ajax(url,"query_ac");
 }