function clearlivesearch(tab) {
	document.getElementById("livesearch1").style.display = "none";
	document.getElementById("livesearch2").style.display = "none";
	var advancedform = document.getElementById("advancedsearch");
	advancedform.tab.value = tab;
//alert(advancedform.tab.value);
}

function searchtypetab(selectedtab) {
	if (selectedtab == "residential") {
		document.getElementById("content1a1").style.display = "block";
		document.getElementById("content1a2").style.display = "none";
		document.getElementById("content1a3").style.display = "none";
	}
	else if (selectedtab == "lots") {
		document.getElementById("content1a1").style.display = "none";
		document.getElementById("content1a2").style.display = "block";
		document.getElementById("content1a3").style.display = "none";
	}
	else if (selectedtab == "rental") {
		document.getElementById("content1a1").style.display = "none";
		document.getElementById("content1a2").style.display = "none";
		document.getElementById("content1a3").style.display = "block";
	}
}

function checknewlistingoption(checkboxid) {
	if (document.getElementById(checkboxid).checked) {
		document.getElementById(checkboxid+"days").disabled = true;
	}
	else {
		document.getElementById(checkboxid+"days").disabled = false;
	}
}
/*
function citysearch(key) {
	
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/citysearch.php?city=" + key,
            // Called when the page loaded successfully
             load: function (data) {
		  if (data == "") {
			//return false;	
			$('#popup-nolocation-found').modal();
		  }
		  return data;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId("livesearch1").innerHTML =
                            "An error occurred, with response: " + data;
		     return data;
             }
       });
}
*/

function location_selected(livesearchdiv, textbox, text) {
	if (livesearchdiv == "citysearch") {
		document.getElementById('iframediv').style.display = "none";
	}
	document.getElementById(livesearchdiv).style.display = "none";	
	document.getElementById(textbox).value = text;
}

function dokeydown(e, resultdiv) { //prevent tab and enter key from default actions
  	var e = window.event || e;
	if (/^\s*$/.test(document.getElementById(resultdiv+'-textbox').value) == true) {
		return;
	}
	if (e.keyCode != 9 && e.keyCode != 13) {
		return;
	}
	if (e.preventDefault) {
		e.preventDefault(); //FF
	}
	else {
		e.returnValue = false;
	}
	if (resultdiv == "citysearch") {
		document.getElementById('iframediv').style.display = "none";
	}
	document.getElementById(resultdiv).style.display = 'none';
	document.getElementById(resultdiv+'-textbox').value = '';
}

function dokey(e, resultdiv) {
  var e = window.event || e;
if (e.preventDefault) { e.preventDefault();} else { e.returnValue = false; };
  //e.prevenDefault();
  //find current pointer
  var key = e.keyCode;
  var divnode = document.getElementById(resultdiv);
  var spannodes = divnode.getElementsByTagName('span');
  var currentnodeindex = -1;
  for(var i=0; i<spannodes.length; i++) {
//alert(selectednode.innerHTML);
//alert(selectednode.style.backgroundColor);
	if (spannodes[i].style.backgroundColor == '#ccc' || spannodes[i].style.backgroundColor == 'rgb(204, 204, 204)') {
		currentnodeindex = i;
		break;
	}
  }
  if (key == 38) {  //arrow up
	for(var i=0; i<spannodes.length; i++) {
		spannodes[i].style.backgroundColor = '#fff';
	}
	currentnodeindex = currentnodeindex > 0 ? currentnodeindex-1 : currentnodeindex;	
	spannodes[currentnodeindex].style.backgroundColor = '#ccc';
  }
  else if (key == 40) {  //arrow down
	for(var i=0; i<spannodes.length; i++) {
		spannodes[i].style.backgroundColor = '#fff';
	}
	currentnodeindex = currentnodeindex < spannodes.length-1 ? currentnodeindex+1 : currentnodeindex;	
	spannodes[currentnodeindex].style.backgroundColor = '#ccc';
  }
  else if (key == 13 || key == 9) {  //enter or tab
	if (resultdiv == 'citysearch') {
		document.getElementById('iframediv').style.display = "none";
		document.getElementById(resultdiv+'-textbox').focus();
		if (currentnodeindex < 0) return;
		if (spannodes[currentnodeindex].innerHTML != 'No match found.') {
			addcity(spannodes[currentnodeindex].innerHTML);
		}
	}
	else { //advanced search page
		if (currentnodeindex < 0) return;
		if (resultdiv == 'livesearch1') {
			//addlocation(spannodes[currentnodeindex].innerHTML);
			addlocation_errorcheck(spannodes[currentnodeindex].innerHTML);
		}
		else {
			getnearbycities(spannodes[currentnodeindex].innerHTML, 5);
		}
	}
  }
  else if (key == 27) { //escape
	if (resultdiv == 'citysearch') {
		document.getElementById('iframediv').style.display = "none";
	}
  	divnode.style.display = 'none';
  }
  else {
	if (resultdiv == 'citysearch') {
		document.getElementById('iframediv').style.display = "block";
	}
	getcitylist(document.getElementById(resultdiv+'-textbox').value, resultdiv);
  }
  return false;
}

function getcitylist(key, resultdiv) { //search key, livesearch div id
  if (key == "") {
	if (resultdiv == 'citysearch') {
		document.getElementById('iframediv').style.display = "none";
	}
	document.getElementById(resultdiv).style.display = "none";
	return;
  }
  else {
	document.getElementById(resultdiv).style.display = "block";
	document.getElementById(resultdiv).style.zIndex = 100;
//alert(document.getElementById(resultdiv).style.zIndex);
  }
  var searchscript = "citysearch.php?key=" + key;
  if (/\d+/.test(key)) {
	searchscript = "zipcodelistsearch.php?zipcode=" + key;
  }
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/" + searchscript,
            // Called when the page loaded successfully
             load: function (data) {
		     var results = data.split("@@");
		     var citylisthtml = "";
		     var iframeheight = results.length == 1 ? 0 : results.length * 13;
		     if (resultdiv == "citysearch") {	
			     document.getElementById("iframebody").style.height = iframeheight + "px";
		     }
		     for(var i = 0; i < results.length; i++) {
			if (resultdiv == 'citysearch') { //searchbox
			   citylisthtml += "<span style=\"background-color:#fff; width:256px; display:block;\" onmouseover=\"this.style.backgroundColor = '#ccc'\" onmouseout=\"this.style.backgroundColor = '#fff';\" onclick=\"location_selected('" + resultdiv + "', '" + resultdiv + "-textbox', ''); addcity('"+results[i]+"');\">" + results[i] + "</span>\n";
			}
			else {  //advanced search
			   citylisthtml += resultdiv == 'livesearch1' ? "<span style=\"background-color:#fff; width:256px; display:block;\" onmouseover=\"this.style.backgroundColor = '#ccc'\" onmouseout=\"this.style.backgroundColor = '#fff';\" ondblclick=\"addlocation_errorcheck('" + results[i] + "');\" onclick=\"location_selected('" + resultdiv + "', '" + resultdiv + "-textbox', '" + results[i] + "'); addlocation_errorcheck('" + results[i] + "');\">" + results[i] + "</span>\n" :  "<span style=\"background-color:#fff; width:256px; display:block;\" onmouseover=\"this.style.backgroundColor = '#ccc'\" onmouseout=\"this.style.backgroundColor = '#fff';\" onclick=\"location_selected('" + resultdiv + "', '" + resultdiv + "-textbox', '" + results[i] + "'); getnearbycities('" + results[i] + "', 5);\" ondblclick=\"getnearbycities('" + results[i] + "', 5);\">" + results[i] + "</span>\n";	
			}
		     }
		     dojo.byId(resultdiv).style.display = "block";
                     dojo.byId(resultdiv).innerHTML = citylisthtml;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId(resultdiv).innerHTML =
                            "An error occurred, with response: " + data;
             }
       });
}


function getzipcodelist(key, resultdiv) {
  if (key == "") {
	if (resultdiv == "citysearch") {
		document.getElementById('iframediv').style.display = "none";
	}
	document.getElementById(resultdiv).style.display = "none";
	return;
  }
  else {
	document.getElementById(resultdiv).style.display = "block";
  }
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/zipcodelistsearch.php?zipcode=" + key,
            // Called when the page loaded successfully
             load: function (data) {
		     var results = data.split("@@");
		     var ziplisthtml = "";
		     for(var i = 0; i < results.length; i++) {
			   ziplisthtml += "<span style=\"background-color:#fff; width:256px; display:block;\" onmouseover=\"this.style.backgroundColor = '#ccc'\" onmouseout=\"this.style.backgroundColor = '#fff';\" onclick=\"location_selected('" + resultdiv + "', '" + resultdiv + "-textbox', ''); addcity('"+results[i]+"');\">" + results[i] + "</span>\n";
		     }
		     dojo.byId(resultdiv).style.display = "block";
                     dojo.byId(resultdiv).innerHTML = ziplisthtml;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId(resultdiv).innerHTML =
                            "An error occurred, with response: " + data;
             }
       });
}

function updateselectedlocation(tbody) {
	var tbody1div = document.getElementById("selected_location_table2");
	if (tbody1div == null) {
		return;
	}
	var tbody1 = document.getElementById("selected_location_table2").getElementsByTagName("TBODY")[0];
	tbody1div.removeChild(tbody1);
	
	var newtbody = tbody.cloneNode(true);
	tbody1div.appendChild(newtbody);

	var selected_comm = new Array();
	var count = 0;
	var tr_nodes = tbody.getElementsByTagName("TR");
	for(var i=0; i<tr_nodes.length; i++) {
			var td_nodes = tr_nodes[i].childNodes;
			var getcitytext = td_nodes[0].innerHTML;
			var citytext = getcitytext.split(new RegExp("<BR>","i"));
				//alert('removing' + citytext[0]);
				selected_comm[count++] = citytext[0];
	}
//alert(selected_comm.join(','));
 var advancedsearchform = document.getElementById("advancedsearch");
  var oldinput = advancedsearchform.cityzip_locations;
  if (oldinput == undefined) {
	var input = document.createElement("INPUT");
	input.setAttribute("type", "hidden");
	input.setAttribute("name", "cityzip_locations");
	input.setAttribute("id", "citizip_locations");
	input.setAttribute("value", selected_comm.join('|'));
	advancedsearchform.appendChild(input);
//alert(input.value);
  }
  else {
	oldinput.value = selected_comm.join('|');
//alert(oldinput.value);
  }
//alert(advancedsearchform.cityzip_locations.value);
}

function updatelocationinput(tbody) {
	var selected_loc = new Array();
	var count = 0;
	var tr_nodes = tbody.getElementsByTagName("TR");
	for(var i=0; i<tr_nodes.length; i++) {
			var td_nodes = tr_nodes[i].childNodes;
			var getcitytext = td_nodes[0].innerHTML;
			var citytext = getcitytext.split(new RegExp("<BR>","i"));
				//alert('removing' + citytext[0]);
				selected_loc[count++] = citytext[0];
	}
        var searchboxform = document.getElementById("property_search");
        var oldinput = searchboxform.search_locations;
        if (oldinput == undefined) {
                var input = document.createElement("INPUT");
                input.setAttribute("type", "hidden");
                input.setAttribute("name", "search_locations");
                input.setAttribute("id", "search_locations");
                input.setAttribute("value", selected_loc.join('|'));
                searchboxform.appendChild(input);
        }
	else {
		searchboxform.search_locations.value = selected_loc.join('|');
	}
}

function removelocation(loc) {
	var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];
	if (tbody.hasChildNodes()) {
		//alert(tbody.innerHTML);
		var tr_nodes = tbody.getElementsByTagName("TR");
		for(var i=0; i<tr_nodes.length; i++) {
			var td_nodes = tr_nodes[i].childNodes;
			var getcitytext = td_nodes[0].innerHTML;
			var citytext = getcitytext.split(new RegExp("<BR>","i"));
			//alert(citytext[0]+":"+loc);
			if (citytext[0] == loc) {
				//alert('removing' + citytext[0]);

				tbody.removeChild(tr_nodes[i]);	
			}
		}
	}
	if (document.getElementById("selected_location_table2") != null) {
		updateselectedlocation(tbody);
	}
	else {
		updatelocationinput(tbody);
	}
}

function alreadyselected(loc) {
	var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];
	if (tbody.hasChildNodes()) {
		//alert(tbody.innerHTML);
		var tr_nodes = tbody.getElementsByTagName("TR");
		for(var i=0; i<tr_nodes.length; i++) {
			var td_nodes = tr_nodes[i].childNodes;
			var getcitytext = td_nodes[0].innerHTML;
			var citytext = getcitytext.split(new RegExp("<BR>","i"));
			//alert(citytext[0]);
			if (citytext[0] == loc) {
				alert(loc + ' has already been added');
				if (document.getElementById('livesearch1-textbox') != null) {
					document.getElementById('livesearch1-textbox').value = "";
				}
				return true;
			}
		}
	}
	return false;
}

function boldselection(obj) {
	var textSpanObj = document.getElementById(obj.value);
	textSpanObj.style.fontWeight = obj.checked ? 'bold' : '';
}

function selectcommunities(loc, page, selected) {
      dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/communitysearch.php?city=" + loc,
            // Called when the page loaded successfully
             load: function (data) {
		     //dojo.byId('livesearch1').style.display = "block";
                     //dojo.byId('livesearch1').innerHTML = data;
                     //returning list of communities
                     var results = data.split("@@");
		     var communityhtml = "<div style=\"border:1px solid #000; width:400px; height:100px text-align:left; scroll=scroll\"><input type=\"hidden\" name=\"city\" value=\"" + loc + "\"><table width=\"400\" border=0>";
		     var selected_array = new Array();
		     if (selected != undefined) {
			document.getElementById("select_communities_form-nearbycities").community_search_option[0].checked = true;
			selected_array = selected.split('|');	
		     }
		     for (var i=1; i<results.length; i++) {
			 communityhtml += i % 2 == 1 ? "<tr><td align=\"left\" width=\"50%\">" : "<td align=\"left\">";
			 var community_selected = false;
			 for (j=0; j<selected_array.length; j++) {
				var thiscomm = selected_array[j];
				var athiscomm = thiscomm.replace(/\s\(.+?\)$/,"")
//alert(athiscomm + '-' + results[i]);
				if (athiscomm == results[i]) {
					community_selected = true;
				}
			 }
//alert(community_selected);
			 if (community_selected) {
				document.getElementById("select_communities_form-nearbycities").community_search_option[1].checked = true;
	                 	communityhtml  += "<input type=\"checkbox\" id=\"communities"+i+"\" name=\"communities[]\" value=\"" + results[i] + " ("+ loc +")\" onclick=\"document.getElementById('select_communities_form"+page+"').community_search_option[1].checked = true; boldselection(this)\" checked>&nbsp;&nbsp;<span id=\"" + results[i] + " ("+ loc +")\" style=\"font-weight:bold;\">" + results[i] + "</span>";
			 }
			 else {
	                 	communityhtml  += "<input type=\"checkbox\" id=\"communities"+i+"\" name=\"communities[]\" value=\"" + results[i] + " ("+ loc +")\" onclick=\"document.getElementById('select_communities_form"+page+"').community_search_option[1].checked = true; boldselection(this)\">&nbsp;&nbsp;<span id=\"" + results[i] + " ("+ loc +")\">" + results[i] + "</span>";
			 }
			 communityhtml += i % 2 == 0 ? "</td></tr>" : "</td>";
		     }
		     communityhtml += "</table></div>";
		     //$('#popup-select-communities').modal();
		     //$('#popup-pleaseenter-location').modal();
	             dojo.byId("communitylist"+page).innerHTML = communityhtml;
	             dojo.byId("citytoselectline1"+page).innerHTML = loc;
	             dojo.byId("citytoselectline2"+page).innerHTML = loc;
		     $('#popup-select-communities'+page).modal();
                     
             },
             error: function(data){
                     dojo.byId("communitylist"+page).innerHTML =
                            "An error occurred, with response: " + data;
             }
       });
}

function addcommunities(form) {
  var loc = form.city.value;
  if (form.community_search_option[0].checked) {
  	$.modal.close(); 
	return;
  }
  var selected_comm_array = new Array();
  var count = 0;
  var inputobj = document.getElementsByName('communities[]');
  for(var j=0; j<inputobj.length; j++) {
//alert(inputobj[j].checked);
	if (inputobj[j].checked == true) {
		selected_comm_array[count++] = inputobj[j].value;
	}
  }
//alert(form.community_search_option[1].checked + ":" + selected_comm_array.length);
  if (form.community_search_option[1].checked && (selected_comm_array.length == 0)) {
	document.getElementById("popup-select-communities-error").innerHTML = 'Please select at least one community, or choose "Search all of '+loc+'".';
	return;
  }
//alert(inputobj[0].checked);
  $.modal.close(); 
  removelocation(loc);
  var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];
  var tr_nodes = tbody.getElementsByTagName("TR");
  var previous_selected_comm_array = new Array();
  var array_count = 0;
  for(var i=0; i<tr_nodes.length; i++) {
	var td_nodes = tr_nodes[i].childNodes;
	var getcitytext = td_nodes[0].innerHTML;
	var citytext = getcitytext.split(new RegExp("<BR>","i"));
	citytext = citytext[0];
	previous_selected_comm_array[array_count] = citytext;
	array_count++;
  }
//alert(selected_comm_array);
  for(var j=0; j<selected_comm_array.length; j++) {
//alert(form.communities[j].value + ":" + citytext);
//alert(inputobj[j].checked);
	//if (inputobj[j].checked == true) {
//alert(form.communities[j].value);
	    var previously_selected = false;
	    for(var i=0; i<previous_selected_comm_array.length; i++) {
//alert(selected_comm_array[j] + ":" + previous_selected_comm_array[i]);
		if (selected_comm_array[j] == previous_selected_comm_array[i]) {
			previously_selected = true;
		}
	    }
	    if (previously_selected == false) {
//alert(selected_comm_array[j]);
		var row = document.createElement("TR")
		var td1 = document.createElement("TD")
		td1.setAttribute("width", "85%");
		td1.innerHTML = selected_comm_array[j];
		var td2 = document.createElement("TD")
		td2.setAttribute("align", "right");
		td2.innerHTML = "<a href=\"javascript:removelocation('" + selected_comm_array[j] + "')\">Remove</a>";
		row.appendChild(td1);
		row.appendChild(td2);
		tbody.appendChild(row);
	    }
	//}
  }
  updateselectedlocation(tbody);
}

function addcommunities_nearbycities(form) {
  var loc = form.city.value;
  var selected_comm_array = new Array();
  if (form.community_search_option[0].checked) {
  	form.community_search_option[0].checked = true;
	//document.getElementById("advancedsearch").nearbycities_communities.value = "";
  	$.modal.close(); 
	//return;
  }
  else {
	  var inputobj = document.getElementsByName('communities[]');
	  var count = 0;
	  for(var j=0; j<inputobj.length; j++) {
		if (inputobj[j].checked == true) {
			selected_comm_array[count++] = inputobj[j].value;
		}
	  }
  }
  if (form.community_search_option[1].checked && (selected_comm_array.length == 0)) {
	document.getElementById("popup-select-communities-error-nearbycities").innerHTML = 'Please select at least one community, or choose "Search all of '+loc+'".';
	return;
  }
  $.modal.close();
  var advancedsearchform = document.getElementById("advancedsearch");
  var oldinput = advancedsearchform.nearbycities_communities;
  if (oldinput == undefined) {
	var input = document.createElement("INPUT");
	input.setAttribute("type", "hidden");
	input.setAttribute("name", "nearbycities_communities");
	input.setAttribute("id", "nearbycities_communities");
	input.setAttribute("value", selected_comm_array.join('|'));
	advancedsearchform.appendChild(input);
  }
  else {
	oldinput.value = selected_comm_array.join('|');
  }
  document.getElementById(loc+"-selectcommlink").innerHTML = "&gt; <a href=\"javascript:selectcommunities('" + loc + "', '-nearbycities', '" + selected_comm_array.join('|') + "')\">Edit Selected Communities</a>";
  for(var i=0; i<advancedsearchform.nearby_cities.length; i++) {
	if (advancedsearchform.nearby_cities[i].value == loc) {
		advancedsearchform.nearby_cities[i].checked = true;
		boldselection(advancedsearchform.nearby_cities[i]);
	}
  }
}


function addlocation(loc) {
  if (loc == "No match found.") {
  	if (document.getElementById('livesearch1-textbox') != null) {
		document.getElementById('livesearch1-textbox').value = "";
	}
	return;
  }
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/communitysearch.php?city=" + loc,
            // Called when the page loaded successfully
             load: function (data) {
		     //dojo.byId('livesearch1').style.display = "block";
                     //dojo.byId('livesearch1').innerHTML = data;
                     //returning list of communities
                     if (data == "") {  //location doesn't have communities
			var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];
			var row = document.createElement("TR")
			var td1 = document.createElement("TD")
			td1.setAttribute("width", "85%");
			td1.innerHTML = loc;
			var td2 = document.createElement("TD")
			td2.setAttribute("align", "right");
			td2.innerHTML = "<a href=\"javascript:removelocation('" + loc + "')\"><small>Remove</small></a>";
			row.appendChild(td1);
			row.appendChild(td2);
			tbody.appendChild(row);
		     }
		     else { // will display a link to select communites
			var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];
			var row = document.createElement("TR")
			var td1 = document.createElement("TD")
			td1.setAttribute("width", "85%");
			td1.innerHTML = loc + "<br>><a href=\"javascript:selectcommunities('" + loc + "', '');\">Select Communities</a>";
			var td2 = document.createElement("TD")
			//td2.setAttribute("style", "vertical-align: top");
			//td2.setAttribute("valign", "top");
			td2.setAttribute("align", "right");
			td2.vAlign = "top";
			td2.innerHTML = "<a href=\"javascript:removelocation('" + loc + "')\">Remove</a>";
			row.appendChild(td1);
			row.appendChild(td2);
			tbody.appendChild(row);
		     }
		     updateselectedlocation(tbody);
		     document.getElementById("livesearch1-textbox").value = "";
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId("livesearch1").innerHTML =
                            "An error occurred, with response: " + data;
             }
       });
}

function addcity(loc) { //leftbar adding a city location
	if (loc == "No match found.") {
		return;
	}
	var tbody = document.getElementById("selected_location_table").getElementsByTagName("TBODY")[0];

	var currentloc = tbody.getElementsByTagName("TD");
	for( var i=0; i<currentloc.length; i++) {
		var thistd = currentloc[i];
		if (thistd.innerHTML == loc) {
			return;
		}
	}

	var row = document.createElement("TR")
	var td1 = document.createElement("TD")
	//td1.setAttribute("width", "85%");
	td1.innerHTML = loc;
	var td2 = document.createElement("TD")
	td2.setAttribute("align", "right");
	td2.innerHTML = "<a href=\"javascript:removelocation('" + loc + "');document.property_search.submit();//getmapresult('','',1,10,'','','','listing');\"><small>Remove</small></a>";
	row.appendChild(td1);
	row.appendChild(td2);
	tbody.appendChild(row);

	//create form input
 	var searchboxform = document.getElementById("property_search");
	var oldinput = searchboxform.search_locations;
	//var oldinput_display = searchboxform.search_locations_display;
	//var searchloc = loc.replace(/\s\([\w+\s+]+\)$/, "", loc);
        if (oldinput == undefined) {
		var input = document.createElement("INPUT");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "search_locations");
		input.setAttribute("id", "search_locations");
		input.setAttribute("value", loc);
		searchboxform.appendChild(input);

		/*
		var input_display = document.createElement("INPUT");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "search_locations_display");
		input.setAttribute("id", "search_locations_display");
		input.setAttribute("value", loc);
		searchboxform.appendChild(input);
		*/
	}
	else {
		oldinput.value += '|' + loc;
		//oldinput_display.value += '|' + loc;
	}
	document.property_search.submit();
	//getmapresult('','',1,10,'','','','listing');
}

function addlocation_zipcode(loc) {
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/zipcodesearch.php?zipcode=" + loc,
            // Called when the page loaded successfully
             load: function (data) {
		  if (data == "0") {
			$('#popup-nolocation-found').modal();
		  }
		  else {
			addlocation(loc);
		  }
		  return data;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId("livesearch1").innerHTML =
                            "An error occurred, with response: " + data;
		     return data;
             }
       });
}

function addlocation_errorcheck(loc) {
  if (loc == undefined || loc == null || loc.replace(/^\s+|\s+$/g,"") == "") {
	$('#popup-pleaseenter-location').modal();
	return;
  }
  if (loc == "No match found.") {
	if (document.getElementById('livesearch1-textbox') != null) {
		document.getElementById('livesearch1-textbox').value = "";
	}
	return;
  }
  if (alreadyselected(loc)) {
	return;
  }
  if (/^\d+$/.test(loc)) {
	addlocation_zipcode(loc);
	return;
  }
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/citysearch.php?city=" + loc,
            // Called when the page loaded successfully
             load: function (data) {
		  if (data == "No match found.") {
			$('#popup-nolocation-found').modal();
		  }
		  else {
			addlocation(loc);
		  }
		  return data;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId("livesearch1").innerHTML =
                            "An error occurred, with response: " + data;
		     return data;
             }
       });
}

function add_multiple_locations() {
	var selectedArray = new Array();
	var selObject = document.getElementById('CityZipLocationList');
	var count = 0;
	for(var i=0; i<selObject.options.length; i++) {
		if (selObject.options[i].selected) {
		  selectedArray[count] = selObject.options[i].value;
		  if (alreadyselected(selObject.options[i].value) == false) {
 			addlocation(selObject.options[i].value);
		  }
		  count++;
		}
	}
  
  if (count == 0) {
	$('#popup-pleaseenter-location').modal();
	return;
  }
	
}

function selectall(inputid) {
	var inputobj = document.getElementsByName(inputid);
	for(var i=0; i<inputobj.length; i++) {
		inputobj[i].checked = true;
	}
}

function clearall(inputid) {
	var inputobj = document.getElementsByName(inputid);
	for(var i=0; i<inputobj.length; i++) {
		inputobj[i].checked = false;
	}
}

function getnearbycities(loc, distance) {
  if (loc == undefined || loc == null || loc.replace(/^\s+|\s+$/g,"") == "") {
	$('#popup-pleaseenter-location').modal();
	return;
  }
  if (loc == "No match found.") {
	if (document.getElementById('livesearch2-textbox') != null) {
		document.getElementById('livesearch2-textbox').value = "";
	}
	return;
  }
  dojo.xhrGet ({
      // Location of the HTML content we want to grab
             url: "/nearbycitysearch.php?key=" + loc + "&distance=" + distance,
            // Called when the page loaded successfully
             load: function (data) {
		var outputhtml = "<div class=\"bold\">";
		if (data == "") {
			$('#popup-nolocation-found').modal();
			outputhtml += "No cities found.";
			if (document.getElementById('livesearch2-textbox') != null) {
				document.getElementById('livesearch2-textbox').value = "";
			}
		}
		else {
			var results = data.split("@@");
			outputhtml += "Cities found within <select name=\"distance\" onchange=\"getnearbycities('" + loc + "',this.value);\" style=\"width:40px\">";
			outputhtml += distance == 1 ? "<option value=\"1\" selected>1</option>" : "<option value=\"1\">1</option>";
			outputhtml += distance == 3 ? "<option value=\"3\" selected>3</option>" : "<option value=\"3\">3</option>";
			outputhtml += distance == 5 ? "<option value=\"5\" selected>5</option>" : "<option value=\"5\">5</option>";
			outputhtml += distance == 10 ? "<option value=\"10\" selected>10</option>" : "<option value=\"10\">10</option>";
			outputhtml += distance == 15 ? "<option value=\"15\" selected>15</option>" : "<option value=\"15\">15</option>";
			outputhtml += distance == 20 ? "<option value=\"20\" selected>20</option>" : "<option value=\"20\">20</option>";
			outputhtml += distance == 25 ? "<option value=\"25\" selected>25</option>" : "<option value=\"25\">25</option>";
			outputhtml += "</select> miles of " + loc;
			outputhtml += "<span class=\"padmenu pad-lt\"><a href=\"javascript:selectall('nearby_cities[]');\"><small>Select All</small></a></span>";
			outputhtml += "<span class=\"padmenu\"><a href=\"javascript:clearall('nearby_cities[]');\"><small>Clear All</small></a></span></div>";
			outputhtml += "<table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" class=\"mar5-top mar2-bot\">";
			for(var i=0; i<results.length; i++) {
				var output = results[i].split("|");
			 	outputhtml += (i+1) % 3 == 1 ? "<tr class=\"brownlightshadebg\"><td style=\"width:210px; vertical-align:top\">" : "<td style=\"width:210px; vertical-align:top;\">";
				outputhtml  += "<span id=\"" + output[0] + "\"><div class=\"float-lt\"><input type=\"checkbox\" id=\"nearby_cities\""+i+" name=\"nearby_cities[]\" value=\"" + output[0] + "\" onclick=\"boldselection(this)\">&nbsp;&nbsp;" + output[0] + "</div><div class=\"float-rt\">" + output[1] + " mi.</div></span><div class=\"clear\"></div>";
				outputhtml += output[2] == "1" ? "<div id=\"" + output[0]+ "-selectcommlink\" class=\"float-lt\" style=\"margin:5px 0 0 20px;\">&gt; <a href=\"javascript:selectcommunities('" + output[0] + "', '-nearbycities')\">Select Communities</a></div>" : "";
				outputhtml += (i+1) % 3 == 0 ? "</td></tr>" : "</td>";
		     }
		     outputhtml += "</table><small>Note: Distances (miles) are approximate.</small></div>";
		}
                dojo.byId("nearbycities_result").innerHTML = outputhtml;
                dojo.byId("nearbycities_result").style.display = "block";
		return data;
             },
            // Called if there was an error (such as a 404 response)
             error: function(data){
                     dojo.byId("nearbycities_result").innerHTML =
                            "An error occurred, with response: " + data;
             }
       });
}

function selectallcommunities(page) {
	var form = document.getElementById("select_communities_form"+page);
	var inputobj = document.getElementsByName('communities[]');
	for(var i=0; i<inputobj.length; i++) {
		inputobj[i].checked = false;
		boldselection(inputobj[i]);
	}
}

function goSearch() {
	var form = document.getElementById("advancedsearch");
	//alert(document.getElementById("communities").value);
	//alert(form.nearbycities_communities.value);
	//alert(form.cityzip_locations.value);
	//alert(form.tab.value);
	form.action = "advancedsearch_act.php";
	form.method = "POST";
	switch (form.tab.value) {
	   case "cityzip":
		form.submit();
		break;
	   case "nearbycities":
		form.submit();
		break;
	   case "address":
        	if (form.address.value == "") {
			alert("Please enter address");
			form.address.focus();
			return;
		}
		form.submit();
		break;
	   case "mls":
        	if (form.mls.value == "") {
			alert("Please enter MLS Number");
			form.mls.focus();
			return;
		}
		form.submit();
		break;
	}
	return;
}
