 
// Creater a geocoder
var geocoder;
	
function initialize() {
  geocoder = new GClientGeocoder();
}

function findLocation() {
  var address = document.getElementById("query").value;
  
  // extra fields??
  if (has_term()) {
    document.getElementById("term").value = document.getElementById("q_term").value;
  } else {
    document.getElementById("term").value = "";
  }
  // extra fields??

  document.getElementById("product").value = document.getElementById("q_product").value;
  if ($("#search_type").val() != 'online') {
    geocoder.getLocations(address, submitAddress);
  } else {
    document.hidden.submit();
  }
}

function submitAddress(response) {
  decodedLocation = decodeGeocoderResponse(response);

  document.getElementById("state").value = decodedLocation['state'];
  document.getElementById("city").value = decodedLocation['city'];
  document.getElementById("zip").value = decodedLocation['zip'];;

  document.hidden.submit();
}
     
function has_term(){
  return (($("#q_product").val() == "cd") || 
          ($("#q_product").val() == "special-cd") || 
          ($("#q_product").val() == "jumbo-cd") ||
          ($("#q_product").val() == "business-cd") ||  
          ($("#q_product").val() == "ira-cd"));
}

function toggle_term_field(){
  if (has_term()) {
    $("#term_field").show('fast');
  } else {
    $("#term_field").hide('fast');
  }
}

function setSearchType(type) {	
  $("#main-search-nav a").removeClass('selected');
  $("#main-search-nav a").addClass('inactive');
  switch(type) {
  case 'local':
    // $("#search_description").hide(fast);
    $("#search_description").text('Local rates only');
    $("#location_field").show('fast');
    $("#search_type").val('local');
    $("#main_search_nav_local").addClass('selected');
    $("#main_search_nav_local").removeClass('inactive');
    break;
  case 'online':

    //$("#search_description").hide('fast');
    $("#search_description").text('Online rates only');
    $("#location_field").hide('fast');
    $("#search_type").val('online');
    $("#main_search_nav_online").addClass('selected');
    $("#main_search_nav_online").removeClass('inactive');
    break;
  case 'all':
    //$("#search_description").show('fast');
    $("#search_description").text('All rates (local and online)');
    $("#location_field").show('fast');
    $("#search_type").val('all');
    $("#main_search_nav_all").addClass('selected');
    $("#main_search_nav_all").removeClass('inactive');
    break; 
  }

}

    

$(document).ready(function(){
  toggle_term_field();
  $("#q_product").change(function(){
    toggle_term_field();
    return false;
  });
  setSearchType($("#search_type").val());


  $("input.location").focus(function()
  {
    if ($(this).val() == $(this)[0].title)
      {
        $(this).removeClass("defaultLocationTextActive");
        $(this).val("");
      }
  });
	
  $("input.location").blur(function()
  {
    if ($(this).val() == "")
      {
        $(this).addClass("defaultLocationTextActive");
        $(this).val($(this)[0].title);
      }
  });

  $("input.location").focus();
  $("input.location").blur(); 

});     
