﻿// global variables
var treeIsDisabled = false;
var getInfo = false
//Event handler for toolBar
function toolBar_ItemSelect(sender, eventArgs)
{
    var item = eventArgs.get_item();
    var val = item.get_value();
    getInfo = false;
    switch (val)
    {
        case "ZOOMIN":
            theMap.SetCursor('default');
            DoMapCallback("ZOOMIN");
            break;
        case "ZOOMOUT":
            theMap.SetCursor('default');
            DoMapCallback("ZOOMOUT");
            break;
        case "REFRESH":
            theMap.SetCursor('default');
            RefreshMap();
            break;
        case "ZOOMEXTENT":
            theMap.SetCursor('default');
            DoMapCallback("ZOOMEXTENT");
            break;
        case "ZOOMBOX":
            ZoomBox();
            break;
        case "PANHAND":
            PanHand();
            break;
        case "SEARCHGAZ":
            ShowSearchGaz();
            break;
        case "FEATUREINFO":
            DoFeartureInfo();
            break;
    }
}

function DoFeartureInfo()
{
    //tell the map it's in 'normal' mode 
    //in this mode it will raise a client side clicked event
    theMap.SetMode(0);
    theMap.SetCursor('help');
    getInfo = true;
}
function MapClicked(x,y)
{
    //we want to open a new browser at the clicked location
    //returning false stops the map from posting back
    var retval = false;
    if(getInfo)
    {
        //get the world coordinates from the screen coordinates passed in
        var wx = theMap.GetWorldX(x);
        var wy = theMap.GetWorldY(y);
        //Open the new browser and check popups are not being blocked
        r=window.open('info.aspx?x=' + wx.toString() + '&y=' + wy.toString(),"Info","height=550,width=460");
        if (r==null)
        {
            alert('Please enable pop ups for this site')
        }
        else
        {
            r.moveTo(10,10);
            window.setTimeout("FocusOnR();", 10000);
            r.focus();
        }
    }
    
    return retval;
}
function RefreshMap()
{
    //need to get the visible overlays from the treeview to send to the server
    if(!mapCallback.CallbackInProgress)
    {
        //root node is called layers
        var rootNode = trvOverlays.get_nodes().getNode(0);
        var visibleOverlays = GetVisibleOverlayNodes(rootNode);
        //causes the map to postback
        mapCallback.Callback("REFRESH", visibleOverlays, theMap.GetCursor());
    }
}
//recusive function to get all the visible overlays
//returns only the visible overlay numbers in a comma delimeted string
function GetVisibleOverlayNodes(parent)
{
    var childNodes = parent.get_nodes();
    var visibleOverlays = '';
    for(var x = 0; x < childNodes.get_length(); x++)
    {
        var childNode = childNodes.getNode(x);
        var id = childNode.get_id();
        if(id.charAt(0) == 'o')
        {
            if(childNode.get_checked() == true)
                visibleOverlays += childNode.get_value() + ',';
        }
        if(childNode.get_nodes().get_length() > 0)
        {
            visibleOverlays += GetVisibleOverlayNodes(childNode);
        }
    }
    return visibleOverlays;
}
//Disable the tree view
function DisableTree(disable)
{
    //start the batch update
    trvOverlays.beginUpdate();
    //root node is called layers
    var rootNode = trvOverlays.get_nodes().getNode(0);
    ToolBar1.set_enabled(!disable);
    if(disable)
    {
        rootNode.set_cssClass('GrayedTreeNode');
        rootNode.set_hoverCssClass('GrayedTreeNode');
        rootNode.set_selectedCssClass('GrayedTreeNode');
        rootNode.set_selectable(false);
    }
    else
    {
        rootNode.set_cssClass('TreeNode');
        rootNode.set_hoverCssClass('HoverTreeNode');
        rootNode.set_selectedCssClass('SelectedTreeNode');
        rootNode.set_selectable(true);
    }
    //disable all sub nodes
    DisableNodes(rootNode, disable);
    //finishes the batch update
    trvOverlays.endUpdate();
}
//recursive function to disable the treeview nodes
function DisableNodes(parent, disable)
{
    var childNodes = parent.get_nodes();
    
    for(var x = 0; x < childNodes.get_length(); x++)
    {
        var childNode = childNodes.getNode(x);
        if(disable)
        {
            childNode.set_cssClass('GrayedTreeNode');
            childNode.set_hoverCssClass('GrayedTreeNode');
            childNode.set_selectable(false);
        }
        else
        {
            childNode.set_cssClass('TreeNode');
            childNode.set_hoverCssClass('HoverTreeNode');
            childNode.set_selectable(true);
        }        
        if(childNode.get_nodes().get_length() > 0)
        {
            DisableNodes(childNode, disable);
        }
    }
}
//show the gaz panel
function ShowSearchGaz()
{
    treeIsDisabled = true;
    DisableTree(true);
    var mapCont = document.getElementById('mapcontainer');
    var addCont = document.getElementById('addresscontainer');
    
    mapCont.className='hidden';
    addCont.className='mapconatiner';

}
//hide the gaz and show the map
function ShowMap()
{
    treeIsDisabled = false;
    DisableTree(false);
    var mapCont = document.getElementById('mapcontainer');
    var addCont = document.getElementById('addresscontainer');
    
    mapCont.className='mapconatiner';
    addCont.className='hidden';
}
//Causes the map callback to post back
function DoMapCallback(param)
{
    if(!mapCallback.CallbackInProgress)
        mapCallback.Callback(param, theMap.GetCursor());
}
//handles the before check changed event for the treeview
//we use this to disable the checkboxes when the tree is disabled
function trvOverlays_BeforeCheckChanged(sender, eventArgs)
{
    eventArgs.set_cancel(treeIsDisabled);
    if(treeIsDisabled)
        trvOverlays.render();
}
//puts the map object in zoom box mode
function ZoomBox()
{
    theMap.SetMode(1);
    theMap.SetCursor('crosshair');
}
//put the map object in Pan mode
function PanHand()
{
    theMap.SetMode(2);
    theMap.SetCursor('move');
}
//The function the map object uses to postback
function MapPostback(id, args)
{
    if(!mapCallback.CallbackInProgress)
        mapCallback.Callback(id, args, theMap.GetCursor());
    return false;
}
//When the user clicks 1 of the direction buttons this function is called
function Pan(direction)
{
    if(!mapCallback.CallbackInProgress)
        mapCallback.Callback("PAN", direction, theMap.GetCursor());
}
//when the search button is clicked
function SearchAddress()
{
    var streetCntrl = document.getElementById('ctl00_ContentPlaceHolder1_txtStreet');
    var postcodeCntrl = document.getElementById('ctl00_ContentPlaceHolder1_txtPostcode');
    if(streetCntrl != null && postcodeCntrl != null)
    {
        //validate that at least 1 of the gazetteer search fields is populated
        //by using a regular expression
        var street = new String(streetCntrl.value);
        var postcode = new String(postcodeCntrl.value);
        
        var regexp = new RegExp(/^\s*\S.*$/);
	    var postcodefound = postcode.match(regexp);
	    var streetfound = street.match(regexp);
	    if(postcodefound == null && streetfound == null)
	    {
	        alert('Please enter some search criteria');
	        postcodeCntrl.focus();
	    }
	    else
	    {
	        addressCallback.Callback(street, postcode);
	    }
	}
}
//Used by the data grid to add the view button to the address results grid.
//This allows us to get the easting and northing for an address
function GetButtonContent(DataItem)
{
    var ret;
    if(DataItem.GetMember("EASTING").Value != null && DataItem.GetMember("NORTHING").Value != null)
    {
        var hidID = new String("hid_" + DataItem.GetMember("UPRN").Value); 
        ret = "<input type=\"button\" id=\"UPRN_" + DataItem.GetMember("UPRN").Value + "\" value=\"View\" onclick=\"ViewClicked('" + hidID + "'); return false;\"/>";
        ret += "<input type=\"hidden\" id=\"" + hidID + "\" value=\"" + DataItem.GetMember("EASTING").Value + "," + DataItem.GetMember("NORTHING").Value + "\" />";
    }
    else
    {
        ret = '';
    }
    return ret;
}
//Handler for the address results view button. 
function ViewClicked(coordsfieldID)
{
    var hid = document.getElementById(coordsfieldID);
    ShowMap();
    if(!mapCallback.CallbackInProgress)
        mapCallback.Callback("SHOWADDRESS", hid.value, theMap.GetCursor());
}
function OpenWindow(url)
{
    var retval = false;
    
    //Open the new browser and check popups are not being blocked
    r=window.open(url,null,"height=768,width=1024");
    if (r==null)
    {
        alert('Please enable pop ups for this site')
    }
    
    return retval;
}
