/**************************************************************
 * JavaScript Date Picker                      Version: v1.02 *
 **************************************************************
 * Written By:   Mark R Hives                                 *
 * Filename:     date-picker.js                               *
 * Release Date: 19-Nov-2007 [v1.02]                          *
 *               23-May-2006 [v1.01]                          *
 *                3-Jan-2005 [v1.00]                          *
 *                                                            *
 * Description:                                               *
 *  This script allows you to provide a pop-up calendar that  *
 *  can be used to select a date and place it into a html     *
 *  form field.                                               *
 *                                                            *
 *  For more information please visit my website at:          *
 *            http://www.gutterbucket.com/                    *
 *                                                            *
 * LICENCE AGREEMENT:                                         *
 *  THIS SOFTWARE AND ALL ACCOMPANYING FILES ARE PROVIDED     *
 *  "AS IS" AND WITHOUT WARRANTIES OF ANY KIND WHETHER        *
 *  EXPRESSED OR IMPLIED. FURTHERMORE WE CANNOT ACCEPT ANY    *
 *  LIABILITY FOR DAMAGE TO HARDWARE / SOFTWARE CAUSED EITHER *
 *  DIRECTLY OR INDIRECTLY BY THE USE OR MISUSE OF THIS       *
 *  SOFTWARE. THE SOFTWARE AND ANY SOFTWARE DERIVED FROM IT   *
 *  IS NOT INTENDED FOR COMMERCIAL USE OF ANY KIND.           *
 *                                                            *
 *  Copyright 2000-2006 Mark R Hives  -  All rights reserved  *
 **************************************************************/

// You can edit this function if you need the date in a different format
function format_date_field(y, m, d) {
   return '' + y + '-' +
          ((m > 9) ? m : '0' + m) + '-' +
          ((d > 9) ? d : '0' + d);
}



m_list = Array("Styczen", "Luty", "Marzec",
               "Kwiecien",   "Maj",      "Czerwiec",
		       "Lipiec",    "Sierpien",   "Wrzesien",
		       "Pazdziernik", "Listopad", "Grudzien");
//target = '';
targets = Array();

function set_date_field(y, m, d, t_idx) {
   eval(targets[t_idx] + ' = unescape(\'' + escape(format_date_field(y, m, d)) + '\')' );
//      set_date();
}

function show_cal(target_a) {
   var d = new Date();
   show_cal_m(d.getFullYear(), d.getMonth(), target_a);
}

function show_cal_m(y,m, target_a) {
   t_idx = targets.push(target_a) - 1;
   write_cal_pop(y,m, open('about:blank','cal_win'+t_idx,'width=225,height=150'), t_idx);
}

function write_cal_pop(y,m, cal_win, t_idx) {
   cal_win.document.open();
   cal_win.document.writeln('<html><head><title>Date Picker</title>');
   cal_win.document.writeln('<style type="text/css">');
   cal_win.document.writeln('td {');
   cal_win.document.writeln('	font-family: Verdana, Arial, Helvetica, sans-serif;');
   cal_win.document.writeln('	font-size: 9pt;');
   cal_win.document.writeln('	color: #000000;');
   cal_win.document.writeln('	background-color: #aaa;');
   cal_win.document.writeln('	text-align: center;');
   cal_win.document.writeln('}');
   cal_win.document.writeln('th {');
   cal_win.document.writeln('	font-family: Verdana, Arial, Helvetica, sans-serif;');
   cal_win.document.writeln('	font-size: 9pt;');
   cal_win.document.writeln('	color: #000000;');
   cal_win.document.writeln('	background-color: #eee;');
   cal_win.document.writeln('	text-align: center;');
   cal_win.document.writeln('}');
   cal_win.document.writeln('a {');
   cal_win.document.writeln('   color: #000000;');
   cal_win.document.writeln('   text-decoration: none;');
   cal_win.document.writeln('}');
   cal_win.document.writeln('</style></head><body leftmargin=2 topmargin=2 marginwidth=2 marginheight=2>');


   var one_day     = 1000 * 60 * 60 * 24;
   //var next_month  = (m==11) ? 0 : m + 1;

   d = new Date(y,m,1, 0,0,0,0);
   d = new Date(d.valueOf() - (one_day * 6));

   while (d.getDay() != 1) {
      d = new Date(d.valueOf() + one_day);
   }

   cal_win.document.writeln('<table align="center" width="100%" height="100%">');
   cal_win.document.writeln('<tr>');

   //PREV
   cal_win.document.write('<th><a href="javascript: opener.write_cal_pop(');
   if (m==0) {  cal_win.document.write((y-1) + ', 11'); }
   else      {  cal_win.document.write(y + ', ' + (m-1)); }
   cal_win.document.writeln(', self, ' + t_idx + ');">&lt;</a></td>');

   //CUR.
   cal_win.document.writeln('<th colspan=5><b>' + m_list[m] + ' ' + y + '</b></td>');

   //NEXT
   cal_win.document.write('<th><a href="javascript: opener.write_cal_pop(');
   if (m==11) {  cal_win.document.write((y+1) + ', 0');    }
   else       {  cal_win.document.write(y + ', ' + (m+1)); }
   cal_win.document.writeln(', self, ' + t_idx + ');">&gt;</a></td>');

   cal_win.document.writeln('</tr>');

   //day of week header
   cal_win.document.writeln('<tr><th>Pn</th><th>Wt</th><th>Sr</th><th>Cz</th><th>Pt</th><th>Sb</th><th>Nd</th></tr>');

   //DAYS
   day_count = 0;
   while (day_count < 42) {
      day_count++

      if (d.getDay() == 1) { cal_win.document.writeln('<tr>'); }
      cal_win.document.writeln('<td>' + (d.getMonth()==m ? '<b>' : ''));
	  cal_win.document.writeln('<a href="javascript: opener.set_date_field(' +
	          d.getFullYear() + ', ' + (d.getMonth()+1) + ', ' + d.getDate() +
			  ', ' + t_idx + '); self.close();">' +  d.getDate() + '</a>');

      cal_win.document.writeln(d.getMonth()==m ? '</b></td>' : '</td>');
	  //next
      //d = new Date(d.valueOf() + one_day); //replaced to stop double day in oct error
	  d.setDate(d.getDate() + 1);
	  //if (d.getMonth() == next_month && d.getDay() == 1) {  break;  }
   }


   cal_win.document.writeln('</table>');
   cal_win.document.writeln('</body></html>');
   cal_win.document.close();
}

/**
 *
 * @access public
 * @return void
 **/
function set_date() {
var co=document.forms['kalendarium'].wydarzeniestart.value;
document.forms['kalendarium'].wydarzeniestop.value=co;
}