var startAt=1;
var imgDir="immagini/";
var cssMenu="border-style:solid;border-width:1px;border-color:#CCCCCC;cursor:pointer;"
var	dayArray=new Array("Domenica","Lunedì","Martedì","Mercoledì","Giovedì","Venerdì", "sabato")
var	monthName=new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre")
var	monthName2=new Array("Gen","Feb","Mar","Apr","Mag","Giu","Lug","Ago","Set","Ott","Nov","Dic")
var	crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear

var date_min, date_max;

var	bPageLoaded=false;
var	ie=document.all;
var	today=new Date();
var	dateNow=today.getDate();
var	monthNow=today.getMonth();
var	yearNow=today.getYear();
var	imgsrc=new Array("drop.gif","left.gif","right.gif");
var	img=new Array();
var	tdStyleStroke="text-decoration:none;color:#999999;text-decoration:line-through;font-family:Verdana,Arial,Helvetica,Geneva,Swiss,SunSans-Regular;font-size:11px;";

var	styleAnchor="text-decoration:none;color:#333333;cursor:pointer;font-family:Verdana,Arial,Helvetica,Geneva,Swiss,SunSans-Regular;font-size:11px;";
var styleBorder="border-style:solid;border-width:1px;border-color:#486683;"
var	styleBgColor="#D5D5D5"
var bShow=false;
var HolidaysCounter=0;
var Holidays=new Array();

for(i=0; i<imgsrc.length; i++){
	img[i]=new Image();
	img[i].src=imgDir+imgsrc[i];
}

document.write("<div onclick='bShow=true' id='calendar' style='z-index:+999;position:absolute;visibility:hidden;'>");
document.write("	<table width='238' style='border-width:1px;border-style:solid;border-color:#A0A0A0;' bgcolor='#FFFFFF'>");
document.write("		<tr bgcolor='#486683'><td>");
document.write("				<table width='228' border='0' cellpadding='0' cellspacing='1'><tr>");
document.write("					<td style='padding:2px;font-family:Verdana,Arial,Helvetica,Geneva; font-size:10px;font-weight:bold;color:#FFFFFF;'>");
document.write("						<span id='caption'></span></td>");
document.write("					<td align='right'>");
document.write("						<a href='' onClick='hideCalendar2(); return false;'><img src='"+imgDir+"close.gif' width='15' height='13' border='0'  onMouseOver=\"this.src='"+imgDir+"close_on.gif'\" onMouseOut=\"this.src='"+imgDir+"close.gif'\"></a></td>");
document.write("				</tr></table>");
document.write("		</td></tr>");
document.write("		<tr><td style='padding:5px' bgColor='#FFFFFF'><span id='content'></span></td></tr>");
document.write("		<tr bgcolor='#E5E5E5'><td style='padding:3px' align='left'><span id='lblToday'></span></td></tr>");
document.write("	</table>");
document.write("</div>");
document.write("<div id='selectMonth' style='z-index:+999;position:absolute;visibility:hidden;'></div>");
document.write("<div id='selectYear' style='z-index:+999;position:absolute;visibility:hidden;'></div>");

if(startAt==0) dayName=new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
else dayName = new Array("Lun","Mar","Mer","Gio","Ven","Sab","Dom");

function hideElement(elmID, overDiv){
	if(ie){
		for(i=0; i<document.getElementsByTagName(elmID).length; i++){
			obj=document.getElementsByTagName(elmID)[i];
			if(!obj || !obj.offsetParent) continue;
			objLeft=obj.offsetLeft;
			objTop=obj.offsetTop;
			objParent=obj.offsetParent;
			while(objParent.tagName.toUpperCase()!="BODY" && objParent.tagName.toUpperCase()!="HTML"){
				objLeft+=objParent.offsetLeft;
				objTop+=objParent.offsetTop;
				objParent=objParent.offsetParent;
			}
			objHeight=obj.offsetHeight;
			objWidth=obj.offsetWidth;
			if((overDiv.offsetLeft+overDiv.offsetWidth)<=objLeft);
			else if(( overDiv.offsetTop+overDiv.offsetHeight)<=objTop);
			else if( overDiv.offsetTop>=(objTop+objHeight));
			else if( overDiv.offsetLeft>=(objLeft+objWidth));
			else obj.style.visibility="hidden";
		}
	}
}
 
function showElement(elmID){
	if(ie){
		for(i=0; i<document.getElementsByTagName(elmID).length; i++){
			obj=document.getElementsByTagName(elmID)[i];
			if(!obj || !obj.offsetParent)continue;
			obj.style.visibility="";
		}
	}
}

function HolidayRec(d, m, y, desc){
	this.d=d;
	this.m=m;
	this.y=y;
	this.desc=desc;
}



function addHoliday(d, m, y, desc){
	Holidays[HolidaysCounter++]=new HolidayRec(d, m, y, desc);
}


function init(){
	if(!ie) yearNow+=1900;
	crossobj=document.getElementById("calendar").style;
	crossMonthObj=document.getElementById("selectMonth").style;
	crossYearObj=document.getElementById("selectYear").style;
	monthConstructed=false;
	yearConstructed=false;
	var actDate=new Date();
	var actDay=actDate.getDay();
	document.getElementById("lblToday").innerHTML=" <span style='"+styleAnchor+";cursor:default;'>"+dayArray[actDay]+" "+dateNow+" "+monthName[monthNow]+" "+yearNow+"</span>";
	sHTML1="<span id='spanLeft' style='"+cssMenu+"padding:1px 0px 1px 0px;' onmouseover=\"this.style.borderColor='#FFFFFF'\" onclick='decMonth()' onmouseout=\"clearInterval(intervalID1);this.style.borderColor='#CCCCCC'\" onmousedown=\"clearTimeout(timeoutID1);timeoutID1=setTimeout('StartDecMonth()',500)\" onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp;";
	sHTML1+="<img id='changeLeft' src='"+imgDir+"left.gif' width='12' height='10' border='0'>&nbsp</span>&nbsp";
	sHTML1+="<span id='spanRight' style='"+cssMenu+"padding:1px 0px 1px 0px;' onmouseover=\"this.style.borderColor='#FFFFFF'\" onmouseout=\"clearInterval(intervalID1);this.style.borderColor='#CCCCCC'\" onclick='incMonth()' onmousedown=\"clearTimeout(timeoutID1);timeoutID1=setTimeout('StartIncMonth()',500)\" onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp";
	sHTML1+="<img id='changeRight' src='"+imgDir+"right.gif' width='12' height='10' border='0'>&nbsp</span>&nbsp";
	sHTML1+="<span id='spanMonth' style='"+cssMenu+"padding:1px 0px 1px 0px;' onmouseover=\"this.style.borderColor='#FFFFFF'\" onmouseout=\"this.style.borderColor='#CCCCCC'\" onclick='popUpMonth()'></span>&nbsp;";
	sHTML1+="<span id='spanYear' style='"+cssMenu+"padding:1px 0px 1px 0px;' onmouseover=\"this.style.borderColor='#FFFFFF'\" onmouseout=\"this.style.borderColor='#CCCCCC'\" onclick='popUpYear()'></span>&nbsp;";
	document.getElementById("caption").innerHTML=sHTML1;
	bPageLoaded=true;
}

function hideCalendar2(){
	crossobj.visibility="hidden";
	if(crossMonthObj!=null) crossMonthObj.visibility="hidden";
	if(crossYearObj!=null) crossYearObj.visibility="hidden";
	showElement('SELECT');
	showElement('APPLET');
}

function padZero(num){
	return (num<10)?'0'+num:num ;
}

function constructDate(d,m,y){
	sTmp = dateFormat
	sTmp = sTmp.replace	("%d",padZero(d))
	sTmp = sTmp.replace	("%e",d)
	sTmp = sTmp.replace	("%b",monthName2[m])
	sTmp = sTmp.replace	("%M",monthName[m])
	sTmp = sTmp.replace	("%m",padZero(m+1))
	sTmp = sTmp.replace	("%c",m+1)
	sTmp = sTmp.replace	("%Y",y)
	now = new Date(y,m,d)
	sTmp = sTmp.replace	("%a",dayName[now.getDay()-1])
	sTmp = sTmp.replace	("%W",dayArray[now.getDay()])
	return sTmp.replace ("%y",padZero(y%100))
}

function closeCalendar(){
	var	sTmp
	hideCalendar2();
	ctlToPlaceValue.value=constructDate(dateSelected,monthSelected,yearSelected);
}

function StartDecMonth(){
	intervalID1=setInterval("decMonth()",80);
}

function StartIncMonth(){
	intervalID1=setInterval("incMonth()",80);
}

function incMonth(){
	monthSelected++;
	if (monthSelected>11) {
		monthSelected=0;
		yearSelected++;
	}
	constructCalendar();
}

function decMonth(){
	monthSelected--;
	if (monthSelected<0){
		monthSelected=11;
		yearSelected--;
	}
	constructCalendar();
}

function constructMonth(){
	popDownYear();
	if (!monthConstructed){
		sHTML="";
		for	(i=0; i<12;	i++){
			sName=monthName[i];
			if(i==monthSelected) sName="<B>"+sName+"</B>";
			sHTML+="<tr><td id='m"+i+"' onMouseOver='this.style.backgroundColor=\"#98B2D8\"' onMouseOut=\"this.style.backgroundColor=''\" style='cursor:pointer' onClick='monthConstructed=false;monthSelected="+i+";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;"+sName+"&nbsp;</td></tr>";
		}
		document.getElementById("selectMonth").innerHTML="<table width='80' style='font-family:verdana,arial;font-size:10px;border-width:1px;border-style:solid;border-color:#a0a0a0;' bgColor='#E5E5E5' cellspacing='0' onMouseOver='clearTimeout(timeoutID1)' onMouseOut='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>"+sHTML+"</table>";
		monthConstructed=true;
	}
}

function popUpMonth(){
	constructMonth();
	crossMonthObj.visibility="visible";
	crossMonthObj.left=(parseInt(crossobj.left)+54)+"px";
	crossMonthObj.top=(parseInt(crossobj.top)+26)+"px";
	hideElement('SELECT', document.getElementById("selectMonth"));
	hideElement('APPLET', document.getElementById("selectMonth"));			
}

function popDownMonth(){
	crossMonthObj.visibility="hidden";
}

function incYear(){
	for(i=0; i<7; i++){
		newYear=(i+nStartingYear)+1;
		if(newYear==yearSelected){
			txtYear="&nbsp;<b>"+newYear+"</b>&nbsp;";
		}else{
			txtYear="&nbsp;"+newYear+"&nbsp;";
		}
		document.getElementById("y"+i).innerHTML=txtYear;
	}
	nStartingYear++;
	bShow=true;
}

function decYear(){
	for	(i=0; i<7; i++){
		newYear=(i+nStartingYear)-1;
		if(newYear==yearSelected) txtYear="&nbsp;<b>"+newYear+"</b>&nbsp;";
		else txtYear="&nbsp;"+newYear+"&nbsp;";
		document.getElementById("y"+i).innerHTML=txtYear;
	}
	nStartingYear--;
	bShow=true;
}

function selectYear(nYear){
	yearSelected=parseInt(nYear+nStartingYear);
	yearConstructed=false;
	constructCalendar();
	popDownYear();
}

function constructYear(){
	popDownMonth();
	sHTML=";"
	if (!yearConstructed){
		sHTML="<tr><td align='left' onmouseover='this.style.backgroundColor=\"#98B2D8\"' onmouseout='clearInterval(intervalID1);this.style.backgroundColor=\"\"' style='cursor:pointer'	onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>";
		j=0;
		nStartingYear=yearSelected-3;
		for(i=(yearSelected-3); i<=(yearSelected+3); i++){
			sName=i;
			if(i==yearSelected) sName="<B>"+sName+"</B>";
			sHTML+="<tr><td id='y"+j+"' onmouseover='this.style.backgroundColor=\"#98B2D8\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;"+sName+"&nbsp;</td></tr>";
			j++;
		}
		sHTML+="<tr><td align='left' onmouseover='this.style.backgroundColor=\"#98B2D8\"' onmouseout='clearInterval(intervalID2);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)' onmouseup='clearInterval(intervalID2)'>+</td></tr>";
		document.getElementById("selectYear").innerHTML="<table width='44' style='font-family:verdana,arial; font-size:10px; border-width:1px; border-style:solid; border-color:#a0a0a0;' bgcolor='#E5E5E5' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>"+sHTML+"</table>";
		yearConstructed=true;
	}
}

function popDownYear(){
	clearInterval(intervalID1);
	clearTimeout(timeoutID1);
	clearInterval(intervalID2);
	clearTimeout(timeoutID2);
	crossYearObj.visibility="hidden";
}

function popUpYear(){
	var	leftOffset;
	constructYear();
	crossYearObj.visibility="visible";
	leftOffset=parseInt(crossobj.left)+document.getElementById("spanYear").offsetLeft+6;
	crossYearObj.left=leftOffset+"px";
	crossYearObj.top=(parseInt(crossobj.top)+26)+"px";
}

function WeekNbr(n){
  // Algorithm used:
  // From Klaus Tondering's Calendar document (The Authority/Guru)
  // hhtp://www.tondering.dk/claus/calendar.html
  // a = (14-month) / 12
  // y = year + 4800 - a
  // m = month + 12a - 3
  // J = day + (153m + 2) / 5 + 365y + y / 4 - y / 100 + y / 400 - 32045
  // d4 = (J + 31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
  // L = d4 / 1460
  // d1 = ((d4 - L) mod 365) + L
  // WeekNumber = d1 / 7 + 1

	year=n.getFullYear();
	month=n.getMonth()+1;
	if(startAt==0){
		day=n.getDate()+1;
	}else{
		day=n.getDate();
	}
	a=Math.floor((14-month)/12);
	y=year+4800-a;
	m=month+12*a-3;
	b=Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400);
	J=day+Math.floor((153*m+2)/5)+365*y+b-32045;
	d4=(((J+31741-(J%7))%146097)%36524)%1461;
	L=Math.floor(d4/1460);
	d1=((d4-L)%365)+L;
	week=Math.floor(d1/7)+1;
	return week;
}

function constructCalendar(){
	var aNumDays=Array (31,0,31,30,31,30,31,31,30,31,30,31);
	var dateMessage;
	var	startDate=new Date(yearSelected,monthSelected,1);
	var endDate;
	if(monthSelected==1){
		endDate=new Date(yearSelected,monthSelected+1,1);
		endDate=new Date(endDate-(24*60*60*1000));
		numDaysInMonth=endDate.getDate();
	}else numDaysInMonth=aNumDays[monthSelected];
	datePointer=0;
	dayPointer=startDate.getDay()-startAt;
	
	if(dayPointer<0) dayPointer=6;
	sHTML="<table border='0' style='font-family:verdana;font-size:10px;'><tr>";
	for(i=0; i<7; i++) sHTML+="<td width='27' align='left'><B>"+dayName[i]+"</B></td>";
	sHTML +="</tr><tr>";
	for(var i=1; i<=dayPointer; i++) sHTML+="<td>&nbsp;</td>";
	for(datePointer=1; datePointer<=numDaysInMonth; datePointer++){
		dayPointer++;
		if((datePointer==odateSelected) && (monthSelected==omonthSelected) && (yearSelected==oyearSelected)) tdStyle=styleAnchor+styleBorder;
		else tdStyle=styleAnchor;

		currDay=(datePointer<10)?"0"+datePointer:datePointer;
		currMonth=((parseInt(monthSelected)+1)<10)?"0"+(parseInt(monthSelected)+1):(parseInt(monthSelected)+1);
		if(date_min=="" || date_max=="" || (parseInt(yearSelected+""+currMonth+""+currDay)>=date_min && parseInt(yearSelected+""+currMonth+""+currDay)<=date_max)){
			sHTML+="<td align='left' style='"+tdStyle+"' onMouseOver=\"this.bgColor='"+styleBgColor+"'\" onMouseOut='this.bgColor=\"#FFFFFF\"' onClick='dateSelected="+datePointer + ";closeCalendar();'>"
			sHTML+="&nbsp;"+datePointer+"&nbsp;";
		}else{
			sHTML+="<td align='left' style='"+tdStyleStroke+"'>"
			sHTML+="&nbsp;"+datePointer+"&nbsp;";
		}
		if((dayPointer+startAt)%7==startAt) sHTML+="</tr><tr>";
	}
	
	document.getElementById("content").innerHTML=sHTML;
	document.getElementById("spanMonth").innerHTML="&nbsp;"+monthName[monthSelected]+"&nbsp;<img id='changeMonth' src='"+imgDir+"drop.gif' width='12' height='10' border='0'>";
	document.getElementById("spanYear").innerHTML="&nbsp;"+yearSelected+"&nbsp;<img id='changeYear' src='"+imgDir+"drop.gif' width='12' height='10' border='0'>";
}

function popUpCalendar(ctl, d_from, d_to) {
	format="%d/%m/%Y";
	if(d_from!=""){
		d_from=d_from.split("-");
		date_min=d_from[2]+d_from[1]+d_from[0];
	}else{
		date_min="";
	}
	if(d_to!=""){
		d_to=d_to.split("-");
		date_max=d_to[2]+d_to[1]+d_to[0];
	}else{
		date_max="";
	}
	var	leftpos=0;
	var	toppos=0;
	if (bPageLoaded){
		if(crossobj.visibility=="hidden"){
			ctlToPlaceValue	= ctl
			dateFormat=format;
			formatChar = " "
			aFormat	= dateFormat.split(formatChar)
			if (aFormat.length<3){
				formatChar = "/"
				aFormat	= dateFormat.split(formatChar)
				if (aFormat.length<3){
					formatChar = "."
					aFormat	= dateFormat.split(formatChar)
					if (aFormat.length<3){
						formatChar = "-"
						aFormat	= dateFormat.split(formatChar)
						if (aFormat.length<3){
							formatChar = ","
							aFormat	= dateFormat.split(formatChar)
							if (aFormat.length<3){
								formatChar="";
							}
						}
					}
				}
			}
			tokensChanged=0;
			try{
				if(formatChar!=""){
					aData=ctl.value.split(formatChar);
					for(i=0; i<5; i++){
						if((aFormat[i]=="%d") || (aFormat[i]=="%e")){
							dateSelected=parseInt(aData[i], 10);
							tokensChanged++;
						}else if((aFormat[i]=="%m") || (aFormat[i]=="%c")){
							monthSelected=parseInt(aData[i], 10)-1;
							tokensChanged++;
						}else if(aFormat[i]=="%y"){
							yearSelected=1900+parseInt(aData[i], 10);
							tokensChanged++;
						}else if(aFormat[i]=="%Y"){
							yearSelected = parseInt(aData[i], 10)
							tokensChanged++;
						}else if(aFormat[i]=="%M"){
							for	(j=0; j<12;	j++){
								if (aData[i].toLowerCase()==monthName[j].toLowerCase()){
									monthSelected=j;
									tokensChanged++;
								}
							}
						}else if(aFormat[i]=="%b"){
							for	(j=0; j<12;	j++){
								if (aData[i].toLowerCase()==monthName2[j].toLowerCase()){
									monthSelected=j;
									tokensChanged++;
								}
							}
						}
					}
				}
			}catch(e){};

			if ((tokensChanged!=3) || isNaN(dateSelected) || isNaN(monthSelected) || isNaN(yearSelected)){
				dateSelected=parseInt(d_from[0], 10);
				monthSelected=parseInt(d_from[1], 10)-1;
				yearSelected=parseInt(d_from[2], 10);
				if (isNaN(dateSelected) || isNaN(monthSelected) || isNaN(yearSelected) || d_from.length<3){
					dateSelected=dateNow;
					monthSelected=monthNow;
					yearSelected=yearNow;
				}
			}
			odateSelected=dateSelected;
			omonthSelected=monthSelected;
			oyearSelected=yearSelected;
			aTag=ctl;
			do{
				aTag=aTag.offsetParent;
				leftpos+=aTag.offsetLeft;
				toppos+=aTag.offsetTop;
			}while(aTag.tagName!="BODY" && aTag.tagName!="HTML");

			constructCalendar (1, monthSelected, yearSelected);
			pLeft=ctl.offsetLeft+leftpos;
			pTop=ctl.offsetTop+toppos+ctl.offsetHeight+2;
			crossobj.top=pTop+"px";
			crossobj.left=pLeft+"px";
			crossobj.visibility="visible";
			hideElement('SELECT', document.getElementById("calendar"));
			hideElement('APPLET', document.getElementById("calendar"));			
			bShow=true;
		}else{
			hideCalendar2();
			if(ctlNow!=ctl) popUpCalendar(ctl, format);
		}
		ctlNow = ctl
	}
}

function hideCalendar(){
	if(!bShow) hideCalendar2()
	bShow=false;
}

document.onclick = function() {
	hideCalendar();	
}
init();

