// showPictureValues.js
/*
*		
* Written by Ben Britton, IDWR
* This routine uses some variables that are declared for use by ESRI's "Print tool"
*		
*/
var rasterValue = "-1";  //it is a character string, because it can hold multi-band values, e.g. "153/133/21"
var rasterLayer = -1;
var rasterMult = -1.0;
var firstActiveRasterLayerIndex = -1;
var tmpLayerName;
var rasterLayerIndex;
var firstActiveRasterLayerID;

//<ARCXML version="1.1">
//  <REQUEST>
//    <GET_RASTER_INFO x="136.333846" y="60.075385" layerid="0" >
//      <COORDSYS id="4326" />
//    </GET_RASTER_INFO>
//  </REQUEST>
//</ARCXML>

function getRasterParameters(e) {
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
	getMapXY(mouseX,mouseY);
	theString = writePixelXML(mapX, mapY);
	sendToServer(imsURL,theString,1006);
}

// prepare the request in XML-format to obtain the value 
// of the current layer (raster) at the cursor location
function writePixelXML(uX, uY) {
//this works	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_RASTER_INFO x="' + uX + '" y="' + uY + '" layerid="2003TM" />\n';


// ***************************************************
// The active layer cannot be a raster.  We must find
// the first visible raster layer and identify pixel
// values with it.
// ***************************************************
//alert('LayerVisible.length =' + parent.MapFrame.LayerVisible.length + '; dlImageName.length=' + parent.MapFrame.dlImageName.length);
	// Set the firstActiveRasterLayerIndex.
	for (i=0; i<parent.MapFrame.LayerVisible.length; i++) {
		if (parent.MapFrame.LayerVisible[i] != 0) {
			for (var j=0; j<parent.MapFrame.dlImageName.length; j++) {
				if (parent.MapFrame.dlImageName[j] == parent.MapFrame.LayerName[i]) {
					//alert('Retrieving pixel values for ' + parent.MapFrame.dlImageName[j]);
					firstActiveRasterLayerIndex = i;	//set index relative to all layers (not just images)
					firstActiveRasterLayerID = parent.MapFrame.LayerID[i];
					rasterLayerIndex = j;			//this is used to find the multiplayer
					tmpLayerName = parent.MapFrame.dlImageName[j];
				}
			}
		}
	}
	//alert('firstActiveRasterLayerIndex = ' + firstActiveRasterLayerIndex);
	rasterMult = parent.MapFrame.pixMult[rasterLayerIndex];
	//alert('rasterMult = ' + rasterMult);

	//var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_RASTER_INFO x="' + uX + '" y="' + uY + '" layerid="' + ActiveLayer + '" />\n';
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_RASTER_INFO x="' + uX + '" y="' + uY + '" layerid="' + firstActiveRasterLayerID + '" />\n';

	theString += '</REQUEST>\n</ARCXML>';
	return theString;
}

function setPixelValue(theReply) {
// if the layer chosen is not an image layer there will be an error in the response...
//	alert(theReply);
	rasterValue = parseRasterInfoReply(theReply);
}

function parseRasterInfoReply(theReply) {
	var theString;
	var retVal="";
	var nbands=0;
	var thePos;
	var i;
	var rV=0.0;

	theString = theReply.substring(0,theReply.indexOf("ERROR"));
	if (theString != '') {
		alert("An error occured!\n\nYou must make the image you wish to query active then click within the active image layer to obtain a value!");
//		retVal = "-1";
//		return retVal;
		return rasterValue;
	}


	thePos = -1;
	thePos = theReply.indexOf("value=");
//alert(thePos);
	theString = theReply.substring(thePos+7);
//	theString = theReply.substring(theReply.indexOf("value=")+7);
//	if (theString != "") {
	if (thePos != -1) {
		nbands = nbands + 1;
		//Now, what should be left is the value for the first band (terminated by a ", then the rest of the string.
		rV = theString.substring(0, theString.indexOf('"'));
		//retVal = rasterMult * rV;
		rV = rasterMult * rV;
		rV = rV.toFixed(2);
		retVal = rV;
	}
	//go back, Jack, do it again...if it is a single band image, this will be it.
//	theString = theString.substring(theString.indexOf("value=")+7);
	thePos = -1;
	thePos = theString.indexOf("value=");
//alert(thePos);
	theString = theString.substring(thePos+7);
	if (thePos != -1) {
		nbands = nbands + 1;
		rV = theString.substring(0, theString.indexOf('"'));
		//rV = rV * rasterMult;
		rV = rasterMult * rV;
		rV = rV.toFixed(2);
		retVal = retVal + "," + rV;

		//go back, Jack, do it again...if it is a single band image, this will be it.
//		theString = theString.substring(theString.indexOf("value=")+7);
		thePos = -1
		thePos = theString.indexOf("value=");
//alert(thePos);
		theString = theString.substring(thePos+7);
//	if (theString != "") {
//		if (thePos < 1) {
			nbands = nbands + 1;
//			retVal = retVal + "," + theString.substring(0, theString.indexOf('"'));
			rV = theString.substring(0, theString.indexOf('"'));
			//rV = rV * rasterMult;
			rV = rasterMult * rV;
			rV = rV.toFixed(2);
			retVal = retVal + "," + rV;
//		}
	}
	retVal = retVal + '  [' + tmpLayerName + ']';
	return retVal;
}

