// (c) Copyright by SimX Corporation 2010

if( !calendarscr_loaded ) {

var CalendarScr = function( edt_obj_, submit_func )
{
    this.edt_obj = edt_obj_;
    this.ddd_obj = null;
    this.do_not_close = false;
    this.old_doc_onmousedown = null;
    this.old_doc_onkeydown = null;
    this.Submit = submit_func;
    this.temp_date = null;
}

CalendarScr.prototype = {
  ShowCalendar : function( e ) {
    if( !this.ddd_obj ) {
         this.ddd_obj = document.createElement('DIV');
         document.body.appendChild(this.ddd_obj);
         this.ddd_obj.id=this.edt_obj+"_ddd";
         this.ddd_obj.style.cssText="DISPLAY: none; POSITION: absolute; Z-INDEX: 5; BORDER: 1px solid windowframe;  CURSOR: default;"
    }
    if(typeof this.ddd_obj != 'undefined' && this.ddd_obj ) {
        if( this.ddd_obj.style.display != "none" )
            return this.CloseCal();
        else {
            if( this.edt_obj.value.length ) {
                var date_s = this.edt_obj.value.replace(/(\d\d)(PM|AM)/i, "$1 $2"); // Gecko favor
                var year_a = date_s.match(/\d{1,2}\/\d{1,2}\/(\d{1,4})/);
                if( year_a && year_a.length == 2 )
                    this.two_digit_year = year_a[1].length <= 2;
                this.temp_date = new Date( date_s ); 
                if( isNaN( this.temp_date.getTime() ) )
                    this.temp_date = new Date();
                else {
                    if( this.two_digit_year ) {
                        if( this.temp_date.getFullYear() < (new Date).getFullYear() - 70 )
                            this.temp_date.setFullYear( this.temp_date.getFullYear() + 100 );
                    }                    
                    this.honor_time = this.temp_date.getHours() != 0 && this.temp_date.getMinutes() != 0 ? true : false;
                    this.ampm_time = this.honor_time && /(am|pm)/i.test( this.edt_obj.value );
                }
            }
            else this.temp_date = new Date();
            
            this.do_not_close = true;
            if( document.onmousedown ) document.onmousedown();

            this.ddd_obj.innerHTML = this.CreateContent();

            var ed_x_pos = GetPageXPos( this.edt_obj );
            var ed_y_pos = GetPageYPos( this.edt_obj );
            var ed_height = GetHeight( this.edt_obj );

            var maxX = GetXScroll() + GetPageWidth();
            var maxY = GetYScroll() + GetPageHeight();
            this.ddd_obj.style.display = "block";
            var ddd_width = GetWidth( this.ddd_obj );
            if( ua.ie ) {
                var l_height = GetHeight( this.ddd_obj );
                if( l_height > 200 )
                    SetHeight( this.ddd_obj, 200 );
            }
            l_height = GetHeight( this.ddd_obj );
            SetWidth(this.ddd_obj, ddd_width);
            var y_pos = ed_y_pos + ed_height;
            if( y_pos + l_height > maxY ) y_pos = ed_y_pos - l_height;
            
            
            if( y_pos >= GetYScroll() ) {
                SetYPos( this.ddd_obj, y_pos );
                SetXPos( this.ddd_obj, ed_x_pos + ddd_width <= maxX ? ed_x_pos : maxX - ddd_width );
            }
            else {
                SetXPos( this.ddd_obj, ed_x_pos + GetWidth( this.edt_obj ) + 18 );
                SetYPos( this.ddd_obj, GetYScroll() + 5 );
            }
            var $this = this;
            this.ddd_obj.onmousedown = function() { $this.do_not_close = true; }
            this.old_doc_onmousedown = document.onmousedown;
            document.onmousedown = function() { $this.CloseCal(); }
            if(ua.ie) {
                this.old_doc_onkeydown = document.onkeydown;
                document.onkeydown = function() { if(event.keyCode == 27) $this.CloseCal(); }
            }
        }
    }
},

CreateContent : function() {
    var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

    //  DECLARE AND INITIALIZE VARIABLES
    var date_prc = new Date( this.temp_date );
    if( isNaN( date_prc.getTime() ) ) date_prc = new Date();
    var year = date_prc.getFullYear();	// Returns year
    var month = date_prc.getMonth();    // Returns month (0-11)
    var today = date_prc.getDate();     // Returns day (1-31)
    var weekday = date_prc.getDay();    // Returns day (1-31)

    var MONTHS_OF_YEAR = 12;
    var DAYS_OF_WEEK = 7;
    var DAYS_OF_MONTH = 31;
    var cal;
    var this_obj_name = this.edt_obj.name + "_scr";

    date_prc.setDate(1);         // Start the calendar day at '1'
    date_prc.setMonth(month);    // Start the calendar month at now

    var TR_start = '<TR>';
    var TR_end = '</TR>';
    var TD_start = '<TD WIDTH="2em">';
    var TD_end = '</TD>';

    cal =  '<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="4" style="BACKGROUND-COLOR: window; COLOR: windowtext; FONT-FAMILY: Arial;"><TR>';
    cal += '<TD align="center" style="FONT-SIZE: 8pt; BACKGROUND-COLOR: threedface">&nbsp;&nbsp;';
    {
        cal += '<A href="javascript:void(0);" title="Previous Month" onclick="'+ this_obj_name +'.SetMonth(-1);" style="COLOR: windowtext; PADDING-RIGHT: 1pt;">&#x25C4;</A>';
    
        cal += '<SELECT onchange="'+ this_obj_name +'.SetMonth(this.selectedIndex);">';
        for( var index=0; index < MONTHS_OF_YEAR; index++ )
            cal += '<OPTION '+(index==month?'selected':'')+'>' + month_of_year[index] + '</OPTION>';
        cal += '</SELECT>';
        cal += '<A href="javascript:void(0);" title="Next Month" onclick="'+ this_obj_name +'.SetMonth(-2);" style="COLOR: windowtext; PADDING-LEFT: 1pt;">&#x25BA;</A>';            
        cal += '&nbsp;&nbsp;';

        cal += '<A href="javascript:void(0);" title="Previous Year" onclick="'+ this_obj_name +'.SetYear(-1);" style="COLOR: windowtext; PADDING-RIGHT: 1pt;">&#x25C4;</A>';

        cal += '<SELECT onchange="'+ this_obj_name +'.SetYear(this.options[this.selectedIndex].text);">';
        var max_year = Math.max( (new Date()).getFullYear(), year ) + 10;
        for( var y=Math.min( (new Date()).getFullYear(), year ) - 20; y < max_year; y++ )
            cal += '<OPTION '+(y==year?'selected':'')+'>' + y + '</OPTION>';
        cal += '</SELECT>';
        
        cal += '<A href="javascript:void(0);" title="Next Year" onclick="'+ this_obj_name +'.SetYear(1);" style="COLOR: windowtext; PADDING-LEFT: 1pt;">&#x25BA;</A>';
        // &#x221A;
        cal += '&nbsp;&nbsp;<input type="button" value="ok" onclick="'+ this_obj_name +
            '.SetDay( -1 );" style="HEIGHT: 2em; aWIDTH: 2em; FONT-SIZE: 7pt; font-family: helvetica;">&nbsp;';
        cal += TD_end + TR_end;
    }
    cal += TR_start + '<TD align="center">';
    cal += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 align="center" style="FONT-SIZE: 8pt;">'
    cal += '<TR style="BACKGROUND-COLOR: window; COLOR: graytext;">';

    for( var index=0; index < DAYS_OF_WEEK; index++)
        cal += TD_start + day_of_week[index] + TD_end;

    cal += TD_end + TR_end;
    cal += TR_start;

    for( var index=0; index < date_prc.getDay(); index++)
        cal += TD_start + '&nbsp; ' + TD_end;
    for( var index=0; index < DAYS_OF_MONTH; index++) {
        if( date_prc.getDate() > index ) {
            week_day = date_prc.getDay();
            if(week_day == 0)
            cal += TR_start;
            if(week_day != DAYS_OF_WEEK) {
                var day = date_prc.getDate();
                var day_cnt = '<a class="cal_day" href="javascript:void(0);" onclick="' +
                                this_obj_name + '.SetDay('+ day +');">&nbsp;' + ( day < 10 ? '&nbsp;' : '') + day + '&nbsp;</a>';

                cal += TD_start + ( today == date_prc.getDate() ? day_cnt.bold() : day_cnt ) + TD_end;
            }
            if(week_day == DAYS_OF_WEEK)
                cal += TR_end;
        }
        date_prc.setDate(date_prc.getDate()+1);
    }

    cal += '</TD></TR></TABLE></TD></TR></TABLE>';
    //alert(cal);
    return cal;
},
SetDay : function( day ) {
    if( day > 0 ) this.temp_date.setDate( day );
    var m = this.temp_date.getMonth() + 1;
    var a = new Array;
    if( m < 10 ) a.push( "0" );
    a.push( m, "/" );
    var d = this.temp_date.getDate()
    if( d < 10 ) a.push( "0" );
    a.push( d, "/" );
    if( this.two_digit_year ) {
        var y = this.temp_date.getYear() % 100;
        if( y < 10 ) a.push( "0" );
        a.push( y );
    }
    else
        a.push( this.temp_date.getFullYear() );
    if( this.honor_time ) {
        var h = this.temp_date.getHours();
        var sfx = ""
        if( this.ampm_time ) {
            if( h > 12 ) { sfx = "PM"; h -= 12; }
            else sfx = "AM";
        }
        a.push( " ", h, ":" );
        if( this.temp_date.getMinutes() < 10 ) a.push( "0" );
        a.push( this.temp_date.getMinutes(), sfx );
    }
    this.edt_obj.value = a.join("");
    changeHandler(this.edt_obj.name);
    this.do_not_close = false;
    this.CloseCal();
},

SetMonth : function( month ) {
    if( month < 0 ) {
        var cur_month = this.temp_date.getMonth();
        switch( month ) {
        case -1: month = cur_month-1; break;
        case -2: month = cur_month+1; break;
        default: month = cur_month;
        }
        if( month < 0 || month >= 12 )
            var dont_adjust_date = true;
    }
    this.temp_date.setMonth( month );
    if( !dont_adjust_date && this.temp_date.getMonth() > month )
        this.temp_date.setDate( 0 );
    this.ddd_obj.innerHTML = this.CreateContent();
},

SetYear : function( year ) {
    var tmp_month = this.temp_date.getMonth();
    this.temp_date.setYear( year <= 1 ? this.temp_date.getFullYear() + year : year );
    if( this.temp_date.getMonth() > tmp_month )
        this.temp_date.setDate( 0 );
    this.ddd_obj.innerHTML = this.CreateContent();
},

CloseCal : function() {
    if( this.do_not_close )
        this.do_not_close = false;
    else {
        this.temp_date = null;
        document.onmousedown = this.old_doc_onmousedown;
        if(document.all) document.onkeydown = this.old_doc_onkeydown;
        if(typeof this.ddd_obj != 'undefined' && this.ddd_obj && this.ddd_obj.style.display != "none" ) {
            this.ddd_obj.style.display = "none";
            if( this.Submit ) 
                this.Submit.apply( this.edt_obj );
        }
    }
},

MaybeShowCalendar : function( event_ ) {
    var al_w = GetWidth(this.edt_obj)-18;
    if( ( event_.offsetX && event_.offsetX > al_w ) ||
        ( event_.layerX  && event_.layerX  > al_w ) )
        this.ShowCalendar( event_ );
}

}

var calendarscr_loaded = true;

}

