// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations

// Edited by: Richard Scholten

var imagedir= '/assets/cms/File/'
var css= '/assets/css/'
var callback = '';
var arr_months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni","Juli", "Augustus", "September", "Oktober", "November", "December"];
var week_days = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"];
var n_weekstart = 1; /* de week begint op een maandag */
var KLEUR_NORMAAL = '#EFEFEF';
var KLEUR_DONKER = '#EAEAEA';
var KLEUR_OPLICHTEN = '#BBBBE9';
var OrgDate = new Date();


/**
 * Functie die wordt aangeroepen als er een callback_arg wordt meegegeven. Dit kan bijvoorbeeld het aanroepen
 * van een functie uit de window.opener zijn.
 */ 
function show_calendar_callback( str_target, str_datetime, callback_arg )
{
        callback = callback_arg;
        show_calendar(str_target,str_datetime);
}


function BekijkKalender( str_target, str_datetime ){

        if( str_datetime != null || str_datetime != "") OrgDate=str2dt(str_datetime);
        show_calendar(str_target,str_datetime);
}


/**
 * Functie die wordt aangeroepen als er geen callback_arg wordt meegegeven.
 */
function show_calendar( str_target, str_datetime ) 
{
        var dt_datetime = ( str_datetime == null || str_datetime == "" ?  new Date() : str2dt(str_datetime));

        var dt_prev_month = new Date(dt_datetime);
        dt_prev_month.setMonth(dt_datetime.getMonth()-1);

        var dt_next_month = new Date(dt_datetime);
        dt_next_month.setMonth(dt_datetime.getMonth()+1);

        var dt_firstday = new Date(dt_datetime);
        dt_firstday.setDate(1);
        dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);

        var dt_lastday = new Date(dt_next_month);
        dt_lastday.setDate(0);

        // bepaal eerst een aantal jaren die in een selectbox komen
        year_select_options = "";
        for( i=1900; i<=2030; i++ ){
                if( dt_datetime.getFullYear() == i ){
                        selected = "selected";
                } else {
                        selected = "";
                }
                year_select_options += "<option value=\""+i+"\" "+selected+">"+i;
        }
        
        // bepaal de maanden die in de selectbox komen
        month_select_options = "";
        for( i=0; i<arr_months.length; i++ ){
                month = arr_months[i];
                if( dt_datetime.getMonth() == i ) month_select_options += "<option value=\""+i+"\" selected>"+month;
                else month_select_options += "<option value=\""+i+"\">"+month;
        }
        
        // de kalender
        var str_buffer = new String (
                "<html>"+
                "       <head>"+
                "               <title>Calendar</title>"+
                "               <link rel=\"stylesheet\" type=\"text/css\" href=\""+css+"/calendar.css\">"+
                "       </head>"+
                "       <body>"+
                "               <table class=\"calendar_header\" cellspacing=\"0\" border=\"0\" width=\"100%\">"+
                "                       <tr><td>"+
                "                               <table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" width=\"100%\">"+
                "                                       <tr><td class=\"calendar_header\"><a href=\"javascript:window.opener.show_calendar('"+
                                                                str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
                "                                               <img src=\""+imagedir+"/prev.gif\" border=\"0\" alt=\"previous month\"></a>"+
                "                                       </td>"+
                "                                       <td class=\"calendar_header\" colspan=\"5\" align=\"center\">"+
                "                                                               <select class=\"form_element\" id=\"maand\" onChange=\"window.opener.show_calendar('"
                                                                                +str_target+"','"
                                                                                +dt_datetime.getDate()+"-'+((this.value-0)+1)+'-"+dt_datetime.getFullYear()
                                                                                +"')\">"+month_select_options+"</select>"+
                "                                                               <select class=\"form_element\" id=\"jaar\" onChange=\"window.opener.show_calendar('"
                                                                                +str_target+"','"
                                                                                +dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-'+this.value"
                                                                                +")\">"+year_select_options+"</select>"+
                "                                       </td>"+
                "                                       <td class=\"calendar_header\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
                                                                +str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
                "                                               <img src=\""+imagedir+"/next.gif\" border=\"0\" alt=\"next month\"></a>"+
                "                                       </td>"+
                "                               </table>"+
                "                       </td></tr>"+
                "               </table>"+
                "               <table class=\"tabel\" cellspacing=\"1\" cellpadding=\"6\" border=\"0\" width=\"100%\">"
        );

        var dt_current_day = new Date( dt_firstday );
        
        // print weekdays titles
        str_buffer += "<tr>\n";
        for (var n=0; n<7; n++)
        {
                str_buffer += " <td class=\"tabel_subheader\" align=\"center\">"+
                week_days[(n_weekstart+n)%7]+"</td>\n";
        }
                
        // print calendar table
        str_buffer += "</tr>\n";
        while (dt_current_day.getMonth() == dt_datetime.getMonth() || dt_current_day.getMonth() == dt_firstday.getMonth()) 
        {
                // print row heder
                str_buffer += "<tr>\n";
                for (var n_current_wday=0; n_current_wday<7; n_current_wday++) 
                {
                        if ( dt_current_day.getDate() == dt_datetime.getDate() && dt_current_day.getMonth() == OrgDate.getMonth() )
                        {
                                // print current date
                                str_buffer += " <td bgcolor=\""+KLEUR_OPLICHTEN+"\" align=\"right\" onmouseover=\"this.style.cursor='hand';\" onmouseout=\"this.style.cursor='pointer';\" onclick=\"window.opener."+str_target+".value='"+dt2dtstr(dt_current_day)+"'; "+callback+" window.close();\">";
                        }
                        else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                        {
                                // weekend days
                                str_buffer += " <td class=\"tabel_cell_donker\" align=\"right\" onmouseover=\"this.style.background='"+KLEUR_OPLICHTEN+"'; this.style.cursor='hand';\" onmouseout=\"this.style.background='"+KLEUR_DONKER+"'; this.style.cursor='pointer';\" onclick=\"window.opener."+str_target+".value='"+dt2dtstr(dt_current_day)+"'; "+callback+" window.close();\">";
                        }
                        else
                        {
                                // print working days of current month
                                str_buffer += " <td class=\"tabel_cell_licht\" align=\"right\" onmouseover=\"this.style.background='"+KLEUR_OPLICHTEN+"'; this.style.cursor='hand';\" onmouseout=\"this.style.background='"+KLEUR_NORMAAL+"'; this.style.cursor='pointer';\" onclick=\"window.opener."+str_target+".value='"+dt2dtstr(dt_current_day)+"'; "+callback+" window.close();\">";
                        }
                        
                        /* grijze kleur als de dag in een andere maand is */
                        if( dt_current_day.getMonth() != dt_datetime.getMonth() ) str_buffer += "<font color=\"#999999\">"+dt_current_day.getDate()+"</font></td>\n";
                        else str_buffer += dt_current_day.getDate()+"</td>\n";
                        
                        /* ga verder met de volgende dag */
                        dt_current_day.setDate(dt_current_day.getDate()+1);
                }
                // print row footer
                str_buffer += "</tr>\n";
        }
        // print calendar footer
        str_buffer +=
                "</table>\n" +
                "</tr>\n</td>\n</table>\n" +
                "</body>\n" +
                "</html>\n";

        var vWinCal = window.open( "", "Calendar", "width=250, height=208,status=no,resizable=no,top=200,left=200");
        vWinCal.focus();
        vWinCal.opener = self;
        var calc_doc = vWinCal.document;
        calc_doc.write (str_buffer);
        calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) 
{
        if( str_datetime == "00-00-0000" )
        {
                return( new Date() );
        }
        else
        {
                str_datetime_array = str_datetime.split("-");
                return( new Date( str_datetime_array[2], str_datetime_array[1]-1, str_datetime_array[0] ) );
        }
}
function dt2dtstr (dt_datetime) 
{
        return (new String ( dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getFullYear()));
}


