var importo;
var costoArr = new Array(0,7.80,7.80,7.80,7.80);
var tempoArr = new Array(0,6,11);

function FormattaEuro2(num) {
	num = Math.floor((num * 100) + 0.50000000001);
	cents = num % 100;
	if (cents < 10) cents = "0" + cents;
	num = Math.floor(num/100).toString();
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3));
	return (num + ',' + cents);
}
function FormattaEuro(num) {
	num = Math.floor((num * 100) + 0.50000000001);
	cents = num % 100;
	if (cents < 10) cents = "0" + cents;
	num = Math.floor(num/100).toString();
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3));
	return (num + '.' + cents);
}

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 calcola() {
	importo = 0;
	var imponibile = 0;
	var iva = 0;
	var setupiniz = 0;
	var prezzo = costoArr[document.pagamento.interfaccia.selectedIndex];
	var tempo = tempoArr[document.pagamento.durata.selectedIndex];
	if (document.pagamento.setup.checked) setupiniz = 52;
	imponibile = (prezzo * tempo) + setupiniz;
	if (imponibile > 0) iva = Math.round((imponibile * 100 * 20) / 100) /100;
	importo = FormattaEuro(imponibile + iva);
	document.pagamento.testoimporto.value = FormattaEuro2(importo);
	if (importo == 0) {
		document.pagamento.testoimporto.value = "0,00";
	}
}

function risetta() {
	document.pagamento.reset();
	document.pagamento.testoimporto.value = "0,00";
	importo = 0;
}

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+='- '+nm+' deve contenere un indirizzo valido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- '+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 += 'il campo '+nm+' è obbligatorio.\n'; }
  }
    if (document.pagamento.interfaccia.options[0].selected) errors += "bisogna selezionare l'interfaccia.\n";
    if (document.pagamento.durata.options[0].selected) errors += 'bisogna selezionare la durata del noleggio.\n';
    if (errors) alert('Si sono verificati i seguenti errori:\n'+errors);
	var NumOrd = NumeroOrdine() + "PAY";
  	var f = document.pagamento;
  	f.redirect.value = "http://asp.atlantisgroup.it/GestPayCrypt/GestPayCrypt.asp?ORDNUM=" + NumOrd + "&IMPORTO=" + importo + "&IDNEGOZIO=atlantis";
	document.MM_returnValue = (errors == '');
}
