// CivXplorer Add Points by Lat/Long

// Suzanne Fliege
// PlanSight, LLC

// contains functions to user creation of map points by Lat/Long.
latlongptAvailable = true;

var defaultLat = "42.289241"; 
var defaultLong = "-85.585514";
var mapserviceCoordSysID = "102690"; // coordinate system id of mapservice data
var coordSysIDIsNumber = true;

//******************************************************************************
// do not edit below here
//******************************************************************************
// do not edit these!
var latlongptShape = "circle";
var latlongptColor = "255,0,0";
var latlongptType = "decimal";
var latlongptLabel = "My Point";
var latlongptX = defaultLat;
var latlongptY = defaultLong;
// multiple pt arrays
var latlongptShapes = new Array();
var latlongptColors = new Array();
var latlongptXs = new Array();
var latlongptYs = new Array();
var latlongptTypes = new Array();
var latlongptLabels = new Array();
var isALatLongPt = false;

// add menu items for Lat/Long pt mgmt
function addLatLongPtMenuItems() {
	document.writeln(createMenuItem("menuItem_LatLongPtZoom","writeLatLongPtForm();","Add a Point on Map by Lat/Long"));
	document.writeln(createMenuItem("menuItem_LatLongPtClear","clearLatLongPts();","Clear Lat/Long Point(s)"));
	document.writeln(createMenuItem("menuItem_LatLongPtCoord","clickFunction(\'latlongcoord\');","Click on Map for Lat/Long Coords"));
	return false;
}

// writes form for user lat/long point creation
function writeLatLongPtForm() {
	var theString = "";
	theString += '<form name="LatLongPtStuff" onsubmit="doLatLongPt();return false;">';
	theString += '<table cellpadding="5" cellspacing="0" width="100%">';
	theString += '<tr><td class="header1" width="100%" align="right"><a href="#" onmousedown="hideAttributeDisplay();"><img src="images/CXclose.gif" align="top"></a></td></tr>';
	theString += '<tr><td class="header1" align="center">Add a Point To Map</td></tr>';
	//theString += '<tr><td class="toolInfoSmall" align="center"><i>You may only add one point to the map! Creating a new point will replace a previously created point!</i></td></tr>';
	theString += '<tr><td class="toolInfoSmall" align="center"><select name="latlongType" onchange="latlongptType=this.options[this.selectedIndex].value;writeLatLongPtForm();">';
	theString += '<option value="decimal"';
	if (latlongptType == "decimal") theString += ' selected';
	theString += '>Decimal Degrees';
	theString += '<option value="degminsec"';
	if (latlongptType == "degminsec") theString += ' selected';
	theString += '>Degree Minute Second';
	theString += '</select></td></tr>';
	theString += '<tr><td class="toolInfo1" align="left">Longitude:</td></tr>';
	if (latlongptType == "decimal") {
		theString += '<tr><td align="center"><input type="Text" name="LongString" size="25" maxlength="25" tabindex="1" value=\''+ defaultLong +'\'></td></tr>';
	} else {
		theString += '<tr><td align="center" class="toolInfo1">';
		theString += '<input type="Text" name="LongDString" size="4" maxlength="4" tabindex="1" value="Deg">';
		theString += '<input type="Text" name="LongMString" size="4" maxlength="4" tabindex="2" value="Min">';
		theString += '<input type="Text" name="LongSString" size="5" maxlength="5" tabindex="3" value="Sec">West';
		theString += '</td></tr>'
	}
	theString += '<tr><td class="toolInfo1" align="left">Latitude:</td></tr>';
	if (latlongptType == "decimal") {
		theString += '<tr><td align="center"><input type="Text" name="LatString" size="25" maxlength="25" tabindex="2" value=\''+ defaultLat +'\'></td></tr>';
		theString += '<tr><td class="toolInfoSmall" align="center"><i>Latitude and Longitude values must be in Decimal Degrees!<br>(ex. 45.3214532)</i></td></tr>';
	} else {
		theString += '<tr><td align="center" class=toolInfo1>';
		theString += '<input type="Text" name="LatDString" size="4" maxlength="4" tabindex="4" value="Deg">';
		theString += '<input type="Text" name="LatMString" size="4" maxlength="4" tabindex="5" value="Min">';
		theString += '<input type="Text" name="LatSString" size="5" maxlength="5" tabindex="6" value="Sec">North';
		theString += '</td></tr>'
	}
	theString += '<tr><td align="center"><input class="toolInfo1" type="submit" value="Add The Point!" name="submit"></td></tr>'; 
	theString += '<tr><td class="toolInfo1" align="left">Point Label:</td></tr>';
	theString += '<tr><td align="center"><input type="Text" name="LabelString" size="25" maxlength="25" value=\''+ latlongptLabel +'\'></td></tr>';
	theString += '<tr><td class="toolInfo1" align="left">Point Symbol:</td></tr>';
	theString += '<tr><td><table cellpadding="2" cellspacing="0" width="100%">';
	theString += '<tr><td class="toolInfo2" align="right">Shape</td><td><select name="latlongShape" onchange="latlongptShape=this.options[this.selectedIndex].value;">';
	for (var i=0;i<graphicPointTypes.length;i++) {
		theString += '<option value="' + graphicPointTypes[i] + '"';
		if (latlongptShape == graphicPointTypes[i]) theString += ' selected';
		theString += '>' + graphicPointTypes[i];
	}
	theString += '</select></td></tr>';
	theString += '<tr><td class="toolInfo2" align="right">Color</td><td><select name="latlongColor" onchange="latlongptColor=this.options[this.selectedIndex].value;">';
	for (var i=0;i<colorSwatchNames.length;i++) {
		theString += '<option value="' + colorSwatchRGBs[i] + '"';
		if (latlongptColor == colorSwatchRGBs[i]) theString += ' selected';
		theString += '>' + colorSwatchNames[i];
	}
	theString += '</select></td></tr>';
	theString += '</table></td></tr>';
	theString += '</table>';
	theString += '</form>';
	setupInputFormSidebar();
	updateContent("cxAttributeBottom",theString);
	return false;
}

// clears out points
function clearLatLongPts() {
	hideAllDropdowns();
	var clearPts = confirm("Delete all added points from map?");
	if (clearPts) {
		latlongptXs.length = 0;
		latlongptYs.length = 0;
		latlongptTypes.length = 0;
		latlongptLabels.length = 0;
		latlongptShapes.length = 0;
		latlongptColors.length = 0;
		isALatLongPt = false;
		sendMapRequest();
	} else {
		return false;
	}
}

// adds point acetate layer to map request
function addLatLongPtToMap() {
	var tempString = '';
	for (var i = 0;i<latlongptXs.length;i++) {
		tempString += '<LAYER type="acetate" name="LatLongPoint' + i + '">\n';
		tempString += '<OBJECT units="database">\n';
		tempString += '<POINT coords="' + latlongptXs[i] + " " + latlongptYs[i] + '">\n';
		tempString += '<SIMPLEMARKERSYMBOL  type="' + latlongptShapes[i] + '"  color="' + latlongptColors[i] + '" outline="0,0,0" width="16" overlap="false" />\n</POINT>\n</OBJECT>\n';
		if (latlongptLabel != "") {
			tempString += '<OBJECT units="database">\n<TEXT coords="' + latlongptXs[i] + " " + latlongptYs[i] + '" label="' + latlongptLabels[i] + '">\n';
			tempString += '<TEXTMARKERSYMBOL fontcolor="' + latlongptColors[i] + '" fontsize="16" fontstyle="bold" outline="255,255,255" glowing="153,153,153" halignment="right" valignment="top" interval="5"/>\n</TEXT>\n</OBJECT>\n';
		}
		tempString += '</LAYER>\n';
	}
	return tempString;
}

// captures user input and saves to session variables
function doLatLongPt() {
	document.getElementById("LoadMap").style.visibility = "visible";
	if (latlongptType == "degminsec") {
		latlongptY = parseInt(document.LatLongPtStuff.LatDString.value) + (parseInt(document.LatLongPtStuff.LatMString.value)/60) + (parseFloat(document.LatLongPtStuff.LatSString.value)/3600);
		latlongptX = 0 - (parseInt(document.LatLongPtStuff.LongDString.value) + (parseInt(document.LatLongPtStuff.LongMString.value)/60) + (parseFloat(document.LatLongPtStuff.LongSString.value)/3600));
	} else {
		latlongptY = parseFloat(document.LatLongPtStuff.LatString.value);
		latlongptX = parseFloat(document.LatLongPtStuff.LongString.value);
	}
	latlongptLabel = document.LatLongPtStuff.LabelString.value;
	var tempString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
	tempString += '<ENVELOPE minx="' + (latlongptX-0.000000001) + '" miny="' + latlongptY + '" maxx="' + latlongptX + '" maxy="' + latlongptY + '" />\n';
	if (coordSysIDIsNumber) {
		tempString += '<FEATURECOORDSYS id="' + mapserviceCoordSysID + '"/>\n';
	} else {
		tempString += '<FEATURECOORDSYS string="' + mapserviceCoordSysID + '"/>\n';
	}
	tempString += '<FILTERCOORDSYS id="4269"/>\n'; //4326
	tempString += '<IMAGESIZE height="1" width="1" />\n';
	tempString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	sendToServer(imsURL,tempString,43)
}

// check that returned coords are on map, and put on map or notify user
function checkLatLongPt() {
	// calculate point
	var theXYs = getEnvelopeXYs(theReply,0)
	latlongptX = (theXYs[0]+theXYs[2])/2;
	latlongptY = (theXYs[1]+theXYs[3])/2;
	if ((latlongptX < limitRight) && (latlongptX > limitLeft) && (latlongptY > limitBottom) && (latlongptY < limitTop)) {
		// add to array
		var tempPtCnt = latlongptXs.length;
		latlongptXs[tempPtCnt] = latlongptX;
		latlongptYs[tempPtCnt] = latlongptY;
		latlongptTypes[tempPtCnt] = latlongptType;
		latlongptLabels[tempPtCnt] = makeXMLsafe(latlongptLabel);
		latlongptShapes[tempPtCnt] = latlongptShape;
		latlongptColors[tempPtCnt] = latlongptColor;
		// zoom map
		saveLastExtent();
		eLeft = latlongptX - selectPointMargin;
		eRight = latlongptX + selectPointMargin;
		eTop = latlongptY + selectPointMargin;
		eBottom = latlongptY - selectPointMargin;
		isALatLongPt = true;
		sendMapRequest();
	} else {
		alert("That coordinate falls outside the extents\n of this map and cannot be displayed!");
		document.getElementById("LoadMap").style.visibility = "hidden";
		return false;
	}
}

// get user click and request lat/long coord
function getLatLongCoord(e) {
    document.getElementById("LoadData").style.visibility = "visible";
	getXY(e);
	var tempXYs = getMapXY(mouseX,mouseY);
	var tempString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
	tempString += '<ENVELOPE minx="' + tempXYs[0] + '" miny="' + tempXYs[1] + '" maxx="' + (tempXYs[0]+0.5) + '" maxy="' + (tempXYs[1]+0.5) + '" />\n';
	tempString += '<FEATURECOORDSYS id="4269"/>\n'; //4326
	if (coordSysIDIsNumber) {
		tempString += '<FILTERCOORDSYS id="' + mapserviceCoordSysID + '"/>\n';
	} else {
		tempString += '<FILTERCOORDSYS string="' + mapserviceCoordSysID + '"/>\n';
	}
	tempString += '<IMAGESIZE height="1" width="1" />\n';
	tempString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	sendToServer(imsURL,tempString,44)
}

// notify user of the clicked coords in Lat/Long
function notifyLatLongPt() {
	var theXYs = getEnvelopeXYs(theReply,0)
    document.getElementById("LoadData").style.visibility = "hidden";
	var u = Math.pow(10,6); // 6 decimal places
	var tempX = parseInt(((theXYs[0]+theXYs[2])/2)*u+0.5)/u;
	var tempY = parseInt(((theXYs[1]+theXYs[3])/2)*u+0.5)/u;
	// create deg/min/sec strings
	u = Math.pow(10,2); // 2 decimal places
	var stringX = "  ( " + parseInt(tempX) + " ";
	var stringY = "  ( " + parseInt(tempY) + " ";
	var tempMinX = (tempX - parseInt(tempX))*60*(-1);
	var tempMinY = (tempY - parseInt(tempY))*60;
	stringX += parseInt(tempMinX) + "' ";
	stringY += parseInt(tempMinY) + "' ";
	var tempSecX = (tempMinX - parseInt(tempMinX))*60;
	var tempSecY = (tempMinY - parseInt(tempMinY))*60;
	stringX += parseInt((tempSecX*u+0.5))/u + "\" )";
	stringY += parseInt((tempSecY*u+0.5))/u + "\" )";
	// alert user
	alert("Latitude: "+tempY+stringY+"\nLongitude: "+tempX+stringX);
	return false;
}

