function check_occupanti_camere(var1,var2)
	{
	var min = document.bol_motorebooking[var1].value
	if (document.bol_motorebooking[var2].value > document.bol_motorebooking[var1].value)
		{
		alert("Room number minor of guests' number")
		document.bol_motorebooking[var2].value = min
		}
	}

function check_camere_occupanti(var1,var2)
	{
	var min = document.bol_motorebooking[var1].value
	if (document.bol_motorebooking[var2].value < document.bol_motorebooking[var1].value)
		{
		alert("Guests' number minor of room number")
		document.bol_motorebooking[var2].value = min
		}
	}

function verify_motorepiccolo() 
	{
	var themessage = "Please fill the following fields: ";
	if (document.bol_motorebooking.start_date.value=="") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motorebooking.start_date.value=="dd/mm/yyyy") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motorebooking.end_date.value=="")
		{
		themessage = themessage + " - Check out";
		}
	if (document.bol_motorebooking.end_date.value=="dd/mm/yyyy")
		{
		themessage = themessage + " - Check out";
		}

	if (themessage == "Please fill the following fields: ") 
		{
		document.bol_motorebooking.submit();
		}
	else 
		{
		alert(themessage);
		return false;
	   }
	}
	
function verify_motorebooking() 
	{
	var themessage = "Please fill the following fields: ";
	if (document.bol_motorebooking.start_date.value=="") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motorebooking.start_date.value=="dd/mm/yyyy") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motorebooking.end_date.value=="")
		{
		themessage = themessage + " - Check out";
		}
	if (document.bol_motorebooking.end_date.value=="dd/mm/yyyy")
		{
		themessage = themessage + " - Check out";
		}

	if (themessage == "Please fill the following fields: ") 
		{
		document.bol_motorebooking.submit();
		}
	else 
		{
		alert(themessage);
		return false;
	   }
	}
	
function verify_motoreftp() 
	{
	var themessage = "Please fill the following fields: ";
	if (document.bol_motoreftp.start_date.value=="") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motoreftp.start_date.value=="dd/mm/yyyy") 
		{
		themessage = themessage + " - Check in";
		}
	if (document.bol_motoreftp.end_date.value=="")
		{
		themessage = themessage + " - Check out";
		}
	if (document.bol_motoreftp.end_date.value=="dd/mm/yyyy")
		{
		themessage = themessage + " - Check out";
		}

	if (themessage == "Please fill the following fields: ") 
		{
		document.bol_motoreftp.submit();
		}
	else 
		{
		alert(themessage);
		return false;
	   }
	}
	
	
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	
	if(sText.length == 0) return false;
	
	for(i = 0; i < sText.length; i++) 
	{
		if(ValidChars.indexOf(sText.charAt(i)) == -1) return false;
	}
	
	return true;
 }
 
function CommaFormatted(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function ToItalianCurrency(amount)
{
	amount = amount.replace('.', 't');
	amount = amount.replace(',', '.');
	amount = amount.replace('t', ',');
	
	return amount;
}

function aggiornaTotale() {
	var totale = 0;
	var totale_parziale = 0;
	var persone = 0;
	var f = document.prenotazioneservizio;
	
	if(f.preno_opzione) {
		if(f.preno_opzione.length) {
			for(var x = 0; x < f.preno_opzione.length; x++) {
				if(f.preno_opzione[x].checked) {
					totale_parziale += parseInt(f.preno_opzione[x].id);
				}
			}
		} else {
			totale_parziale += parseInt(f.preno_opzione.id);
		}
	}
	for(var id in f) {
		if(id.substring(0, 12) == 'preno_extra[' && f[id].checked) {
			if(f[id].className.indexOf('commissionabile') != -1) totale_parziale += parseInt(f[id].id);
		}
	}
	for(var id in f) {
		if(id.substring(0, 14) == 'preno_persone[' && IsNumeric(f[id].value) && f[id].value > 0) {
			persone++;
			if(f[id].id == 0) {
				totale += totale_parziale * f[id].value;
			} else {
				totale += (totale_parziale - (totale_parziale / 100 * f[id].id)) * f[id].value;
			}
		}
	}
	
	document.getElementById('servizio-totale-val').innerHTML = ToItalianCurrency(CommaFormatted(CurrencyFormatted(persone > 0 ? totale : totale_parziale)));
}