﻿function ncsSetActiveTool(toolbarName, toolbarItemName, buttonType)
{
    var map = $find('Map1');
    map.divObject.onmouseup = null;
    var f = document.forms[docFormID];
	var imageTag = toolbarName + toolbarItemName + "Image";
	var cell = toolbarName + toolbarItemName;
	var toolbar = Toolbars[toolbarName];
	if (toolbar == null) return;
	if (toolbar.items[toolbarItemName].disabled)
        return;
        
	if (buttonType == "Tool")
	{
	  //change toolbar's selected tool
		f.elements[toolbar.currentToolField].value = toolbarItemName;
		//clientAction for each buddy control
		var clientAction =  toolbar.items[toolbarItemName].clientAction;
		if (clientAction != null)
		{
			var clientActions = "";
		  if (!toolbar.items[toolbarItemName].isClientActionCustom)
		  {
				var buddies = toolbar.buddyControls;
				if (buddies != null)
				{
					for (var i = 0; i < buddies.length; i++)
					{
						var modeField = f.elements[buddies[i] + "_mode"];
						if (modeField != null)
						  modeField.value = toolbarItemName;
						var cursor = toolbar.items[toolbarItemName].cursor;
						if (cursor != null)
						  clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + ",'" + cursor + "'); ";
						else
						  clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + "); ";
					}
				}
			}
			else
			{
				clientActions = clientAction;
			}
	        if (toolbar.items[toolbarItemName].preExecFunction != null)
	            clientActions += toolbar.items[toolbarItemName].preExecFunction;
	            
			var clientActionFunction = new Function(clientActions);
			clientActionFunction.call(null);
			//select this tool and unselect others
			Toolbars[toolbarName].selectTool();
			Toolbars[toolbarName].refreshGroup();
		}
	}
	else if (buttonType == "Command")
	{
	    if (toolbar.items[toolbarItemName].preExecFunction != null)
	        eval(toolbar.items[toolbarItemName].preExecFunction);
	        
		document.body.style.cursor = "wait";
		//reset toolbar images: IE does not refresh toolbar properly if this is not done.
		Toolbars[toolbarName].refreshCommands();
		Toolbars[toolbarName].selectTool();
		//set selected style and image
		var cellElement = document.getElementById(cell);
		cellElement.style.cssText = toolbar.selectedStyle;
		if (toolbar.toolbarStyle != "TextOnly")
		{
			var img = document.images[imageTag];
            switchImageSourceAndAlphaBlend(img,toolbar.items[toolbarItemName].selectedImage);
		}
		var clientAction =  toolbar.items[toolbarItemName].clientAction;
		if (clientAction != null && clientAction.length>0)
        {	
            esriClientActionFunction	 = clientAction;
            window.setTimeout("eval(esriClientActionFunction);", 0);     
	    }
	    else
	        window.setTimeout("postBack('" + toolbarName + "','" +  toolbarItemName + "');", 0);
		    	
		document.body.style.cursor = "default";
	}
	else if (buttonType == "DropDownBox")
	{
	  var selectDropDown = toolbarName + toolbarItemName + "DropDownBox";
	  var selectValueField = toolbarName + toolbarItemName + "Value";
    f.elements[selectValueField].value = f.elements[selectDropDown].value;

        window.setTimeout("postBack('" + toolbarName + "','" +  toolbarItemName + "');", 0);
	}
}

function checkPendingTiles(sender) {

    if (sender!=null) {
          if (sender.pendingTiles.length > 0) {
                Ext.get('MapPanel').mask("Loading...", "x-mask-loading");
          }
          else
          {
                Ext.get('MapPanel').unmask(true);
          }
    } 

}

function ShowBufferWorking()
{
    Ext.get('MapPanel').mask("Buffering...", "x-mask-loading");
    Ext.get('BufferToolbarContainer').mask("Buffering...", "x-mask-loading");
}

function ClearBufferWorking()
{
    Ext.get('MapPanel').unmask(true);
    Ext.get('BufferToolbarContainer').unmask(true);
}


function ncsChangeMeasureUnits() 
{
    var map = $find('Map1');
    var aunits = Ext.get('AreaUnits').dom;
    var munits = Ext.get('MeasureUnits').dom;
    var munits2 = Ext.get('MeasureUnits2').dom;
    var i = munits2.selectedIndex;
    var m = munits2.options[i].value;
    munits.value = m; 
    coordString = map.coords;
    if (coordString==null) coordString="";
    var argument = "ControlID=" + map.controlName + "&EventArg=" + m_currentMeasureToolbarTool + "&ControlType=Map&coords=" + coordString + "&VectorMode=" + map.mode + "&VectorAction=AddPoint&MeasureUnits=" + m;
   if (checkForFormElement(document, 0, "AreaUnits")) {
        argument += "&AreaUnits=" + aunits.value;
   } 
    var context = map.controlName + "," + m_currentMeasureToolbarTool;

    	// Debug stuff to be removed, or at least commented out
   	if (checkForFormElement(document, 0, "MapDebugBox")) document.forms[0].MapDebugBox.value += ("Vector Request: " + context + ": MeasureUnits=" + m + "\n"); 
		                
    eval(map.vectorCallbackFunctionString);    
}

// update area unit settings... request new totals from server
function ncsChangeAreaUnits() 
{
    var map = $find('Map1');
    var munits = Ext.get('MeasureUnits').dom;
    var aunits = Ext.get('AreaUnits').dom;
    var aunits2 = Ext.get('AreaUnits2').dom;
    var i = aunits2.selectedIndex;
    var a = aunits2.options[i].value;
    aunits.value = a; 
    coordString = map.coords;
    if (coordString==null) coordString="";
    var argument = "ControlID=" + map.controlName + "&EventArg=" + m_currentMeasureToolbarTool + "&ControlType=Map&coords=" + coordString + "&VectorMode=" + map.mode + "&VectorAction=AddPoint&AreaUnits=" + a;
   if (checkForFormElement(document, 0, "MeasureUnits")) {
        argument += "&MeasureUnits=" + munits.value;
   } 
    var context = map.controlName + "," + m_currentMeasureToolbarTool;

    	// Debug stuff to be removed, or at least commented out
   	if (checkForFormElement(document, 0, "MapDebugBox")) document.forms[0].MapDebugBox.value += ("Vector Request: " + context + ": AreaUnits=" + a + "\n"); 
		                
	eval(map.vectorCallbackFunctionString);    
}

function ncsGoPreviousExtent()
{
    var map = $find('Map1');
    var message = 'ControlID=Map1&ControlType=Map&EventArg=';
    var context = map.controlName;
    message = message + 'PreviousExtent';
    eval("WebForm_DoCallback('__Page',message,ESRI.ADF.System.processCallbackResult,context,null,true)");
}

function ncsRefreshMap()
{
    var map = $find('Map1');
    var message = 'ControlID=Map1&ControlType=Map&EventArg=';
    var context = map.controlName;
    message = message + 'RefreshMap';
    eval("WebForm_DoCallback('__Page',message,ESRI.ADF.System.processCallbackResult,context,null,true)");
}

function ncsClearHighlights()
{
    var map = $find('Map1');
    var message = 'ControlID=Map1&ControlType=Map&EventArg=';
    var context = map.controlName;
    message = message + 'ClearHighlights';
    eval("WebForm_DoCallback('__Page',message,ESRI.ADF.System.processCallbackResult,context,null,true)");
}

function ncsClearBuffer()
{
    var map = $find('Map1');
    var message = 'ControlID=Map1&ControlType=Map&EventArg=';
    var context = map.controlName;
    message = message + 'ClearBuffer';
    eval("WebForm_DoCallback('__Page',message,ESRI.ADF.System.processCallbackResult,context,null,true)");
}

