  // initialize arrays
  var months = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre","Ottobre", "Novembre", "Dicembre"); 
  var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var days = new Array("Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab");
  var fromEl=null         
  var today = new getToday();
  var dayClass=null;
  var selDay=null;      
  var BORDERCOLOR="#AAAAAA"

  function element_top(el){
  var et = 0
  while (el)
  { 
    if (el.style.position=="absolute") el=null
    else
    {
      et += el.offsetTop
      el = el.offsetParent
    }  
  }
  return et
}


function element_left(el)
{
  var et = 0
  while (el)
  { 
    if (el.style.position=="absolute") el=null
    else
    {
      et += el.offsetLeft
      el = el.offsetParent
    }  
  }	
  return et
}

  function newCalendar(obj) {
    dayClass=null;
    if (obj!=null) fromEl=obj;
    if (!document.all.divCal) 
      doCalendar();
      
    st=document.all.divCal.style
    st.left= element_left(fromEl);
    st.top= element_top(fromEl)+20;
    st.display="block";
    
    var parseYear = parseInt(document.all.year[document.all.year.selectedIndex].text) ;
    var newCal = new Date(parseYear, document.all.month.selectedIndex, 1);
    var day = -1;
    var startDay = newCal.getDay();
    var daily = 0;
    if ((today.year == newCal.getYear() ) && (today.month == newCal.getMonth())) day = today.day;
    
    if (selDay==null) 
      selDay=today.day;
    else
      if (selDay>intDaysInMonth) selDay=intDaysInMonth;
      
    // cache the calendar table's tBody section, dayList
    var tableCal = document.all.calendar.tBodies.dayList;
    var intDaysInMonth = getDays(newCal.getMonth(), newCal.getYear());
    for (var intWeek = 0; intWeek < tableCal.rows.length; intWeek++)
      for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length; intDay++) {
        var cell = tableCal.rows[intWeek].cells[intDay];
 
        // start counting days
        if ((intDay == startDay) && (0 == daily)) daily = 1;

        // highlight the current day
        cell.className = (day == daily) ? "today" : "days";
        if (selDay==daily) 
          cell.style.borderColor=BORDERCOLOR;
        else
          cell.style.borderColor="";  
        // output the day number into the cell
        if ((daily > 0) && (daily <= intDaysInMonth))
          cell.innerText = daily++;
        else
          cell.innerText = "";
      }
  }
  
  function getDate() {
    
    if ("TD" == event.srcElement.tagName)
      if ("" != event.srcElement.innerText)
        d="0" + event.srcElement.innerText
    m="0" + (month.selectedIndex+1)
    fromEl.value= d.substring(d.length-2) + "/" +  m.substring(m.length-2) + "/" + year.options(year.selectedIndex).text
    document.all.divCal.style.display="none"
  }
  
  function doCalendar(){
   s='<DIV onfocus="this.style.display=\'block\'" ID="divCal" style="display:none;position:absolute;left:0px;top:0px;">'
   s=s+'<TABLE ID="calendar" class="tblCalendar">'
   s=s+'<THEAD><TR><TD COLSPAN=7 ALIGN=CENTER>'
   s=s+'<SELECT ID="month" ONCHANGE="newCalendar(null)">'
   
   for (var intLoop = 0; intLoop < months.length;intLoop++)
     s=s+ '<OPTION ' + (today.month == intLoop ? "Selected" : "") + '>' + months[intLoop]
   
   s=s+ '</SELECT><SELECT ID="year" ONCHANGE="newCalendar(null)">'
   
   for (var intLoop = 2000; intLoop < 2010; intLoop++)
   s=s+"<OPTION " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop
   s=s+'</SELECT></TD></TR><TR CLASS="days">'
   
   for (var intLoop = 0; intLoop < days.length; intLoop++)
     s=s+"<TD><b>" + days[intLoop] + "</TD>";
   s=s+'</TR></THEAD><TBODY ID="dayList" ALIGN=CENTER ONCLICK="getDate()">'
   
   for (var intWeeks = 0; intWeeks < 6; intWeeks++) {
     s=s+"<TR>"
     for (var intDays = 0; intDays < days.length;intDays++)
       s=s+"<TD style='cursor:hand;' onmouseover='calMouseOver(this);' onmouseout='calMouseOut(this)'></TD>";
     s=s+"</TR>"
   }
   s=s+"</TBODY></TABLE></DIV>"
   window.document.body.insertAdjacentHTML("afterBegin",s) 
   
 }
 
 function calMouseOver(entity) {
   dayClass=entity.className
   entity.className='clsMouseOver'
 }
 
 function calMouseOut(entity) {
   entity.className=dayClass
   dayClass=null
 }
 
 function getDays(month, year) {
    if (1 == month)
      return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
    else
      return daysInMonth[month];
  }

  function getToday() {
    this.now = new Date();
    this.year = this.now.getYear() ;
    this.month = this.now.getMonth();
    this.day = this.now.getDate();
  }

  
  
   
   
   
   
