// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;

/**************************************
* DCED parameters
**************************************/

var countyLayerName = "DCED.PADOTCOUNTIES";
var countyNameVar = "COUNTY_NAM";
var congressLayerName = "DCED.PADOTCONGRESS";
var congressLastNameVar = "C_LASTNAME";
var congressFirstNameVar = "C_FIRSTNAM";
var congressLegDisVar = "LEG_DISTRI";
var houseLayerName = "DCED.PADOTHOUSE";
var houseFirstNameVar = "H_FIRSTNAM";
var houseLastNameVar = "H_LASTNAME";
var houseLegDisVar = "LEG_DISTRI";
var senateLayerName = "DCED.PADOTSENATE";
var senateFirstNameVar = "S_FIRSTNAM";
var senateLastNameVar = "S_LASTNAME";
var senateLegDisVar = "LEG_DISTRI";
var regionLayerName = "DCED.PAREGIONS";
var regionNameVar = "NAME";

// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
						// insert code here
						return false;
					
				}
				if (toolMode == 1002) {
						// insert code here

				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
	//} else if (XMLMode==1002) {
		// insert code here
	//} else {
		//alert(msgList[55] + XMLMode + msgList[56]);
	//}
	} else if (XMLMode==2001) {
		//alert(theReply);
		//alert(getEnvelopeXYs(theReply),1);
		setQueryString = getQueryInfo(theReply);
		selectCount=1;
		activityExtents = getEnvelopeXYs(theReply),1;
		parent.MapFrame.zoomToEnvelope(activityExtents[0],activityExtents[1],activityExtents[2],activityExtents[3]);
	} else {
		alert("Unknown Mode:" + XMLMode + "\nUnable to execute request.");
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	
	customString += '';
	
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

function zoomToActivity(theActivity, theLayer) {
	var whereString;
	//alert(theActivity);
	//alert(theLayer);
	if (theActivity.length == 0) {
		//Nothing picked
	} else {
		t = parent.MapFrame;
		var saveLayer=t.ActiveLayer;
		//t.setActiveLayer(3);
		if (theActivity.indexOf("'") != -1) {
			var theActivitySplitApos = theActivity.split("'");
			theActivity = theActivitySplitApos[0] + '\'\'' + theActivitySplitApos[1];
			//alert(theActivity);
		}
		if (theLayer == 'counties') {
			if (parent.parent.document.myForm.RadioGroupMV["LandUse"].checked){
			t.setVisibleLayer(0);
			}
			else{
			t.LayerVisible[0] = 1;
			t.LayerVisible[1] = 1;
			}
			t.setActiveLayer(0);
		
			whereString = countyLayerName + '.' + countyNameVar + ' = &apos;' + theActivity + '&apos;';
			//alert(whereString);
		} else if (theLayer == 'cong') {
			t.setVisibleLayer(2);
			t.setActiveLayer(2);
			//whereString = congressLayerName + '.' + congressLastNameVar + ' = &apos;' + theActivity + '&apos;';
			whereString = congressLayerName + '.' + congressLegDisVar + ' = &apos;' + theActivity + '&apos;';
		} else if (theLayer == 'house') {
			t.setVisibleLayer(3);
			t.setActiveLayer(3);
			//if (theActivity.indexOf("~") == -1) { 
			//	whereString = houseLayerName + '.' + houseLastNameVar + ' = &apos;' + theActivity + '&apos;';
			//} else {
			//	var theActivitySplit = theActivity.split("~");
			//	whereString = houseLayerName + '.' + houseLastNameVar + ' = &apos;' + theActivitySplit[0] + '&apos;' + ' AND ' + houseLayerName + '.' + houseFirstNameVar + ' = &apos;' + theActivitySplit[1] + '&apos;';
			//}
			whereString = houseLayerName + '.' + houseLegDisVar + ' = &apos;' + theActivity + '&apos;';
		} else if (theLayer == 'senate') {
			t.setVisibleLayer(4);
			t.setActiveLayer(4);
			//if (theActivity.indexOf("~") == -1) {
			//	whereString = senateLayerName + '.' + senateLastNameVar + ' = &apos;' + theActivity + '&apos;';
			//} else {
			//	var theActivitySplit = theActivity.split("~");
			//	whereString = senateLayerName + '.' + senateLastNameVar + ' = &apos;' + theActivitySplit[0] + '&apos;' + ' AND ' + senateLayerName + '.' + senateFirstNameVar + ' = &apos;' + theActivitySplit[1] + '&apos;';
			//}
			whereString = senateLayerName + '.' + senateLegDisVar + ' = &apos;' + theActivity + '&apos;';
		} else if (theLayer == 'region') {
			if (parent.parent.document.myForm.RadioGroupMV["LandUse"].checked){
			t.setVisibleLayer(5);
			t.setActiveLayer(5);
			}
			else{
			t.setVisibleLayer(2);
			t.LayerVisible[2] = 1;
			t.LayerVisible[1] = 1;
			t.setActiveLayer(1);
			}
			
			whereString = regionLayerName + '.' + regionNameVar + ' = &apos;' + theActivity + '&apos;';
			
		}
		
		t.selectionMode=1;
		axlString = t.writeQueryXML(whereString);
		//alert(axlString);
		imsQueryURL = t.imsURL + "&CustomService=Query";
		//switch back to active layer;
		//t.setActiveLayer(t.layerCount-saveLayer-1);
		//send to server and set toolMode to 2001 - a custom toolMode
		//alert(imsQueryURL+axlString);
		t.sendToServer(imsQueryURL,axlString,2001);
	}
}

function getQueryInfo(theString) {

	//theString = theString.toUpperCase();
	var tempString = "";
	var startpos = 1;
	var pos = theString.indexOf("FIELDS",startpos);
	if (pos!=-1) {
		pos = pos + 7;
		var endpos = theString.indexOf(".",pos);
		endpos = endpos + 1;
		endpos = theString.indexOf(".",endpos);
		tempString = theString.substring(pos,endpos);
		if (tempString == countyLayerName) {
			pos = theString.indexOf(countyNameVar,startpos);
			pos = pos + 12;
			endpos = theString.indexOf("\"",pos);
			tempString = tempString + '.' + countyNameVar + ' = &apos;' + theString.substring(pos,endpos) + '&apos;';
		} else if (tempString == congressLayerName) {
			pos = theString.indexOf(congressLastNameVar,startpos);
			pos = pos + 12;
			endpos = theString.indexOf("\"",pos);
			tempString = tempString + '.' + congressLastNameVar + ' = &apos;' + theString.substring(pos,endpos) + '&apos;';
		} else if (tempString == houseLayerName) {
			pos = theString.indexOf(houseLastNameVar,startpos);
			pos = pos + 12;
			endpos = theString.indexOf("\"",pos);
			var fnstartpos = theString.indexOf(houseFirstNameVar,startpos);
			fnstartpos = fnstartpos + 12;
			var fnendpos = theString.indexOf("\"",fnstartpos);
			if (theString.substring(pos,endpos).indexOf("&apos;") != -1) {
				var theStringSplitApos = theString.substring(pos,endpos).split("&apos;");
				var theCorrectedSubString = theStringSplitApos[0] + '\'\'' + theStringSplitApos[1];
				tempString = tempString + '.' + houseLastNameVar + ' = &apos;' + theCorrectedSubString + '&apos;' + ' AND ' + tempString + '.' + houseFirstNameVar + ' = &apos;' + theString.substring(fnstartpos,fnendpos) + '&apos;';
			} else {
				tempString = tempString + '.' + houseLastNameVar + ' = &apos;' + theString.substring(pos,endpos) + '&apos;' + ' AND ' + tempString + '.' + houseFirstNameVar + ' = &apos;' + theString.substring(fnstartpos,fnendpos) + '&apos;';
			}
		} else if (tempString == senateLayerName) {
			pos = theString.indexOf(senateLastNameVar,startpos);
			pos = pos + 12;
			endpos = theString.indexOf("\"",pos);
			var fnstartpos = theString.indexOf(senateFirstNameVar,startpos);
			fnstartpos = fnstartpos + 12;
			var fnendpos = theString.indexOf("\"",fnstartpos);
			if (theString.substring(pos,endpos).indexOf("&apos;") != -1) {
				var theStringSplitApos = theString.substring(pos,endpos).split("&apos;");
				var theCorrectedSubString = theStringSplitApos[0] + '\'\'' + theStringSplitApos[1];
				tempString = tempString + '.' + senateLastNameVar + ' = &apos;' + theCorrectedSubString + '&apos;' + ' AND ' + tempString + '.' + senateFirstNameVar + ' = &apos;' + theString.substring(fnstartpos,fnendpos) + '&apos;';
			} else {
				tempString = tempString + '.' + senateLastNameVar + ' = &apos;' + theString.substring(pos,endpos) + '&apos;' + ' AND ' + tempString + '.' + senateFirstNameVar + ' = &apos;' + theString.substring(fnstartpos,fnendpos) + '&apos;';
			}
		} else if (tempString == regionLayerName) {
			pos = theString.indexOf(regionNameVar,startpos);
			pos = pos + 6;
			endpos = theString.indexOf("\"",pos);
			tempString = tempString + '.' + regionNameVar + ' = &apos;' + theString.substring(pos,endpos) + '&apos;';
		}
	}
	return tempString;

}
