function isNumofnights(NN){
	var digits="0123456789";
	var s = NN.value;
	var isaNumber = true;
	var i = 0;
	for(i=0;i<NN.value.length;i++){
		if (digits.indexOf(s.charAt(i))==-1) {
	    	isaNumber = false;
	    	break;
	    }
	    if (s < 1) isaNumber = false;
	    return isaNumber;
	}
}
function validate_form(Fsearch){
	var dayIndex;
	var monthIndex;
	//var yearIndex;
    //initStrValidate();
    dayIndex = Fsearch.CD.selectedIndex;
    monthyearIndex = Fsearch.CMY.selectedIndex;
    //yearIndex = Fsearch.CY.selectedIndex;
    window.status=txtWait;
    if (dayIndex==0 || monthyearIndex==0){
	    alert(txtBadDate);
	    if (dayIndex==0) Fsearch.CD.focus();
	    else Fsearch.CMY.focus();
	    window.status="";
	    return false;
    }
    if (!isNumofnights(Fsearch.NN)){
	    alert(txtBadNNight);
	    Fsearch.NN.focus();
	    window.status="";
	    return false;
    }
    window.status="";
    return true;
}
// Calendar global declarations and initialization
function init () {
    isSecure = 0;
    today = new Date();
    currDay = today.getDate();
    currMonth = today.getMonth();
    currYear = today.getYear();
    if (currYear < 1000) currYear += 1900;
    today = new Date(currYear, currMonth, currDay);
    nextYear = new Date(currYear, currMonth, currDay);
	nextYear.setDate (nextYear.getDate() + 365);

    // Get the date that is selected on the pull down menus if any.
    var d;
    var m;
    var y;
    eval("indexPos=document.forms['" + formName + "']." + monthyearFld  + ".selectedIndex");
    eval("d=document.forms['" + formName + "']." + dayFld  + ".selectedIndex");
    eval("my=document.forms['" + formName + "']." + monthyearFld  + ".options[indexPos].value");
    //eval("y=document.forms['" + formName + "']." + monthyearFld  + ".selectedIndex");
    y=my.substr(0,4);
    m=my.substr(4,2);
    if (d == 0 && m == 0 && y == 0) displayMonth = currMonth;
    else {
		if (d == 0) d = 1;		// set defaults
		if (m == 0) m = 1;
		if (y == 0) y = currYear;
		//else y = currYear + y - 1;	// translate year index */
		var tempDate = new Date (y, m - 1, d);
		if (tempDate.getTime() < today.getTime()) {
			tempDate.setDate(today.getDate());
		}
		else if (tempDate.getTime() >= nextYear.getTime()) {
			tempDate.setDate(nextYear.getDate() - 1);
		}
		displayMonth = tempDate.getMonth();
		var displayYear = tempDate.getYear();
		if (displayYear < 1000) displayYear += 1900;
		tempDate = new Date(displayYear, displayMonth + 1, 1);
		if (tempDate.getTime() >= nextYear.getTime()) {
			displayMonth -= 1;
		}
		if (displayYear > currYear) displayMonth += 12;
	}
}

var calendarWinOpen = false;
var calendarWindow = null;
function openCalendarWin() {
	var windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=430,height=205";
    calendarWindow = this.open("","calendarWindow",windowOptions);
    calendarWindow.callingForm = this;    
    calendarWinOpen = true;
}

function closeCalendarWindow() {
    if (calendarWinOpen) {
    	calendarWinOpen = false;
	    if(checkOnClose()){
	    	calendarWindow.close();
	    } 
		else {
	    	calendarWinOpen = true;
	    }
	}
}
// Calendar main entry point
function popUpCalendar (dayFldName, monthyearFldName, formFldName,type) {
	var indexIn=document[formFldName].meseannoarr.selectedIndex;
    var dIn=document[formFldName].giornoarr.selectedIndex;
    var myIn=document[formFldName].meseannoarr.options[indexIn].value;
	var yIn=myIn.substr(0,4);
    var mIn=myIn.substr(4,2);
	var indexOut=document[formFldName].meseannopar.selectedIndex;
    var dOut=document[formFldName].giornopar.selectedIndex;
    var myOut=document[formFldName].meseannopar.options[indexIn].value;
	var yOut=myOut.substr(0,4);
    var mOut=myOut.substr(4,2);
	dayIn = new Date(yIn,mIn,dIn);
	dayOut = new Date(yOut,mOut,dOut);
    dayFld  = dayFldName;
    monthyearFld = monthyearFldName;
    //yearFld = yearFldName;
    formName  = formFldName;
    init();
	openCalendarWin();
	redrawCalendar();
}
// repaint the calendar
function redrawCalendar() {
    calendarWindow.callingForm = this;
    calendarWindow.document.open();
    calendarTitle = "<title>" + txtCalendar + "</title>";
    calendarWindow.document.write(calendarTitle);
    var firstOfMonth = new Date(currYear, displayMonth, 1);
    drawCalendar(firstOfMonth);
    calendarWindow.document.write(htmlBuffer);
    calendarWindow.document.close();
    calendarWindow.callingForm = this;
    calendarWindow.focus();
}
// fill the calling forms date and month
function fillCalDate(d, m, y) {    
    ny=new Date().getFullYear();
    if(y>1) ny+=1;
    formMY=document.forms[formName][monthyearFld];
    formD=document.forms[formName][dayFld];
	date=new Date(ny,m-1,d);
	my = date.getFullYear()+""+pad(date.getMonth()+1);
	d = date.getDate();
	setSelIndexByValue(formD,d);
	setSelIndexByValue(formMY,my);

	blinkBg(formD,formMY);
	closeCalendarWindow();
}	
// set the month
function changeMonth (increment) {
    var nextMonth = displayMonth;
    if (increment == 1) nextMonth++ ;
    else nextMonth-- ;
    if ((nextMonth - currMonth >= 13) || (nextMonth < currMonth))  {
	    nextMonth = currMonth;
   	}
    displayMonth = nextMonth;
    redrawCalendar();
}
// generate the calendar document
function drawCalendar (theDate) {
    var y = theDate.getYear();
    if (y < 1000) y += 1900;
    var monthNum = theDate.getMonth();
    var nextDate = new Date(y, monthNum + 1, 1);
    htmlBuffer  = "<html><head><link rel='stylesheet' type='text/css' href='http://www.traveleurope.it/calendar.css'></head>";
    htmlBuffer += "<body>";
    htmlBuffer += "<table id='topFrame' cellspacing='0'><tr><td class='titolo'>";
    htmlBuffer += txtSelectDate;
    htmlBuffer += "</td><td class='command'>";

	// Get the last day of the month before the first month we drew.
	var tempDate = new Date (y, monthNum, 1);
	tempDate.setDate(tempDate.getDate() - 1);
    if (tempDate.getTime() >= today.getTime()) {
	    htmlBuffer += "<a href='javascript:callingForm.changeMonth(-1)'>";
		htmlBuffer += txtPrevious;
		htmlBuffer += "</a>";
    }

	// Get the first day of the month after the last month we drew.
	tempDate = new Date (y, monthNum + 2, 1);
    if (tempDate.getTime() < nextYear.getTime()) {
	    htmlBuffer += "<a href='javascript:callingForm.changeMonth(1)'>";
		htmlBuffer += txtNext;
		htmlBuffer += "</a>";
    }
	htmlBuffer += "</td></tr></table>";
	htmlBuffer += "<table id='calendarFrame'><tr><td class='mese'>";
    drawOneMonth(theDate);
    htmlBuffer += "</td>";
    htmlBuffer += "<td class='mese'>";
    drawOneMonth(nextDate);
    htmlBuffer += "</td></tr>";
    htmlBuffer += "</table></body></html>";
}

function drawOneMonth(theDate) {
    var d;
    var monthNum = theDate.getMonth();
	htmlBuffer += "<p class='titoloMese'>";
    htmlBuffer += monthName[monthNum+1];
    var dispYear = theDate.getYear();
    if (dispYear < 1000) dispYear += 1900;
    htmlBuffer += " " + dispYear;
    htmlBuffer += "</p>";
    htmlBuffer += "<table cellspacing='0' class='meseWrap'><tr><td>";
	htmlBuffer += "<table cellspacing='1' bgcolor='#cccccc' class='meseInWrap'>";
    htmlBuffer += "<tr>";
    for (d = 1; d <= 7; d++) {
	    htmlBuffer += "<th>"
		+ dayName[d]
		+ "</th>";
    }
    htmlBuffer += "</tr>";
    drawBody(theDate);
    htmlBuffer += "</table></td></tr></table>";
}

// generate the calendar body
function drawBody (theDate) {
    var w;
    var d;
    var y = theDate.getYear();
    if (y < 1000) y += 1900;
    var myDate = new Date (y, theDate.getMonth(), 1);
    var monthNum = myDate.getMonth();
    firstSunday (myDate);
    for (w=0; w<6; w++) {
		htmlBuffer += "<tr>";
    	for (d=0; d<7; d++) {
	    	htmlBuffer += "<td";
			if (myDate.getMonth() != monthNum) {
				htmlBuffer += " class='dayPassive'>&nbsp;"; // this square on the calendar is not part of the month
			}
			else {
        		var date = myDate.getDate();
				var date_str;
				if (date < 10) date_str = "0" + date;
				else date_str = "" + date;
        		//htmlBuffer += "&nbsp;";
				if (myDate.getTime() >= today.getTime() && myDate.getTime() < nextYear.getTime()){
					// create a link
					y = (myDate.getYear() - today.getYear()) + 1;
					var m = myDate.getMonth() + 1;
//mie modifiche
					if(m==dayIn.getMonth() && date==dayIn.getDate()){
						htmlBuffer += " id='dayIn'";
					}
					if(m==dayOut.getMonth() && date==dayOut.getDate()){
						htmlBuffer += " id='dayOut'";
					}
        			htmlBuffer += " class='dayActive'>&nbsp;<a onmouseover='this.parentNode.className = \"dayActiveOver\";' onmouseout='this.parentNode.className = \"dayActive\";' href='javascript:callingForm.fillCalDate(";
					htmlBuffer += date + "," + m + ", " + y + ")'";
        			htmlBuffer += ">";
        			htmlBuffer += date_str;
        			htmlBuffer += "</a>";
        		}
				else {
					htmlBuffer +=  " class='dayPassive'>&nbsp;"+date_str;
				}
        		htmlBuffer += "&nbsp;";
	    	}
        	htmlBuffer   += "</td>";
	    	// increment the date
	    	myDate.setDate(myDate.getDate() + 1);
        }
        htmlBuffer += "</tr>";
    }
}

function firstSunday (fromDate) {
	while (fromDate.getDay() != 0) {
		fromDate.setDate(fromDate.getDate() - 1);
    }
}
function thisWindowOnFocus() {
    closeCalendarWindow();
}
//check if check-in < check-out and numofnights <= 99
function checkOnClose(){
	return true;
}
function checkNights(sSelectMonthIn, sSelectDayIn,sSelectMonthOut, sSelectDayOut){
	var sMonthOut = sSelectMonthOut.options[sSelectMonthOut.selectedIndex].value;
	var sMonthIn  = sSelectMonthIn.options[sSelectMonthIn.selectedIndex].value;
	var sDayIn    = sSelectDayIn.options[sSelectDayIn.selectedIndex].value;
	var sDayOut   = sSelectDayOut.options[sSelectDayOut.selectedIndex].value;
	var dDateIn   = retrieveDate(sMonthIn, sDayIn);
	var dDateOut  = retrieveDate(sMonthOut, sDayOut);
	var nMillsec  = dDateOut.getTime(dDateOut) - dDateIn.getTime(dDateIn);
	var nNights   = Math.ceil(nMillsec /1000/60/60/24);
	if (nNights >= '99'){
        return true;
	} else {
		return false;
   	}
}
//modifiche & aggiunte
function swapOut(el){
el.parentNode.className = "dayActive";
}
function swapOver(el){
el.parentNode.className = "dayActiveOver";
}
var iter= 0;

function blinkBg(){
	iter++;
	formD.style.backgroundColor== "#fee" ? formD.style.backgroundColor="#ffffff" : formD.style.backgroundColor="#fee";
	formMY.style.backgroundColor== "#fee" ? formMY.style.backgroundColor="#ffffff" : formMY.style.backgroundColor="#fee";
	if(iter>=10){ 
		formD.style.backgroundColor == "#ffffff";
		formMY.style.backgroundColor == "#ffffff";
		iter=0;
		}
	else{
		window.setTimeout("blinkBg()",100);
		}
	}
