/* 
* DHTML_Calendar.js $Revision: 1.1.1.1 $ ($Date: 2005/05/06 07:32:52 $)
* Copyright 2005 Eric Pretorious (eric@pretorious.net)
* Distributed under the terms of the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
*/

// <script> // this tag forces visual studio to syntax recognition in .js sources 
// ' versione DHTML_calendar italianizzato e modificato da Aug 2006 07 09 (forza Italia !!!) 

var now;
var currYear,currMonth;
var i,j,currDateObj,firstDay,daysInMonth,sevenDayRows;;
var DHTMLC,prev,curr,next,moniker;
var months	= new Array('Gennaio','Febbraio','Marzo','Aprile','Magio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
var days	= new Array('Domenica','Lunedì','Martedì','Mercoledì','Giovedì','Venerdì','Sabato');

/*
1. Set currMonth & currYear to now.
2. CBE will automatically call windowOnLoad() which, in turn, will...
 a. Create the CrossBrowserElement objects.
 b. Call Calendar() which will...
  i. Calculate the calendar info: firstDay, daysInMonth, sevenDayRows.
  ii. Determine nextMonth, nextYear, prevMonth, & prevYear.
  iii. Call cbeTraverseTree() with a reference to drawCalendar() which will...
   * Populate the innerHTML for each object in the CBE Object Tree.
*/

// Use the present date to set the global variables (used to generate the calender)
now		= new Date();
currYear	= now.getYear();
currMonth	= now.getMonth();

// Some browsers may not be Y2K compliant...
if (currYear < 1900) currYear += 1900;

function Calendar(Month,Year) {
	currDateObj	= new Date(Year,Month,1);

	// Update the global variables (used to generate the calender)
	currMonth	= Month;
	currYear	= Year;
	
	// Perform the basic calculations necessary to generate a calendar
	firstDay	= currDateObj.getDay();	// The day-of-the-week of the first day of the month;
	daysInMonth	= 32 - new Date(Year,Month,32).getDate();
	sevenDayRows	= Math.ceil((firstDay + daysInMonth) / 7);
	
	switch (Month) {
	case 0:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= 11;
		prevYear	= Year - 1;
		break;
	
	case 11:
		nextMonth	= 0;
		nextYear	= Year + 1;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	
	default:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	}
	
	i = 0; j = 1;
	cbeTraverseTree('preorder',DHTMLC,drawCalendar);

	DHTMLC.resizeTo(215, sevenDayRows * 20 + 60);
	moniker.top(sevenDayRows * 20 + 50);
	prev.innerHtml('<A HREF="javascript:Calendar(prevMonth,prevYear);">' + months[prevMonth].substr(0,3) + '</A>');
	curr.innerHtml(months[Month] + ', ' + Year);
	next.innerHtml('<A HREF="javascript:Calendar(nextMonth,nextYear);">' + months[nextMonth].substr(0,3) + '</A>');
}

function drawCalendar(node,level,branch) {
	var HREF;
	if (level == 2 && branch > 2) {
		if (i >= firstDay && j <= daysInMonth) {
		
			// -- questa era l'originale
			//
			// HREF = 'javascript:void(document.forms[0].elements[0].value = \'' + (currMonth + 1) + '/' + j + '/' + currYear +'\')';

			// -- mia versione numero 1, assegnava sempre il valore cliccato all'input "DhtmlCal"
			//if (document.getElementById("DhtmlCal"))
			//	HREF = 'javascript:void(document.getElementById("DhtmlCal").value = "' + (currMonth + 1) + '/' + j + '/' + currYear +'")';
			//else
			//	HREF = '';

			// -- mia versione ultima, se il calendario è stato costruito passando il nome di una funzione
			//    viene invocata tale funzione passandole come parametro la stringa inserita
			//
			if (cChgAction != '')
				HREF = 'javascript:' + cChgAction + '("' + j + '/' + (currMonth + 1) + '/' + currYear +'")';
			else
				HREF = 'javascript:void(document.getElementById("DhtmlCal").value = "' + (currMonth + 1) + '/' + j + '/' + currYear +'")';

			window.status = HREF;
			node.innerHtml("<A HREF='" + HREF + "'>" + j++ + '</A>');
			//node.innerHtml(); // anche questo c'era nella versione originale ma non ho capito a cosa serviva
			node.show();
		} else {
			node.hide();
		}
		i++;
	}
	return true;
}

function windowOnload() {
	DHTMLC	= cbeGetElementById('DHTMLC').cbe;
	prev	= cbeGetElementById('prev').cbe;
	curr	= cbeGetElementById('curr').cbe;
	next	= cbeGetElementById('next').cbe;
	moniker	= cbeGetElementById('moniker').cbe;

	Calendar(currMonth,currYear);
	DHTMLC.clip('auto');
	DHTMLC.show();
	//DHTMLC.addEventListener('drag');
}

