



var weekDay = new Array(7);
var months  = new Array(12);

var lang= "it";

try
{
 if (window.linguacal) { 
     lang = window.linguacal;
 }else if (window.linguaFlash) {   
     lang = window.linguaFlash;
 }else {  
     lang = 'it';              
 }
}
catch(error){}	


if(lang=="it"){
	weekDay[0] = "D";
	weekDay[1] = "L";
	weekDay[2] = "M";
	weekDay[3] = "M";
	weekDay[4] = "G";
	weekDay[5] = "V";
	weekDay[6] = "S";

	months[0] = "Gennaio";
	months[1] = "Febbraio";
	months[2] = "Marzo";
	months[3] = "Aprile";
	months[4] = "Maggio";
	months[5] = "Giugno";
	months[6] = "Luglio";
	months[7] = "Agosto";
	months[8] = "Settembre";
	months[9] = "Ottobre";
	months[10] = "Novembre";
	months[11] = "Dicembre";	
}else if(lang=="en"){
	weekDay[0] = "S"
	weekDay[1] = "M"
	weekDay[2] = "T"
	weekDay[3] = "W"
	weekDay[4] = "T"
	weekDay[5] = "F"
	weekDay[6] = "S"

	months[0] = "January"
	months[1] = "February"
	months[2] = "March"
	months[3] = "April"
	months[4] = "May"
	months[5] = "June"
	months[6] = "July"
	months[7] = "August"
	months[8] = "September"
	months[9] = "October"
	months[10] = "November"
	months[11] = "December"
}else if(lang=="fr"){
	weekDay[0] = "D"
	weekDay[1] = "L"
	weekDay[2] = "M"																																														
	weekDay[3] = "M"																																														
	weekDay[4] = "J"																																														
	weekDay[5] = "V"																																																		
	weekDay[6] = "S"	
	months[0] = "Janvier"
	months[1] = "Fevreir"
	months[2] = "Mars"
	months[3] = "Avril"
	months[4] = "Mai"
	months[5] = "Juin"
	months[6] = "Juillet"
	months[7] = "Aout"
	months[8] = "Septembre"
	months[9] = "Octobre"
	months[10] = "Novembre"
	months[11] = "Decembre"	
}else if (lang=="de") {
	weekDay[0] = "S"
	weekDay[1] = "M"
	weekDay[2] = "D"
	weekDay[3] = "M"
	weekDay[4] = "D"
	weekDay[5] = "F"
	weekDay[6] = "S"
	months[0] = "Januar"
	months[1] = "Februar"
	months[2] = "März"
	months[3] = "April"
	months[4] = "Mai"
	months[5] = "Juni"
	months[6] = "Juli"
	months[7] = "August"
	months[8] = "September"
	months[9] = "Oktober"
	months[10] = "November"
	months[11] = "Dezember"
}else if (lang=="nl" || lang=="ol") {
	weekDay[0] = "S"
	weekDay[1] = "M"
	weekDay[2] = "T"
	weekDay[3] = "W"
	weekDay[4] = "T"
	weekDay[5] = "F"
	weekDay[6] = "S"

	months[0] = "January"
	months[1] = "February"
	months[2] = "March"
	months[3] = "April"
	months[4] = "May"
	months[5] = "June"
	months[6] = "July"
	months[7] = "August"
	months[8] = "September"
	months[9] = "October"
	months[10] = "November"
	months[11] = "December"
}


function gestisciCalendario( memDay, dta, gg, mm, aa, index, searchType, idPosDiv ) {
    memorizzaGiorno=memDay;    
    if(dta !="")   
    	dest_dta=dta; 
    if(mm !="")
    	dest_mm=mm;
    if(gg !="")
    	dest_gg=gg;
    if(aa !="")	
    	dest_aa=aa;

    var pos = getElementAbsolutePos(document.getElementById(idPosDiv));   
   
    document.getElementById('cal_div').style.left= pos.x+"px";
    document.getElementById('cal_div').style.top = pos.y+"px";

    var htmlDiv = "";
    htmlDiv += '<iframe frameborder="no" style="width:163px;visibility:visible;overflow:hidden;position:absolute;top:1px;left:1px;height:163px;"></iframe><div style="width:165px;border:1px solid #999999;">';
    htmlDiv += '<div class="calTop" style="position:relative;">';
    htmlDiv += '<a href="#" onclick="document.getElementById(\'cal_div\').style.display=\'none\';return false" id="closeCal"></a>';
    htmlDiv += '</div>';                    
    htmlDiv += '<div style="position:relative; top:0px; background-color:#E7E7E7;height:163px; overflow:hidden;padding:0!important;margin:0!important;">';
    htmlDiv += '<div id="calNEW" style="position:relative; top:0px;height:138px;background-color:#fff;border:0px solid #666666;border-bottom:0px;margin:0!important;">';
    htmlDiv += '</div>';            
    htmlDiv += '</div>';
    htmlDiv += '</div>';
    
    document.getElementById('cal_div').innerHTML =htmlDiv;


    document.getElementById('cal_div').style.display='block';
    ridisegnaCalendari(index, searchType);
}
    

function hideCal(){
    if(document.getElementById('cal_div'))
	document.getElementById('cal_div').style.display='none';
		
    
}



function leapYear(year) {
	if (year % 4 == 0) // basic rule
		return true // is leap year	
   return false // is not leap year
}

function getDays(month, year) {
	// create array to hold number of days in each month
	var ar = new Array(12)
	ar[0] = 31 // January
	ar[1] = (leapYear(year)) ? 29 : 28 // February
	ar[2] = 31 // March
	ar[3] = 30 // April
	ar[4] = 31 // May
	ar[5] = 30 // June
	ar[6] = 31 // July
	ar[7] = 31 // August
	ar[8] = 30 // September
	ar[9] = 31 // October
	ar[10] = 30 // November
	ar[11] = 31 // December

	// return number of days in the specified month (parameter)
	return ar[month]
}

function getMonthName(month) {	
	// return name of specified month (parameter)
	if(month==12){ //revert to january
		return months[0];
		//return months[month];
	}else{
		return months[month];
	}
}


//render the top month in the calendar
function drawCal(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType) {	
	searchProductType = searchType;	
	var text = "" 	
	text += '<TABLE class=\'tabCalendario\' CELLSPACING="1" border="0">';
	text += '<caption>';
	if(dep_or_ret == 1) {
    		text += "<div style='position:absolute; top:-18px; left:0px;width: 165px;'><a class='sx' href='javascript:previousMonth(1, 1)'><<</a><a class='dx' href='javascript:nextMonth(1, 1)'>>></a></div>"
	}else if(dep_or_ret == 2){
   	 	text += "<div style='position:absolute; top:-18px; left:0px;width: 165px;'><a class='sx' href='javascript:previousMonth(1, 2)'><<</a><a class='dx' href='javascript:nextMonth(1, 2)'>>></a></div>"
	}
	text += "<span id='nomeMese'>"+ monthName + ' ' + year +"</span>";
	text += '</caption>'; // close header cell
	
	var openCol = ''
	var closeCol = '</TH>'
	
	// create first row of table to set column width and specify week day
	text += '<TR ALIGN="center" VALIGN="center">'
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
	    switch(dayNum) {
		 case 6: {
		   openCol='<TH class="sab" >';
		   break;
		 }
		 case 0: {
		   openCol='<TH class="dom" >';
		   break;
		 }
		 default: {
		   openCol='<TH>';
		 }
		}
		text += openCol + weekDay[dayNum] + closeCol 
		
	}
	text += '</TR>'
	
	// declaration and initialization of two variables to help with tables
	var digit = 1
	var curCell = 1

	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="right" VALIGN="top">'
		for (var col = 1; col <= 7; ++col) {
		switch(col)
		{
		 case 7:
		 {
		 openCol='class="sab"'
		 break
		 }
		 case 1:
		 {
		 openCol='class="dom"'
		  break
		 }
		 default:
		 {
		 openCol='class="oldays"'
		 }
		}		
		if (digit > lastDate)
			break
		if (curCell < firstDay) {
			text += '<TD></TD>';
			curCell++
		} else {
			
		var DATAnow = new Date();
		var annoN = DATAnow.getFullYear();
		var meseN = DATAnow.getMonth();
		meseN++;
		var giornoN = DATAnow.getDate();
		
		if ((anno>annoN)||((anno==annoN) && (mese>meseN))||((anno==annoN) && (mese==meseN) && (giornoN<=digit))) {
				
                
                if(dep_or_ret == 1) { //if they clicked the depart calendar
                
                    if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1==digit)) {  //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day                    
                        text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px; background-color:#CEDEEC!important;color:#000!important">' + digit + '</a></TD>'
                    }
                    else if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2==digit)){
                        if(searchType == 'F') {
                        	text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px;background-color:#CEDEEC!important;color:#000!important" href="#" onclick="injData(this,\'dtaa\',\'gga\',\'mma\',\'aaa\', 1);document.getElementById(\'cal_div\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }else {
                        	text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px; background-color:#CEDEEC!important;color:#000!important">' + digit + '</a></TD>'
                        }
                    }
                    else{
                        text += '<TD '+openCol+' style="text-align:center;"><a style="font-size:10px;" href="#" onclick="injData(this,\'dtaa\',\'gga\',\'mma\',\'aaa\', 1);document.getElementById(\'cal_div\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                    }
                }
                else  if(dep_or_ret==2) {   //if they clicked the return calendar                
                    if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2==digit)) {  //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day                    
                        text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px; background-color:#CEDEEC!important;color:#000!important">' + digit + '</a></TD>'
                    }
                    //added this 'else if' to show the depart date on the return calendar.
                    else if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1==digit)) {  //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day                    
                        if(searchType == 'F') {
                        	text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px; background-color:#CEDEEC!important;color:#000!important" href="#" onclick="injData(this,\'dtar\',\'ggr\',\'mmr\',\'aar\', 2);document.getElementById(\'cal_div\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }else {
                            text += '<TD '+openCol+' style="text-align:center;background-color:#CEDEEC!important"><a style="font-size:10px; background-color:#CEDEEC!important;color:#000!important">' + digit + '</a></TD>'
                      	}
                    }
                    else {
                        text += '<TD '+openCol+' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData(this,\'dtar\',\'ggr\',\'mmr\',\'aar\', 2);document.getElementById(\'cal_div\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                    }
                }
		}
		else
			text += '<TD style="cursor:default!important" '+openCol+'> ' + digit + '</TD>'
	   digit++
	    }
		}
		text += '</TR>';
	}
	
	text += '</TABLE>';
if(dep_or_ret == 1) {
    text += "<div style='position:absolute; top:128px; left:0px;width: 165px;'><a class='sx' href='javascript:previousMonth(1, 1)'><<</a><a class='dx' href='javascript:nextMonth(1, 1)'>>></a></div>"
}else if(dep_or_ret == 2){
    text += "<div style='position:absolute; top:128px; left:0px;width: 165px;'><a class='sx' href='javascript:previousMonth(1, 2)'><<</a><a class='dx' href='javascript:nextMonth(1, 2)'>>></a></div>"
}	
document.getElementById(target).innerHTML=text;		
	
}


function previousMonth(perc, dep_ret) { //move to previous month 
    
    mese = parseInt(mese,10)-parseInt(perc,10);	
	  
    if(mese == -1) //it was Jan when they hit back, display Nov & Dec
    {   
        mese=11;
        anno--;
    }
    else if(mese == 0)//it was Feb when they hit back, display Dec & Jan
    {
        mese=12;
        anno--;
    }

    show_date = new Date(mese+"/6"+"/"+anno);
    now = new Date(mese+"/6"+"/"+anno);

    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);
    
    monthName2 = getMonthName(parseInt(month,10)+1);
    firstDayInstance2 = new Date(year, parseInt(month,10)+1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(parseInt(month,10)+1, year);

    drawCal(firstDay + 1, days, date, monthName, year,'calNEW', dep_ret, searchProductType);
    document.getElementById('nomeMese').innerText= (months[parseInt(mese,10)-1] + " " + anno); //change contents of month title

    var year2 = year;
    var anno2 = anno;
    
    if(mese == 12)
    {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(parseInt(year,10)+1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, parseInt(year,10)+1);   //get days for january
        
        year2 += 1;
        anno2 += 1;
    }
    
   
}

function nextMonth(perc, dep_ret) {  //move to next month 

    mese = parseInt(mese,10)+parseInt(perc,10);

    if (mese == 14) { //it was december when they hit next, display Feb & Mar    
        mese=2;
        anno++;
    }else if(mese == 13) { //it was november when they hit next, display Jan & Feb    
        mese=1;
        anno++;
    }
       
    show_date = new Date(mese+"/6"+"/"+anno);   
    now = new Date(mese+"/6"+"/"+anno);    
    
    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);
    
    monthName2 = getMonthName(parseInt(month,10)+1);
    firstDayInstance2 = new Date(year, parseInt(month,10)+1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(parseInt(month,10)+1, year);

    drawCal(parseInt(firstDay,10) + 1, days, date, monthName, year,'calNEW', dep_ret, searchProductType);
    document.getElementById('nomeMese').innerText= (months[parseInt(mese,10)-1] + " " + anno); //change contents of month title
   
    var year2 = year;
    var anno2 = anno;
    var mese2 = mese;
    
    if(mese == 12) {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(parseInt(year,10)+1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, parseInt(year,10)+1);   //get days for january
        
        year2 += 1;
        anno2 += 1;
        mese2 = 0;
    }    
  
}

function injData(who,dta,gg,mm,aa,dep_or_ret){	
	var day = who.innerHTML;    //stores the day selected in the top month
	var month = ""+eval(mese); //convert to string
	var year = anno;

	//Pad the month and day is required	
	if(day.length==1) day = '0'+day;
	if(month.length==1) month = '0'+month;	
	if(document.getElementById(dest_dta)){									
		document.getElementById(dest_dta).value=day+"-"+(month)+"-"+year;
		if(document.getElementById(dest_dta).onchange)		
			document.getElementById(dest_dta).onchange();//forzo lancio metodi onchange
	}else{				
		document.getElementById(dest_gg).value=day;
		document.getElementById(dest_mm).value=month;
		document.getElementById(dest_aa).value=year;
		if(document.getElementById(dest_mm+'_'+dest_aa))
			document.getElementById(dest_mm+'_'+dest_aa).value=month+'_'+year;
		
	}
		
		
	if(dep_or_ret == 1){
	        memGiorno1=who.innerHTML;
	        memMese1=mese;
	        memAnno1=anno;
	}else if(dep_or_ret == 2 ){
	        memGiorno2=who.innerHTML;
	        memMese2=mese;
	        memAnno2=anno;
    }
}



//Introdotta per compatibilità Gestione visualizzazione controlli
function mostraControlli() {
}
    
//Introdotta per compatibilità Gestione visualizzazione controlli
function nascondiControlli() {
}
    
var now = null;
var show_date = null;
var anno = null;
var mese = null;
var memorizzaGiorno=0;
        
initCalendar();
        
function initCalendar() {            
    // calendario
    now = new Date();			
    show_date = new Date();		
    anno=show_date.getFullYear();
    mese=parseInt(show_date.getMonth(),10)+1;
 }        
// gestione calendario in NLS
var dest_dta;
var dest_gg;
var dest_mm;
var dest_aa;


// stores the selected day/month/year for depart (1) and return (2)
var memGiorno1=0
var memMese1=0
var memAnno1=0
var memGiorno2=0
var memMese2=0
var memAnno2=0

//called when page 1st loads
var now = new Date();

var year = now.getFullYear();
var month = now.getMonth();
var monthName = getMonthName(month);

var date = now.getDate();
now = null;
var firstDayInstance = new Date(year, month, 1);

var firstDay = firstDayInstance.getDay();

firstDayInstance = null;
var days = getDays(month, year);
var monthName2 = getMonthName(parseInt(month,10)+1);
var firstDayInstance2 = new Date(year, parseInt(month,10)+1, 1);
var firstDay2 = firstDayInstance2.getDay();
var days2 = getDays(parseInt(month,10)+1, year);
var days3 = getDays(0, parseInt(year,10)+1); //used when bottom panel displays january

function loadDateCal(){
	if(document.getElementById(dest_gg) && document.getElementById(dest_gg).value != "" && document.getElementById(dest_mm) && document.getElementById(dest_mm).value != "" && document.getElementById(dest_aa) && document.getElementById(dest_aa).value != "" ){
	    	date = document.getElementById(dest_gg).value;
	    	month = document.getElementById(dest_mm).value;
	    	year = document.getElementById(dest_aa).value;  
	    	anno = year; 
	    	mese=month;
	    	monthName = getMonthName(parseInt(month,10)-1);
	    	days = getDays(parseInt(month,10)-1, year);
	    	firstDayInstance = new Date(year, parseInt(month,10)-1, 1);
	    	firstDay = firstDayInstance.getDay();	  
		if(month == 12)
			monthName2 = getMonthName(month);
		else	
			monthName2 = getMonthName(parseInt(month,10)+1);
			
		firstDayInstance2 = new Date(year, parseInt(month,10)+1, 1);
		firstDay2 = firstDayInstance2.getDay();
		days2 = getDays(parseInt(month,10)+1, year);	    	
    	}else if(document.getElementById(dest_dta) && document.getElementById(dest_dta).value!=""){
	    	var selDta = document.getElementById(dest_dta).value;    	
	    	date = selDta.substring(0,2);
	    	month = selDta.substring(3,5);
	    	year =selDta.substring(6,10);
	    	anno = year;
	    	mese= month;
	    	monthName = getMonthName(parseInt(month,10)-1);
	    	days = getDays(month-1, year);
	    	firstDayInstance = new Date(year, parseInt(month,10)-1, 1);
	    	firstDay = firstDayInstance.getDay();	
	    	if(month == 12)
			monthName2 = getMonthName(month);
		else	
			monthName2 = getMonthName(parseInt(month,10)+1);
		firstDayInstance2 = new Date(year, parseInt(month,10)+1, 1);
		firstDay2 = firstDayInstance2.getDay();
		days2 = getDays(parseInt(month,10)+1, year);		    	    	
    	}	   	
}


function ridisegnaCalendari(dep_or_ret, searchType) {  //called each time the calendar is opened. dep_or_ret is 1 (depart) or 2 (return)  
	//initCalendar();  	
	loadDateCal();		      
    	drawCal(parseInt(firstDay,10) + 1, days, date, monthName, year,'calNEW', dep_or_ret, searchType);            
}



function getElementAbsolutePos(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
	var tmp = getElementAbsolutePos(element.offsetParent);
	r.x += tmp.x;
	r.y += tmp.y;
	}
return r;
};
