// sjcParsing.js
// Copyright (C) 2005 San Joaquin County Community Development Geographic Information Systems, ALL RIGHTS RESERVED
// initial creation: 01/14/2004 David Bollinger
//




// getTagValue()
// return string between open/close tag markers
//
function getTagValue(sXML,iPos,sTag,sTagEnd) {
	var value = '';
	var iTagPos = sXML.indexOf(sTag,iPos);
	if (iTagPos != -1) { // found start of tag
		var iTagEnd = sXML.indexOf(sTagEnd, iTagPos);
		if (iTagEnd != -1) { // found end of tag
			value = sXML.substring(iTagPos, iTagEnd + sTagEnd.length);
		}
	}
	return value;
}

// getAttValue()
// return attribute value from single-tag string
//
function getAttValue(sXML,sAtt,sDefault) {
	var value = sDefault || '';
	var iAttPos = sXML.indexOf(sAtt+'="', 0);
	if (iAttPos != -1) { // found start of att
		var iValPos = iAttPos + sAtt.length + 2;
		var iValEnd = sXML.indexOf('"', iValPos);
		if ((iValEnd > iValPos)) { // found end of val
			value = sXML.substring(iValPos, iValEnd);
		}
	}
	return value;
}


// getTagAttValue()
// db@sjc
// returns the value of specified attribute with specified tag
// a better version of ESRI's justGetValue() in aimsCommon.js
// example, to parse out url attribute from OUTPUT tag:
// var sURL = getTagAttValue(theReply,'OUTPUT','url','');
//
function getTagAttValue(sXML,sTag,sAtt,sDefault) {
	var value = sDefault || '';
  var iTagPos = sXML.indexOf('<'+sTag);
	if (iTagPos != -1) { // found start of tag
		var iTagEnd = sXML.indexOf('>', iTagPos);
		if (iTagEnd != -1) { // found end of tag
			var iAttPos = sXML.indexOf(sAtt+'="', iTagPos);
			if (iAttPos != -1) { // found start of att
				var iValPos = iAttPos + sAtt.length + 2;
				var iValEnd = sXML.indexOf('"', iValPos);
				if ((iValPos != -1)  && (iValPos < iTagEnd)) { // found end of val
					value = sXML.substring(iValPos, iValEnd);
				}
			}
		}
	}
 return value;
}





// get min and max x,y's from xml stream . . . return an array with values
function getEnvelopeXYs(theString, startpos) {
	var theEnvelope = new Array();
	theEnvelope[0] = s2n(getTagAttValue(theString, 'ENVELOPE', 'minx', '0'));
	theEnvelope[1] = s2n(getTagAttValue(theString, 'ENVELOPE', 'miny', '0'));
	theEnvelope[2] = s2n(getTagAttValue(theString, 'ENVELOPE', 'maxx', '0'));
	theEnvelope[3] = s2n(getTagAttValue(theString, 'ENVELOPE', 'maxy', '0'));
	//log.dump('envelope ' + theEnvelope[0] + ' ' + theEnvelope[1] + ' ' + theEnvelope[2] + ' ' + theEnvelope[3]);
	return theEnvelope;
}



// check if there is an error message in the response
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;
}





