function DM_calendar(id, name, yearFrom, yearTo) {
 this.date = new Date();
 this.days = new Array();
 this.months = new Array();
 this.years = new Array();
 this.hours = new Array();
 this.minutes = new Array();
 this.year = Number(this.date.getFullYear());
 this.formObject = new Object();
 this.id = id;
 this.name = name;
 this.showAll = true;
 this.yearFrom = this.year;
 this.yearTo = this.year + 2;
 if (yearFrom!=undefined) this.yearFrom = yearFrom;
 if (yearTo!=undefined) this.yearTo = yearTo;
 this.init();
}

DM_calendar.prototype.init = function() {
 var t;
 var m = ["GEN", "FEB", "MAR", "APR", "MAG", "GIU", "LUG", "AGO", "SET", "OTT", "NOV", "DIC"];
 for(var i = 0; i < m.length; i++) {
  this.months.push(m[i]);
 }
 for(var i = this.yearFrom; i < this.yearTo; i++) {
  this.years.push(i.toString());
 }
 for(var i = 0; i < 24; i++) {
  t = (i+1).toString();
  if (t.length==1) t = "0" + t;
  this.hours.push(t);
 }
 this.minutes = ["00", "15", "30", "45"];
}

DM_calendar.prototype.setValue = function(d, mo, y, h, mi) {
 this.date = new Date(y, mo - 1, d, h, mi);
 this.year = Number(this.date.getFullYear());
}

DM_calendar.prototype.setValue2 = function(d) {
 this.date = new Date(d);
 this.year = Number(this.date.getFullYear());
 this.setSelectValues();
}

DM_calendar.prototype.getValue = function() {
 return this.date;
}

DM_calendar.prototype.bind = function(formObject) {
 this.formObject = formObject;
}

DM_calendar.prototype.onChange = function() {
}


DM_calendar.prototype.setInitValues = function() {
 var e = this.months;
 for(var i = 0; i < e.length; i++) {
  var o = new Option(e[i], i + 1);
  this.formObject["month_"+this.id].options[i] = o;
 }

 e = this.years;
 for(var i = 0; i < e.length; i++) {
  var o = new Option(e[i], e[i]);
  this.formObject["year_"+this.id].options[i] = o;
 }

 if (this.showAll) {
  e = this.hours;
  for(var i = 0; i < e.length; i++) {
   var o = new Option(e[i], e[i]);
   this.formObject["hour_"+this.id].options[i] = o;
  }

  e = this.minutes;
  for(var i = 0; i < e.length; i++) {
   var o = new Option(e[i], e[i]);
   this.formObject["minute_"+this.id].options[i] = o;
  }
 }
}

DM_calendar.prototype.display = function(show) {
 this.showAll = true;
 if(show==false) this.showAll = false;
 document.write("<select name=\"day_"+this.id+"\" size=\"1\" onchange=\""+this.name+".dataChange();\"></select>");
 document.write("<select name=\"month_"+this.id+"\" size=\"1\" onchange=\""+this.name+".change();\"></select>");
 document.write("<select name=\"year_"+this.id+"\" size=\"1\" onchange=\""+this.name+".change();\"></select>");
 if(this.showAll) {
  document.write("<select name=\"hour_"+this.id+"\" size=\"1\" onchange=\""+this.name+".dataChange();\"></select>");
  document.write("<select name=\"minute_"+this.id+"\" size=\"1\" onchange=\""+this.name+".dataChange();\"></select>");
 }
 document.write("<input type=\"hidden\" name=\"data_"+this.id+"\" value=\"\">");
 this.setInitValues();
 this.setSelectValues();
}

DM_calendar.prototype.dataChange = function() {
 var d = Number(this.formObject["day_"+this.id].value);
 var mo = Number(this.formObject["month_"+this.id].value) - 1;
 var y = Number(this.formObject["year_"+this.id].value);
 var h = 12;
 var mi = 0;
 if(this.showAll) {
  h = Number(this.formObject["hour_"+this.id].value);
  mi = Number(this.formObject["minute_"+this.id].value);
 }
 if(this.showAll) {
  this.formObject["data_"+this.id].value = d+"/"+(mo+1)+"/"+y+" "+h+":"+mi+":00";
 } else {
  this.formObject["data_"+this.id].value = d+"/"+(mo+1)+"/"+y;
 }
 this.date = new Date(y, mo, d, h, mi);
 this.onChange();
}

DM_calendar.prototype.change = function() {
 var d = Number(this.formObject["day_"+this.id].value);
 var mo = Number(this.formObject["month_"+this.id].value) - 1;
 var y = Number(this.formObject["year_"+this.id].value);
 var h = 12;
 var mi = 0;
 if(this.showAll) {
  h = Number(this.formObject["hour_"+this.id].value);
  mi = Number(this.formObject["minute_"+this.id].value);
 }
 this.date = new Date(y, mo, d, h, mi);
 this.setSelectValues();
}

DM_calendar.prototype.setSelectValues = function() {
 var d = this.date.getDate();
 var mo = this.date.getMonth();
 var y = this.date.getFullYear();
 var h = this.date.getHours();
 var mi = this.date.getMinutes();
 var ld = 31;
 mi = Math.floor(mi/15) * 15
 var nd = new Date(y, mo, ld, h, mi);
 while(nd.getMonth()!=mo) {
  ld--;
  nd = new Date(y, mo, ld, h, mi);
 }

 var e = this.formObject["day_"+this.id];
 while(e.options.length > 0) {
  e.options[0] = null;
 }

 for(var i = 0; i < ld; i++) {
  t = (i+1).toString();
  if (t.length == 1) t = "0" + t;
  var o = new Option(t, t);
  this.formObject["day_"+this.id].options[i] = o;
 }

 this.setOptions("day", d);
 this.setOptions("month", mo + 1);
 this.setOptions("year", y);
 if(this.showAll) {
  this.setOptions("hour", h);
  this.setOptions("minute", mi);
 }
 this.dataChange();
}

DM_calendar.prototype.setOptions = function(par, val) {
 var e = this.formObject[par+"_"+this.id].options;
 for(var i = 0; i<e.length; i++) {
  if(Number(e[i].value) == val) {
   e[i].selected = true;
   e[i].defaultSelected = true;
  }
 }
}
