function Calendar(){

       this.startDate = null;
       this.calendarOutput = new Array();
       this.language = 'en';
       this.totalMonths = 0;
       this.calCellId = 'main_cal';
       this.objName = null;
       this.formTargetFormatted = null;
       this.formTargetDb = null;
	   this.selectedCell = '';
	   this.selectedDate = null;
	   this.secondCalendarObj = '';
	   this.subdir = '';
	   
       this.setStartDate = function (day,month,year){
               //set start date
			   days_no = this.getMonthDays(year, (month-1));
               if(day > days_no){
                       alert('Invalid date: days' + days_no);
               }else{
                       this.startDate = new Date(year, (month-1), day);
               }
			   
			   //set cell
       }

       this.getStartDate = function(){
               alert(this.startDate.toString());
               alert(this.startDate.getDate())
               //return this.startDate;
       }

       this.getMonthDays = function (theYear, theMonth) {
               var oneHour = 1000 * 60 * 60
               var oneDay = oneHour * 24
               var thisMonth = new Date(theYear, theMonth, 1)
               var nextMonth = new Date(theYear, theMonth + 1, 1)
               var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime() -
oneHour)/oneDay)
               return len ;
       }

       this.getCurrentMonth = function(month){
               if(this.language == 'en'){
				   arr = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'Dicember');
               }else if(this.language == 'it'){
				   arr = new Array('Gennaio','Febbraio','Marzo', 'Aprile','Maggio', 'Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
               }else if(this.language == 'es'){
				   arr = new Array('Enero','Febrero','Marzo', 'Abril','Mayo', 'Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Deciembre');

               }else if(this.language == 'fr'){
				   arr = new Array('Enero','Febrero','Marzo', 'Abril','Mayo', 'Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Deciembre');
			   }

			   return  arr[month];
       }

       this.getCurrentDay = function(weekday){
               if(this.language == 'en'){
				   arr = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
               }else if(this.language == 'it'){
				   arr = new Array('Dom','Lun','Mar','Mer','Gio','Ven','Sab');
               }else if(this.language == 'es'){
				   arr = new Array('Dom','Lun','Mar','Mie','Jue','Vie','Sab');
               }else if(this.language == 'fr'){
				   arr = new Array('Dom','Lun','Mar','Mie','Jue','Vie','Sab');
			   }
			   return  arr[weekday];
       }


       this.buildCalendar = function(monthsDuration){
		   	   this.totalMonths = monthsDuration;
               o = new String();

               // initial date
               day = this.startDate.getDate();
               month = this.startDate.getMonth();
               year = this.startDate.getFullYear();
			   //alert(weekday);	
				
			   // build months
               
			   for(i=0;i < monthsDuration;i++){
						
                       numberOfDays = this.getMonthDays(year,month);

                       o = new String();
                       o += '<div style="width:200px; height:210px; padding:4px; border:1px solid #000000;font-family:Arial; background-color:#FFFFFF">';
								
								// prev button
								prev = '';
								if(i != 0){
									prev = '<span style="margin-right:15px"><a href="javascript:'+this.objName+'.displayMonth('+(i-1)+')"><img src="'+this.subdir+'pics/calendar_prev.gif" alt="" border="0" width="17" /></a></span>';
								}
								
								// next button
								next = '';
								if(i != (monthsDuration - 1)){
									next = '<span style="margin-left:15px"><a href="javascript:'+this.objName+'.displayMonth('+(i+1)+')"><img src="'+this.subdir+'pics/calendar_next.gif" alt="" alt="" border="0" width="17" /></a></span>';
								}
								
								o += '<div style="width:200px; font-size:14px; line-height:1em; padding:5px" align="center"><b>'+prev+this.getCurrentMonth(month)+' '+year+next+'</b></div>';
								
								for(ii=1;ii < (numberOfDays + 1);ii++){

									 loopDate = new Date(year,month,ii);
									 weekday = loopDate.getDay();

									if(i == 0 && day > ii){
									 	 selector_open = ''
									 	 selector_close = ''
									}else{
									 	 selector_open = '<a href="javascript:'+this.objName+'.setValues('+ii+','+(month + 1)+', '+year+', \''+this.getCurrentDay(weekday)+'\', '+i+')" style="text-decoration:none">'
									 	 selector_close = '</a>'
									}
									 
									 if(i > 0){
									 	 selector_open = '<a href="javascript:'+this.objName+'.setValues('+ii+','+(month + 1)+', '+year+', \''+this.getCurrentDay(weekday)+'\', '+i+')" style="text-decoration:none">'
									 	 selector_close = '</a>'
									 }
									 
						   				 
										 o += '<div align="center" style="width:22px;padding:3px; font-size:12px; float:left;;background-color:#ffffff" id="cal_'+this.objName+'_'+i+'_'+ii+'">';
											
											if(weekday == 0){
												wd = '<b>' + this.getCurrentDay(weekday) + '</b>';	
											}else{
												wd =  this.getCurrentDay(weekday) ;	
											}

											o += '<div style=" font-size:11px;"><b>' + ii + '</b></div>';
											o += '<div style="font-size:11px;line-height:1em; ">'+selector_open + wd + selector_close +'</div>';
										 o += '</div>';
										 
										 
										 
										 if(weekday == 6){
											 weekday = 0;
										 }else{
											 weekday++;
										 }
								}
                      
							o += '<div align="right" style="width:200px; margin-right:10px; float:left;">';
							 o += '<a href="javascript:'+this.objName+'.hideCal()"><b>Close</b></a>';
							o += '</div>';

						o += '</div>';
                       this.calendarOutput.push(o);
                       ///////////////////////////////// ADD month and Year
                       if(month == 11){
                               month = 0;
                               year++;
                       }else{
                               month++;
                       }
					   
               }
       }

	   this.displayMonth = function(goto){
		   // alert(this.selectedCell);
			document.getElementById(this.calCellId).innerHTML = this.calendarOutput[goto];
			
			if(this.selectedCell != ''){
				components = this.selectedCell.split(',');
				if(components[1] == goto){
					this.getSelectedCell(components[0]);
				}
			}
	   }
		
	   this.openCal = function(){
		   if(this.selectedCell == ''){
		   		document.getElementById(this.calCellId).innerHTML = this.calendarOutput[0];
		   }else{
			   components = this.selectedCell.split(',');
			   document.getElementById(this.calCellId).innerHTML = this.calendarOutput[components[1]];
			   this.getSelectedCell(components[0]);
		   }
	   }

	   this.hideCal = function(){
		   document.getElementById(this.calCellId).innerHTML = '';
	   }

	   this.getSelectedCell = function(cell_id){
	   		components = this.selectedCell.split(',');
			if(this.selectedCell != ''){
				if(components[0] == cell_id){
					document.getElementById(cell_id).style.backgroundColor = '#cccccc';					
				}else{
					document.getElementById(cell_id).style.backgroundColor = '#ffffff';					
				}
			}else{
				document.getElementById(cell_id).style.backgroundColor = '#ffffff';					
			}
	   }
	   
	   this.setValues = function(day, month, year, weekday, i){
	       document.getElementById(this.formTargetFormatted).value = weekday +', '+ day+'/'+month+'/'+year;
	       document.getElementById(this.formTargetDb).value = year+'-'+month+'-'+day;
		   this.selectedDate = new Date(year, (month-1), day);
		   
		   try{ // calculation nights
				getDaysOfStay('get_days');
		   }catch(E){}
			
		   
		   this.hideCal();
		   this.selectedCell = 'cal_'+this.objName+'_'+i+'_'+day + ',' + i;
		   
			if(this.secondCalendarObj != ''){
				
				if(this.getMonthDays(year,(month-1)) == day){ // last day?
					cal_2.selectedCell = 'cal_cal_2_'+(i+1)+'_'+ 1 + ',' + (i+1);
				}else{
					if(this.selectedDate > cal_2.selectedDate){
						//	alert(this.selectedDate);
						cal_2.selectedCell = 'cal_cal_2_'+i+'_'+ (day+1) + ',' + i;
					}else{
						
					}
				}
				
			}else{
			}
	   }
	   
	   this.hideCal = function(){
		   document.getElementById(this.calCellId).innerHTML = '';
	   }
	   
       this.displayAll = function(){
               for(i=0;i < this.calendarOutput.length;i++){
                       document.writeln(this.calendarOutput[i]);
               }
       }


}

