var refMonth=new Array("gennaio","febbraio","marzo","aprile","maggio","giugno","luglio","agosto","settembre","ottobre","novembre","dicembre");
var refDayOfWeek=new Array("domenica","lunedi","martedi","mercoledi","giovedi","venerdi","sabato");
var refSMonth=new Array("gen","feb","mar","apr","mag","giu","lug","ago","set","ott","nov","dic");
var refSDayOfWeek=new Array("dom","lun","mar","mer","gio","ven","sab");
var refPercorso="../../utility/calendario/";


var cal=new CalendarPopup("calDiv");
cal.setReturnFunction("setMultipleValues");
var overCalendar=false;
var inputFieldDate="";
window.onresize=hideCal;
function hideCal() {
document.getElementById('calDiv').style.visibility="hidden";
}
function setMultipleValues(y,m,d,idParam,cntryOfRes) {
if (cntryOfRes=="US")
document.getElementById(idParam).value=(formatDate(y,d,m));
else
document.getElementById(idParam).value=(formatDate(y,m,d));
document.getElementById(idParam).focus();
setTimeout("hideCal()", 200);
}
function AnchorPosition_getPageOffsetLeft(o)
{
var ol=o.offsetLeft;
while((o=o.offsetParent)!=null)
{
ol+=o.offsetLeft;
}
return ol;
}
function AnchorPosition_getPageOffsetTop(o)
{
var ot=o.offsetTop;
while((o=o.offsetParent)!=null)
{
ot+=o.offsetTop;
}
return ot;
}
function PopupWindow_populate(_1f)
{
this.contents=_1f;
this.populated=false;
}
function PopupWindow_refresh()
{
document.getElementById(this.divName).innerHTML=this.contents;
}
function PopupWindow_showPopup(_31)
{
this.x=AnchorPosition_getPageOffsetLeft(document.getElementById(_31)) + this.offsetX;
this.y=AnchorPosition_getPageOffsetTop(document.getElementById(_31)) + this.offsetY;
if(!this.populated&&(this.contents!=""))
{
this.populated=true;
this.refresh();
}
document.getElementById(this.divName).style.left=this.x+"px";
document.getElementById(this.divName).style.top=this.y+"px";
document.getElementById(this.divName).style.visibility="visible";
}
function PopupWindow_hidePopup()
{
document.getElementById(this.divName).style.visibility="hidden";
}
function PopupWindow_hideIfNotClicked()
{
if(!overCalendar)
this.hidePopup();
}
function PopupWindow_hidePopupWindows()
{
for(var i=0;i<popupWindowObjects.length;i++)
{
if(popupWindowObjects[i]!=null)
{
var p=popupWindowObjects[i];
p.hideIfNotClicked();
}
}
}
function PopupWindow_attachListener()
{
window.popupWindowOldEventListener=document.onmouseup;
if(window.popupWindowOldEventListener!=null)
document.onmouseup=new Function("window.popupWindowOldEventListener(); PopupWindow_hidePopupWindows();");
else
document.onmouseup=PopupWindow_hidePopupWindows;
}
function PopupWindow()
{
if(!window.popupWindowIndex)
window.popupWindowIndex=0;
if(!window.popupWindowObjects)
window.popupWindowObjects=new Array();
if(!window.listenerAttached)
{
window.listenerAttached=true;
PopupWindow_attachListener();
}
this.index=popupWindowIndex++;
popupWindowObjects[this.index]=this;
this.popupWindow=null;
this.populated=false;
this.contents="";
this.divName=arguments[0];
this.populate=PopupWindow_populate;
this.refresh=PopupWindow_refresh;
this.showPopup=PopupWindow_showPopup;
this.hidePopup=PopupWindow_hidePopup;
this.hideIfNotClicked=PopupWindow_hideIfNotClicked;
}
function CalendarPopup()
{
var c=new PopupWindow(arguments[0]);
c.offsetX=85;
c.offsetY=0;
c.monthNames=refMonth;
c.dayHeaders=refDayOfWeek;
c.weekStartDay=1;
c.currentDate=null;
c.setReturnFunction=CP_setReturnFunction;
c.showCalendar=CP_showCalendar;
c.hideCalendar=CP_hideCalendar;
c.getCalendar=CP_getCalendar;
c.select=CP_select;
c.returnDiv=null;
c.cntryOfRes=null;
c.prevCalVal=null;
c.prevCalValFormatted=null;
return c;
}
function CP_setReturnFunction(_28, _31)
{
this.returnFunction=_28;
}
function CP_hideCalendar()
{
window.popupWindowObjects[arguments[0]].hidePopup();
}
function CP_refreshCalendar()
{
var _2f=window.popupWindowObjects[0];
_2f.populate(_2f.getCalendar(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
_2f.refresh();
}
function CP_showCalendar(_31, origDiv){
	this.returnDiv=origDiv;
	this.populate(this.getCalendar());
	this.showPopup(_31);
}

function CP_select(_31,_32,country,prevCalVal)
{
	this.prevCalValFormatted=null;
	var _33=(arguments.length>3)?arguments[3]:null;
	this.currentDate=null;
	this.cntryOfRes=country;
	if (prevCalVal!=null){
		if (this.cntryOfRes=="US"){
			this.prevCalValFormatted=formatUStoUK(document.getElementById(prevCalVal).value);
			if (checkDate(this.prevCalValFormatted)){
				this.prevCalVal=prevCalVal;
				this.prevCalValFormatted=inputFieldDate;
			}else
				this.prevCalValFormatted=null;
		}else if (checkDate(document.getElementById(prevCalVal).value)){
			this.prevCalValFormatted=inputFieldDate;
			this.prevCalVal=prevCalVal;
		}
	}
	this.showCalendar(_31, _32);
}

function CP_getCalendar()
{
var now=new Date();
var _39="";
var validEnteredDate=false;
if (this.cntryOfRes=="US")
var submittedDate=formatUStoUK(document.getElementById(this.returnDiv).value);
else
var submittedDate=document.getElementById(this.returnDiv).value;
if (checkDate(submittedDate))
{
validEnteredDate=true;
submittedDate=inputFieldDate;
}
else if(this.prevCalValFormatted!=null)
{
submittedDate=this.prevCalValFormatted;
validEnteredDate=true;
}
if(this.currentDate==null)
this.currentDate=now;
if(arguments.length>0)
var _3a=arguments[0];
else
{
if (validEnteredDate)
var _3a=submittedDate.getMonth()+1;
else
var _3a=this.currentDate.getMonth()+1;
}
if(arguments.length>1&&arguments[1]>0&&arguments[1]-0==arguments[1])
var _3b=arguments[1];
else
{
if (validEnteredDate)
var _3b=submittedDate.getFullYear();
else
var _3b=this.currentDate.getFullYear();
}
var _3c=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
if(((_3b%4==0)&&(_3b%100!=0))||(_3b%400==0))
_3c[2]=29;
var _3d=new Date(_3b,_3a-1,1);
var _3e=_3b;
var _3f=_3a;
var _40=1;
var _41=_3d.getDay();
var _42=(_41>=this.weekStartDay)?_41-this.weekStartDay:7-this.weekStartDay+_41;
if(_42>0)
{
_3f--;
if(_3f<1)
{
_3f=12;
_3e--;
}
_40=_3c[_3f]-_42+1;
}
var _43=_3a+1;
var _44=_3b;
if(_43>12)
{
_43=1;
_44++;
}
var _45=_3a-1;
var _46=_3b;
if(_45<1)
{
_45=12;
_46--;
}
_39+="<iframe src=\""+ refPercorso +"blank.gif\" scrolling=\"no\" frameborder=\"0\" style=\" width:188px; height:188px; z-index:-1; position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); \"></iframe>";
_39+="<table border=\"0\" cellspacing=\"2\" cellpadding=\"3\">";
_39+="<tr>\n";
var _48="CP_refreshCalendar";
var _49="javascript:"+_48;
_39+="<td class=\"cpMonthNavigation\"><a class=\"cpMonthNavigation\" href=\""+_49+"("+this.index+","+_45+","+_46+");\"><img src=\""+ refPercorso +"redArrowLeft.gif\" alt=\"Previous month\" width=\"6\" height=\"11\" /></a></td>\n";
_39+="<td colspan=\"5\" class=\"cpMonthNavigation\">"+this.monthNames[_3a-1]+" "+_3b+"</td>\n";
_39+="<td class=\"cpMonthNavigation\"><a class=\"cpMonthNavigation\" href=\""+_49+"("+this.index+","+_43+","+_44+");\"><img src=\""+ refPercorso +"redArrowRight.gif\" alt=\"Next month\" width=\"6\" height=\"11\" /></a></td>\n";
_39+="</tr>\n";
_39+="<tr>\n";
for(var j=0;j<7;j++)
{
if(j<5)
_39+="<td class=\""+"cpDayColumnHeader\">"+this.dayHeaders[(this.weekStartDay+j)%7].substr(0,1)+"</td>\n";
else
_39+="<td class=\""+"cpWeekendColumnHeader\">"+this.dayHeaders[(this.weekStartDay+j)%7].substr(0,1)+"</td>\n";
}
_39+="</tr>\n";
for(var row=1;row<=6;row++)
{
_39+="<tr>\n";
for(var col=1;col<=7;col++)
{
var _4e="";
if (validEnteredDate==true)
{
if((_3f==_3a)&&((_3f==submittedDate.getMonth()+1)&&(_40==submittedDate.getDate())&&(_3e==submittedDate.getFullYear())))
_4e="cpCurrentDate";
else
{
if((_3f==_3a) && (validEnteredDate))
_4e="cpCurrentMonthDate";
else
_4e="cpOtherMonthDate";
}
}
else
{
if(_3f==_3a && ((_3f==this.currentDate.getMonth()+1)&&(_40==this.currentDate.getDate())&&(_3e==this.currentDate.getFullYear())))
_4e="cpCurrentDate";
else
{
if((_3f==_3a) || (validEnteredDate))
_4e="cpCurrentMonthDate";
else
_4e="cpOtherMonthDate";
}
}
var _4f=_40;
if (this.prevCalValFormatted!=null)
var dateToCheck=this.prevCalValFormatted;
else
var dateToCheck=this.currentDate;
// GIANNARELLI - if((_40 < dateToCheck.getDate() && _3f==dateToCheck.getMonth()+1 && _3e==dateToCheck.getFullYear()) || (_3f < dateToCheck.getMonth()+1 && _3e == dateToCheck.getFullYear()) || (_3e < dateToCheck.getFullYear()) || (bookRange(_3e, _3f-1, _40) >= 353))
// GIANNARELLI - _4e += " cpUnavailDate";
_39+="\t<td class=\""+_4e+"\" >";
if(_3f==_3a)
{
// GIANNARELLI (da attivare per prenotazione) - if((_40 < dateToCheck.getDate() && _3f==dateToCheck.getMonth()+1 && _3e==dateToCheck.getFullYear()) || (_3f < dateToCheck.getMonth()+1 && _3e == dateToCheck.getFullYear()) || (_3e < dateToCheck.getFullYear()) || (bookRange(_3e, _3f-1, _40) >= 353))
// GIANNARELLI (da attivare per prenotazione) - _39+=_40;
// GIANNARELLI (da attivare per prenotazione) - else
_39+="<a class=\""+_4e+"\" title=\""+_40+" "+this.monthNames[_3a-1]+" "+_3e+"\" href=\"javascript:"+this.returnFunction+"("+_3e+","+_3f+","+_4f+",'"+this.returnDiv+"','"+this.cntryOfRes+"');"+"CP_hideCalendar('"+this.index+"');\">"+_40+"</a>";
}
else
_39+="&nbsp;";
_39+="</td>\n";
_40++;
if(_40>_3c[_3f])
{
_40=1;
_3f++;
}
if(_3f>12)
{
_3f=1;
_3e++;
}
}
_39+="</tr>";
}
_39+="\t</table>\n";
var _3b=now.getFullYear();
return _39;
}
function formatDate(x,y,z) {
x=(x<0||x>9?"":"0")+x;
y=(y<0||y>9?"":"0")+y;
z=(z<0||z>9?"":"0")+z;
// GIANNARELLI
//if(x.length = 4);
//x=x.substr(2,2);
return (z+"/"+y+"/"+x);
}
function bookRange(year, month, day)
{
var today=new Date()
var calDate=new Date(year, month, day);
var oneDay=1000*60*60*24;
return Math.ceil((calDate.getTime()-today.getTime())/(oneDay))
}

function checkDate(dateFieldValue)
{
	var date_array=dateFieldValue.split("/");
	if (date_array[2])
	{
		var inputFieldDateYear;
		if (date_array[2].length==2)
			inputFieldDateYear=20+date_array[2];
		else
			inputFieldDateYear=date_array[2];
		var inputFieldDateMonth=date_array[1];
		var inputFieldDateDay=date_array[0];
		inputFieldDate=new Date(inputFieldDateYear, inputFieldDateMonth-1, inputFieldDateDay);
		var parseInputFieldDateDay=parseFloat(inputFieldDateDay);
		var parseInputFieldDateMonth=parseFloat(inputFieldDateMonth);
		var parseInputFieldDateYear=parseFloat(inputFieldDateYear);
		if (!((parseInputFieldDateDay==inputFieldDate.getDate())&&(parseInputFieldDateMonth==inputFieldDate.getMonth()+1)&&(parseInputFieldDateYear==inputFieldDate.getFullYear())))
			return false;
		var todaysDate=new Date();
		todaysDate=todaysDate.getTime();
		var checkDate=inputFieldDate.getTime();
		// GIANNARELLI - if (todaysDate>checkDate)
			// GIANNARELLI - return false;
		// GIANNARELLI - else
			return true;
	}
	else
		return false;
}

function formatUStoUK(USdate)
{
var USDateArray=USdate.split("/");
var UKformat=USDateArray[1]+'/'+USDateArray[0]+'/'+USDateArray[2];
return UKformat;
}
function clearDefault(boxValue, defaultValue, boxId)
{
if (boxValue==defaultValue)
document.getElementById(boxId).value="";
}
function toggleOverCalendar(over)
{
overCalendar=over;
}
function stopBubble(e)
{
e.cancelBubble=true;
}

