/*
  USGS_Profile Tool
  
  Description:
    This JavaScript provides an extension to the HTML ArcIMS viewer that will 
    allow the user to enter several points and request an Elevation Profile
    following those points.  They can specify the points, Data source, and 
    either an interval in decimal degrees, or a number of samples to make 
    between points.
    
  Requirements:
    The Elevation_Profile_Chart PHP application must be installed on the server
    with the phpChartDirector from advsofteng installed on the server also. A 
    virtual directory should be setup so that this exists off the root of the 
    server.  Also, the getElevation_Profile.aspx must be in the XMLWebServices
    directory of the server.
    
    These have already been done on GISDATA servers.
     
  Created By:  Chris Rusanowski
  
  Installation:
    -Copy USGS_Elevation_Profile.js into the javascript directory of the HTML Viewer
    -Include the javascript/USGS_Elevation_Profile.js in MapFrame.htm 
    -Add a button to the toolbar that calls parent.MapFrame.setProfileTool();
    -Add code below to aimsCustom.js so the map tool functions...
      if (toolMode == 1089) { 
        Profile_Add_Point_To_Array(e);
        return false; 
      }
    
    -Copy Silver_Elevation_Profile.gif and Red_Elevation_Profile.gif to usgs_images directory, and add button using these to toolbar... Code similar to below:
      if (parent.MapFrame.useElevationProfile) { selectToolCount++; }
        if (parent.MapFrame.useElevationProfile) {
          if (parent.MapFrame.modeName == "ElevationProfile") {
            LocalString += WriteButton( MakeButton2( "usgs_images/red_elevation_Profile.gif", 'Elevation Profile Tool', "elevation_profile", "parent.MapFrame.modeName=\'ElevationProfile\'; parent.MapFrame.Profile_setProfileTool();", 'status=\'Elevation Profile Tool\'', "", true ) );
          } else {
            LocalString += WriteButton( MakeButton2( "usgs_images/silver_elevation_Profile.gif", 'Elevation Profile Tool', "elevation_profile", "parent.MapFrame.modeName=\'ElevationProfile\'; parent.MapFrame.Profile_setProfileTool();", 'status=\'Elevation Profile Tool\'', "", true ) );
          }
          buttonCol++;
          LocalString += Table_Structure(buttonCol,selectToolCount);
        }
      
  Future Enhancements:
    -Display the profile segments on the map
      This is just a matter of copying what the measure tool does into this
      script and adding the code to the appropriate ESRI code...
    -Vertical Exaggeration
    -Plotting Segment, or sample point markers on X-Axis of profile  
*/
var useElevationProfile=true;
var Profile_Point_Array = new Array();
var Profile_Interval='';
var Profile_Num_Samples='10';
var Profile_Elev_DataSet='-1';
var Profile_Width="600";
var Profile_Height="400";
var Profile_Skip_Zeros="FALSE";
var Profile_Units="FEET";


/*
  function Add_Point_To_Array
*/
function Profile_Add_Point_To_Array(e) {
  var theX = mouseX;
  var theY = mouseY;
  getMapXY(theX,theY);
  var Num_Points = Profile_Point_Array.length;
  Profile_Point_Array[Num_Points] = mapX;
  Profile_Point_Array[Num_Points+1] = mapY;
  Profile_Generate_User_Prompt_HTML();

  // draw click points and lines between them on map

  clickPointX[clickCount]=mapX;
  clickPointY[clickCount]=mapY;
  clickCount += 1;

  legendTemp=legendVisible;
  legendVisible=false;
  var theString = writeXML();
  var theNum = 99;
  sendToServer(imsURL,theString,theNum);
				
}


/*
  function Reset_Point_Array
*/
function Profile_Reset_Point_Array() {
  Profile_Point_Array = new Array();
  Profile_Generate_User_Prompt_HTML();
}

/*
  function Profile_Open_Chart
*/
function Profile_Open_Chart() {
  var temp_URL="http://"+ parent.USGShost + "/XMLWebServices/getElevation_Profile.aspx?Chart=1";
  temp_URL += "&POINTS_LIST=" + Profile_Point_Array.join(',');
  temp_URL += "&INTERVAL=" + Profile_Interval;
  temp_URL += "&NUM_SAMPLES=" + Profile_Num_Samples;
  temp_URL += "&ELEV_DATASET=" + Profile_Elev_DataSet;
  temp_URL += "&ELEV_UNITS=" + Profile_Units;
  temp_URL += "&WIDTH=" + Profile_Width;
  temp_URL += "&HEIGHT=" + Profile_Height;
  temp_URL += "&SKIP_ZEROS=" + Profile_Skip_Zeros;
  //prompt("temp_URL" , temp_URL);
  var temp_Window_Settings='titlebar=no,personalbar=no,directories=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+Profile_Width+',height='+Profile_Height;
  window.open(temp_URL, '', temp_Window_Settings);
}

/*
  function Generate_User_Prompt_HTML
*/
function Profile_Generate_User_Prompt_HTML() {
  var Num_Points = Profile_Point_Array.length;
  var theFrame = "parent.MapFrame";
  var WindowContent='';
	if ((useExternalWindow) || (!useTextFrame)) {
    theFrame = "opener";
    if (parent.MapFrame!==null) { theFrame = "opener.parent.MapFrame"; }
	}
  var HTML_Content = '';
  if (Num_Points<1) {
      HTML_Content += '<b>Please Select a starting Point for the Elevation Profile by clicking on the map</b>';
  } else if (Num_Points>0 && Num_Points<3) {
    HTML_Content += '<b>Please Select a second Point for the Elevation Profile by clicking on the map</b>';
  } else {
    //HTML_Content += 'Generate a Profile between the ' + Num_Points / 2 + ' points by using the form below, or add additional points by clicking on the map.<br />';
    HTML_Content += '<form name="Profile_Form" method="POST" onsubmit="return ' + theFrame + '.Process_Profile();">';
    HTML_Content += '<button type="button" value="Generate" name="Generate" onclick="' + theFrame + '.Process_Profile();">Generate Profile for ' + Num_Points / 2 + ' Points</button>&nbsp;<button type="button" value="Clear" name="Clear" onclick="' + theFrame + '.Profile_Clear_Points();">Reset Points</button>';
    HTML_Content += '<br /><frameset><legend>Profile Options (<a href="javascript:void(0);" onclick="' + theFrame + '.Profile_Options_Help();">help</a>):</legend>';
    HTML_Content += 'Dataset:<select name="Elev_Dataset" onchange="' + theFrame + '.Profile_Process_Elev_DataSet(this);">';
    if (Profile_Elev_DataSet=="-1") { HTML_Content += '<option value="-1" selected="selected">Best Available</option>'; } else { HTML_Content += '<option value="-1">Best Available</option>'; }
    if (Profile_Elev_DataSet=="NED.BATHY_DELAWARE_BAY_ELEV_METERS") { HTML_Content += '<option value="NED.BATHY_DELAWARE_BAY_ELEV_METERS" selected="selected">Delaware Bay Topobathy Meters</option>'; } else { HTML_Content += '<option value="NED.BATHY_DELAWARE_BAY_ELEV_METERS">Delaware Bay Topobathy Meters</option>'; }
    if (Profile_Elev_DataSet=="NED.BATHY_TAMPA_BAY_ELEV_METERS") { HTML_Content += '<option value="NED.BATHY_TAMPA_BAY_ELEV_METERS" selected="selected">Tampa Bay Topobathy Meters</option>'; } else { HTML_Content += '<option value="NED.BATHY_TAMPA_BAY_ELEV_METERS">Tampa Bay Topobathy Meters</option>'; }
    if (Profile_Elev_DataSet=="NED.PGTSND_NED_19") { HTML_Content += '<option value="NED.PGTSND_NED_19" selected="selected">NED 1/9th Puget Sound</option>'; } else { HTML_Content += '<option value="NED.PGTSND_NED_19">NED 1/9th Puget Sound</option>'; }
    if (Profile_Elev_DataSet=="NED.NC_LIDAR_ELEVATION") { HTML_Content += '<option value="NED.NC_LIDAR_ELEVATION" selected="selected">NC LIDAR</option>'; } else { HTML_Content += '<option value="NED.NC_LIDAR_ELEVATION">NC LIDAR</option>'; }
    if (Profile_Elev_DataSet=="NED.TI_W_NED_13") { HTML_Content += '<option value="NED.TI_W_NED_13" selected="selected">NED 1/3rd West Territories</option>'; } else { HTML_Content += '<option value="NED.TI_W_NED_13">NED 1/3rd West Territories</option>'; }
    if (Profile_Elev_DataSet=="NED.TI_E_NED_13") { HTML_Content += '<option value="NED.TI_E_NED_13" selected="selected">NED 1/3rd East Territories</option>'; } else { HTML_Content += '<option value="NED.TI_E_NED_13">NED 1/3rd East Territories</option>'; }
    if (Profile_Elev_DataSet=="NED.LN_ND_NED_3") { HTML_Content += '<option value="NED.LN_ND_NED_3" selected="selected">NED 3m Lincoln, NE</option>'; } else { HTML_Content += '<option value="NED.LN_ND_NED_3">NED 3m Lincoln, NE</option>'; }
    if (Profile_Elev_DataSet=="NED.CONUS_NED_13") { HTML_Content += '<option value="NED.CONUS_NED_13" selected="selected">NED 1/3rd CONUS</option>'; } else { HTML_Content += '<option value="NED.CONUS_NED_13">NED 1/3rd CONUS</option>'; }
    if (Profile_Elev_DataSet=="NED.LN9741_NED_10") { HTML_Content += '<option value="NED.LN9741_NED_10" selected="selected">NED 10m Lincoln, NE</option>'; } else { HTML_Content += '<option value="NED.LN9741_NED_10">NED 10m Lincoln, NE</option>'; }
    if (Profile_Elev_DataSet=="NED.CONUS_NED") { HTML_Content += '<option value="NED.CONUS_NED" selected="selected">NED 1 arc-second CONUS</option>'; } else { HTML_Content += '<option value="NED.CONUS_NED">NED 1 arc-second CONUS</option>'; }
    if (Profile_Elev_DataSet=="NED.AK_NED") { HTML_Content += '<option value="NED.AK_NED" selected="selected">NED 2 arc-second AK</option>'; } else { HTML_Content += '<option value="NED.AK_NED">NED 2 arc-second AK</option>'; }
    if (Profile_Elev_DataSet=="NED.HI_NED_13") { HTML_Content += '<option value="NED.HI_NED_13" selected="selected">NED 1/3rd HI</option>'; } else { HTML_Content += '<option value="NED.HI_NED_13">NED 1/3rd HI</option>'; }
    if (Profile_Elev_DataSet=="NED.HI_NED") { HTML_Content += '<option value="NED.HI_NED" selected="selected">NED 1 arc-second HI</option>'; } else { HTML_Content += '<option value="NED.HI_NED">NED 1 arc-second HI</option>'; }
    if (Profile_Elev_DataSet=="NED.PR_NED") { HTML_Content += '<option value="NED.PR_NED" selected="selected">NED 1 arc-second PR</option>'; } else { HTML_Content += '<option value="NED.PR_NED">NED 1 arc-second PR</option>'; }
    if (Profile_Elev_DataSet=="SRTM.US_1_ELEVATION") { HTML_Content += '<option value="SRTM.US_1_ELEVATION" selected="selected">SRTM US</option>'; } else { HTML_Content += '<option value="SRTM.US_1_ELEVATION">SRTM US</option>'; }
    if (Profile_Elev_DataSet=="SRTM.AE_1_ELEVATION") { HTML_Content += '<option value="SRTM.AE_1_ELEVATION" selected="selected">SRTM US Aleutians</option>'; } else { HTML_Content += '<option value="SRTM.AE_1_ELEVATION">SRTM US Aleutians</option>'; }
    if (Profile_Elev_DataSet=="SRTM.TE_1_ELEVATION") { HTML_Content += '<option value="SRTM.TE_1_ELEVATION" selected="selected">SRTM US Territories</option>'; } else { HTML_Content += '<option value="SRTM.TE_1_ELEVATION">SRTM US Territories</option>'; }
    if (Profile_Elev_DataSet=="SRTM.NA_3_ELEVATION") { HTML_Content += '<option value="SRTM.NA_3_ELEVATION" selected="selected">SRTM North America</option>'; } else { HTML_Content += '<option value="SRTM.NA_3_ELEVATION">SRTM North America</option>'; }
    if (Profile_Elev_DataSet=="SRTM.SA_3_ELEVATION") { HTML_Content += '<option value="SRTM.SA_3_ELEVATION" selected="selected">SRTM South America</option>'; } else { HTML_Content += '<option value="SRTM.SA_3_ELEVATION">SRTM South America</option>'; }
    HTML_Content += '</select>';
    HTML_Content += '<br />Samples/segment:<select name="Num_Samples" onchange="' + theFrame + '.Profile_Process_Num_Samples(this);">';
    if (Profile_Num_Samples=="5") { HTML_Content += '<option value="5" selected="selected">5</option>'; } else { HTML_Content += '<option value="5">5</option>'; }
    if (Profile_Num_Samples=="10") { HTML_Content += '<option value="10" selected="selected">10</option>'; } else { HTML_Content += '<option value="10">10</option>'; }
    if (Profile_Num_Samples=="20") { HTML_Content += '<option value="20" selected="selected">20</option>'; } else { HTML_Content += '<option value="20">20</option>'; }
    if (Profile_Num_Samples=="50") { HTML_Content += '<option value="50" selected="selected">50</option>'; } else { HTML_Content += '<option value="50">50</option>'; }
    if (Profile_Num_Samples=="100") { HTML_Content += '<option value="100" selected="selected">100</option>'; } else { HTML_Content += '<option value="100">100</option>'; }
    if (Profile_Num_Samples=="200") { HTML_Content += '<option value="200" selected="selected">200</option>'; } else { HTML_Content += '<option value="200">200</option>'; }
    HTML_Content += '</select>';
    HTML_Content += '&nbsp;OR&nbsp;Interval (Degrees):<select name="Interval" onchange="' + theFrame + '.Profile_Set_Interval(this);">';
    if (Profile_Interval=="") { HTML_Content += '<option value="" selected="selected">None</option>'; } else { HTML_Content += '<option value="">None</option>'; }
    if (Profile_Interval==".1") { HTML_Content += '<option value=".1" selected="selected">.1</option>'; } else { HTML_Content += '<option value=".1">.1</option>'; }
    if (Profile_Interval==".05") { HTML_Content += '<option value=".05" selected="selected">.05</option>'; } else { HTML_Content += '<option value=".05">.05</option>'; }
    if (Profile_Interval==".01") { HTML_Content += '<option value=".01" selected="selected">.01</option>'; } else { HTML_Content += '<option value=".01">.01</option>'; }
    if (Profile_Interval==".005") { HTML_Content += '<option value=".005" selected="selected">.005</option>'; } else { HTML_Content += '<option value=".005">.005</option>'; }
    if (Profile_Interval==".001") { HTML_Content += '<option value=".001" selected="selected">.001</option>'; } else { HTML_Content += '<option value=".001">.001</option>'; }
    if (Profile_Interval==".0005") { HTML_Content += '<option value=".0005" selected="selected">.0005</option>'; } else { HTML_Content += '<option value=".0005">.0005</option>'; }
    if (Profile_Interval==".0001") { HTML_Content += '<option value=".0001" selected="selected">.0001</option>'; } else { HTML_Content += '<option value=".0001">.0001</option>'; }
    HTML_Content += '</select>';
    HTML_Content += '<br />Units:<select name="Units" onchange="' + theFrame + '.Profile_Set_Units(this);">';
    if (Profile_Units=="FEET") { HTML_Content += '<option value="FEET" selected="selected">FEET</option>'; } else { HTML_Content += '<option value="FEET">FEET</option>'; }
    if (Profile_Units!="FEET") { HTML_Content += '<option value="METERS" selected="selected">METERS</option>'; } else { HTML_Content += '<option value="METERS">METERS</option>'; }
    HTML_Content += '</select>';
    HTML_Content += '&nbsp;Width:<select name="Width" onchange="' + theFrame + '.Profile_Set_Width(this);">';
    if (Profile_Width=="200") { HTML_Content += '<option value="200" selected="selected">200</option>'; } else { HTML_Content += '<option value="200">200</option>'; }
    if (Profile_Width=="400") { HTML_Content += '<option value="400" selected="selected">400</option>'; } else { HTML_Content += '<option value="400">400</option>'; }
    if (Profile_Width=="600") { HTML_Content += '<option value="600" selected="selected">600</option>'; } else { HTML_Content += '<option value="600">600</option>'; }
    if (Profile_Width=="700") { HTML_Content += '<option value="700" selected="selected">700</option>'; } else { HTML_Content += '<option value="700">700</option>'; }
    if (Profile_Width=="800") { HTML_Content += '<option value="800" selected="selected">800</option>'; } else { HTML_Content += '<option value="800">800</option>'; }
    if (Profile_Width=="900") { HTML_Content += '<option value="900" selected="selected">900</option>'; } else { HTML_Content += '<option value="900">900</option>'; }
    if (Profile_Width=="1000") { HTML_Content += '<option value="1000" selected="selected">1000</option>'; } else { HTML_Content += '<option value="1000">1000</option>'; }
    if (Profile_Width=="1100") { HTML_Content += '<option value="1100" selected="selected">1100</option>'; } else { HTML_Content += '<option value="1100">1100</option>'; }
    HTML_Content += '</select>';
    HTML_Content += '&nbsp;Height:<select name="Height" onchange="' + theFrame + '.Profile_Set_Height(this);">';
    if (Profile_Height=="100") { HTML_Content += '<option value="100" selected="selected">100</option>'; } else { HTML_Content += '<option value="100">100</option>'; }
    if (Profile_Height=="200") { HTML_Content += '<option value="200" selected="selected">200</option>'; } else { HTML_Content += '<option value="200">200</option>'; }
    if (Profile_Height=="300") { HTML_Content += '<option value="300" selected="selected">300</option>'; } else { HTML_Content += '<option value="300">300</option>'; }
    if (Profile_Height=="400") { HTML_Content += '<option value="400" selected="selected">400</option>'; } else { HTML_Content += '<option value="400">400</option>'; }
    if (Profile_Height=="500") { HTML_Content += '<option value="500" selected="selected">500</option>'; } else { HTML_Content += '<option value="500">500</option>'; }
    if (Profile_Height=="600") { HTML_Content += '<option value="600" selected="selected">600</option>'; } else { HTML_Content += '<option value="600">600</option>'; }
    if (Profile_Height=="700") { HTML_Content += '<option value="700" selected="selected">700</option>'; } else { HTML_Content += '<option value="700">700</option>'; }
    if (Profile_Height=="800") { HTML_Content += '<option value="800" selected="selected">800</option>'; } else { HTML_Content += '<option value="800">800</option>'; }
    HTML_Content += '</select>';
    /*HTML_Content += '&nbsp;Skip Zeroes?:<select name="Skip_Zeroes" onchange="' + theFrame + '.Profile_Set_Zeroes(this);">';
    if (Profile_Skip_Zeros=="TRUE") { HTML_Content += '<option value="TRUE" selected="selected">TRUE</option>'; } else { HTML_Content += '<option value="TRUE">TRUE</option>'; }
    if (Profile_Skip_Zeros!="TRUE") { HTML_Content += '<option value="FALSE" selected="selected">FALSE</option>'; } else { HTML_Content += '<option value="FALSE">FALSE</option>'; }
    HTML_Content += '</select>';*/
    HTML_Content += '<input type="hidden" name="Points_List" value="' + Profile_Point_Array.join(',') + '" /></frameset>';
    HTML_Content += '</form>';
  }
  Create_Pop_Up_Window("Elevation_Profile", "", HTML_Content, "Elevation_Profile", "", false)
}

/*
  function setProfileTool
*/
function Profile_setProfileTool() {
  var isOk;
  var j;    
	if (hasLayer("measureBox")) {
		hideLayer("measureBox");
  }
  panning=false;
  zooming=false;
  selectBox=false;
  shapeSelectBuffer = false;
  toolMode = 1089;
  if (canQuery) {
  	if (isIE)	{
  		document.all.theTop.style.cursor = "crosshair";
  		theCursor = document.all.theTop.style.cursor;
  	}
  	modeBlurb = "USGS Elevation Profile";
  } else {
  	alert(msgList[46]);
  }
  showGeocode=false;
  drawSelectBoundary=false;
  //if (useTextFrame) { parent.TextFrame.document.location= appDir + "getElevation.htm"; }

  //Profile_Point_Array.length=0
  //Profile_Generate_User_Prompt_HTML();
  Profile_Reset_Point_Array();

  hideLayer("measureBox");

  resetProfileClick();

}

// zero out all clicks in Num_Points
function resetProfileClick() {

	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	
	legendTemp=legendVisible;
	legendVisible=false;
	var theString = writeXML();
	var theNum = 99;

	sendToServer(imsURL,theString,theNum);
		
}	

function Process_Profile() {
  Profile_Open_Chart();return false;
}
function Profile_Clear_Points() {
  resetProfileClick();
  Profile_Reset_Point_Array();return false;
}
function Profile_Set_Interval(Interval) {
  Profile_Interval=Interval.value;return false;
}
function Profile_Process_Num_Samples(Num_Samples) {
  Profile_Num_Samples=Num_Samples.value;return false;
}
function Profile_Process_Elev_DataSet(Elev_DataSet) {
  Profile_Elev_DataSet=Elev_DataSet.value;return false;
}
function Profile_Set_Height(Height) {
  Profile_Height=Height.value;return false;
}
function Profile_Set_Width(Width) {
  Profile_Width=Width.value;return false;
}
function Profile_Set_Units(Units) {
  Profile_Units=Units.value;return false;
}
function Profile_Set_Zeroes(Skip_Zeroes) {
  Profile_Skip_Zeroes=Skip_Zeroes.value;return false;
}

function Profile_Options_Help() {
  var HTML_Content = '';
  HTML_Content += 'The default elevation profile options will generate a fast chart.  The options below allow the user to generate a custom chart that may portray the elevation profile more accurately, or at a different size.  A description of each option is below:<br /><br />';
  HTML_Content += 'Dataset<ul>The Best Available Query will tell the USGS Elevation Web Service to return the first matching dataset at each point, starting from the Highest resolution data and proceeding to the lowest resolution dataset.  This means a query that crosses multiple datasets will have the results from multiple datasets combined into one chart.  It does not give any indication of which dataset each point comes from.  The other dataset options will query only a single dataset, and if that dataset does not have coverage or values, then it will display a 0 or null value at that point on the profile.</ul>';
  HTML_Content += 'Samples<ul>A profile is generated by calculating an increment along a line drawn between each point in the points list.  The number of samples specifies how many elevation queries (samples) will be made between each set of points.</ul>';
  HTML_Content += 'Interval<ul>The Interval setting over-rides the samples option, if it is set.  This is used to calculate a Samples value based upon the segment and the distance in decimal degrees specified in this option.  This allows for each segment to have a similar sample ratio based upon the length of the segment and thus makes the profile proportional to the actual length of the lines.</ul>';
  HTML_Content += '<span style="color:#FF0000;"><small>NOTE: Setting a small Interval, or large number of samples can make generating the profile take a long time, or time-out.  Each segment is limited to a maximum of 500 samples, no matter which option above is specified.</small></span>';
  HTML_Content += '<br /><br />Units<ul>The USGS Elevation Web Service can return values in Feet or Meters, so you can specify which one your profile should use.</ul>';
  HTML_Content += 'Height and Width<ul>This is the height and width (in pixels) of the pop-up window that contains the profile.</ul>';
  Create_Pop_Up_Window("Elevation_Profile_Option_Help", "", HTML_Content, "Elevation_Profile_Option_Help", 'titlebar=no,personalbar=no,directories=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+Profile_Width+',height='+Profile_Height, true)
}

