var crossStreet;
var Response;
var theCoords;
var streetAdd;
var zipCode="";
var XCoordEnvelope;
var YCoordEnvelope;
var AcetateLayers;
var AddressLabel;
var theCustom = "";
var SchoolType;
var NameofSchool="";
var AddofSchool="";
var CityofSchool="";
var ZipofSchool="";
var PhoneofSchool="";
var PrincofSchool="";
var VPofSchool="";
var WebsiteofSchool="";
//determines how the response will be processed
	var hostName = document.location.host;
	var serverURL  = "http://" + hostName + "/servlet/com.esri.esrimap.Esrimap?ServiceName=";
	var cVersion = "&ClientVersion=9";
	var theService;
	var imsURL;
//	var imsURL = serverURL + theService + cVersion;
var XMLMode;
var imsURL;
// current extent vars
var minx;
var miny;
var maxx;
var maxy;
// full extent vars
var startMinx;
var startMiny;
var startMaxx;
var startMaxy;
// Zoom-to vars
var dQuote = '"'

function sendServiceXML(){
	parent.SchoolInfoFrame.location = "./SchoolInfo.htm";
	parent.ReadmeFrame.location = "ReadmeFrame.htm";
  var MapService = getService();
  var theRequest = writeXML("Service");
  sendToServer(imsURL,theRequest,1);
}
function sendMapXML(){
  var theRequest = writeXML("Map");
  sendToServer(imsURL,theRequest,2);
}
function sendCoordsXML(){
  var theRequest = writeXML("Coords");
  sendToServer(imsURL,theRequest,5);
}
function sendAddressXML(){
//	parent.SchoolInfoFrame.location = "./SchoolInfo.htm";
  var theRequest = writeXML("Address");
  //alert("THE REQUEST IS:                         " + theRequest);
  sendToServer(imsURL,theRequest,3);
}
function sendAttribsXML(){
  var theRequest = writeXML("Attribs");
  sendToServer(imsURL,theRequest,4);
}

function writeXML(XMLType){
	var theProlog = '<?xml version="1.0" encoding="UTF-8" ?>\n';
	var theXML = theProlog;
  if (XMLType == "Service") {
    // GET_SERVICE_INFO request
    theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_SERVICE_INFO fields="false" envelope="true" renderer="false" extensions="false" />';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
  }
  else if (XMLType == "Map") {
    // GET_IMAGE request
    theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_IMAGE>';
	theXML = theXML + '<PROPERTIES>';
	theXML = theXML + '<ENVELOPE minx="' + minx + '" miny="' + miny + '" maxx="' + maxx + '" maxy="' + maxy +'" />';
	theXML = theXML + '<IMAGESIZE width="500" height="480"  />';
	theXML = theXML + '</PROPERTIES>';
	theXML = theXML + AcetateLayers;
//	theXML = theXML + '<LAYER type="acetate" name="Condor_Logo">';
//	theXML = theXML + '<OBJECT units="pixel">';
//	theXML = theXML + '<POINT coords="475 25" >';
//	theXML = theXML + '<RASTERMARKERSYMBOL image="L:/ArcIMS/Data/MUSD/graphics/Birdbutton.gif" antialiasing="false" />';
//	theXML = theXML + '</POINT>';
//	theXML = theXML + '</OBJECT>';
//	theXML = theXML + '</LAYER>';
	theXML = theXML + '</GET_IMAGE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
  }
  else if (XMLType == "Coords") {
  theCustom = "";
  var theSchoolString = Response;
  var SchoolName = getSchoolName(theSchoolString);
	getSchoolAdd(theSchoolString);
  	getSchoolCity(theSchoolString);
  	getSchoolZip(theSchoolString);
  	getSchoolPhone(theSchoolString);
  	getSchoolPrinc(theSchoolString);
  	getSchoolVP(theSchoolString);
  	getSchoolWebsite(theSchoolString);
  	// GET_IMAGE request
	theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_IMAGE show="layers">';
	theXML = theXML + '<PROPERTIES>';
	theXML = theXML + '<ENVELOPE minx="' + minx + '" miny="' + miny + '" maxx="' + maxx + '" maxy="' + maxy +'" />';
	theXML = theXML + '<IMAGESIZE width="500" height="480" />';
	theXML = theXML + '</PROPERTIES>';
	theXML = theXML + '<LAYER type="featureclass" featurelimit="10" name="theBlock" id ="SelectedBlock">';
	theXML = theXML + '<DATASET fromlayer="' + SchoolType + '" />';
	theXML = theXML + '<SPATIALQUERY where="SCHOOL=&apos;' + SchoolName + '&apos;">';
	theXML = theXML + '</SPATIALQUERY>';
	theXML = theXML + '<SIMPLERENDERER>';
	theXML = theXML + '<SIMPLEPOLYGONSYMBOL boundarycolor="255,255,0" boundarywidth="3" filltransparency="0" fillcolor="255,255,0"/>';
	theXML = theXML + '</SIMPLERENDERER>';
	theXML = theXML + '</LAYER>';
	theXML = theXML + '<LAYER type="acetate" name="YouAreHere">';
	theXML = theXML + '<OBJECT units="database">';
	theXML = theXML + '<POINT coords="' + XCoordEnvelope + ', ' + YCoordEnvelope + '" >';
	theXML = theXML + '<SIMPLEMARKERSYMBOL width="20" type="Star" color="255,255,0" antialiasing="true"/>';
	theXML = theXML + '</POINT>';
	theXML = theXML + '</OBJECT>';
	theXML = theXML + '</LAYER>';
	theXML = theXML + '<LAYER type="acetate" name="YouAreHereLabel">';
	theXML = theXML + '<OBJECT units="database">';
	theXML = theXML + '<TEXT coords="' + XCoordEnvelope + ', ' + YCoordEnvelope + '" label="' + AddressLabel + '" >';
	theXML = theXML + '<TEXTMARKERSYMBOL overlap="false" antialiasing="true" font="Arial" fontsize="9" shadow="64,64,64" glowing="255,255,255" valignment="top" halignment="right"/>';
	theXML = theXML + '</TEXT>';
	theXML = theXML + '</OBJECT>';
	theXML = theXML + '</LAYER>';
//	theXML = theXML + '<LAYER type="acetate" name="Condor_Logo">';
//	theXML = theXML + '<OBJECT units="pixel">';
//	theXML = theXML + '<POINT coords="475 25" >';
//	theXML = theXML + '<RASTERMARKERSYMBOL image="D:/ArcIMS/Data/MUSD/graphics/Birdbutton.gif" antialiasing="false" />';
//	theXML = theXML + '</POINT>';
//	theXML = theXML + '</OBJECT>';
//	theXML = theXML + '</LAYER>';
	theXML = theXML + '</GET_IMAGE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	var Layers = getLayers(theXML);
	}
  else if (XMLType == "Address") {
  theCustom = "&CustomService=Geocode";
	var theInputForm = parent.LegendFrame.document.forms[0];
	var streetAdd = theInputForm.streetAdd.value;
//	var zipCode = theInputForm.zipCode.value;
	var crossStreet = theInputForm.crossStreet.value;
	if (crossStreet == "") {
		AddressLabel = theInputForm.streetAdd.value;
	} else {
		AddressLabel = theInputForm.streetAdd.value + " &amp; " + theInputForm.crossStreet.value;
	}
    //GET_GEOCODE request
	theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_GEOCODE maxcandidates="10" minscore="25">';
	theXML = theXML + '<LAYER id="Streets" />';
	theXML = theXML + '<ADDRESS>';
	theXML = theXML + '<GCTAG id="STREET" value="' + streetAdd + '"/>';
	theXML = theXML + '<GCTAG id="Zone" value="' + zipCode + '"/>';
	theXML = theXML + '<GCTAG id="CrossStreet" value="' + crossStreet + '"/>';
	theXML = theXML + '</ADDRESS>';
	theXML = theXML + '</GET_GEOCODE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	}
  else if (XMLType == "Attribs") {
  theCustom = "&CustomService=Query";
  var theCoordString = Response;
  var XCoord = getXCoord(theCoordString);
  var YCoord = getYCoord(theCoordString);
    //GET_FEATURES request
	theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_FEATURES outputmode="xml" featurelimit="1" envelope="true" geometry="false">';
	theXML = theXML + '<LAYER id="' + SchoolType + '" />';
	theXML = theXML + '<SPATIALQUERY subfields="#ALL#">';
	theXML = theXML + '<SPATIALFILTER relation="area_intersection">';
	theXML = theXML + '<MULTIPOINT>';
	theXML = theXML + '<POINT x="' + XCoord + '" y="' + YCoord + '" />';
	theXML = theXML + '</MULTIPOINT>';
	theXML = theXML + '</SPATIALFILTER>';
	theXML = theXML + '</SPATIALQUERY>';
	theXML = theXML + '</GET_FEATURES>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	}
  return theXML;
}

function sendToServer(imsURL,theRequest,theType) {
	//set the XML Mode and send request to server
	XMLMode = theType;
	var thePostForm = parent.PostFrame.document.forms[0];
	thePostForm.action=imsURL + theCustom + "&Form=True";
	thePostForm.ArcXMLRequest.value=theRequest;
////////////////////////////////////////////////////////////////////////////
	//alert("THE REQUEST IS:                         " + theRequest);
////////////////////////////////////////////////////////////////////////////
	thePostForm.JavaScriptFunction.value = "parent.LegendFrame.processXML";
	thePostForm.submit();
}

function processXML(theReply){
////////////////////////////////////////////////////////////////////////////
	//alert("THE ANSWER IS:                     " + theReply);
////////////////////////////////////////////////////////////////////////////
	Response = theReply;
	switch(XMLMode) {
	case 1:  //response is from a GET_SERVICE_INFO request
		  //extract the initial envelope
		  var theEnvelope = getEnvelopeXYs(theReply);
		  minx = theEnvelope[0];
		  miny = theEnvelope[1];
		  maxx = theEnvelope[2];
		  maxy = theEnvelope[3];
		  //store initial extent
		  startMinx = theEnvelope[0];
		  startMiny = theEnvelope[1];
		  startMaxx = theEnvelope[2];
		  startMaxy = theEnvelope[3];
//		  sendMapXML();
		  sendAddressXML();
		break

	case 2:  //response is from a GET_IMAGE request
		  var theURL = "";
		    //extract the image URL
			theURL = getURL(theReply);
			var NewURL = getNewURL(theURL);
			NewURL = "../../output/" + NewURL;
		if (parent.ReadmeFrame.location == "http://www.condordataserver.com:81/website/musd/ButtonFrame.htm" )  {
			parent.MapFrame.document.theMapImage.src = NewURL;
			}
		else
			sendAddressXML();
		break

	case 3:  //response is from a GET_GEOCODE request
		var GCCount = getGCCount(theReply);
		if (GCCount == 0 )  {
			alert("ADDRESS NOT FOUND... PLEASE RETRY WITHOUT A ZIPCODE...");
			theCustom = "";
			}
		else
		  sendAttribsXML();
		break

	case 4:  //response is from a GET_FEATURES request
		  var theEnvelope = getEnvelopeXYs(theReply);
		  minx = theEnvelope[0];
		  miny = theEnvelope[1];
		  maxx = theEnvelope[2];
		  maxy = theEnvelope[3];
		  sendCoordsXML();
		break

	case 5:  //response is from a GET_IMAGE request
		  var theURL = "";
		    //extract the image URL
			theURL = getURL(theReply);
//		    parent.MapFrame.document.theMapImage.src = theURL;

			var NewURL = getNewURL(theURL);
			NewURL = "../../output/" + NewURL;
			parent.MapFrame.document.theMapImage.src = NewURL;
			parent.SchoolInfoFrame.writeSchoolInfo();
			parent.ReadmeFrame.location = "http://www.condordataserver.com:81/website/musd/ButtonFrame.htm";
			 if (MapService == "musd_elem_03") {
				parent.MapFrame.document.theLegendImage.src = "./images/legend_elem.png";
				}
				else
				{
				parent.MapFrame.document.theLegendImage.src = "./images/legend_high_3_03.png";
				}

		break
		}
}


	function getService() {
//	 if (document.RadioForm.theSchoolType.Elementary.checked) {
	 if (document.RadioForm.theSchoolType[0].checked) {
	MapService = "musd_elem_03";
	SchoolType = "Elementary";
	}
	else
	{
	MapService = "musd_high_3_03";
	SchoolType = "Highschools";
	}
	var theService = MapService;
	imsURL = serverURL + theService + cVersion;
}

function getEnvelopeXYs(theString) {
	var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var tempString = "";
	var startpos;
	var endpos;
	//Get the start of the ENVELOPE element
	var pos = theString.indexOf("ENVELOPE",startpos);
	if (pos!=-1) {
		pos = pos + 8;
		//Extract minx
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[0] = parseFloat(tempString);
		//Extract miny
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[1] = parseFloat(tempString);
		//Extract maxx
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[2] = parseFloat(tempString);
		//Extract maxy
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[3] = parseFloat(tempString);
	}
	return theEnvelope;
}

function getLayers(theXML) {
		var Layers;
		var startpos = 0;
		var endpos = 0;
		startpos = theXML.indexOf("</PROPERTIES>",0);
		startpos = startpos + 13;
		endpos = theXML.indexOf("</GET_IMAGE>",startpos);
		Layers = theXML.substring(startpos,endpos);
		AcetateLayers = Layers;
	return AcetateLayers;
	}

function getGCCount(theGCString) {
		var GCCount = "";
		var startpos = 0;
		var endpos = 0;
		pos = theGCString.indexOf("GCCOUNT");
	    //extract the Geocode Count
		startpos = theGCString.indexOf("count=",pos);
		startpos = startpos + 7;
		endpos = theGCString.indexOf(dQuote,startpos);
		GCCount = theGCString.substring(startpos,endpos);
	return GCCount;
	}

function getSchoolName(theSchoolString) {
		var SchoolName = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Name
		startpos = theSchoolString.indexOf("SCHOOL=",pos);
		startpos = startpos + 8;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolName = theSchoolString.substring(startpos,endpos);
		NameofSchool = SchoolName;
	return SchoolName;
	}

function getSchoolAdd(theSchoolString) {
		var SchoolAdd = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Address
		startpos = theSchoolString.indexOf("ADDRESS=",pos);
		startpos = startpos + 9;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolAdd = theSchoolString.substring(startpos,endpos);
		AddofSchool = SchoolAdd;
	return SchoolAdd;
	}

function getSchoolCity(theSchoolString) {
		var SchoolCity = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School City
		startpos = theSchoolString.indexOf("CITY=",pos);
		startpos = startpos + 6;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolCity = theSchoolString.substring(startpos,endpos);
		CityofSchool = SchoolCity;
	return SchoolCity;
	}

function getSchoolZip(theSchoolString) {
		var SchoolZip = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Zip code
		startpos = theSchoolString.indexOf("ZIP=",pos);
		startpos = startpos + 5;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolZip = theSchoolString.substring(startpos,endpos);
		ZipofSchool = SchoolZip;
	return SchoolZip;
	}

function getSchoolPhone(theSchoolString) {
		var SchoolPhone = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Phone
		startpos = theSchoolString.indexOf("PHONE_NUMB=",pos);
		startpos = startpos + 12;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolPhone = theSchoolString.substring(startpos,endpos);
		PhoneofSchool = SchoolPhone;
	return SchoolPhone;
	}

function getSchoolPrinc(theSchoolString) {
		var SchoolPrinc = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Principal
		startpos = theSchoolString.indexOf("PRINCIPAL=",pos);
		startpos = startpos + 11;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolPrinc = theSchoolString.substring(startpos,endpos);
		PrincofSchool = SchoolPrinc;
	return SchoolPrinc;
	}

function getSchoolVP(theSchoolString) {
		var SchoolVP = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the School Vice Principal
		startpos = theSchoolString.indexOf("VICE_PRINC=",pos);
		startpos = startpos + 12;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolVP = theSchoolString.substring(startpos,endpos);
		VPofSchool = SchoolVP;
	return SchoolVP;
	}

function getSchoolWebsite(theSchoolString) {
		var SchoolWebsite = "";
		var startpos = 0;
		var endpos = 0;
		pos = theSchoolString.indexOf("FIELDS");
	    //extract the URL
		startpos = theSchoolString.indexOf("WEBSITE=",pos);
		startpos = startpos + 9;
		endpos = theSchoolString.indexOf(dQuote,startpos);
		SchoolWebsite = theSchoolString.substring(startpos,endpos);
		WebsiteofSchool = SchoolWebsite;
	return SchoolWebsite;
	}

function getXCoord(theCoordString) {
		var XCoord = "";
		var startposX = 0;
		var endposX = 0;
		var pos = theCoordString.indexOf("POINT");
	    //extract the XCoord
		var startposX = theCoordString.indexOf("x=",pos);
		startposX = startposX + 3
		var endposX = theCoordString.indexOf(dQuote,startposX);
		var XCoord = theCoordString.substring(startposX,endposX);
		XCoordEnvelope = XCoord;
	return XCoord;
	}

function getYCoord(theCoordString) {
		var YCoord = "";
		var startposY = 0;
		var endposY = 0;
		var pos = theCoordString.indexOf("POINT");
	    //extract the YCoord
		var startposY = theCoordString.indexOf("y=",pos);
		startposY = startposY + 3
		var endposY = theCoordString.indexOf(dQuote,startposY);
		var YCoord = theCoordString.substring(startposY,endposY);
		YCoordEnvelope = YCoord;
	return YCoord;
	}

function getURL(theString) {
	var theURL = "";
	var startpos = 0;
	var endpos = 0;
	theString = theString.toUpperCase();
	//Get the start of the OUTPUT element
	var pos = theString.indexOf("OUTPUT");
	if (pos != -1) {
	    //extract the URL
		pos = pos + 6;
		startpos = theString.indexOf("URL=",pos);
		startpos += 5;
		endpos = theString.indexOf(dQuote,startpos);
		theURL = theString.substring(startpos,endpos);
		theURL = theURL.toLowerCase();
	}
	return theURL;
}

	function getNewURL(passTheURL) {
		var NewURL = "";
		var startpos = 0;
		var endpos = 0;
		pos = passTheURL.indexOf("http:");
	    //extract the Geocode Count
		startpos = passTheURL.indexOf("output/",pos);
		startpos = startpos + 7;
		endpos = passTheURL.indexOf(".png",startpos);
		endpos = endpos + 4;
		NewURL = passTheURL.substring(startpos,endpos);
	return NewURL;
	}

	function getimsURL() {
	 if (theInputForm.Elementary.checked) {
	theService = "musd_elem_03";
	}
	else
	{
	theService = "musd_high_3_03";
	}
	var hostName = document.location.host;
	var serverURL  = "http://" + hostName + "/servlet/com.esri.esrimap.Esrimap?ServiceName=";
	var cVersion = "&ClientVersion=3.1";
	var URL = serverURL + theService + cVersion;
	imsURL = URL;
	}
