// aimsXML.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*/

aimsXMLPresent=true;
/********************************************************
	Functions for sending XML requests and XML reponses
*********************************************************/

// common dynamic variables
var XMLMode = 1;
var okToSend = true;

// boolean for determining if extent coordinates should have 
// comma instead of point for decimals
var forceCommaInRequest = false;

var requestOVMap = true; //if true, overview map url will be reset with next map draw.
if (hasOVMap != true) requestOVMap = false;
								
// process the response xml
function processXML(theReplyIn) {
	var theReply = parseEntity(theReplyIn);
	okToSend = true;

	if (debugOn > 2) {
		theReply = formatXML(theReply);
		clientLogResponse(msgList[8] + theReply);
	}

	switch (XMLMode) {
	
		case REQUEST_TYPE_GET_IMAGE: 
			customImageResponseProcess(theReply);
			hideRetrieveMap();
			break;
	
		case REQUEST_TYPE_SERVICE_INFO: //  just get full extent - service info
	
			if (getLimitExtent) {
				var pos = theReply.indexOf("<PROPERTIES");
				var theXYs =  getEnvelopeXYs(theReply, pos)
	
				limitMinX = theXYs[0];
				limitMinY = theXYs[1];
				limitMaxX = theXYs[2];
				limitMaxY = theXYs[3];
			}
	
	
			forceCommaInRequest = false;
			processStartExtent(theReply);
			break;
	
		default:
			alert(theReply + msgList[4] + ' :  XMLMode = ' + XMLMode);

	} // end switch
}

// get the map extents from xml reply
function getXYs(theString) {
	var theXYs =  getEnvelopeXYs(theString, 0)
	curExt.set(theXYs[0],theXYs[1],theXYs[2],theXYs[3]);
}

function getURL(theReply) {
	var theURL = "";
	theURL = getInsideString(theReply,'url="',dQuote,0,0,false);
	return theURL;
}


// get min and max x,y's from xml stream . . . return an array with values
function getEnvelopeXYs(theString, startpos) {
	var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var tempString = "";
	var pos = theString.indexOf("ENVELOPE",startpos);
	if (pos!=-1) {
		pos = pos + 8;
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[0] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[1] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[2] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[3] = parseFloat(setDecimalString(tempString)); 
	}
	return theEnvelope;

}

// set number string to have decimal character to match browser language type - point or comma
function setDecimalString(numberString) {
	if (numberString.indexOf(",")!=-1) forceCommaInRequest = true;
	if (decimalChar==".") {
		numberString = numberString.replace(/,/g, ".");
	} else {
		numberString = numberString.replace(/./g, ",");
	}
	return numberString;
}

function forceComma(theNumber) {
	var comma = ",";
	var dot = ".";
	var charOut = comma;
	var charIn = dot;
	var numberString = new String(theNumber);
	if (forceCommaInRequest) {
		charOut = dot;
		charIn = comma;
	}
	var pos = numberString.indexOf(charOut);
	if (pos!=-1) {
		var begin = numberString.substring(0,pos);
		var ending = numberString.substring(pos+1, numberString.length);
		numberString = begin + charIn + ending;
	}
	return numberString;
}

// check if there is an error message in the response
// THIS METHOD IS DUPLICATED IN Authorize.htm
function getXMLErrorMessage(theString) {
	var pos1 = 0;
	var pos2 = 0;
	var pos3 = 0;
	var theError = "";
	pos3 = theString.indexOf("<ERROR");
	if (pos3!=-1) {
		pos1 = theString.indexOf(">",pos3);
		pos1 += 1;
		pos2 = theString.indexOf("</ERROR");
		theError = theString.substring(pos1,pos2)
	}
	return theError;
}