﻿ $(document).ready(function(){
   $("#monthup").click(function () { 
       if ($('select#month option:selected').val() == 12){
            if ($('select#year')[0].selectedIndex == 0){
                $('select#year')[0].selectedIndex++;
                $('select#month')[0].selectedIndex = 0;    
            }
       }else{
            $('select#month')[0].selectedIndex++;
       }
       //change_date();
       month_change();
    });
   $("#monthdn").click(function () {
     if ($('select#month')[0].selectedIndex > 0 || $('select#year')[0].selectedIndex > 0){
        if ($('select#month')[0].selectedIndex == 0){
            if($('select#year')[0].selectedIndex == 1){
                $('select#year')[0].selectedIndex--;            
                $('select#month')[0].selectedIndex = 11;
            }    
        }else{
            $('select#month')[0].selectedIndex--;
        }
        //change_date();
        month_change();
    }
   });
   
   $("#duration").change(function () {
     set_dates($('select#day option:selected').val() + $('select#month option:selected').val() + $('select#year option:selected').val());
   });
   
   $("#year").change(function () {
        year_change();
    });
   
   $("#month").change(function () {
        month_change();    
   });
   $("#day").change(function () {
        day_change();
   });

    $("#room").change(function () {
        //$.get('/WebServices/getRoomImage.aspx',{room:$('select#room option:selected').val()},function(data) { 
        //$('#room_thumb').html(data);
        //day_change();
        room_update();
    //});
   });

    set_dates($('select#day option:selected').val() + $('select#month option:selected').val() + $('select#year option:selected').val());
    //change_date();
 });
 
 function day_change(iDay){
    set_dates($('select#day option:selected').val() + $('select#month option:selected').val() + $('select#year option:selected').val());
}

function change_room(iIndex) {
    document.getElementById('room').selectedIndex = iIndex;
    room_update();
}

 function month_change(){
    $.get('/WebServices/getNewDays.aspx',{day:$('select#day option:selected').val(), month:$('select#month option:selected').val(), year:$('select#year option:selected').val()},function(data) { 
        $('#day').html(data);
        day_change();
        //$('#day').change();
    });
 }
 function year_change(){
    $.get('/WebServices/getNewMonths.aspx',{month:$('select#month option:selected').val(), year:$('select#year option:selected').val()},function(data) { 
            $('#month').html(data);
            month_change();
            //$('#month').change();
    });
 }

 function room_update() {
    $.get('/WebServices/getRoomImage.aspx',{room:$('select#room option:selected').val()},function(data) { 
        $('#room_thumb').html(data);
        day_change();
    });
 }

 function change_date(){
    $('#calinfo').html('getting calendar information');
    $.get('/WebServices/getMonth.aspx',{month:$('select#month option:selected').val(), year:$('select#year option:selected').val(), room:$('select#room option:selected').val()},function(data) { 
            $('#calroot').html(data);
            $('#calinfo').html('');
        }
    );
 }
 
 function hover(objDate){
      $(objDate).addClass("over");
 }
 function hoverout(objDate){
      $(objDate).removeClass("over");
 }
 function set_dates(objDate) {

     //check availablility for all rooms not just the selected one.
     $('#calinfo').html('checking availablility');

     /*
     //loop through the array
     $('select#room option').each(function (index) {
         if (index + 1 == $('select#room option:selected').val()) {


             alert('SELECTED : ' + index + ': ' + $(this).text());
         } else {


             alert(index + ': ' + $(this).text());
         };
     });
     */

     //hide the continue to booking button
     $('#continue').hide();

    //post the date to the server
    
     $.get('/WebServices/checkAvailability.aspx', { room: $('select#room option:selected').val(), date: objDate, duration: $('select#duration option:selected').val() }, function (data) {
         var sRoomAvailable = '';
         if (data == "Available") {

             $.get('/WebServices/getMonth.aspx', { month: $('select#month option:selected').val(), year: $('select#year option:selected').val(), room: $('select#room option:selected').val(), date: objDate, duration: $('select#duration option:selected').val() }, function (data) {
                 $('#calroot').html(data);
                 sRoomAvailable = '<h4>' + $('select#room option:selected').text() + ' is available for your selected dates</h4>';


                 var startDay = Math.abs(document.getElementById('day').options[0].value);
                 //alert('startDay' + startDay);
                 $('select#day')[0].selectedIndex = Math.abs(objDate.substring(0, 2)) - startDay;
                 $('#calinfo').html(sRoomAvailable);

                 //show the continue to booking button
                 $('#continue').show();
             }
                );
         } else {
             $.get('/WebServices/getMonth.aspx', { month: $('select#month option:selected').val(), year: $('select#year option:selected').val(), room: $('select#room option:selected').val(), date: '', duration: '' }, function (data) {
                 $('#calroot').html(data);
             }
                );
             sRoomAvailable = '<h4>Sorry, ' + $('select#room option:selected').text() + ' is not available,<br />we have the following rooms available for your selected dates:</h4>';
             //check other availability
             $.get('/WebServices/checkOtherAvailability.aspx', { room: $('select#room option:selected').val(), date: objDate, duration: $('select#duration option:selected').val() }, function (data) {
                 $('#calinfo').html(sRoomAvailable + data);
             });
             //sRoomAvailable = '<h4>' + data + '</h4>';
             //$('#calinfo').html(data);
         }



     }
    ); 
 }
