// 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;


var lblsText =new Array();
var lblX=new Array();
var lblY=new Array();
var lblsXY=new Array();
var lblColor=new Array();
var lblSize=new Array();


// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				

				if (toolMode == 1004) { 
			    drillIdentify(e);
  				}
				if (toolMode == 1001) {
						// insert code here
						var theX = mouseX;
						var theY = mouseY;
						clickCount=clickCount+1
						getMapXY(theX,theY);
						lblX[clickCount]=mapX;
						lblY[clickCount]=mapY;
						return false;	
				}
				if (toolMode == 1002) {
						// insert code here
				}
				if (toolMode == 1005) { 
						startSelectBox(e);
						//identify(e)
						return false;
  				}

}

// 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) {
		
			//alert(theReply);
			var theURL = "";
			theURL = getURL(theReply);
			//alert(theURL);
			var Win1;	
			Win1 = open(theURL,"Window1","width=800, height=600, scrollbars=yes,resizable=yes");
			hidePrintjob();

		// insert code here
    } else if (toolMode == 1004) {
		      doDrill(theReply);
	} else if (toolMode==1005) {
		//alert(theReply)
		displayAttributeData(theReply);
	}
		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 = "";
	//alert(clickCount)
	for (var j=1;j<clickCount+1;j++) {

	lblsXY[j]=(lblX[j]) + ' ' + (lblY[j])
	
	if (j>1 && j%2 ==0 ) {
			// draw click points and lines between them on map
			customString+= '<LAYER type="ACETATE" name="allTheClicks">\n';
			
				customString += '<OBJECT units="DATABASE">\n<LINE coords="' + lblsXY[j-1] ;
				
				customString += " "  + lblsXY[j] ; 
				
				customString += '" >\n';
				
				switch (lblColor[j-1])

		                {
						case "Red":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="255,0,0" width="2" />\n</LINE>\n</OBJECT>\n'; 			
							break;
						case "Green":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="0,255,0" width="2" />\n</LINE>\n</OBJECT>\n'; 	
							break;
						case "Blue":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="0,0,255" width="2" />\n</LINE>\n</OBJECT>\n'; 	
							break;
						case "Yellow":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="255,255,0" width="2" />\n</LINE>\n</OBJECT>\n'; 	
							break;	
						case "Black":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="0,0,0" width="2" />\n</LINE>\n</OBJECT>\n';
							break;
						case "Cyan":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="0,255,255" width="2" />\n</LINE>\n</OBJECT>\n';
							break;
						case "Magenta":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="255,0,255" width="2" />\n</LINE>\n</OBJECT>\n';
							break;	
						case "White":
							customString += '<SIMPLELINESYMBOL type="SOLID" color="255,255,255" width="2" />\n</LINE>\n</OBJECT>\n';
							break;		
			       }
			customString += '</LAYER>\n';
		
	}
	}
	
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";

	var reswidth = screen.width;
		var u = Math.pow(10,0);
		var zscale=0;
		var lr=0;
		var tb=0;
		var lblwidth;
		var lblxOffset;
		var scaleright = parseInt(right * u + 0.5) / u;
		var scaleleft =  parseInt(left * u + 0.5) / u;
		var offLR=(scaleright-scaleleft);
		//alert(offLR)
		if (reswidth=640){
		lr=7;
		}
		if (reswidth=800){
		lr=8.5;
		}
		if (reswidth=1024){
		lr=9;
		}
		if (reswidth=1152){
		lr=12;
		}
		if (reswidth=1280){
		lr=12;
		}
		zscale = offLR/lr;
		
		var lblwidth=new Array();
		var lblxOffset=new Array();
		var lblyOffset=new Array();
		
	for (var j=1;j<clickCount+1;j++) {	
	if (j>1 && j%2==0 ){

		//mode 1 x1<x2 y1>y2

		lblwidth[j-1]=lblsText[j-1].length
		lblxOffset[j-1]=zscale*lblwidth[j-1]*.5*lblSize[j-1]/72
		lblyOffset[j-1]=zscale*lblSize[j-1]/72

		if (lblX[j-1]<lblX[j] && lblY[j-1]>lblY[j]) {
		lblsXY[j-1]=(lblX[j-1]-lblxOffset[j-1]) + ' ' + (lblY[j-1] +lblyOffset[j-1])
		}

		//mode 2 x1>x2 y1>y2

		if (lblX[j-1]>lblX[j] && lblY[j-1]>lblY[j]) {
		lblsXY[j-1]=(lblX[j-1]) + ' ' + (lblY[j-1] + lblyOffset[j-1])
		}

		//mode 3 x1>x2 y1<y2

		if (lblX[j-1]>lblX[j] && lblY[j-1]<lblY[j]) {
		lblsXY[j-1]=(lblX[j-1]) + ' ' + (lblY[j-1])
		}

		// mode 4 x1<x2 y1<y2
		if (lblX[j-1]<lblX[j] && lblY[j-1]<lblY[j]) {
		lblsXY[j-1]=(lblX[j-1]-lblxOffset[j-1]) + ' ' + (lblY[j-1])
		}		
   
		customString += '<LAYER type="acetate" name="Label">\n'; 
		customString += '<OBJECT units="database">\n<TEXT coords="' + lblsXY[j-1] + '" label="' + lblsText[j-1] + '">\n'; 
		switch (lblColor[j-1])
		{
		case "Red":
			customString += '<TEXTMARKERSYMBOL fontcolor="255,0,0" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;
		case "Green":
			customString += '<TEXTMARKERSYMBOL fontcolor="0,255,0" fontsize="' + lblSize[j-1]+ '"  halignment="right" valignment="bottom" ';
			break;
		case "Blue":
			customString += '<TEXTMARKERSYMBOL fontcolor="0,0,255" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;
		case "Yellow":
			customString += '<TEXTMARKERSYMBOL fontcolor="255,255,0" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;	
		case "Black":
			customString += '<TEXTMARKERSYMBOL fontcolor="0,0,0" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;
		case "Cyan":
			customString += '<TEXTMARKERSYMBOL fontcolor="0,255,255" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;
		case "Magenta":
			customString += '<TEXTMARKERSYMBOL fontcolor="255,0,255" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;	
		case "White":
			customString += '<TEXTMARKERSYMBOL fontcolor="255,255,255" fontsize="' + lblSize[j-1] + '"  halignment="right" valignment="bottom" ';
			break;		
		}
		
		customString += 'overlap="true"'  + ' /></TEXT></OBJECT>\n';
		customString += '</LAYER>\n';  
	}
	}
	//alert(customString);
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var 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");
	theurl = serverURL + "NewBerlinExtract";
	thestring = writeExtractXML();
//alert(theurl);
//alert(thestring);
	sendToServer(theurl,thestring,1000);
}


