
var url;
    
var debug = 0;

function clearNoOfRooms() {
       
    var noOfRoomsChecks = document.getElementsByName('noOfRooms');
    
    var count = noOfRoomsChecks.length;
 
    // Uncheck all the number of rooms check boxes.

    for (i=0; i < count; i++) {

        noOfRoomsChecks[i].checked = false;
    }  
}

function clearFacilities() {

    var facilitiesChecks = document.getElementsByName('selectedFacility');
    
    var count = facilitiesChecks.length;
      
    if (debug == 1) {
        alert("clear facilities"); 
    }
    
    // Uncheck all the facilities check boxes.

    for (i=0; i < count; i++) {

        facilitiesChecks[i].checked = false;
    }  
} 
   
function showHideFacilities() {

    if (document.getElementById('advancedBrowseTable').style.display == "none") {
    
        document.getElementById('facilityDropDown').src = '/images/assets/advanced_search_close.gif';
        document.getElementById('advancedBrowseTable').style.display = 'inline';
    }
    else {
    
        document.getElementById('facilityDropDown').src = '/images/assets/advanced_search_open.gif';
        document.getElementById('advancedBrowseTable').style.display = 'none';        
    }
}

function createSearchIframe() {

    var searchIframe = document.createElement('iframe');
        searchIframe.id = 'searchIframe';
        searchIframe.name = 'searchIframe';
        searchIframe.src = url;
        searchIframe.style.display = 'none';

    document.body.appendChild(searchIframe);
}

function destroySearchIframe() {

    if (document.getElementById("searchIframe")) {
        document.body.removeChild(document.getElementById('searchIframe'));
    }
}
	
function typeChange() {

    var optionSelected = document.getElementById('type');

    var selectedIndex = optionSelected.selectedIndex;

    var typeId = optionSelected[selectedIndex].value;

    // Clear the No of rooms and Facilities check boxes

    clearNoOfRooms();
    clearFacilities();

    // Hotel - hide the number of rooms check boxes.

    if (typeId == 68) {  

        document.getElementById('noOfRoomsChecks1').style.display = 'none';
        document.getElementById('noOfRoomsChecks2').style.display = 'none';
        document.getElementById('noOfRoomsChecks3').style.display = 'none';
    }
    else {
    
        document.getElementById('noOfRoomsChecks1').style.display = 'inline';
        document.getElementById('noOfRoomsChecks2').style.display = 'inline';
        document.getElementById('noOfRoomsChecks3').style.display = 'inline';
    }

    resortChange();

    url = "/common/searchDisplayDataPushlet.jsp?command="+"facilityChange"+"&typeId="+escape(typeId);

    createSearchIframe(url);
}

function areaChange() {

    var optionSelected = document.getElementById("area");

    var selectedIndex = optionSelected.selectedIndex;

    if (debug == 1) {
        alert("DEBUG: Area change: " + selectedIndex);
    }

    // Clear the No of rooms and Facilities check boxes
    
    clearNoOfRooms();
    clearFacilities();

    var areaId = optionSelected[selectedIndex].value;
        
    // Clear the accommodation list and populate with the default option.
    
    if (document.forms['search_form'].accommodationId.options.length > 1) {

        document.forms['search_form'].accommodationId.options.length = 0;

        document.forms['search_form'].accommodationId.options[0] = new Option('-- Accommodation --','-1');
    }

    // Prevent notification of change - retieval of resorts
    // if the first option, "-- Area -- " is selected.

    if (areaId == -1) {

        // First / default option selected.  Clear the resort 
        // list and populate with the default option.

        if (document.forms['search_form'].resortId.options.length > 1) {

            document.forms['search_form'].resortId.options.length = 0;

            document.forms['search_form'].resortId.options[0] = new Option('-- Resort --','-1');
        }
    }
    else {

        url = "/common/searchDisplayDataPushlet.jsp?command="+"areaChange"+"&areaId=" + escape(areaId);

        createSearchIframe(url);
    }
}

function resortChange() {

    var resortSelect = document.getElementById("resort");

    var resortIndex = resortSelect.selectedIndex;

    if (debug == 1) {
        alert("DEBUG: Resort change: " + resortIndex);
    }

    // Clear the No of rooms and Facilities check boxes

    clearNoOfRooms();
    clearFacilities();

    var resortId = resortSelect[resortIndex].value;

    // Prevent notification of change - retieval of resorts
    // if the first option, "-- Resort -- " is selected.

    if (resortId == -1) {

        // First / default option selected.  Clear the
        // accommodation list and populate with the default 
        // option.

        if (document.forms['search_form'].accommodationId.options.length > 1) {

            document.forms['search_form'].accommodationId.options.length = 0;

            document.forms['search_form'].accommodationId.options[0] = new Option('-- Accommodation --','-1');
        }
    }
    else {
    
        var typeSelect = document.getElementById("type");

        var typeIndex = typeSelect.selectedIndex;

        if (debug == 1) {
            alert("DEBUG: Type index: " + typeIndex);
        }

        var typeId = typeSelect[typeIndex].value;

        if (debug == 1) {
            alert("DEBUG: Type id: " + typeId);
        }

        url = "/common/searchDisplayDataPushlet.jsp?command="+"resortChange"+"&resortId="+ escape(resortId)+"&typeId="+escape(typeId);

        createSearchIframe(url);
    }
}

function retrievedResorts(resorts) {

    if (debug == 1) {
        alert("DEBUG: Retrieved resort");

        alert("DEBUG: select list length: " + 
        document.forms['search_form'].resortId.options.length);
    }

    var arrayLength = resorts.length;

    if (debug == 1) {
        alert ("Array length: " + arrayLength);
    }

    var index = 0;
    var resortName;
    var resortId;

    document.forms['search_form'].resortId.options.length = 0;

    document.forms['search_form'].resortId.options[0] = new Option('-- Resort --','-1');

    for (i=0; i < (arrayLength / 2); i++) {

        resortName = resorts[index++];
        resortId = resorts[index++];

        if (debug == 1) {
            alert("Resort name: " + resortName + " index: " + resortId);
        }

        document.forms['search_form'].resortId.options[i+1] = new Option(resortName, resortId);
    }

    destroySearchIframe();
}

function accommodationChange() {

    // Clear the No of rooms and Facilities check boxes

    clearNoOfRooms();
    clearFacilities();
}

function retrievedAccommodation(accommodation) {

    if (debug == 1) {
        alert("DEBUG: Retrieved accommodation");

        alert("DEBUG: select list length: " + 
        document.forms['search_form'].accommodationId.options.length);
    }

    var arrayLength = accommodation.length;

    if (debug == 1) {
        alert ("Array length: " + arrayLength);
    }

    var index = 0;
    var accomName;
    var accomId;

    document.forms['search_form'].accommodationId.options.length = 0;

    document.forms['search_form'].accommodationId.options[0] = new Option('-- Accommodation --','-1');

    for (i=0; i < (arrayLength / 2); i++) {

        accomName = accommodation[index++];
        accomId = accommodation[index++];

        if (debug == 1) {
            alert("Accommodation name: " + accomName + " index: " + accomId);
        }

        document.forms['search_form'].accommodationId.options[i+1] = new Option(accomName, accomId);
    }

    destroySearchIframe();
}

function retrievedFacilities(facilities) {

    if (debug == 1) {
        alert("DEBUG: Retrieved facilities");

        alert("DEBUG: Checkbox length: " + 
        document.forms['search_form'].selectedFacility.length);
    }

    var arrayLength = facilities.length;

    if (debug == 1) {
        alert ("Array length: " + arrayLength);
    }

    var index = 0;
    var facilityName;
    var facilityId;

    removeCheckBoxes();

    var parentObj = document.getElementById('advancedBrowseTable');

    var check;
    var span;
    var spanTxt;
    var br;

    for (i=0; i < (arrayLength / 2); i++) {

        facilityName = facilities[index++];
        facilityId = facilities[index++];

        if (debug == 1) {
            alert("Facility name: " + facilityName + " index: " + facilityId);
        }

        check = document.createElement('input');
        check.setAttribute('type', 'checkbox');
        check.setAttribute('id', 'selectedFacility');
        check.setAttribute('name', 'selectedFacility');
        check.setAttribute('value', facilityId);

        parentObj.appendChild(check);

        span = document.createElement('span');
        span.style.fontSize = '10px';
        spanTxt = document.createTextNode(facilityName);
        span.appendChild(spanTxt);

        br = document.createElement('br');

        parentObj.appendChild(span);
        parentObj.appendChild(br);            
    }

    destroySearchIframe();
}


function removeCheckBoxes() {

    if (debug == 1) {
        alert("DEBUG: Remove checkboxes");
    }

    var parentObj = document.getElementById('advancedBrowseTable');

    while (parentObj.hasChildNodes()) {
        parentObj.removeChild(parentObj.childNodes[0]);
    }
}
        
