function Calendario(percorso,attivo,t,tipo) {
    this.base = Modulo ;
    this.base(percorso) ;
    this.date = new Date() ;
    this.tipo = tipo ;
    this.active = attivo!='no' ;
    this.setTime(t) ;
}

Calendario.prototype.clear = function() {
    this.nullDate = true ;
    this.fireDateRefresh() ;
    if(this.tipo=='compatto') {
        document.modulo[this.path+'@giorno'].value = null ;
        document.modulo[this.path+'@mese'].selectedIndex = 0 ;
	document.modulo[this.path+'@anno'].value = null ;
    }

}

Calendario.prototype.layoutYear = function() {
    if(!this.nullDate) {
        document.modulo[this.path+'@mese'].selectedIndex = this.date.getMonth()+1 ;
	document.modulo[this.path+'@anno'].value = this.date.getFullYear() ;
    }
}

Calendario.prototype.layoutMonth = function() {
    
    document.modulo[this.path].value = this.date.getTime() ;
    
    if(this.tipo=='compatto') {

	if(!this.nullDate)
	    document.modulo[this.path+'@giorno'].value = this.date.getDate() ;

    } else {

	var temp = new Date(this.date.getTime()) ;
	temp.setDate(1) ;
	var day = 0 ;
	var first = (temp.getDay() + 6) % 7 ;
	var last = this.getMaximumDay(temp.getMonth(),temp.getFullYear()) ;

	var instance = this ;
	for(w=0;w<6;w++) {
	    for(d=0;d<7;d++) {
		var wd = w*7+d ;
		if(day>=last)
		    day=-1 ;
		if(day>=0 && wd>=first) {
		    day++ ;
		    temp.setDate(day) ;
		}
		var el = document.getElementById(this.name+'_day'+wd) ;
		el.setAttribute('title',temp.toString()) ;
		el.innerHTML = day>0 ? day : "" ;
		el.className = day==this.date.getDate() && !this.nullDate ? 'day_selected' : '' ;
		el.onclick = function() {
		    if(instance.active)
			instance.setDay(this.innerHTML) ;
		}
	    }
	}
    }
}

Calendario.prototype.layout = function() {
    this.layoutYear() ;
    this.layoutMonth() ;
}

Calendario.prototype.getMaximumDay = function(month, year) {
    var d = new Date(year,month+1,1) ;
    return new Date(d.valueOf() - 86400000).getDate() ;
}

Calendario.prototype.setTime = function(msec) {
    this.internalSetting = true ;
    if(msec==='' || isNaN(msec)) {
	//this.date = new Date() ;
	this.nullDate = true ;
    } else {
	this.date.setTime(parseInt(msec)) ;
	this.nullDate = false ;
    }
    this.layout() ;
    this.internalSetting = false ;
    //non propaga il settaggio al listener
}

Calendario.prototype.setYear = function(year) {
    if(isNaN(year)) {
	this.internalSetting = true ;
	document.modulo[this.path+'@anno'].value = this.date.getFullYear() ;
	this.internalSetting = false ;
    } else if(!this.internalSetting) {
	var maxday = this.getMaximumDay(this.date.getMonth(),year) ;
	if(this.date.getDate()>maxday)
	    this.date.setDate(maxday) ;
	this.date.setYear(year) ;
	this.denullize() ;
	this.layoutMonth() ;
	this.fireDateChange() ;
    }
}

Calendario.prototype.setMonth = function(month) {
    if(!this.internalSetting) {
	var maxday = this.getMaximumDay(month,this.date.getFullYear()) ;
	if(this.date.getDate()>maxday)
	    this.date.setDate(maxday) ;
	if(month)//xk c'e' l'option vuota
	    this.date.setMonth(month-1) ;
	this.denullize() ;
	this.layoutMonth() ;
	this.fireDateChange() ;
	pagina.selectClicked = true ;
    }
}

Calendario.prototype.denullize = function() {
    if(this.tipo=='compatto') {
	var annval = document.modulo[this.path+'@anno'].value ;
	var monval = document.modulo[this.path+'@mese'].selectedIndex ;
	var dayval = document.modulo[this.path+'@giorno'].value ;
	this.nullDate = !annval ;
	if(!this.nullDate) {
	    if(!monval) {
		this.date.setMonth(0) ;
		document.modulo[this.path+'@mese'].selectedIndex = 1 ;
	    }
	    if(!dayval)
		this.date.setDate(1) ;
	}
    }
}

Calendario.prototype.setDate = function(date) {
    this.date.setDate(date) ;
    this.fireDateChange() ;
}

Calendario.prototype.setDay = function(day) {
    //metodo a rozzo (pensare a un metodo solamente interno)
    var i ;
    for(i=0;i<49;i++) {
	var el = document.getElementById(this.name+'_day'+i) ;
	if(el)
	    el.className = day==el.innerHTML ? 'day_selected' : '' ;
    }
    this.date.setDate(day) ;
    this.nullDate = false ;

    //propaga il settaggio al listener
    this.fireDateChange() ;
}

Calendario.prototype.fireDateChange = function() {
    this.updateText() ;
    if(this.listener!=null && !this.nullDate)
	this.listener.dateChange(this.date.getTime(), this.toString()) ;
}

Calendario.prototype.fireDateRefresh = function() {
    this.updateText() ;
    if(this.listener!=null)
        if(this.nullDate)
            this.listener.dateRefresh("","") ;
        else
            this.listener.dateRefresh(this.date.getTime(), this.toString()) ;
	
}

Calendario.prototype.updateText = function() {
    document.modulo[this.path+'@text'].value = this.toString() ;
    document.modulo[this.path+'@text'].className = "field_calendar"+(this.nullDate ? "" : "_selected") ;
}

Calendario.prototype.setDateListener = function(l) {
    this.listener = l ;
    this.fireDateRefresh() ;
}

Calendario.prototype.removeDateListener = function() {
    this.listener = null ;
}

Calendario.prototype.toString = function() {
    return this.nullDate ? "" : this.date.getDate()+"/"+(this.date.getMonth()+1)+"/"+this.date.getFullYear()
}

