<!-- hide script
//
// Global variable declarations.
//
// var PlacesURL = "http://maps.dnr.state.mn.us/cgi-bin/places.pl";
var PlacesURL = "http://www.dnr.state.mn.us/maps/places.html";
var PlacesArray;

function PlaceObject(name, type, x, y, county) {
  this.name = name;
  this.type = type;
  this.x = x;
  this.y = y;
  this.county = county;
}

//
// find - core places.js function to actually hit places.pl (if necessary) and run an application's
// callback functions to handle the search results.
//
// Arguments:
//     place - a string containing a place of some sort
//     type - the type of place (optional)
//
function find(place, type) {  	
  var re_pls = /[Tt](\d{1,3})[Rr](\d{1,2})[Ss]?(\d{0,2})/; // regex for pls lookups
  var re_coord = /(-?[0-9]+(\.[0-9]*)?) +(-?[0-9]+(\.[0-9]*)?)/; // regex for coordinates

 if(window.places_exception)
    var exception = window.places_exception;
  else
    var exception = function(message) { alert(message) };

  if(!window.places_zoom) {
    exception("Required function 'places_zoom' is not defined.");
    return;
  }
    
  if(!window.places_list) {
    exception("Required function 'places_list' is not defined.");
    return;
  }

  if(re_pls.test(place)) {    	  

    if(RegExp.$1 < 27 || RegExp.$1 > 168) {
      exception("Valid township numbers must be between 27 and 168.");
      return false;
    }
    if(RegExp.$2 < 1 || RegExp.$2 > 51) {
      exception("Valid range numbers must be between 1 and 51.");
      return false;
    }
    if(RegExp.$3 && (RegExp.$3 < 0 || RegExp.$3 > 36)) {
      exception("Valid section numbers must be between 1 and 36.");
      return false;
    }
	  
    // build pls search term
    var pls = 't';
    for(var i=RegExp.$1.length; i<3; i++) pls += '0';
    pls += RegExp.$1;
    for(i=RegExp.$2.length; i<2; i++) pls += '0';
    pls += RegExp.$2;
    if(RegExp.$3) {
      for(i=RegExp.$3.length; i<2; i++) pls += '0';
      pls += RegExp.$3;
    }

    url = PlacesURL + "?source=pls&place=" + pls;
    data = get_content(url);
  } else if(re_coord.test(place)) {
    var p = new Point(parseFloat(RegExp.$1), parseFloat(RegExp.$3));

    if(p.x < 180.0 && p.y < 180.0) { // check lat/lon

      // swap x, y if x appears to be invalid longitude
      if (Math.abs(p.x) > 97.20 || Math.abs(p.x) < 89.46) {
        temp = p.x
        p.x = p.y
        p.y = temp
      }

      if(p.x > 0) 
	p.x = -1.0*p.x; // longitude should be negative

      if(p.x < -97.20 || p.x > -89.46) {
	exception("Valid longitude values in Minnesota are between -97.20 and -89.46.");
	return false;
      }
      if(p.y < 43.48 || p.y > 49.40) {
	exception("Valid latitude values in Minnesota are between 43.48 and 49.40.");
	return false;
      }
	    
      p = geographicToUTM(15, p);
    } else { // check UTM
      if(p.x < 156000 || p.x > 795000) {
	exception("Valid UTM easting coordinates in Minnesota are between 156000.0 and 795000.0.");
	return false;
      }
      if(p.y < 4720000 || p.y > 5571000) {
	exception("Valid UTM northing coordinates in Minnesota are between 4720000.0 and 5571000.0.");
	return false;
      }
    }

    window.places_zoom(p.x, p.y, 'coordinate');
    return false;
  } else {
    url = PlacesURL + "?source=gnis&place=" + place;
    if(type) url += "&type=" + type;        
    data = get_content(url);
  }

  // check the data for exceptions
  if(data.indexOf(":") == -1) {    
    exception(data);
    return false;
  }

  var records = data.split("\n");

  PlacesArray = new Array();
  for(var i=0; i<records.length; i++) {
    values = records[i].split(":");    
    if(values[0]) PlacesArray.push(new PlaceObject(values[0],values[1],values[2],values[3],values[4]));
  }

  if(PlacesArray.length == 0) // raise an exception
    exception("Place '" + place + "' not found, please try again.");
  else if(PlacesArray.length == 1)
    window.places_zoom(PlacesArray[0].x, PlacesArray[0].y, PlacesArray[0].type);
  else 
    window.places_list(PlacesArray);

  return false;
}
// end script -->

