function openSTUDYAREA(UNIQUEID)
{
//alert(UNIQUEID);
top.myWindow=top.top.dhtmlwindow.open('myWindow','iframe','/SHOREKEEPERS/STUDYAREA_edit.php?editid1=' + UNIQUEID, 'Study Area', 'width=800px,height=600px,resize=1,scrolling=1,center=1');
}


function digFirstPoint()
{
		//alert('got here');
		top.iFrameMap.DigitizePoint(top.OnFirstPointDigitized);
		top.myWindow.hide();
}

function digLastPoint()
{
		//alert('got here');
		top.iFrameMap.DigitizePoint(top.OnLastPointDigitized);
		top.myWindow.hide();
}


function OnFirstPointDigitized(point) 
{
	

	var ZONE = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_UTM_ZONE.value;
	var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleFirstResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "/SHOREKEEPERS/getUTMandLLfromSPHEREM.php?X="+point.X+"&Y="+point.Y+"&ZONE="+ZONE, true); 
  xmlHttp.send(null);
}
function OnLastPointDigitized(point) 
{
	

	var ZONE = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_UTM_ZONE.value;
	var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleLastResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "/SHOREKEEPERS/getUTMandLLfromSPHEREM.php?X="+point.X+"&Y="+point.Y+"&ZONE="+ZONE, true); 
  xmlHttp.send(null);
}

function HandleFirstResponse(response)
{
  
  coords = response.split(","); 

	//alert(response);
	X = parseFloat(coords[0]);
    Y = parseFloat(coords[1]);
    LAT = parseFloat(coords[3]);
    LON = parseFloat(coords[2]);
    
   
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_UTM_X.value = X;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_UTM_Y.value = Y;

    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_LAT.value = LAT;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_LONG.value = LON;



	top.myWindow.show();
   
}

function HandleLastResponse(response)
{
  
  coords = response.split(","); 

	//alert(response);
	X = parseFloat(coords[0]);
    Y = parseFloat(coords[1]);
    LAT = parseFloat(coords[3]);
    LON = parseFloat(coords[2]);
    
   
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_UTM_X.value = X;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_UTM_Y.value = Y;

    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_LAT.value = LAT;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_LONG.value = LON;



	top.myWindow.show();
   
}



function convertFIRSTUTMCoordinates()
{
	var ZONE = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_UTM_ZONE.value;
    var X = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_UTM_X.value;
    var Y = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_UTM_Y.value;
    
    var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleFIRSTUTMResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "../../../SHOREKEEPERS/getLLfromUTM.php?X="+X+"&Y="+Y+"&ZONE="+ZONE, true); 
  xmlHttp.send(null);

}

function convertLASTUTMCoordinates()
{
	var ZONE = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_UTM_ZONE.value;
    var X = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_UTM_X.value;
    var Y = document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_UTM_Y.value;
    
    var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleLASTUTMResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "../../../SHOREKEEPERS/getLLfromUTM.php?X="+X+"&Y="+Y+"&ZONE="+ZONE, true); 
  xmlHttp.send(null);

}


function getXMLHttp()
{
  var xmlHttp
  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}



function HandleFIRSTUTMResponse(response)
{
  
  coords = response.split(","); 

	X = parseFloat(coords[0]);
    Y = parseFloat(coords[1]);

    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_LONG.value = X;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_ZEROM_LAT.value = Y;
    
}

function HandleLASTUTMResponse(response)
{
  
  coords = response.split(","); 

	X = parseFloat(coords[0]);
    Y = parseFloat(coords[1]);

    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_LONG.value = X;
    document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_MAXM_LAT.value = Y;
    
}


////DIGIZE LINESTRING
function digLine()
{
top.parent.iFrameMap.mapFrame.ClearSelection();
var theScale=top.parent.iFrameMap.mapFrame.GetScale();


if(theScale > 2000000)
	{
	alert("You must zoom into 1:20,000 Scale or more to Digitze");
	}
	else
	{
		top.parent.iFrameMap.mapFrame.DigitizeLineString(top.OnLineDigitized);
		top.myWindow.hide();
	}

}
 function OnLineDigitized(lineString)
 {
	var totalCoords = "";
	var coords = "";
	
	for(var i=0;i<lineString.Count;i++)
	{
	point  = lineString.Point(i);
	
	var xmlHttp = getXMLHttp();
	xmlHttp.open("GET", "../../../EELGRASS/getLLfromALbers.php?X="+point.X+"&Y="+point.Y, false);
	xmlHttp.send(null);
	 
	if (xmlHttp.status == 200)
	{
		coords = ReturnPointResponse(xmlHttp.responseText);
	}
  	
  	//for each point, add the pairs together. Oracle wants 3D so set the Z to 0
  	totalCoords = coords + ",0," + totalCoords;
	
	}	
	//remove the extra comma off the end...
	//totalCoords = totalCoords.substr(0,totalCoords.length-1); //don't need it with new trigger- needs the comma at the end now
	
	//finally, populate the COORDS column in the form
	top.myWindow.show();

	document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_COORDS.value=totalCoords;
	
	
	
}

////DIGIZE POLYGON
function digPolygon()
{
top.parent.iFrameMap.mapFrame.ClearSelection();
var theScale=top.parent.iFrameMap.mapFrame.GetScale();


if(theScale > 2000000)
	{
	alert("You must zoom into 1:20,000 Scale or more to Digitze");
	}
	else
	{
		top.parent.iFrameMap.mapFrame.DigitizePolygon(top.OnPolygonDigitized);
		top.myWindow.hide();
	}

}
 function OnPolygonDigitized(lineString)
 {
	var totalCoords = "";
	var coords = "";
	
	for(var i=0;i<lineString.Count;i++)
	{
	point  = lineString.Point(i);
	
	var xmlHttp = getXMLHttp();
	xmlHttp.open("GET", "../../../EELGRASS/getLLfromALbers.php?X="+point.X+"&Y="+point.Y, false);
	xmlHttp.send(null);
	 
	if (xmlHttp.status == 200)
	{
		coords = ReturnPointResponse(xmlHttp.responseText);
	}
	

  	
  	//for each point, add the pairs together. Oracle wants 3D so set the Z to 0
  	totalCoords = coords + ",0," + totalCoords;
	
	}	
	//remove the extra comma off the end...or don't bother because the trigger likes the end comma
	//totalCoords = totalCoords.substr(0,totalCoords.length-1);
	
	//finally, populate the COORDS column in the form
	document.getElementById("_iframe-myWindow").contentWindow.document.forms[0].value_COORDS.value=totalCoords;
	
	top.myWindow.show();
	
	
}

//The response is used by both POLYGON and POLYLINE functions

function ReturnPointResponse(response)
{
  var coords = response; 
  return coords;
}


//ZOOM AND SELECT CALL

function ZoomAndSelect(KEY,KEYNAME)
{
	var MapName =  top.parent.iFrameMap.mapFrame.GetMapName(); 
	var Session =  top.parent.iFrameMap.mapFrame.GetSessionId();
	var LayerName = "EELGRASS";
	var ResourceName = "Library://EELGRASS/1_DATA/GISDB_ADSK_EELGRASS.FeatureSource"; 
    
    top.parent.iFrameMap.GetScriptFrame().location.href= "ZOOMANDSELECT.php?SESSION=" + Session + "&MAPNAME=" + MapName + "&LAYERNAME=" + LayerName + "&RESOURCENAME=" + ResourceName  + "&KEYNAME=" + KEYNAME + "&KEY=" + KEY;
 
	
}


function ZoomAndSelectGPS(KEY,KEYNAME)
{
	var MapName =  top.parent.iFrameMap.mapFrame.GetMapName(); 
	var Session =  top.parent.iFrameMap.mapFrame.GetSessionId();
	var LayerName = "TBL_GPS_POINTS";
	var ResourceName = "Library://EELGRASS/1_DATA/GISDB_ADSK_EELGRASS.FeatureSource"; 
    
    top.parent.iFrameMap.GetScriptFrame().location.href= "ZOOMANDSELECT.php?SESSION=" + Session + "&MAPNAME=" + MapName + "&LAYERNAME=" + LayerName + "&RESOURCENAME=" + ResourceName  + "&KEYNAME=" + KEYNAME + "&KEY=" + KEY;
 
	
}