function fl() {
  var error  = document.getElementById("findLocals_Error");
  var postal = document.getElementById("findLocals_PostalCode");

  if ( !/^[a-z][0-9][a-z](|-| )[0-9][a-z][0-9]$/i.test(postal.value) ) {
    error.innerHTML = "You have entered an invalid Postal Code.";
    return false;
  }

  error.innerHTML = "";
  
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    postal.value,
    function(point) {
      flbll(point.lat(), point.lng());
    }
  ); 
}

function sp(j) {
  var producers = document.getElementById("findLocals_Producers");
  
  if ( producers.innerHTML.length > 0 ) {
    $("#findLocals_Producers").slideUp("slow");
    producers.innerHTML = '';
  }

  if ( j.length == 0 ) {
    producers.innerHTML += '<li>No Local Stores Found</li>'; 
  } else {
    for( i = 0; i < j.length; i++ ) {
      producers.innerHTML += '<li><a href="?cid=12&id=' + j[i].id + '">' + j[i].name + '</a></li>';
    }
  }

  $("#findLocals_Producers").slideDown("slow");
}

function lo() {
  if (typeof document.body.style.maxHeight === "undefined") {
    $("body","html").css({height: "100%", width: "100%"});
    $("html").css("overflow","hidden");
    if (document.getElementById("TB_HideSelect") === null) {
      $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
      $("#TB_overlay").click(tb_remove);
    }
  } else {
    if(document.getElementById("TB_overlay") === null){
      $("body").append("<div id='TB_overlay'></div><div id='TB_window'>");
      $("#TB_overlay").click(tb_remove);
    }
  }
  TB_WIDTH = (500*1) + 30;
  TB_HEIGHT = (400*1) + 40;
  ajaxContentW = TB_WIDTH - 30;
  ajaxContentH = TB_HEIGHT - 45;
  //$("#TB_window").append("<div id='TB_title'><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
  //$("#TB_closeWindowButton").click(tb_remove);
}

function lm() { 
  lo();

  $("#TB_window").append('<div id="findLocals_Map"></div>');

  var point = new GLatLng(45.305803, -63.017578);
  var map = new GMap2(document.getElementById("findLocals_Map"));
  map.setCenter(point, 6);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  
  var marker = new GMarker(point, {draggable: true});

  GEvent.addListener(marker, "dragstart", function() { 
    map.closeInfoWindow();
  });

  GEvent.addListener(marker, "dragend", function() {
    var lat = marker.getPoint().lat();
    var lng = marker.getPoint().lng(); 
    marker.openInfoWindowHtml('<a href="javascript:void(0);" onclick="flbll(\'' + lat + '\',\'' + lng + '\');">Find producers near this spot</a>');
  });

  map.addOverlay(marker);
  marker.openInfoWindowHtml("Drag me to a spot near you");
}

function ls(sid) {  
  lo();
	var producers = $.ajax({
	  type: "post",
    url: "dr.php",
    data: "a=pi&id=" + sid,
    dataType: "json",
    success: function(j) {
      var html = '<h2>' + j.name + '</h2>';
      $("#TB_window").append(html);
    }
	});
}

function flbll(lat, lng) {
	var producers = $.ajax({
	  type: "post",
    url: "dr.php",
    data: "a=flbll&lat=" + lat + "&lng=" + lng + "&" + document.getElementById("findLocals_PostalCode").className,
    dataType: "json",
    success: sp
	}); 
  tb_remove();
}

function tb_remove() {
  $("#TB_overlay").unbind("click");
 // $("#TB_closeWindowButton").unbind("click");
  $("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
  if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
    $("body","html").css({height: "auto", width: "auto"});
    $("html").css("overflow","");
  }
  document.onkeydown = "";
  return false;
}