/*
   christian russo for justweb ( r3i at www.justweb.it )
   Classe per la creazione di un calendario interattivo in html valido e supportato da Firefox e Internet Explorer.
   Per favore non eliminare questa intestazione: grazie *^^
*/
   
   function Calendario( nomeThis, lingua, attiva_passato, idContenitore, idIntestazione, idCorpo, idPiede ) {
      if( idPiede ) this.idPiede = idPiede;
      else this.idPiede = "calendario_piede";
      if( idCorpo ) this.idCorpo = idCorpo;
      else this.idCorpo = "calendario_corpo";
      if( idIntestazione ) this.idIntestazione = idIntestazione;
      else this.idIntestazione = "calendario_intestazione";
      if( idContenitore ) this.idContenitore = idContenitore;
      else this.idContenitore = "calendario_contenitore";

      this.nomeThis = nomeThis;
		this.attiva_passato = attiva_passato;
      this.campoRestituzione = "";
      this.ultimaChiamata = "";
      this.attivo = 0;

      this.inizializzaListeners = function() {
         var div = document.getElementById( this.idContenitore );
         if( div ) {
            this.attivo = 0;
            eval( "document.getElementById('"+ this.idContenitore+ "').onmouseover = function(){ "+ this.nomeThis+ ".attivo = 0; }" );
            eval( "document.getElementById('"+ this.idContenitore+ "').onmouseout = function(){ "+ this.nomeThis+ ".attivo = 1 }" );
            eval( "document.onmousedown = function(){ if( ( "+ this.nomeThis + ") && ("+ this.nomeThis+ ".attivo == 1 )) { document.getElementById('"+ this.idContenitore+ "').style.display='none'; "+ this.nomeThis+".selectVisibili(true); "+ this.nomeThis+ ".attivo=0;  } return true;}" );
         }
      }

      this.display = function() {
         var div = document.getElementById( this.idContenitore );
         if( div ) {
            div.style.display = "";
            this.selectVisibili( false );
         }
      }

      this.isOggi = function( data ) {
         giornoO = data.getDate();
         meseO = data.getMonth();
			annoO = data.getFullYear();
         oggi = new Date();
         if( ( oggi.getDate() == giornoO ) && ( ( oggi.getMonth() ) == meseO ) && ( annoO == oggi.getFullYear() ) ) {
            return true;
         }
         else {   return false;  }
      }
		
		this.isPassato = function( data ) {
			//alert( "here I am" );
			oggi = new Date();
			anno1 = data.getFullYear();
			anno2 = oggi.getFullYear();
			if(anno1 > anno2 )	return false;
			if(anno1 == anno2) {
				mese1 = data.getMonth() -1;
				mese2 = oggi.getMonth() -1;
				if(mese1 > mese2 )	return false;
				if( mese1 == mese2) {
						giorno1 = data.getDate();
						giorno2 = oggi.getDate();
						if(giorno1>giorno2) return false;
				}
			}
			return true;
		}

      this.isVacanza = function( data ) {
         mese = data.getMonth() + 1;
         giorno = data.getDate();
         for( IndiceK =0; IndiceK< this.giorniVacanze.length; IndiceK++ ) {
            temp = this.giorniVacanze[ IndiceK ].split( "-", 3 );
            giornoV = parseInt( temp[0] );
            meseV = parseInt( temp[1] );
            if( ( giornoV == giorno ) && ( mese == meseV ) ) return temp[2];
         }
         return "";
      }

      this.isPasqua = function( data ) {
         anno = data.getFullYear();
         giorno = data.getDate();
         mese = data.getMonth() + 1;
         correzione = 0;
         if( anno < 1700 ) correzione = 4;
         else if( anno < 1800 ) correzione = 5;
         else if( anno < 1900 ) correzione = 6;
         else if( anno < 2100 ) correzione = 0;
      	else if( anno < 2200 ) correzione = 1;
      	else if( anno < 2300 ) correzione = 2;
      	else if( anno < 2500 ) correzione = 3;
         giornoP = ( 19 * ( anno % 19 ) + 24 ) % 30;
         giornoP = 22 + giornoP + ( ( 2 * ( anno % 4 ) + 4 * ( anno % 7 ) + 6 * giornoP + 5 + correzione ) % 7 );
         if( giornoP > 31 ) {
            meseP = 4;
            giornoP -= 31;
         }
         else meseP = 3;

         if( giornoP == giorno && mese == meseP ) return this.nomePasqua;
         else return "";
      }

      this.visualizzaInIntestazione = function( testoHTML ) {
         var intestazione = document.getElementById( this.idIntestazione );
         if( ! intestazione ) return;
         intestazione.innerHTML = testoHTML;
      }
      this.visualizzaInCorpo = function( testoHTML ) {
         var corpo = document.getElementById( this.idCorpo );
         if( !corpo ) return;
         corpo.innerHTML = testoHTML;
      }
      this.visualizzaInPiede = function( testoHTML ) {
         var piede = document.getElementById( this.idPiede );
         if( !piede ) return;
         piede.innerHTML = testoHTML;
      }

      this.visualizza = function( mese, anno ) {
         this.ultimaChiamata = "this.visualizza("+ mese+ ","+ anno+ ");";
         html = "";
         data = new Date( anno, mese, 1 );
         inizio = data.getDay();

      // html per l'intestazione
         mese = data.getMonth();
         dataTemp = new Date( data.getFullYear(), data.getMonth(), data.getDate() );
         dataTemp.setMonth( dataTemp.getMonth() -1 );
         azionePrec = this.nomeThis + ".visualizza("+ dataTemp.getMonth() + ","+ dataTemp.getFullYear() +");";
         dataTemp = new Date( data.getFullYear(), data.getMonth(), data.getDate() );
         dataTemp.setMonth( dataTemp.getMonth() + 1 );
         azioneSucc = this.nomeThis + ".visualizza("+ dataTemp.getMonth() + ","+ dataTemp.getFullYear() +");";

			html = "<table cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"155px\"><tr>"+
						"<td width=\"23\"><a href=\"javascript:\" onclick=\""+ azionePrec+ "\"><img border=\"0\" src=\"images/calendario/prev.gif\" alt=\"Prev\"></a></td>" +
						"<td width=\"109\" class=\"calendario_tit\" align=\"center\">"+ this.nomiMesi[mese]+ "&nbsp;"+ data.getFullYear() + "</td>"+
						"<td width=\"23\"><a href=\"javascript:\" onclick=\""+ azioneSucc+ "\"><img border=\"0\" src=\"images/calendario/next.gif\" alt=\"Next\"></a></td>"+
						"</tr></table>";

         this.visualizzaInIntestazione( html );

      // html per il corpo

			html = "<table cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"155px\"><tr><td class=\"calendario_giorni\">"+
						"<table align=\"center\" cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"145px\"><tr>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 0 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 1 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 2 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 3 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 4 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 5 ] + "</h1></td><td width=\"2\"></td>" +
						"<td width=\"19\" align=\"center\"><h1>"+ this.nomi[ 6 ] + "</h1></td>" +
						"</tr></table></td></tr></table>"+
						"<table cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"155px\">" +
						"<tr><td border=\"1px solid black\">";

         FlagRiempi = 0;
         mese = data.getMonth();
         meseS = mese;
         if( inizio == 0 ) inizio = 7;
         if( inizio < 0 )  inizio = Math.abs( inizio );

         for( Indice = 0; Indice <= 36 ; Indice ++ ) {

            if( ( Indice % 7 ) == 0 ) html +="<tr><td width=\"4\">&nbsp;</td>";
            if( ( FlagRiempi == 0 ) && ( Indice == ( inizio - 1 ) ) ) FlagRiempi = 1;
				if( mese != meseS ) FlagRiempi = 0;

            if( FlagRiempi == 1 ) {

					idT = "class='calendario_numeri' ";
					azione5 = this.nomeThis + ".restituisciData(" + data.getDate()+ ","+ data.getMonth()+ ","+ data.getFullYear()+ ");";
					contenuto_cella = "<a href=\"javascript:;\" onclick=\""+ azione5+ "\">"+ data.getDate() + "</a>";

				   //if( ( Indice % 7 ) == 6 ) idT = "class='calendario_vacanza'";
					//if( ( Indice % 7 ) != 5 && ( Indice % 7 ) != 6 ) idT = "class='calendario_numeri'";
					if( this.isVacanza( data ) ) idT = "class='calendario_vacanza' title=\""+ this.isVacanza( data ) + "\" ";
					if( this.isPasqua( data ) ) idT = "class='calendario_vacanza' title=\""+ this.isPasqua( data ) + "\" ";
					if( this.isOggi( data ) ) idT = "class='calendario_active' ";
					else if( ( this.attiva_passato == 0 ) && ( this.isPassato( data ) ) ) {
						idT = "class='calendario_numeri_no' ";
						if( this.isVacanza( data ) ) idT = "class='calendario_vacanza' title=\""+ this.isVacanza( data ) + "\" ";
						if( this.isPasqua( data ) ) idT = "class='calendario_vacanza' title=\""+ this.isPasqua( data ) + "\" ";
						contenuto_cella =  ""+ data.getDate();
					}

					html += "<td "+ idT + " align=\"center\">"+ contenuto_cella+ "</td>";
					data.setDate( data.getDate() +1 );
					meseS = data.getMonth();
            }
            if( FlagRiempi == 0 ) {
               html += "<td class=\"calendario_numeri\" align=\"center\">&nbsp;</td>";
            }
            if( ( Indice % 7 ) == 6 ) html += "<td width=\"4\">&nbsp;</td></tr>";
         }
         html += "</table></td></tr></table>";

         this.visualizzaInCorpo( html );

      //html per il piede
         oggiT = new Date();
			azione5 = this.nomeThis + ".restituisciData(" + oggiT.getDate()+ ","+ oggiT.getMonth()+ ","+ oggiT.getFullYear()+ ");";
			html = "<table cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"155px\" title=\"Copywright by justweb.it\" class=\"calendario_bottom\">"
						+"<tr><td>&nbsp;</td>"
						+"<td width=\"73\" align=\"center\"><a href=\"javascript:;\" onclick=\""+ azione5+ "\">"+ this.etichette[0]+"</a></td>"
						+ "<td align=\"right\" width=\"39\"><a target=\"blank\" href=\"http://www.justweb.it\">&nbsp;&nbsp;&nbsp;</a></td></tr></table>";

         this.visualizzaInPiede( html ); 

         return;
      }

      this.impostaLingua = function( lingua, flag ) {
         switch( lingua ) {
            case "Inglese" :
               this.nomi = Array( "M", "Tu", "W", "Th", "F", "Sa", "Su" );
               this.nomiMesi = Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
               this.nomiMesiCorti = Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" );
               this.giorniVacanze = Array( "25-12-Christmas", "31-12-New Year's Day", "06-1-Epifania", "25-4-Liberation", "1-5-Festa dei lavoratori", "15-8-Ferragosto" );
               this.nomePasqua = "Easter";
               this.etichette = Array( "TODAY" );
               break;
            default :
               this.nomi = Array( "L", "M", "M", "G", "V", "S", "D" );
               this.nomiMesi = Array( "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" );
               this.nomiMesiCorti = Array( "Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Sett", "Ott", "Nov", "Dic" );
               this.giorniVacanze = Array( "25-12-Natale", "31-12-Capodanno", "06-1-Epifania", "25-4-Liberazione", "1-5-Festa lavoratori", "15-8-Ferragosto" );
               this.nomePasqua = "Pasqua";
               this.etichette = Array( "OGGI" );
         }
         if( flag != 1 ) this.display();
         eval( this.ultimaChiamata );
         //this.attivo = 1;
      }

      this.richiediData = function( event, idCampo, coordinataX, coordinataY ){
         this.campoRestituzione = idCampo;
         var contenitore = document.getElementById( this.idContenitore );
  		 scrollTop = 0;
		 scrollLeft = 0;
		 if (document.documentElement && document.documentElement.scrollTop) {
		 	scrollTop = document.documentElement.scrollTop ;
		 	scrollLeft = document.documentElement.scrollLeft;
		 }
		else if (document.body && document.body.scrollTop) {
			scrollTop = document.body.scrollTop ;
			scrollLeft = document.body.scrollLeft ;
		}

         if( contenitore ) {

            coordinataX = parseInt( coordinataX );
            coordinataY = parseInt( coordinataY );
            coordinataXCorrente = 0;
            coordinataYCorrente = 0;
            if( window.event ) {
               coordinataXCorrente = window.event.clientX + scrollLeft;
               coordinataYCorrente = window.event.clientY + scrollTop ;
            }
            else {
               coordinataXCorrente = event.clientX;
               coordinataYCorrente = event.clientY;
            }

            coordinataX += coordinataXCorrente;
            coordinataY += coordinataYCorrente;

            contenitore.style.top = coordinataY + "px";
            contenitore.style.left = coordinataX + "px";

            this.selectVisibili( false );
            contenitore.style.display = "";
            this.attivo = 1;
         }
         return;
      }

      this.restituisciData = function ( giorno, mese, anno ){
         var data = new Date( anno, mese, giorno );
         var contenitore = document.getElementById( this.idContenitore );
         var target = document.getElementById( this.campoRestituzione );
         if( target ) {
               target.value = giorno + "/"+ (mese +1 )+ "/"+ anno;
         }
         if( contenitore ) {
            this.selectVisibili( true );
            contenitore.style.display = "none";
            this.attivo = 0;
         }
         return;
      }

	this.selectVisibili = function( flag )	{
		if( flag == false ) visibilita = "hidden";
		else visibilita = "visible";

		var vettore = new Array();
		vettore = document.getElementsByTagName("select");

		for( j =0 ; j< vettore.length; j++)
			{	vettore[j].style.visibility = visibilita;	}

		/*
		var numeroFrames = document.frames.length;
		for( j=0; j<numeroFrames; j++) {
				var fr = top.frames[j];
				var	vettore = new Array();
					vettore = fr.document.getElementsByTagName("select");
				for( k =0 ; k< vettore.length; k++)
					{	vettore[k].style.visibility = visibilita;	}
		}
		*/		
	}

      this.impostaLingua( lingua, 1 );
      this.inizializzaListeners();

   }