
///////////////////////////////////////////////// Variabili globali
var elementi = new Array();
var prezzi = new Array()
/////////////////////////////////////////////////
function zeri(stringa)// Aggiunge gli zeri (EURO)
{ 
if (stringa.indexOf(',')==-1) stringa=stringa+",00"; 
else 
{ 
indice=stringa.indexOf(','); 
intero=stringa.substr(0, indice); 
decimale=stringa.substr(indice+1, stringa.length-indice); 
if (decimale.length==1) decimale+='0'; 
stringa=intero+','+decimale; 
} 
return stringa; 
}
///////////////////////////////////////////////////////////
function arrotonda(stringa)//Arrotonda a due cifre decimali
{ 
indice=stringa.indexOf(',');
stringa=stringa.substr(0,indice+3);
return stringa
}
///////////////////////////////////////////////////////
function trattino(stringa)//Ricava quantità (Trattino )
{ 
if (stringa.indexOf('-')==-1) stringa=stringa+"-"; 
indice=stringa.indexOf('-'); 
stringa=stringa.substr(0,indice); 
return stringa; 
}
////////////////////////////////////////////////////
function somma(valore,prezzo,pos) { // Calcola somma
prezzo=prezzo.replace(",",".");
dato=document.acquista[valore].value;
dato=trattino(dato);
elementi[pos]=dato;
prezzi[pos]=prezzo;
var totale_fin=0; // Azzera totale
var totale_libri=0; // Azzera totale libri
var spese_sped=0; // Azzera spese di spedizione
/////////////////////////////////////////////////////////Calcola totale
for (var i=0; i<elementi.length;i++){
if (elementi[i]==null){elementi[i]=0;}//Se l'elemento è indefinito deve essere uguale a 0
if (prezzi[i]==null){prezzi[i]=0;}//Se l'elemento è indefinito deve essere uguale a 0
totale_fin=totale_fin+(elementi[i]*parseFloat(prezzi[i]));
totale_libri=parseFloat(totale_libri) + parseFloat(elementi[i]);
}
//Calcola prezzi unitari * qta ( costo )
for (var i=0; i<document.acquista.length; i++){ 
    if (document.acquista.elements[i].ident==pos){
	   costo=elementi[pos]*parseFloat(prezzi[pos]);
	   costo=costo.toString();
	   costo=costo.replace(".",",");
       costo=zeri(costo);
	   document.acquista.elements[i].value=arrotonda(costo);
   }
} 
//Calcola spese di spedizione
if (totale_libri<=2){
spese_sped="0";
}else{spese_sped="0";}
////////////////////////////////////////////////////////Assegna valori
document.acquista.spese_sped.value=spese_sped;
// Coneverte e formatta ( Euro )
totale=parseFloat(totale_fin)+parseFloat(spese_sped);
totale = totale.toString();
totale=totale.replace(".",",");
totale=zeri(totale);
/////////////////////////////////////////////////
document.acquista.totale.value=arrotonda(totale);
document.acquista.tot_libri.value=totale_libri;
}// end somma 
