// JavaScript Document

// INPUT DA PHP 

// COSTANTI
var giorniMesi = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // array coi giorni dei mesi

 
function CalendarManager(tourObj, lang){
	// properties
	this.tourObj = tourObj;
	if(lang != 'en') {
		this.lang = lang;
	} else {
		this.lang = 'en';
	}
	this.month = (tourObj.month-1);
	this.year = tourObj.year;
	this.form = document.calendar;
	this.form2 = document.tourright;
	this.curDate = new Date();
	this.currentMonth = this.curDate.getMonth(); 
	this.currentYear = this.curDate.getFullYear();
	this.thisMonth = false; // verifica se si sta scegliendo il mese corrente
	this.primaDataUtile = 1; 
	this.totalPrice = 0;
	this.totalPeople = 0;
	if(this.month == this.currentMonth && this.year == this.currentYear) {
		this.thisMonth = true;
		this.primaDataUtile = this.curDate.getDate() + 1;
		//alert(this.primaDataUtile);
	}// se questoMese  true,  la prima data utile per una prenotazione
	this.myDate = new Date( this.year, this.month);
	this.selectedDay = 0;
	
	// methods
	this.fill = function() {
		var a = this.form;
		var counter = 1;
		var giorniTotaliMese = giorniMesi[ this.myDate.getMonth()];
		var bisestile = false;
		var z = this.myDate.getFullYear();
		if (z%4==0) {
			bisestile = true;
		}
		if (this.myDate.getMonth() == 1 && bisestile == true) {
			giorniTotaliMese = 29;
		}
	
	
		a.mese.value = nomeMesi[this.myDate.getMonth()] + " " + this.myDate.getFullYear();
		for (i=0 ; i< 42; i++) {
			var tb = a["b" + i];
			if (i==0 || i%7==0) {
				tb.style.color="red";
				tb.style.backgroundColor="white";
				tb.style.cursor="pointer";
			} else {
				tb.style.color="#000000";
				tb.style.backgroundColor="#f79b52";
				tb.style.cursor="pointer";
			}
			
			
			if (i >=  this.myDate.getDay() && counter <= giorniTotaliMese) {
				
				tb.value=counter;
				if(this.tourObj.dates["date_" +  counter]) { 
				
				} else { // grigio: date in cui il tour non e disponibile
					//alert("date_" + counter);
					tb.style.backgroundColor="#f4efec";
					tb.style.color="#CCCCCC";
					tb.style.cursor="default";
				}
				
				
				counter++;
			} else {
				tb.value="";
				tb.style.backgroundColor="#f4efec";
				tb.style.color="#CCCCCC";
				tb.style.cursor="default";
				
			}
		}
	}
	this.update_right = function(theDate) {
		 
		this.selectedDay = theDate;
		var a = this.form2;
		if(this.tourObj.dates["date_" +  theDate]) { 
			var tmp = this.tourObj.dates["date_" +  theDate];
			a.chosendate.value = this.date_format(theDate);
			a.anno.value = this.year;
			a.mese.value = (this.month + 1);
			a.giorno.value = theDate;
			a.minpeople.value = tmp.minposti;
			a.maxpeople.value = tmp.maxposti;
			if(tmp.anticipo != 0) {
				var anticipotr = MM_findObj("anticipotr");
				if(anticipotr!= false) {
					anticipotr.style.display = "block";
				}
				a.anticipo.value = tmp.anticipo;
			} else {
				var anticipotr = MM_findObj("anticipotr");
				if(anticipotr!= false) {
					anticipotr.style.display = "none";
				}
			}
			a.totalprice.value = 0;
			this.totalPrice = 0;
			a.totalpeople.value = 0;
			this.totalPeople = 0 ;
			a.orario.options.length = 0;
			var extradata = MM_findObj("extradata");
			extradata.style.display= "block";
			for (i=0; i<tmp.orari.length; i++) {
				var myopt = new Option (tmp.orari[i][1], tmp.orari[i][0],false, false); 
				a.orario.options[a.orario.options.length] = myopt;
			}
			for(i=0; i<tariffe.length; i++) { 
				var b = MM_findObj("cont_tariffa_" + tariffe[i]);
				var c = a["tariffa_"+ tariffe[i]]; 
				if(tmp.tariffe["t_" + tariffe[i]]) {
					 c.options.length = 0;
					 var myopt2 = new Option (0, 0,false, false); 
					 c.options[0] = myopt2;
					 for(k=1; k<=tmp.maxposti; k++) {
						c.options[c.options.length] = new Option (k, k, false, false);
					 } 
					 b.style.display="block";
				} else {
					 b.style.display="none";
				}
				
			}
		}
	}
	
	this.calculate_totals = function() {
		var a = this.form2;
		this.totalPrice = 0;
		this.totalPeople = 0; 
		var tmp = this.tourObj.dates["date_" +  this.selectedDay];
		for (var prop in tmp.tariffe) {
			tariffa_id = prop.substr(2, prop.length); 
			number_selected = Number(a["tariffa_"+tariffa_id].options[a["tariffa_"+tariffa_id].selectedIndex].value)  ;
			this.totalPeople += number_selected;
			this.totalPrice +=  number_selected * tmp.tariffe[prop] ;
		}
		this.totalPrice = this.safe_float_display(this.totalPrice);
		a.totalprice.value = this.totalPrice;
		a.totalpeople.value = this.totalPeople ;
	}
	
	this.verify_form = function () {
		this.calculate_totals;
		var tmp = this.tourObj.dates["date_" +  this.selectedDay];
		if(this.totalPeople < tmp.minposti) {
			alert(fewer_than_required);
			return false;
		} else if (this.totalPeople > tmp.maxposti) {
			alert(more_than_allowed);
			return false;
		} else {
			return true;
		}
	}
	
	this.date_format = function(day) {
		if(this.lang == 'en') {
			return (nomeMesi[this.month] + " " + day + ", " + this.year);
		} else {
			return (day + " " + nomeMesi[this.month] + " " + this.year);
		}
		
	}
	this.safe_float_display = function(x){
		var tmpx = x*100;
		tmpx=Math.round(tmpx); 
		tmpx = tmpx/100;
		tmpx = tmpx.toString();
		var pos_point = tmpx.lastIndexOf('.'); 
		if(pos_point == -1) {
			return tmpx + ".00";
		} else {
			switch(tmpx.length - pos_point) {
				case 1:
					return tmpx + ".00";			
					break;
				case 2:
					return tmpx + "0";
					break; 
				default:
					return tmpx;
					break;
			}
		}
	}
	 

}


 

 
 