/*
 * Version: $Revision: 22343 $
 * Author: BeSite <info@besite.nl>
 * Copyright 2007 BeSite
 */

var JScalendarKeyPressed = new Array();
var JScalendarSeperator = "-";
var JScalendarDisabledMonday = false;
var JScalendarDisabledTuesday = false;
var JScalendarDisabledWednesday = false;
var JScalendarDisabledThursday = false;
var JScalendarDisabledFriday = false;
var JScalendarDisabledSaturday = false;
var JScalendarDisabledSunday = false;
var JScalendarDateSetCallbackFunctions = new Array();

function JScalendarOnChange( eElement, iIndex, iYearFrom, iYearTo, bForce )
{
    if ( JScalendarKeyPressed[iIndex] || bForce === true )
    {
        var arr;
        arr = eElement.value.split( JScalendarSeperator );
        var d = JScalendarGetDate( iIndex );
        d.setFullYear( parseInt( document.getElementById( "CalendarDateYear" + iIndex ).value ) );
        d.setMonth( parseInt( document.getElementById( "CalendarDateMonth" + iIndex ).value ) - 1 );
        d.setDate( parseInt( document.getElementById( "CalendarDateDay" + iIndex ).value ) );
        if( arr.length == 3 )
        {
             // parseInt( "09" ) gives 0? same for 08, so cut of leading 0 with regexp
            var ye = parseInt( arr[2].match(/0*([0-9]+)/)[1] );
            var mo = parseInt( arr[1].match(/0*([0-9]+)/)[1] );
            var da = parseInt( arr[0].match(/0*([0-9]+)/)[1] );
            if ( !isNaN( ye ) && !isNaN( mo ) && !isNaN( da ) )
            {
                if ( ye < 100 )
                {
                    // 2 digit
                    if ( ye <= 30 )
                    {
                        ye += 2000;
                    }
                    else
                    {
                        ye += 1900;
                    }
                }
                if ( ye < iYearFrom )
                {
                    ye = iYearFrom;
                }
                else if ( ye > iYearTo )
                {
                    ye = iYearTo;
                }
                d.setFullYear( ye );
                d.setMonth( mo - 1 );
                d.setDate( da );
            }
        }
        JScalendarSetDate( iIndex, d );
    }
    JScalendarKeyPressed[iIndex] = false;
}

function JScalendarOnKeyPress( eElement, iIndex, eEvent )
{
    var keyCode = eEvent.keyCode;
    if ( eEvent.which )
    {
        keyCode = eEvent.which;
    }
    switch( keyCode )
    {
        case 38: // up key
        case 40: // down key
        case 33: // page up
        case 34: // down key
            if ( JScalendarKeyPressed[iIndex] )
            {
                // update date with these changes first (years are bogus)
                JScalendarOnChange( document.getElementById( "JSdate" + iIndex ), iIndex, 0, 9999 );
            }
            break;
    }
    var date = JScalendarGetDate( iIndex );
    switch( keyCode )
    {
        case 38: // up key
            date.setDate( date.getDate() - 1 );
            break;
        case 40: // down key
            date.setDate( date.getDate() + 1 );
            break;
        case 33: // page up
            date.setMonth( date.getMonth() - 1 );
            break;
        case 34: // down key
            date.setMonth( date.getMonth() + 1 );
            break;
        default:
            //alert( keynum );
            switch( keyCode )
            {
                case 8: // backspace
                case 46: // delete
                case 48: // 0
                case 49: // 1
                case 50: // 2
                case 51: // 3
                case 52: // 4
                case 53: // 5
                case 54: // 6
                case 55: // 7
                case 56: // 8
                case 57: // 9
                    JScalendarKeyPressed[iIndex] = true;
                    break;
                case 9: // tab
                case 35: // end
                case 36: // home
                case 37: // left key
                case 39: // right key
                    // handle normal way
                    break;
                default:
                    if ( String.fromCharCode( keyCode ) != JScalendarSeperator )
                    {
                        // we don't want to input his!
                        //alert( keyCode + ": " + String.fromCharCode( keyCode ) );
                        return false;
                    }
            }
            return true;
    }
    JScalendarKeyPressed[iIndex] = false;
    JScalendarSetDate( iIndex, date, false );
    document.getElementById( "JSdate" + iIndex ).onchange();
    return false; // we hanled it
}

function JScalendarSetDate( iIndex, oDate, bTime )
{
    var day = oDate.getDate();
    if ( day < 10 )
    {
        day = "0" + day;
    }
    var month = oDate.getMonth() + 1;
    if ( month < 10 )
    {
        month = "0" + month;
    }
    document.getElementById( "CalendarDateDay" + iIndex ).value = oDate.getDate();
    document.getElementById( "CalendarDateMonth" + iIndex ).value = oDate.getMonth() + 1;
    document.getElementById( "CalendarDateYear" + iIndex ).value = oDate.getFullYear();
    if ( bTime !== false && document.getElementById( "CalendarTimeHours" + iIndex ) && 
        document.getElementById( "CalendarTimeMinutes" + iIndex ) )
    {
        document.getElementById( "CalendarTimeHours" + iIndex ).value = oDate.getHours();
        document.getElementById( "CalendarTimeMinutes" + iIndex ).value = ( oDate.getMinutes() < 10 ? "0" : "" ) + oDate.getMinutes();
    }
    if ( document.getElementById( "JSdate" + iIndex ) )
    {
        document.getElementById( "JSdate" + iIndex ).value = day + JScalendarSeperator + month + JScalendarSeperator + oDate.getFullYear();
    }
    for( var i = 0; i < JScalendarDateSetCallbackFunctions.length; i++ )
    {
        JScalendarDateSetCallbackFunctions[i]( iIndex );
    }
}

function JScalendarGetDate( iIndex )
{
    if ( document.getElementById( "CalendarDateYear" + iIndex ) && 
            document.getElementById( "CalendarDateMonth" + iIndex ) && 
            document.getElementById( "CalendarDateDay" + iIndex ) )
    {
        var d = new Date();
        d.setFullYear( document.getElementById( "CalendarDateYear" + iIndex ).value );
        d.setMonth( document.getElementById( "CalendarDateMonth" + iIndex ).value - 1 );
        d.setDate( document.getElementById( "CalendarDateDay" + iIndex ).value );
        d.setHours( 0 );
        d.setMinutes( 0 );
        d.setSeconds( 0 );
        d.setMilliseconds( 0 );
        if ( document.getElementById( "CalendarTimeHours" + iIndex ) && 
            document.getElementById( "CalendarTimeMinutes" + iIndex ) &&
            CheckHoursString( document.getElementById( "CalendarTimeHours" + iIndex ).value ) && 
            CheckMinutesString( document.getElementById( "CalendarTimeMinutes" + iIndex ).value ) )
        {
            d.setHours( document.getElementById( "CalendarTimeHours" + iIndex ).value );
            d.setMinutes( document.getElementById( "CalendarTimeMinutes" + iIndex ).value );
        }
        return d;
    }
    return false;
}

function JScalendarDateStatus( oDate, bOnlyFuture )
{
    if ( oDate )
    {
        if ( bOnlyFuture )
        {
            var now = new Date();
            now.setHours( 0 );
            now.setMinutes( 0 );
            now.setSeconds( 0 );
            now.setMilliseconds( 0 );
            if ( oDate.getTime() < now.getTime() )
            {
                return true;
            }
        }
        switch( oDate.getDay() )
        {
            case 0: // sunday
                return JScalendarDisabledSunday;
            case 1: // monday
                return JScalendarDisabledMonday;
            case 2: // tuesday
                return JScalendarDisabledTuesday;
            case 3: // wednesday
                return JScalendarDisabledWednesday;
            case 4: // thursday
                return JScalendarDisabledThursday;
            case 5: // friday
                return JScalendarDisabledFriday;
            case 6: // saturday
                return JScalendarDisabledSaturday;
        }
    }
    return false;
}
