function getThisMap()
{
	if (navigator.appName == "Netscape")
		return parent.main.document.map;
	else
		return parent.main.map;
}



function openPointDialog()
{	
	getMGSymbols()
	newwindow = window.open("menucontrols/point_dialog.asp","Point_Dialog", "height=215,width=370,left=300,top=300");
	newwindow.creator=self;
	document.the_form.target = "Point_Dialog";
	document.the_form.action = "menucontrols/point_dialog.asp";
	document.the_form.submit();
}

function openPrintDialog()
{
	newwindow = window.open("menucontrols/print_dialog.asp","Print_Dialog", "height=310,width=340,left=250,top=250");
	newwindow.creator=self;
}

function openCircleDialog()
{
	newwindow = window.open("menucontrols/circle_dialog.asp","Circle_Dialog", "height=225,width=400,left=300,top=300");
	newwindow.creator=self;
}

function openPolylineDialog()
{
	newwindow = window.open("menucontrols/pline_dialog.asp","Polyline_Dialog", "height=200,width=400,left=300,top=300");
	newwindow.creator=self;
}


function openPolygonDialog()
{
	newwindow = window.open("menucontrols/pgon_dialog.asp","Polygon_Dialog", "height=215,width=450,left=300,top=300");
	newwindow.creator=self;
}


function openTextDialog()
{
	getMGfonts();
	newwindow = window.open("menucontrols/text_dialog.asp","text_Dialog", "height=215,width=370,left=300,top=300");
	newwindow.creator=self;
	document.the_form.target = "text_Dialog";
	document.the_form.action = "menucontrols/text_dialog.asp";
	document.the_form.submit();
}


function getMGfonts()

{ 
	var styles;
	var list;
	var map = getThisMap();
	var rlObj = map.getRedlineSetup();
	var txt = rlObj.getTextAttr();
	styles = txt.getFontNames();
	
	list = "<option value ="+String.fromCharCode(34)+styles.item(0) + String.fromCharCode(34)+">" + styles.item(0)+ "</option>";
	
	for (i=1;i<styles.size();i++)
	{
		list = list + "<option value ="+ String.fromCharCode(34)+ styles.item(i) +String.fromCharCode(34)+ ">" + styles.item(i)+ "</option>";
	}
	parent.cmdmenu.document.the_form.TEMPVAR.value = list;
}

	

function addPolygon()
{
	//get map object
	var map = getThisMap();
	
	//if the browser is netscape, use 'Ex' version and pass
	//observer applet;  if browser is IE use "non-Ex' version with no 
	//argument
	if (navigator.appName =="Netscape")
		map.digitizePolygonEx(parent.main.document.obs);
	else
		map.digitizePolygon();
}




function addText()
{
	//add a 1 to the plne_variable
	parent.tmenu.document.the_form.PLINE.value = "1";	
	
	var map = getThisMap();
	
	if (navigator.appName =="Netscape")
		map.digitizePolylineEx(parent.main.document.obs);
	else
		map.digitizePolyline();
}



function addPolyline()
{
	//add 2 to the plne_variable
	parent.tmenu.document.the_form.PLINE.value = "2";
	
	//get map object
	var map = getThisMap();

	//if the browser is netscape, use 'Ex' version and pass
	//observer applet;  if browser is IE use "non-Ex' version with no 
	//argument
	if (navigator.appName =="Netscape")
		map.digitizePolylineEx(parent.main.document.obs);
	else
		map.digitizePolyline();
}


function addCircle()
{
	var map = getThisMap();
	
	if (navigator.appName == "Netscape")
	{	
		map.digitizeCircle("M",parent.main.document.obs);
	}
	else 
	{
		map.digitizeCircle("");
	}
}

function addPoint()
{
	var map = getThisMap();
	top.main.document.the_form.POINTVAR.value = 2;	
	if (navigator.appName == "Netscape")
		map.digitizePoint(parent.main.document.obs);
	else
		map.digitizePoint();
}


function measureDistance()
{
	var map = getThisMap();
    	if (!map.isBusy()) {
		map.viewDistance("");
	} else {
		alert("Wait - The Map is not fully loaded");
	}
}



function onViewedDistance(map, totalDistance, distances, units)
{
	alert(' Measurement Completed.\n Total Distance: ' + formatDecimal(totalDistance, true, 4) 
        + ' ' + units
        + '\n Number of Segments: ' + distances.size()
        + '\n 2nd Last Seg: ' +  formatDecimal(distances.item(distances.size()-2), true, 4)
		+ ' ' + units
		+ '\n Last Seg: ' + formatDecimal(distances.item(distances.size()-1), true, 4)
		+ ' ' + units); 
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

function deleteSelected()
{
	var map=getThisMap();
	var selection=map.getSelection();
	var layer = map.getMapLayer("Redline Layer");
	var select=map.getSelection();
	var keys=select.getAsString(layer,"");	
	
	if (layer != null)
	{	
		if (layer.getMapObject(keys) !=null)
		{
			var obj = layer.getMapObject(keys);
			var prm = obj.getPrimitives();
			layer.removeObject(obj);
		}else{
			alert ("Please select one redline feature at a time.");
		}
	}else{
		alert ("Please select one redline feature at a time.");
	}
}




function bufferSetup()			//Get Buffer Dialogue
{
	var map = getThisMap();
	var theSelection = map.getSelection();
  	var numSelectedObjects = theSelection.getNumObjects();
 	if (numSelectedObjects == 0)
		{
			alert("Please select one or more map features to buffer")
			return
		}
	// Set up buffer attributes
	var buffSet = map.getBufferSetup();
	buffSet.setUnit("ft");
	buffSet.setDistance(300);
	buffSet.setLayerName("Buffer");
	buffSet.setOneObject(true);
	var edge = buffSet.getEdgeAttr();
	edge.setColor(4);
	edge.setStyle("Solid");
	edge.setThickness(3);
	edge.setVisible(true);
	var fill = buffSet.getFillAttr();
	fill.setStyle("Hatch");
	fill.setHatchPattern("Diagonal135");
	fill.setColor(5);
	map.viewBufferDlg();
}



function getMGSymbols()
{
	var map= getThisMap();
	var colSyms = map.getSymbols();
	if (colSyms.size() != 0)
	{
		var list = "<option value = "+String.fromCharCode(34)+ colSyms.item(0)+String.fromCharCode(34) + ">" + colSyms.item(0) + "</option>";
		for (i = 0; i<colSyms.size();i++)
		{
			list = list + "<option value = "+String.fromCharCode(34)+ colSyms.item(i)+String.fromCharCode(34) + ">" + colSyms.item(i) + "</option>";
		}
		parent.cmdmenu.document.the_form.TEMPVAR.value = list;
		//alert(document.the_form.TEMPVAR.value);
	}else{
		parent.cmdmenu.document.the_form.TEMPVAR.value = "";
	}	
}
		
function removeBuffer()
{
	var map = getThisMap();
	map.removeMapLayer("Buffer");
}

function clearSelection()
{
	var map = getThisMap();
	map.getSelection().clear();
}
	

function selectWithin() 
{
	var map = getThisMap();
	map.getSelectionMode();
	map.setSelectionMode("Intersection");
	map.selectWithinDlg();
}

function enable_print_events()
{
	var map = getMap();
	map.enablePrintingEvents();
}


