﻿var redlineLayerName = "My Redline";

function getMainMap()
{
    if (navigator.appName == "Netscape")
    {
        try
        {
            return top.opener.top.mapFrame.map.document.embeds[0];
        }
        catch(Error)
        {
            return top.opener.top.mapFrame.document.embeds[0];
        } 	
    }
    else
    {
        try
        {
            return top.opener.top.mapFrame.map.document.myMap;
        }
        catch(Error)
        {
            return top.opener.top.mapFrame.document.myMap;
        } 	
    }
}

function getMap()
{
    if (navigator.appName == "Netscape")
    {
        return document.myMap;
    }
    else
    {
        return myMap;
    }
}

function openDrawingToolkitWindow()
{
    if(drawingToolkitWindow == null || drawingToolkitWindow.closed == true)
    {
        var drawingToolkitWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=790,height=450";
	    drawingToolkitWindow = open("DrawingTKtabs.aspx", "drawingToolkit", drawingToolkitWindowProperties);
	}
	else
	{
	    drawingToolkitWindow.focus();
	}
}

function doThis(theAction)
{
    var theMap = getMap();
        		
    try
    {
	    var theMapFrame = top.mapFrame.map;
	}
	catch(Error)
	{
	    var theMapFrame = top.mapFrame;
	}
	
	if (theMap.isBusy() || theMap == null)
	{
	    alert("The map is busy.");
		theMap.stop();
		theMap.refresh();
	}
	else
	{
	    switch (theAction)
	    {
		    case "print":
			    theMapFrame.print();
				break;
			default :
				alert("Unknown Action");
	    }
	}
}
            
function add_pline()
{
    var map = getMainMap();
    var symOpt = document.draw_form.CtrDrawingToolKit_Pline1_draw_colour.options;
    var symOptline = document.draw_form.CtrDrawingToolKit_Pline1_draw_linetype.options;
                
	if (symOpt.value == "Select Color")
	{  
	    alert("Please select a Color.");
		return false;
	}
	else
	{
        if (symOptline.value == "Select Type")
        {  
		    alert("Please select a Line Type.");
			return false;
		}
		else
		{
	        // if browser is Netscape, use 'Ex' version and pass observer applet; if browser is Internet Explorer, use 'non-Ex' version with no argument
	        if (navigator.appName == "Netscape")
	        {
	            try
	            {
	                map.digitizePolylineEx(top.opener.top.mapFrame.map.document.obs);
	            }
	            catch(Error)
	            {
	                map.digitizePolylineEx(top.opener.top.mapFrame.document.obs);
	            }
	        }
	        else
	        {
	            try
	            {
	                top.opener.top.mapFrame.map.addLine("tkLine");
	            }
	            catch(Error)
	            {
	                top.opener.top.mapFrame.addLine("tkLine");
	            }
	            top.opener.focus();
            }
        }
    }
}

function addLine(sender)
{
    lnType = sender;	
    getMap().digitizePolyline();
}

function addLineFromDrawingToolkit(map, numPoints, points)
{
    // create redline layer, or get it if it already exists
   	var myLayer = rlLyrSetup(redlineLayerName);
	var redSetup = getMap().getRedlineSetup();
	var lineAtt = redSetup.getLineAttr();
	var theThickness = parseInt(drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Pline1_draw_thick.value);
	var theColorOpt = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Pline1_draw_colour.options;
    var theColor = optSel(theColorOpt);
	var theLineTypeOpt = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Pline1_draw_linetype.options;
	var theLineType = optSel(theLineTypeOpt);
	lineAtt.setColor(theColor);
	lineAtt.setThickness(theThickness);
	lineAtt.setStyle(theLineType);
    // create redline feature, or get it if it already exists (getMapObject takes a feature key as its value, 
    // while createMapObject takes a key and a name -- the formText variable supplies both of those values)
    var obj = myLayer.getMapObject("");
    if (obj == null)
    {
        var obj = myLayer.createMapObject("", "", "");
    }
    // create MGCollection that holds user-specified polygon vertices
    var user_vertices = map.createObject("mgcollection");
    user_vertices.add(numPoints);
    // use MGCollection to create polyline primitive and add it to redline feature 
    obj.addPolylinePrimitive(points, user_vertices, false);
}

function add_pgon()
{
    var map = getMainMap();	
    var symOpt = document.draw_form.CtrDrawingToolKit_Poly1_draw_colour_poly.options;
    var symOptline = document.draw_form.CtrDrawingToolKit_Poly1_draw_linetype_poly.options;
                
	if (symOpt.value == "Select Color")
	{  
	    alert("Please select a Color.");
		return false;
	}
	else
    {        		
	    if (symOptline.value == "Select Type")
	    {  
		    alert("Please select a Line Type.");
		    return false;
	    }
	    else
        {
            if (map.isBusy() || map == null)
            {
		        setTimeout("add_pgon();", 500);
	        }
	        else
	        {
	            // if browser is Netscape, use 'Ex' version and pass observer applet; if browser is Internet Explorer, use 'non-Ex' version with no argument
	            if (navigator.appName == "Netscape")
	            {
	                map.digitizePolygonEx(top.opener.top.mapFrame.document.obs);
	            }
	            else
	            {
	                map.digitizePolygon();
	            }
	            window.opener.focus();
            }
        }
    }
}

function add_text()
{
    var map = getMainMap();	
    var symOpt = document.draw_form.CtrDrawingToolKit_Text1_draw_colour_addtext.options;

    if (symOpt.value == "Select Color")
    {  
        alert("Please select a Color.");
        return false;
    }
    else
    {           
        if (map.isBusy() || map == null)
        {
            setTimeout("add_text();", 500);
        }
        else
        {
            if (navigator.appName == "Netscape")
            {
                try
                {
                    map.digitizePoint(top.opener.top.mapFrame.map.document.obs);
                }
                catch(Error)
                {
                    map.digitizePoint(top.opener.top.mapFrame.document.obs);
                } 	
            }
            else
            {
                try
                {
                    top.opener.top.mapFrame.map.addPoint("tkText");
                }
                catch(Error)
                {
                    top.opener.top.mapFrame.addPoint("tkText");
                } 	
            }
        }
    }
    window.opener.focus();
}

function drawPolygonFromDrawingToolkit(map, numPoints, points)
{
    // create variable and assign it user-specified value from 'Polygon Name' text box
	// Get the settings from the drawing toolkit window...
	var theThickness = parseInt(drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Poly1_draw_thick_poly.value);
	// Because the colour is from an option list, we need to loop through the list in order to find the selected item...
	var theColorOpt = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Poly1_draw_colour_poly.options;
	for(var i = 0; i < theColorOpt.length; i++)
	{
	    if(theColorOpt[i].selected)
		{  
		    var theColor = theColorOpt[i].value;
		}
	}
	var theLineTypeOpt = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Poly1_draw_linetype_poly.options;
	for(var i = 0; i < theLineTypeOpt.length; i++)
	{
	    if(theLineTypeOpt[i].selected)
		{  
			var theLineType = theLineTypeOpt[i].value;
		}
	}
    var formText = "redline object";
    // create redline layer, or get it if it already exists
    var myLayer = map.getMapLayer(redlineLayerName);
    if (myLayer == null)
    {
        myLayer = map.createLayer("redline", redlineLayerName);
    }
	var redSetup = getMap().getRedlineSetup();
	var edgeAtt = redSetup.getEdgeAttr();
	// Set the Edge attributes (can also add fill attributes for the polygon)...
	edgeAtt.setColor(theColor);
	edgeAtt.setThickness(theThickness);
	edgeAtt.setStyle(theLineType);

    // create redline feature, or get it if it already exists (getMapObject takes a feature key as its value, while createMapObject takes a key and
    // a name -- the formText variable supplies both of those values)
    var obj = myLayer.getMapObject("");
    if (obj == null)
    {
        var obj = myLayer.createMapObject("", "", "");
    }
    // create MGCollection that holds user-specified polygon vertices
    var user_vertices = map.createObject("mgcollection");
    user_vertices.add(numPoints);
    // use MGCollection to create polyline primitive and add it to redline feature 
    obj.addPolygonPrimitive(points, user_vertices, false);
}

function add_symbol(type)
{
    var map = getMainMap();
	var symOpt = document.draw_form.Ctrdrawingtoolkit_Symbol1_draw_symbol_name.options;
	if (symOpt.value == "Select Type")
	{  
	    alert("Please select a symbol.");
		return false;
	}
	else
	{
	    if (map.isBusy() || map == null)
	    {
		    setTimeout("add_symbol();", 500);
		}
		else
		{
		    // if browser is Netscape, use 'Ex' version and pass observer applet; if browser is Internet Explorer, use 'non-Ex' version with no argument
			if (navigator.appName == "Netscape")
			{
			    try
			    {
			        map.digitizePoint(top.opener.top.mapFrame.map.document.obs);
			    }
			    catch(Error)
			    {
			        map.digitizePoint(top.opener.top.mapFrame.document.obs);
			    }
			}
			else
			{
			    try
			    {
			        top.opener.top.mapFrame.map.addPoint(type);
			    }
			    catch(Error)
			    {
			        top.opener.top.mapFrame.addPoint(type);
			    }
			}
		    window.opener.focus();
		}
	}
}

function addPoint(sender)
{
    ptType = sender;	
    getMap().digitizePoint();
}

function addSymbolFromDrawingToolkit(map, point)
{		    
    //Get the symbol attributes from the values selected in the drawing toolkit
	var theSymbolOpt = drawingToolkitWindow.document.draw_form.Ctrdrawingtoolkit_Symbol1_draw_symbol_name.options;
	var	theSymName = optSel(theSymbolOpt);
	var theRotation = drawingToolkitWindow.document.draw_form.Ctrdrawingtoolkit_Symbol1_draw_symbol_rotate.value;
	var theWidth = drawingToolkitWindow.document.draw_form.Ctrdrawingtoolkit_Symbol1_draw_symbol_width.value;
	var theHeight = drawingToolkitWindow.document.draw_form.Ctrdrawingtoolkit_Symbol1_draw_symbol_height.value;
	//Call the function to set the new redline symbol attributes
	setSymAttt(theSymName, theRotation, theWidth, theHeight);
	//Create the symbols on the layer called My Redline
	var myLayer = rlLyrSetup(redlineLayerName);
	//Redline objects don't have a key value
	var keyID = "";
	//Call the function to create the new redline object
	addSymObj(point, myLayer, keyID);
}
			
function addTextFromDrawingToolkit(map, point)
{
    //Create the text on the redline layer
	var myLayer = rlLyrSetup(redlineLayerName);
	//Call the function to draw text		
	addTxt(map,point,myLayer);
}

function addTxt(map,point,myLayer)
{
    var redSetup = map.getRedlineSetup();	
	var textAtt = redSetup.getTextAttr();
	var redSetup = map.getRedlineSetup();
	// Add colour to text
	var theColourOpt = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Text1_draw_colour_addtext.options;
    var theColour = optSel(theColourOpt);
	var theHeight = parseInt(drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Text1_draw_text_size.value);
	var theRotation = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Text1_draw_text_rotate.value;
	textAtt.setColor(theColour);
	textAtt.setRotation(theRotation);
	textAtt.setHeight(theHeight, "M");
	var theText = drawingToolkitWindow.document.draw_form.CtrDrawingToolKit_Text1_draw_text_text.value;
	// create redline object, or get it if it already exists (getMapObject
	// takes an object key as its value, while createMapObject takes a key and
	// a name -- the formText variable supplies both of those values)
	var obj = myLayer.getMapObject("");
	if(obj == null)
	{
	    var obj = myLayer.createMapObject("", theText, "");
	}
	obj.addTextPrimitive(point, false, theText);
}

function optSel(options)
{
    for(var i=0; i < options.length; i++)
    {
	    if (options[i].selected)
		{  
		    return options[i].value;
		}
	}
}

function rlLyrSetup(theLayerName)
{
    var myMap = getMap();
    var myLayer = myMap.getMapLayer(theLayerName);
    if (myLayer == null)
    {
        myLayer = myMap.createLayer("redline", theLayerName);
	}
    return myLayer;
}

function setSymAttt(theSymName, theRotation, theWidth, theHeight)
{
    var theMap = getMap();
	var redSetup = theMap.getRedlineSetup();
	var symAtt = redSetup.getSymbolAttr();
	symAtt.setSymbol(theSymName);
	symAtt.setRotation(theRotation);
	symAtt.setWidth(theWidth, "M");
	symAtt.setHeight(theHeight, "M");
}

function addSymObj(point, myLayer, theKeyID)
{
    var obj = myLayer.getMapObject("");
    if(obj == null)
    {
	    var obj = myLayer.createMapObject(theKeyID, "", "");
	}
	obj.addSymbolPrimitive(point, false);
}

function rem_obj()
{
    var map = getMainMap();
	if (map.isBusy() || map == null)
	{
	    setTimeout("rem_obj();", 500);
	}
	else
	{
	    //get selected objects
		map.setAutoRefresh(false);
		var theSelection = map.getSelection();

		if (theSelection.getNumObjects() > 0)
		{
		    // Get all the selected objects
			var theObjects = theSelection.getMapObjectsEx(null);
			// Get the redline layer...
			var theLayer = map.getMapLayer(redlineLayerName);
			// Remove all the selected objects that are on the redline layer...
			theLayer.removeObjects(theObjects);
			// Refresh the viewer...
			map.setAutoRefresh(true);
			map.refresh();
		}
		else
		{
		    alert("Select an object to remove.");
		}
    }
}