  /****************************************************************
    Enter handler 
  ****************************************************************/
  function OnEnterDown(e)
  {
    if (e == null) 
    {
      e = window.event;
    }
    
    if (e.keyCode == 13)
    {
      PerformAreaSearch(); 
    }
  }

  /****************************************************************
    save the initial prams description for RESET
    function 
  ****************************************************************/
  function SaveInitPRAMSDescription()
  {
    var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID); 

    if (divProjectDetails != null)
    {
      g_initProjectDetailsDisplay = divProjectDetails.innerHTML;
    }
  }
  
  /****************************************************************
    Set initial value for search related controls 
  ****************************************************************/
  function InitialiseSearchControls()
  { 
    // this is to reset the forms for FireFox in case the refresh button was clicked     
    if (document.forms.length > 0)
    {
      for (var i = 0; i < document.forms.length; i++)
      {
        document.forms[i].reset();
      }
    }

    InitAddressSearchControls();
    if (SHOW_PROJECT_SEARCH) InitProjectSearchControls();
    if (SHOW_COUNCIL_SEARCH) InitCouncilSearchControls();
    if (SHOW_ELECTORATE_SEARCH) InitElectorateSearchControls();    
        
    if(document.addEventListener)
    { 
      document.addEventListener('keydown',OnEnterDown, true); //W3C 
    } 
    else if (document.attachEvent)
    {
      document.attachEvent('onkeydown',OnEnterDown); //IE 
    } 
    else
    {
      document.onkeydown = OnEnterDown;
    }   
  }
     
  /****************************************************************
    Init Address search tab. 
    This function will allow server to set up initial values for the controls 
  ****************************************************************/
  function InitAddressSearchControls()
  {
    var txtStreet = document.getElementById(CTRL_STREET_ID);
    if (txtStreet != null && (txtStreet.value == null || txtStreet.value == ""))
    {
      txtStreet.value = INIT_STREET;
      txtStreet.style.color = COLOUR_WATERMARK;  
    }

    var txtSuburb = document.getElementById(CTRL_SUBURB_ID);
    if (txtSuburb != null && (txtSuburb.value == null || txtSuburb.value == ""))
    {
      txtSuburb.value = INIT_SUBURB;
      txtSuburb.style.color = COLOUR_WATERMARK;  
    }
    
    var txtPostcode = document.getElementById(CTRL_POSTCODE_ID);
    if (txtPostcode != null && (txtPostcode.value == null || txtPostcode.value == ""))
    {
      txtPostcode.value = INIT_POSTCODE;
      txtPostcode.style.color = COLOUR_WATERMARK;  
    }

    var cboState = document.getElementById(CTRL_STATE_ID);
    if (cboState != null && cboState.value == "")
    {
      cboState.style.color = COLOUR_WATERMARK;  
    }
  }
    
  /****************************************************************
    Init Project search tab 
    This function will allow server to set up initial values for the controls 
  ****************************************************************/
  function InitProjectSearchControls()
  {
    var txtProjectId = document.getElementById(CTRL_PROJECT_ID);
    if (txtProjectId != null && (txtProjectId.value == null || txtProjectId.value == ""))
    {
      txtProjectId.value = INIT_PROJECT_ID;
      txtProjectId.style.color = COLOUR_WATERMARK;  
    }

    var txtProjectName = document.getElementById(CTRL_PROJECT_NAME);
    if (txtProjectName != null && (txtProjectName.value == null || txtProjectName.value == ""))
    {
      txtProjectName.value = INIT_PROJECT_NAME;
      txtProjectName.style.color = COLOUR_WATERMARK;  
    }    
  }
  
  /****************************************************************
    Init Council search tab 
    This function will allow server to set up initial values for the controls 
  ****************************************************************/
  function InitCouncilSearchControls()
  {
    var cboCouncil = document.getElementById(CTRL_COUNCIL_ID);
    if (cboCouncil != null && cboCouncil.value == "")
    {
      cboCouncil.style.color = COLOUR_WATERMARK;  
    }
  }
  
  /****************************************************************
    Init Electrate search tab 
    This function will allow server to set up initial values for the controls 
  ****************************************************************/
  function InitElectorateSearchControls()
  {
    var cboElectorate = document.getElementById(CTRL_ELECTORATE_ID);
    if (SHOW_ELECTORATE_SEARCH && cboElectorate != null && cboElectorate.value == "")
    {
      cboElectorate.style.color = COLOUR_WATERMARK;  
    }    
  }
  
  /****************************************************************
    Flex Callback function
    after the flex component loaded 
  ****************************************************************/
  function mapLoaded()
  {
    if (INIT_FUNCTION_EXECUTION) 
    {
      if (SHOW_PROJECT_SEARCH) InitProjectSearchState();
    }
    else
    {
      //ResetSearch();
    }
    //HideWaitScreen();    
  }  
  
  /****************************************************************
    Restore the project search display and replay the project search 
  ****************************************************************/
  function InitProjectSearchState()
  {
    var i;
    
    if (arrInitMapLayers != null)
    {
      for (i=0; i<arrInitMapLayers.length; i++)
      {
        updateLayerVisibilityCheckbox(LAYER_GIOSERVICE, arrInitMapLayers[i], true);
      }
    }
    if (arrInitBoundaryLayers != null)
    {
      for (i=0; i<arrInitBoundaryLayers.length; i++)
      {      
        updateLayerVisibilityCheckbox(BOUNDARY_GIOSERVICE, arrInitBoundaryLayers[i], true);
      }
    }
    
    var optProjectSearch = document.getElementById(SEARCH_OPTION_BY_PROJECT); 
    
    if (optProjectSearch != null) 
    {
      optProjectSearch.checked = true;
      OnSelectSearchOption(optProjectSearch);
    }

    var txtProjectId = document.getElementById(CTRL_PROJECT_ID);
    if (txtProjectId != null)
    {
      txtProjectId.value = INIT_SESSION_PROJECT_ID;
      txtProjectId.style.color = COLOUR_DEFAULTTEXT;  
    }  
    
    TriggerProjectSearch();    
    //if(document.addEventListener)
    //{ 
    //  window.addEventListener('load',TriggerProjectSearch,false); //W3C 
    //} 
    //else
    //{
    //  window.attachEvent('onload',TriggerProjectSearch); //IE 
    //}
  }
  
  /****************************************************************
    Replay the project search 
  ****************************************************************/
  function TriggerProjectSearch()
  {
    if (INIT_SESSION_PROJECT_ID != null && INIT_SESSION_PROJECT_ID != "")
    {    
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        try
        {        
          PerformAreaSearchWraper(flashComponent, "", "", "", "", "", "", INIT_SESSION_PROJECT_ID, "");	        
        }
        catch (ex)
        {
          alert(ex);
        } 
      } 
    }
  }
  
  /****************************************************************
    Reset page 
  ****************************************************************/  
  function ResetSearch()
  {
    var flashComponent = GetMapComponent();
    if (flashComponent != null)
    {
	    //reset the global variables
	    g_layerUpdateNeverCalled = true;
      g_boundaryUpdateNeverCalled = true;

      g_lastSearchCriteria = null;
      g_xmlParseErrorCode = null;
      g_flashingProjectQueue = ",";
  
      // reset map, callback will reset layers
	    flashComponent.ResetMap();  
	  }
	  
	  //reset search 
    var optAddressSearch = document.getElementById(SEARCH_OPTION_BY_ADDRESS); 
    if (optAddressSearch != null) 
    {
      optAddressSearch.checked = true;
      OnSelectSearchOption(optAddressSearch);
    }
	  
	  //reset report link 
    ResetReportLink();  

	  //reset project display 
    var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID); 
    if (divProjectDetails != null)
    {
      if (g_isIE)
      { 
        divProjectDetails.innerHTML = g_initProjectDetailsDisplay;
      }
      else
      {
        var ci = divProjectDetails.childNodes.length - 1; 
        for (; ci >= 0; ci--)
        {
          divProjectDetails.removeChild(divProjectDetails.childNodes[ci]);
        }
        divProjectDetails.innerHTML = g_initProjectDetailsDisplay;

      }
    }

    //window.event.cancelBubble = true;
    //window.event.returnValue = false;

    //return false;
	  //-----------------------
	  // previous implementation
	  //-----------------------
    //history.go(0);
	  //-----------------------
  }
  
  /****************************************************************
    Reset address search tab 
  ****************************************************************/    
  function ResetAddressSearch()
  {
    var txtStreet = document.getElementById(CTRL_STREET_ID);
    if (txtStreet != null)
    {
      txtStreet.value = INIT_STREET;
      txtStreet.style.color = COLOUR_WATERMARK;  
    }

    var txtSuburb = document.getElementById(CTRL_SUBURB_ID);
    if (txtSuburb != null)
    {
      txtSuburb.value = INIT_SUBURB;
      txtSuburb.style.color = COLOUR_WATERMARK;  
    }
    
    var txtPostcode = document.getElementById(CTRL_POSTCODE_ID);
    if (txtPostcode != null)
    {
      txtPostcode.value = INIT_POSTCODE;
      txtPostcode.style.color = COLOUR_WATERMARK;  
    }

    var cboState = document.getElementById(CTRL_STATE_ID);
    if (cboState != null)
    {
      cboState.value = "";
      cboState.style.color = COLOUR_WATERMARK;  
    }
  }

  /****************************************************************
    Reset project search tab 
  ****************************************************************/    
  function ResetProjectSearch()
  {
    var txtProjectId = document.getElementById(CTRL_PROJECT_ID);
    if (txtProjectId != null)
    {
      txtProjectId.value = INIT_PROJECT_ID;
      txtProjectId.style.color = COLOUR_WATERMARK;  
    }

    var txtProjectName = document.getElementById(CTRL_PROJECT_NAME);
    if (txtProjectName != null)
    {
      txtProjectName.value = INIT_PROJECT_NAME;
      txtProjectName.style.color = COLOUR_WATERMARK;  
    }
  }
  
  /****************************************************************
    Reset council search tab 
  ****************************************************************/    
  function ResetCouncilSearch()
  {
    var cboCouncil = document.getElementById(CTRL_COUNCIL_ID);
    if (cboCouncil != null)
    {
      cboCouncil.value = "";
      cboCouncil.style.color = COLOUR_WATERMARK;  
    }
  }

  /****************************************************************
    Reset electorate search tab 
  ****************************************************************/    
  function ResetElectorateSearch()
  {
    var cboElectorate = document.getElementById(CTRL_ELECTORATE_ID);
    if (SHOW_ELECTORATE_SEARCH)
    {
      cboElectorate.value = "";
      cboElectorate.style.color = COLOUR_WATERMARK;  
    }
  }

  /****************************************************************
    Reset project details area to empty 
  ****************************************************************/    
  function ResetProjectDetails()
  {
    var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID);
    if (divProjectDetails != null)
    {
      divProjectDetails.innerHTML = "&nbsp;";
    }
  }
    
  /****************************************************************
    event handler in response to the search tab selection 
  ****************************************************************/    
  function OnSelectSearchOption(radOption)
  {
    if (radOption == null || radOption.id == null) return;

    var secAddress = document.getElementById(SECTION_ADDRESS_SEARCH);
    var secProject = document.getElementById(SECTION_PROJECT_SEARCH);
    var secCouncil = document.getElementById(SECTION_COUNCIL_SEARCH);
    var secElectorate = document.getElementById(SECTION_ELECTORATE_SEARCH);

    if (radOption.id == SEARCH_OPTION_BY_ADDRESS)
    {
      if (secAddress != null) secAddress.style.display = "block";
      if (secProject != null) secProject.style.display = "none";       
      if (secCouncil != null) secCouncil.style.display = "none"; 
      if (secElectorate != null) secElectorate.style.display = "none";      
    }
    else if (radOption.id == SEARCH_OPTION_BY_PROJECT)
    {
      if (secAddress != null) secAddress.style.display = "none";
      if (secProject != null) secProject.style.display = "block";       
      if (secCouncil != null) secCouncil.style.display = "none"; 
      if (secElectorate != null) secElectorate.style.display = "none";
    }
    else if (radOption.id == SEARCH_OPTION_BY_COUNCIL)
    {
      if (secAddress != null) secAddress.style.display = "none";
      if (secProject != null) secProject.style.display = "none";       
      if (secCouncil != null) secCouncil.style.display = "block"; 
      if (secElectorate != null) secElectorate.style.display = "none";
    }    
    else if (radOption.id == SEARCH_OPTION_BY_ELECTORATE)
    {
      if (secAddress != null) secAddress.style.display = "none";
      if (secProject != null) secProject.style.display = "none";       
      if (secCouncil != null) secCouncil.style.display = "none"; 
      if (secElectorate != null) secElectorate.style.display = "block";       
    }
    
    ResetAddressSearch();
    ResetProjectSearch();
    ResetCouncilSearch();
    ResetElectorateSearch();
  }
  
  /****************************************************************
    return the user selected search option  
  ****************************************************************/    
  function GetSearchOption()
  {
    var radOptionAddressSearch = document.getElementById(SEARCH_OPTION_BY_ADDRESS);
    var radOptionProjectSearch = document.getElementById(SEARCH_OPTION_BY_PROJECT);    
    var radOptionCouncilSearch = document.getElementById(SEARCH_OPTION_BY_COUNCIL);    
    var radOptionElectorate = document.getElementById(SEARCH_OPTION_BY_ELECTORATE);
        
    if (radOptionAddressSearch != null && radOptionAddressSearch.checked)
    { 
      return SEARCH_OPTION_BY_ADDRESS;  
    }

    if (radOptionProjectSearch != null && radOptionProjectSearch.checked)
    { 
      return SEARCH_OPTION_BY_PROJECT;  
    }
    
    if (radOptionCouncilSearch != null && radOptionCouncilSearch.checked)
    {    
      return SEARCH_OPTION_BY_COUNCIL;
    }

    if (SHOW_ELECTORATE_SEARCH && radOptionElectorate != null && radOptionElectorate.checked)
    {        
      return SEARCH_OPTION_BY_ELECTORATE;
    }
    
    return "";
  }
  
  /****************************************************************
    event handler for Text controls to turn off the water mark display  
  ****************************************************************/    
  function OnTextGetFocus(sender, initVal)
  {
    if (sender != null && sender.value == initVal)
    {
      sender.value = "";
      sender.style.color = COLOUR_DEFAULTTEXT;  
    }
  }
  
  /****************************************************************
    event handler for Text controls to turn on the water mark display  
  ****************************************************************/    
  function OnTextLostFocus(sender, initVal)
  {
    if (sender != null && (sender.value == null || sender.value == ""))
    {
      sender.style.color = COLOUR_WATERMARK;  
      sender.value = initVal;
    }
  }

  /****************************************************************
    event handler for combo controls to turn off the water mark display  
  ****************************************************************/    
  function OnComboGetFocus(sender)
  {
    if (sender != null)
    {
      sender.style.color = COLOUR_DEFAULTTEXT;
    }
  }
  
  /****************************************************************
    event handler for combo controls to turn on the water mark display  
  ****************************************************************/    
  function OnComboLostFocus(sender)
  {
    if (sender != null && sender.value == "")
    {
      sender.style.color = COLOUR_WATERMARK;
    }
  }
  
  /****************************************************************
    event handler for combo controls to show type-ahead behaviour  
  ****************************************************************/    
  function FilterComboEntries(sender)
  {
    if (window.event.ctrlKey) return; 
  
    g_typeAheadData.Reset(sender.id);
      
    switch (window.event.keyCode)
    {
      case 8: // BACKSPACE   
        if (g_typeAheadData.m_keyStrokes.length > 0)
        {
          g_typeAheadData.m_keyStrokes = g_typeAheadData.m_keyStrokes.substr(0, g_typeAheadData.m_keyStrokes.length-1);
        }
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        break;

      case 9: // TAB
        g_typeAheadData.m_keyStrokes = "";
        return;
                      
      case 10: // LF
      case 13: // CR  
        g_typeAheadData.m_keyStrokes = "";
        sender.fireEvent("onchange");
        return;
                 
      default:
        var c = '';
        if ((window.event.keyCode >= 48)&&(window.event.keyCode <= 57)) //Numbers 0-9
        {
          c = (window.event.keyCode-96).toString();

          g_typeAheadData.m_keyStrokes += c;
          window.event.cancelBubble = true;
          window.event.returnValue = false;
          break;
        }
        else if (window.event.keyCode >= 30)
        {
          c=String.fromCharCode(window.event.keyCode).toLowerCase();
          g_typeAheadData.m_keyStrokes += c;
          window.event.cancelBubble = true;
          window.event.returnValue = false;
          break;
        }
    }
    if (ComboPeekItem(g_typeAheadData) == false)
    {
      g_typeAheadData.m_keyStrokes = "";
    }
    else
    {
      sender.fireEvent("onchange");
    }
  }
  
  /****************************************************************
    global type-ahead data storage  
  ****************************************************************/    
  var g_typeAheadData = 
  {
    m_keyStrokes:"",
    m_comboId:"",
  
    Reset:function(id) 
    {
      if (this.m_comboId != id) 
      {
        this.m_comboId = id; 
        this.m_keyStrokes = "";
      }
    }
  };
  
  /****************************************************************
    select type-ahead item from the combo box  
  ****************************************************************/    
  function ComboPeekItem(typeAheadData)
  {
    var combo = document.getElementById(typeAheadData.m_comboId);
    combo.selectedIndex = -1;
    
    if (typeAheadData.m_keyStrokes.length > 0)
    {
      for (i = 0; i < combo.options.length; i++) 
      {
        if ((combo.options[i].text.length >= typeAheadData.m_keyStrokes.length) &&  (combo.options[i].text.substr(0, typeAheadData.m_keyStrokes.length).toLowerCase() == typeAheadData.m_keyStrokes))
        {
          combo.selectedIndex = i;
          return true;
        }
      }
    }
    return false;
  }  

  /****************************************************************
    cross browser getElementById  
  ****************************************************************/    
  function getElementById(objectId)
  {
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
      return document.getElementById(objectId);
    }
    else
    {
      return document[objectId];
    }
  }

  /****************************************************************
    search criteria data storage  
  ****************************************************************/    
  function LastSearchCriteria(searchOption, address, suburb, state, postcode, council, electorate, projectId, projectName) 
  { 
    this.m_searchOption = searchOption;
    this.m_address = address;
    this.m_suburb = suburb;
    this.m_state = state;
    this.m_postcode = postcode;
    this.m_council = council;
    this.m_electorate = electorate;
    this.m_projectId = projectId;
    this.m_projectName = projectName;    
    this.m_resultXml = null;
  }

  /****************************************************************
    event handler in response to the Search button clicked  
  ****************************************************************/          
  function PerformAreaSearch()
  {
  
    var streetValue = "";
    var suburbValue = "";
    var postcodeValue = "";
    var stateValue = "";
    var councilValue = "";
    var electorateValue = "";
    var projectId = "";
    var projectName = "";    

    var radOptionAddressSearch = document.getElementById(SEARCH_OPTION_BY_ADDRESS);
    var radOptionProjectSearch = document.getElementById(SEARCH_OPTION_BY_PROJECT);
    var radOptionCouncilSearch = document.getElementById(SEARCH_OPTION_BY_COUNCIL);    
    var radOptionElectorate = document.getElementById(SEARCH_OPTION_BY_ELECTORATE);
    
    if (radOptionAddressSearch != null && radOptionAddressSearch.checked)
    { 
      var txtStreet = document.getElementById(CTRL_STREET_ID);
      if (txtStreet != null)
      {
        streetValue = txtStreet.value;
        if (streetValue == INIT_STREET) streetValue = "";
      }
      
      var txtSuburb = document.getElementById(CTRL_SUBURB_ID);
      if (txtSuburb != null)
      {
        suburbValue = txtSuburb.value;  
        if (suburbValue == INIT_SUBURB) suburbValue = "";
      }
      
      var txtPostcode = document.getElementById(CTRL_POSTCODE_ID);
      if (txtPostcode != null)
      {
        postcodeValue = txtPostcode.value;
        if (postcodeValue == INIT_POSTCODE) postcodeValue = "";
      }

      var cboState = document.getElementById(CTRL_STATE_ID);
      if (cboState != null)
      {
        stateValue = cboState.value;
      }
    }
    
    if (SHOW_PROJECT_SEARCH && radOptionProjectSearch != null && radOptionProjectSearch.checked)
    { 
      var txtProjectId = document.getElementById(CTRL_PROJECT_ID);
      if (txtProjectId != null)
      {
        projectId = txtProjectId.value;
        if (projectId == INIT_PROJECT_ID) projectId = "";
      }
      
      var txtProjectName = document.getElementById(CTRL_PROJECT_NAME);
      if (txtProjectName != null)
      {
        projectName = txtProjectName.value;  
        if (projectName == INIT_PROJECT_NAME) projectName = "";
      }
    }
    
    if (SHOW_COUNCIL_SEARCH && radOptionCouncilSearch != null && radOptionCouncilSearch.checked)
    {    
      var cboCouncil = document.getElementById(CTRL_COUNCIL_ID);
      if (cboCouncil != null)
      {
        if (cboCouncil.selectedIndex != null && cboCouncil.selectedIndex > 0)
        {
          councilValue = cboCouncil.options[cboCouncil.selectedIndex].text;
        }
      }
    }

    if (SHOW_ELECTORATE_SEARCH && radOptionElectorate != null && radOptionElectorate.checked)
    {        
      var cboElectorate = document.getElementById(CTRL_ELECTORATE_ID);
      if (cboElectorate != null)
      {
        if (cboElectorate.selectedIndex != null && cboElectorate.selectedIndex > 0)
        {
          electorateValue = cboElectorate.options[cboElectorate.selectedIndex].text;
        }
      }
    }
    
    var msg = Validate(streetValue, suburbValue, stateValue, postcodeValue, councilValue, electorateValue, projectId, projectName);

    if (msg == null || msg == "")
    { 
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        try
        {
	        PerformAreaSearchWraper(flashComponent, streetValue, suburbValue, stateValue, postcodeValue, councilValue, electorateValue, projectId, projectName);
	        
	        if (suburbValue != null && suburbValue != "") ShowBoundary("Suburb");
	        if (postcodeValue != null && postcodeValue != "") ShowBoundary("Postcode");
	        if (councilValue != null && councilValue != "") ShowBoundary("Council");
	        if (electorateValue != null && electorateValue != "")	ShowBoundary("Electorate");        
	      }
	      catch (ex)
	      {
	        alert(ex);
	      }
	    }
	  }
	  else
	  {
	    alert(msg);
	  }
	  
    //window.event.cancelBubble = true;
    //window.event.returnValue = false;
	  //return false;	  
  }
  
  /****************************************************************
    turn on the map boundary on the map   
  ****************************************************************/          
  function ShowBoundary(boundary)
  {
    if (boundary == null || boundary == "") return;
    
    boundary = boundary.toLowerCase();
    
    var flashComponent = GetMapComponent();

    if (flashComponent != null)
    {
      var layerId = null;
      
      var i;
      var layerName;
      for (i = 0; i < arrLayerLookup.length; i++)
      {
        if (arrLayerLookup[i][1] == BOUNDARY_GIOSERVICE)
        {
          layerName = arrLayerLookup[i][0];
          if (layerName != null && layerName != "")
          {
            layerName = layerName.toLowerCase();
            if (layerName.indexOf(boundary) != -1)
            {
              layerId = arrLayerLookup[i][2];
              break;
            }
          } 
        }
      }
      
      if (layerId == null) return;
      
      ChangeLayerVisibilityWraper(flashComponent, BOUNDARY_GIOSERVICE, layerId, true); 
      
      var checkBox = FindCheckBox(document.getElementById(CTRL_DIV_BOUNDARY), boundary); 
      if (checkBox != null) checkBox.checked = true;        
    }    
  }
  
  /****************************************************************
    find checkbox control for the specified boundary   
  ****************************************************************/          
  function FindCheckBox(container, boundary)
  {
    if (container == null) return;
    if (boundary == null || boundary == "") return;
    
    boundary = boundary.toLowerCase();
    
    var checkBoxes = container.getElementsByTagName("INPUT");
    if (checkBoxes != null)
    {
      var i;
      var checkBoxText;
      for (i = 0; i < checkBoxes.length; i++)
      {
        if (checkBoxes[i].type == "checkbox")
        {
          checkBoxText = checkBoxes[i].value;
          if (checkBoxText != null && checkBoxText != "")
          {
            checkBoxText = checkBoxText.toLowerCase();
            if (checkBoxText.indexOf(boundary) != -1)
            {
              return checkBoxes[i];
            }
          }
        }
      }
    }
  }
    
  /****************************************************************
    validate search criteria before conducting the search   
  ****************************************************************/          
  function Validate(street, suburb, state, postcode, council, electorate, projectId, projectName)
  {  
    if (street == "" && suburb == "" && state == "" && postcode == "" && council == "" && electorate == "" && projectId == "" && projectName == "")
    {
      return "Please enter a search criterion.";
    }
    if (projectId != "" && projectName != "")
    {
      return "Please enter either a Project ID or a Project Name. PRAMS does not accept both values.";
    }
    return "";
  }
    
  /****************************************************************
    toggle map layer   
  ****************************************************************/          
  function ChangeLayerVisibilityWraper(flashComponent, serviceName, layerId, visible)
  {
    /* 
    alert(flashComponent.id);
    alert(serviceName);
    alert(layerId);
    alert(visible);
    */ 
    flashComponent.ChangeLayerVisibility(serviceName, layerId, visible);    
  } 
  
  /****************************************************************
    perform search   
  ****************************************************************/          
  function PerformAreaSearchWraper(flashComponent, streetValue, suburbValue, stateValue, postcodeValue, councilValue, electorateValue, projectIdValue, projectNameValue)
  {
    /*
    alert(flashComponent.id);
    alert(streetValue);
    alert(suburbValue);
    alert(stateValue);
    alert(postcodeValue);
    alert(councilValue);
    alert(electorateValue);
    alert(projectIdValue);
    alert(projectNameValue);
    */    

    if ((g_lastSearchCriteria != null) && (g_lastSearchCriteria.m_address == streetValue) && (g_lastSearchCriteria.m_suburb == suburbValue) && (g_lastSearchCriteria.m_state == stateValue) && (g_lastSearchCriteria.m_postcode == postcodeValue)
    && (g_lastSearchCriteria.m_projectId == projectIdValue) && (g_lastSearchCriteria.m_projectName == projectNameValue) 
    && (g_lastSearchCriteria.m_council == councilValue) && (g_lastSearchCriteria.m_electorate == electorateValue) 
    && (g_lastSearchCriteria.m_resultXml != null))
    {
      displayProjectDetails(g_lastSearchCriteria.m_resultXml);
      return;
    }
       
    try
    {
      ShowWaitScreen();
    
      g_lastSearchCriteria = new LastSearchCriteria(GetSearchOption(), streetValue, suburbValue, stateValue, postcodeValue, councilValue, electorateValue, projectIdValue, projectNameValue); 
      g_flashingProjectQueue = ",";

      ResetReportLink();  
      
      var councilArray;
      if (councilValue != null) councilArray = councilValue.split(",");
      
      var electorateArray;
      if (electorateValue != null) electorateArray = electorateValue.split(",");
      
      if (projectIdValue != null && projectIdValue != "")
      {
        g_projectIdServerSearch = true;
        flashComponent.FindProjectByID(projectIdValue);
      } 
      else if (projectNameValue != null && projectNameValue != "")
      {
        flashComponent.FindProjectByName(projectNameValue);
      }
      else 
      {
        flashComponent.PerformAreaSearch(streetValue, suburbValue, stateValue, postcodeValue, councilArray, electorateArray);
      }
    }
    catch (ex)
    {
      window.status = ex;
      g_lastSearchCriteria = null;
      
      HideWaitScreen();
    }    
  }

  /****************************************************************
    state varible to help correct turn on map layers when
    the user clicked on the project link sent by a friend    
  ****************************************************************/          
  var g_layerUpdateNeverCalled = true;
  var g_boundaryUpdateNeverCalled = true;
  
  /****************************************************************
    Flex Callback function
    update the layer display when the user uses the saved state to
    open the map    
  ****************************************************************/          
  function updateLayerVisibilityCheckbox(serviceName, layerId, visible)
  {  
    if (serviceName == LAYER_GIOSERVICE && g_layerUpdateNeverCalled)
    {
      g_layerUpdateNeverCalled = false;
      ResetCheckBoxes(SECTION_LAYERS, false);
    }
    if (serviceName == BOUNDARY_GIOSERVICE && g_boundaryUpdateNeverCalled)
    {
      g_boundaryUpdateNeverCalled = false;
      ResetCheckBoxes(CTRL_DIV_BOUNDARY, false);
    }
    
    var checkboxValue = null;
    if (arrLayerLookup != null)
    {
      for (var i = 0; i < arrLayerLookup.length; i++)
      {      
        if (arrLayerLookup[i][1] == serviceName && arrLayerLookup[i][2] == layerId.toString())
        {
          checkboxValue = arrLayerLookup[i][0];
          break;
        } 
      }      
    }
    
    var checkbox = null;
    if (checkboxValue != null)
    {
      if (serviceName == LAYER_GIOSERVICE)
      { 
        checkbox = GetCheckboxByValue(SECTION_LAYERS, checkboxValue);
        if (checkbox != null) 
        {
          checkbox.checked = visible;
          SynchroniseFromMember(checkbox); 
        }
      }
      if (serviceName == BOUNDARY_GIOSERVICE)
      { 
        checkbox = GetCheckboxByValue(CTRL_DIV_BOUNDARY, checkboxValue);
        if (checkbox != null) 
        {
          checkbox.checked = visible;
        }
      }     
    }
  }

  /****************************************************************
    find checkbox control by its value.
  ****************************************************************/          
  function GetCheckboxByValue(container, value)
  {  
    if (container == null) return;
    if (value == null) state = false;
    
    var containerCtrl = document.getElementById(container);
    if (containerCtrl == null) return;
    
    var checkboxes = containerCtrl.getElementsByTagName("input");
    
    if (checkboxes != null)
    {
      for (var i = 0; i < checkboxes.length; i++)
      {
        if (checkboxes[i] != null && checkboxes[i].type == "checkbox")
        {
          if (checkboxes[i].value == value) return checkboxes[i];
        }
      }
    }
    return null;
  }

  /****************************************************************
    set checkbox control to the specified state
  ****************************************************************/          
  function ResetCheckBoxes(container, state)
  {
    if (container == null) return;
    if (state == null) state = false;
    
    var containerCtrl = document.getElementById(container);
    if (containerCtrl == null) return;
    
    var checkboxes = containerCtrl.getElementsByTagName("input");
    
    if (checkboxes != null)
    {
      for (var i = 0; i < checkboxes.length; i++)
      {
        if (checkboxes[i] != null && checkboxes[i].type == "checkbox")
        {
          checkboxes[i].checked = state;
        }
      }
    }
  }
  
  /****************************************************************
    Flex Callback function
    display the search criteria in the search controls, which might
    be different from what the user was intially entered.
  ****************************************************************/          
  function displaySearchResults(addressLocation, suburb, state, postcode, council, federalElectorate)
  {  
    if (addressLocation == FLEX_NO_RESULT)
    {
      var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID); 
      if (divProjectDetails != null) divProjectDetails.innerHTML = "no Projects found that satisfy the search criteria.";
      
      HideWaitScreen();
      return;
    }
    
    var txtStreet = document.getElementById(CTRL_STREET_ID);
    if (txtStreet != null)
    {
      txtStreet.value = addressLocation;
      if (addressLocation == null || addressLocation == "")
      {
        txtStreet.value = INIT_STREET;
        txtStreet.style.color = COLOUR_WATERMARK;  
      }
      else
      {
        txtStreet.style.color = COLOUR_DEFAULTTEXT;  
      }
    }
    
    var txtSuburb = document.getElementById(CTRL_SUBURB_ID);
    if (txtSuburb != null)
    {
      txtSuburb.value = suburb;  
      if (suburb == null || suburb == "")
      {
        txtSuburb.value = INIT_SUBURB;
        txtSuburb.style.color = COLOUR_WATERMARK; 
      }
      else
      {
        txtSuburb.style.color = COLOUR_DEFAULTTEXT; 
      } 
    }
    
    var txtPostcode = document.getElementById(CTRL_POSTCODE_ID);
    if (txtPostcode != null)
    {
      txtPostcode.value = postcode;
      if (postcode == null || postcode == "")
      {
        txtPostcode.value = INIT_POSTCODE;
        txtPostcode.style.color = COLOUR_WATERMARK; 
      }
      else
      {
        txtPostcode.style.color = COLOUR_DEFAULTTEXT;
      }
    }

    var cboState = document.getElementById(CTRL_STATE_ID);
    if (cboState != null)
    {
      cboState.value = state;
      if (state == null || state == "")
      {
        cboState.style.color = COLOUR_WATERMARK;
      }
      else
      {
        cboState.style.color = COLOUR_DEFAULTTEXT;
      }
    }

    var txtProjectId = document.getElementById(CTRL_PROJECT_ID);
    if (txtProjectId != null)
    {
      txtProjectId.value = projectId;
      if (projectId == null || projectId == "")
      {
        txtProjectId.value = INIT_PROJECT_ID;
        txtProjectId.style.color = COLOUR_WATERMARK; 
      }
      else
      {
        txtProjectId.style.color = COLOUR_DEFAULTTEXT;
      }
    }
    var txtProjectName = document.getElementById(CTRL_PROJECT_NAME);
    if (txtProjectName != null)
    {
      txtProjectName.value = projectName;
      if (projectName == null || projectName == "")
      {
        txtProjectName.value = INIT_PROJECT_NAME;
        txtProjectName.style.color = COLOUR_WATERMARK; 
      }
      else
      {
        txtProjectName.style.color = COLOUR_DEFAULTTEXT;
      }
    }
    
    var cboCouncil = document.getElementById(CTRL_COUNCIL_ID);
    if (cboCouncil != null)
    {
      cboCouncil.value = GetCouncilIdByName(council);  
      if (council == null || council == "")
      {
        cboCouncil.style.color = COLOUR_WATERMARK;
      }
      else
      {
        cboCouncil.style.color = COLOUR_DEFAULTTEXT;
      }
    }
        
    var cboElectorate = document.getElementById(CTRL_ELECTORATE_ID);
    if (SHOW_ELECTORATE_SEARCH && cboElectorate != null)
    {
      cboElectorate.value = GetElectorateIdByName(federalElectorate); 
      if (federalElectorate == null || federalElectorate == "")
      {
        cboElectorate.style.color = COLOUR_WATERMARK;
      } 
      else
      {
        cboElectorate.style.color = COLOUR_DEFAULTTEXT;
      }
    }
  }
  
  /****************************************************************
    select the specified council in the council dropdown
  ****************************************************************/          
  function GetCouncilIdByName(councilName)
  {
    var cboCouncil = document.getElementById(CTRL_COUNCIL_ID);
    if (cboCouncil != null)
    {
      var i;
      for (i = 0; i < cboCouncil.options.length; i++)
      {
        if (cboCouncil.options[i].text == councilName) return cboCouncil.options[i].value;
      }
    }
    return "";
  }

  /****************************************************************
    select the specified electorate in the electorate dropdown
  ****************************************************************/          
  function GetElectorateIdByName(electorateName)
  {
    var cboElectorate = document.getElementById(CTRL_ELECTORATE_ID);
    if (SHOW_ELECTORATE_SEARCH && cboElectorate != null)
    {
      var i;
      for (i = 0; i < cboElectorate.options.length; i++)
      {
        if (cboElectorate.options[i].text == electorateName) return cboElectorate.options[i].value;
      }
    }
    return "";
  }
  
  /****************************************************************
    select the specified state in the state dropdown
  ****************************************************************/          
  function GetStateNameByAbbreviate(abbr)
  {
    if (abbr == null || abbr == "") return "";
    
    var cboState = document.getElementById(CTRL_STATE_ID);
    if (cboState != null)
    {
      var i;
      for (i = 0; i < cboState.options.length; i++)
      {
        if (cboState.options[i].value == abbr) return cboState.options[i].text;
      }
    }
    return "";    
  }
  
  /****************************************************************
    Flex Callback function
    display the project details
  ****************************************************************/          
  function displayProjectDetails(projects)
  {  
    if (g_lastSearchCriteria != null) g_lastSearchCriteria.m_resultXml = projects;
    
    if (projects == null || projects == "")
    {      
      var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID); 
      if (divProjectDetails != null) divProjectDetails.innerHTML = "no Projects found that satisfy the search criteria.";
    }
    else
    {
      g_xmlParseErrorCode = 0;
      var projectDetailsData = ToHTML(projects);
    
      var divProjectDetails = document.getElementById(CTRL_PROJECTDETAILS_ID); 

      if (g_xmlParseErrorCode == 0)
      {
        PrepareReportLink();
       
        if (divProjectDetails != null)
        {
          if (g_isIE)
          { 
            divProjectDetails.innerHTML = projectDetailsData;
            
            if (g_debug)
            {
              var debugArea = document.getElementById("debugArea");
              debugArea.value =  projectDetailsData;
            }           
          }
          else
          {
            var ci = divProjectDetails.childNodes.length - 1; 
            for (; ci >= 0; ci--)
            {
              divProjectDetails.removeChild(divProjectDetails.childNodes[ci]);
            }
            divProjectDetails.appendChild(projectDetailsData);
          }
          
          // if this is project id search, need to fix the flash display
          if (g_lastSearchCriteria.m_projectId != null && g_lastSearchCriteria.m_projectId != "" && HasProjectDetailsShown(g_lastSearchCriteria.m_projectId) && g_projectIdServerSearch)
          {
            var layerid = FindLayerIdByProjectId(g_lastSearchCriteria.m_projectId);
            AddToQueue(g_lastSearchCriteria.m_projectId, layerid);            
          }
          AdjustDetailExpandStates();           
        }
      }
      else
      {      
        if (divProjectDetails != null) divProjectDetails.innerHTML = "Invalid XML format (" + g_xmlParseErrorCode + ").";
        g_xmlParseErrorCode = 0;
      }       
    }
    
    g_projectIdServerSearch = false;
    HideWaitScreen();
  }

  /****************************************************************
    Test if the project details is shown in the details area
  ****************************************************************/          
  function HasProjectDetailsShown(projectId)
  {
    var ctrlId = "divProjectHeader_" + projectId;
    
    var projectHeader = document.getElementById(ctrlId);
    return projectHeader != null;    
  }
     
  /****************************************************************
    Get layer id from the project displayed in the details area
  ****************************************************************/          
  function FindLayerIdByProjectId(projectId)
  { 
    var layerId = -1;

    var ctrlId = "divProjectHeader_" + projectId;
    var projectHeader = document.getElementById(ctrlId);

    if (projectHeader != null)
    {
      var container = projectHeader;

      while (true)
      {
        container = container.parentNode;
        if (container == null || container.tagName == "div" || container.tagName == "DIV") break; 
      }
      
      var idString = "";
      if (container != null)
      {
        var id = container.id;

        for (var i = id.length - 1; i >= 0; i--)
        {
          if (id[i] == "_") break;
          idString = id[i] + idString;
        }
        if (idString != "") layerId = idString;
      }
    }
    return layerId;
  }
  
  /****************************************************************
    Turn project flash on 
  ****************************************************************/          
  function TurnOnFlashProject(projectId)
  {
    var ctrlId = "aProjectFlashToggle_" + projectId;
    
    var projectFlash = document.getElementById(ctrlId);
    if (projectFlash != null)
    {
      img = GetImmediateChildrenByTagName(projectFlash, "IMG")[0];

      if (img != null)
      {
        img.src = IMG_FLASH_ON;
      } 
    }
  }

  function AdjustDetailExpandStates()
  {
    try
    {
      var ids = g_flashingProjectQueue.split(",");
      if (ids != null)
      {
        var i;
        var parts;  
        var groupExpanderCtrlId = "";
        var groupExpander;        
        var projectExpanderCtrlId = "";
        var projectExpander;        
        
        for (i = 0; i < ids.length; i++)
        {
          if (ids[i] != null && ids[i] != "")
          {
            parts = ids[i].split(";");
            
            if (parts.length != 2) continue;
            
            // expand group if it is not open
            if (groupExpanderCtrlId != "aProjectGroupExpander_" + parts[1]) // parts[1] is layer id
            {
              groupExpanderCtrlId = "aProjectGroupExpander_" + parts[1];
              groupExpander = document.getElementById(groupExpanderCtrlId);

              if (groupExpander != null) ExpandBlock(groupExpander.parentNode.parentNode.parentNode, groupExpander) 
            }  
            // expand the project
            //projectExpanderCtrlId = "aProjectExpander_" + parts[0]; // parts[0] is project id
            //projectExpander = document.getElementById(projectExpanderCtrlId);        
            
            //if (projectExpander != null) ExpandBlock(projectExpander.parentNode.parentNode, projectExpander) 
                        
            TurnOnFlashProject(parts[0]);
          }
        }
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }    
  
  }
  
  
  /****************************************************************
    form the project search criteria string to be displayed along
    with the project details
  ****************************************************************/          
  function GetSearchCriteriaDisplayString()
  {
    var s = "";
    
    if (g_lastSearchCriteria != null)
    {
      s = "";
      if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ADDRESS)
      {
        if (g_lastSearchCriteria.m_address != null && g_lastSearchCriteria.m_address != "")
        {
          s += "street address: ";
          s += g_lastSearchCriteria.m_address;
          s += " ";
        }
        if (g_lastSearchCriteria.m_suburb != null && g_lastSearchCriteria.m_suburb != "")
        {
          s += "suburb: ";
          s += g_lastSearchCriteria.m_suburb;
          s += " ";
        }
        if (g_lastSearchCriteria.m_state != null && g_lastSearchCriteria.m_state != "")
        {
          s += "state: ";
          s += GetStateNameByAbbreviate(g_lastSearchCriteria.m_state);
          s += " ";
        }
        if (g_lastSearchCriteria.m_postcode != null && g_lastSearchCriteria.m_postcode != "")
        {
          s += "postcode: ";
          s += g_lastSearchCriteria.m_postcode;
          s += " ";
        }           
      }
      else if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_PROJECT)
      {
        if (g_lastSearchCriteria.m_projectId != null && g_lastSearchCriteria.m_projectId != "")
        {
          s += "project ID: ";
          s += g_lastSearchCriteria.m_projectId;
          s += " ";
        }
        if (g_lastSearchCriteria.m_projectName != null && g_lastSearchCriteria.m_projectName != "")
        {
          s += "project name: ";
          s += g_lastSearchCriteria.m_projectName;
          s += " ";
        }
      }      
      else if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_COUNCIL)
      {
        s += "council: ";
        s += g_lastSearchCriteria.m_council;
        s += " ";
      }
      else if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ELECTORATE)
      {
        s += "electorate: ";
        s += g_lastSearchCriteria.m_electorate;
        s += " ";
      }
    }
    
    return s;
  }

  /****************************************************************
    Reset/hide the report links 
  ****************************************************************/          
  function ResetReportLink()
  {
    var divReport = document.getElementById(SECTION_REPORT);
    if (divReport != null) divReport.style.display = "none";
  }
  
  /****************************************************************
    Show the report links 
  ****************************************************************/          
  function PrepareReportLink()
  {
    if (g_lastSearchCriteria == null) return;
        
    var divReport = document.getElementById(SECTION_REPORT);

    if (divReport != null && (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_COUNCIL || g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ELECTORATE))
    {
      var lblDownloadLink = document.getElementById(CTRL_DOWNLOAD_REPORT_LABEL);
      var lblViewLink = document.getElementById(CTRL_VIEW_REPORT_LABEL);
                  
      if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_COUNCIL)
      {      
        if (lblDownloadLink != null) lblDownloadLink.innerHTML = "Download Council Projects";
        if (lblViewLink != null) lblViewLink.innerHTML = "View Council Report";
      }
      else if (SHOW_ELECTORATE_SEARCH && g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ELECTORATE)
      {
        if (lblDownloadLink != null) lblDownloadLink.innerHTML = "Download Electorate Projects";
        if (lblViewLink != null) lblViewLink.innerHTML = "View Electorate Report";
      }

      divReport.style.display = "block";      
    }      
  }

  /****************************************************************
    Convert project details from xml to html 
  ****************************************************************/          
  function ToHTML(projects)
  { 
    try
    {
      if (g_xsl == null) g_xsl = LoadXMLDoc(PROJECT_XSLT_FILE_URL); 
       
      if (window.ActiveXObject) // ie
      {                  
        var xsltDocument = new ActiveXObject("MSXML2.FreeThreadedDOMDocument" );
        xsltDocument.async = false;
        xsltDocument.loadXML(g_xsl.xml);  // get xsl as xml string

        var xmlDocument = new ActiveXObject("MSXML2.FreeThreadedDOMDocument" );
        xmlDocument.async = false;
        
        projects = projects.replace(/&(?!amp;)/g, "&amp;");

        xmlDocument.loadXML(projects);
        
        if (xmlDocument.parseError != 0)
        {
          g_xmlParseErrorCode = xmlDocument.parseError
        }
        
        var transformedHtml;
        
        var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
        
        xslTemplate.stylesheet = xsltDocument;
        
        var xslProc = xslTemplate.createProcessor();
        
        xslProc.input = xmlDocument;
        
        //xslProc.resolveExternals = true;
        //xslProc.setProperty("AllowDocumentFunction", true);

        xslProc.addParameter("search-criteria", GetSearchCriteriaDisplayString());
        xslProc.addParameter("layer-ids", GetLayerIDsString());
        xslProc.addParameter("mapLayerAbsUrl", PROJECT_LAYERXML_FILE_ABS_URL);
         
        xslProc.transform();
         
        transformedHtml = xslProc.output;

        return transformedHtml;
      }
      else if (document.implementation && document.implementation.createDocument)
      {
        // code for Mozilla, Firefox, opera and etc 
        projects = projects.replace(/&(?!amp;)/g, "&amp;");
                
        var xmlDocument = new DOMParser().parseFromString(projects, 'application/xml');
        if (xmlDocument.documentElement.namespaceURI == 'http://www.mozilla.org/newlayout/xml/parsererror.xml' && xmlDocument.documentElement.tagName == 'parsererror')
        {
          g_xmlParseErrorCode = xmlDocument.documentElement.firstChild.nodeValue;
        }
                
        //var xmlDocumentReverse = new XMLSerializer().serializeToString(xmlDocument);
        //alert(xmlDocumentReverse);
        
        var xsltProcessor= new XSLTProcessor();
        xsltProcessor.importStylesheet(g_xsl);

        //xsltProcessor.resolveExternals = true;
        //xsltProcessor.setProperty("AllowDocumentFunction", true);
               
        xsltProcessor.setParameter(null, "search-criteria", GetSearchCriteriaDisplayString());
        xsltProcessor.setParameter(null, "layer-ids", GetLayerIDsString());
        xsltProcessor.setParameter(null, "mapLayerAbsUrl", PROJECT_LAYERXML_FILE_ABS_URL);

        resultDocument = xsltProcessor.transformToFragment(xmlDocument,document);
        return resultDocument;        
      }
    }
    catch (ex)
    {
      window.status = ex;
      document.body.style.cursor = 'default';    
    }    
  }
  
  /****************************************************************
    get the xml document from the server 
  ****************************************************************/          
  function LoadXMLDoc(dname)
  {
    var xhttp;
    
    if (window.XMLHttpRequest)
    {
      xhttp = new XMLHttpRequest();
    }
    else
    {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xhttp.open("GET",dname,false);
    xhttp.send("");
    
    var xsl = xhttp.responseXML;
     
    return xsl ;
  }
  
  /****************************************************************
    toggle the html block display 
  ****************************************************************/          
  function ExpandBlock(divContainer, anchorContainer)
  { 
    var imgControl;
        
    if (anchorContainer != null)
    {
      imgControl = GetImmediateChildrenByTagName(anchorContainer, "IMG")[0];
    }
    if (divContainer != null && divContainer.tagName == "DIV")
    {
      var toExpand = imgControl.src.indexOf(IMG_PLUS) != -1;   // divContainer.childNodes[1].style.display == "display" || divContainer.childNodes[1].style.display == "";
            
      if (toExpand)
      {
        if (imgControl != null && imgControl.tagName == "IMG")
        {
          imgControl.src = IMG_MINUS;
        }
        GetImmediateChildrenByTagName(divContainer, "DIV")[1].style.display="";
      }
      else
      {
        if (imgControl != null && imgControl.tagName == "IMG")
        {
          imgControl.src = IMG_PLUS;
        }
        GetImmediateChildrenByTagName(divContainer, "DIV")[1].style.display="none";
      }
    }
  }
  
  /****************************************************************
    get the immediate child controls of the certain type from the container 
  ****************************************************************/          
  function GetImmediateChildrenByTagName(node, tagName)
  {  
    if (node == null) return null;
    
    var childArray = new Array();
    var j = -1;
    
    for (var i = 0; i < node.childNodes.length; i++)
    {
      if (node.childNodes[i].tagName == tagName)
      {
        childArray[++j] = node.childNodes[i]; 
      }
    }
    if (j == -1) return null;
    return childArray; 
  }
  
  /****************************************************************
    form the correct query string and download the excel report 
  ****************************************************************/            
  function DownloadExcelReport()
  {
    if (g_lastSearchCriteria != null)
    {
      var progIds = GetAllSelectedProgramIds(LAYER_GIOSERVICE);

      if (progIds == null || progIds == "")
      {
        alert("Please select at least one category in order to generate the report.");
        return;
      }
            
      var url = "";
      if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_COUNCIL)
      {
        var councilId = GetCouncilIdByName(g_lastSearchCriteria.m_council); 
        if (REPORT_DOWNLOAD_COUNCIL_URL_TEMPLATE != null && REPORT_DOWNLOAD_COUNCIL_URL_TEMPLATE != "")
        {
          url = REPORT_DOWNLOAD_COUNCIL_URL_TEMPLATE.replace(REPORT_PROGRAMIDS, progIds);
          url = url.replace(REPORT_COUNCILIDS, councilId);
        }
      }
      else if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ELECTORATE)
      {
        var electorateId = GetElectorateIdByName(g_lastSearchCriteria.m_electorate); 
        if (REPORT_DOWNLOAD_ELECTORATE_URL_TEMPLATE != null && REPORT_DOWNLOAD_ELECTORATE_URL_TEMPLATE != "")
        {
          url = REPORT_DOWNLOAD_ELECTORATE_URL_TEMPLATE.replace(REPORT_PROGRAMIDS, progIds)
          url = url.replace(REPORT_ELECTORATEIDS, electorateId);
        }
      }
      
      if (url != "") window.open(url, "_blank");
    }    
  }

  /****************************************************************
    form the correct query string and open the report link 
  ****************************************************************/            
  function ViewReport()
  {
    if (g_lastSearchCriteria != null)
    {
      var progIds = GetAllSelectedProgramIds(LAYER_GIOSERVICE);
      
      if (progIds == null || progIds == "")
      {
        alert("Please select at least one category in order to generate the report.");
        return;
      }
      
      var url = "";
      if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_COUNCIL)
      {
        var councilId = GetCouncilIdByName(g_lastSearchCriteria.m_council); 
        if (REPORT_VIEW_COUNCIL_URL_TEMPLATE != null && REPORT_VIEW_COUNCIL_URL_TEMPLATE != "")
        {
          url = REPORT_VIEW_COUNCIL_URL_TEMPLATE.replace(REPORT_PROGRAMIDS, progIds);
          url = url.replace(REPORT_COUNCILIDS, councilId);
        }
      }
      else if (g_lastSearchCriteria.m_searchOption == SEARCH_OPTION_BY_ELECTORATE)
      {
        var electorateId = GetElectorateIdByName(g_lastSearchCriteria.m_electorate); 
        if (REPORT_VIEW_ELECTORATE_URL_TEMPLATE != null && REPORT_VIEW_ELECTORATE_URL_TEMPLATE != "")
        {
          url = REPORT_VIEW_ELECTORATE_URL_TEMPLATE.replace(REPORT_PROGRAMIDS, progIds)
          url = url.replace(REPORT_ELECTORATEIDS, electorateId);
        }
      }
      if (url != "") window.open(url, "_blank");
    }      
  }
  
  /****************************************************************
    form a comma delimited string of map layer ids 
  ****************************************************************/            
  function GetAllSelectedProgramIds(service)
  {   
    var progIds = "";
    var layerProgId;
  
    var divLayers = null;
    
    if (service == LAYER_GIOSERVICE) divLayers = document.getElementById(SECTION_LAYERS);
    if (service == BOUNDARY_GIOSERVICE) divLayers = document.getElementById(CTRL_DIV_BOUNDARY);
    
    if (divLayers != null)
    {
      var checkBoxes = divLayers.getElementsByTagName("INPUT");
      if (checkBoxes != null)
      {
        var i;
        for (i = 0; i < checkBoxes.length; i++)
        {
          if (checkBoxes[i].checked)
          {
            layerProgId = GetLayerProgramId(checkBoxes[i].value, service);

            if (progIds == "")
            {
              if (layerProgId != "") progIds = layerProgId;
            }
            else
            {
              if (layerProgId != "") progIds += ("," + layerProgId);
            }
          }
        }
      }      
    }
    return progIds;
  }
  
  /****************************************************************
    get a map layer id based on layer name 
  ****************************************************************/            
  function GetLayerProgramId(layerName, service)
  {
    var progId = "";
    try
    {
      var i;
      for (i = 0; i < arrLayerLookup.length; i++)
      {
        if (arrLayerLookup[i][0] == layerName && arrLayerLookup[i][1] == service)
        {        
          if (progId == "")
          {
            progId = arrLayerLookup[i][2];
          }
          else
          {
            progId += ("," + arrLayerLookup[i][2]);
          }
        }
      }
    }
    catch (ex)
    {
    }
    return progId;
  }
  
  /****************************************************************
    toggle the a map layer 
  ****************************************************************/            
  function ChangeSingleLayerVisibility(checkBox, service, layerId)
  {  
    var flashComponent = GetMapComponent();

    if (flashComponent != null)
    {
      ChangeLayerVisibilityWraper(flashComponent, service, layerId, checkBox.checked);
          
			if (g_lastSearchCriteria != null)
			{
				PerformAreaSearchWraper(flashComponent, g_lastSearchCriteria.m_address, g_lastSearchCriteria.m_suburb, g_lastSearchCriteria.m_state, g_lastSearchCriteria.m_postcode, g_lastSearchCriteria.m_council, g_lastSearchCriteria.m_electorate, g_lastSearchCriteria.m_projectId, g_lastSearchCriteria.m_projectName);
			}
    }
    SynchroniseFromMember(checkBox);
    AdjustProjectFlashQueue(flashComponent, layerId);
  }
  
  /****************************************************************
    return the flex map component 
  ****************************************************************/            
  function GetMapComponent()
  {
    var flashComponent;
    
    if (g_isIE)
    {
        flashComponent = document.getElementById(CTRL_MAP_ID); //window[CTRL_MAP_ID];
    }
    else
    {
      flashComponent = document.getElementById(CTRL_MAP_ID);      
    }
    
    return flashComponent;
  }
  
  /****************************************************************
    synchronise the checkbox display based on the group checkbox status 
  ****************************************************************/            
  function SynchroniseFromGroup(sender)
  {
    if (sender != null)
    {
      var i = 0;
      var divRow = sender;
      while (i < 3)
      {
        divRow = divRow.parentNode;
        if (divRow == null) return;
        
        i++
      }
      
      var inputs = divRow.getElementsByTagName("INPUT")

      if (inputs == null) return;
      
      var checkBoxState;
      for ( i = 0; i < inputs.length; i++)
      {
        if (inputs[i].type == "checkbox")
        {
          checkBoxState = inputs[i].checked;
          inputs[i].checked = sender.checked;          
        } 
      }
    }
  }
  
  /****************************************************************
    synchronise the group checkbox display based on the member checkbox status 
  ****************************************************************/            
  function SynchroniseFromMember(sender)
  {
    if (sender != null)
    {
      var i = 0;
      var divBody = sender;
      while (i < 3)
      {
        divBody = divBody.parentNode;
        if (divBody == null) return;
        
        i++
      }
      
      var inputs = divBody.getElementsByTagName("INPUT")
      var groupStatus = true;
    
      if (inputs == null) return;
      
      for ( i = 0; i < inputs.length; i++)
      {
        if (inputs[i].type == "checkbox")
        {
          groupStatus = groupStatus && inputs[i].checked;
        } 
      }

      var divGroupRow = divBody.parentNode;
      if (divGroupRow == null) return;
      
      var divGroupHeader = divGroupRow.children[0];
      inputs = divGroupHeader.getElementsByTagName("INPUT")
      
      if (inputs == null || inputs.length == 0) return;
      
      for ( i = 0; i < inputs.length; i++)
      {
        if (inputs[i].type == "checkbox")
        {
          inputs[i].checked = groupStatus;
          return;
        } 
      }
    }
  }
  
  /****************************************************************
    get a comma delimited layer id string 
  ****************************************************************/            
  function GetLayerIDsString()
  { 
    var s = GetAllSelectedProgramIds(LAYER_GIOSERVICE);
    if (s == null || s == "") 
    {
      return ",";
    }
    else
    {
      return "," + s + ",";
    }
  }

  /****************************************************************
    toggle project flash display 
  ****************************************************************/            
  function ToggleProjectFlash(sender, projectId, layerId)
  {
    if (projectId == null) return;
    
    var img;
    if (QueueContains(projectId, layerId))
    {
      if (sender != null)
      {      
        img = GetImmediateChildrenByTagName(sender, "IMG")[0];

        if (img != null)
        {
          img.src = IMG_FLASH_OFF;
        } 
      }
      
      DeflashProjectWrapper(projectId, layerId);
    }
    else
    {
      if (sender != null)
      {
        img = GetImmediateChildrenByTagName(sender, "IMG")[0];

        if (img != null)
        {
          img.src = IMG_FLASH_ON;
        } 
      }
      FlashProjectWrapper(projectId, layerId); 
    }
  }

  /****************************************************************
    turn off projects flash display 
  ****************************************************************/            
  function DeflashAllWrapper()
  {   
    try
    {
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        var ids = g_flashingProjectQueue.split(",");
        if (ids != null)
        {
          var i;
          var parts;          
          for (i = 0; i < ids.length; i++)
          {
            if (ids[i] != null && ids[i] != "")
            {
              parts = ids[i].split(";");
              flashComponent.RemoveProjectFlash(parts[0]);
            }
          }
        }
      }
      g_flashingProjectQueue = ",";
      
      // change the icons back
      var displayArea = document.getElementById(CTRL_PROJECTDETAILS_ID);
      if (displayArea != null)
      {
        var flashIcons = displayArea.getElementsByTagName("IMG");
        if (flashIcons != null)
        {
          var i;
          var src;
          var onImg = IMG_FLASH_ON.toLowerCase();
          
          for (i = 0; i < flashIcons.length; i++)
          {
            if (flashIcons[i].src != null && flashIcons[i].src != "")
            { 
              src = flashIcons[i].src.toLowerCase();
              if (src.indexOf(onImg) != -1)
              {
                flashIcons[i].src = IMG_FLASH_OFF;
              }
            }
          }
        } 
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }    
  }

  /****************************************************************
    zoom out to the recordset view  
  ****************************************************************/            
  function ZoomOutWrapper()
  {   
    try
    {
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        flashComponent.ZoomToProjectResultsExtent();
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }      
  }

  /****************************************************************
    zoom in to the project view  
  ****************************************************************/            
  function ZoomInToProjectWrapper(projectId)
  {
    /*
    alert(projectId == null ? "NULL" : projectId);
    */

    if (projectId == null) return;
   
    try
    {
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        flashComponent.ZoomToProject(projectId);
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }      
  }
  
  /****************************************************************
    turn on the flash of the project point  
  ****************************************************************/            
  function FlashProjectWrapper(projectId, layerId)
  {
    /*
    alert(projectId == null ? "NULL" : projectId);
    */
    if (projectId == null) return;
   
    try
    {
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        AddToQueue(projectId, layerId);
        flashComponent.AddProjectFlash(projectId);
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }    
  }
  
  /****************************************************************
    turn off the flash of the project point  
  ****************************************************************/            
  function DeflashProjectWrapper(projectId, layerId)
  {
    /*
    alert(projectId == null ? "NULL" : projectId);
    */

    if (projectId == null) return;
   
    try
    {
      var flashComponent = GetMapComponent();
      if (flashComponent != null)
      {
        flashComponent.RemoveProjectFlash(projectId);
        RemoveFromQueue(projectId, layerId);
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }    
  }
  
  /****************************************************************
    add the project to the queue (for flash display) 
  ****************************************************************/              
  function AddToQueue(projectId, layerId)
  {
    if (projectId == null) return;
    
    if (g_flashingProjectQueue.indexOf("," + projectId + ";" + layerId + ",") > 0) return;
    
    g_flashingProjectQueue += ("," + projectId + ";" + layerId + ",");
  }
  
  /****************************************************************
    remove the project from the queue (for flash display)  
  ****************************************************************/              
  function RemoveFromQueue(projectId, layerId)
  {
    if (projectId == null) return;

    if ( g_flashingProjectQueue.indexOf("," + projectId + ";" + layerId + ",") > 0)
    {
      g_flashingProjectQueue = g_flashingProjectQueue.replace("," + projectId + ";" + layerId + ",", ",");
    }    
  }
  
  /****************************************************************
    test if the project is contained within the queue  
  ****************************************************************/              
  function QueueContains(projectId, layerId)
  {
    if (projectId == null) return false;
    return (g_flashingProjectQueue.indexOf("," + projectId + ";" + layerId + ",") > 0);    
  }
    
  /****************************************************************
    adjust the project flash display after the map layer selection was changed  
  ****************************************************************/              
  function AdjustProjectFlashQueue(flashComponent, layerId)
  {
    try
    {
      if (flashComponent != null)
      {
        var layerIds = GetLayerIDsString();

        var ids = g_flashingProjectQueue.split(",");
        if (ids != null)
        {
          var i;
          var parts;          
          for (i = 0; i < ids.length; i++)
          {
            if (ids[i] != null && ids[i] != "")
            {
              parts = ids[i].split(";");
              
              if (parts.length != 2) continue;
              
              if (layerIds.indexOf("," + parts[1] + ",") == -1)
              {
                flashComponent.RemoveProjectFlash(parts[0]);
                RemoveFromQueue(parts[0], parts[1]);
              }
            }
          }
        }
      }
    }    
    catch (ex)
    {
      window.status = ex;
    }    
  }
  
  /****************************************************************
    show the translucent wait screen 
  ****************************************************************/              
		function ShowWaitScreen()
		{	
      var w;
      var h;
      var hourglass;
      var map;
                  
      var waitScreen = document.getElementById(CTRL_DIV_WAITSCREEN); 

      if (waitScreen != null)
      {
        waitScreen.style.pixelTop = document.body.scrollTop;
        waitScreen.style.pixelLeft = document.body.scrollLeft;
        waitScreen.style.top = "0px";
        waitScreen.style.left = "0px";

		    waitScreen.style.pixelWidth = document.body.scrollWidth;
		    waitScreen.style.pixelHeight = document.body.scrollHeight;
        waitScreen.style.width = "100%";
        waitScreen.style.height = "100%";

  		  waitScreen.style.visibility="visible";

        hourglass = document.getElementById(CTRL_IMG_HOURGLASS);
        if (hourglass != null)
        {

          map = document.getElementById(CTRL_DIV_MAP_ID);
          
          if (map != null)
          {          
            w = (map.offsetLeft + map.offsetWidth - 54)/2
            h = (document.body.scrollHeight - 55)/2
          } 
          else
          {
            w = (document.body.scrollWidth - 54)/2
            h = (document.body.scrollHeight - 55)/2
          }
          hourglass.style.left = (w > 0 ? w : 0) + "px";
          hourglass.style.top = (h > 0 ? h : 0) + "px";   
        }
      }   
		}	

  /****************************************************************
    hide the translucent wait screen 
  ****************************************************************/              
		function HideWaitScreen()
		{
      var waitScreen = document.getElementById(CTRL_DIV_WAITSCREEN); 

      if (waitScreen != null)
      {
		    waitScreen.style.visibility="hidden";
		  }
		}

  /****************************************************************
    open the email client and prepare the email message for sending out the project link 
  ****************************************************************/              
    function EmailProject(projectId, projectDescription)
    {
      var mailToUrl = "mailto:<recipient>?subject=NBII PROJECT LINK - ";
      mailToUrl += projectDescription;
      mailToUrl += "&body=Please click the following link to display the project [ ";
      mailToUrl += projectDescription;
      mailToUrl += "] in NBII%0D    ";      
      mailToUrl = mailToUrl.replace(" ", "%20");
      
      //var completeUrl = window.location.href;
      var completeUrl = URL_IFRAME_CONTAINER;

      //make it work with iframe container      
      if (completeUrl == null || completeUrl == "")
      {
        var w = window;
        while ( w.parent != null && w.parent != w)
        {
          w = w.parent;
        }      
        completeUrl = w.location.href;
      }
      
      completeUrl = completeUrl.replace(" ", "%2520");
      var i = completeUrl.indexOf("?");
      if (i == -1)
      { 
        mailToUrl += completeUrl;
      }
      else
      {
        mailToUrl += completeUrl.substr(0, i);
      }
               
      mailToUrl += "?"
      mailToUrl += QUERYSTRING_PROJECTID;
      mailToUrl += "=";            
      mailToUrl += projectId;
            
      mailToUrl += "%26";
      mailToUrl += QUERYSTRING_LAYERS;
      mailToUrl += "=";      
      mailToUrl += GetAllSelectedProgramIds(LAYER_GIOSERVICE);

      mailToUrl += "%26";
      mailToUrl += QUERYSTRING_BOUNDARIES;
      mailToUrl += "=";      
      mailToUrl += GetAllSelectedProgramIds(BOUNDARY_GIOSERVICE)

      window.location = mailToUrl;
    }
  
  

