<!--

  /**  Cracow, Poland 23.5.2001
   *
   *   This component was created by Dominik Radziszowski  <radzisz@radzisz.com>
   *
   *   The supported date format is now:   dd-mm-yyyy 
   *   Send me your improvements if you do some
   */

   /* Callendar component

    Html code usage example follows:
    it should be copy&paste in separate *.html file   

      <html>
          <head>
           <title>Callendar</title>
           <script language="JavaScript1.2" fptype="calendar" src="calendar.js"></script>
         </head>
         <body bgcolor="#FFFFFF">
          <form name="form1">
           <input type="text" name="test1" value="">
                  <A HREF="javascript:set_cal('test1', 'form1')"><img src="kalendarz.gif" border="0"></a>
          </form>
        </body>
     </html>

    */

     /* to do: - different data format */


    // define the following according to your preferences
  
    var year_offset = 3; //number of years displayed arround current year
    var sun_color = "#eeeeFF";   //colour of Sunday
    var right_arrow_img = "script/arrowr.gif";  //name of right arrow
    var left_arrow_img = "script/arrowl.gif";    //name of left arrow

    //end




























    //some public variables

    var doc; //window with callendar
    var input_name; //name of the input which we want to set
    var form_name; //name of the form in wich input is located


    function inc_year(date_month, date_year){
       date_year++;
       show_cal(date_month, date_year);
    }

    function dec_year(date_month, date_year){
       date_year--;
       show_cal(date_month, date_year);
    }

    function inc_month(date_month, date_year){
     if (date_month == 11)
          {date_year++; date_month=0;}
       else
          {date_month++;}
       show_cal(date_month, date_year);

    }
    function dec_month(date_month, date_year){
     if (date_month == 0)
          {date_year--; date_month=11;}
       else
          {date_month--;}
       show_cal(date_month, date_year);
    }


//function which shows the callendar
    function show_cal(date_month, date_year){
      // monts are coutend strating from 0  (0- January etc) - it is not my idea - but JavaScript desigers
       
       date_suf="-"+(date_month+1)+"-"+date_year;
       first_date = new Date(date_year, date_month, 1);
       first_day = first_date.getDay();



       doc=window.open("",
                        "calendar",
                        "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1,width=282,height=235,top=300,left=685, dependent"); 

        doc.document.open();
        doc.document.writeln("<html>");
        doc.document.writeln("<head><title>Pick up a date...</title>");
        doc.document.writeln("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-2'>" +
              "<style>" + 
                 "body{font-family:Tahoma,Verdana;font-size:8pt}"+
                 "select{font-family: tahoma, verdana; font-size: 8pt; color: #000080;background-color: #f0f0FF; border: 1 solid #000080}" +
                 "td{border-color:navy;font-family:Tahoma,Verdana;font-size:8pt}table{border:none}"+
                  "a{text-decoration:none}</style>" +
              "</head>"+
                  "<body topmargin=10>"+
                  "<center><table border=1 cellpadding=0 cellspacing=0 width=100%><tr><td>"+
                 "<table border='0' bgcolor='#eeeeFF' cellspacing='0' cellpadding='3' width='100%'>"+
                              "<tr><td align='right' widht=30%>");

        doc.document.writeln("<A href=javascript:opener.dec_year("+date_month+","+date_year+")>"+
                             "<img src='"+left_arrow_img+"' border='0'></A>");
        doc.document.writeln("</td><td width=40%>");
        doc.document.writeln("<center>" + year_combo(date_year, date_month) + "</center>");
        doc.document.writeln("</td><td align='left' width=30%>");
        doc.document.writeln("<A href=javascript:opener.inc_year("+date_month+","+date_year+")>"+
                             "<img src='"+right_arrow_img+"' border='0'></A>");
        doc.document.writeln("</td></tr><tr><td align='right'>");
        doc.document.writeln("<A href=javascript:opener.dec_month("+date_month+","+date_year+")>"+
                             "<img src='"+left_arrow_img+"' border='0'></A>");
        doc.document.writeln("</td><td>");
        doc.document.writeln("<center>" + month_combo(date_month, date_year) + "</center>");
        doc.document.writeln("</td><td align='left'>");
        doc.document.writeln("<A href=javascript:opener.inc_month("+date_month+","+date_year+")>"+
                             "<img src='"+right_arrow_img+"' border='0'></A>");
        doc.document.writeln("</td></tr></table></tr></td></table></center>");


        doc.document.writeln("<center><img src='about:blank' width=1 height=10><table border='1' cellspacing='0' cellpadding='3' width='100%'><tr>");
        doc.document.writeln(
            "<td width=10% bgcolor='#eeeeFF'><b>w.</b></td><td width=6%></td><td width=12%><b>Mon</b></td><td width=12%><b>Tue</b></td><td width=12%><b>Wed</b></td>" +
            "<td width=12%><b>Thu</b></td><td width=12%><b>Fri</b></td><td width=12%><b>Sat</b></td><td width=12% bgcolor='#eeeeFF'><b>Sun</b></td>");    
        doc.document.writeln("</tr><tr>"); 
        
        doc.document.writeln("</tr><tr><td bgcolor='#eeeeFF'><center>"+get_week_nr(1,date_month,date_year)+"</center></td><td></td>");


        var j;
        for (j=0;j<(6+first_day)%7;j++)
        {
          doc.document.writeln("\t<td><center></center></td>");
        } 

       days_count = days_num(date_month, date_year);

       var i;
       for (i=1;i<=days_count;i++)
        {
      
          if ((i+first_day)%7 == 1)
            {
             doc.document.writeln("\t<td bgcolor='"+sun_color+
                                  "'><center><A HREF=javascript:opener.set_date(\""+i+date_suf+"\")>"
                                + i + "</center></td>\n");
             if (i<days_count)
              {
              week_nr = get_week_nr(i+1,date_month,date_year);
               doc.document.writeln("</tr>\n<tr>\t<td bgcolor='#eeeeFF'><center>"+week_nr+"</center></td>\n\t<td></td>");
              }
            }
          else
          doc.document.writeln("\t<td><center><A HREF=javascript:opener.set_date(\""+i+date_suf+"\")>"
                                + i + "</center></td>");

        }

        for (k=1;(i+j+k-2)%7!=0;k++)
        {
          doc.document.writeln("<td><center></center></td>");
        } 

        doc.document.writeln("</tr></table></center>");
        doc.document.writeln("</body>");
        doc.document.writeln("</html>"); 
        doc.document.close();
        doc.focus();
   }

//finally called function
//It set value in appriopriate input and close window with calendar
   function set_date(date_val)
   {
     doc.close();
     self.document[form_name][input_name].value=date_val;
   }



//returns number of days in the specified month in specified year
   function days_num(month, year)
   {
     switch(month)
     {
      case 0 : return 31;
      case 1 :
              if (year%4 == 0)
               if (year%100 == 0)
                 {
                  if (year%400 ==0)
                    return 29;
                  else
                   return 28;
                  }
               else
                 return 29;
              else
                return 28;
      case 2 : return 31;
      case 3 : return 30;
      case 4 : return 31;
      case 5 : return 30;
      case 6 : return 31;
      case 7 : return 31;
      case 8 : return 30;
      case 9 : return 31;
      case 10 : return 30;
      case 11 : return 31;
     default: return 50;  //it should not happen
   }
 }


//returns the name of the month
function month_name(month)
   {
     switch(month)
     {
      case 0 : return "January";
      case 1 : return "February";
      case 2 : return "March";
      case 3 : return "April";
      case 4 : return "May";
      case 5 : return "June";
      case 6 : return "July";
      case 7 : return "August";
      case 8 : return "September";
      case 9 : return "October";
      case 10 : return "November";
      case 11 : return "December";
     default: return "unknown";  //it should not happen
   }
}


//function called to change currently displayed month
function set_month(date_year)
{
  date_month = doc.month.value;  //by byla cyferka
  date_month = parseInt(date_month);
  show_cal(date_month, date_year);    
}

//function called to change the currently displayed year
function set_year(date_month)
{
  date_year = doc.year.value;  //to make sure it is a number not a String
  date_month = parseInt(date_month);
  date_year = parseInt(date_year);
  show_cal(date_month, date_year);    
}
 

//creates combo box with months for specified year selecting specified month
function month_combo(month, date_year)
  {
   var combo = "<select name=\"month\" onChange=\"javascript:opener.set_month("+date_year+")\">";
    for(i=0;i<12;i++)
     {
      if (i==month) sel="selected"; else sel="";
      combo += "<option value="+i+" "+ sel+">"+month_name(i)+"</option>";
     }
     combo += "</select>";
   return combo;
 }

// creates combobox with years selecting specified year (mont param is to make proper onChange
// reference
function year_combo(date_year, date_month)
  {
   b_date_year = date_year - year_offset;
   e_date_year = date_year + year_offset;
   var combo = "<select name=\"year\" onChange=\"javascript:opener.set_year("+date_month+")\">";
    for(i=b_date_year;i<=e_date_year;i++)
     {
      if (i==date_year) sel="selected"; else sel="";
      combo += "<option value="+i+" "+ sel+">"+i+"</option>";
     }
   combo += "</select>";
    return combo;
 }


 function get_week_nr(day, month, year)
 {
  days = get_day_nr(day, month, year);
  first_date = new Date(year, 0, 1);
  last_date = new Date(year, 11, 31);
  last_day_of_year = last_date.getDay();
  first_day_of_year = first_date.getDay();

  if (first_day_of_year==0) first_day_of_year=7;
 days = days -2 + first_day_of_year;
  with (Math) {
   week = floor(days/7);
  }

   //mond, thus, wensd, thurs -> week nr. 1 starts in previous year so week++
  if((first_day_of_year == 1) ||
     (first_day_of_year == 2) ||
     (first_day_of_year == 3) ||
     (first_day_of_year == 4))
        week++;

  if (week == 0) week = get_week_nr(31,11,year-1); //it is 53 or 52

  //year ends with mond, thus, wensd -> so week nr. 1
  if ((week == 53) && ((last_day_of_year == 1) ||
                      (last_day_of_year == 2) ||
                      (last_day_of_year == 3)))
                       week=1;

  return week;
}

function get_day_nr(day, month, year)
{
 day_nr=0;
 for (i=0; i<month; i++)
   day_nr+=days_num(i, year);
 
 day_nr+=day;
 return day_nr;
}

//function set_cal(_input_name, _form_name)

function set_cal(_input_name, _form_name)
{
   input_name = _input_name;
   form_name  = _form_name;

 //trying to get current value of input

    cdate = new String(self.document[form_name][input_name].value);

     first_id= cdate.indexOf("-",0); 
     sec_id = cdate.indexOf("-",first_id+1);  
     thr_id = cdate.length;
     first_str= cdate.substring(0,first_id);
     sec_str=cdate.substring(first_id+1, sec_id);
     thr_str= cdate.substring(sec_id+1, thr_id);
 
      year = parseInt(thr_str);
      month=parseInt(sec_str);


   //if it fails get today's date
   if(isNaN(year)  || year <1900 || year >2100)  
       year = (new Date()).getYear();

   if(isNaN(month) || month <1 || month >12)
      month = (new Date()).getMonth();
    else
       month--; //becouse month are indexed from 0
 
    //alert(first_str + "--"+month + "--"+ year + thr_str);  //test

   //shows calendar for specified month and year
   show_cal(month,year);
}

//-->
