//MapGuide JavaScript Functions File
//This is file is to be included in MapGuide HTML files
//to provide the API functions 

function getMap()
{
	if (navigator.appName == "Netscape")
		return parent.Mapwindow.document.map;
	else
		return parent.Mapwindow.map;
}

function checkSelection()
{
	var MyMapSel = getMap().getSelection();
	
	if (MyMapSel.getNumObjects() >= 1)
        return 0;     
	else
		return 1;
}

function staticBuffer(size)
{
	if (checkSelection() == 0)
	{
	  var MyBuffer = getMap().getBufferSetup();
	  MyBuffer.setDistance(size);
	  MyBuffer.setUnit("FT");
	  MyBuffer.setLayerName("Buffer");
	  var MyEdge = MyBuffer.getEdgeAttr();
	  var MyFill = MyBuffer.getFillAttr();
	  MyFill.setBackMode("Transparent");
	  MyFill.setHatchPattern("DiagonalCross");
	  MyFill.setColor(4);
	  MyFill.setStyle("Hatch");
	  MyEdge.setColor(4);
	  getMap().viewBuffer();
	  parent.Mapwindow.changeBufferFlag(size);
	  }
}	

function measureDistance(){
	var map = getMap();
	map.viewDistance("FT");
}

function userDefBuffer()
{
	if (checkSelection() == 0)
	{
		var userBufSize = parseFloat(prompt("Enter Buffer distance in feet:",""));
		if (!(isNaN(userBufSize)))
		staticBuffer(userBufSize);
	}
}

function zoomToParcel(PIN)
{
	var MyMap = getMap();
	parent.Mapwindow.changePIN(PIN);
	MyMap.zoomGotoLocation("Parcel",PIN,7000);
}

function queryParcel()
{

	var MyMap = getMap();
	var PIN = prompt("Enter PIN:","");
	if (PIN != "" && PIN != null)
	{
	MyMap.zoomGotoLocation("Parcel",PIN,3000);
    }
}

function queryAddress()
{

	var MyMap = getMap();
	var Address = prompt("Enter Address: ","");
	if (Address != "" && Address != null)
	{
	MyMap.zoomGotoLocation("Address",Address,1000);
	}
}

function queryLatLon()
{
	var MyMap = getMap();
	var MyLat = parseFloat(prompt("Enter Latitude: ",""));
	var MyLon = parseFloat(prompt("Enter Longitude: ",""));
	
	
	if (MyLat != "" && MyLat != null && MyLon != "" && MyLon != null && !(isNaN(MyLat)) && !(isNaN(MyLon)))
	{
		MyMap.zoomWidth(MyLat, MyLon, 2000, "FT");
	}

}
function querySectionTwpRng()
{
	var sectionNumber = prompt("Enter Section-Township-Range (Ex: 09-23-15)","");
	var MyMap = getMap();
	if (sectionNumber != "" && sectionNumber != null)
	{
		MyMap.zoomGotoLocation("Sections",sectionNumber,16000);
	}
}
function checkRoadSelection()
{

	var MyMap = getMap();
	var MyMapSel = MyMap.getSelection();
	var numSelected = MyMapSel.getNumObjects();
		
	if (numSelected > 1)   
	    { 
	    	window.open ('selectRoad.cfm?goodSelect=large','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Please select the road segment for the new address and click continue.<br>');
			document.writeln('Please select only one road segment.<br>');
			document.writeln('<a href="drawLeader.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
	
	if (numSelected < 1)
		{
			window.open ('selectRoad.cfm?goodSelect=none','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Please select the road segment for the new address and click continue.<br>');
			document.writeln('Please select only one road segment.<br>');
			document.writeln('<a href="drawLeader.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
	    
	var MyRoadLayer = MyMap.getMapLayer("Roads");
   	var MyRoadSelection = MyMapSel.getMapObjectsEx(MyRoadLayer);
	if (MyRoadSelection.size() > 0)
		{
			
			
			var MyRoadObject = MyRoadSelection.item(0);
			var MyRoadName = MyRoadObject.getName();
			
			document.writeln('You have chosen ' + MyRoadName + ' as the address road<br>');
			document.writeln('If this is OK, click continue<br>');
			document.write('<a href="drawLeader2.cfm?roadName=');
			document.writeln(MyRoadName + '">Continue</a> <a href="Welcome.html">Cancel</a>');
			
			
			return;
		}
		
	else 
		{
			window.open ('selectRoad.cfm?goodSelect=notRoad','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Please select the road segment for the new address and click continue.<br>');
			document.writeln('Please select only one road segment.<br>');
			document.writeln('<a href="drawLeader.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
}

function checkAddressSelection()
{
	var MyMap = getMap();
	var MyMapSel = MyMap.getSelection();
	var numSelected = MyMapSel.getNumObjects();
	
	if (numSelected > 1)   
	    { 
	    	window.open ('selectAddress.cfm?goodSelect=large','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Select the Address Point to be deleted and click continue<br>');
			document.writeln('Please select only one Address Point.<br>');
			document.writeln('<a href="delAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
	
	if (numSelected < 1)
		{
			window.open ('selectAddress.cfm?goodSelect=none','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Select the Address Point to be deleted and click continue<br>');
			document.writeln('Please select only one Address Point.<br>');
			document.writeln('<a href="delAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
		
	var MyAddressLayer = MyMap.getMapLayer("Addresses");
   	var MyAddressSelection = MyMapSel.getMapObjectsEx(MyAddressLayer);
	if (MyAddressSelection.size() > 0)
	{
				
		var MyAddressObject = MyAddressSelection.item(0);
		var MyAddress = MyAddressObject.getKey();
			
		document.writeln('You have chosen ' + MyAddress + ' as the address to delete<br>');
		document.writeln('If this is OK, click continue<br>');
		document.write('<a href="delAddress3.cfm?address=');
		document.writeln(MyAddress + '">Continue</a> <a href="Welcome.html">Cancel</a>');
			
		return;
	}
		
	else 
	{
		window.open ('selectAddress.cfm?goodSelect=notAddress','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
		document.writeln('Select the Address Point to be deleted and click continue<br>');
		document.writeln('Please select only one Address Point.<br>');
		document.writeln('<a href="delAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
		return;
	}
}

function startAddress()
{
	
	parent.frames.ReportWindow.document.clear();
	parent.frames.ReportWindow.document.location = "pickRoad.html";
	
}

function deleteAddress()
{
	parent.frames.ReportWindow.document.clear();
	parent.frames.ReportWindow.document.location = "delAddress.html"
}

function modifyAddress()
{
	parent.frames.ReportWindow.document.clear();
	parent.frames.ReportWindow.document.location = "modifyAddress.html"
}

function checkModAddressSelection()
{
	var MyMap = getMap();
	var MyMapSel = MyMap.getSelection();
	var numSelected = MyMapSel.getNumObjects();
	
	if (numSelected > 1)   
	    { 
	    	window.open ('selectAddress.cfm?goodSelect=large','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Select the Address Point to be modified and click continue<br>');
			document.writeln('Please select only one Address Point.<br>');
			document.writeln('<a href="modAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
	
	if (numSelected < 1)
		{
			window.open ('selectAddress.cfm?goodSelect=none','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
			document.writeln('Select the Address Point to be modified and click continue<br>');
			document.writeln('Please select only one Address Point.<br>');
			document.writeln('<a href="modAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
			return;
		}
		
	var MyAddressLayer = MyMap.getMapLayer("Addresses");
   	var MyAddressSelection = MyMapSel.getMapObjectsEx(MyAddressLayer);
	if (MyAddressSelection.size() > 0)
	{
				
		var MyAddressObject = MyAddressSelection.item(0);
		var MyAddress = MyAddressObject.getKey();
		var MyAddressText = MyAddressObject.getName();
			
		document.writeln('You have chosen ' + MyAddressText + ' as the address to modify<br>');
		document.writeln('Enter new address and click continue, or cancel to quit<br>');
		document.writeln('<form method="post" action="modAddress3.cfm">');
		document.write('<input type="text" name="newAddress" value="');
		document.writeln(MyAddressText + '">');
		document.write('<input type="hidden" name="oldAddress" value="');
		document.writeln(MyAddress + '"><input type="submit" value="Continue"><br><a href="Welcome.html">Cancel</a>');
			
		return;
	}
		
	else 
	{
		window.open ('selectAddress.cfm?goodSelect=notAddress','ModalMap','left=0,top=0,toolbar=0,status=1,resize=1,height='+(screen.height*.25)+',width='+(screen.width*.25));
		document.writeln('Select the Address Point to be deleted and click continue<br>');
		document.writeln('Please select only one Address Point.<br>');
		document.writeln('<a href="delAddress2.cfm">Continue</a> <a href="Welcome.html">Cancel</a>');
		return;
	}
}
function digitizeLeader()
{
	var MyMap=getMap();
	MyMap.digitizePolyline();
}

/*function queryArea()
{
	
	var MyMap = getMap();
	if (checkSelection() == 0)
	{
	//MyMap.setAutoRefresh(false); 
	//MyMap.zoomSelected();
	var MySel = MyMap.getSelection();
	var MyParcelLayer = MyMap.getMapLayer("IndianaCounties");
	var MyParcelSelection = MySel.getMapObjectsEx(MyParcelLayer); 
	if (MyParcelSelection.size() == 1)
		{
			var MyParcelObject = MyParcelSelection.item(0);
			var MyArea = MyParcelObject.getArea();
			//alert("Square Feet: " + MyArea);
			alert ("Acreage is: " + MyArea/43560 + "\n" + "Square Footage is: " + MyArea + "\n" + "Square Miles: " + MyArea/27878400);
			//MyMap.setAutoRefresh(true); 
			//MyMap.refresh();
		}
	else 
		{
			alert ("Select one parcel and click query area again");
		}
	}
	else 
		{
			alert ("Select one parcel and click query area again");
		}
	
}*/

function DynSQL()
{
	window.open('DynSQL.cfm','Modal','toolbar=0,status=1, resizable=1,height=490,width=560');
}

function queryArea(what)
{
	var MyMap = getMap();
	//calculate area of selected parcels
	if (what == "parcel"){
		if (checkSelection() == 0)
		{
			var MySel = MyMap.getSelection();
			var MyParcelLayer = MyMap.getMapLayer("Parcels");
			var MyParcelSelection = MySel.getMapObjectsEx(MyParcelLayer);
			var totalArea = 0;
			for (i=0;i<MyParcelSelection.size();i++){
				var MyParcelObject = MyParcelSelection.item(i);
				var MyArea = MyParcelObject.getArea();
				totalArea = totalArea + MyArea;
			}
			var redlayer = MyMap.getMapLayer("Digitized Polygon");
			var digitizedTotalArea = 0;
			if (redlayer != null){
				var selectedpolygon = MySel.getMapObjectsEx(redlayer);
				for (i=0;i<selectedpolygon.size();i++){
					var redobj = selectedpolygon.item(i);
					var polygonArea = redobj.getArea();
					digitizedTotalArea = digitizedTotalArea + polygonArea;
				}
			}
			var accummulatedArea = totalArea + digitizedTotalArea;		
			alert ("Total Acreage is: " + round(accummulatedArea/43560) + "\n" + "Total Square Footage is: " + round(accummulatedArea,4));
		}
		else 
		{
			alert ("Select a parcel and click query area again");
		}
	}
	//digitizing polygon and calculating area
	else if (what == "drawing"){
		if (MyMap.getScale() > 50000){
			alert("Please zoom in to Parcels layer to draw your polygon.");
		}
		else{
			//remove digitized polygon layer before drawing a new polyon
			var redlayer = MyMap.getMapLayer("Digitized Polygon");
			if (redlayer != null)
			{
				redlayer.removeAllObjects();
			}
			//start digitizing the polygon and calculating its area
			MyMap.digitizePolygon();
		}
	}
}

function clearDigitizedParcels(){
	var redlayer = getMap().getMapLayer("Digitized Polygon");
	if (redlayer != null)
	{
		redlayer.removeAllObjects();
	}
}

//this function was called from map.html
function calcArea(){
	var redlayer = map.getMapLayer("Digitized Polygon");
	if (redlayer != null){
		var redlineobj =  redlayer.getMapObjectsEx();
		var objsize = redlineobj.size();
		var MyArea = redlineobj.item(0).getArea();
		alert ("Total Acreage is: " + round(MyArea/43560) + "\n" + "Total Square Footage is: " + round(MyArea,4));
	}
}
function round(number,X) {
	//rounds number to X decimal places, defaults to 2
    X = (!X ? 4 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}



//this function is used by sales disc application
function parcelzoom(parcel){
		var map = getMap();
		var pin = parcel;
		var objarea = 0;
		var mapSel = map.getSelection();
	    var mapLayer = map.getMapLayer("Parcels");
	    var mapObjects = map.createObject("MGCollection");
		if (!map.isBusy()){
			var mapObj = mapLayer.getMapObject(parcel);
			if (mapObj == null)
				alert("The parcel  " + parcel + "  could not be located on the map.");
			else{
				mapObjects.add(mapObj);
			}
			if (mapObjects.size() > 0){
				mapSel.clear();
		     	mapSel.addObjectsEx(mapObjects, false);
				map.zoomSelected();
			}
		}
}