var is_open = 0;

/*
  Make ajax request to lookup city, on certain keystrokes perform
  relevant actions (added parameters for lookup)
*/
function lookup(inputString, e, p, parameters)
{
  if(inputString.length == 0)
  {
    // Hide the suggestion box.
    jQuery('#suggestions').hide();
  }
  else if(e.keyCode == 40)
  // down arrow
  {
    // get index of current selected li
    index = jQuery('#autoSuggestionsList li').index(jQuery('li.listSelect')[0]);
    
    // jQuery('#tools-members').prepend(index + ' | ' + jQuery('#autoSuggestionsList li').length + '<br/>');
    
    // if index < length of the list highlight next li
    if(index + 1 < jQuery('#autoSuggestionsList li').length)
    {
      // remove existing bg colour
      jQuery('#autoSuggestionsList li').removeClass('listSelect');
      
      // make next li selected
      jQuery('#autoSuggestionsList li:eq(' + (index + 1) + ')').addClass('listSelect');
    }
  }
  else if(e.keyCode == 38)
  // up arrow
  {
    // get index of current selected li
    index = jQuery('#autoSuggestionsList li').index(jQuery('li.listSelect')[0]);
    
    // alert(index);
    
    // if index > 0 highlight previous li
    if(index > 0)
    {
      // remove existing bg colour
      jQuery('#autoSuggestionsList li').removeClass('listSelect');
      
      // make prev li selected
      jQuery('#autoSuggestionsList li:eq(' + (index - 1) + ')').addClass('listSelect');
    }
  }
  else if(e.keyCode == 13)
  // enter
  {
    if(is_open==1)
    {
      jQuery('#city').val(jQuery('li.listSelect').html());

      is_open = 0;
      
      jQuery('#suggestions').hide();
      
      return true;
    }
  }
  else
  // do ajax request
  {
    if(p == "restaurants")
    // restaurants page
    {
      jQuery.post("/rpc.php", {queryString: ""+inputString+""}, function(data)
      {
        if(data.length >0)
        {
          is_open=1;
          jQuery('#suggestions').show();
          jQuery('#autoSuggestionsList').html(data);
          jQuery('#autoSuggestionsList li:first').addClass('listSelect');
        }
      });
    }  

    if(p == "register")
    // registration page
    {
      jQuery.post("/rpc1.php", {queryString: ""+inputString+"", params: (parameters == null) ? "" : parameters }, function(data)
      {
        if(data.length >0)
        {
          is_open=1;
          jQuery('#suggestions').show();
          jQuery('#autoSuggestionsList').html(data);
          //jQuery('#autoSuggestionsList li:first').addClass('listSelect');
        }
      });
    }
	
	if(p == "citysearch")
    // registration page
    {
      jQuery.post("/rpc2.php", {queryString: ""+inputString+"", params: (parameters == null) ? "" : parameters }, function(data)
      {
        if(data.length >0)
        {
          is_open=1;
          jQuery('#suggestions').show();
          jQuery('#autoSuggestionsList').html(data);
          //jQuery('#autoSuggestionsList li:first').addClass('listSelect');
        }
      });
    }
  }
} // lookup


/*
  called when focus lost from city input, fill in with current selected
  details if suggestion box open
*/
function fill(fill_str)
{
  if(jQuery('li.listSelect').length > 0)
  {
    jQuery('#city').val(jQuery('li.listSelect p').html());
    jQuery('#cityid').val(jQuery('li.listSelect input').val());
    is_open = 0;

    jQuery('#suggestions').hide();
  }
}

/*
  Called when mouse over in suggestion list
*/
function rowOver(e)
{
  // remove existing bg colour
  jQuery('#autoSuggestionsList li').removeClass('listSelect');
  
  // set selection class
  jQuery(e).addClass('listSelect');
}

/*
  Called when mouse out in suggestion list
*/
function rowOut(e)
{
  // do nothing handled by row over
}

//added by Binod
// edited by RC
function validate()
{
	if(document.getElementById("city").value=='' && document.getElementById("srzip").value=='' && !document.getElementById("near_chk").checked)
	{
		alert("Please enter either City or Zip");
		return false;
	}
	
	if(is_open==1)
	{
		// alert('failed submit');
		
		return false;
	}
	else
	{
		//alert("submitted");
		return true;
	}
}

function clbox(cl)
{
  //alert(cl);
  if(cl=='city')
  {
    document.getElementById("srzip").value="";
    document.getElementById("city").focus();
  }
  
  if(cl=='zip')
  {
    document.getElementById("city").value="";
    document.getElementById("srzip").focus();
  }
  
  if(cl == 'near')
  {
    document.getElementById("srzip").value="";
    document.getElementById("city").value="";
  }
}

function rr(n)
{
  if(n=='srcity')
  {
    document.getElementById("srzip").value="";
    document.getElementById('city_chk').checked=true;
  }
  
  if(n=='srzip')
  {
    document.getElementById("city").value="";
    document.getElementById('zip_chk').checked=true;
  }
}
