// JavaScript Document
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo email valido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' deve contenere un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' deve contenere un numero tra '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' è un campo obbligatorio.\n'; }
  } 
  if ((document.ordineofferte.PartitaIva.value=="") && (document.ordineofferte.CodFiscale.value==""))
{ errors+='- Part. IVA o Cod. Fiscale sono campi obbligatori.\n';
}
 if (document.ordineofferte.Provincia.value==0)
 { errors+='- Provincia è un campo obbligatorio.\n';}
 if ((document.ordineofferte.quantita1.value==0) || (document.ordineofferte.prodotto1.value==0)){ errors+="- E' NECESSARIO ORDINARE ALMENO UN PRODOTTO PRINCIPALE.\n";}

  if (errors) alert('Si sono verificati i seguenti errori:\n'+errors);
  if (!(document.ordineofferte.Autorizzo_Trattamento_Dati.checked)) {alert('Per poter inviare il modulo è necessario acconsentire al trattamento dei dati personali');
	errors += '- autorizzazione non concessa \n';}
	document.MM_returnValue = (errors == '');
}

function CambiaPrezzo(objProdotto, objQuantita, objPrezzo, objPrh,objCod) {
var Prezzo, Quantita, Valore, PrezzoUnitario, PrezzoTotale; 
var objP = MM_findObj(objProdotto);
var objQ=MM_findObj(objQuantita);
var objPr=MM_findObj(objPrezzo);
var objPh=MM_findObj(objPrh);
var objCd=MM_findObj(objCod);
		PrezzoUnitario=Offerta[parseFloat(objP.value)]/1.2; //togliere 1.2 se iva esclusa
		Quantita=objQ.value;
	if (parseFloat(Quantita)==0 || parseFloat(objP.value)==0) {
		objPr.value=""
		objPh.value=0;
		AggiornaTotali();
		}
	else
	{
		Prezzo=PrezzoUnitario*parseFloat(Quantita)
		Valore=formattaeuro(Prezzo.toString())
		objPr.value=formattaeuro(Valore)
		objPh.value=formattaeuro(Valore)
		AggiornaTotali();
		if (parseFloat(objP.value)>12)
		{ objCd.value="Ac"+parseFloat(objP.value); }
		else
		{
		objCd.value="Of"+parseFloat(objP.value);
		} }
}

function AggiornaTotali()
{
var TotaleNoIVA, TotaleConIVA, Trasporto, IVA, tmp, valore, controllo, tmpIVA;
TotaleNoIVA=0;
Trasporto=30;
tmp=0;
controllo="";
valore=0;
controllo=document.ordineofferte.prh1.value;
if (!isNaN(controllo))
{
valore=parseFloat(document.ordineofferte.prh1.value);
tmp=valore;
}
controllo="";
valore=0;
controllo=document.ordineofferte.prh2.value;

if (!isNaN(controllo))
{
valore=parseFloat(document.ordineofferte.prh2.value)
tmp=tmp+valore

}
controllo="";
valore=0;
controllo=document.ordineofferte.prh3.value;

TotaleNoIVA=tmp;
if (document.ordineofferte.pagamento.value=="bonifico anticipato")
{ TotaleNoIVA=TotaleNoIVA*0.98}
document.ordineofferte.totalenoiva.value=formattaeuro(TotaleNoIVA.toString());
document.ordineofferte.totalenoivah.value=formattaeuro(TotaleNoIVA.toString());
Trasporto=calcolatrasporto();
document.ordineofferte.ptrasporto.value=formattaeuro(Trasporto.toString());
document.ordineofferte.ptrasportoh.value=formattaeuro(Trasporto.toString());
IVA=(TotaleNoIVA+Trasporto)*0.2;
document.ordineofferte.iva.value=formattaeuro(IVA.toString());
document.ordineofferte.ivah.value=formattaeuro(IVA.toString());
TotaleConIVA=TotaleNoIVA+Trasporto+IVA;
document.ordineofferte.totaleconiva.value=formattaeuro(TotaleConIVA.toString());
document.ordineofferte.totaleconivah.value=formattaeuro(TotaleConIVA.toString());

/*da controllare*/
/*Prodott1*/
if (document.ordineofferte.prodotto1.value=="1") {document.ordineofferte.ProdottoOrdinato1.value="Cuscino Ergonomico PRIMULAcompleto di accessori"}
if (document.ordineofferte.prodotto1.value=="2") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Legno ARMONIA"}
if (document.ordineofferte.prodotto1.value=="3") {document.ordineofferte.ProdottoOrdinato1.value="Sedia Ergonomica VIOLA completa di accessori"}
if (document.ordineofferte.prodotto1.value=="4") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Legno con Schien. Reg. BEAUTY"}
if (document.ordineofferte.prodotto1.value=="5") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Alluminio FITNESS"}
if (document.ordineofferte.prodotto1.value=="6") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Alluminio con Schien. Reg. CHAMPION"}
if (document.ordineofferte.prodotto1.value=="7") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Bamboo GELSOMINO completo di accessori"}
if (document.ordineofferte.prodotto1.value=="8") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Legno NATURA con Sost. Viso e Custodia"}
if (document.ordineofferte.prodotto1.value=="9") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Portatile in Legno AURORA con Acc. facilitato e Custodia"}
if (document.ordineofferte.prodotto1.value=="10") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Stazionario in Legno MIMOSA completo di accessori"}
if (document.ordineofferte.prodotto1.value=="11") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Stazionario in Legno MELISSA completo di accessori"}
if (document.ordineofferte.prodotto1.value=="12") {document.ordineofferte.ProdottoOrdinato1.value="Lettino Port. in All. ProfessionalLINE, QLFR, Supp. Braccia e Custodia"}
if (document.ordineofferte.prodotto1.value=="0") {document.ordineofferte.ProdottoOrdinato1.value=""}
/*Prodotto2*/
if (document.ordineofferte.prodotto2.value=="20") {document.ordineofferte.ProdottoOrdinato2.value="Custodia Natura"}
if (document.ordineofferte.prodotto2.value=="21") {document.ordineofferte.ProdottoOrdinato2.value="Custodia Aurora"}
if (document.ordineofferte.prodotto2.value=="22") {document.ordineofferte.ProdottoOrdinato2.value="Custodia SportiveLINE"}
if (document.ordineofferte.prodotto2.value=="23") {document.ordineofferte.ProdottoOrdinato2.value="Allungo per il lettino"}
if (document.ordineofferte.prodotto2.value=="24") {document.ordineofferte.ProdottoOrdinato2.value="Portarotolo Universale"}
if (document.ordineofferte.prodotto2.value=="25") {document.ordineofferte.ProdottoOrdinato2.value="Scatola di cuffie igieniche da 100 pezzi"}
if (document.ordineofferte.prodotto2.value=="26") {document.ordineofferte.ProdottoOrdinato2.value="Sostegno Viso Semplice"}
if (document.ordineofferte.prodotto2.value=="27") {document.ordineofferte.ProdottoOrdinato2.value="Sostegno Viso QLFR"}
if (document.ordineofferte.prodotto2.value=="28") {document.ordineofferte.ProdottoOrdinato2.value="Cuscino Triangolare Sterno"}
if (document.ordineofferte.prodotto2.value=="29") {document.ordineofferte.ProdottoOrdinato2.value="Fascia Supporto Braccia Natura"}
if (document.ordineofferte.prodotto2.value=="30") {document.ordineofferte.ProdottoOrdinato2.value="Set 2Pz. Coprilettino Colorato"}
if (document.ordineofferte.prodotto2.value=="0") {document.ordineofferte.ProdottoOrdinato2.value=""}
/*Prodotto3*/
if (document.ordineofferte.prodotto3.value=="20") {document.ordineofferte.ProdottoOrdinato3.value="Custodia Natura"}
if (document.ordineofferte.prodotto3.value=="21") {document.ordineofferte.ProdottoOrdinato3.value="Custodia Aurora"}
if (document.ordineofferte.prodotto3.value=="22") {document.ordineofferte.ProdottoOrdinato3.value="Custodia SportiveLINE"}
if (document.ordineofferte.prodotto3.value=="23") {document.ordineofferte.ProdottoOrdinato3.value="Allungo per il lettino"}
if (document.ordineofferte.prodotto3.value=="24") {document.ordineofferte.ProdottoOrdinato3.value="Portarotolo Universale"}
if (document.ordineofferte.prodotto3.value=="25") {document.ordineofferte.ProdottoOrdinato3.value="Scatola di cuffie igieniche da 100 pezzi"}
if (document.ordineofferte.prodotto3.value=="26") {document.ordineofferte.ProdottoOrdinato3.value="Sostegno Viso Semplice"}
if (document.ordineofferte.prodotto3.value=="27") {document.ordineofferte.ProdottoOrdinato3.value="Sostegno Viso QLFR"}
if (document.ordineofferte.prodotto3.value=="28") {document.ordineofferte.ProdottoOrdinato3.value="Cuscino Triangolare Sterno"}
if (document.ordineofferte.prodotto3.value=="29") {document.ordineofferte.ProdottoOrdinato3.value="Fascia Supporto Braccia Natura"}
if (document.ordineofferte.prodotto3.value=="30") {document.ordineofferte.ProdottoOrdinato3.value="Set 2Pz. Coprilettino Colorato"}
if (document.ordineofferte.prodotto3.value=="0") {document.ordineofferte.ProdottoOrdinato3.value=""}
}

function formattaeuro (euro)
{
var eurostringa, punto, interi, lunghezza, decimali;
euro=arrotonda(euro);
eurostringa=euro.toString();
punto=eurostringa.indexOf('.');
if (punto>0) {
	interi=eurostringa.substring(0,punto)
	decimali=eurostringa.substring(punto+1,punto+3)
	lunghezza= decimali.length 
	
	if (lunghezza==1) {
		decimali=decimali+"0"
		}
	}
else
{	interi=eurostringa
	decimali = "00" }
	
eurostringa=interi +'.'+ decimali;
return eurostringa;

}

function arrotonda(cifra)
{
var numero,risultato;
numero=parseFloat(cifra);
numero=numero*100;
risultato=Math.round(numero);
risultato=risultato/100;
return risultato;
}

function calcolatrasporto()
{var trasportosingolo,trasporto,prezzo;
trasporto=0;

if (document.ordineofferte.trasporto.value=="proprio")
{
document.ordineofferte.resa.value="porto assegnato"

}
else 
{
if (document.ordineofferte.Provincia.value==0)
{ alert ("ERRORE! Inserire la provincia per il corretto calcolo delle spese di trasporto")
}
else 
{
document.ordineofferte.resa.value="porto franco";
quantita=parseInt(document.ordineofferte.quantita1.value);
trasportosingolo=altroprezzo(parseInt(document.ordineofferte.prodotto1.value));
trasporto=trasporto+trasportosingolo*quantita;
quantita=parseInt(document.ordineofferte.quantita2.value);
trasportosingolo=altroprezzo(parseInt(document.ordineofferte.prodotto2.value));
trasporto=trasporto+trasportosingolo*quantita;
quantita=parseInt(document.ordineofferte.quantita3.value);
trasportosingolo=altroprezzo(parseInt(document.ordineofferte.prodotto3.value));
trasporto=trasporto+trasportosingolo*quantita;
}
}
return trasporto;

}

function altroprezzo (codice) {
var isoleocalabria,x,provincia, trasporto;
var prov= new Array("AG","CL","CT","EN","ME","PA","RG","SR","TP","CZ","CS","KR","RC","VV","CA","NU","OR","SS");
isoleocalabria=false;

x=0;
while (x<18)
{ if (document.ordineofferte.Provincia.value==prov[x])
{
isoleocalabria=true}
x++;}
if (document.ordineofferte.isola.value=="si")
{
isoleocalabria=true}

if (codice==2 || codice==3  || codice==4 || codice==5 || codice==6  || codice==7 || codice==8 || codice==9 || codice==12) 
	{ if (isoleocalabria)
		{ trasporto=45;
		 }
	  else
	  	{ trasporto=30;
		}
	}
if  (codice==1) // offerta cuscino
	{ if (isoleocalabria)
		{ trasporto=30;
		}
	  else
	  	{ trasporto=20; 
		}
	}
 if  (codice==10 || codice==11)  //Lettini fissi 
	{ if (isoleocalabria)
		{ trasporto=115;
		}
	  else
	  	{ trasporto=80; 
		}
	} 
if  (codice==0 ) 
	{ trasporto=0;
	}
if (codice >= 20 )
	{ if (document.ordineofferte.quantita1.value>=1) {trasporto =0;}
	  if ((document.ordineofferte.quantita1.value==0) && (document.ordineofferte.prodotto1.value==0)) 
	  	{alert('ATTENZIONE!: Gli accessori possono essere ordinati\nsolo con un prodotto principale');
	  	 trasporto=0;}
	 }
return trasporto;
}

function aggiornatrasporto()
{var Trasporto;
Trasporto=calcolatrasporto();
document.ordineofferte.ptrasporto.value=formattaeuro(Trasporto.toString());
document.ordineofferte.ptrasportoh.value=formattaeuro(Trasporto.toString());
AggiornaTotali();
}

function attenzioneresa()
{
if (document.ordineofferte.trasporto.value=="corriere") 
{ document.ordineofferte.resa.value="porto franco"}
else
{ document.ordineofferte.resa.value="porto assegnato"}
}