/////////////////////////////////
///// Room display managing /////
/////////////////////////////////

// function used to update rooms display.
// NEEDED GLOBAL VARS : totalNbRooms
//
// nbroomsField : the room number field. Example: document.firstBookingForm.nbRooms
function initRooms(nbRoomsField) {
    initAllRoomsSelects();
    updateRoomsDisplay(nbRoomsField);
}

// function used to update rooms display.
// NEEDED GLOBAL VARS : totalNbRooms
//
// nbroomsField : the room number field. Example: document.firstBookingForm.nbRooms.
function updateRoomsDisplay(nbRoomsField) {
    var selectedNbrooms = parseInt(nbRoomsField.value, 10);
    displayRooms(selectedNbrooms, totalNbRooms);
}

///////////////////////////
//// Private functions ////
///////////////////////////

// util 'private' function used to display the correct number of rooms.
//
// displayedNb : the number of room to display
// totalNb : the total number of rooms
function displayRooms(displayedNb, totalNb) {
    // display first rooms
    for (var i = 1; i <= displayedNb; i++) {
        var eltId = "layerRoom" + i;
        document.getElementById(eltId).style.display = 'block';
        enableFields(document.getElementById("room_type_" + i),
                     document.getElementById("room_nb_adult_" + i),
                     document.getElementById("room_nb_child_" + i),
                     document.getElementById("room_nb_baby_" + i));
    }

    // hide last rooms
    for (var j = (displayedNb+1); j <= totalNb; j++) {
        var eltId = "layerRoom" + j;
        disableFields(document.getElementById("room_type_" + j),
                      document.getElementById("room_nb_adult_" + j),
                      document.getElementById("room_nb_child_" + j),
                      document.getElementById("room_nb_baby_" + j));
        document.getElementById(eltId).style.display = 'none';
    }
}

function updateChildrenAgesDisplay(nbChildrenField, roomIndex) {
    var nbChildren = parseInt(nbChildrenField.value, 10);
    if (nbChildren != 0) {
        document.getElementById("childrenAgesLayer" + roomIndex).style.display = 'block';
        for (var i = 0; i < nbChildren; i++) {
            document.getElementById("childAgeLayer" + roomIndex + i).style.display = 'inline';
            document.getElementById("childAge" + roomIndex + i).disabled = false;
        }
        for (var i = nbChildren; i < 4; i++) {
            document.getElementById("childAgeLayer" + roomIndex + i).style.display = 'none';
            document.getElementById("childAge" + roomIndex + i).disabled = true;
        }        
    } else {
        for (var i = 0; i < 4; i++) {
            document.getElementById("childAge" + roomIndex + i).disabled = true;
            document.getElementById("childAgeLayer" + roomIndex + i).style.display = 'none';
        }
        document.getElementById("childrenAgesLayer" + roomIndex).style.display = 'none';
    }
}

// ******************************************************************************************** //
// ******************************************************************************************** //

//////////////////////////////
//// ROOM TYPES FUNCTIONS ////
//////////////////////////////

// Function used to validate all rooms fields
// NEEDED GLOBAL VARS : totalNbRooms
//                      roomErrorTitleGlobal (with 1 argument),
//                      adultNotEqualsErrorMsgGlobal, childNotEqualsErrorMsgGlobal, babyNotEqualsErrorMsgGlobal  (with 1 argument)
//                      adultNotBetweenErrorMsgGlobal, childNotBetweenErrorMsgGlobal, babyNotBetweenErrorMsgGlobal  (with 2 arguments)
//
// return true if rooms validation is ok ; false otherwise
function validateRooms() {
    var msg = "";
    
    for (var j = 1; j <= totalNbRooms; j++) {
        msg = msg + validateRoomFromIndex(j,
                                          roomErrorTitleGlobal,
                                          adultNotEqualsErrorMsgGlobal, adultNotBetweenErrorMsgGlobal,
                                          childNotEqualsErrorMsgGlobal, childNotBetweenErrorMsgGlobal,
                                          babyNotEqualsErrorMsgGlobal, babyNotBetweenErrorMsgGlobal);
    }
    
    if (msg.length != 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}


// Function used to initialize all rooms fields
// NEEDED GLOBAL VARS : totalNbRooms, preSelectedRoomTypeCode_%, preSelectedAdultNb_%, preSelectedChildNb_%, preSelectedBabyNb_%.
function initAllRoomsSelects() {
    for (var j = 1; j <= totalNbRooms; j++) {
        initRoomCombinationsSelects(document.getElementById("room_type_" + j),
                                    document.getElementById("room_nb_adult_" + j),
                                    document.getElementById("room_nb_child_" + j),
                                    document.getElementById("room_nb_baby_" + j),
                                    eval("preSelectedRoomTypeCode_" + j),
                                    eval("preSelectedAdultNb_" + j),
                                    eval("preSelectedChildNb_" + j),
                                    eval("preSelectedBabyNb_" + j));
    }
}



// Function used to initialize all selectboxes for one room
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
// preSelectedRoomTypeCode : the preselected room type value.
// preSelectedAdultNb : the preselected adult nb.
// preSelectedChildNb : the preselected child nb.
// preSelectedBabyNb : the preselected baby nb.
function initRoomCombinationsSelects(roomTypeField, adultNbField, childNbField, babyNbField, preSelectedRoomTypeCode, preSelectedAdultNb ,preSelectedChildNb ,preSelectedBabyNb) {
    initRoomTypeSelect(roomTypeField, preSelectedRoomTypeCode);
    initPassengerSelects(roomTypeField, adultNbField, childNbField, babyNbField, preSelectedAdultNb ,preSelectedChildNb, preSelectedBabyNb);
}


// Default init of prodRoomTypeCodeArray
var prodRoomTypeCodeArray = null;

// Function used to initialize the room type selectbox with global vars.
//
// roomTypeField : the roomType field.
// preSelectedRoomTypeCode : the preselected room type value.
function initRoomTypeSelect(roomTypeField, preSelectedRoomTypeCode) {
    roomTypeField.options.length = 0;
    
    var preselectedIndex = -1;
    var doubleIndex = -1;
    var doubleCode = roomTypeCodeArray[1];
    
    var i;
    for (i=0; i < roomTypeCodeArray.length; i++) {
        if (prodRoomTypeCodeArray == null || isInArray(prodRoomTypeCodeArray, roomTypeCodeArray[i])) {
            roomTypeField.options[roomTypeField.length] = new Option(eval("roomTypeTitleGlobal_" + roomTypeCodeArray[i]), roomTypeCodeArray[i]);
            if (roomTypeCodeArray[i] == preSelectedRoomTypeCode) {
                preselectedIndex = roomTypeField.length - 1;
            }
            if (roomTypeCodeArray[i] == doubleCode) {
                doubleIndex = roomTypeField.length - 1;
            }
        }
    }
    
    if (preselectedIndex == -1) {
      if (doubleIndex != -1) {
        preselectedIndex = doubleIndex;
      } else {
        preselectedIndex = 0;
      }
    }
    
    roomTypeField.selectedIndex = preselectedIndex;
}

// Function used to initialize the passenger number selectboxes with global vars.
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
// preSelectedAdultNb : the preselected adult nb.
// preSelectedChildNb : the preselected child nb.
// preSelectedBabyNb : the preselected baby nb.
function initPassengerSelects(roomTypeField, adultNbField, childNbField, babyNbField, preSelectedAdultNb ,preSelectedChildNb ,preSelectedBabyNb) {
    // get roomtypeIndex
    var roomTypeCodeIndex = getRoomTypeIndex(roomTypeField);

    // init adult nb field
    var adultIndex = buildPassengerSelect(roomTypeCodeIndex, adultNbField, adultMinNbArray, adultMaxNbArray, false, preSelectedAdultNb);
    adultIndex = adultIndex == -1 ? 0 : adultIndex;

    // init child nb field
    var childIndex = buildPassengerSelect(roomTypeCodeIndex, childNbField, childMinNbArray, childMaxNbArray, false, preSelectedChildNb);
    childIndex = childIndex == -1 ? 0 : childIndex;

    // init adult nb field
    // var babyIndex = buildPassengerSelect(roomTypeCodeIndex, babyNbField, babyMinNbArray, babyMaxNbArray, false, preSelectedBabyNb);
    // babyIndex = babyIndex == -1 ? 0 : babyIndex;
    
    // preselected values
    adultNbField.selectedIndex = adultIndex;
    childNbField.selectedIndex = childIndex;
    // babyNbField.selectedIndex = babyIndex;
}

// function used to update passenger nb fields (after a roomtype change) from roomIndex.
//
// roomIndex : the room index in page.
function updateRoomPassengerSelects(roomIndex) {
    updatePassengerSelects(document.getElementById("room_type_" + roomIndex),
                           document.getElementById("room_nb_adult_" + roomIndex),
                           document.getElementById("room_nb_child_" + roomIndex),
                           document.getElementById("room_nb_baby_" + roomIndex));
}

// function used to update passenger nb fields (after a roomtype change)
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
function updatePassengerSelects(roomTypeField, adultNbField, childNbField, babyNbField) {
    // get roomtypeIndex
    var roomTypeCodeIndex = getRoomTypeIndex(roomTypeField);

    // get previously setted values
    var currentAdultNb = adultNbField.options[adultNbField.selectedIndex].value;
    var currentChildNb = childNbField.options[childNbField.selectedIndex].value;
    // var currentBabyNb = babyNbField.options[babyNbField.selectedIndex].value;

    var preselectedIsPossible = true;

    // init adult nb field
    var adultIndex = buildPassengerSelect(roomTypeCodeIndex, adultNbField, adultMinNbArray, adultMaxNbArray, false, currentAdultNb);
    preselectedIsPossible = preselectedIsPossible & (adultIndex != -1);

    // init child nb field
    var childIndex = buildPassengerSelect(roomTypeCodeIndex, childNbField, childMinNbArray, childMaxNbArray, false, currentChildNb);
    preselectedIsPossible = preselectedIsPossible & (childIndex != -1);

    // init adult nb field
    // var babyIndex = buildPassengerSelect(roomTypeCodeIndex, babyNbField, babyMinNbArray, babyMaxNbArray, false, currentBabyNb);
    // babyIndex = babyIndex == -1 ? 0 : babyIndex;
    // preselectedIsPossible = preselectedIsPossible & (babyIndex != -1);
    
    // preselected values
    if (preselectedIsPossible) {
        adultNbField.selectedIndex = adultIndex;
        childNbField.selectedIndex = childIndex;
        // babyNbField.selectedIndex = babyIndex;
    } else {
        adultNbField.selectedIndex = 0;
        childNbField.selectedIndex = 0;
        // babyNbField.selectedIndex = 0;
    }
}

///////////////////////////
//// Private functions ////
///////////////////////////

// Util 'private' function used to validate a room using its room index.
//
// roomIndex : the room index in page.
// roomErrorTitle : the message title to display if room validation failed.
// adultEqualsErrorMsg, adultBetweenErrorMsg : error messages to display if adult nb validation failed.
// childEqualsErrorMsg, childBetweenErrorMsg : error messages to display if child nb validation failed.
// babyEqualsErrorMsg, babyBetweenErrorMsg : error messages to display if baby nb validation failed.
//
// return the eventual error msg (empty if no pb).
function validateRoomFromIndex(roomIndex,
                               roomErrorTitle,
                               adultEqualsErrorMsg, adultBetweenErrorMsg,
                               childEqualsErrorMsg, childBetweenErrorMsg,
                               babyEqualsErrorMsg, babyBetweenErrorMsg) {
    var msg = validateRoomFields(document.getElementById("room_type_" + roomIndex),
                                 document.getElementById("room_nb_adult_" + roomIndex),
                                 document.getElementById("room_nb_child_" + roomIndex),
                                 document.getElementById("room_nb_baby_" + roomIndex),
                                 adultEqualsErrorMsg, adultBetweenErrorMsg,
                                 childEqualsErrorMsg, childBetweenErrorMsg,
                                 babyEqualsErrorMsg, babyBetweenErrorMsg);
    if (msg.length != 0) {
        msg = getArgMessage(roomErrorTitle, String(roomIndex), null, null, null) + "\n" + msg;
    }
    return msg;
}

// Util 'private' function used to validate all room fields.
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
// adultEqualsErrorMsg, adultBetweenErrorMsg : error messages to display if adult nb validation failed.
// childEqualsErrorMsg, childBetweenErrorMsg : error messages to display if child nb validation failed.
// babyEqualsErrorMsg, babyBetweenErrorMsg : error messages to display if baby nb validation failed.
//
// return the eventual error msg (empty if no pb).
function validateRoomFields(roomTypeField,
                            adultNbField,
                            childNbField,
                            babyNbField,
                            adultEqualsErrorMsg, adultBetweenErrorMsg,
                            childEqualsErrorMsg, childBetweenErrorMsg,
                            babyEqualsErrorMsg, babyBetweenErrorMsg) {

    // get roomtypeIndex
    var roomTypeCodeIndex = getRoomTypeIndex(roomTypeField);
    
    // check adults nb
    var msg = validatePassengerNb(roomTypeCodeIndex, adultNbField, adultMinNbArray, adultMaxNbArray, adultEqualsErrorMsg, adultBetweenErrorMsg);
    
    // check children nb
    msg = msg + validatePassengerNb(roomTypeCodeIndex, childNbField, childMinNbArray, childMaxNbArray, childEqualsErrorMsg, childBetweenErrorMsg);
   

    // check babies nb
    // msg = msg + validatePassengerNb(roomTypeCodeIndex, babyNbField, babyMinNbArray, babyMaxNbArray, babyEqualsErrorMsg, babyBetweenErrorMsg); 


    return msg;
}

// Util 'private' function used to validate a passenger nb field
//
// roomTypeCodeIndex : the index in roomTypeCodeArray of the selected roomType
// nbPassengerField : the passenger (adult, children or baby) nb selectbox
// minArray : the Array with min values
// maxArray : the Array with max values
// errorEqualsMsg : the message error to display if a "not equals" validation failed.
// errorBetweenMsg : the message error to display if a "not between" validation failed.
//
// return the eventual error msg (empty if no pb).
function validatePassengerNb(roomTypeCodeIndex, nbPassengerField, minArray, maxArray, errorEqualsMsg, errorBetweenMsg) {
    // get min-max
    var minValue = minArray[roomTypeCodeIndex];
    var maxValue = maxArray[roomTypeCodeIndex];

    // get value
    var currentNb = parseInt(nbPassengerField.options[nbPassengerField.selectedIndex].value, 10);
    
    var isOk = (minValue <= currentNb) && (currentNb <= maxValue);
    var isEqualsMsg = (minValue == maxValue);
    
    var msg = "";
    if (!isOk) {
        msg = isEqualsMsg ? getArgMessage(errorEqualsMsg, String(minValue), null, null, null)
                          : getArgMessage(errorBetweenMsg, String(minValue), String(maxValue), null, null);
        msg = msg + "\n";
    }
    return msg;
}

// Util 'private' function used to disable all rooms fields
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
function disableFields(roomTypeField, adultNbField, childNbField, babyNbField) {
    roomTypeField.disabled = true;
    adultNbField.disabled = true;
    childNbField.disabled = true;
    // babyNbField.disabled = true;
}

// Util 'private' function used to enable all rooms fields
//
// roomTypeField : the roomType field.
// adultNbField : the adult number field.
// childNbField : the child number field.
// babyNbField : the baby number field.
function enableFields(roomTypeField, adultNbField, childNbField, babyNbField) {
    roomTypeField.disabled = false;
    adultNbField.disabled = false;
    childNbField.disabled = false;
    // babyNbField.disabled = false;
}

// Util 'private' function used to build a passenger (adult, children or baby) nb selectbox
//
// roomTypeCodeIndex : the index in roomTypeCodeArray of the selected roomType
// nbPassengerField : the passenger (adult, children or baby) nb selectbox
// minArray : the Array with min values
// maxArray : the Array with max values
// putZero : a boolean indicating if the value 0 is added to the select
// preSelectedValue : the asked preselectValue
//
// return the index of the preselected value if found ; -1 if not found.
function buildPassengerSelect(roomTypeCodeIndex, nbPassengerField, minArray, maxArray, putZero, preSelectedValue) {

    // get min-max
    var minValue = minArray[roomTypeCodeIndex];
    var maxValue = maxArray[roomTypeCodeIndex];
    
    // Reinit list
    nbPassengerField.options.length = 0;

    // build list
    var preselectedIndex = -1;
    if (putZero && minValue > 0) {
        nbPassengerField.options[nbPassengerField.length] = new Option(0, 0);    
    }

    var i;
    for (i = minValue; i <= maxValue; i++) {
        nbPassengerField.options[nbPassengerField.length] = new Option(i, i);    
        if (i == preSelectedValue) {
            preselectedIndex = i - minValue;
        }
    }

    return preselectedIndex;
}

// Util 'private' function used to get the index in roomTypeCodeArray of the selected roomType
//
// roomTypeField : the roomType field.
function getRoomTypeIndex(roomTypeField) {
    var roomTypeCodeValue = roomTypeField.options[roomTypeField.selectedIndex].value;

	// get room type code index
    var roomTypeCodeIndex = 0;
    var roomTypeIndexFound = false;
    var i;
    for (i=0; i < roomTypeCodeArray.length && !roomTypeIndexFound; i++) {
        if (roomTypeCodeArray[i] == roomTypeCodeValue) {
            roomTypeCodeIndex = i;
        }
    }
    
    return roomTypeCodeIndex;
}