/* 
 * Javascript addOn to aaaahotels.com which add search box to a-prague.com
 * @author Miroslav Koula
 */

var monthNames = {1:"January",2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"}
var dayNames = {0:'Sun', 1:'Mon', 2:'Tue', 3:'Wed', 4:'Thu', 5:'Fri', 6:'Sat'}
var today = new Date();
var tommorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate()+1);
//var today = new Date(2011,8,1);
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var dayInWeek = today.getDay();

function daysInMonth(iMonth, iYear){
    //console.log('month', iMonth);
    //console.log('year', iYear);
    return 32 - new Date(iYear, iMonth, 32).getDate();
}

function initDays(input, m, y) {
    
    var daysMonth = daysInMonth(m-1, y);
    //console.log(daysMonth);
    $(input).each(function(index) {
        $(this).removeAttr('disabled');
        
        dayDate = new Date(y, m-1, $(this).val());
        var clearToday = new Date(today.getFullYear(), today.getMonth(), today.getDate());
        
        if(dayDate < clearToday) {
            $(this).attr('disabled', 'disabled');
        }
                
        if((index+1) > daysMonth) {
            $(this).attr('disabled', 'disabled');
        }

        $(this).html(dayNames[dayDate.getDay()] + ' ' + $(this).val());
    });
}

function disableDays(input, year, month) {
    
    var myDate = new Date(year, month-1, 1);
    
    $( input.id + ' option').each(function() {
        if($(this).val() < today.getDate() && $('#checkin_year_month').val() == today.getFullYear() + '-' + getStrFromNum(today.getMonth()+1)) {
            $(this).attr('disabled', 'disabled');
        }
        
    });
}

function getStrFromNum(number) {
    var myStrMonth = number.toString();
    if(myStrMonth.length == 1) {
        myStrMonth = '0' + myStrMonth;
    }
    return myStrMonth;
}

function customRange(input) {
    if(input.id == "checkout_date") {
        var dc = $("#checkin_date").datepicker("getDate");
        if(dc) {
            var dd = {
                minDate: new Date(dc.getFullYear(), dc.getMonth(), dc.getDate()+1),
                maxDate: null
            };
            
            return dd;
        }
        else {
            var dd = {
                minDate: new Date(today.getFullYear(), today.getMonth(), today.getDate()+1),
                maxDate: null
            };
            
            return dd;
        }            
    }
    else if(input.id == "checkin_date") {
        var de = {
            minDate: today, 
            maxDate: null //$("#endDate").datepicker("getDate")
        };
        
        return de; 
    }
}

function fchangedDate(dateString, inst) {
    var chi = $("#checkin_date").datepicker("getDate");
    var chobc = $("#checkout_date").datepicker("getDate");
        
    if(inst.id == "checkin_date") {
        var de = null;
        if(chi >= chobc) {
            de = new Date(chi.getFullYear(), chi.getMonth(), chi.getDate())
            //console.log(de);
            de.setDate(chi.getDate()+1);
            if(de.getDate() == 1 && chi.getDate() != 1 && chi.getMonth() == 11)
            {
                de.setMonth(chi.getMonth()+1);
                de.setFullYear(chi.getFullYear()+1);
            }
            else if(de.getDate() == 1 && chi.getDate() != 1)
            {
                de.setMonth(chi.getMonth()+1);
            }
            
            var date_y = de.getFullYear();
            var date_m = (parseInt(de.getMonth())+1);
            var date_d = de.getDate();
        }
        else {
            de = chobc;
        }
        
        
        $('#checkin_day option').each(function() {

            initDays($('#checkin_day option'), chi.getMonth()+1, chi.getFullYear());

            if($(this).val() == chi.getDate()) {
                $(this).attr('selected', 'selected')
            }
            else {
                $(this).removeAttr('selected');
            }

            $('#checkin_year_month option').each(function() {
                if($(this).val() == chi.getFullYear() + '-' + getStrFromNum(chi.getMonth()+1)) {
                    $(this).attr('selected', 'selected');
                }
                else {
                    $(this).removeAttr('selected');
                }
            });

        });
        
        $("#checkout_date").datepicker('setDate', de);
        $('#checkout_day option').each(function() {
            //initDays($('#checkout_day option'), de.getMonth()+1, de.getFullYear());
            if(chi.getMonth() != de.getMonth()) {
                //console.log(de.getMonth()+1, de.getMonth())
                initDays($('#checkout_day option'), de.getMonth()+1, de.getFullYear());

                $('#checkout_year_month option').each(function() {
                    if($(this).val() == de.getFullYear() + '-' + getStrFromNum(de.getMonth()+1)) {
                        $(this).attr('selected', 'selected');
                    }
                    else {
                        $(this).removeAttr('selected');
                    }
                });
            }
            else {
                initDays($('#checkout_day option'), de.getMonth()+1, de.getFullYear());

                $('#checkout_year_month option').each(function() {
                    if($(this).val() == de.getFullYear() + '-' + getStrFromNum(de.getMonth()+1)) {
                        $(this).attr('selected', 'selected');
                    }
                    else {
                        $(this).removeAttr('selected');
                    }
                });
            }

            if($(this).val() == de.getDate()) {
                $(this).attr('selected', 'selected')
            }
            else {
                $(this).removeAttr('selected');
            }
        });
    } else {
        //if(chobc.getMonth() != chi.getMonth()) {
            initDays($('#checkout_day option'), chobc.getMonth()+1, chobc.getFullYear());
            
            $('#checkout_year_month option').each(function() {
                if($(this).val() == chobc.getFullYear() + '-' + getStrFromNum(chobc.getMonth()+1)) {
                    $(this).attr('selected', 'selected');
                }
                else {
                    $(this).removeAttr('selected');
                }
            });
            
            $('#checkout_day option').each(function() {
                if($(this).val() == chobc.getDate()) {
                    $(this).attr('selected', 'selected')
                }
                else {
                    $(this).removeAttr('selected');
                }
            });
        //}
    }
}




