$(document).ready(function(){

    $('#checkin_dateControl').datepicker({
        dateFormat: 'dd-mm-yy',
        minDate: 0,
        onSelect: function(){
        
            var date1 = $('#checkin_dateControl').datepicker('getDate');
            var date = new Date( Date.parse( date1 ) );
            date.setDate( date.getDate() + 3 );

            var newDate = date.toDateString();
            newDate = new Date( Date.parse( newDate ) );

            $('#checkout_dateControl').datepicker('setDate', newDate );
        }
    }
    );

    $('#checkout_dateControl').datepicker({
        dateFormat: 'dd-mm-yy',
        minDate: 0
    }
    );

    $('#checkinDateButtonControl').click(
        function(){
            $('#checkin_dateControl').datepicker('show');
        }
        );

    $('#checkoutDateButtonControl').click(
        function(){
            $('#checkout_dateControl').datepicker('show');
        }
        );

    /* Input field functionality */
    $('#selectAdultsControl').focus(function(){
        if($('#selectAdultsControl').val()== "Adults")
        {
            $('#selectAdultsControl').val("");
        }
    });

    $('#selectAdultsControl').blur(function(){
        if($('#selectAdultsControl').val()== "")
        {
            $('#selectAdultsControl').val("Adults");
        }
    });

    $('#selectRoomsControl').focus(function(){
        if($('#selectRoomsControl').val()== "Rooms")
        {
            $('#selectRoomsControl').val("");
        }
    });

    $('#selectRoomsControl').blur(function(){
        if($('#selectRoomsControl').val()== "")
        {
            $('#selectRoomsControl').val("Rooms");
        }
    });

    $('#errorMessageRow').hide();
    /*End input field functionality*/

    /*Validation*/
    $('#btnSubmit').click(function(){
        if (isNaN($('#selectAdultsControl').val())!= true && isNaN($('#selectRoomsControl').val()) != true)
        {
            var checkinDateArray = $('#checkin_dateControl').val().split('-');
            var checkoutDateArray = $('#checkout_dateControl').val().split('-');

            var startDate = new Date(checkinDateArray[2],checkinDateArray[1],checkinDateArray[0]);
            var endDate = new Date(checkoutDateArray[2],checkoutDateArray[1],checkoutDateArray[0]);
            
            if (startDate != "Invalid Date"
                && endDate != "Invalid Date")
                {

                if (startDate < endDate)
                {
                    $('#errorMessageRowControl').hide();
                    $('#frmSubmit').submit();
                }
                else
                {
                    $('#errorMessageDivControl').html("Start date must be less than the End date")
                    $('#errorMessageRowControl').show();
                }
            }
            else
            {
                // alert(new Date($('#checkin_date').val()));
                $('#errorMessageDivControl').html("Checkin and Checkout dates must be valid.")
                $('#errorMessageRowControl').show();
            }
        }
        else
        {
            $('#errorMessageDivControl').html("Number of Adults and Rooms must be a valid number.")
            $('#errorMessageRowControl').show();
        }
    }
    );
/*End validation*/

});