
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';
	try {
	
	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	} catch (e) {
	//alert("Permission UniversalBrowserRead denied.");
} 
	//http = getHTTPObject();
	
	try {
	      http = new XMLHttpRequest();
	    } catch (trymicrosoft) {
	      try {
	        http = new ActiveXObject("Msxml2.XMLHTTP");
	      } catch (othermicrosoft) {
	        try {
	          http = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (failed) {
	          
	        }
	      }
    }
	

	if ((http != null) && (!isWorking)) {
	if ((http != null)) {
	try
	{
		
	    http.open("POST", url, true);
	    http.onreadystatechange = parseLayers;
	    
	    isWorking = true;
	    showLayer('loading');
	    http.send(axl);
	}
	catch(exc){}
  }
}
}

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);
     //window.status="minx==" + minx +", miny==" + miny + "   and maxx==" + maxx + ", maxy==" +maxy ;
     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){
        //alert("shift");
	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){

	
		
	minx = startX;
	miny = startY;
	
	/*if(document.forms[0].extent != null)
	{
	  document.forms[0].extent.value=minx;
	}*/
	
	
	
	
	maxx =endX;
	maxy = endY;
	
		

}
function convertPixelToMap(px, py){
	var dx = (maxx - minx)/mwidth;
	var mx = minx + (dx * px);
	var my = miny + (dx * py);
	//alert("mx>>>>>>>= "+mx);
	//alert("my>>>>>>>>= "+my);
	//window.status="mx==" + mx + "  my==" + my;
	var newpoint = new Array();
	newpoint[0] = mx;
	newpoint[1] = my;
	return newpoint;

}

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;
	//window.status="x1=" + newValues[0] + " y1=" + newValues[1];
	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;
     //alert("sending :" + axl);
    showLayer('loading');
    http.send(axl);
  }
}

function getMapRequest(envelope){
	//LayerVisible[2]=1;
	if(LayerVisibleName!="")
	
	for (var i=0;i<layerCount;i++) {
			
		        
			if(LayerName[i].toUpperCase()==LayerVisibleName.toUpperCase())
			{
			 
			  LayerVisible[i]=1;
			 }
		}


	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++) {
		//alert("layer name=" + LayerName[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 += '    </GET_IMAGE> </REQUEST></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) {

      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.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(" received");
	 //alert(minx +","+ miny +","+ maxx+","+maxy);

	
	// parseEnvelope(result, 0);
	// var theURL = getURL(result);
	//document.getElementById('theImage').src = theURL;
	hideLayer('loading');
    }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);

	}

}
