function getServiceInfo(){
	var axl = '<?xml version="1.0" encoding="UTF-8"?><ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO renderer="false" extensions="false" fields="false" />\n</REQUEST>\n</ARCXML>\n';
	http = getHTTPObject();
	if ((http != null)) {
	    http.open("POST", url, true);
	    http.onreadystatechange = parseLayers;
	    showLayer('loadingMap');
	    http.send(axl);
	}
}

function parseLayers(){
 	if (http.readyState == 4) {
		if (http.status == 200) {
			var result = http.responseText;
			getLayers(result);
			sendSubdQuery();
		} else alert("Error retreiving data");  
	}
}

function fixedZoomIn(){
	growEnvelope(0.5);
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}

function fixedZoomOut(){
	growEnvelope(2.0);
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}

function fixedZoomOutPoint(cx,cy){
	var value=1.5;
	var dx = maxx - minx;
	var dy = maxy - miny;
	var dx1 = 0.5 * value * dx;
	var dy1 = 0.5 * value * dy;
	minx = cx - dx1;
	miny = cy - dy1;
	maxx = cx + dx1;
	maxy = cy + dy1;
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}

function zoom(left, bottom, right, top){
	getExtentForZoomIn(left, bottom, right, top);	
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);	
}

function pan(ix, iy){
	var dx = (maxx - minx)/mwidth;
	var mx = dx*ix;
	var my = dx*iy;
	minx += mx;
	maxx += mx;
	miny += my;
	maxy += my;
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
     	getMap(env);
}

function panDirection(dir){
	shift(dir);
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
     	getMap(env);
}

function shift(dir){
	var dx = maxx - minx;
	var dy = maxy - miny;
		
	switch(dir) {
		//NORTH
		case "north":
			miny += 0.3*dy;
			maxy += 0.3*dy;
		break;
		//south
		case "south":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
		break;
		//east
		case "east":
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//west
		case "west":
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break;
		//northeast
		case "ne":
			miny += 0.3*dy;
			maxy += 0.3*dy;
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//northwest
		case "nw":
			miny += 0.3*dy;
			maxy += 0.3*dy;
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break;
		//southeast
		case "se":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//southwest
		case "sw":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break
		
	}
}


function getExtentForZoomIn(left, bottom, right, top){
	var idMins = convertPixelToMap(left-(pixelTolerance/2), bottom+(pixelTolerance/2));
	var idMaxs = convertPixelToMap(right+(pixelTolerance/2), top-(pixelTolerance/2));
	minx = idMins[0];
	miny = idMins[1];
	maxx = idMaxs[0];
	maxy = idMaxs[1];
}

function getMapXY(e) {
	var newValues = new Array();
	getXY(e);
	var x1 = theX - mleft;
	var y1 = theY - mtop;
	var idMins = convertPixelToMap(x1-(pixelTolerance/2), y1+(pixelTolerance/2));
	var idMaxs = convertPixelToMap(x1+(pixelTolerance/2), y1-(pixelTolerance/2));
	var pointX=(idMaxs[0]+idMins[0])/2;
	var pointY=(idMaxs[1]+idMins[1])/2;
	newValues[0] = pointX;
	newValues[1] = pointY;
	return newValues; 
}

function growEnvelope(value){
	var dx = maxx - minx;
	var dy = maxy - miny;

	var cx = (maxx + minx)/2.0;
	var cy = (maxy + miny)/2.0;

	var dx1 = 0.5 * value * dx;
	var dy1 = 0.5 * value * dy;

	minx = cx - dx1;
	miny = cy - dy1;
	maxx = cx + dx1;
	maxy = cy + dy1;
}

function getMapWithCurrentExtent(){
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}

function getMap(envelope) {
	http = getHTTPObject();
	if ((http != null)) {
		var axl = getMapRequest(envelope);
		http.open("POST", url, true);
		http.onreadystatechange = printResponse;
		showLayer('loadingMap');
		http.send(axl);
	}
}

function getMapRequest(envelope){
	var axl = '<?xml version="1.0" encoding="UTF-8"?>\n';
	var clrstr = "";


	axl += '<ARCXML version="1.1">\n';
	axl += '  <REQUEST>\n';
	axl += '    <GET_IMAGE>\n';
	axl +=  '     <PROPERTIES>\n';
	axl += envelope;
	axl += '        <IMAGESIZE height="' + mheight + '" width="' + mwidth + '" />\n';
	axl += '        <LAYERLIST dynamicfirst="false" nodefault="false" order="false">\n';
	for (var i=0;i<layerCount;i++) {
		if (LayerVisible[i]==1) {
			axl += '        <LAYERDEF id="' + LayerID[i] + '" visible="true" />\n';
		} else {
			axl += '        <LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
		}
	}
	axl += '        </LAYERLIST>\n';
	axl += '      </PROPERTIES>\n';

//	GeoCode address
	if (showGC) {
		axl += '<LAYER type="acetate" name="GeoCode1">\n';
		axl += '<OBJECT units="database">\n<POINT coords="' + GCpointX + " " + GCpointY + '">\n';
		axl += '<SIMPLEMARKERSYMBOL  type="circle"  color="' + geocodePointColor + '" width="' + geocodePointSize +'" overlap="false" />\n</POINT></OBJECT>\n';
		if (GCaddress!="") {
			axl += '<OBJECT units="database">\n<TEXT coords="' + GCpointX + " " + GCpointY + '" label=" ' + GCaddress + '">\n';
			axl += '<TEXTMARKERSYMBOL fontcolor="' + geocodePointColor + '" fontsize="11" glowing="255,204,204" halignment="right" valignment="top" overlap="false" /></TEXT>\n';
		}
		axl += '</OBJECT>\n';
		axl += '</LAYER>\n';
	}
	axl += '    </GET_IMAGE>\n';
        axl += '  </REQUEST>\n';
        axl += '</ARCXML>';
	return axl;
}



//need to define loadXML function for non IE browser which do not have loadXML implemented
//we use loadXML (can't use load method) to load ArcXML response because the response from ArcIMS is in plain text format   

if(!isIE){
	Document.prototype.loadXML = function (s) {
      
	   	// parse the string to a new doc   
	   	var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
      
	   	// remove all initial children
	   	while (this.hasChildNodes())
	   	   this.removeChild(this.lastChild);
         
	   	// insert and import nodes
	   	for (var i = 0; i < doc2.childNodes.length; i++) {
	   	   this.appendChild(this.importNode(doc2.childNodes[i], true));
	   	}
       };
}


function printResponse(){
	if (http.readyState == 4) {
		if (http.status == 200) {
			var result = http.responseText;
      			var theURL;
			//var result = http.responseXML;  //fails
			//alert(result);
			if (navigator.userAgent.indexOf( 'Safari' ) != -1) {		//no xmlDoc for Safari
				theURL = getURL(result);
				parseEnvelope(result, 0);
			} else {
				var xmlDoc;
				if (document.implementation && document.implementation.createDocument) { 
					// MOZILLA 
					xmlDoc = document.implementation.createDocument("", "", null);
      					xmlDoc.async="false";
        				xmlDoc.loadXML(result);
					theURL = xmlDoc.getElementsByTagName("OUTPUT").item(0).getAttribute("url");
				} else if (window.ActiveXObject){
					//IE
					xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(result);
					theURL = xmlDoc.getElementsByTagName("OUTPUT").item(0).getAttribute("url");
				}
				var env = xmlDoc.getElementsByTagName("ENVELOPE").item(0);
				minx = parseFloat(env.getAttribute("minx"));
				miny = parseFloat(env.getAttribute("miny"));
				maxx = parseFloat(env.getAttribute("maxx"));
				maxy = parseFloat(env.getAttribute("maxy"));
			}
			document.getElementById('theImage').src = theURL;
			hideLayer('loadingMap');

		} else alert("Error retreiving data"); 
	} 
}

function getURL(theReply) {
	var theURL = "";
	var startpos = 0;
	var endpos = 0;

	var pos = theReply.indexOf("OUTPUT");
	var pos1 = theReply.indexOf("url", pos);
	pos1 += 5;
	var pos2 = theReply.indexOf("\"", pos1);
	theURL = theReply.substring(pos1,pos2);


	return theURL;

}

function parseEnvelope(theString, startpos) {
	theString = theString.toUpperCase();
	var tempString = "";
	var dQuote="\"";
	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);
		minx = parseFloat(tempString);
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		miny = parseFloat(tempString);
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		maxx = parseFloat(tempString);
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		maxy = parseFloat(tempString);

	}

}

var decimalChar = ((("theChar is" + (10/100)).indexOf("."))==-1) ? "," : ".";
// set number string to have decimal character to match browser language type - point or comma
function setDecimalString(numberString) {
	if (numberString.indexOf(",")!=-1) forceCommaInRequest[activeMapServiceIndex] = true;
	if (decimalChar==".") {
		numberString = numberString.replace(/,/g, ".");
	} else {
		numberString = numberString.replace(/./g, ",");
	}
	return numberString;
}