﻿var RoomPlacementManager =
{
    _tableId: null,
    _roomCountDropDownid: null,

    Init: function(tableId, roomCountDropDownId)
    {
        this._tableId = tableId;
        this._roomCountDropDownId = roomCountDropDownId;

        RoomPlacementManager.SetupRooms();
    },

    SetupRooms: function()
    {
        var $dropDown = $("#" + RoomPlacementManager._roomCountDropDownId).change(RoomPlacementManager.SetRoomContainersVisibility);
        //RoomPlacementManager.SetRoomContainersVisibility.call($dropDown[0]);
    },

    SetRoomContainersVisibility: function() {
		var $this = $(this),
			newRoomCount = $this.val();
		
		$('.RoomContainer', $this.closest('.RoomsPlacementPanel')).each(function(index) {

            var currentRoomNumber = index + 1;
            currentRoomNumber > newRoomCount ? $(this).hide() : $(this).show();
        });
    }
}

var ChildAgeValidator =
{
    Validate: function(source, arguments)
    {
        if ($(source).parent().find(".ChildAges:visible").length > 0)
        {
            arguments.IsValid = Sembo.Validations.validateChildAges(parseInt(arguments.Value), $(source).parent().find(".ChildAges").first().val());
        }
        else
        {
            arguments.IsValid = true;
        }
    }
};

var MaxTravellerCountValidator =
{
    _maxTravellerCount: 0,
    _roomPlacementControlSelector: null,
    _errorMessage: null,

    Init: function (maxTravellerCount, roomPlacementControlSelector, errorMessage) {
        this._maxTravellerCount = maxTravellerCount;
        this._roomPlacementControlSelector = roomPlacementControlSelector;
        this._errorMessage = errorMessage;

        $(this._roomPlacementControlSelector).find(".AdultCountDropDown, .NumberOfChildrenDropDown").change(function () {
            var travellerCount = MaxTravellerCountValidator.GetTotalTravellerCount();

            if (travellerCount > MaxTravellerCountValidator._maxTravellerCount) 
            {
                alert(MaxTravellerCountValidator._errorMessage + MaxTravellerCountValidator._maxTravellerCount);
            }
        });
    },

    Validate: function (source, arguments) {
        var travellerCount = MaxTravellerCountValidator.GetTotalTravellerCount();
        arguments.IsValid = travellerCount <= MaxTravellerCountValidator._maxTravellerCount;
    },

    GetTotalTravellerCount: function () {
        var travellerCount = 0;
        $(document).find(".AdultCountDropDown:visible, .NumberOfChildrenDropDown:visible").each(function (index, element) {
            travellerCount += parseInt($(element).val());
        });

        return travellerCount;
    }
};



