﻿// Code-behind for Map.aspx

function pageLoad(e) {
  zoomBar.attachEvent("levelchange", zoomBar_levelChange);
}

//
// WebADF overrides
//

function MapDoubleClick(e) {
    iframeShowWorkaroundForVML();
	var vo = map.vectorObject;
	var pix = vo.pixelObject;
	var xycoord = vo.xyCoord;
	map.divObject.style.cursor = "wait";
	var restart = false;
	var clientAction = "Line";
	switch (vo.mode) {
		case 0:
			coordString = pix.x1 + ":" + pix.y1 + "|" + pix.x2 + ":" + pix.y2;
			restart = true;
			break;
		case 1:
			var blurb = "PolyLine:\n";
			coordString ="";
			for (var i=0;i<xycoord.x.length;i++) {
				blurb += "[" + (i+1) + "] " + xycoord.x[i]+ ", "  + xycoord.y[i] + "\n";
				if (i > 0) coordString += "|";
				coordString += xycoord.x[i] + ":" + xycoord.y[i];
			}
			restart = true;
			clientAction = "Polyline";
			break;
		case 2:
			var n = xycoord.x.length - 1;
			var m = n - 1;
			if ((xycoord.x[m]==xycoord.x[n]) && (xycoord.y[m]==xycoord.y[n])) {
				xycoord.x.length = n;
				xycoord.y.length = n;
			}
				
			if (vo.vectorCount>2) {
				var blurb = "Polygon:\n";
				coordString ="";
				for (var i=0;i<xycoord.x.length;i++) {
					blurb += "[" + (i+1) + "] " + xycoord.x[i] + ", "  + xycoord.y[i] + "\n";
					if (i > 0) coordString += "|";
					coordString += xycoord.x[i] + ":" + xycoord.y[i];
				}
				coordString += "|" + xycoord.x[0] + ":" + xycoord.y[0];
				restart = true;
				clientAction = "Polygon";
			} else {
        map.divObject.style.cursor = map.cursor;
        alert("Please add at least three points for an area measurement.");
			}
			
			break;
		case 3:
			var dwidth = Math.abs(pix.x2 - pix.x1);
			var dheight = Math.abs(pix.y2 - pix.y1);
			var dradius;
			if ((dwidth == 0) || (dheight == 0))  {
				dradius = (dwidth == 0) ? dheight : dwidth;
			} else {
				dradius = Math.sqrt((dwidth * dwidth) + (dheight * dheight));
			}
			coordString = pix.x1 + ":" + pix.y1 + ":" + dradius;
			restart = true;
			clientAction = "Circle";
			break;
		case 4:
			var dwidth = Math.abs(pix.x2 - pix.x1);
			var dheight = Math.abs(pix.y2 - pix.y1);
			var dleft = (pix.x1 < pix.x2) ? pix.x1 : pix.x2;
			var dtop = (pix.y1 < pix.y2) ? pix.y1 : pix.y2;
			var centerX = dleft + (dwidth / 2);
			var centerY = dtop + (dheight / 2);
			coordString = centerX+ ":" + centerY + ":" + dwidth + ":" + dheight;
			restart = true;
			clientAction = "Oval";
			break;
		case 5:

			// box
			var dwidth = Math.abs(pix.x2 - pix.x1);
			var dheight = Math.abs(pix.y2 - pix.y1);
			var dleft = (pix.x1 < pix.x2) ? pix.x1 : pix.x2;
			var dtop = (pix.y2 < pix.y1) ? pix.y2 : pix.y1;
			coordString = dleft + ":" + dtop + "|" + (dleft + dwidth) + ":" + (dtop + dheight);
			restart = true;
			clientAction = "Rectangle";			
			break;
	}
	if (restart) {
		vo.vectorCount = 0;
		xycoord.x = new Array();
		xycoord.y = new Array();

		map.divObject.ondblclick = null;
		vo.vectordiv.onmousemove = null;
		vo.vectordiv.ondblclick = null;
		pix.lastX = -9999;
		pix.lastY = -9999;
		
		map.coords = coordString;
		
		if (map.vectorCallbackFunctionString!=null && map.vectorCallbackFunctionString.length>0) {
		    //
		    var argument = "ControlID=" + map.controlName + "&EventArg=" + clientAction + "&ControlType=Map&coords=" + coordString + "&VectorMode=" + map.mode + "&VectorAction=Finish";
		    var context = map.controlName + "," + clientAction;
		    eval(map.vectorCallbackFunctionString);
		} else
		{
		    if (map.preModeExecFunction != null)
		        eval(map.preModeExecFunction);
		
		    postBack( map.controlName, clientAction);
		}
		
		//vo.clear();
		vo.restart(map.clearDrawnVectors);
		map.divObject.style.cursor = map.cursor;
	}
	jumpToFinish = false;
	map.drawShape = false;
	return false;
}

//
// Task Callbacks
//

function taskEmail_failed(error) {
  alert(error);
}

function taskEmail_succeeded(result) {
  alert(result);
  hideFloatingPanel(_controlPrefix + 'fpEmail');
}

function taskMeasureArea_succeeded(result) {
  showMeasureResult("Area", result);
}

function taskMeasureDistance_succeeded(result) {
  showMeasureResult("Distance", result);
}

function taskPrint_succeeded(result) {
  window.open(result);
}

//
// DOM Events
//

function cmdEmail_click(e) {
  showFloatingPanel(_controlPrefix + 'fpEmail');
  document.getElementById("txtEmailRecipientName").focus();
}

function cmdEmailSend_click(e) {
  var arguments = "RecipientName=" + document.getElementById("txtEmailRecipientName").value;
  arguments += "&RecipientAddress=" + document.getElementById("txtEmailRecipientAddress").value;
  arguments += "&SenderName=" + document.getElementById("txtEmailSenderName").value;
  arguments += "&SenderAddress=" + document.getElementById("txtEmailSenderAddress").value;
  arguments += "&Comments=" + document.getElementById("txtEmailComments").value;
  executeTask(arguments, taskEmail);
}

//
// Public Methods
//

function capitalProjectSetTheme(theme) {
  executeTask("Theme=" + theme, taskCapitalProjectSetTheme);
}

function printMap() {
  hideFloatingPanel(_controlPrefix + "fpPrint");
  var txtPrintTitle = document.getElementById("txtPrintTitle");
  executeTask("Title=" + txtPrintTitle.value, taskPrint);
}

function mapMain_scaleChanged(result) {
  zoomBar.setCurrentLevel(new Number(result));
}

function selectTab(tabValue) {
  for (var i = 0; i < _tabValues.length; i++) {
    var panel = document.getElementById(_tabPanelPrefix + _tabValues[i]);
    
    if (_tabValues[i] == tabValue) {
      setVisible(panel, true);
    } else {
      setVisible(panel, false);
    } 
  }
    
  executeTask("TabValue=" + tabValue, taskSelectTab);
}

function setTabState(key, value, type, dataBind, refresh) {
  /// <summary>Use this to set tab control state from a client click</summary>
  executeTask("Key=" + key + "&Value=" + value + "&Type=" + type + "&DataBind=" + String(dataBind) + "&Refresh=" + String(refresh), taskSetTabState);
}

function showMeasureResult(header, result) {
  var lblMeasureResult = document.getElementById("lblMeasureResult");
  
  if (result !== "") {
    lblMeasureResult.innerHTML = header + ": " + result;
    showFloatingPanel(_controlPrefix + "fpMeasure");
  } else {
    lblMeasureResult.innerHTML = "";
    hideFloatingPanel(_controlPrefix + "fpMeasure");
  }
}

function toolbarMain_measureArea() {
  map = Maps[MapNames[0]];
  map.setTool("taskMeasureArea", false, "ClickShape", "crosshair", 2, "visible", "Measure Area - Click to start line. Click again to add points. Double-click to add last point and complete area.", false, taskMeasureArea);
}

function toolbarMain_measureDistance() {
  map = Maps[MapNames[0]];
  map.setTool("taskMeasureDistance", false, "ClickShape", "crosshair", 1, "visible", "Measure Distance - Click to start line. Click again to add points. Double-click to add last point and complete line.", false, taskMeasureDistance);
}

function zoomBar_levelChange(e) {
  executeTask("zoomLevel=" + e.eventData, taskSetZoomLevel);
}
