function conta(){
   var testo = document.forms.modulo.messaggio.value;
   if (testo.length>255) { alert("Lunghezza massima Testo superata, il testo oltre il limite non verra' inserito.");
   document.modulo.messaggio.value = document.modulo.messaggio.value.substr(0, 255);
   }
   document.getElementById("contatore").innerHTML="(<b>"+(255-testo.length)+"</b> caratteri rimanenti)";
}

function valiDate(f){
	var dmy = f.split(/\//);
	var td = new Date( dmy[2], dmy[1]-1, dmy[0] );
	var result = (td.getFullYear() == parseInt(dmy[2],10) && td.getMonth() == parseInt(dmy[1]-1,10) && td.getDate() == parseInt(dmy[0],10));
	
	if (result == false) {
		alert('Inserire correttamente la data di consegna.');
		document.getElementById('data').focus();
		return false;
	}

	return result;
}

function verifica_orario() {
	var arrdate = document.getElementById('data').value.split("/");
	var orario = document.getElementById('orario');
	var oraat= new Date();
	var giorno = arrdate[0];
	var mese = arrdate[1];
	var anno = arrdate[2];
	var dataspedizione = new Date(anno, mese-1, giorno, 14);
	var data= new Date();
	var dataod = new Date(data.getFullYear(),data.getMonth(),data.getDate(), 14);
	
	if (dataspedizione.valueOf() < dataod.valueOf()) {
		alert("La data di consegna indicata è già trascorsa.")
		document.getElementById('data').focus();
		return false;
	}
	
	if (oraat.getHours()>=20) {
	
	if ((dataspedizione.valueOf() == dataod.valueOf()) && (orario.checked==false)) {
		alert("Siamo spiacenti ma l'orario di consegna impostato è precedente all'orario attuale. La preghiamo di scegliere un orario diverso. Mattino: 08:00->13:00 , Pomeriggio: 14:00->20:00")
		document.getElementById('orario').focus();
		return false;
	}
	}
	
	if(oraat.getHours()>=13) {
	if ((dataspedizione.valueOf() == dataod.valueOf()) && (orario.checked==true)) {
		alert("Siamo spiacenti ma l'orario di consegna impostato è precedente all'orario attuale. La preghiamo di scegliere un orario diverso. Mattino: 08:00->13:00 , Pomeriggio: 14:00->20:00")
		document.getElementById('orario').focus();
		return false;
	}
	}
	
	return true
}

function verifica_prezzo() {
		prezzobase=Math.abs(parseInt(document.getElementById("prezzo1").value));
		prezzoscelto=parseFloat(document.getElementById("prezzo4").value);
		if (document.getElementById("scelta2").checked==true) {
				if(isNaN(prezzoscelto)) {
					alert("Numero non valido. Per favore inserisci solo cifre nel campo evidenziato.");
					document.getElementById('prezzo4').focus();
					return false;
				}

				
				if (prezzoscelto<=prezzobase) {
					alert("Il prezzo personalizzato non puo' essere uguale o minore di "+prezzobase+" \u20AC");
					document.getElementById('prezzo4').focus();
					return false;
				}
		}
		return true;
}

function verifica_modulo() 
{
	with(document.forms.modulo) {
		if (valiDate(data.value)==false) {
			document.modulo.data.focus();
			return false;
		}
		
		if (verifica_orario()==false) {
			return false;
		}
		if (verifica_prezzo()==false) {
			return false;
		}
	}
	return true;
}

function MM_openBrWindow(theURL,features)
{
	window.open(theURL,"SpeedyFlowers",features);
}

function verifica_coupon() {
		codice=document.getElementById("coupon").value;
		if (codice=="") {
					alert("Inserire il codice del Coupon, altrimenti non compilare nulla!");
					return false;
				}
		return true;
}