var xmlhttp;

function start_browse_type(str)
  {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="browse_query.php";
	url=url+"?browse_type="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
			document.getElementById("geo_b").innerHTML = "<a href=\"#\" class=\"search\" name=\"Geography_Browse\" onclick=\"start_browse_type(this.name);return false;\">Browse By Location</a>";
			document.getElementById("geo_s").innerHTML = "<a href=\"#\" class=\"search\" name=\"Geography_Search\" onclick=\"start_browse_type(this.name);return false;\">Search By Location</a>";
			document.getElementById("nam_s").innerHTML = "<a href=\"#\" class=\"search\" name=\"Name_Search\" onclick=\"start_browse_type(this.name);return false;\">Search By Name</a>";
			document.getElementById("adv_s").innerHTML = "<a href=\"#\" class=\"search\" name=\"Advanced_Search\" onclick=\"start_browse_type(this.name);return false;\">Advanced Search</a>";
			if (str == "Geography_Browse")
				document.getElementById("geo_b").innerHTML = "<a href=\"#\" class=\"search\" id=\"chosen\" name=\"Geography_Browse\" onclick=\"start_browse_type(this.name);return false;\">Browse By Location</a>";
			else if (str == "Geography_Search")
				document.getElementById("geo_s").innerHTML = "<a href=\"#\" class=\"search\" id=\"chosen\" name=\"Geography_Search\" onclick=\"start_browse_type(this.name);return false;\">Search By Location</a>";
			else if (str == "Name_Search")
				document.getElementById("nam_s").innerHTML = "<a href=\"#\" class=\"search\" id=\"chosen\" name=\"Name_Search\" onclick=\"start_browse_type(this.name);return false;\">Search By Name</a>";
			else if (str == "Advanced_Search")
				document.getElementById("adv_s").innerHTML = "<a href=\"#\" class=\"search\" id=\"chosen\" name=\"Advanced_Search\" onclick=\"start_browse_type(this.name);return false;\">Advanced Search</a>";
		  }
	  }
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  }

function browse_geography(str)
  {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="../browse_query.php";
	url=url+"?"+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
		  }
	  }
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  }

function submit_location(location)
  {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="../browse_query.php";
	url=url+"?browse_type=Geography_Search&location="+location;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
		  }
	  }
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  }

function submit_name(name)
  {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="../browse_query.php";
	url=url+"?browse_type=Name_Search&name="+name;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
		  }
	  }
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  }


function GetXmlHttpObject()
  {
	if (window.XMLHttpRequest)
	  {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
  }

$(document).ready(function()
  {
	$("#geo_b").hover(
		function () {$("#geo_search").hide(); $("#nam_search").hide(); $("#adv_search").hide(); $("#geo_browse").fadeIn("slow"); $("#initial_text").hide();}, 
		function () {}
	);
	$("#geo_s").hover(
		function () {$("#geo_browse").hide(); $("#nam_search").hide(); $("#adv_search").hide(); $("#geo_search").fadeIn("slow"); $("#initial_text").hide();}, 
		function () {}
	);
	$("#nam_s").hover(
		function () {$("#geo_browse").hide(); $("#geo_search").hide(); $("#adv_search").hide(); $("#nam_search").fadeIn("slow"); $("#initial_text").hide();},
		function () {}
	);
	$("#adv_s").hover(
		function () {$("#geo_browse").hide(); $("#geo_search").hide(); $("#nam_search").hide(); $("#adv_search").fadeIn("slow"); $("#initial_text").hide();}, 
		function () {}
	);

  });

function locationlookup(select)
  {
	if(select.length == 0)
	  {
		// Hide the suggestion box.
		$('#suggestions').hide();
	  }
	else
	  {
		$.post("ajax/search/location_search.php", {queryString: ""+select+""}, function(data){
		if(data.length >0)
		  {
			$('#suggestions').show();
			$('#autoSuggestionsList').html(data);
		  }
	  });
		}
  } // lookup
	
function fill_full(thisValue)
  {
	$('#select_full_location').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
  } 

//Functions for hiding/showing content on pages
 $(function ()
  {
    $("#add").hide();

	$("#trigger_add").click(function(event)
	  {
		event.preventDefault();
		$("#add").slideToggle();
		$("#select").slideToggle();
	  });

  });

// Used to toggle in and out separate fields
function switch_entry()
  {
	$("#add").slideToggle("fast");
	$("#select").slideToggle("fast");
  }

function namelookup(select)
  {
	if(select.length == 0)
	  {
		// Hide the suggestion box.
		$('#suggestions').hide();
	  }
	else
	  {
		$.post("ajax/search/name_search.php", {queryString: ""+select+""}, function(data){
		if(data.length >0)
		  {
			$('#suggestions').show();
			$('#autoSuggestionsList').html(data);
		  }
	  });
		}
  } // lookup

function fill_name(thisValue)
  {
	$('#select_name').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
  } 




function getXMLHTTP()
  { //fuction to return the xml http object
	var xmlhttp=false;
	try
	  {
		xmlhttp=new XMLHttpRequest();
	  }
	catch(e)
	  {		
		try
		  {
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		  }
		catch(e)
		  {
			try
			  {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			  }
			catch(e1)
			  {
				xmlhttp=false;
			  }
		  }
	  }
	return xmlhttp;
  }
	
function getState(countryId)
  {
	var strURL="ajax/search/findState.php?country="+countryId;
	var req = getXMLHTTP();

	if (req)
	  {
		getCity("Reset", "Reset");

		req.onreadystatechange = function()
		  {
			if (req.readyState == 4)
			  {
				// only if "OK"
				if (req.status == 200)
				  {
					document.getElementById('statediv').innerHTML=req.responseText;
				  }
			  }
		  }
		req.open("GET", strURL, true);
		req.send(null);
	  }		
  }

function getCity(countryId,stateId)
  {
	var strURL="ajax/search/findCity.php?country="+countryId+"&state="+stateId;
	var req = getXMLHTTP();

	if (req)
	  {
		req.onreadystatechange = function()
		  {
			if (req.readyState == 4)
			  {
				// only if "OK"
				if (req.status == 200)
				  {
					document.getElementById('citydiv').innerHTML=req.responseText;
				  }
			  }
		  }
		req.open("GET", strURL, true);
		req.send(null);
	  }
  }

function toggle_location()
  {
	$('#use_location').toggle();
	$('#remove_location').toggle();
	if (document.getElementById('remove_location').style.display != "inline")
		document.getElementById('general_location_use').value = "1";
	else
		document.getElementById('general_location_use').value = "0";
  }

function toggle_specific_location()
  {
	$('#use_specific_location').toggle();
	$('#remove_specific_location').toggle();
	if (document.getElementById('remove_specific_location').style.display != "inline")
		document.getElementById('specific_location_use').value = "1";
	else
		document.getElementById('specific_location_use').value = "0";
  }

function toggle_keyword()
  {
	$('#use_keyword').toggle();
	$('#remove_keyword').toggle();
	if (document.getElementById('remove_keyword').style.display != "inline")
		document.getElementById('keyword_use').value = "1";
	else
		document.getElementById('keyword_use').value = "0";
  }

function toggle_name()
  {
	$('#use_name').toggle();
	$('#remove_name').toggle();
	if (document.getElementById('remove_name').style.display != "inline")
		document.getElementById('name_use').value = "1";
	else
		document.getElementById('name_use').value = "0";
  }

function append_location()
  {
	var newword = document.getElementById("select_full_location").value;
	if (newword.length > 5)
	  {
		var result = ""; 
		for (var i = 0; i < document.getElementById("my_select").length; i++)
		  { 
			if (document.getElementById("my_select").options[i].value == newword)
			  { 
				result += "\n " + document.getElementById("my_select").options[i].value; 
			  } 
		  } 
		if (result == "")
		  {
			$('#already_entered_location').hide();
			var new_option = document.createElement("option");  //creates a new <option>
			var text = document.createTextNode(newword);  //creates the displayed text
			new_option.appendChild(text);  //puts the text between the <option> tags
			new_option.setAttribute("value", newword);  //gives the option a value attribute equal to "hat"
			document.getElementById("my_select").appendChild(new_option); //appends the <option> to the end of the list
			document.getElementById("list_items").style.display = "inline";
			document.getElementById("list_items_title").style.display = "inline";
		  }
		else
			$('#already_entered_location').show();

		document.getElementById("select_full_location").value = "";
	  }
  }

function removeOptions()
  {
	var dropdownIndex = document.getElementById('my_select').selectedIndex;
	var dropdownValue = document.getElementById('my_select')[dropdownIndex].value;
	document.getElementById('my_select').remove(dropdownValue);
	if (document.getElementById('my_select').options.length == 0)
	  {
		document.getElementById("list_items").style.display = "none";
		document.getElementById("list_items_title").style.display = "none";
	  }
  }

function countSelected(select,maxNumber)
  {
	var selectedOptions = [];
	for(var i=0; i<select.options.length; i++)
	  {
		if(select.options[i].selected && !new RegExp(i,'g').test(selectedOptions.toString()))
		  {
			selectedOptions.push(i);
		  }
		if(!select.options[i].selected && new RegExp(i,'g').test(selectedOptions.toString()))
		  {
			selectedOptions = selectedOptions.sort(function(a,b){return a-b});  
       		for(var j=0; j<selectedOptions.length; j++)
       		  {
         		if(selectedOptions[j] == i)
         		  {
					selectedOptions.splice(j,1);
				  }
			  }
		  }
		if(selectedOptions.length > maxNumber)
		  {
			var throwAlert = true;
			select.options[i].selected = false;
			selectedOptions.pop();
		  }  
	  }            
	if(throwAlert == true)
	  {
        document.body.focus();
	  }
  }

function advanced_search()
  {
	var country = document.getElementById("country").value;
	var state = document.getElementById("select_state").value;
	var city = document.getElementById("select_city").value;
	var general_location_use = document.getElementById("general_location_use").value;
	var specific_locations=new Array();
		for (var i = 0; i < document.getElementById("my_select").length; i++)
		  { 
			specific_locations[i] = document.getElementById("my_select").options[i].value; 
		  }
	var specific_location_use = document.getElementById("specific_location_use").value;
	var chosen_keywords=new Array();
		for (var i = 0; i < document.getElementById("keywords_list").length; i++)
		  { 
			if (document.getElementById("keywords_list").options[i].selected)
			  {
				chosen_keywords.push(document.getElementById("keywords_list").options[i].value);
			  }
		  }
	var keyword_use = document.getElementById("keyword_use").value;
	var nameholder = document.getElementById("name").value;
	var names=new Array();
	names = nameholder.split(',');
	var name_use = document.getElementById("name_use").value;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="browse_query.php";
	url=url+"?browse_type=Advanced_Search&query=submitted";
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
		  }
	  }

	xmlhttp.open("POST", url, true)
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send('names='+names+'&keywords='+chosen_keywords+'&specific_locations='+specific_locations+'&country='+country+'&state='+state+'&city='+city+'&general_location_use='+general_location_use+'&specific_location_use='+specific_location_use+'&keyword_use='+keyword_use+'&name_use='+name_use)
  }

function resort(sorting)
  {
	var query = document.getElementById("stringQuery").value;
	var queryname = document.getElementById("name").value;
	var format = document.getElementById("format").value;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		alert ("Browser does not support HTTP Request");
		return;
	  }
	var url="../ajax/search/resort.php";
	url=url+"?sorting="+sorting+"&sid="+Math.random();
	if (document.getElementById("country"))
	  {
		url = url+"&country="+document.getElementById("country").value;
	  }
	if (document.getElementById("state"))
	  {
		url = url+"&state="+document.getElementById("state").value;
	  }
	if (document.getElementById("city"))
	  {
		url = url+"&city="+document.getElementById("city").value;
	  }

	xmlhttp.onreadystatechange=
	function stateChanged()
  	  {
		document.getElementById("loading_results").innerHTML = "<img src=images/ajax_loader.gif>";
		if (xmlhttp.readyState==4)
		  {
//			document.getElementById("query_results").innerHTML=xmlhttp.responseText;
//			document.getElementById("results_listings_table").innerHTML=xmlhttp.responseText;
			document.getElementById("results_listings_table_div_tag").innerHTML=xmlhttp.responseText;
			document.getElementById("loading_results").innerHTML = "";
		  }
	  }
	xmlhttp.open("POST", url, true)
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send('query='+query+"&name="+queryname+"&format="+format)
  }
