/* Function library for Parcels ActiveX/ASP application */
var ovIsVisible=false;

// Functions used by toolbar frame to load other frames
function loadLegend() {
	var mapService = parent.PostFrame.document.forms[0].mapService.value;
	parent.TextFrame.location = "../Legend.asp?MapService=" + mapService;
	//return true;
}

function loadSearchMenu() {
	parent.QueryFrame.location = "../menu.asp";
	parent.TextFrame.location = "blank.htm";
	return true;
}

function loadIntersectionForm() {
	parent.TextFrame.location = "../FindIntersection.htm";
}

// Functions used to submit post form to server

function sendIDValue(Value) {
	var sAction;
	var postForm = parent.PostFrame.document.forms[0];
	// got APN from user, send to Parcel or Buffer select
	if (parent.theApp == "Parcels") {
		sAction = "SelectAndZoom";
	} else if (parent.theApp == "Buffer") {
		sAction = "BufferAPN";
	}
	postForm.IDValue.value = Value;
	postForm.appName.value = parent.theApp;
	postForm.action.value = sAction;
	showRetrieveMap();
	postForm.submit();
}

// Set extent in form and submit to ASP page
function sendToASP(sAction) {
	var postForm = parent.PostFrame.document.forms[0];
	postForm.mapXMin.value = eLeft;
	postForm.mapYMin.value = eBottom;
	postForm.mapXMax.value = eRight;
	postForm.mapYMax.value = eTop;
	postForm.appName.value = parent.theApp;
	postForm.action.value = sAction;
	showRetrieveMap();
	postForm.submit();
}

// send extent to postframe form and submit to asp page
function sendMapASP() {
	var sAction = "ZoomToExtent";
	sendToASP(sAction);
}

// Send mouse click x/y to ASP Page to identify/highlight, then get attributes from DB
function sendIDSelect(e, sAction) {
	var postForm = parent.PostFrame.document.forms[0];

	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);

	postForm.IDx.value = mapX;
	postForm.IDy.value = mapY;
	postForm.action.value = sAction;
	if (parent.theApp == "Buffer") {
		// update buffer distance in case user doesn't change focus after setting distance
		parent.BottomFrame.BuffForm.Distance.blur();
		postForm.Distance.value = parent.BottomFrame.BuffForm.Distance.value;
	}
	sendToASP(sAction);
}

// send mouse click x/y to ASP Page to add point to graphic line (for buffer select)
function addPointToLine(e) {
	var postForm = parent.PostFrame.document.forms[0];
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	if (postForm.DrawX.value == "") {
		postForm.DrawX.value = mapX;
		postForm.DrawY.value = mapY;
	} else {
		postForm.DrawX.value = postForm.DrawX.value + "," + mapX;
		postForm.DrawY.value = postForm.DrawY.value + "," + mapY;
	}
	if (parent.MapFrame.toolMode==16) postForm.BufferGraphic.value = "False";
	sendToASP("ZoomToExtent");
}

// erase user-drawn line
function resetClick() {
	var postForm = parent.PostFrame.document.forms[0];
	if (postForm.DrawX.value != "") {
		postForm.DrawX.value = "";
		postForm.DrawY.value = "";
		postForm.BuffIDs.value = "";
		postForm.DrawType.value = "";
		postForm.BufferGraphic.value = "False";
		sendMapASP();
	}
}

// delete last point in user-drawn line
function deleteClick() {
	var postForm = parent.PostFrame.document.forms[0];
	if (postForm.DrawX.value != "") {
		var arrDrawX = postForm.DrawX.value.split(",");
		var arrDrawY = postForm.DrawY.value.split(",");
		postForm.DrawX.value = arrDrawX.slice(0,arrDrawX.length-1);
		postForm.DrawY.value = arrDrawY.slice(0,arrDrawY.length-1);
		postForm.DrawType.value = "";	
		postForm.BuffIDs.value = "";
		postForm.BufferGraphic.value = "False";
		sendMapASP();
	}
}

// get the map extents from asp reply
function getMapData() {
	var postForm = parent.PostFrame.document.forms[0];
	eLeft = parseFloat(postForm.mapXMin.value);
	eBottom = parseFloat(postForm.mapYMin.value);
	eRight = parseFloat(postForm.mapXMax.value);
	eTop = parseFloat(postForm.mapYMax.value);
	xDistance = Math.abs(eRight-eLeft);
	yDistance = Math.abs(eTop-eBottom);
	xHalf = xDistance / 2;
	yHalf = yDistance / 2;
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	mapScaleFactor = xDistance / iWidth;
	
	if (bDoingPan) {
		moveLayer("theMap",hspc,vspc);
		clipLayer2("theMap",0,0,iWidth,iHeight);
		window.setTimeout('showLayer("theMap");',1000);
	}

	// Display/process form attribute info only if not just redisplaying map
	if ((postForm.action.value == "BufferLineConfirm") || (postForm.action.value=="BufferPolyConfirm")) {
		var sPcls = postForm.Attributes.value;
		postForm.Attributes.value = "";
		var sMsg = "Warning!\n\n" + sPcls + " parcels were found under the graphic drawn.";
		sMsg += "\nBuffering from this many parcels will take a long time.\n\nContinue?";
		var bContinue = confirm(sMsg);
		if (bContinue) { 
			parent.TextFrame.document.location = "processing.htm";
			sendToASP(postForm.action.value);
		}
		
	} else if (postForm.action.value == "BufferDeny") {
		var sPcls = postForm.Attributes.value;
		var sMsg = "Warning!\n\n" + sPcls + " parcels were found under the graphic drawn.";
		sMsg += "\n\nBuffering from this many parcels is not allowed. Please select fewer parcels and try again.";
		alert(sMsg);
	
	} else if (postForm.action.value != "ZoomToExtent") {
		var sBuffIDs = postForm.BuffIDs.value;
		var sAPNval = postForm.IDValue.value;
		var sAttributes = postForm.Attributes.value;
		var sNewLoc = "";
		if (sBuffIDs != "") {
			sendIDsByPost(sBuffIDs, "parcels_details.asp");
		} else if (sAPNval != "") {
			if (postForm.action.value == "Identify") {		// for SanGIS
				sNewLoc = 'parcels_details.asp?IDValue=' + sAPNval;
			}
		} else if (sAttributes != "") {
			sNewLoc = 'AttributesDisplay.htm';
		} else {
			//sNewLoc = "text.htm";
		}
		if (sNewLoc != "") {
			//parent.TextFrame.document.location = sNewLoc;
			parent.popupWin = window.open(sNewLoc,"DataWin","height=500,width=350,top=100,left=350,scrollbars,resizable");
			parent.popupWin.focus()
		}
	}
	
	if (ovIsVisible) {
		putExtentOnOVMap();
		hideRetrieveMap();
	}
}

function sendIDsByPost(sIDs, sPg) {
	// creates new page in TextFrame, inserts IDs in form and sends to ASP page
	var sStr = "<HTML><body onload='javascript:document.forms[0].submit();'>";
	sStr += "<form action='" + sPg + "' method=post>";
	sStr += "<input type='hidden' name='App' value='Buffer'>"
	sStr += "<input type=hidden name='IDValue' value='" + sIDs + "'></form>"
	sStr += "<p><i>Getting feature data...</i></body></html>";
	parent.TextFrame.document.open();
	parent.TextFrame.document.write(sStr);
	parent.TextFrame.document.close();
}

function clearSelection() {
	var postForm = parent.PostFrame.document.forms[0];
	postForm.IDValue.value = "";
	postForm.IDx.value = "";
	postForm.IDy.value = "";
	postForm.XStrLabel.value = "";
	postForm.DrawX.value = "";
	postForm.DrawY.value = "";
	postForm.DrawType.value = "";
	postForm.BuffIDs.value = "";
	postForm.BufferGraphic.value = "False";
	sendMapASP();
}

