// JavaScript Document
// caleder javascript function
// 23 marzo 2009 - 15.01
// function addlink: aggiunge data e link a un array x collegarlo al calendario
// function ModData: modifica la data in formato DD MMM, YYYY
// function ModTime: passa l'ora del post
// function NumComment: restituisce il numero dei commenti + link
// function calender: genera il calendario

monthnames = new Array(
"Gennaio",
"Febbraio",
"Marzo",
"Aprile",
"Maggio",
"Giugno",
"Luglio",
"Agosto",
"Settembre",
"Ottobre",
"Novembre",
"Dicembre");

linkcount=0;
linkdays = new Array();
//Array.prototype.addlink = addlink;

function addlink(day, month, year, href) {
	var entry = new Array(3);
	entry[0] = day;
	entry[1] = month;
	entry[2] = year;
	entry[3] = href;
	//this[linkcount++] = entry;
	linkdays[linkcount] = entry[0] + '@' + entry[1] + '@' + entry[2] + '@' + entry[3];
	linkcount++;
	}


	function ModData(str, strLink) { 
		var arrMonths = ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic']; 
		var arrDate = str.split('/'); 
		if (arrDate.length!=3) return ' '; 
		//var date = new Date(arrDate[2], arrDate[1]-1, arrDate[0]); 
		addlink(arrDate[0], arrDate[1], arrDate[2] , strLink);
		var day =  arrDate[0];
		var month = arrMonths[arrDate[1]-1]; 
		var year = arrDate[2]; 
		
		return day + ' ' + month + ', ' + year; 
	} 
	
	function ModTime(str) {
		return str
	}
	
	function NumComment(str) {
		//str='commenti';
		var a = 0;
		//controllo se la stringa in uscita è nella forma commenti (1) oppure solo commenti e sostituisco nel <a la parola
		var a = str.indexOf('commenti (');
		if (a == -1) {
			var result = str.replace("commenti", ' (#)');
		}
		else {
			var result = str.replace("commenti ", ' ');
		}
		
		return result;
	}

	function Elenco(idSource) {
		var str = document.getElementById(idSource);
		var testo = str.value;
		testo=testo.replace(/\s+/g," "); 
		var a = 0;
		//controllo se la stringa in uscita è nella forma commenti (1) oppure solo commenti e sostituisco nel <a la parola
		var a = testo.indexOf('<br />');
		if (a == -1) {
			testo = testo + '<br />';
		}
		var arr = testo.split('<br />'); 
		testo= ' ';
		testo= '<ul>';
		var len = arr.length;
		
		for (s=0; s < len-1; s++) {
			testo= testo + '<li>' + arr[s] + '</li>'; 
			} 
		testo = testo + '</ul>'
		document.write(testo);
		return ' ' 
	}


//linkdays = new Array();
monthdays = new Array(12);
monthdays[0]=31;
monthdays[1]=28;
monthdays[2]=31;
monthdays[3]=30;
monthdays[4]=31;
monthdays[5]=30;
monthdays[6]=31;
monthdays[7]=31;
monthdays[8]=30;
monthdays[9]=31;
monthdays[10]=30;
monthdays[11]=31;

function Mese() {
	todayDate=new Date();
	thismonth=todayDate.getMonth();
	thisyear=todayDate.getFullYear();
	document.write(monthnames[thismonth] + " " + thisyear);
	return " "
}

function calender() {
	todayDate=new Date();
	thisday=todayDate.getDay();
	thismonth=todayDate.getMonth();
	thisyear=todayDate.getFullYear();
	
	thisdate=todayDate.getDate();

	if (((thisyear % 4 == 0) && !(thisyear % 100 == 0)) || (thisyear % 400 == 0)) monthdays[1]++;

	startspaces=thisdate;
	while (startspaces > 7) startspaces-=7;
	startspaces = thisday - startspaces + 1;
	if (startspaces < 0) startspaces+=7;
	document.write("<table class=calender border=0 cellspacing=0 cellpadding=5>" );<!--Border size-->
	
	document.write("<tr>"); 
	document.write("<td class=cellaDays>D</td>");
	document.write("<td class=cellaDays>L</td>");
	document.write("<td class=cellaDays>M</td>");
	document.write("<td class=cellaDays>M</td>");
	document.write("<td class=cellaDays>G</td>");
	document.write("<td class=cellaDays>V</td>");
	document.write("<td class=cellaDays>S</td>");
<!--Last color chage here-->
	document.write("</tr>");
	document.write("<tr>");

	//ciclo per inserire le celle vuote del primo giorno 
	for (s=0;s<startspaces;s++) {
		document.write("<td class=cella></td>"); 
	}
	count=1;
	// ciclo di tutti i giorni
	while (count <= monthdays[thismonth]) {
	//ciclo per inserire le righe
	for (b = startspaces;b<7;b++) {
	linktrue=false;
	document.write("<td ");
	//scorro i link e li inserisco
	if (count==thisdate) {
		document.write("class=cellaOggi>");<!--todays date color-->
		}
	else {
		document.write("class=cella>");
		}
		
	for (c=0;c<linkdays.length;c++) {
			if (linkdays[c] != null) {
				var arrlink = linkdays[c].split('@'); 
				if ((arrlink[1]==thismonth + 1) && (arrlink[0]==count) && (arrlink[2]==thisyear)) {
				document.write("<a href=" + arrlink[3] + '">');
				linktrue=true;
			}
		}
	}
	//scrivo il giorno
	if (count <= monthdays[thismonth]) {
		document.write(count);
	}
	//senno lascio bianco
	else {
		document.write(" ");
		}
	// se è link chiudo con il tag
	if (linktrue)
		document.write("</a>");
		document.write("</td>");
		count++;
	}
		document.write("</tr>");
		document.write("<tr>");
	startspaces=0;
	}
	document.write("</table>");
	document.write("</center>");
	
	return " "
	} 
	
	