// JScript File

// Requires: Constants.js.

// Helper Javascript Objects.

// PopupWindow implementation.
function PopupWindow()
{
   this.Url = "";
   this.WindowName = "popupwindow";
   this.Width = 640;
   this.Height = 480;
   this.ShowLocationBar = false;
   this.ShowMenuBar = false;
   this.IsResizable = false;
   this.UseDefaultInitialPosition = true;
   this.InitialPositionX = 320;
   this.InitialPositionY = 240;
   this.ShowScrollBars = false;
   this.ShowStatusBar = false;
   this.ShowToolBar = false;
   this.JSWindow = null;
   this.Open = Open;
   this.BuildWindowFeaturesString = BuildWindowFeaturesString;
   
   function Open()
   {
       if (this.Url == "") {
           alert("PopupWindow: Open: Url not specified.");
           return;
       }
       this.JSWindow = window.open(this.Url, this.WindowName, this.BuildWindowFeaturesString());
       this.JSWindow.focus();
   }
   
   function Close()
   {
       if (this.JSWindow) {
           this.JSWindow.close();
       }
   }
   
   function BuildWindowFeaturesString()
   {
       var windowFeatures = "";
       
       windowFeatures += "width=" + this.Width + "px";
       windowFeatures += ",height=" + this.Height + "px";
       
       if (this.ShowLocationBar) {
          windowFeatures += ",location";
       }
       
       if (this.ShowMenuBar) {
          windowFeatures += ",menubar";
       }
       
       if (this.IsResizable) {
          windowFeatures += ",resizable";
       }
       
       if (!this.UseDefaultInitialPosition) {
          if (document.all) { 
              // Explorer
              windowFeatures += ",left=" + this.InitialPositionX + "px";
              windowFeatures += ",right=" + this.InitialPositionY + "px";
          }
          else { 
              // W3C
              windowFeatures += ",screenX=" + this.InitialPositionX + "px";
              windowFeatures += ",screenY=" + this.InitialPositionY + "px";
          }
       }
       
       if (this.ShowScrollBars) {
           windowFeatures += ",scrollbars";
       }
       
       if (this.ShowStatusBar) {
           windowFeatures += ",status";
       }
       
       if (this.ShowToolBar) {
           windowFeatures += ",toolbar";
       }
       
       return windowFeatures;
   }    
}

// Functions that to process map tools.
function ToolRedraw()
{
    UnselectMapTools();
    MapSetCommand(CMD_REDRAW);
    MapExecute();
}

function ToolZoomIn()
{
    UnselectMapTools();
    SelectMapTool(TOOL_ID_ZOOMIN);
    MapSetCommand(CMD_ZOOMIN);
}

function ToolZoomOut()
{
    UnselectMapTools();
    SelectMapTool(TOOL_ID_ZOOMOUT);
    MapSetCommand(CMD_ZOOMOUT);
}

function ToolZoomBox()
{
    UnselectMapTools();
    SelectMapTool(TOOL_ID_ZOOMBOX);
    SetRBandAspectRatio(RBAND_ASPECT_RATIO_FIXED);
    MapSetCommand(CMD_ZOOMBOX);
}

function ToolZoomAll()
{
    UnselectMapTools();
    MapSetCommand(CMD_ZOOMALL);
    MapExecute();
}

function ToolPan()
{
    UnselectMapTools();
    SelectMapTool(TOOL_ID_PAN);
    MapSetCommand(CMD_PAN);
}

function ToolQuery()
{
    UnselectMapTools();
    SelectMapTool(TOOL_ID_QUERY);
    MapSetCommand(CMD_QUERY);
}

function ToolFind()
{
    UnselectMapTools();
    
    // Open search feature form.
    var pw = new PopupWindow();
    pw.Url = "MapFindFeature.aspx?querylayername=" + encodeURIComponent(GetActiveLayer());
    pw.Width = 400;
    pw.Height = 200;
    pw.WindowName = "MapFindFeaturePopupWindow";
    pw.Open();
    
    MapSetCommand(CMD_FIND);
}

function ToolClearSelection()
{
    UnselectMapTools();
    MapSetCommand(CMD_CLEARSELECTION);
    MapExecute();
}

function ToolMeasureDistance()
{
    UnselectMapTools();
    
    alert("Tool is still under construction. Stay tuned.");
    return;
    
    SelectMapTool(TOOL_ID_MEASUREDISTANCE);
    MapSetCommand(CMD_MEASUREDISTANCE);
}

function ToolLocateLongLat()
{
    UnselectMapTools();
    
    var pw = new PopupWindow();
    pw.Url = "MapFindLongLat.aspx";
    pw.Width = 400;
    pw.Height = 200;
    pw.WindowName = "MapFindLongLatPopupWindow";
    pw.Open();
        
    MapSetCommand(CMD_LOCATELONGLAT);
}

function ToolGetHelp()
{
    UnselectMapTools();
    
    var pw = new PopupWindow();
    pw.Url = "MapHelpPortal.aspx";
    pw.Width = 400;
    pw.Height = 800;
    pw.IsResizable = true;
    pw.ShowLocationBar = true;
    pw.ShowScrollBars = true;
    pw.WindowName = "MapHelpPortalWindow";
    pw.Open();
}

function ToolBuffer() {
    var txtBufferDistance = document.getElementById("txtBufferDistance");
    var ddlBufferUnit = document.getElementById("ddlBufferUnit");
    if(MapIFrame()) {
        MapIFrame().getElementById("hdnbufferdistance").value = txtBufferDistance.value;
        MapIFrame().getElementById("hdnbufferunit").value = ddlBufferUnit.options[ddlBufferUnit.selectedIndex].value;
    }
    MapSetCommand(CMD_CREATEBUFFER);
    MapExecute();
}

var printWindow;
function ToolPrint() {
    if (printWindow && printWindow.close) {
        printWindow.close();
    }
    printWindow = window.open("./PrintLayout.aspx", "PrintLayout", "width=860,height=650,resizable=1");
    if (printWindow && printWindow.focus) {
        printWindow.focus();
    }
}

function ToolSelectBuffer() {
    UnselectMapTools();
    MapSetCommand(CMD_SELECTBUFFER);
    MapExecute();

}

function ToolBufferAddressExcel() {
    HideBufferPopup();
    document.getElementById("ifrDownload").src = "./PrintLayout.aspx?cmd=addresses";
}

function ToolBufferAddressPDF() {
    HideBufferPopup();
    document.getElementById("ifrDownload").src = "./PrintLayout.aspx?cmd=mailer";
}

var prevPaintLayout = false;

function setPrintLayout(printLayout) {
    if (prevPaintLayout == printLayout) return;

    prevPaintLayout = printLayout;

    if (printLayout) {
        setIFrameSize(840, 630);
        MapIFrame().getElementById("hdnsetprintlayout").value = "1";
    } else {
        setIFrameSize(631, 684);
        MapIFrame().getElementById("hdnsetprintlayout").value = "0";
    }

    MapSetCommand(CMD_NOOP);
    UnselectMapTools();
    MapExecute();
}

function setIFrameSize(width, height) {
    var mapIFrame = document.getElementById("mapIFrame");
    if (mapIFrame) {
        mapIFrame.width = width + "px";
        mapIFrame.height = height + "px";
    }
}

// Functions that controls the map in the iframe.

function MapIFrame()
{
    var the_frame = document.getElementById("mapIFrame").contentDocument;
    
    if (the_frame == undefined || the_frame == null) {
        the_frame = document.getElementById("mapIFrame").contentWindow.document;
    }
    
    return the_frame;
}

function MapSetCommand(cmd)
{
    if (MapIFrame()) {
        MapIFrame().getElementById("toolcmd").value = cmd;
    }            
}

function MapExecute()
{
    LoadingIcon().style.visibility = "visible";
    
    if (MapIFrame()) {
        MapIFrame().getElementById("form1").submit();
    }        
}

function SetRBandAspectRatio(val)
{
    if (MapIFrame()) {
        MapIFrame().getElementById("rubberbandaspectratio").value = val;
    }        
}

function SelectMapTool(toolID)
{
    if (document.getElementById(toolID)) {
        document.getElementById(toolID).style.borderStyle = "inset";
    }        
}

function UnselectMapTools()
{
    var tools = document.getElementsByTagName("td");
    for (var i = 0; i < tools.length; i++) {
        // IE 7 specific.
        if (tools[i].className == "maptoolbuttons") {
            tools[i].style.borderStyle = "outset";
        }
    }   
}

function SetLegendRadio(clientID)
{
    // ResetLegendRadios() is generated by server-side scripting.
    ResetLegendRadios();
    if (document.getElementById(clientID)) {
        document.getElementById(clientID).checked = true;
    }        
}

function LoadingIcon()
{
    return document.getElementById("maploadingicon");
}

function MapImageCanvas()
{
    if (MapIFrame()) {
        return MapIFrame().getElementById("mapImageCanvas");
    }
}

function BodyOnLoad()
{
    if (LoadingIcon()) {
        LoadingIcon().style.visibility = "hidden";
    }
}

function SetFeatureVisibility(clientID, layerName)
{
    var newVisibilityStatus = 0;
    if (document.getElementById(clientID).checked) {
        newVisibilityStatus = 1;
    }
    else {
        newVisibilityStatus = 0;
    }

// To be investigated in the future.
//    var httpHandlerUrl = "HttpHandlers/LayerStatusUpdater.ashx";
//    var encodedLayerName = encodeURIComponent(layerName);
//    var postParams = "layername=" + encodedLayerName + "&visibilitytype=1" + "&visible=" + newVisibilityStatus;
//    MakeAsyncXmlHttpCallPost(SetFeatureVisibilityCallBack, httpHandlerUrl, postParams, true);
    
    var encodedLayerName = encodeURIComponent(layerName);
    var httpHandlerUrl = "HttpHandlers/LayerStatusUpdater.ashx?layername=" + encodedLayerName + "&visibilitytype=1" + "&visible=" + 
        newVisibilityStatus;
    
    MakeAsyncXmlHttpCall(SetFeatureVisibilityCallBack, httpHandlerUrl, true);
}

function SetFeatureVisibilityCallBack()
{
    var xmlHttpObj = GetXmlHttpRequestObject();
    if (xmlHttpObj.readyState == READYSTATE_COMPLETE) {
        if (xmlHttpObj.status != HTTPSTATUS_OK) {
            alert("Failed to update feature visibility.");
            return;
        }
        if (AutomaticMapRedraw()) {
            ToolRedraw();
        }
    }
}

function SetLabelVisibility(clientID, layerName)
{
    var newVisibilityStatus = 0;
    if (document.getElementById(clientID).checked) {
        newVisibilityStatus = 1;
    }
    else {
        newVisibilityStatus = 0;
    }

// To be investigated in the future.
//    var httpHandlerUrl = "HttpHandlers/LayerStatusUpdater.ashx";
//    var encodedLayerName = encodeURIComponent(layerName);
//    var postParams = "layername=" + encodedLayerName + "&visibilitytype=2" + "&visible=" + newVisibilityStatus;
//    MakeAsyncXmlHttpCallPost(SetFeatureVisibilityCallBack, httpHandlerUrl, postParams, true);

    var encodedLayerName = encodeURIComponent(layerName);
    var httpHandlerUrl = "HttpHandlers/LayerStatusUpdater.ashx?layername=" + encodedLayerName + "&visibilitytype=2" + "&visible=" + 
        newVisibilityStatus;
    
    MakeAsyncXmlHttpCall(SetFeatureVisibilityCallBack, httpHandlerUrl, true);
}

function SetLabelVisibilityCallBack()
{
    var xmlHttpObj = GetXmlHttpRequestObject();
    if (xmlHttpObj.readyState == READYSTATE_COMPLETE) {
        if (xmlHttpObj.status != HTTPSTATUS_OK) {
            alert("Failed to update label visibility.");
            return;
        }
        if (AutomaticMapRedraw()) {
            ToolRedraw();
        }
    }
}

function GetActiveLayer()
{
    if (MapIFrame()) {
        return MapIFrame().getElementById("activelayername").value;
    }
}

function SetActiveLayer(clientID, layerName)
{
    SetLegendRadio(clientID);
    if (MapIFrame()) {
        MapIFrame().getElementById("activelayername").value = layerName;
    }
}

function AutomaticMapRedraw()
{
    return document.getElementById("ckBoxAutoRedraw").checked;
}

function PlusMinusClick(plusMinusClientID, svMappingClientID)
{
    var plusMinus = document.getElementById(plusMinusClientID);
    var svMapping = document.getElementById(svMappingClientID);
    
    if (PMIsOpened(plusMinus)) 
    {
        PMClose(plusMinus);
        SVMMakeInvisible(svMapping);
    }
    else if (PMIsClosed(plusMinus)) 
    {
        PMOpen(plusMinus);
        SVMMakeVisible(svMapping);
    }
    
}

function PMIsOpened(plusMinus)
{
    return (plusMinus.src.indexOf("tree-minus") > -1);
}

function PMIsClosed(plusMinus)
{
    return (plusMinus.src.indexOf("tree-plus") > -1);
}

function PMOpen(plusMinus)
{
    plusMinus.src = "Resources/images/tree-minus.gif";
}

function PMClose(plusMinus)
{
    plusMinus.src = "Resources/images/tree-plus.gif";
}

function SVMMakeVisible(svMapping)
{
    svMapping.style.display = "";
}

function SVMMakeInvisible(svMapping)
{
    svMapping.style.display = "none";
}

function MapSetLongVal(longval)
{
    if (MapIFrame()) {
        MapIFrame().getElementById("maplongval").value = encodeURIComponent(longval);
    }
}

function MapSetLatVal(latval)
{
    if (MapIFrame()) {
        MapIFrame().getElementById("maplatval").value = encodeURIComponent(latval);
    }
}
