﻿
function lookup(inputString, mlsTable) 
{
	if(inputString.length == 0) 
	{
		$('#acResults').hide();
	}
	else 
	{
		$.post("SearchIdxListings.aspx", {
	    keyinString: "" + inputString + "", 
	    mlsTable: "" + mlsTable + "" 
	    }, function(data) {
	      //alert(data);
		    if(data.length > 0) 
		    {
		      var records = data.split('|');
		      var lists = "";
		      for (var i = 0; i < records.length; i++)
		      {
	  	      var dataType = records[i].split(':')[0];
  		      var dataItem = records[i].split(':')[1];
            if (dataItem != undefined)
            {
		          lists += "<li onclick=\"selectItem('" + dataItem.split("'").join("\\'") + 
		            "', '" + dataType + "')\">" + dataItem + "</li>";
		        }
		      }
		      lists += "<li onclick='closeBox()' class='close'><< close >></li>";
			    $('#acResults').show();
			    $('#acList').html(lists);
		    }
		  }
		);
	}
} // lookup

function closeBox()
{
	$('#acResults').hide();
}

var acTotalCity = 0;
var acCities = "";
// @param thisType city (c) or subdivision (s)
function selectItem(thisValue, thisType)
{
	//$('#acSearch').val(thisValue);
	$('#acResults').hide();
	$('#acSelectedItems').hide();
  
  // update stats
  ++acTotalCity;
  acCities = concatVal(thisValue);
  if (thisType == "c")
    $('#acHiddens').append("<input type='hidden' name='city' value=\""+ thisValue +"\" />");
  else
    $('#acHiddens').append("<input type='hidden' name='subdivision' value=\""+ thisValue +"\" />");
  
  showStats();
}

function showStats()
{
  var stats = "<a href='#' onclick='showSelected()'>[+]</a> " + acTotalCity + ((acTotalCity > 1) ? 
    " Items" : " Item") + " selected";
	$('#acSelectedStats').html(stats);
  $('#acSearch').val('');
  $('#acSearch').focus();
}

var acSelectedValues = "";
function concatVal(str)
{
  if (acSelectedValues != "")
    acSelectedValues += ",";
  acSelectedValues += str;
	setHiddenValue();
  
  return acSelectedValues;
}

function removeVal(str)
{
  --acTotalCity;
  var arrItems = acSelectedValues.split(",");
  var newItems = "";
  for (var i = 0; i < arrItems.length; i++)
  {
    if (arrItems[i] != str)
    { // construct hidden values
      if (newItems != "")
        newItems += ",";
      newItems += arrItems[i];
    }
    else
    { // remove from hidden
      $(document).find('input').each(function() {
        if ($(this).attr('type') == 'hidden' && $(this).val() == str)
        {
          $(this).remove();
        }
      });
    }
  }
  acSelectedValues = newItems;
  $('#acSelectedItems').slideToggle();
	setHiddenValue();
  showStats();
	showSelected();
}

function showSelected()
{
  var lists = "";
  if (acSelectedValues != "")
  {
    var arrItems = acSelectedValues.split(",");
    for (var i = 0; i < arrItems.length; i++)
    {
      lists += "<li>" + arrItems[i] + " <a href='#' onclick=\"removeVal('"+ arrItems[i].split("'").join("\\'") +"')\">[x]</a></li>";
    }
  }
  if (lists != "")
    lists = "<ul>" + lists + "</ul>";
  $('#acSelectedItems').html(lists);
  $('#acSelectedItems').slideToggle();
}

function setHiddenValue()
{
  acCities = acSelectedValues;
}

// while switch tabs
function removeAllCities()
{
  acTotalCity = 0;
  $('#acHiddens').html("");
  $('#acSelectedStats').html("");
  $("#acSelectedItems").html("");
}

function removeRadius()
{
  $("#findfield").val("");
  $("#circleRadius").val("");
}
