//*****************************************************************************
//
// USGS Greenness Web Service Query Tool 
// Created By:  Chris Rusanowski
// Installation:
//   -Copy USGS_Greenness_Service.js into the javascript directory of the HTML Viewer
//   -Include the javascript/USGS_Greenness_Service.js in MapFrame.htm 
//      <script type="text/javascript" language="JavaScript" src="javascript/USGS_Greenness_Service.js"></script>
//   -Add a button to the toolbar.htm that calls parent.MapFrame.setGreennessTool().
//     (Actually add a button to USGS_Toolbar.js... The code below is and example from that JS file.)
//      if (parent.MapFrame.useGreennessService)  { miscCount++; }
//        if (parent.MapFrame.useGreennessService) {
//          if (parent.MapFrame.modeName == "Greenness") {
//            LocalString += WriteButton( MakeButton2( "usgs_images/Greenness1.gif", 'Greenness Query Tool', "Greenness", "parent.MapFrame.modeName=\'Greenness\'; parent.MapFrame.setGreennessTool();", 'status=\'Greenness Query Tool\'', "", true ) );
//          } else {
//            LocalString += WriteButton( MakeButton2( "usgs_images/Greenness2.gif", 'Greenness Query Tool', "Greenness", "parent.MapFrame.modeName=\'Greenness\'; parent.MapFrame.setGreennessTool();", 'status=\'Greenness Query Tool\'', "", true ) );
//          }
//          buttonCol++;
//          LocalString += Table_Structure(buttonCol,miscCount);
//        }
//   -Add code below to aimsCustom.js so the map tool functions...
//      if (toolMode == 1090) { 
//        getGreenness(e);
//        return false; 
//      }
//   -Copy the getGreenness.php file into the main HTML viewer directory 
//     (IE: same directory mapframe.htm is in...)
//   -Copy the Greenness1.gif and Greenness2.gif into the usgs_images folder.
//*****************************************************************************
// useGreennessService determines if the toolbar should show this button or not.
var useGreennessService = true;
//*****************************************************************************
// Function setGreennessTool
// This function sets the current tool to the Greenness Query Tool
// Sets the toolMode to 1090
//*****************************************************************************
function setGreennessTool() {
  var isOk;
  var j;    
	if (hasLayer("measureBox")) {
		hideLayer("measureBox");
  }
  panning=false;
  zooming=false;
  selectBox=false;
  shapeSelectBuffer = false;
  toolMode = 1090;
  if (canQuery) {
  	if (isIE)	{
  		document.all.theTop.style.cursor = "crosshair";
  		theCursor = document.all.theTop.style.cursor;
  	}
  	modeBlurb = "USGS Greenness Query";
  } else {
  	alert(msgList[46]);
  }
  showGeocode=false;
  drawSelectBoundary=false;
  //if (useTextFrame) { parent.TextFrame.document.location= appDir + "getGreenness.asp?FIELDS=USER_WAIT"; }
  if (useTextFrame) { parent.TextFrame.document.location= appDir + "getGreenness.php?FIELDS=USER_WAIT"; }
  hideLayer("measureBox");
}

//*****************************************************************************
// Function getGreenness
// This function pops a new window (or the text frame) with the Greenness Service
// query using the GET methodology and passing a return url that points to 
// getGreenness.php.  This is what gets called from the aimsCustom.js
//*****************************************************************************
function getGreenness(e) {
  var theX = mouseX;
  var theY = mouseY;
  getMapXY(theX,theY);
  if ((useExternalWindow) || (!useTextFrame)) {
    window.open("/XMLWebServices/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php&FIELDS=QUERYING_SERVICE","Greenness_Service","width=425,height=300,scrollbars=yes,resizable=yes");
	} else {
    //parent.TextFrame.document.location = "http://edcw2ks51.cr.usgs.gov/XMLWebServices/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php&FIELDS=QUERYING_SERVICE";
    //var search = prompt("URL","http://edcw2ks51.cr.usgs.gov/XMLWebServices/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php&FIELDS=QUERYING_SERVICE");
    //parent.TextFrame.document.location = "/XMLWebServices2/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.asp?&FIELDS=QUERYING_SERVICE";
	//var newWin = window.open("","thisWin","width=400,height=400");
	//newWin.document.writeln("/XMLWebServices2/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php?&FIELDS=QUERYING_SERVICE");
    parent.TextFrame.document.location = "/XMLWebServices2/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php?&FIELDS=QUERYING_SERVICE";
	   //window.open("/XMLWebServices/getGreenness.aspx?X=" + mapX + "&Y=" + mapY + "&RETURNURL=http://" + hostName + appDir + "getGreenness.php?mapX&FIELDS=QUERYING_SERVICE");
	}
}

//*****************************************************************************
// Function get_Greenness_Rows
// This function parses the response from the server and adds it to the 
// the Greenness_HTML
//*****************************************************************************
function get_Greenness_Rows(FIELDCOUNT, FIELDS_Array, Value_Array, theX, theY) {
    var Greenness_HTML = "<p><b>Lat:&nbsp;&nbsp;" + theX + "&nbsp;&nbsp;&nbsp; Long:&nbsp;&nbsp;" + theY + "</b></p>";

   	Greenness_HTML += "<table border=1 cellpadding=0 cellspacing=2>";

 	 if (FIELDS_Array[0]!="") { 
		Greenness_HTML += "<thead><tr>";
		
		for (var i=0; i<= FIELDCOUNT ; i++) {
			if (FIELDS_Array[i]!="Data_ID") {
			Greenness_HTML += "<td>" + FIELDS_Array[i] + "</td>";
			}
		}
		Greenness_HTML += "</tr></thead>";
		
		for (var j=0; j < Value_Array[0].length; j++) {
			Greenness_HTML += "<tr>";
			for (var i=0; i <= FIELDCOUNT; i++) {
				if (FIELDS_Array[i]!="Data_ID") {
				Greenness_HTML += "<td>" + Value_Array[i][j] + "</td>";
				//prompt("greennness", Greenness_HTML);
				}
			}	 
			Greenness_HTML += "</tr>"; 
		}
		
	} else {
		Greenness_HTML += "<thead><tr>";
		Greenness_HTML += "<td>No Data returned.</td>";
		Greenness_HTML += "</tr></thead>";  
	}
	Greenness_HTML += "</table>";
 	
	Create_Pop_Up_Window("Drought Monitoring", "", Greenness_HTML, "Drought Monitoring", "", false);
	//Must have this line otherwise will get "undefine" string.
	return "";
}