﻿var _asAlready = false;
var _asRegion = -1;
var _asRegionHovered = -1;
var _asDepartments = new Array(-1, 21, 18, 3, 20, 20, 20, 21, 7, 14, 7, 10, 14, 20, 13, 3, 19, 19, 6, 11, -1, 5, 4, 11, 2, 9, 21, 22, 6, 4, 10, 14, 14, 2, 10, 4, 6, 6, 21, 9, 2, 6, 21, 3, 17, 6, 14, 2, 10, 17, 13, 7, 7, 17, 12, 12, 4, 12, 5, 15, 18, 13, 15, 3, 2, 14, 10, 1, 1, 21, 9, 5, 17, 21, 21, 16, 22, 16, 16, 19, 18, 14, 14, 20, 20, 17, 19, 11, 12, 5, 9, 16, 16, 16, 16, 16, 8, 8);
var _asDepartment = -1;
var _asDepartmentHovered = -1;
var _asRegionPopup = -1;
var _asCache = new Array(9);
var _asRegionShow = null;
var _asOldRegion = -1;
var _asDeptShow = null;
var _asOldDept = null;
var _asInfo = false;

//Cache two images so the page is displayed quickly
_asCache[0] = new Image();
_asCache[0].src = '/images/loaderbig.gif';
_asCache[1] = new Image();
_asCache[1].src = '/map/blank.gif';

//Initialise the page once it has loaded
$(document).ready(function() { InitAdvancedSearch() });

//Toggles the items in a checked list box so that the first item is checked for "No preference" when all other items are unchecked and vice versa.
function ToggleChecked(listBox, e) {
    if (_asAlready) return;  //Only run if not already running - prevents recursion

    _asAlready = true;
    var selectedItem = listBox.GetItem(e.index);
    var noPrefChecked = false;
    var noPrefItem = new Array(1);
    noPrefItem[0] = -1;

    if (selectedItem.value == -1) {
        noPrefChecked = true;
    }

    if (noPrefChecked) {
        listBox.UnselectAll();
        listBox.SelectValues(noPrefItem);
    }
    else {
        listBox.UnselectValues(new Array(noPrefItem));
    }

    if (listBox.GetSelectedValues().length == 0) {
        listBox.SelectValues(noPrefItem);
    }

    _asAlready = false;
}

//Initialises the page.
function InitAdvancedSearch() {
    PrepareCache();
    CheckCache();

    if ($('#rdinfo').length == 1) {
        _asInfo = true;  //Only show the info pages if the info div is on the page
    }

    //Assign the hovering functions to the main map
    $('#region1').hover(function() { RegionHovered(1); }, function() { RegionOut(1); });
    $('#region2').hover(function() { RegionHovered(2); }, function() { RegionOut(2); });
    $('#region3').hover(function() { RegionHovered(3); }, function() { RegionOut(3); });
    $('#region4').hover(function() { RegionHovered(4); }, function() { RegionOut(4); });
    $('#region5').hover(function() { RegionHovered(5); }, function() { RegionOut(5); });
    $('#region6').hover(function() { RegionHovered(6); }, function() { RegionOut(6); });
    $('#region7').hover(function() { RegionHovered(7); }, function() { RegionOut(7); });
    $('#region8').hover(function() { RegionHovered(8); }, function() { RegionOut(8); });
    $('#region9').hover(function() { RegionHovered(9); }, function() { RegionOut(9); });
    $('#region10').hover(function() { RegionHovered(10); }, function() { RegionOut(10); });
    $('#region11').hover(function() { RegionHovered(11); }, function() { RegionOut(11); });
    $('#region12').hover(function() { RegionHovered(12); }, function() { RegionOut(12); });
    $('#region13').hover(function() { RegionHovered(13); }, function() { RegionOut(13); });
    $('#region14').hover(function() { RegionHovered(14); }, function() { RegionOut(14); });
    $('#region15').hover(function() { RegionHovered(15); }, function() { RegionOut(15); });
    $('#region16').hover(function() { RegionHovered(16); }, function() { RegionOut(16); });
    $('#region17').hover(function() { RegionHovered(17); }, function() { RegionOut(17); });
    $('#region18').hover(function() { RegionHovered(18); }, function() { RegionOut(18); });
    $('#region19').hover(function() { RegionHovered(19); }, function() { RegionOut(19); });
    $('#region20').hover(function() { RegionHovered(20); }, function() { RegionOut(20); });
    $('#region21').hover(function() { RegionHovered(21); }, function() { RegionOut(21); });
    $('#region22').hover(function() { RegionHovered(22); }, function() { RegionOut(22); });

    //Assign the click function to the main map
    $('#region1').click(function() { SelectRegion(1); });
    $('#region2').click(function() { SelectRegion(2); });
    $('#region3').click(function() { SelectRegion(3); });
    $('#region4').click(function() { SelectRegion(4); });
    $('#region5').click(function() { SelectRegion(5); });
    $('#region6').click(function() { SelectRegion(6); });
    $('#region7').click(function() { SelectRegion(7); });
    $('#region8').click(function() { SelectRegion(8); });
    $('#region9').click(function() { SelectRegion(9); });
    $('#region10').click(function() { SelectRegion(10); });
    $('#region11').click(function() { SelectRegion(11); });
    $('#region12').click(function() { SelectRegion(12); });
    $('#region13').click(function() { SelectRegion(13); });
    $('#region14').click(function() { SelectRegion(14); });
    $('#region15').click(function() { SelectRegion(15); });
    $('#region16').click(function() { SelectRegion(16); });
    $('#region17').click(function() { SelectRegion(17); });
    $('#region18').click(function() { SelectRegion(18); });
    $('#region19').click(function() { SelectRegion(19); });
    $('#region20').click(function() { SelectRegion(20); });
    $('#region21').click(function() { SelectRegion(21); });
    $('#region22').click(function() { SelectRegion(22); });

    //Assign the hover functions to the individual region maps
    $('#dept1').hover(function() { DeptHovered(1); }, function() { DeptOut(1); });
    $('#dept2').hover(function() { DeptHovered(2); }, function() { DeptOut(2); });
    $('#dept3').hover(function() { DeptHovered(3); }, function() { DeptOut(3); });
    $('#dept4').hover(function() { DeptHovered(4); }, function() { DeptOut(4); });
    $('#dept5').hover(function() { DeptHovered(5); }, function() { DeptOut(5); });
    $('#dept6').hover(function() { DeptHovered(6); }, function() { DeptOut(6); });
    $('#dept7').hover(function() { DeptHovered(7); }, function() { DeptOut(7); });
    $('#dept8').hover(function() { DeptHovered(8); }, function() { DeptOut(8); });
    $('#dept9').hover(function() { DeptHovered(9); }, function() { DeptOut(9); });
    $('#dept10').hover(function() { DeptHovered(10); }, function() { DeptOut(10); });
    $('#dept11').hover(function() { DeptHovered(11); }, function() { DeptOut(11); });
    $('#dept12').hover(function() { DeptHovered(12); }, function() { DeptOut(12); });
    $('#dept13').hover(function() { DeptHovered(13); }, function() { DeptOut(13); });
    $('#dept14').hover(function() { DeptHovered(14); }, function() { DeptOut(14); });
    $('#dept15').hover(function() { DeptHovered(15); }, function() { DeptOut(15); });
    $('#dept16').hover(function() { DeptHovered(16); }, function() { DeptOut(16); });
    $('#dept17').hover(function() { DeptHovered(17); }, function() { DeptOut(17); });
    $('#dept18').hover(function() { DeptHovered(18); }, function() { DeptOut(18); });
    $('#dept19').hover(function() { DeptHovered(19); }, function() { DeptOut(19); });
    $('#dept21').hover(function() { DeptHovered(21); }, function() { DeptOut(21); });
    $('#dept22').hover(function() { DeptHovered(22); }, function() { DeptOut(22); });
    $('#dept23').hover(function() { DeptHovered(23); }, function() { DeptOut(23); });
    $('#dept24').hover(function() { DeptHovered(24); }, function() { DeptOut(24); });
    $('#dept25').hover(function() { DeptHovered(25); }, function() { DeptOut(25); });
    $('#dept26').hover(function() { DeptHovered(26); }, function() { DeptOut(26); });
    $('#dept27').hover(function() { DeptHovered(27); }, function() { DeptOut(27); });
    $('#dept28').hover(function() { DeptHovered(28); }, function() { DeptOut(28); });
    $('#dept29').hover(function() { DeptHovered(29); }, function() { DeptOut(29); });
    $('#dept30').hover(function() { DeptHovered(30); }, function() { DeptOut(30); });
    $('#dept31').hover(function() { DeptHovered(31); }, function() { DeptOut(31); });
    $('#dept32').hover(function() { DeptHovered(32); }, function() { DeptOut(32); });
    $('#dept33').hover(function() { DeptHovered(33); }, function() { DeptOut(33); });
    $('#dept34').hover(function() { DeptHovered(34); }, function() { DeptOut(34); });
    $('#dept35').hover(function() { DeptHovered(35); }, function() { DeptOut(35); });
    $('#dept36').hover(function() { DeptHovered(36); }, function() { DeptOut(36); });
    $('#dept37').hover(function() { DeptHovered(37); }, function() { DeptOut(37); });
    $('#dept38').hover(function() { DeptHovered(38); }, function() { DeptOut(38); });
    $('#dept39').hover(function() { DeptHovered(39); }, function() { DeptOut(39); });
    $('#dept40').hover(function() { DeptHovered(40); }, function() { DeptOut(40); });
    $('#dept41').hover(function() { DeptHovered(41); }, function() { DeptOut(41); });
    $('#dept42').hover(function() { DeptHovered(42); }, function() { DeptOut(42); });
    $('#dept43').hover(function() { DeptHovered(43); }, function() { DeptOut(43); });
    $('#dept44').hover(function() { DeptHovered(44); }, function() { DeptOut(44); });
    $('#dept45').hover(function() { DeptHovered(45); }, function() { DeptOut(45); });
    $('#dept46').hover(function() { DeptHovered(46); }, function() { DeptOut(46); });
    $('#dept47').hover(function() { DeptHovered(47); }, function() { DeptOut(47); });
    $('#dept48').hover(function() { DeptHovered(48); }, function() { DeptOut(48); });
    $('#dept49').hover(function() { DeptHovered(49); }, function() { DeptOut(49); });
    $('#dept50').hover(function() { DeptHovered(50); }, function() { DeptOut(50); });
    $('#dept51').hover(function() { DeptHovered(51); }, function() { DeptOut(51); });
    $('#dept52').hover(function() { DeptHovered(52); }, function() { DeptOut(52); });
    $('#dept53').hover(function() { DeptHovered(53); }, function() { DeptOut(53); });
    $('#dept54').hover(function() { DeptHovered(54); }, function() { DeptOut(54); });
    $('#dept55').hover(function() { DeptHovered(55); }, function() { DeptOut(55); });
    $('#dept56').hover(function() { DeptHovered(56); }, function() { DeptOut(56); });
    $('#dept57').hover(function() { DeptHovered(57); }, function() { DeptOut(57); });
    $('#dept58').hover(function() { DeptHovered(58); }, function() { DeptOut(58); });
    $('#dept59').hover(function() { DeptHovered(59); }, function() { DeptOut(59); });
    $('#dept60').hover(function() { DeptHovered(60); }, function() { DeptOut(60); });
    $('#dept61').hover(function() { DeptHovered(61); }, function() { DeptOut(61); });
    $('#dept62').hover(function() { DeptHovered(62); }, function() { DeptOut(62); });
    $('#dept63').hover(function() { DeptHovered(63); }, function() { DeptOut(63); });
    $('#dept64').hover(function() { DeptHovered(64); }, function() { DeptOut(64); });
    $('#dept65').hover(function() { DeptHovered(65); }, function() { DeptOut(65); });
    $('#dept66').hover(function() { DeptHovered(66); }, function() { DeptOut(66); });
    $('#dept67').hover(function() { DeptHovered(67); }, function() { DeptOut(67); });
    $('#dept68').hover(function() { DeptHovered(68); }, function() { DeptOut(68); });
    $('#dept69').hover(function() { DeptHovered(69); }, function() { DeptOut(69); });
    $('#dept70').hover(function() { DeptHovered(70); }, function() { DeptOut(70); });
    $('#dept71').hover(function() { DeptHovered(71); }, function() { DeptOut(71); });
    $('#dept72').hover(function() { DeptHovered(72); }, function() { DeptOut(72); });
    $('#dept73').hover(function() { DeptHovered(73); }, function() { DeptOut(73); });
    $('#dept74').hover(function() { DeptHovered(74); }, function() { DeptOut(74); });
    $('#dept75').hover(function() { DeptHovered(75); }, function() { DeptOut(75); });
    $('#dept76').hover(function() { DeptHovered(76); }, function() { DeptOut(76); });
    $('#dept77').hover(function() { DeptHovered(77); }, function() { DeptOut(77); });
    $('#dept78').hover(function() { DeptHovered(78); }, function() { DeptOut(78); });
    $('#dept79').hover(function() { DeptHovered(79); }, function() { DeptOut(79); });
    $('#dept80').hover(function() { DeptHovered(80); }, function() { DeptOut(80); });
    $('#dept81').hover(function() { DeptHovered(81); }, function() { DeptOut(81); });
    $('#dept82').hover(function() { DeptHovered(82); }, function() { DeptOut(82); });
    $('#dept83').hover(function() { DeptHovered(83); }, function() { DeptOut(83); });
    $('#dept84').hover(function() { DeptHovered(84); }, function() { DeptOut(84); });
    $('#dept85').hover(function() { DeptHovered(85); }, function() { DeptOut(85); });
    $('#dept86').hover(function() { DeptHovered(86); }, function() { DeptOut(86); });
    $('#dept87').hover(function() { DeptHovered(87); }, function() { DeptOut(87); });
    $('#dept88').hover(function() { DeptHovered(88); }, function() { DeptOut(88); });
    $('#dept89').hover(function() { DeptHovered(89); }, function() { DeptOut(89); });
    $('#dept90').hover(function() { DeptHovered(90); }, function() { DeptOut(90); });
    $('#dept91').hover(function() { DeptHovered(91); }, function() { DeptOut(91); });
    $('#dept92').hover(function() { DeptHovered(92); }, function() { DeptOut(92); });
    $('#dept93').hover(function() { DeptHovered(93); }, function() { DeptOut(93); });
    $('#dept94').hover(function() { DeptHovered(94); }, function() { DeptOut(94); });
    $('#dept95').hover(function() { DeptHovered(95); }, function() { DeptOut(95); });
    $('#dept201').hover(function() { DeptHovered(201); }, function() { DeptOut(201); });
    $('#dept202').hover(function() { DeptHovered(202); }, function() { DeptOut(202); });

    //Assign the click function to the individual region maps
    $('#dept1').click(function() { SelectDepartmentTop(1); });
    $('#dept2').click(function() { SelectDepartmentTop(2); });
    $('#dept3').click(function() { SelectDepartmentTop(3); });
    $('#dept4').click(function() { SelectDepartmentTop(4); });
    $('#dept5').click(function() { SelectDepartmentTop(5); });
    $('#dept6').click(function() { SelectDepartmentTop(6); });
    $('#dept7').click(function() { SelectDepartmentTop(7); });
    $('#dept8').click(function() { SelectDepartmentTop(8); });
    $('#dept9').click(function() { SelectDepartmentTop(9); });
    $('#dept10').click(function() { SelectDepartmentTop(10); });
    $('#dept11').click(function() { SelectDepartmentTop(11); });
    $('#dept12').click(function() { SelectDepartmentTop(12); });
    $('#dept13').click(function() { SelectDepartmentTop(13); });
    $('#dept14').click(function() { SelectDepartmentTop(14); });
    $('#dept15').click(function() { SelectDepartmentTop(15); });
    $('#dept16').click(function() { SelectDepartmentTop(16); });
    $('#dept17').click(function() { SelectDepartmentTop(17); });
    $('#dept18').click(function() { SelectDepartmentTop(18); });
    $('#dept19').click(function() { SelectDepartmentTop(19); });
    $('#dept20').click(function() { SelectDepartmentTop(20); });
    $('#dept21').click(function() { SelectDepartmentTop(21); });
    $('#dept22').click(function() { SelectDepartmentTop(22); });
    $('#dept23').click(function() { SelectDepartmentTop(23); });
    $('#dept24').click(function() { SelectDepartmentTop(24); });
    $('#dept25').click(function() { SelectDepartmentTop(25); });
    $('#dept26').click(function() { SelectDepartmentTop(26); });
    $('#dept27').click(function() { SelectDepartmentTop(27); });
    $('#dept28').click(function() { SelectDepartmentTop(28); });
    $('#dept29').click(function() { SelectDepartmentTop(29); });
    $('#dept30').click(function() { SelectDepartmentTop(30); });
    $('#dept31').click(function() { SelectDepartmentTop(31); });
    $('#dept32').click(function() { SelectDepartmentTop(32); });
    $('#dept33').click(function() { SelectDepartmentTop(33); });
    $('#dept34').click(function() { SelectDepartmentTop(34); });
    $('#dept35').click(function() { SelectDepartmentTop(35); });
    $('#dept36').click(function() { SelectDepartmentTop(36); });
    $('#dept37').click(function() { SelectDepartmentTop(37); });
    $('#dept38').click(function() { SelectDepartmentTop(38); });
    $('#dept39').click(function() { SelectDepartmentTop(39); });
    $('#dept40').click(function() { SelectDepartmentTop(40); });
    $('#dept41').click(function() { SelectDepartmentTop(41); });
    $('#dept42').click(function() { SelectDepartmentTop(42); });
    $('#dept43').click(function() { SelectDepartmentTop(43); });
    $('#dept44').click(function() { SelectDepartmentTop(44); });
    $('#dept45').click(function() { SelectDepartmentTop(45); });
    $('#dept46').click(function() { SelectDepartmentTop(46); });
    $('#dept47').click(function() { SelectDepartmentTop(47); });
    $('#dept48').click(function() { SelectDepartmentTop(48); });
    $('#dept49').click(function() { SelectDepartmentTop(49); });
    $('#dept50').click(function() { SelectDepartmentTop(50); });
    $('#dept51').click(function() { SelectDepartmentTop(51); });
    $('#dept52').click(function() { SelectDepartmentTop(52); });
    $('#dept53').click(function() { SelectDepartmentTop(53); });
    $('#dept54').click(function() { SelectDepartmentTop(54); });
    $('#dept55').click(function() { SelectDepartmentTop(55); });
    $('#dept56').click(function() { SelectDepartmentTop(56); });
    $('#dept57').click(function() { SelectDepartmentTop(57); });
    $('#dept58').click(function() { SelectDepartmentTop(58); });
    $('#dept59').click(function() { SelectDepartmentTop(59); });
    $('#dept60').click(function() { SelectDepartmentTop(60); });
    $('#dept61').click(function() { SelectDepartmentTop(61); });
    $('#dept62').click(function() { SelectDepartmentTop(62); });
    $('#dept63').click(function() { SelectDepartmentTop(63); });
    $('#dept64').click(function() { SelectDepartmentTop(64); });
    $('#dept65').click(function() { SelectDepartmentTop(65); });
    $('#dept66').click(function() { SelectDepartmentTop(66); });
    $('#dept67').click(function() { SelectDepartmentTop(67); });
    $('#dept68').click(function() { SelectDepartmentTop(68); });
    $('#dept69').click(function() { SelectDepartmentTop(69); });
    $('#dept70').click(function() { SelectDepartmentTop(70); });
    $('#dept71').click(function() { SelectDepartmentTop(71); });
    $('#dept72').click(function() { SelectDepartmentTop(72); });
    $('#dept73').click(function() { SelectDepartmentTop(73); });
    $('#dept74').click(function() { SelectDepartmentTop(74); });
    $('#dept75').click(function() { SelectDepartmentTop(75); });
    $('#dept76').click(function() { SelectDepartmentTop(76); });
    $('#dept77').click(function() { SelectDepartmentTop(77); });
    $('#dept78').click(function() { SelectDepartmentTop(78); });
    $('#dept79').click(function() { SelectDepartmentTop(79); });
    $('#dept80').click(function() { SelectDepartmentTop(80); });
    $('#dept81').click(function() { SelectDepartmentTop(81); });
    $('#dept82').click(function() { SelectDepartmentTop(82); });
    $('#dept83').click(function() { SelectDepartmentTop(83); });
    $('#dept84').click(function() { SelectDepartmentTop(84); });
    $('#dept85').click(function() { SelectDepartmentTop(85); });
    $('#dept86').click(function() { SelectDepartmentTop(86); });
    $('#dept87').click(function() { SelectDepartmentTop(87); });
    $('#dept88').click(function() { SelectDepartmentTop(88); });
    $('#dept89').click(function() { SelectDepartmentTop(89); });
    $('#dept90').click(function() { SelectDepartmentTop(90); });
    $('#dept91').click(function() { SelectDepartmentTop(91); });
    $('#dept92').click(function() { SelectDepartmentTop(92); });
    $('#dept93').click(function() { SelectDepartmentTop(93); });
    $('#dept94').click(function() { SelectDepartmentTop(94); });
    $('#dept95').click(function() { SelectDepartmentTop(95); });
    $('#dept201').click(function() { SelectDepartmentTop(201); });
    $('#dept202').click(function() { SelectDepartmentTop(202); });

    $('#rdback').click(function(event) { event.preventDefault(); BackClicked(); });
}

//When the user hovers a region, highlight it and show the info.
function RegionHovered(region) {
    //if (_asRegionHovered > -1) RegionOut(_asRegionHovered);
    MoveRegionH(region);
    $('#rdregh').show();
    ShowRegion(region);
    _asRegionHovered = region;
}

//Hides the selected region.
function RegionOut(region) {
    $('#rdregh').hide();
    ShowRegion(-1);
    if (_asRegion > -1) ShowRegion(_asRegion);
}

//Show the info for the supplied region.
function ShowRegion(region) {
    clearTimeout(_asRegionShow);
    if (_asInfo) {
        _asRegionShow = setTimeout('ShowRegionDelay(' + region + ')', 100);
    }
}

//Shows info for the supplied region.
function ShowRegionDelay(region) {
    if (_asInfo) {
        if (_asOldRegion > -1) $('#rdrinfo' + _asOldRegion).hide();
        if (region > -1) $('#rdrinfo' + region).show();
        if (_asDepartment > -1) {
            if (region == _asRegion) {
                $('#rddinfo' + _asDepartment).show();
            }
            else {
                $('#rddinfo' + _asDepartment).hide();
            }
        }
        _asOldRegion = region;
    }
}

//Show the info for the supplied department.
function ShowDept(dept) {
    clearTimeout(_asDeptShow);
    if (_asInfo) {
        _asDeptShow = setTimeout('ShowDeptDelay(' + dept + ')', 100);
    }
}

//Show the info for the supplied department.
function ShowDeptDelay(dept) {
    if (_asInfo) {
        if (_asOldDept > -1) $('#rddinfo' + _asOldDept).hide();
        if (dept > -1) $('#rddinfo' + dept).show();
        _asOldDept = dept;
    }
}

//When the user hovers over a department, highlight it and show the info.
function DeptHovered(dept) {
    //if (_asDepartmentHovered > -1) DeptOut(_asDepartmentHovered);
    MoveDeptH(dept);
    $('#rddepth').show();
    ShowDept(dept);
    _asDepartmentHovered = dept;
}

//Hide the previously highlighted department.
function DeptOut(dept) {
    $('#rddepth').hide();
    ShowDept(-1);
    if (_asDepartment > -1) ShowDept(_asDepartment);
}

//When the user selects a region, select it and display the region map.
function SelectRegion(region) {
    if (_asRegion != region) {
        UnselectDepartment();
        comboDepartment.PerformCallback(region);
    }
    SelectRegionLower(region);
    if (region == -1) {
        HideRegionPopup();
        $('#rdmain').show();
    }
    else {
        $('#rdmain').hide();
        ShowRegionPopup(region);
    }
    ShowRegionDelay(region);
}

//Selects the supplied region.
function SelectRegionLower(region) {
    UnselectRegion();
    _asRegion = region;
    if (_asRegion > -1) {
        MoveRegionX(region);
        $('#rdreg').show();
    }
    if (comboRegion.GetValue() != region) {
        comboRegion.SetValue(region);
    }
}

//Unselects the currently selected region.
function UnselectRegion() {
    if (_asRegion > -1) {
        $('#rdreg').hide();
        _asRegion = -1;
    }
}

//When the user selects a department, select it and display the info.
function SelectDepartmentTop(department) {
    SelectDepartment(department);
    if (comboDepartment.GetValue() != _asDepartment) {
        comboDepartment.SetValue(_asDepartment);
    }
}

//Selects the supplied department.
function SelectDepartment(department) {
    var region = -1;
    comboDepartment.HideDropDown();
    if (_asDepartment != department) {
        UnselectDepartment();
    }
    _asDepartment = department;

    if (department > -1) {
        region = _asDepartments[(department > 200 ? department - 105 : department)];
        if (comboRegion.GetValue() != region) {
            SelectRegionLower(region);
            comboDepartment.PerformCallback('x' + department);
            ShowRegionDelay(region);
        }
        MoveDeptX(department);
        MoveDeptS(department);
        $('#rdmain #rddept').show();
        $('#rdregions #rddepts').show();
    }
    ShowDeptDelay(department);
}

//Unselects the currently selected department.
function UnselectDepartment() {
    if (_asDepartment > -1) {
        $('#rdmain #rddept').hide();
        //MoveDeptS(100);
        $('#rdregions #rddepts').hide();
        _asDepartment = -1;
    }
    ShowDeptDelay(-1);
}

//Resets the value in the department combo box (workaround for a bug).
function ResetDepartment() {
    comboDepartment.SetValue(_asDepartment);
}

//Shows the region map.
function ShowRegionPopup(region) {
    if (_asRegionPopup != region) {
        HideRegionPopup();
        _asRegionPopup = region;
        //$('#rdr' + region).css('display', 'inline');
        MoveRegionZ(region);
        MoveRegionO(region);
        $('#rdregz').show();
        $('#rdrego').show();
        $('#rdregmap')[0].useMap = '#rdmap' + region;
        $('#rdregions').show();
    }
}

//Hides the region map.
function HideRegionPopup() {
    if (_asRegionPopup > -1) {
        //$('#rdr' + _asRegionPopup).css('display', 'none');
        $('#rdregions').hide();
        $('#rdregz').hide();
        $('#rdregmap')[0].usemap = '';
        _asRegionPopup = -1;
    }
}

//When the user clicks on a region's Back button, show the main map again.
function BackClicked() {
    HideRegionPopup();
    $('#rdmain').show();
}

//Prepares the large cache of images for the map.
function PrepareCache() {
    _asCache[2] = new Image();
    _asCache[2].src = '/map/regions.gif';

    _asCache[3] = new Image();
    _asCache[3].src = '/map/regionx.gif';

    _asCache[4] = new Image();
    _asCache[4].src = '/map/regionh.gif';

    _asCache[5] = new Image();
    _asCache[5].src = '/map/deptx.gif';

    _asCache[6] = new Image();
    _asCache[6].src = '/map/depts.gif';

    _asCache[7] = new Image();
    _asCache[7].src = '/map/depth.gif';

    _asCache[8] = new Image();
    _asCache[8].src = '/map/regionz.gif';

    _asCache[9] = new Image();
    _asCache[9].src = '/map/regiono.png';
}

//Keeps checking until all of the gifs (except the png region overlay) have been downloaded.
function CheckCache() {
    var isComplete = true;
    for (count = 0; count < 9; count++) {
        if (_asCache[count] == 'undefined' || !_asCache[count].complete) {
            isComplete = false;
            break;
        }
    }
    if (isComplete) {
        PrepareMap();
    }
    else {
        setTimeout('CheckCache()', 10);
    }
}

//Once the cache is loaded, prepares the map by setting the image source for each overlay.
function PrepareMap() {
    var item = null;
    
    $('#rdmain #rd0').css('background-image', "url('" + _asCache[2].src + "')");
    $('#rdmain #rdreg').css('background-image', "url('" + _asCache[3].src + "')");
    $('#rdmain #rdregh').css('background-image', "url('" + _asCache[4].src + "')");
    $('#rdmain #rddept').css('background-image', "url('" + _asCache[5].src + "')");
    $('#rdregions #rddepts').css('background-image', "url('" + _asCache[6].src + "')");
    $('#rdregions #rddepth').css('background-image', "url('" + _asCache[7].src + "')");
    $('#rdregions #rdregz').css('background-image', "url('" + _asCache[8].src + "')");
    $('#rdregions #rdrego').css('background-image', "url('" + _asCache[9].src + "')");

    for (count = 0; count < _asRegions.length; count++) {
        if (_asRegions[count] > 0) {
            item = $('#region' + count)[0];
            item.title = item.alt + " : " + _asRegions[count] + " " + (_asRegions[count] == 1 ? _asPropertyTrans : _asPropertiesTrans);
        }
    }

    for (count = 0; count < _asDepts.length; count++) {
        if (_asDepts[count] > 0) {
            item = $('#dept' + count)[0];
            item.title = item.alt + " : " + _asDepts[count] + " " + (_asDepts[count] == 1 ? _asPropertyTrans : _asPropertiesTrans);
        }
    }

    $('#rdloader').hide();  //Hides the loading gif

    $('#rdmain').show();

    //If a region is already selected, display it
    if (comboRegion.GetValue() > -1) {
        var region = comboRegion.GetValue();
        $('#rdrinfo' + region).show();
        _asRegion = region;
        _asOldRegion = region;
        MoveRegionX(region);
        $('#rdmain #rdreg').show();
    }

    //If a department is already selected, display it
    if (comboDepartment.GetValue() > -1) {
        var dept = comboDepartment.GetValue();
        $('#rddinfo' + dept).show();
        _asDepartment = dept;
        _asOldDept = dept;
        MoveDeptX(dept);
        MoveDeptS(dept);
        $('#rdmain #rddept').show();
        $('#rdregions #rddepts').show();
    }
}


function MoveRegionX(region) {
    if ($.browser.msie) $('#rdmain #rdreg').css('background-position', GetRegionMargin(region));
    else $('#rdmain #rdreg').css(GetRegionPos(region));
}


function MoveRegionH(region) {
    if ($.browser.msie) $('#rdmain #rdregh').css('background-position', GetRegionMargin(region));
    else $('#rdmain #rdregh').css(GetRegionPos(region));
}


function MoveRegionO(region) {
    if ($.browser.msie) $('#rdregions #rdrego').css('background-position', GetRegionMargin(region));
    else $('#rdregions #rdrego').css(GetRegionPos(region));
}


function MoveRegionZ(region) {
    if ($.browser.msie) $('#rdregions #rdregz').css('background-position', GetRegionMargin(region));
    else $('#rdregions #rdregz').css(GetRegionPos(region));
}


function MoveDeptX(dept) {
    if ($.browser.msie) $('#rdmain #rddept').css('background-position', GetDeptMargin(dept));
    else $('#rdmain #rddept').css(GetDeptPos(dept));
}


function MoveDeptS(dept) {
    if ($.browser.msie) $('#rdregions #rddepts').css('background-position', GetDeptMargin(dept));
    else $('#rdregions #rddepts').css(GetDeptPos(dept));
}


function MoveDeptH(dept) {
    if ($.browser.msie) $('#rdregions #rddepth').css('background-position', GetDeptMargin(dept));
    else $('#rdregions #rddepth').css(GetDeptPos(dept));
}


function GetRegionPos(region) {
    var index = region - 1;
    var y = Math.floor(index / 11);
    var x = index - y * 11;

    return { 'left': (x * -350) + 'px', 'top': (y * -372) + 'px' };
}


function GetRegionMargin(region) {
    var index = region - 1;
    var y = Math.floor(index / 11);
    var x = index - y * 11;
        
    return (x * -350) + ' ' + (y * -372);
    //return (index * -350) + ' -' + y;
}


function GetDeptMargin(dept) {
    var index = dept - 1;

    if (dept == 202) index = 95;
    else if (dept == 201) index = 94;
    else if (dept > 20) index--;

    var y = Math.floor(index / 10);
    var x = index - y * 10;

    return (x * -350) + ' ' + (y * -372);
}


function GetDeptPos(dept) {
    var index = dept - 1;

    if (dept == 202) index = 95;
    else if (dept == 201) index = 94;
    else if (dept > 20) index--;

    var y = Math.floor(index / 10);
    var x = index - y * 10;

    return { 'left': (x * -350) + 'px', 'top': (y * -372) + 'px' };
}