function envelope(xmin, xmax, ymin, ymax) {
  this.xmin = xmin;
  this.xmax = xmax;
  this.ymin = ymin;
  this.ymax = ymax;
  this.getDeltaX = getDeltaX;
  this.getDeltaY = getDeltaY;
  
  //****Accessor Methods****//
  function getDeltaX() {
	return this.xmax - this.xmin;
  }
  
  function getDeltaY() {
	return this.ymax - this.ymin;
  }
}

function point(numX,numY) {
	this.x = numX;
	this.y = numY;
}

function map(mapobj) {
  var mapimg = mapobj; 
  var mMapsource = null;
  var xmin = null;
  var xmax = null;
  var ymin = null;
  var ymax = null;
  var city = null;
  var street = null;
  var zip = null;
  var geoX = null;
  var geoY = null;
  var pixX = null;
  var pixY = null;
  this.setXmin = setXmin;
  this.setXmax = setXmax;
  this.setYmin = setYmin;
  this.setYmax = setYmax;
  this.getXmin = getXmin;
  this.getXmax = getXmax;
  this.getYmin = getYmin;
  this.getYmax = getYmax;
  this.getCity = getCity;
  this.getStreet = getStreet;
  this.getZip = getZip;
  this.getGeoX = getGeoX;
  this.getGeoY = getGeoY;
  this.getDeltaX = getDeltaX;
  this.getDeltaY = getDeltaY;
  this.getWidth = getWidth;
  this.getHeight = getHeight;
  this.getConvXtoP = getConvXtoP;
  this.getConvYtoP = getConvYtoP;
  this.setURL = setURL;
  this.clear = clear;
  this.bufferPolyline = bufferPolyline;
  this.bufferFeature = bufferFeature;
  this.bufferPoint = bufferPoint;
  this.IdentifyHS = IdentifyHS;
  this.SearchAddress = SearchAddress;
  this.SearchCommunity = SearchCommunity;
  this.getGeocode = getGeocode;
  this.getCommunity = getCommunity;
  this.getCounty = getCounty;
  this.getLegislativeDistrict = getLegislativeDistrict;
  this.getRRDDistrict = getRRDDistrict;
  this.IdentifyPoint = IdentifyPoint;
  this.getWatershed = getWatershed;
  this.getMapSource = getMapSource;
  this.setMapSource = setMapSource;
  this.setAXL = setAXL;
  this.setExtent = setExtent;
  this.setLayer = setLayer;
  this.setSelect = setSelect;
  this.getMapStartUp = getMapStartUp;
  this.getMapTop = getMapTop;
  this.getMapLeft = getMapLeft;
  this.getMapObj = getMapObj;
  this.getSessionKeeper = getSessionKeeper;

//****Methods****//

function setXmin(x) {
  xmin = x;
}

function setXmax(x) {
  xmax = x;
}

function setYmin(y) {
  ymin = y;
}

function setYmax(y) {
  ymax = y;
}

function setURL(url) {
  mapimg.src = url;
}

function setMapSource(s) {
  mMapsource = s;
}

//****Accessor Methods****//

function getXmin() {
  return xmin;
}

function getXmax() {
  return xmax;
}

function getYmin() {
  return ymin;
}

function getYmax() {
  return ymax;
}
function getDeltaX() {
  return xmax - xmin;
}
  
function getDeltaY() {
  return ymax - ymin;
}

function getMapTop() {
	var obj = mapimg;
  	var curtop = 0;
  	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else {if (obj.y) {
		curtop += obj.y;};}
	return curtop;
}

function getMapLeft() {
	var obj = mapimg;
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else {if (obj.x) {
		curleft += obj.x;};}
	return curleft;
}

function getCity() {
  return city;
}

function getStreet() {
  return street;
}

function getZip() {
  return zip;
}

function getGeoX() {
  return geoX;
}

function getGeoY() {
  return geoY;
}

function getConvXtoP() {
  return parseInt(parseInt(mapimg.width)*(geoX - xmin)/(xmax - xmin) + parseInt(getMapLeft()));
}

function getConvYtoP() {
  return parseInt(parseInt(mapimg.height) - (mapimg.height*(geoY - ymin)/(ymax - ymin)) + parseInt(getMapTop()));
}

//****AJAX Methods****//
function clear() {
  show();
  fnCloseDHTML('gBox');
  MCGI.setClear(ajaxResponse_Map);
}

function getSessionKeeper() {
  MCGI.getSessionKeeper(ajaxResponse_Map);
}

function getMapStartUp() {
show();
 fnHideDHTML();
 MCGI.getMapStartUp(parseInt(mapimg.width), parseInt(mapimg.height), ajaxResponse_Map);
}

function setAXL() {
 show();
 fnHideDHTML();
 MCGI.setAXL(mMapsource, ajaxResponse_Map);
}

function setLayer(name, vis) {
  show();
  fnHideDHTML();
  MCGI.setLayer(name, vis, ajaxResponse_Map);
}

function setExtent() {
  show();
  fnHideDHTML();
  MCGI.setExtent(parseFloat(xmin), parseFloat(ymin), parseFloat(xmax), parseFloat(ymax), parseInt(mapimg.width), parseInt(mapimg.height), ajaxResponse_Map);
}

function setSelect(sXmin, sXmax, sYmin, sYmax) {
  show();
  fnCloseDHTML('gBox');
  MCGI.setSelect(parseFloat(sXmin), parseFloat(sYmin), parseFloat(sXmax), parseFloat(sYmax), ajaxResponse_Map);
}

function bufferPolyline(xArray, yArray, units, dist) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getBufferPolyline(xArray, yArray, units, dist, ajaxResponse_Map);
}

function bufferPoint(xArray, yArray, units, dist) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getBufferPoint(xArray, yArray, units, dist, ajaxResponse_Map);
}

function bufferFeature(zXmin, zYmin, zXmax, zYmax, unit, dist, ilayer) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getBufferFeature(parseFloat(zXmin), parseFloat(zYmin), parseFloat(zXmax), parseFloat(zYmax), unit, dist, ilayer, ajaxResponse_Map);
}

function IdentifyHS(x, y) {
  fnCloseDHTML('gBox');
  MCGI.getIdentifyHS(parseFloat(x), parseFloat(y), ajaxResponse_Identify);
}

function IdentifyPoint(x, y) {
  fnCloseDHTML('gBox');
  MCGI.GetIdentifyPoint(parseFloat(x), parseFloat(y), ajaxResponse_SiteIdentify);
}

function getGeocode(x, y, a, c, z) {
  show();
  fnCloseDHTML('gBox');
  geoX = x;
  geoY = y;
  street = a;
  city = c;
  zip = z;
  MCGI.getGeocode(parseFloat(x), parseFloat(y), ajaxResponse_GeoCode);
}

function getCommunity(place, cnty, type, fips, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getCommunity(place, cnty, type, fips, showpnts, ajaxResponse_Map);
}

function getCounty(cnty, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getCounty(cnty, showpnts, ajaxResponse_Map);
}

function getRRDDistrict(rrd, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getRRDDistrict(rrd, showpnts, ajaxResponse_Map);
}

function getWatershed(wtr, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getWatershed(wtr, showpnts, ajaxResponse_Map);
}

function getLegislativeDistrict(distval, type, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getDistrict(distval, type, showpnts, ajaxResponse_Map);
}

function SearchAddress(addy, city, zip) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getSearchAddress(addy, city, zip, ajaxResponse_MultipleAddress);
}

function SearchCommunity(place, ptype, showpnts) {
  show();
  fnCloseDHTML('gBox');
  MCGI.getSearchCommunity(place, parseInt(ptype), showpnts, ajaxResponse_MultipleCommunity);
}

function ajaxResponse_MultipleAddress(response){
  if (response.error != null) {
	alert(response.error);
    return;
  } else { 
	var ds = response.value;
	  if(ds != null && typeof(ds) == "object" && ds.Tables != null) {
		if (ds.Tables[0].Rows.length == 1) {
		  alert(ds.Tables[0].Rows[0].error)
		  document.getElementById('searchResults').innerHTML = ""
		  //fnScrollerStartup(true);
		} else {
		var arr = new Array();
		arr[arr.length] = "<table width='175' cellSpacing='0' cellPadding='0' border='0' bgcolor='#B8B8B9'>";
		for(var i=0; i<ds.Tables[0].Rows.length; i++) {
		  arr[arr.length] = "<tr><td colspan=2 align='left'>Result #" + (i+1) + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>Address: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].address + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>City: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].city + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>Zip Code: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].zipcode + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td colspan=2 align='center'><input type=button class='divbtn' id='btnMapAddy' value='Map It' onclick=\"fnMapAddressMultiple('" + ds.Tables[0].Rows[i].mgrx + "', '" + ds.Tables[0].Rows[i].mgry + "', '" + ds.Tables[0].Rows[i].address + "', '" + ds.Tables[0].Rows[i].city + "', '" + ds.Tables[0].Rows[i].zipcode + "')\"></td></tr>";
		  arr[arr.length] = "<tr><td>&nbsp;</td></tr>";
		  arr[arr.length] = "<tr bgcolor='FFFFFF'><td colspan=2>&nbsp;</td></tr>";
		}
		arr[arr.length] = "</table>";
		document.getElementById('searchResults').innerHTML = "Multiple Results Found..." + arr.join("");
		}
	} else {
	var rmap = response.value; 
	  if (rmap.url.indexOf('ERROR') > 0) {
		alert(rmap.url)
	  } else {
	  mapimg.src = rmap.url
	  xmin = rmap.xmin
	  xmax = rmap.xmax
	  ymin = rmap.ymin
	  ymax = rmap.ymax
	  street = rmap.street
	  city = rmap.city
	  zip = rmap.zip
	  geoX = rmap.geoCodeX
	  geoY = rmap.geoCodeY
	  dynDivWidth = 200;
	  //dynDivHeight = 95;
	  var x = 'gBox';
	  dynInsideContents = '<p class=\"notopgap\"><span style=\"float: right; position: relative;\"><img src=images/close.gif style=\"CURSOR: hand\" title=\"close\" onmouseover=\"clickDisable()\" onmouseout=\"clickEnable()\" onclick=\"fnCloseDHTML('+x+')\"></span></p><span style=\"float: left; font-family: Arial; font-weight: bold; font-size: 12px;">Address:</span><br><p>'+myMap.getStreet()+'</p><p class=\"notbottomgap\">'+myMap.getCity()+' '+myMap.getZip()+'</p>';
	  fnAddElement('gBox');
	  }
	}
  }
  //fnScrollerStartup(false);
  fnInitiateConnect(sessionTime);
  hide();
}

function ajaxResponse_MultipleCommunity(response){
  if (response.error != null) {
	alert(response.error);
    return;
  } else { 
	var ds = response.value;
	  if(ds != null && typeof(ds) == "object" && ds.Tables != null) {
		if (ds.Tables[0].Rows.length == 1) {
		  alert(ds.Tables[0].Rows[0].error)
		  document.getElementById('communResults').innerHTML = ""
		  //fnScrollerStartup(true);
		} else {
		var arr = new Array();
		arr[arr.length] = "<table width='175' cellSpacing='0' cellPadding='0' border='0' bgcolor='#B8B8B9'>";
		for(var i=0; i<ds.Tables[0].Rows.length; i++) {
		  arr[arr.length] = "<tr><td colspan=2 align='left'>Result #" + (i+1) + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>Name: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].fname + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>County: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].county + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td align='left'>Type: </td>";
		  arr[arr.length] = "<td align='left'>" + ds.Tables[0].Rows[i].ptype + "</td></tr>";
		  arr[arr.length] = "<tr bgcolor='#F5F5F5'><td colspan=2 align='center'><input type=button class='divbtn' id='btnMapCommunity' value='Map It' onclick=\"fnMapCommunityMultiple('" + ds.Tables[0].Rows[i].fname + "', '" + ds.Tables[0].Rows[i].county + "', '" + ds.Tables[0].Rows[i].ptype + "', '" + ds.Tables[0].Rows[i].fips + "')\"></td></tr>";
		  arr[arr.length] = "<tr><td>&nbsp;</td></tr>";
		  arr[arr.length] = "<tr bgcolor='FFFFFF'><td colspan=2>&nbsp;</td></tr>";
		}
		arr[arr.length] = "</table>";
		document.getElementById('communResults').innerHTML = "Multiple Results Found..." + arr.join("");
		}
	} else {
	var rmap = response.value; 
	  if (rmap.url.indexOf('ERROR') > 0) {
		alert(rmap.url)
	  } else {
	  mapimg.src = rmap.url
	  xmin = rmap.xmin
	  xmax = rmap.xmax
	  ymin = rmap.ymin
	  ymax = rmap.ymax
	  }
	}
  }
  //fnScrollerStartup(false);
  fnInitiateConnect(sessionTime);
  hide();
}

function ajaxResponse_Map(response){
  if (response.error != null) {
	alert(response.error);
    return;
  } else {  
	if (response != null && response.value != null) {
	  var rmap = response.value; 
	  if (rmap.url.indexOf('error') > 0 || rmap.message!=null) {
		var txt=(rmap.message)? rmap.message : map.url;
		alert(txt)
	  } else {
	  mapimg.src = rmap.url
	  xmin = rmap.xmin
	  xmax = rmap.xmax
	  ymin = rmap.ymin
	  ymax = rmap.ymax
	  mapimg.style.top='0px';
	  mapimg.style.left='0px';
	  //(rmap.message)? alert(rmap.message):"";
	  }
	}
  }
  fnInitiateConnect(sessionTime);
  fnMoveDHTML();
  hide();
}

function ajaxResponse_GeoCode(response){
  if (response.error != null) {
	alert(response.error);
    return;
  } else {  
	if (response != null && response.value != null) {
	  var rmap = response.value; 
	  if (rmap.url.indexOf('error') > 0) {
		alert(rmap.url)
	  } else {
	  mapimg.src = rmap.url
	  xmin = rmap.xmin
	  xmax = rmap.xmax
	  ymin = rmap.ymin
	  ymax = rmap.ymax
	  dynDivWidth = 200;
	  //dynDivHeight = 95;
	  var x = 'gBox';
	  dynInsideContents = '<p class=\"notopgap\"><span style=\"float: right; position: relative;\"><img src=images/close.gif style=\"CURSOR: hand\" title=\"close\" onmouseover=\"clickDisable()\" onmouseout=\"clickEnable()\" onclick=\"fnCloseDHTML('+x+')\"></span></p><span style=\"float: left; font-family: Arial; font-weight: bold; font-size: 12px;">Address:</span><br><p>'+myMap.getStreet()+'</p><p class=\"notbottomgap\">'+myMap.getCity()+' '+myMap.getZip()+'</p>';
	  fnAddElement('gBox');
	  }
	}
  }
  fnInitiateConnect(sessionTime);
  hide();
}

function ajaxResponse_Identify(response){
  var ds = response.value;
  if(ds != null && typeof(ds) == "object" && ds.Tables != null) {
	var arr = new Array();
	arr[arr.length] = "<table>";
	for(var i=0; i<ds.Tables[0].Rows.length; i++) {
	  arr[arr.length] = "<tr>";
	  arr[arr.length] = "<td>" + ds.Tables[0].Rows[i].HeaderCol + ": </td>";
	  if (ds.Tables[0].Rows[i].HeaderCol == 'WEBSITE') {
	  arr[arr.length] = "<td><a href='" + ds.Tables[0].Rows[i].ValueCol + "' target=_blank>Click Here</a></td>";
	  } else {
	  arr[arr.length] = "<td>" + ds.Tables[0].Rows[i].ValueCol + "</td>";
	  }
	  arr[arr.length] = "</tr>";
	}
	arr[arr.length] = "</table>";
	document.getElementById('mapInfo').innerHTML = arr.join("");
	//alert(document.getElementById('mapInfo').innerHTML)
  }else{
	alert("Error: [3001] " + response.request.responseText);
  }
  fnInitiateConnect(sessionTime);
  fnCloseDHTML('gBox');
}

function ajaxResponse_SiteIdentify(response){
    var ds = response.value;
  if(ds != null && typeof(ds) == "object" && ds.Tables != null) {
	var arr = new Array();
	arr[arr.length] = "<table>";
	for(var i=0; i<ds.Tables[0].Rows.length; i++) {
	  if (ds.Tables[0].Rows[i].HeaderCol == 'MGRX') {
		geoX = ds.Tables[0].Rows[i].ValueCol;
	  } else {if (ds.Tables[0].Rows[i].HeaderCol == 'MGRY') {
		geoY = ds.Tables[0].Rows[i].ValueCol;
	  } else {if (ds.Tables[0].Rows[i].HeaderCol != 'radius') {
		arr[arr.length] = "<tr>";
		arr[arr.length] = "<td height=8 align=left><b>" + ds.Tables[0].Rows[i].HeaderCol + ": </b></td>";
		if (ds.Tables[0].Rows[i].HeaderCol == 'Facility ID') {
		   arr[arr.length] = "<td height=8 align=left><a href='http://www.deq.state.mi.us/sid-web/Tank_Detail.aspx?mod=ust&amp;mode=CGI&amp;Facility_ID=" + ds.Tables[0].Rows[i].ValueCol + "' target=_blank onmouseover='clickDisable()' onmouseout='clickEnable()' onclick='clickEnable()'>" + ds.Tables[0].Rows[i].ValueCol + "</a></td>";
		} else {
		arr[arr.length] = "<td height=8 align=left>" + ds.Tables[0].Rows[i].ValueCol + "</td>";
		arr[arr.length] = "</tr>";
		}
	  };};};
	}
	arr[arr.length] = "</table>";
	dynDivWidth = 300;
	//dynDivHeight = ((ds.Tables[0].Rows.length - 3)*17 + 65);
	var x = 'gBox';
	dynInsideContents = '<p class=\"notopgap\"><span style=\"float: right; position: relative;\"><img src=images/close.gif style=\"CURSOR: hand\" title=\"close\" onmouseover=\"clickDisable()\" onmouseout=\"clickEnable()\" onclick=\"fnCloseDHTML('+x+')\"></span></p><p>'+arr.join("")+'</p><p class=\"notbottomgap\">&nbsp;</p>';
	fnAddElement('gBox');
	fnMoveDHTML();
	fnSetIdentTool();
	}else{
	  alert("Error: [3001] " + response.request.responseText);
	}
  fnInitiateConnect(sessionTime);
}

function show() {
  ld.style.visibility = 'visible';
}

function hide() {
  ld.style.visibility = 'hidden';
}

//****Accessor Methods****//

function getMapObj() {
  return mapimg;
}

function getWidth() {
  return mapimg.width;
}

function getHeight() {
  return mapimg.height;
}

function getMapSource() {
  return mMapsource;
}
}

//map.prototype = new envelope();  //*** AJAX is not playing well with javascript inheritence
