    /*
     * Script include
     * <script language="Javascript" type="text/javascript" src="./s/cal.js"></script>
     *
     * button include
     *
     * <button style="margin: 0px; padding: 0px; background: "+color_5+" url(./i/cal.gif) no-repeat center middle; border: 1px raised white; width: 20px; height: 20px;" onClick="show_calendar(this.form.date_field);return false;" title="Click Here to Pick a date" /></button>
     * <button onClick="show_calendar(this.form.date_field);return false;" title="Click Here to Pick a date" /><img src="./i/cal.gif" border="0" alt="Click Here to Pick a date" /></button>
     * <button onClick="show_calendar(this.form.date_field);return false;" title="Click Here to Pick a date" />Pick Date</button>
     */

    var current_date_field = null;
    var color_1 = "#000";
    var color_2 = "#b90100";
    var color_3 = "#b90100";		// current date background
    var color_4 = "#c9c9c9";		// weekday and days of other weeks
    var color_5 = "#fff";
    var color_bg = "710002"
    var css_font = "0.55em verdana,arial,helvetica,sans-serif";
    var popup_width = 190
    var popup_height = 190

    function show_calendar(obj_target, str_datetime) {
        current_date_field = obj_target;

        str_datetime = (str_datetime != ''? str_datetime : current_date_field.value);

        var arr_months = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
        var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
        var n_weekstart = 1; // day week starts from (normally 0 or 1)

        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);

        // html generation (feel free to tune it for your particular application)
        // print calendar header
        var str_buffer = new String (
            "<html><head><title>Calendar</title></head>"+
            "<body style=\"background:"+color_bg+";border:0;margin:0;padding:10px;\" onBlur=\"window.focus()\">"+
            "<table style=\"cursor:pointer;\" cellspacing=\"0\" border=\"0\" width=\"100%\">"+
            "<tr><td bgcolor=\""+color_1+"\">"+
            "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">"+
            "<tr><td bgcolor=\""+color_1+"\"><button onclick=\"window.opener.show_calendar(window.opener.current_date_field"+
            ", '"+ dt2dtstr(dt_prev_month)+"');\" style=\"border:1px solid "+color_2+";font:"+css_font+";color:"+color_5+";background:"+color_2+";width:22px;height:19px;\">"+
            "<<</button></td>"+
            "   <td bgcolor=\""+color_1+"\" colspan=\"5\" style=\"color:"+color_5+";font:"+css_font+";\">"+
            arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</td>"+
            "   <td bgcolor=\""+color_1+"\" align=\"right\"><button onclick=\"javascript:window.opener.show_calendar(window.opener.current_date_field"+
            ", '"+dt2dtstr(dt_next_month)+"');\" style=\"border:1px solid "+color_2+";font:"+css_font+";color:"+color_5+";background:"+color_2+";width:22px;height:19px;\">"+
            ">></button></td></tr>"
        );

        var dt_current_day = new Date(dt_firstday);
        // print weekdays titles
        str_buffer += "<tr>";
        for (var n=0; n<7; n++)
            str_buffer += " <td bgcolor=\""+color_2+"\" style=\"color:"+color_5+";font:"+css_font+";\">"+
            week_days[(n_weekstart+n)%7]+"</td>";
        // print calendar table
        str_buffer += "</tr>";
        while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
            dt_current_day.getMonth() == dt_firstday.getMonth()) {
            // print row heder
            str_buffer += "<tr>";
            for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {

                    // cellcolor
                    if (dt_current_day.getDate() == dt_datetime.getDate() &&
                        dt_current_day.getMonth() == dt_datetime.getMonth())
                        // print current date
                        str_cell_color = color_3;
                    else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                        // weekend days
                        str_cell_color = color_4;
                    else
                        // print working days of current month
                        str_cell_color = ""+color_5+"";


                    if (dt_current_day.getDate() == dt_datetime.getDate() &&
                        dt_current_day.getMonth() == dt_datetime.getMonth())
                        // print current date
                        str_text_color = color_5;
                    else if (dt_current_day.getMonth() == dt_datetime.getMonth())
                        // print days of current month
                        str_text_color = "black";
                    else
                        // print days of other months
                        str_text_color = color_4;

                    // add the cell
                    str_buffer += "<td "+
                        "style=\"cursor:pointer;background:"+str_cell_color+";color:"+str_text_color+";font:"+css_font+";;\" "+
                        "align=\"right\" onClick=\"window.opener.current_date_field.value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
                        dt_current_day.getDate()+"</a></td>";
                    dt_current_day.setDate(dt_current_day.getDate()+1);
            }
            // print row footer
            str_buffer += "</tr>";
        }

        // print calendar footer
        str_buffer +=
            "</table>" +
            "</tr></td></table>" +
            "</body>" +
            "</html>";

				var vWinCal = window.open("", "Calendar", "width="+popup_width+",height="+popup_height+",status=no,resizable=no,top=200,left=200");

        vWinCal.opener = self;
        var calc_doc = vWinCal.document;
        calc_doc.open ();
        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) {
        var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
        if (!re_date.exec(str_datetime))
            return alert("Invalid Datetime format: "+ str_datetime);
        return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
    }

    function dt2dtstr (dt_datetime) {
        return (new String (
                dt_datetime.getDate()+"/"+(dt_datetime.getMonth()+1)+"/"+dt_datetime.getFullYear()));
    }

