var searchDataFile = '/content/common/corpinfo/careers/routes-into-t-mobile/call-centre/where-could-i-work/cs_search.xml';
var xmlRoot;
var xmlDoc;

var ROLES     = 1;
var LOCATIONS = 2;

var _location_id;
var _area_id;
var _role_id;

function debug(message) {
  debugOutput = document.getElementById('debugOutput');
  debugParagraph = document.createElement('p');
  debugParagraph.appendChild(document.createTextNode(message));
  debugOutput.appendChild(debugParagraph);
}

function outputRoleSection (areaId, showLocations, locationId, showAlternatives, showAlternativeLocations) {
  _location_id = -1;
  _area_id = -1;

  areaId = areaId || -1;
  locationId = locationId || -1;

  _area_id = areaId;
  _location_id = locationId;

  clearOutputNode(true);

  if (_location_id != -1) {
    moveFlag(fetchXmlLocationNode(_location_id));
  }

  showAlternatives = showAlternatives || false;

  showLocations = showLocations || false;

  linkEvent = function () { outputRoleSection(this.id); return false; };

  xmlLocations = fetchLocations();


  if (showLocations) {
    found = false;
    for (var i = 0; i < xmlLocations.length; i++) {
      xmlAreas = fetchAreas(xmlLocations[i].getAttribute('id'));
      for (var j = 0; j < xmlAreas.length; j++)  {
        if (!found && xmlAreas[j].getAttribute('id') == _area_id) {
          found = true;
          myXmlArea = xmlAreas[j];
          areaLozenge = outputAreaLozenge(myXmlArea, true);
          if (showAlternatives) {
            outputOtherAreas(_area_id, ROLES);
          }
          areaLozenge.onclick = linkEvent;
          areaLozenge.id = myXmlArea.getAttribute('id');
          outputAreaDescription(myXmlArea, !showLocations);
          if (_location_id != -1) {
            locationsLozenge = outputLocationLozenge(fetchXmlLocationNode(_location_id), true, ROLES);
          } else {
            locationsLozenge = outputLozenge("Locations");
          }
          if (showAlternativeLocations) {
            outputOtherLocations(_location_id, ROLES);
          }
          locationsLozenge.onclick = function() { outputRoleSection(_area_id, true); return false; };
        }
      }
    }

    if (_location_id != -1) {
      for (var i = 0; i < xmlLocations.length; i++) {
        if (xmlLocations[i].getAttribute('id') == _location_id) {
          outputLocationDescription(xmlLocations[i], -1, true, true, false);
        }
      }
      levelsLozenge = outputLozenge("Level", true);
      xmlRoles =  fetchRoles(_location_id, _area_id);
      if (xmlRoles.length > 0) {
        for (var i = 0; i < xmlRoles.length; i++) {
          outputRole(xmlRoles[i]);
        }
      } else {
        outputSorry();
      }
    } else {
      for (var i = 0; i < xmlLocations.length; i++) {
        xmlAreas = fetchAreas(xmlLocations[i].getAttribute('id'));
        for (var j = 0; j < xmlAreas.length; j++)  {
          if (xmlAreas[j].getAttribute('id') == _area_id) {
            outputLocationDescription(xmlLocations[i], -1, true, true, true);
            break;
          }
        }
      }
    }
  } else {
    areasAlreadyOutput = new Array();
    for (var i = 0; i < xmlLocations.length; i++) {
      xmlAreas = fetchAreas(xmlLocations[i].getAttribute('id'));
      for (var j = 0; j < xmlAreas.length; j++)  {
        // Check not output already
        found = false;
        for (var k = 0; k < areasAlreadyOutput.length; k++) {
          if (xmlAreas[j].getAttribute('id') == areasAlreadyOutput[k]) {
            found = true;
            break;
          }
        }
        if (!found) {
          areaLozenge = outputAreaLozenge(xmlAreas[j]);
          areaLozenge.onclick = linkEvent;
          areaLozenge.id = xmlAreas[j].getAttribute('id');
          if (areaId == xmlAreas[j].getAttribute('id')) {
            outputAreaDescription(xmlAreas[j], !showLocations);
          }
          areasAlreadyOutput[areasAlreadyOutput.length] = xmlAreas[j].getAttribute('id');
        }
      }
    }
  }
}

// Output state functions
function outputLocations(locationId, showAreas, areaId, showRoles, showAlternativeLocations, showAlternativeAreas) {

  // Reset location and area values
  _location_id = -1;
  _area_id = -1;
  // Set the default values for parameters
  locationId = locationId || -1;
  areaId = areaId || -1;
  showAreas = showAreas || false;
  showRoles = showRoles || false;
  showAlternativeLocations = showAlternativeLocations || false;
  showAlternativeAreas = showAlternativeAreas || false;

  dieNow = false;

  clearOutputNode(false);

  locations = fetchLocations();
  linkEvent = function() {outputLocations(this.id); return false; };

  // If location id supplied, save the current location choice
  if (locationId != -1) {
    _location_id = parseInt(locationId);
    moveFlag(locations[locationId - 1]);
  }

  if (areaId != -1) {
    _area_id = parseInt(areaId);
  }

  if (showAreas) {
    lozenge = outputLocationLozenge(locations[_location_id - 1], true);
    lozenge.onclick = function() { outputLocations(_location_id); return false; };
    if (showAlternativeLocations) {
      outputOtherLocations(_location_id, LOCATIONS);
    }
    outputLocationDescription(locations[_location_id - 1], _location_id, true, false, false);
    if (!showRoles) {
      rolesLozenge = outputLozenge("Roles", false);
      rolesLozenge.onclick = function () { outputLocations(_location_id, true); return false; };
    } else {
      if (_area_id != -1) {
        currentXmlAreaNode = fetchArea(_location_id, _area_id);
        if (currentXmlAreaNode) {
          rolesLozenge = outputAreaLozenge(fetchArea(_location_id, _area_id), true, LOCATIONS);
          rolesLozenge.onclick = function () { outputLocations(_location_id, true); return false; };
        } else {
          rolesLozenge = outputLozenge('Roles', false);
          rolesLozenge.onclick = function () { outputLocations(_location_id, true); return false; };
          outputOtherAreas(_area_id, LOCATIONS);
          outputSorry();
          dieNow = true;
        }
      } else {
        rolesLozenge = outputLozenge('Roles', false);
        rolesLozenge.onclick = function () { outputLocations(_location_id, true); return false; };
      }
    }
    if (showAlternativeAreas) {
      outputOtherAreas(_area_id, LOCATIONS);
    }

    if (!dieNow) {
      outputAreas(showRoles);
    }
  } else {
    for(var i = 0; i < locations.length; i++) {
      lozenge = outputLocationLozenge(locations[i], false);
      if (locations[i].getAttribute('id') == _location_id) {
        outputLocationDescription(locations[i], _location_id, false, false, true);
      }
      lozenge.onclick = linkEvent;
      lozenge.id = locations[i].getAttribute('id');
    }
  }

  if (showRoles && !dieNow) {
    outputLozenge("Level", true);
    outputRoles();
  }
}

function outputSorry() {
  outputNode = document.getElementById('wciw-content');
  sorryDiv = document.createElement('div');
  sorryDiv.className = "location";
  sorryParagraph = document.createElement('p');
  sorryParagraph.appendChild(document.createTextNode("Sorry, there are no levels available for this combination of location and role."));
  sorryTitle = document.createElement('h4');
  sorryTitle.appendChild(document.createTextNode('Sorry'));
  sorryDiv.appendChild(sorryTitle);
  sorryDiv.appendChild(sorryParagraph);
  outputNode.appendChild(sorryDiv);
}

function outputOtherLocations (locationId, section) {
  xmlLocations = fetchLocations();
  outputNode = document.getElementById('wciw-content');
  if (section == LOCATIONS) {
    linkEvent = function() { outputLocations(this.location_id, true, _area_id, false); return false; };
  } else {
    linkEvent = function() { outputRoleSection(_area_id, true, this.location_id, false); return false; };
  }
  otherLocationUL = document.createElement('ul');
  otherLocationUL.className = "other-choices";

  for (var i = 0; i < xmlLocations.length; i++) {
    if (xmlLocations[i].getAttribute('id') != locationId) {
      otherLocationLink = document.createElement('a');
      otherLocationLink.setAttribute('href', '#');
      otherLocationLink.setAttribute('title', xmlLocations[i].getAttribute('name'));
      otherLocationLink.location_id = xmlLocations[i].getAttribute('id');
      otherLocationLink.onclick = linkEvent;
      otherLocationLink.appendChild(document.createTextNode(xmlLocations[i].getAttribute('name')));

      otherLocationLI = document.createElement('li');
      otherLocationLI.appendChild(otherLocationLink);

      otherLocationUL.appendChild(otherLocationLI);
    }
  }

  outputNode.appendChild(otherLocationUL);
}

function outputOtherAreas (areaId, section) {
  xmlLocations = fetchLocations();
  areasAlreadyOutput = new Array();
  areasAlreadyOutput[0] = areaId;
  outputNode = document.getElementById('wciw-content');

  otherAreaUL = document.createElement('ul');
  otherAreaUL.className = "other-choices";

  for (var i = 0; i < xmlLocations.length; i++) {
    xmlAreas = fetchAreas(xmlLocations[i].getAttribute('id'));
    for (var j = 0; j < xmlAreas.length; j++)  {
      // Check not output already
      found = false;
      for (var k = 0; k < areasAlreadyOutput.length; k++) {
        if (xmlAreas[j].getAttribute('id') == areasAlreadyOutput[k]) {
          found = true;
          break;
        }
      }
      if (!found) {
        otherAreaLink = document.createElement('a');
        otherAreaLink.setAttribute('href', '#');
        otherAreaLink.area_id = xmlAreas[j].getAttribute('id');
        if (section == ROLES) {
          otherAreaLink.onclick = function() { outputRoleSection(this.area_id, true, _location_id, false, false); return false; };
        } else {
          otherAreaLink.onclick = function() { outputLocations(_location_id, true, this.area_id, true); return false; };
        }
        otherAreaLink.appendChild(document.createTextNode(xmlAreas[j].getAttribute('name')));

        otherAreaLI = document.createElement('li');
        otherAreaLI.appendChild(otherAreaLink);

        otherAreaUL.appendChild(otherAreaLI);

        areasAlreadyOutput[areasAlreadyOutput.length] = xmlAreas[j].getAttribute('id');
      }
    }
  }

  outputNode.appendChild(otherAreaUL);
}

function outputLocationLozenge(locationNode, showChoice, section) {
  showChoice = showChoice || false;
  thisLozenge = outputLozenge(locationNode.getAttribute('name'));
  if (showChoice) {
    lozengeContainer = thisLozenge.parentNode;
    chooseAnotherLink = document.createElement('a');
    chooseAnotherLink.setAttribute('href', '#');
    if (section == ROLES) {
      chooseAnotherLink.onclick = function() { outputRoleSection(_area_id, true, _location_id, false, true); return false; };
    } else {
      chooseAnotherLink.onclick = function() { outputLocations(_location_id, true, _area_id, false, true, false); return false; };
    }
    chooseAnotherLink.appendChild(document.createTextNode("change selection"));
    chooseAnotherLinkContainer = document.createElement('span');
    chooseAnotherLinkContainer.className = "change-selection";
    chooseAnotherLinkContainer.appendChild(chooseAnotherLink);
    lozengeContainer.appendChild(chooseAnotherLinkContainer);
  }
  return thisLozenge;
}

function outputAreaLozenge(areaNode, showChoice, section) {
  showChoice = showChoice || false;
  thisLozenge = outputLozenge(areaNode.getAttribute('name'));
  if (showChoice) {
    lozengeContainer = thisLozenge.parentNode;
    chooseAnotherLink = document.createElement('a');
    chooseAnotherLink.setAttribute('href', '#');
    if (section == LOCATIONS) {
      chooseAnotherLink.onclick = function() { outputLocations(_location_id, true, _area_id, true, false, true); return false; };
    } else {
      chooseAnotherLink.onclick = function() { outputRoleSection(_area_id, true, _location_id, true, false); return false; };
    }
    chooseAnotherLink.appendChild(document.createTextNode("change selection"));
    chooseAnotherLinkContainer = document.createElement('span');
    chooseAnotherLinkContainer.className = "change-selection";
    chooseAnotherLinkContainer.appendChild(chooseAnotherLink);
    lozengeContainer.appendChild(chooseAnotherLinkContainer);
  }
  return thisLozenge;
}

function outputLozenge (lozengeText, plain) {
  plain = plain || false;
  currentOutputNode = document.getElementById('wciw-content');
  //<div id="lozenge">
  //  <h1>Greenock : Functional areas : Sales : Roles</h1>
  //</div>
  oLozenge = document.createElement('div');
  oLozenge.setAttribute('id', 'lozenge');
  oHead = document.createElement('h1');

  if (!plain) {
    oLink = document.createElement('a');
    oLink.setAttribute('href', '#');
    oLink.appendChild(document.createTextNode(lozengeText));
    oHead.appendChild(oLink);
  } else {
    oHead.appendChild(document.createTextNode(lozengeText));
  }
  oLozenge.appendChild(oHead);

  currentOutputNode.appendChild(oLozenge);
  if (!plain) {
    return oLink;
  }
}

function outputAreas(showAreas) {
  showAreas = showAreas || false;
  xmlAreas = fetchAreas(_location_id);
  if (showAreas && _area_id != -1) {
    outputArea(xmlAreas[_area_id - 1], true, false);
  } else {
    for (var i = 0; i < xmlAreas.length; i++) {
      outputArea(xmlAreas[i], false, true);
    }
  }
}

function outputRoles() {
  xmlRoles = fetchRoles(_location_id, _area_id);
  for (var i = 0; i < xmlRoles.length; i++) {
    outputRole(xmlRoles[i]);
  }
}

function outputTabs(state) {
  outputNode = document.getElementById('wciw-content');
  //<div id="contentTabs">
  //  <ul>
  //    <li><a href="#">Location</a></li>
  //    <li><a href="#">Roles</a></li>
  //  </ul>
  //</div>
  oContentTabs = document.createElement('div');
  oContentTabs.setAttribute('id', 'content-nav');
  oList = document.createElement('ul');

  oListArea = document.createElement('li');
  oListAreaLink = document.createElement('a');
	oListAreaLink.setAttribute('href', '#');
  oListAreaLink.onclick = function() { outputRoleSection(); return false; };
  oListAreaLink.appendChild(document.createTextNode('Roles'));
  oListArea.appendChild(oListAreaLink);
  oList.appendChild(oListArea);
  if (state) {
    oListArea.setAttribute('class', 'current');
    oListArea.className = "current";
  } else {
    oListArea.setAttribute('class', '');
    oListArea.className = "";
  }

  oListLocation = document.createElement('li');
  oListLocationLink = document.createElement('a');
	oListLocationLink.setAttribute('href', '#');
  oListLocationLink.onclick = function() { outputLocations(); return false; };
  oListLocationLink.appendChild(document.createTextNode('Location'));
  oListLocation.appendChild(oListLocationLink);
  oList.appendChild(oListLocation);
  if (!state) {
    oListLocation.setAttribute('class', 'current');
    oListLocation.className = "current";
  } else {
    oListLocation.setAttribute('class', '');
    oListLocation.className = "";
  }

  oContentTabs.appendChild(oList);
  outputNode.appendChild(oContentTabs);
}

function clearFlag() {
  mapNode = document.getElementById('wciw-map').getElementsByTagName('div')[0];
  mapNode.style['backgroundPosition'] = "0 0";
}

function moveFlag(locationNode) {
  locationOutputName = locationNode.getAttribute('outputName');
  mapNode = document.getElementById('wciw-map').getElementsByTagName('div')[0];
  mapNode.setAttribute('class', 'flag' + locationOutputName);
  mapNode.className = 'flag' + locationOutputName;
  
  switch(locationOutputName) {
    case 'Doxford':
      backgroundPosition = "-410px 0";
      break;
    case 'Greenock':
      backgroundPosition = "-615px 0";
      break;
    case 'MerthyrTydfil':
      backgroundPosition = "-820px 0";
      break;
  }
  mapNode.style['backgroundPosition'] = backgroundPosition;
}

function clearOutputNode(state) {
  outputNode = document.getElementById('wciw-content');
  outputChildren = outputNode.childNodes;
  for (i = outputChildren.length - 1; i >= 0; i--) {
    outputNode.removeChild(outputChildren[i]);
  }
  outputNode.innerHTML = "";
  outputTabs(state);
  clearFlag();
}

function outputLocationDescription(xmlLocation, additionalLocationId, hideLink, showOtherLink, has_slide) {
  hideLink = hideLink || false;
  showOtherLink = showOtherLink || false;
  // Fetch the html content area
  outputNode = document.getElementById('wciw-content');

  // Create a new role node
  locationHtmlNode = document.createElement('div');
  locationHtmlNode.setAttribute('class', 'location');
  locationHtmlNode.className = 'location';
  // Create H2 with the role title
  locationHeader = document.createElement('h4');
  locationHeader.appendChild(document.createTextNode(xmlLocation.getAttribute('name')));
  locationHtmlNode.appendChild(locationHeader);
  // Add a P node to the DIV
  locationDescription = document.createElement('p');
  locationDescription.appendChild(document.createTextNode(xmlLocation.getElementsByTagName('description')[0].firstChild.nodeValue));
  locationHtmlNode.appendChild(locationDescription);

  if (!hideLink) {
    // <p class="findOutMore"><a href="#">find out more</a></p>
    findOutMore = document.createElement('p');
    findOutMoreLink = document.createElement('a');
    findOutMore.appendChild(findOutMoreLink);
    locationHtmlNode.appendChild(findOutMore);
    findOutMore.setAttribute('class', 'findOutMore');
    findOutMore.className = 'findOutMore';
    findOutMoreLink.setAttribute('href', '#');
    findOutMoreLink.appendChild(document.createTextNode('find out more about the roles available at this location'));
    findOutMoreLink.onclick = function() { outputLocations(additionalLocationId, true); return false; }
  } else if (showOtherLink && has_slide) {
    outputViewRoles(locationHtmlNode, xmlLocation.getAttribute('id'), _area_id, true);
  }
  // Put the new DIV in the content area
  outputNode.appendChild(locationHtmlNode);

  if (has_slide) {
    var myLocEffect = new fx.Height(locationHtmlNode , {duration: 500});
    locationHtmlNode.style.height = 0;
    myLocEffect.toggle();
  }
}

function outputAreaDescription(xmlArea, has_slide) {
  // Fetch the html content area
  outputNode = document.getElementById('wciw-content');

  // Create a new area node
  areaHtmlNode = document.createElement('div');
  areaHtmlNode.setAttribute('class', 'location');
  areaHtmlNode.className = 'location';
  // Create H2 with the role title
  areaHeader = document.createElement('h4');
  areaHeader.appendChild(document.createTextNode(xmlArea.getAttribute('name')));
  areaHtmlNode.appendChild(areaHeader);
  // Add a P node to the DIV
  areaDescription = document.createElement('p');
  areaDescription.appendChild(document.createTextNode(xmlArea.getElementsByTagName('description')[0].firstChild.nodeValue));
  areaHtmlNode.appendChild(areaDescription);

  if (has_slide) {
    findOutMore = document.createElement('p');
    findOutMore.setAttribute('class', 'linkViewLiveVacancies');
    findOutMore.className = 'linkViewLiveVacancies';
    findOutMoreLink = document.createElement('a');
    findOutMoreLink.setAttribute('href', '#');
    findOutMoreLink.onclick = function() { outputRoleSection(_area_id, true); return false; };
    buttonText = "Learn more about where our " + xmlArea.getAttribute('name') + " opportunities exist...";
    findOutMoreLink.appendChild(document.createTextNode(buttonText));
    findOutMore.appendChild(findOutMoreLink);
    areaHtmlNode.appendChild(findOutMore);
  }

  // Put the new DIV in the content area
  outputNode.appendChild(areaHtmlNode);

  if (has_slide) {
    var myEffect = new fx.Height(areaHtmlNode , {duration: 500});
    areaHtmlNode.style.height = 0;
    myEffect.toggle();
  }
}

function outputRole(role) {false
  // Fetch the html content area
  outputNode = document.getElementById('wciw-content');
  // Make it visible
  outputNode.style.display = 'block';
  // Create a new role node
  roleNode = document.createElement('div');
  roleNode.setAttribute('class', 'role');
  roleNode.className = 'role';
  // Create H2 with the role title
  roleHeader = document.createElement('h4');
  roleHeader.appendChild(document.createTextNode(role.getAttribute('name')));
  roleNode.appendChild(roleHeader);
  // Add a P node to the DIV
  roleDescription = document.createElement('p');
  roleDescription.appendChild(document.createTextNode(role.firstChild.nodeValue));
  roleNode.appendChild(roleDescription);

  // Add the 3rd party link
  // <p class="linkViewLiveVacancies"><a href="#">Apply now</a></p>
  findOutMore = document.createElement('p');
  findOutMore.setAttribute('class', 'linkViewLiveVacancies');
  findOutMore.className = 'linkViewLiveVacancies';
  findOutMoreLink = document.createElement('a');
  findOutMoreLink.setAttribute('href', role.getAttribute('href'));
  findOutMoreLink.appendChild(document.createTextNode('Apply now'));
  findOutMore.appendChild(findOutMoreLink);
  roleNode.appendChild(findOutMore);

  // Put the new DIV in the content area
  outputNode.appendChild(roleNode);
}

function outputArea(area, hideLink, has_slide) {
  hideLink = hideLink || false;

  // Fetch the html content area
  outputNode = document.getElementById('wciw-content');
  // Create a new role node
  areaNode = document.createElement('div');
  areaNode.setAttribute('class', 'area');
  areaNode.className = 'area';
  // Create H2 with the role title
  areaHeader = document.createElement('h4');
  areaHeader.appendChild(document.createTextNode(area.getAttribute('name')));
  areaNode.appendChild(areaHeader);
  // Add a P node to the DIV
  areaDescription = document.createElement('p');
  areaDescription.appendChild(document.createTextNode(area.getElementsByTagName('description')[0].firstChild.nodeValue));
  areaNode.appendChild(areaDescription);

  if (!hideLink) {
    areaNode = outputViewRoles(areaNode, _location_id, area.getAttribute('id'));
  }

  // Put the new DIV in the content area
  outputNode.appendChild(areaNode);

  if (has_slide) {
    var myLocEffect = new fx.Height(areaNode , {duration: 500});
    areaNode.style.height = 0;
    myLocEffect.toggle();
  }
}

function outputViewRoles(outputAreaNode, locationId, areaId, is_roles) {
    is_roles = is_roles || false;

    // <p class="linkViewRoles"><a href="#">That sounds like me... view roles.</a></p>
    findOutMore = document.createElement('p');
    findOutMore.setAttribute('class', 'linkViewRoles');
    findOutMore.className = 'linkViewRoles';
    findOutMoreLink = document.createElement('a');
    findOutMoreLink.setAttribute('href', '#');
    if (is_roles) {
      findOutMoreLink.appendChild(document.createTextNode('Tell me more about the vacancies at this location.'));
    } else {
      findOutMoreLink.appendChild(document.createTextNode('That sounds like me... view roles.'));
    }
    findOutMoreLink.location_id = locationId;
    findOutMoreLink.area_id = areaId;
    if (is_roles) {
      findOutMoreLink.onclick = function() { outputRoleSection(this.area_id, true, this.location_id); return false; };
    } else {
      findOutMoreLink.onclick = function() { outputLocations(this.location_id, true, this.area_id, true); return false; };
    }
    findOutMore.appendChild(findOutMoreLink);
    outputAreaNode.appendChild(findOutMore);
    return outputAreaNode;
}

///////////////////////////////////////////////////////////////////////////////
//                     XML FUNCTIONS
///////////////////////////////////////////////////////////////////////////////

function loadXML(filename) {
  if (window.ActiveXObject) {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.load(filename);
    processXML();
  } else if (document.implementation && document.implementation.createDocument) {
    xmlDoc = document.implementation.createDocument("","",null);
    if (xmlDoc.load) {
      xmlDoc.load(filename);
    } else {
      // If we can't load the XML, we quit straight away...
      return;
    }
    xmlDoc.onload = processXML;
  } else {
    alert('Your browser cannot handle this script');
  }
}

function processXML() {
  xmlRoot = xmlDoc.getElementsByTagName('search');
  locations = fetchLocations();
  // Remove the accessible content from the content area
  clearOutputNode(false);
  outputRoleSection();
}

function fetchXmlLocationNode(locationId) {
  locations = fetchLocations();
  for (i = 0; i < locations.length; i++) {
    if (locations[i].getAttribute('id') == locationId) {
      return locations[i];
    }
  }
  return false;
}

function fetchXmlAreaNode(locationId, areaId) {
  myLocationNode = fetchXmlLocationNode(locationId);
  if (myLocationNode) {
    areas = myLocationNode.getElementsByTagName('area');
    for (i = 0; i < areas.length; i++) {
      if (areas[i].getAttribute('id') == areaId) {
        return areas[i];
      }
    }
  }
  return false;
}

function fetchLocations() {
  return xmlRoot[0].getElementsByTagName('location');
}

function fetchAreas(locationId) {
  myLocationNode = fetchXmlLocationNode(locationId);
  if (myLocationNode) {
    return myLocationNode.getElementsByTagName('area');
  }
  return false;
}

function fetchArea(locationId, areaId) {
  areas = fetchAreas(locationId);
  for (i = 0; i < areas.length; i++) {
    if (areas[i].getAttribute('id') == areaId) {
      return areas[i];
    }
  }
  return false;
}

function fetchRoles(locationId, areaId) {
  myAreaNode = fetchXmlAreaNode(locationId, areaId);
  if (myAreaNode) {
    return myAreaNode.getElementsByTagName('role');
  }
  return false;
}

function fetchRole (locationId, areaId, roleId) {
  myRoleNodes = fetchRoles(locationId, areaId);
  for (i = 0; i < myRoleNodes.length; i++) {
    if (myRoleNodes[i].getAttribute('id') == roleId) {
      return myRoleNodes[i];
    }
  }
  return false;
}

