
function getInitialMap(){
	getMap();
}


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) && (!isWorking)) {
	if ((http != null)) {

	    http.open("POST", url, true);
	    http.onreadystatechange = parseLayers;
	    isWorking = true;
	    showLayer('loading');
	    http.send(axl);
  }
}


function parseLayers(){
  if (http.readyState == 4) {
  
   if (http.status == 200) {

      var result = http.responseText;
      //alert(result);
      getLayers(result); 
      getInitialMap();
   } else alert("Error retreiving data");
          
  }  
  
 }
function fixedZoomIn(){
    growEnvelope(0.75);
    var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}

function fixedZoomOut(){
	growEnvelope(1.25);
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
}
function zoom(left, bottom, right, top){
     if(activeTool == "zoomin") getExtentForZoomIn(left, bottom, right, top);
     else getExtentForZoomOut(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 LLPoint = getMapXY(left, bottom);
	var URPoint = getMapXY(right, top);
	minx = LLPoint[0];
	miny = LLPoint[1];
	maxx = URPoint[0];
	maxy = URPoint[1];
}


function getExtentForZoomOut(left, bottom, right, top){
		var xDiff= maxx-minx;
		var yDiff= maxy-miny;

		var pwidth = right-left;
		var pheight = top-bottom;
		var xRatio = mwidth / pwidth;
		var yRatio = mheight / pheight;
		var xAdd = xRatio * xDiff / 2;
		var yAdd = yRatio * yDiff / 2;
		minx =  minx - xAdd;
		maxx = maxx + xAdd;
		miny = miny - yAdd;
		maxy = maxy + yAdd;
		

}

function getMapXY(xIn,yIn) {
	var newValues = new Array();

	var mouseX = xIn;
	var pixelX = (maxx-minx) / mwidth;
	var newX = pixelX * mouseX + minx;
	var mouseY = mheight - yIn;
	var pixelY = (maxy-miny) / mheight;
	var newY = (pixelY * mouseY) + miny;
	newValues[0] = newX;

	newValues[1] = newY;
	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(minx, miny, maxx, maxy){
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	var axl = getMapRequest(env);
	 showLayer('loading');
	//alert(axl);
  	http = getHTTPObject();
	http.open("POST", url, true);
    http.onreadystatechange = printResponse;
	http.send(axl);
}

function getMap() {
  var queryUrl = url + "&CustomService=Query";
  http = getHTTPObject();
  if ((http != null)) {
	http.open("POST", queryUrl, true);
	var axl = sendQueryString(tax_id);
    http.onreadystatechange = getEnvelope;
    //alert("sending :" + axl);
    showLayer('loading');
	//alert(axl);
    http.send(axl);
  }
}

function getZoomedMap() {
  http = getHTTPObject();
  if ((http != null)) {
	http.open("POST", url, true);
	var axl = getMapWithCurrentExtent(envelope);
    showLayer('loading');
	//alert(axl);
    http.send(axl);
  }
}


function getMapRequest(envelope){
	var axl = '<?xml version="1.0" encoding="UTF-8"?>';
	axl += '<ARCXML version="1.1">';
	axl += ' 	<REQUEST>';
	axl += '    	<GET_IMAGE>';
	axl += '    		<PROPERTIES>';
	axl +=  				envelope;
	axl += '				<IMAGESIZE height="' + mheight + '" width="' + mwidth + '" />';
	axl += '				<LAYERLIST >\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>';
	axl += '			<LAYER type="featureclass" name="HighlightedFeature" visible="true">';
	axl += '				<DATASET fromlayer="0"/>';
	axl += '				<QUERY where= "pin=&quot;' + pin_old + '&quot;" />';
	axl += '				<SIMPLERENDERER>';
	axl += '					<SIMPLEPOLYGONSYMBOL boundary="true" boundarywidth="2" boundarycolor="0, 255, 255" filltransparency="0" />';
	axl += '				</SIMPLERENDERER>';
	axl += '			</LAYER>';
	axl += '   		</GET_IMAGE> </REQUEST></ARCXML>';
	//alert(axl);
	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) {

      isWorking = false;
      var result = http.responseText;
      
	//var result = http.responseXML;  //fails
	//alert(result);

	var xmlDoc;
	
      if(document.implementation && document.implementation.createDocument) { 
      	// MOZILLA 
       	 xmlDoc = document.implementation.createDocument("", "", null);
      	 xmlDoc.async="false";
         xmlDoc.loadXML(result);
      } else if (window.ActiveXObject){
      	 //IE
      	  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  
	  xmlDoc.async="false";
	  xmlDoc.loadXML(result);
       }
	   //alert(xmlDoc);
	//alert(xmlDoc.getElementsByTagName("OUTPUT").item(0).getAttribute("url"));
	var theURL = xmlDoc.getElementsByTagName("OUTPUT").item(0).getAttribute("url");
	 document.getElementById('theImage').src = theURL;
	 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"));
	  //alert(minx +","+ miny +","+ maxx+","+maxy);

	//alert(result + " received");
	// parseEnvelope(result, 0);
	// var theURL = getURL(result);
	//document.getElementById('theImage').src = theURL;
	hideLayer('loading');
    }else alert("Error retreiving data");
     
  } 
}

function getEnvelope(){

if (http.readyState == 4) {
    if (http.status == 200) {

      isWorking = false;
      var result = http.responseText;
      
	//var result = http.responseXML;  //fails
	//alert(result);
	var pos = result.indexOf("<FEATURE>");
	var theXYs = getSarahXYs(result, pos)
	var minx = theXYs[0] - 50;
	var miny = theXYs[1] - 50;
	var maxx = theXYs[2] + 50;
	var maxy = theXYs[3] + 50;
	//alert(minx +","+ miny +","+ maxx+","+maxy);
	getMapWithCurrentExtent(minx, miny, maxx, maxy)    
	}
	else alert("Error retreiving data");
     
  } 

}


/*
 * 
 * (Enhancement function to getXYs)
 * Gets the _entire_ map extents from xml reply
 * 
 * Called from aimsQuery.parseStartQuery instead of getXYs
 * Other functions that call getXYs have not been altered
 *
 * By Sarah Jolliffe - 21/7/2003   
 * Animal and Plant Health Service
 * Deparment of Primary Industries
 * Queensland, Australia
 */

function getSarahXYs(theString) {
	
	var dQuote = '"';
	var theReply = theString;
	var tempMinx;
	var tempMiny;
	var tempMaxx;
	var tempMaxy;

	var tempPos = theReply.indexOf("FEATURECOUNT",0);
	var tempStartPos = theReply.indexOf("count=", tempPos);
	tempStartPos += 7;
	var tempEndPos = theReply.indexOf(dQuote,tempStartPos);
	var featureCount = theReply.substring(tempStartPos, tempEndPos);


	var tempStr = "";
	var smallStr = "";
	var startpos = 0;
	var endpos = 0;

	for (i=0; i<featureCount; i++) {

		var XYArray = getEnvelopeXYs(theReply, startpos);

		if (i==0) {
			tempMinx = XYArray[0];
			tempMiny = XYArray[1];
			tempMaxx = XYArray[2];
			tempMaxy = XYArray[3];

		} else {

			if (XYArray[0] < tempMinx) tempMinx = XYArray[0];
			if (XYArray[3] > tempMaxy) tempMaxy = XYArray[3];
			if (XYArray[2] > tempMaxx) tempMaxx = XYArray[2];
			if (XYArray[1] < tempMiny) tempMiny = XYArray[1];
		}
		startpos = theReply.indexOf("ENVELOPE",startpos);
		startpos += 10; //so next iteration gets next envelope
		//alert(tempMinx, XYArray[1], XYArray[2], XYArray[3]);
	}
	XYArray[0] = tempMinx;
	XYArray[1] = tempMiny;
	XYArray[2] = tempMaxx;
	XYArray[3] = tempMaxy;
	
	return XYArray;
}
function getEnvelopeXYs(theString, startpos) {
	var dQuote='"';
	var theEnvelope = new Array();
	//forceCommaInRequest[activeMapServiceIndex] = false;
	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)); 
		xmlEndPos = endpos;
	}
	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[activeMapServiceIndex] = true;
	if (decimalChar==".") {
		numberString = numberString.replace(/,/g, ".");
	} else {
		numberString = numberString.replace(/./g, ",");
	}
	return numberString;
}

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);

	}

}


