function get_calendar (month, year, room_type_id, op_id) {

    $A(document.getElementsByClassName('booking_calendar')).each(function(e){
        e.innerHTML = '';
    }
    );

    ajax = new Ajax.Updater(room_type_id + 'calendar','/ant/booking/calendar/' + year +'/' + month + '/' + room_type_id + '/' + op_id,
        {method:'get',onComplete:booking_form_onload} 
                )
//    $('id_room_type').value = room_type_id;
}

var origin = 'nun';
	
//TODO create a booking validation handler
var r_dates = new Array()

function reserve_room_day(e){
    if ( e.className.indexOf('booked') != -1)    {
        //alert(  "Room is booked for that day" );
        return false;
    } else if ( e.className.indexOf('reserve') != -1){
        e.className = 'available';
    } else {
        e.className = 'reserve'; 
        if (r_dates.length == 0){
            r_dates.push(e.id) ;
        } else {
            //determine if the date given is before or after the
            //current first date

            ds = r_dates[0].split('_');
            dg = e.id.split('_');

            ds_date = new Date();
            ds_date.setFullYear(ds[2],ds[1]-1,ds[0]);
            dg_date = new Date();
            dg_date.setFullYear(dg[2],dg[1]-1,dg[0]);

            if (ds_date < dg_date){
                while (ds_date < dg_date){
                    id = ds_date.getDate() + '_' + (ds_date.getMonth() +1) + '_' + ds_date.getFullYear()

                    if (select_room_date(document.getElementById(id))){
                        r_dates.push(id);
                    } else {
                        alert (ds_date + " is booked!");
                        return false;
                    }
                    ds_date.setDate(ds_date.getDate() + 1);
                }
            }
        }
    }
}

function select_room_date(e){
    if ( e.className.indexOf('booked') != -1)    {
        //alert(  "Room is booked for that day" );
        return false;
    } else {
        e.className='reserve';
        return true;
    }
}

function get_room_date(form_prefix){
    if (!form_prefix){
    form_prefix = '';
    }

    //get the selected dates
    a_days = $A(document.getElementsByClassName('reserve'));

    if (a_days.length == 0){
        alert('No dates selected!');
        return false;
    }

    if ($(form_prefix + 'id_payment_method')){
    if ($(form_prefix + 'id_payment_method').value == ''){
        alert("Please Select A Payment Method");
        return false;
    }}

    start_date = new Date();
    ds = a_days.shift().id.split('_');

    start_date.setFullYear(ds[2],ds[1]-1,ds[0]);

    prev_date = start_date;
    nights = 1;
    while(a_days.length){
        ds = a_days.shift().id.split('_');
        mydate = new Date();
        mydate.setFullYear(ds[2],ds[1]-1,ds[0]);
        test_date = new Date();
        test_date.setFullYear(ds[2],ds[1]-1,ds[0]);
        test_date.setDate(test_date.getDate()-1);
        if (!(prev_date.getDate() == test_date.getDate())){
            alert ('Your booking nights must be consecutive!');
            return false;
        } else {
           nights ++; 
           prev_date = mydate;
        }


    }
    
    $(form_prefix + 'id_arrive_date').value = (start_date.getMonth() +1) + '/' + start_date.getDate() + '/' + start_date.getFullYear();
    $(form_prefix + 'id_nights').value = nights;

    
    if ($('id_minimum_stay').value > nights){
        alert('Minimum stay is ' +  $('id_minimum_stay').value + ' nights. Please Specify all nights you would like to stay. Please use the general contact form if you would like to enquire about a special length stay.'); 
        return false;
    }
    return true;
}

function booking_form_onload(){
//    if ($('id_room_type')){
//    $('id_room_type').parentNode.parentNode.style.display = 'none';
//    }
}

