var selectedAuthorities = new Array();
var debugLevel = 0;
// maps the authority name to their ID
var authorityDB = new Array();
authorityDB["Alnwick"] = new Array( '337' );
authorityDB["Berwick"] = new Array( '338' );
authorityDB["Blyth"] = new Array( '339' );
authorityDB["Castle"] = new Array( '340' );
authorityDB["Tynedale"] = new Array( '341' );
authorityDB["Wansbeck"] = new Array( '342' );

// Lowest level of logging - required debug level of 2 or greater
function debug( msg )
{
        if( debugLevel >= 2 )
        {
                alert( msg );
        }
}

// INFO level debugging - requires debug level of 1 or greater
function info( msg )
{
        if( debugLevel >= 1 )
        {
                alert( msg );
        }
}

// Defines an Authority object
function Authority( _name )
{
  this._name = _name;
}

// Adds the authority (aka district, aka area) to the array of those to be searched
function addAuthority( name )
{
  selectedAuthorities[selectedAuthorities.length] = new Authority( name);
  debug( "Added authority '" + name + "'" );
}

// Removes the authority (aka district, aka area) from the array of those to be searched, if it's present
function removeAuthority( name )
{
  info("checking if area is already added");
  for( var kk = 0; kk < selectedAuthorities.length; kk++ )
  {
    if( selectedAuthorities[kk]._name == name )
    {
      debug( "found area and removing it")
      selectedAuthorities[kk] = null;
      debug( "Removed authority '" + name + "'");
    }
    else
    {
      debug("area not found so sending back array");
      tmpArray[tmpArray.length] = selectedAuthorities[kk];
    }
  }
  selectedAuthorities = tmpArray;
}

// Removes the all the authorities from the array of those to be searched
function clearAuthorities()
{
  var blankArray = new Array();
  selectedAuthorities = blankArray;
}


function searchAllAuthorities()
{
  var frm = document.forms['locationForm'];
  frm.all_areas.value = "true";
  searchAuthorities();
}

function searchSelectedAuthorities()
{
  var frm = document.forms['locationForm'];
  frm.all_areas.value = "false";
  searchAuthorities();
}

// Perform the search by districts
function searchAuthorities()
{
  var frm = document.forms['locationForm'];

  if (frm.all_areas.value == "true") {
        info( "Searching all districts" );
        frm.submit();
        return;
  } else {
        if (! selectedAuthorities.length > 0) {
                alert("Please select an area to search or click the Search All Areas button");
                return;
        } else {
                var buffer = "Searching the following districts:";
                buffer += "\n-------------------------------------";
                for( var kk = 0; kk < selectedAuthorities.length; kk++ ) {
                        buffer += "\n" + selectedAuthorities[kk]._name;
                }
                buffer += "\n-------------------------------------";
                info( buffer );
                var idString = "";
                for( var kk = 0; kk < selectedAuthorities.length; kk++ ) {
                        if( selectedAuthorities[kk] != null ) {
                                authorityIdArray = authorityDB[selectedAuthorities[kk]._name];
                                info( "Found district id array: " + authorityIdArray );
                                        for( var jj = 0; jj < authorityIdArray.length; jj++ ) {
                                                if( idString != "" ) {
                                                        idString += ",";
                                                }
                                                idString +=authorityIdArray[jj];
                                        }
                        }
                }
                info( "Search ID String is: " + idString );
                //frm.action = getAreaAction();
                frm.areaIds.value = idString;
                //frm.proximity.value = "";
                frm.submit();
        }
  }
}

// Toggle all district checkboxes (in HTML form) to an on/off state, following the 'all_areas' checkbox
function toggleAuthorityCheckboxes()
{
  var frm = document.forms['HTMLlocationForm'];
  var targetState = frm.all_areas.checked ? true : false;
  for ( var kk = 0; kk < frm.elements['area'].length; kk++ )
  {
                frm.elements['area'][kk].checked = targetState;
  }
}

// Validate the district search form, when using the HTML form
function validateAuthoritySearchForm()
{
        var frm = document.forms['HTMLlocationForm'];
        if( frm.all_areas.checked )
        {
                return true;
        }

        for( var kk = 0; kk < frm.elements['area'].length; kk++ )
        {
                var box = frm.elements['area'][kk];
                if( box.checked )
                {
                        return true;
                }
        }
        alert( "Please ensure you have selected one or more districts to search within" );
        return false;
}