

// Gerardo Lombardo, basato sull'esempio di DDenis Gritcyuk - 23/05/2008

var IE = document.all?true:false
if (document.layers){
document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove = getMouseXY;

var tempX = 0
var tempY = 0

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  return true
}

function initCalendario() {

	var objContenitore = document.body;

	if ( !objContenitore ) return;

	var objDiv = document.createElement("div");
	objDiv.setAttribute('id','contenitoreCalendario');
	objDiv.style.display ='none';

	var objDiv2 = document.createElement("div");
	objDiv2.setAttribute('id','sfondoCalendario');
	objDiv2.style.display ='none';
	objDiv2.onmouseover = function() { closeCalendario(); };

	objContenitore.appendChild(objDiv);
	objContenitore.appendChild(objDiv2);

}

function show_calendar(str_target, str_datetime,lingua) {


	if ( lingua == "eng" ) {
		var arr_months = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",	"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];
		var week_days = ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"];
	} else if ( lingua=="deu" ) {
		var arr_months = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",	"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];
		var week_days = ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"];
	} else {
		var arr_months = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",	"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];
		var week_days = ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"];
	}

	var str_target2="''";
	if ( str_target == 'document.contatti.form_dataPartenza' ) var str_target2='document.contatti.form_dataArrivo';

	var n_weekstart = 1; // day week starts from (normally 0 or 1)
	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var header = 
		"<table cellspacing=\"0\" border=\"0\" width=\"150\">\n"+
		"<tr><td bgcolor=\"#CFCFCF\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#CFCFCF\"><a href=\"javascript:;\" onclick=\"show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
		"<img src=\""+urlSito+"img/calendario/prev.png\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#CFCFCF\" colspan=\"5\"><div align=\"center\">"+
		"<span class=\"testoNormale\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</span></div></td>\n"+
		"	<td bgcolor=\"#CFCFCF\" align=\"right\"><a href=\"javascript:;\" onclick=\"show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
		"<img src=\""+urlSito+"img/calendario/next.png\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n";
	

	var dt_current_day = new Date(dt_firstday);
	var current_month = (new Date()).getMonth();
	var current_day = (new Date()).getDate();

	// print weekdays titles
	header += "<tr>\n";
	var body = "";
	for (var n=0; n<7; n++)
		body += "	<td bgcolor=\"#336699\">"+
		"<span class=\"testoNormaleBianco\">"+
		week_days[(n_weekstart+n)%7]+"</span></td>\n";
	// print calendar table
	body += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		body += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (current_day == dt_current_day.getDate() &&
					current_month == dt_datetime.getMonth() )
					// print current date 
					body += "	<td bgcolor=\"#FFB6C1\"><div class=\"link\">";
				else if (dt_current_day.getDay() == 0 )
					// weekend days
					body += "	<td bgcolor=\"#D2D9DF\"><div class=\"link\">";
				else
					// print working days of current month
					body += "	<td bgcolor=\"#FFFFFF\"><div class=\"link\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth()) 
					// print days of current month
					body += "<a href=\"javascript:;\" onclick=\""+str_target+
					".value='"+dt2dtstr(dt_current_day)+"';closeCalendario();form_controllaData("+str_target+","+str_target2+");\">"+
					dt_current_day.getDate()+"</a></div></td>\n";

				dt_current_day.setDate(dt_current_day.getDate()+1);
				
		}
		// print row footer
		body += "</tr>\n";
	}
	// print calendar footer
	var footer=
		"</table>\n" +
		"</td>\n</tr>\n</table>\n";


	var contenitoreCalendario = document.getElementById('contenitoreCalendario');
	var sfondoCalendario = document.getElementById('sfondoCalendario');

	if ( contenitoreCalendario.style.display=="none" ) {
		contenitoreCalendario.style.top=(tempY-15)+"px";
		contenitoreCalendario.style.left=(tempX-15)+"px";
	}

	contenitoreCalendario.innerHTML = header+body+footer;
	new Effect.Appear(contenitoreCalendario, {duration:0.5,
		afterFinish: function() {
			if ( sfondoCalendario ) sfondoCalendario.style.display="inline";
		}
	});

}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {

	var slash = str_datetime.lastIndexOf("/");
	var anno = str_datetime.substr(slash+1,str_datetime.length-slash);

	if ( anno.length == 2 && parseInt(slash) > 0 ) str_datetime = str_datetime.substr(0,slash+1)+'20'+anno;
		else if ( anno.length < 4 ) str_datetime = "";

	var re_date = /^(\d+)\/(\d+)\/(\d+)/;

	if (!re_date.exec(str_datetime))
		return new Date();
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
}
function dt2dtstr (dt_datetime) {
	return (new String (dt_datetime.getDate()+"/"+(dt_datetime.getMonth()+1)+"/"+dt_datetime.getFullYear()));
}
function closeCalendario() {
	var contenitoreCalendario = document.getElementById('contenitoreCalendario');
	var sfondoCalendario = document.getElementById('sfondoCalendario');

	if ( sfondoCalendario.style.display!="none" ) sfondoCalendario.style.display="none";
	if ( contenitoreCalendario.style.display!="none") new Effect.Fade(contenitoreCalendario, {duration:0.5});
}
function cleanFieldData(field) {
	if ( field.value == "gg/mm/aa" ) field.value="";
}

addLoadEvent(initCalendario);