/* Function library for Parcels ActiveX/ASP application */
var ovIsVisible=false;

// Functions used by toolbar frame to load other frames
function loadLegend() {
	parent.QueryFrame.document.location = "Legend.asp";

}

//function loadSearchMenu() {
//	parent.QueryFrame.document.location = "menu.asp";
//	//parent.TextFrame.location = "blank.htm";
//	return true;
//}

function loadIntersectionForm() {
	//parent.TextFrame.location = "FindIntersection.htm";
	//single data frame
	parent.QueryFrame.location = "FindIntersection.htm";
//	parent.QueryFrame.location = "notavail.htm";

}

// Functions used to submit post form to server

function sendIDValue(Value) {
	var sAction;
	var postForm = document.MapForm;
	// got APN from user, send to Parcel or Buffer select
	sAction = "SelectAndZoom";
	
	postForm.IDValue.value = Value;
	postForm.appName.value = parent.theApp;
	postForm.action.value = sAction;
	postForm.DrawX.value = "";
	postForm.DrawY.value = "";
	showRetrieveMap();
	postForm.submit();
}

function searchValue(id, field, value) {
		//alert(id);
		var postForm = document.MapForm;
		//alert(postForm.LayerVisible.value);
		/*var lv = postForm.LayerVisible.value;
		
		
		if (lv != "") {
			var arrV = lv.split(",");
			//alert(arrV[1]);
			if (arrV[id-1]== 0) {
				arrV[id-1] = 1;
			} 
		}
		var sLyrV = arrV.join(",");
		//alert(sLyrV);
		postForm.LayerVisible.value = sLyrV;
		*/
		postForm.layerID.value = id;
		postForm.field.value = field;
		postForm.fvalue.value = value;
		postForm.appName.value = parent.theApp;
		postForm.action.value = "Search";
		showRetrieveMap();
		postForm.submit();
}
// Set extent in form and submit to ASP page
function sendToASP(sAction) {
	//alert("sendToASP  sAction: "+sAction);
	var postForm = document.MapForm;
	postForm.mapX.value = eLeft;
	postForm.mapY.value = eBottom;
	postForm.mapXMax.value = eRight;
	postForm.mapYMax.value = eTop;
	postForm.lastMapX.value = lastLeft;
	postForm.lastMapY.value = lastBottom;
	postForm.lastMapXMax.value = lastRight;
	postForm.lastMapYMax.value = lastTop;
	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 = document.MapForm;

	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 = document.MapForm;
	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;
	}
	clickPointX[clickCount]=mapX;
	clickPointY[clickCount]=mapY;
	clickCount += 1;
	totalMeasure = totalMeasure + currentMeasure;
	clickMeasure[clickCount]=totalMeasure;
	sendToASP("ZoomToExtent");
}

// use user-drawn line/polygon to select/buffer
function sendShapeSelect(iType) {
	gType = iType;
	var postForm = document.MapForm;
	postForm.IDValue.value = "";
	if (postForm.appName.value == "Buffer") {
		if (iType==1) {
			postForm.DrawType.value = "LINE";
			sendToASP("SelectLine");
		} else {
			postForm.DrawType.value = "POLY";
			sendToASP("SelectPoly");
		}
	} else {
		if (iType==1) {
			postForm.DrawType.value = "LINE";
			sendToASP("BufferLine");
		} else {
			postForm.DrawType.value = "POLY";
			sendToASP("BufferPoly");
		}
	}
}

function sendShapeBuffer() {
	var postForm = document.MapForm;
	//alert("i am in");
	if (postForm.IDValue.value != "") {
		
		sendToASP("BufferAPN");
	}
	else if (postForm.DrawX.value != "" && postForm.APNIDs.value != ""){
		if (gType==1) {
			postForm.DrawType.value = "LINE";
			sendToASP("BufferLine");
		
		} else {
			postForm.DrawType.value = "POLY";
			sendToASP("BufferPoly");
		}
	}
	else {
		alert("There are no selected parcels to buffer");
	}
}



function sendSalesBuffer() {
	var postForm = document.MapForm;
	//alert("i am in");
	if (postForm.IDValue.value != "") {
		
		sendToASP("BufferSale");
	}
	else {
		alert("There are no selected parcels to buffer");
	}
}

// erase user-drawn line
function resetClick() {
	var postForm = document.MapForm;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	//selectCount=0;
	if (postForm.DrawX.value != "") {
		postForm.DrawX.value = "";
		postForm.DrawY.value = "";
		postForm.BuffIDs.value = "";
		postForm.APNIDs.value = "";
		postForm.DrawType.value = "LINE";
		sendMapASP();
	}
	//if (toolMode==20) updateMeasureBox();
}

// delete last point in user-drawn line
function deleteClick() {
	var postForm = document.MapForm;
	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 = "LINE";
		
		postForm.BuffIDs.value = "";
		sendMapASP();
	}
}

// get the map extents from asp reply
function getMapData() {
	
	var postForm = document.MapForm;
	eLeft = parseFloat(postForm.mapX.value);
	eBottom = parseFloat(postForm.mapY.value);
	eRight = parseFloat(postForm.mapXMax.value);
	eTop = parseFloat(postForm.mapYMax.value);
	lastLeft = parseFloat(postForm.lastMapX.value);
	lastBottom = parseFloat(postForm.lastMapY.value);
	lastRight = parseFloat(postForm.lastMapXMax.value);
	lastTop = parseFloat(postForm.lastMapYMax.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;
	var sNewLoc = "";

	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) { 
			////single data frame
			//parent.TextFrame.document.location = "processing.htm";
			parent.QueryFrame.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 sAPNIDs = postForm.APNIDs.value;

		if (postForm.action.value == "BufferAPN") {
//			alert("paso2: " + sBuffIDs);
		// ID string too long for querystring; use post method
			sendIDsByPost(sBuffIDs, "bufferDetails.asp", postForm.useType.value);
			parent.QueryFrame.document.forms[0].submit();
			} 
		
		else if (sBuffIDs != "") {
//			alert("paso1:" + postForm.action.value);
			if ( (postForm.action.value == "BufferSale") ) {
//				alert("aimsActiveX, sBuffIDs and BufferSale");
				var sCat = postForm.sCat.value;
				var sOrdr = postForm.sOrdr.value;
				var smainapn = postForm.IDValue.value;
				var sAddress = postForm.sAddress.value;
				var sUsecode = postForm.sUsecode.value;
				var sRecdate = postForm.sRecdate.value;
				var sArea = postForm.sArea.value;
				var sBB = postForm.sBB.value;
				var sHapn = postForm.sHapn.value;
				
				if (sCat == "") {sCat = parent.sCat; } else {postForm.sCat.value = sCat;}
				if (sOrdr == "") {sOrdr = parent.sOrdr;} else {postForm.sOrdr.value = sOrdr;}
				if (sAddress == "") {sAddress = parent.sAddress;} else {postForm.sAddress.value = sAddress;}
				if (sUsecode == "") {sUsecode = parent.sUsecode;} else {postForm.sUsecode.value = sUsecode;}
				if (sRecdate == "") {sRecdate = parent.sRecdate;} else {postForm.sRecdate.value = sRecdate;}
				if (sArea == "") {sArea = parent.sArea;} else {postForm.sArea.value = sArea;}
				if (sBB == "") {sBB = parent.sBB;} else {postForm.sBB.value = sBB;}
				if (sHapn == "") {sHapn = parent.sHapn;} else {postForm.sHapn.value = sHapn;}

				if (sCat == "") {
					sCat = "010";
				}
				if (sOrdr == "") {
					sOrdr = "AIN";
				}
				sURL = "output.asp?IDValue="+sAPNval+"&BuffIDs="+sBuffIDs+"&sAPNIDs="+sAPNIDs+"&cat="+sCat+"&ordr="+sOrdr;
				sURL = sURL + "&hapn="+sHapn+"&sAddress="+sAddress;
				sURL = sURL + "&sUsecode="+sUsecode+"&sRecdate="+sRecdate;
				sURL = sURL + "&sArea="+sArea+"&sBB="+sBB;
				//		alert("sURL to Output: " + sURL+"!!!");
				parent.QueryFrame.document.location = sURL;

/*** Moved from here to above so as to allow user to select wider Search Radius in bufferDetails.asp.  RAH 01/25/2008
			} else if (postForm.action.value == "BufferAPN") {
//				alert("paso2: " + sBuffIDs);
				// ID string too long for querystring; use post method
				sendIDsByPost(sBuffIDs, "bufferDetails.asp", postForm.useType.value);
				parent.QueryFrame.document.forms[0].submit();
***/			
			}
			
		} else if (sAPNval != "") {

		} else if (sAPNIDs != "") {
//			alert("paso3:" + sAPNIDs);
			sendIDsByPost(sAPNIDs, "bufferDetails.asp", postForm.useType.value);
			parent.QueryFrame.document.forms[0].submit();
			
		} else if (postForm.action.value == "Search") {
			//retaining the textframe element to the search form		
			
		} else {
			sNewLoc = "text.htm";
		}
		
//	if (sAttributes != "") {
		if ( postForm.action.value == "IdentifySingle" || postForm.action.value == "Identify") 
		{
//			alert("call details" + sAPNval);
			sNewLoc = "";
			var sURL;

			if (sAPNval.length<20) {
				sURL = 'rolldata.asp?AIN=' + sAPNval;
			}
			else {
				sURL = 'selectBaseLotAINs.asp?ains=' + sAPNval;
			}

			parent.QueryFrame.document.location = sURL;
		} 
//		else {
//				sNewLoc = "";
//				var sURL = 'AttributesDisplay.htm';
//				if (! eval(parent.ToolFrame.win1)) {
//					parent.ToolFrame.win1 = window.open("","Attributes","width="+parent.g_iAttrWidth+",height="+parent.g_iAttrHeight+",scrollbars=yes,resizable=yes");
//				} else if (parent.ToolFrame.win1 && parent.ToolFrame.win1.closed) {
//					parent.ToolFrame.win1 = window.open("","Attributes","width="+parent.g_iRptWidth+",height="+parent.g_iRptHeight+",scrollbars=yes,resizable=yes");
//				} 
//				//theWin = win1;
//				parent.ToolFrame.win1.document.location = sURL;
//				parent.ToolFrame.win1.focus();
//		}			//IdentifySingle
//		}				//sAttributes != null

		if (sNewLoc != "") {
			parent.QueryFrame.document.location = sNewLoc;
		}
	} 
	
//	if ( eval(parent.ToolFrame.win1)) {	//
//		if (! parent.ToolFrame.win1.closed) {
//			parent.focus();
//			parent.ToolFrame.win1.focus();
//			
//		}
//	}
	
	if (ovIsVisible) {
		putExtentOnOVMap();
		hideRetrieveMap();
	}
	
	////single data frame
//	var pathLegend = parent.TextFrame.document.location;
	var pathLegend = parent.QueryFrame.document.location;
	pathLegend = pathLegend.toString();
	
	var uLegend = pathLegend.indexOf("legend");
	ur = "legend.asp?act=" + document.MapForm.actLayer.value + "&sFactor=" + mapScaleFactor;
	//alert("parent.theApp == GenMap: " + parent.theApp);
	if ((sNewLoc == "") &&  (uLegend != -1)) {
			//alert("i am in");
			parent.QueryFrame.document.location = ur;
	}
	
	if (parent.theApp == "GenMap"){
		parent.QueryFrame.document.location = ur;
	}
	//alert(sNewLoc);
	
}

function sendIDsByPost(sIDs, sPg, ppt) {
	// creates new page in TextFrame, inserts IDs in form and sends to ASP page
	//alert("sPg: " + sPg);
	//var sStr = "<HTML><body onload='javascript:document.MapForm.submit();'>";
	var sStr = '<HTML><head><LINK REL="stylesheet" TYPE="text/css" HREF="styles.asp" TITLE="styles"></head><body class="colr">';
	sStr += "<form name='process' action='" + sPg + "' method=post>";
	sStr += "<input type='hidden' name='useType' value='" + ppt + "'>"
	sStr += "<input type=hidden name='IDValue' value='" + sIDs + "'></form>"
	sStr += "</body></html>";
	//single data frame
	//parent.TextFrame.document.open();
	//parent.TextFrame.document.write(sStr);
	//parent.TextFrame.document.close();
	//alert("queryFrame: " + sStr);
	
	parent.QueryFrame.document.open();
	parent.QueryFrame.document.write(sStr);
	parent.QueryFrame.document.close();
}

function clearSelection() {
	var postForm = document.MapForm;
	/*if ((postForm.APNIDs.value != "") || (postForm.BuffIDs.value != "") ) {
		var bstr;
		bstr = "<html><body><div align='center'></div></body></html>";
		parent.TextFrame.document.open();
		parent.TextFrame.document.write(bstr);
		parent.TextFrame.document.close();
	}
	*/
	postForm.IDValue.value = "";
	postForm.IDx.value = "";
	postForm.IDy.value = "";
	postForm.XStrLabel.value = "";
	postForm.DrawX.value = "";
	postForm.DrawY.value = "";
	postForm.BuffIDs.value = "";
	postForm.APNIDs.value = "";
	postForm.fvalue.value = "";
	postForm.layerID.value = "";
	postForm.field.value = "";
	
	postForm.sCat.value = "";
	postForm.sAddress.value = "";
	postForm.sRecdate.value = "";
	postForm.sUsecode.value = "";
	postForm.sArea.value = "";
	postForm.sBB.value = "";
	postForm.sHapn.value = "";
	postForm.sOrdr.value = "";
	
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
	
	sendMapASP();
	if (toolMode==20) updateMeasureBox();
}