/**
 * A iHotelier date picker
 *
 * @author  Martin Ng <martin@avalade.com>
 * @since   2008-12-08
 * @param   input       The original text box
 * @param   locale      The locale
 * @param   onclose     Callback function on calendar close
 */
function iHotelierDatePicker( input, locale, onclose )
{
    // Input
    this.input = input;
    if ( !input.id )
    {
        input.setAttribute( "id", Math.floor(Math.random() * new Date()) );
    }
    input.onkeypress = function()
    {
        return false;
    }

    // Locale
    this.locale = locale;
    var locales = {
        en: {
            days: {
                char: ["S", "M", "T", "W", "T", "F", "S"],
                short: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
                mid: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
                long: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
            },
            months: {
                short: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
                long: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
            },
            am_pm: {
                lowerCase: ["am", "pm"],
                upperCase: ["AM", "PM"]
            }
        },
        zh: {
            days:{
                char: ["日", "一", "二", "三", "四", "五", "六"],
                short: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
                mid: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
                long: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
            },
            months:{
                short: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
                long: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
            },
            am_pm:{
                lowerCase: ["上午", "下午"],
                upperCase: ["上午", "下午"]
            }
        }
    }

    // Holding element
    this.holdingElement = document.createElement( "div" );
    this.holdingElement.id = input.id + "_placeholder";
    this.holdingElement.className = "calendar";
    input.parentNode.insertBefore( this.holdingElement, input );
    input.parentNode.insertBefore( input, this.holdingElement );
    this.holdingElement.style.width = input.scrollWidth + "px";
    if ( typeof onclose == "function" )
    {
        this.holdingElement.onclick = function()
        {
            onclose( input );
        };
    }

    // Toggler
    this.toggler = document.createElement( "button" );
    this.toggler.setAttribute( "type", "button" );
    this.toggler.id = input.id + "_picker";
    this.toggler.className = "picker";
    this.toggler.appendChild( document.createTextNode(String.fromCharCode(160)) );
    input.parentNode.insertBefore( this.toggler, input );
    input.parentNode.insertBefore( input, this.toggler );

    // Options
    var options = {
        inputField: input.id,
        dateFormat: "m/d/Y",
        idPrefix: input.id,
        daysText: "char",
        monthsText: "long",
        allowDaysOffSelection: true,
        allowWeekendSelection: true,
        preTdHTML: "&laquo;",
        nexTdHTML: "&raquo;",
        speedFireFox: true,
        onSelect: function()
        {
            this.closeCalendar();
        },
        onUnSelect: function()
        {
            this.closeCalendar();
        }
    };

    if ( locales[locale] )
    {
        options.language = locales[locale];
    }

    // Calendar
    input.calendar = new Calendar(
        this.holdingElement.id,
        this.toggler.id,
        options
    );
}

/**
 * Initialize iHotelier reservation form
 *
 * @author  Martin Ng <martin@avalade.com>
 * @since   2008-12-08
 * @param   form    The iHotelier reservation form
 * @param   locale  The locale
 */
function ihotelier_reservation_setup( form, locale )
{
    window.addEvent( "load", function()
    {
        // Make sure the date in and out are correct
        function check_dates( input )
        {
            var today = new Date();
            today.setHours( 0, 0, 0, 0 );
            var date_in = form.elements["DateIn"];
            var date_in_value = new Date().fromString( date_in.value );
            date_in_value.setHours( 0, 0, 0, 0 );
            var date_out = form.elements["DateOut"];
            var date_out_value = new Date().fromString( date_out.value );
            date_out_value.setHours( 0, 0, 0, 0 );

            // Past date not allowed
            if ( date_in_value < today )
            {
                date_in_value = today;
            }
            if ( date_out_value < today )
            {
                date_out_value = today;
            }

            // Date in must be less than date out
            if ( date_in_value >= date_out_value )
            {
                // If currently changing date in, set date out as tomorrow
                if ( input == date_in )
                {
                    date_out_value = new Date( date_in_value.toString() );
                    date_out_value.setDate( date_out_value.getDate() + 1 );
                }

                // If currently changing date out, set date in as yesterday
                else
                {
                    if ( date_out_value.valueOf() == today.valueOf() )
                    {
                        date_out_value.setDate( date_out_value.getDate() + 1 );
                    }
                    date_in_value = new Date( date_out_value.toString() );
                    date_in_value.setDate( date_in_value.getDate() - 1 );
                }
            }

            // Update the form elements
            if ( date_in.value != date_in_value.print(date_in.calendar.options.dateFormat) )
            {
                date_in.calendar.selectDate( date_in_value );
            }
            if ( date_out.value != date_out_value.print(date_out.calendar.options.dateFormat) )
            {
                date_out.calendar.selectDate( date_out_value );
            }
            form.elements["Length"].value = Math.round( (date_out_value - date_in_value) / (1000 * 60 * 60 * 24) );
        }

        // Date picker for date in
        var date_in = form.elements["DateIn"];
        var date_in_picker = new iHotelierDatePicker( date_in, locale, check_dates );

        // Date picker for date out
        var date_out = form.elements["DateOut"];
        var date_out_picker = new iHotelierDatePicker( date_out, locale, check_dates );

        // Set default value for date in and out
        date_in.calendar.selectDate( new Date() );
        date_out.calendar.selectDate( new Date().fromString( "today+" + form.elements["Length"].value ) );
    } );
}