function Cambia_Imm(x) {
	document.getElementById('IMGROSE').src = 'http://www.speedyflowers.it/picture_library/rose/'+x+'.jpg';
}

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 valore_orario()
{
for (var i=0; i < document.forms.modulo.orario.length; i++)
   {
   if (document.forms.modulo.orario[i].checked)
      {
      return document.forms.modulo.orario[i].value;
      }
   }
}

function tipo_rose()
{
for (var i=0; i < document.forms.modulo.colore.length; i++)
   {
   if (document.forms.modulo.colore[i].checked)
      {
      return document.forms.modulo.colore[i].value;
      }
   }
}

function valore_prezzo()
{
	  valore=document.getElementById('quantita').value;
	  if(valore<3){return '€ '+36}
	  if(valore==3){return '€ '+(valore*12)}
	  if(valore==4){return '€ '+(valore*10)}
	  if(valore==5){return '€ '+(valore*9)}
	  if(valore>=6){return '€ '+(valore*8)}
}

function verifica_orario() {
	var arrdate = document.getElementById('data').value.split("/");
	var orario = document.getElementById('orario').value;
	var orah = document.getElementById('OraH').value;
	var minm = document.getElementById('minM').value;
	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 ((document.getElementById('data').value=='14/02/2012') && (valore_orario()=="Pomeriggio"  || valore_orario()=="Mattino" || valore_orario()=="Pers")) {
		alert("Speedy Flowers: La data selezionata è San Valentino! Speedyflowers ti informa che tutte le consegne in questo giorno, saranno effettuate nell'arco dell'intera giornata. Tra le ore 09.00 e le ore 20.00, SENZA ALCUN VINCOLO ORARIO. Scelga come opzione la consegna IN GIORNATA.");
		document.forms.modulo.orario[2].checked=true;
		$('barraora').hide();
		return false;		
	}
	
	
	if (dataspedizione.valueOf() < dataod.valueOf()) {
		alert("La data di consegna indicata è già trascorsa.");
		return false;
	}
	
	if (oraat.getHours()>=20) {
	
	if ((dataspedizione.valueOf() == dataod.valueOf()) && (valore_orario()=="Pomeriggio")) {
		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");
		return false;
	}
	}
	
	if(oraat.getHours()>=13) {
	if ((dataspedizione.valueOf() == dataod.valueOf()) && (valore_orario()=="Mattino")) {
		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");
		return false;
	}
	}
	
	if ((valore_orario()=="Mattino") && (dataspedizione.valueOf()==dataod.valueOf())) {
		if (oraat.getHours()>=11 && oraat.getHours()<=13) {
			alert("L'ordine ricevuto dopo le 11:30 potrebbe essere effettuato nel primo pomeriggio.");
		}
	}
	
	if ((valore_orario()=="Pomeriggio") && (dataspedizione.valueOf()==dataod.valueOf())) {
		if (oraat.getHours()>=18 && oraat.getHours()<=20) {
			alert("Ciao sono Katia! - Sono Passate le ore 18:30, a causa dell'intenso traffico di ordini in arrivo Speedy Flowers non garantisce al 100% la consegna per Oggi Pomeriggio, solitamente riusciamo a soddisfare comunque la vostra richiesta. In caso di problemi la consegna verrà effettuata Domani o il primo giorno lavorativo. Speedy Flowers non accetta reclami qualora la consegna non venga effettuata per oggi. Per avere la GARANZIA di consegna Oggi stesso, le consigliamo di scegliere il supplemento 'ORARIO TASSATIVO'. Grazie per aver scelto SPEEDYFLOWERS!");
		}
	}
	
	if ((valore_orario()=="Giornata") && (dataspedizione.valueOf()==dataod.valueOf())) {
		if (oraat.getHours()>=18 && oraat.getHours()<=20) {
			alert("Ciao sono Katia! - Sono Passate le ore 18:30, a causa dell'intenso traffico di ordini in arrivo Speedy Flowers non garantisce al 100% la consegna per Oggi Pomeriggio, solitamente riusciamo a soddisfare comunque la vostra richiesta. In caso di problemi la consegna verrà effettuata Domani o il primo giorno lavorativo. Speedy Flowers non accetta reclami qualora la consegna non venga effettuata per oggi. Per avere la GARANZIA di consegna Oggi stesso, le consigliamo di scegliere il supplemento 'ORARIO TASSATIVO'. Grazie per aver scelto SPEEDYFLOWERS!");
		}
	}
	
	if ((valore_orario()=="Pers") && (dataspedizione.valueOf()==dataod.valueOf())) {
		if ((parseInt(orah))>=20) {
			alert("Ciao sono Katia! - Sono Passate le ore 20:00, non è possibilie più effettuare consegne con 'ORARIO TASSATIVO' per questa giornata. Grazie per aver scelto SPEEDYFLOWERS!");
			return false;
		}
	}
	
	if ((valore_orario()=="Pers") && (dataspedizione.valueOf()==dataod.valueOf())) {
		var date1 = new Date();
		var date2 = new Date(anno, mese-1, giorno, orah, minm,00);  
		var date_diff = Math.abs(date2.getTime() - date1.getTime())/(1000 * 60 * 60);

		if (date_diff<4) {
			alert("Ciao sono Katia! - scegliere un orario superiore di almeno 4 ore dall'orario corrente per garantire che la spedizione sia eseguita. Grazie per aver scelto SPEEDYFLOWERS!");
			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_quantita() {
		var numerorose=document.getElementById("quantita");
		var totale=Math.abs(parseInt(document.getElementById("totalemazzo").value));
		if (numerorose.value=="" && tipo_rose()!='Blu') {
					alert("Il numero minimo di Rose da inviare è di 3");
					document.getElementById('quantita').focus();
					document.getElementById('quantita').value=3;
					return false;
		}
		if (numerorose.value=="" && tipo_rose()=='Blu') {
					alert("Il numero minimo di Rose da inviare è di 3");
					document.getElementById('quantita').focus();
					document.getElementById('quantita').value=3;
					return false;
		}
		if (numerorose.value<3 && tipo_rose()!='Blu') {
					alert("Il numero minimo di Rose da inviare è di 3");
					document.getElementById('quantita').focus();
					document.getElementById('quantita').value=3;
					return false;
		}
		if (numerorose.value<12 && tipo_rose()=='Blu') {
					alert("Il numero minimo di Rose Blu da inviare è di 12");
					document.getElementById('quantita').focus();
					document.getElementById('quantita').value=12;
					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 verifica_modulo_rose()
{
	with(document.forms.modulo) {
		if (valiDate(data.value)==false) {
			document.modulo.data.focus();
			return false;
		}
		
		if (verifica_orario()==false) {
			return false;
		}
		
		if (verifica_quantita()==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;
}
