// aimsCommon.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsMap.js
*/

aimsCommonPresent=true;

// character used by browser in decimals - either point or comma

// process the start extent and set up layers
// for a GET_SERVICE_INFO response
function processStartExtent(theReply) {

	MapUnits = MapUnits.toUpperCase();
	if (MapUnits == "DECIMAL_DEGREES") {
		MapUnits = "DEGREES";
	}
	
	if (getStartingExtent) {
		pos = theReply.indexOf("<PROPERTIES");
		var theXYs =  getEnvelopeXYs(theReply, pos)
    reqExt.set(theXYs[0],theXYs[1],theXYs[2],theXYs[3]);
    startExt.setFrom(reqExt);
	} 
	else {
		reqExt.setFrom(startExt);
	}
	// Process reply to extract Theme and class info
	getThemesAndClasses(theReply);
	setDefaultLayers();

	mapScaleFactor = curExt.xSize / iWidth;
	mouseX = 0;
	mouseY = 0;
	pixelX = curExt.xSize/iWidth;
	pixelY = curExt.ySize/iHeight;
	mapX = curExt.minX;
	mapY = curExt.maxY;
 

	parent.TOCFrame.document.location=appDir + "toc.htm";

	hideRetrieveData();
	if ((ovIsVisible) && (aimsDHTMLPresent)) {
		ovIsVisible = false;
		toggleOVMap();
	}
	requestNewMainMap();
}

// request a list of available Image MapServices
function startMap() {
	if (debugOn > 0) {
		clientLogMemo("Starting client debug log");
	}
	if (hasOVMap) {
		document.ovImage.src = generateOVUrl();
		requestOVMap = false;
	}
	getInitContext();
}

// get the Map Image width
function getMapWidth () {
	var mapFrameWidth = thePageWin.innerWidth;
	
	if (mapFrameWidth == null) {
		mapFrameWidth = thePageDoc.body.clientWidth;
	}
	return mapFrameWidth;
}

 //get the Map Image height
function getMapHeight () {
	var mapFrameHeight = thePageWin.innerHeight;
	
	if (mapFrameHeight == null) {
		mapFrameHeight = thePageDoc.body.clientHeight;
	}
	return mapFrameHeight;
}

// Returns a copy of the string, with leading and trailing whitespace omitted
function trim(str) {
	return str.replace(/^ +/, '').replace(/ +$/, '');
}

// replace common HTML entitys with the characters they represent
function parseEntity(oldString) {
	oldString = oldString.replace(/&apos;/g, "'");
	oldString = oldString.replace(/&gt;/g, ">");
	oldString = oldString.replace(/&lt;/g, "<");
	oldString = oldString.replace(/&quot;/g, '"');
	oldString = oldString.replace(/&amp;/g, "&");
	return oldString;
}

// replace the five problem characters for the server's XML parser
function makeXMLsafe(oldString) {
	oldString = oldString.replace(/&/g, "&amp;");
	oldString = oldString.replace(/'/g, "&apos;");
	oldString = oldString.replace(/>/g, "&gt;");
	oldString = oldString.replace(/</g, "&lt;");
	oldString = oldString.replace(/"/g, "&quot;");
	return oldString;
}

// get the substring between beforeString and afterString, 
// starting at startpos	must be found before limitpos (0 
// for no limit) caseSensitive = true or false
function getInsideString(inString, beforeString, afterString,
                         startpos, limitpos, caseSensitive) {
	var returnString = "";
	var ucInString   = inString;
	var ucBefore     = beforeString;
	var ucAfter      = afterString;

	if (limitpos == 0) {
		limitpos = inString.length;
	}

	if (!caseSensitive) {
		ucInString = inString.toUpperCase();
		ucBefore   = beforeString.toUpperCase();;
		ucAfter    = afterString.toUpperCase();;
	}

	pos = ucInString.indexOf(ucBefore,startpos);
	//alert(startpos);

	if ((pos != -1) && (pos<limitpos)) {
		pos = pos + ucBefore.length;
		var endpos = ucInString.indexOf(ucAfter,pos);
		if (endpos >= 0) {
			returnString = inString.substring(pos,endpos);
		} else {
			// get through the end of the string if afterString is not found
			returnString = inString.substring(pos);
		}
	}	
	
	return returnString;
}

// inString is a single XML element, with no children.
// Search for attrName on inString and return 
// attribute value, or an empty string if not found.
function getXmlAttribute(inString, attrName) {

	// Modify search string to include a space char prefix.
	// Avoids getting a false match if attrName occurs
	// elsewhere in inString.
	attrName = " " + attrName;

	// find pos of attribute
	var idx = inString.indexOf(attrName);
	if (idx < 0) {
		return "";
	}

	// find position of attribute open quote
	var attrLeftBoundIdx = inString.indexOf('"', idx);
	// find position of attribute end quote
	var attrRightBoundIdx = inString.indexOf('"', attrLeftBoundIdx + 1);

	// extract attribute value
	if (attrLeftBoundIdx > 0 && attrRightBoundIdx > 0) {
		strOut = inString.slice(attrLeftBoundIdx + 1, attrRightBoundIdx);
		return strOut;
	}
	else {
		// alert('did not pass index test'); // DEBUG: for debugging
		return "";
	}
}