function jobTypeChangeEvent(form)
{
    // Build the salary range drop down list if possible
    buildsalaryRangeSelectControl(form);
}

function buildsalaryRangeSelectControl(thisForm)
{
    // Clear the select control
    clearSelectControl(thisForm.salaryRange);

    // Get the selected parent value (country)
    var selectedParentValueCountry;
    selectedParentValueCountry = thisForm.homeCountryId.value;

    // Get the selected parent value (jobType)
    var selectedParentIndexJobType, selectedParentValueJobType;
    selectedParentIndexJobType = thisForm.jobTypeIds.selectedIndex;
    selectedParentValueJobType = thisForm.jobTypeIds.options[selectedParentIndexJobType].value;

    // If a jobtype and country hasn't been selected then the list can't be built
    if (selectedParentIndexJobType == 0) {
        var newOp = new Option(thisForm.selectJobTypeMessage.value, '0', true, true);
        thisForm.salaryRange.options[thisForm.salaryRange.length] = newOp;
        return;
    }

    // The key used to find the index in the salaryRangeOptions array is a combination
    // of the countryId and the jobtypeId
    var parentKey = selectedParentValueCountry + "-" + selectedParentValueJobType;

    // Search for the key in the parent array list to try and find it's index
    // that will be used in the children array.
    var found = false;
    var childrenIndex;
    for (var i=0; i < countryJobType.length && !found; i++) {
        if (countryJobType[i] == parentKey) {
            found = true;
            childrenIndex = i;
        }
    }

    // Loop through the parent's children array and add each one as an option
    // to the select control
//alert("children index: " + childrenIndex);
//alert("what's at: " + salaryRangeOptions[childrenIndex]);
//alert("one: " + salaryRangeOptions[0]);
//alert("three: " + salaryRangeOptions[2]);
    if (found && salaryRangeOptions[childrenIndex].length > 0) {

// enable the child
         thisForm.salaryRange.disabled = false;

        // Add any pre options that are to be added to each children list
        var newOp = new Option(thisForm.availableSalaryRangesMessage.value, '0', true, true);
        thisForm.salaryRange.options[thisForm.salaryRange.length] = newOp;
        // Loop through the parent's children array and add each one as an option
        // to the select control
//alert ("childrenindex is: " + salaryRangeOptions[childrenIndex].length);
        for (var i=0; i < salaryRangeOptions[childrenIndex].length; i++) {
//alert ("i is: " + i);

//alert("boo3A" + salaryRangeOptions[childrenIndex][i][0]);
            var newOp = new Option(salaryRangeOptions[childrenIndex][i][0], salaryRangeOptions[childrenIndex][i][1], false, false);
            thisForm.salaryRange.options[thisForm.salaryRange.length] = newOp;
        }
//alert("boo4");
    }
    else {

        // Add a reason for the user to explain why there are no salaries
        var newOp = new Option(thisForm.noSalaryRangeAvailable.value, '-2', true, true);
        thisForm.salaryRange.options[thisForm.salaryRange.length] = newOp;

        // Disable the child
        thisForm.salaryRange.disabled = false;
        thisForm.salaryRange.selectedIndex = 0;
    }
}


function buildSubSpecialisationSelectControl(thisForm) {

    // Clear the select control
    clearSelectControl(thisForm.subSpecialisations);

    // Get the selected parent value
    var selectedParentIndex, selectedParentValue;
    selectedParentValue = thisForm.parentSpecialisationIds.value;

    if( selectedParentValue == undefined ) return;

    // Search for the value in the parent array list to try and find it's index
    // that will be used in the children array. This needs to be done instead of
    // simply using the parent index incase a 'please select....' option has been added
    // to the parent control
    var found = false;
    var childrenIndex;
    for (var i=0; i < subSpecialisationsKey.length && !found; i++) {
        if (subSpecialisationsKey[i] == selectedParentValue) {
            found = true;
            childrenIndex = i;
        }
    }

        // Add any pre options that are to be added to each children list
    var newOp = new Option('Any Sub Specialisation', '0', true, true);
    thisForm.subSpecialisations.options[thisForm.subSpecialisations.length] = newOp;

    // Loop through the parent's children array and add each one as an option
    // to the select control
    if (found) {
        // enable the child
         thisForm.subSpecialisations.disabled = false;

        // Loop through the parent's children array and add each one as an option
        // to the select control
//alert(subSpecialisationsOptions[childrenIndex].length-1);
        var lengthy = subSpecialisationsOptions[childrenIndex].length-1;
        for (var i=0; i <= lengthy; i++) {
         var newOp = new Option(subSpecialisationsOptions[childrenIndex][i][0], subSpecialisationsOptions[childrenIndex][i][1], false, false);
         thisForm.subSpecialisations.options[thisForm.subSpecialisations.length] = newOp;
        }
    }
    else {
        // Disable the child
         thisForm.subSpecialisations.disabled = true;
         thisForm.subSpecialisations.selectedIndex = -1;
    }
}

function clearSelectControl(controlObject) {
    controlObject.options.length = 0;
}