	/*     original source from : www.totallysmartit.com        Date : 7/3/2001*/
	var lingua = "Ita";
	var timeoutDelay = 2000; // milliseconds, change this if you like, set to 0 for the calendar to never auto disappear
	var g_startDay = 1;// 0=sunday, 1=monday
	
	var X_offset = 60;
	var Y_offset = 0;
	
	// preload images
	//var imgUp = new Image(8,12);
	//imgUp.src = 'images/up.gif';
	//var imgDown = new Image(8,12);
	//imgDown.src = 'images/down.gif';
	
	// used by timeout auto hide functions
	var timeoutId = false;
	
	// global to hold the calendar reference, set by constructor
	var g_Calendar;  
	
	// the now standard browser sniffer class
	function Browser(){
	  this.dom = document.getElementById?1:0;
	  this.ie4 = (document.all && !this.dom)?1:0;
	  this.ns4 = (document.layers && !this.dom)?1:0;
	  this.ns6 = (this.dom && !document.all)?1:0;
	  this.ie5 = (this.dom && document.all)?1:0;
	  this.ok = this.dom || this.ie4 || this.ns4;
	  this.platform = navigator.platform;
	}
	var browser = new Browser();
	        
	if (browser.dom || browser.ie4){
	  document.write('<div id="calContainer" class="calContainer" onclick="calClick()"  ');
		if (timeoutDelay) document.write(' onmouseout="calendarTimeout();" onmouseover="if (timeoutId) clearTimeout(timeoutId);"');
		document.write('></div>');
	}

  function calendarTimeout(){
    if (browser.ie4 || browser.ie5){
      if (window.event.srcElement && window.event.srcElement.name!='month') timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
    }
  }
  
  // constructor for calendar class
  function Calendar(){
    g_Calendar = this;
    // some constants needed throughout the program
		if (lingua=="Ita") {
	  	this.daysOfWeek = new Array("Do","Lu","Ma","Me","Gi","Ve","Sa");
	  	this.months = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
		} else {
	  	this.daysOfWeek = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
	  	this.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		}
    this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
    if (browser.dom || browser.ie4){
          var tmpLayer = browser.dom ? document.getElementById('calContainer') : document.all.calContainer;
    }
    this.containerLayer = tmpLayer;
  }
  
  Calendar.prototype.getFirstDOM = function() {
    var thedate = new Date();
    thedate.setDate(1);
    thedate.setMonth(this.month);
    thedate.setFullYear(this.year);
    return thedate.getDay();
  }

  Calendar.prototype.getDaysInMonth = function (){
    if (this.month!=1) {
      return this.daysInMonth[this.month]
    } else {
       // is it a leap year
      if (Date.isLeapYear(this.year)) {
        return 29;
      } else {
        return 28;
      }
    }
  }
   
  Calendar.prototype.buildString = function(){
    var tmpStr = '<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBorderColor"><tr><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
    tmpStr += '<tr><td class="cal"><a href="#" ondrag="moveCal()" title="clicca e trascina il calendario">°</a></td>';
    tmpStr += '<td width="60%" class="cal" align="left">';
    if (this.hasDropDown) {
      tmpStr += '<select class="month" name="month" onchange="g_Calendar.selectChange();">';
      for (var i=0;i<this.months.length;i++){
        tmpStr += '<option value="' + i + '"' 
        if (i == this.month) tmpStr += ' selected';
        tmpStr += '>' + this.months[i] + '</option>';
      }
      tmpStr += '</select>';
    } else {
      tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="#" onclick="javascript: g_Calendar.changeMonth(-1);">&darr;</a></td><td class="cal" width="100%" align="center">' + this.months[this.month] + '</td><td class="cal"><a href="#" onclick="javascript: g_Calendar.changeMonth(+1);">&uarr;</a></td></tr></table>';
    }
    tmpStr += '</td>' //<td class="year" width="100%"  align="center">' + this.year + '</td><td class="cal"><a href="#" onclick="javascript: g_Calendar.changeYear(-1);">&darr;</a></td><td class="cal"><a href="#" onclick="javascript: g_Calendar.changeYear(+1);">&uarr;</a>';
    tmpStr += '<td width="40%" align="right" class="cal">';         
    tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="year" width="100%"  align="center">&nbsp;' + this.year + '&nbsp;</td><td class="cal"><a href="#" onclick="javascript: g_Calendar.changeYear(-1);">&darr;</a></td><td class="cal"><a href="#" onclick="javascript: g_Calendar.changeYear(+1);">&uarr;</a></td></tr></table>';
    tmpStr += '</td>';
    tmpStr += '</tr>';
    tmpStr += '</table>';
    var iCount = 1;

    var iFirstDOM = (7+this.getFirstDOM()-g_startDay)%7; // to prevent calling it in a loop

    var iDaysInMonth = this.getDaysInMonth(); // to prevent calling it in a loop
    
    tmpStr += '<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
    tmpStr += '<tr>';
    for (var i=0;i<7;i++){
      tmpStr += '<td align="center" class="calDaysColor">' + this.daysOfWeek[(g_startDay+i)%7] + '</td>';
    }
    tmpStr += '</tr>';
    var tmpFrom = parseInt('' + this.dateFromYear + this.dateFromMonth + this.dateFromDay,10);
    var tmpTo = parseInt('' + this.dateToYear + this.dateToMonth + this.dateToDay,10);
    var tmpCompare;
    for (var j=1;j<=6;j++){
      tmpStr += '<tr>';
      for (var i=1;i<=7;i++){
        tmpStr += '<td width="16" align="center" '
        if ( (7*(j-1) + i)>=iFirstDOM+1  && iCount <= iDaysInMonth ){
          if (iCount==this.day && this.year==this.oYear && this.month==this.oMonth) 
            tmpStr += 'class="calHighlightColor"';
          else {
            tmpStr += (i==7-g_startDay || i==((7-g_startDay)%7)+1) ? 'class="calWeekend"': 'class="cal"';
          }
          tmpStr += '>';
          /* could create a date object here and compare that but probably more efficient to convert to a number
             and compare number as numbers are primitives */
          tmpCompare = parseInt('' + this.year + padZero(this.month) + padZero(iCount),10);
          if (tmpCompare >= tmpFrom && tmpCompare <= tmpTo) {
            tmpStr += '<a class="cal" href="#" onclick="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
          } else {
            tmpStr += '<span class="disabled">' + iCount + '</span>';
          }
          iCount++;
        } else {
          tmpStr += (i==7-g_startDay || i==((7-g_startDay)%7)+1) ? 'class="calWeekend"': 'class="cal"';
          tmpStr += '>&nbsp;';
        }
        tmpStr += '</td>'
      }
      tmpStr += '</tr>'
    }
    tmpStr += '</table></td></tr></table>'
   return tmpStr;
  }
  
  Calendar.prototype.selectChange = function(){
    this.month = this.containerLayer.all.month.selectedIndex;
    this.writeString(this.buildString());
  }
  
  Calendar.prototype.changeYear = function(incr){
    (incr==1) ? this.year++ : this.year--;
    this.writeString(this.buildString());
  }
  
  Calendar.prototype.changeMonth = function(incr){
    if (this.month==11 && incr==1){
      this.month = 0;
      this.year++;
    } else {
      if (this.month==0 && incr==-1){
        this.month = 11;
        this.year--;
      } else {
        (incr==1) ? this.month++ : this.month--;
      }
    }
    this.writeString(this.buildString());
  }
  
  Calendar.prototype.clickDay = function(day){
    var tmp = eval('document.' + this.target);
    if (this.dateFormat=='dd-mmm-yyyy' || this.dateFormat=='dd/mmm/yyyy') tmp.value = day + this.dateDelim + this.months[this.month].substr(0,3) + this.dateDelim + this.year;
    if (this.dateFormat=='dd/mm/yyyy' || this.dateFormat=='dd-mm-yyyy') tmp.value = padZero(day) + this.dateDelim + padZero(this.month+1) + this.dateDelim + this.year;
    if (this.dateFormat=='mm/dd/yyyy' || this.dateFormat=='mm-dd-yyyy') tmp.value = padZero(this.month+1) + this.dateDelim + padZero(day) + this.dateDelim + this.year;
    if (this.dateFormat=='yyyy-mm-dd') tmp.value = this.year + this.dateDelim + padZero(this.month+1) + this.dateDelim + padZero(day);
    
    if (browser.dom || browser.ie4){
      this.containerLayer.style.visibility='hidden'
    }
  }
  
  Calendar.prototype.writeString = function(str){
    if (browser.dom || browser.ie4){
      this.containerLayer.innerHTML = str;
    }
  }
  
  Calendar.prototype.showSAS = function(event, target, bHasDropDown, dateFormat, offsetX, offsetY){
 	  X_offset = offsetX;
 	  Y_offset = offsetY;
     this.show(event, target, bHasDropDown, dateFormat);
  }
   
  Calendar.prototype.show = function(event, target, bHasDropDown, dateFormat, dateFrom, dateTo){
    // calendar can restrict choices between 2 dates, if however no restrictions
    // are made, let them choose any date between 1900 and 3000
    this.dateFrom = (dateFrom) ? dateFrom : new Date(1900,0,1);
    this.dateFromDay = padZero(this.dateFrom.getDate());
    this.dateFromMonth = padZero(this.dateFrom.getMonth());
    this.dateFromYear = this.dateFrom.getFullYear();
    this.dateTo = (dateTo) ? dateTo : new Date(3000,0,1);
    this.dateToDay = padZero(this.dateTo.getDate());
    this.dateToMonth = padZero(this.dateTo.getMonth());
    this.dateToYear = this.dateTo.getFullYear();
    this.hasDropDown = bHasDropDown;
    this.dateFormat = (dateFormat) ? dateFormat : 'dd-mmm-yyyy';
     
    switch (this.dateFormat){
      case 'dd-mmm-yyyy':
      case 'dd-mm-yyyy':
      case 'yyyy-mm-dd':
        this.dateDelim = '-';
            break;
      case 'dd/mm/yyyy':
      case 'mm/dd/yyyy':
      case 'dd/mmm/yyyy':
        this.dateDelim = '/';
            break;
    }
    if (browser.dom || browser.ie4){
      if ((this.containerLayer.style.visibility=='visible')&&(this.target==target)) {
        this.containerLayer.style.visibility='hidden';
        return;
      }
    }
    if (browser.ie5 || browser.ie4){
      var event = window.event;
      var obj = event.srcElement;
     // alert('name' + obj.name)
      x = getAbsoluteX( obj );          
      y = getAbsoluteY( obj );          
      
      this.containerLayer.style.left = x + X_offset;
 	     
      if (y>0) this.containerLayer.style.top = y + Y_offset;
    }
    this.target = target;
    var tmp = eval('document.' + this.target);
    if (tmp && tmp.value && tmp.value.split(this.dateDelim).length==3 && tmp.value.indexOf('d')==-1){
      var atmp = tmp.value.split(this.dateDelim)
      
      switch (this.dateFormat){
        case 'dd-mmm-yyyy':
        case 'dd/mmm/yyyy':
          for (var i=0;i<this.months.length;i++){
            if (atmp[1].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
              this.month = this.oMonth = i;
              break;
            }
          }
          this.day = parseInt(atmp[0],10);
          this.year = this.oYear = parseInt(atmp[2],10);
          break;
        case 'dd/mm/yyyy':
        case 'dd-mm-yyyy':
          this.month = this.oMonth = parseInt(atmp[1]-1,10); 
          this.day = parseInt(atmp[0],10);
          this.year = this.oYear = parseInt(atmp[2],10);
          break;
        case 'mm/dd/yyyy':
        case 'mm-dd-yyyy':
          this.month = this.oMonth = parseInt(atmp[0]-1,10);
          this.day = parseInt(atmp[1],10);
          this.year = this.oYear = parseInt(atmp[2],10);
          break;
        case 'yyyy-mm-dd':
          this.month = this.oMonth = parseInt(atmp[1]-1,10);
          this.day = parseInt(atmp[2],10);
          this.year = this.oYear = parseInt(atmp[0],10);
          break;
      }
    } else { // no date set, default to today
      var theDate = new Date();
      this.year = this.oYear = theDate.getFullYear();
      this.month = this.oMonth = theDate.getMonth();
      this.day = this.oDay = theDate.getDate();
    }
    this.writeString(this.buildString());
    // and then show it!
    if (browser.dom || browser.ie4){
      this.containerLayer.style.visibility='visible';
    }
  }
  
  Calendar.prototype.hide = function(){
    if (browser.dom || browser.ie4){
      this.containerLayer.style.visibility='hidden';
    }
  }
  
  function calClick(e){
    if (browser.ie4 || browser.ie5) e = window.event;
    if (browser.ie4 || browser.ie5){
      // extra test to see if user clicked inside the calendar but not on a valid date, we don't want it to disappear in this case
      var bTest = (e.x > parseInt(g_Calendar.containerLayer.style.left,10) && 
                   e.x <  (parseInt(g_Calendar.containerLayer.style.left,10)+125) && 
                   e.y < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && 
                   e.y > parseInt(g_Calendar.containerLayer.style.top,10)
                  );
      if (e.srcElement.name!='imgCalendar' && 
          e.srcElement.name!='month' && 
          e.srcElement.name!='year' && 
          !bTest & 
          typeof(e.srcElement)!='object'){
            g_Calendar.hide(); 
          }
    }
  }
        
  // utility function
  function moveCal(e){
  	moveOnDrag(e,'calContainer')
  }

  function getAbsoluteX( obj ) {
    x = 0;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft - obj.scrollLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    return x;
  }
  
  function getAbsoluteY( obj ) {
    y = 0;
    while (obj.offsetParent != null) {
      y += obj.offsetTop - obj.scrollTop;
      obj = obj.offsetParent;
    }
    y += obj.offsetTop;
    return y;
  }

  function padZero(num) {
    return ((num <= 9) ? ("0" + num) : num);
  }
  
  // Finally licked extending  native date object;
  Date.isLeapYear = function(year){ if (year%4==0 && ((year%100!=0) || (year%400==0))) return true; else return false; }
  Date.daysInYear = function(year){ if (Date.isLeapYear(year)) return 366; else return 365;}
  var DAY = 1000*60*60*24;
  Date.prototype.addDays = function(num){
    return new Date((num*DAY)+this.valueOf());
  }     
    
  // REMEMBER to add createCalendar to onload event of the body tag
  function createCalendar( lingua ) {
  //	alert('create calendar');
    new Calendar(new Date());
  }


