var Articolo1 = new Array(0,50,70.15,100,150.50,200);
var Articolo2 = new Array(0,50,70.15,100,150.50,200);
var Articolo3 = new Array(0,50,70.15,100,150.50,200);
var Articolo4 = new Array(0,50,70.15,100,150.50,200);
var totale_generale;

function calcola() {
	var totale1 = 0;
	var totale2 = 0;
	var totale3 = 0;
	var totale4 = 0;
	var imponibile = 0;
	var iva = 0;
	var indice = 0;
    totale_generale = 0;
    
    indice = document.modulo.articolo1.selectedIndex;
    if (indice > 0) totale1 = Articolo1[indice];
    indice = document.modulo.articolo2.selectedIndex;
    if (indice > 0) totale2 = Articolo2[indice];
    indice = document.modulo.articolo3.selectedIndex;
    if (indice > 0) totale3 = Articolo3[indice];
    indice = document.modulo.articolo4.selectedIndex;
    if (indice > 0) totale4 = Articolo4[indice];
    
	document.modulo.totale_articolo1.value = FormattaEuro2(totale1);
	document.modulo.totale_articolo2.value = FormattaEuro2(totale2);
	document.modulo.totale_articolo3.value = FormattaEuro2(totale3);
	document.modulo.totale_articolo4.value = FormattaEuro2(totale4);
	imponibile = totale1 + totale2 + totale3 + totale4;
    document.modulo.imponibile.value = FormattaEuro2(imponibile);
	iva = Math.round((imponibile * 100 * 20) / 100) /100;
    document.modulo.iva.value = FormattaEuro2(iva);
	totale_generale = imponibile + iva;
    document.modulo.totale.value = FormattaEuro2(totale_generale);
}

function risetta() {
    document.modulo.reset();
    document.modulo.totale_articolo1.value = 0;
    document.modulo.totale_articolo2.value = 0;
    document.modulo.totale_articolo3.value = 0;
    document.modulo.totale_articolo4.value = 0;
    document.modulo.imponibile.value = 0;
    document.modulo.iva.value = 0;
    document.modulo.totale.value = 0;
    totale_generale = 0;
}

function NumeroOrdine() {
	var date = new Date();
	var Mese = date.getMonth() + 1;
	if (Mese < 10) Mese = "0" + Mese;
	var Giorno = date.getDate();
	if (Giorno < 10) Giorno = "0" + Giorno;
	var Ora = date.getHours();
	if (Ora < 10) Ora = "0" + Ora;
	var Minuti = date.getMinutes();
	if (Minuti < 10) Minuti = "0" + Minuti;
	var Secondi = date.getSeconds();
	if (Secondi < 10) Secondi = "0" + Secondi;
	var sOrd = "" + Mese + Giorno + Ora + Minuti + Secondi;
	return sOrd;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_validateForm() { //v3.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+='- la casella '+nm+' deve contenere un indirizzo di E-mail valido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- la casella '+nm+' deve contenere numeri.\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+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- la casella '+nm+' è obbligatoria.\n'; }
  }
    if (document.modulo.cliente.value == "") errors += "- bisogna indicare il nome e il cognome per l'intestazione della fattura.\n";
    if ((totale_generale == 14000) || (totale_generale == 0)) errors += "- non è stato selezionato alcun articolo.\n";
    if (errors) alert('Si sono verificati i seguenti errori:\n' + errors);
    if (errors == '') {
    	alert("Modulo dimostrativo");
    	document.MM_returnValue = false;
    } else {
    	document.MM_returnValue = (errors == '');
    }
}
