function round(n) {
	n = Math.round(n * 100) / 100;
    n = (n + 0.001) + '';
    return n.substring(0, n.indexOf('.') + 3); }

function CalcolaPreventivo(mesirestanti) {
	var TotSetupServ = 0;
	var TotCanoneServ = 0;
	var TotSetupLingue = 0;
	var TotCanoneLingue = 0;
	var TotSetupAIC = 0;
	var TotCanoneAIC = 0;
	var mesicanone = mesirestanti;
	var tot_elementi = document.preventivo.tot_voci.value;

	// SETUP
	document.preventivo.tot_setup_serv.value = 0;
	document.preventivo.tot_setup.value = 0;
	//document.preventivo.tot_setup_lingue.value = 0;
	// CANONI
	document.preventivo.tot_canone.value = 0;
	document.preventivo.tot_canone_serv.value = 0;
	//document.preventivo.tot_canone_lingue.value = 0;
	
	//alert(tot_elementi);
	
	for (i=1 ; i<=tot_elementi ; i++)
	{
		if (!parseInt(document.preventivo.elements['qta'+i].value)) {
			document.preventivo.elements['qta'+i].value = 0;
		} else {
			for (x=0; x < document.preventivo.elements['qta'+i].value.length; x++) {
				c = document.preventivo.elements['qta'+i].value.charAt(x);
				if ("0123456789.".indexOf(c,0) < 0) {
					document.preventivo.elements['qta'+i].value = 0;
				}
			}
		}

	TotSetupServ = TotSetupServ + (document.preventivo.elements['qta'+i].value * document.preventivo.elements['setup'+i].value);
	TotCanoneServ = TotCanoneServ + (document.preventivo.elements['qta'+i].value * document.preventivo.elements['canone'+i].value);
	}

	/*
	if (document.preventivo.lingue.options[document.preventivo.lingue.selectedIndex].value != "0")	
		{
		tot_lingue = document.preventivo.lingue.options[document.preventivo.lingue.selectedIndex].value
		if (document.preventivo.qta1.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua1.value * tot_lingue * document.preventivo.qta1.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua1.value * tot_lingue * document.preventivo.qta1.value);
			}
		if (document.preventivo.qta4.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua4.value * tot_lingue * document.preventivo.qta4.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua4.value * tot_lingue * document.preventivo.qta4.value);
			}
		if (document.preventivo.qta5.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua5.value * tot_lingue * document.preventivo.qta5.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua5.value * tot_lingue * document.preventivo.qta5.value);
			}
		
		}
	*/
	
	document.preventivo.tot_setup_serv.value = round(parseFloat(TotSetupServ));
	document.preventivo.tot_canone_serv.value = round(parseFloat(TotCanoneServ));
	//document.preventivo.tot_setup_lingue.value = round(parseFloat(TotSetupLingue));
	//document.preventivo.tot_canone_lingue.value = round(parseFloat(TotCanoneLingue));
	document.preventivo.tot_setup.value = round(parseFloat(TotSetupServ + TotSetupLingue));
	document.preventivo.tot_canone.value = round(parseFloat(TotCanoneServ + TotCanoneLingue));

	TotSetupAIC = TotSetupServ + TotSetupLingue;
	TotCanoneAIC = (TotCanoneServ + TotCanoneLingue)*mesicanone;

	document.preventivo.tot_setup_aic.value = round(parseFloat(TotSetupAIC));
	document.preventivo.tot_canone_aic.value = round(parseFloat(TotCanoneAIC));
	document.preventivo.tot_aic.value = round(parseFloat(TotSetupAIC + TotCanoneAIC));
}


function OLDDDDDDDDDDDDDDCalcolaPreventivo(mesirestanti) {
	var TotSetupServ = 0;
	var TotCanoneServ = 0;
	var TotSetupLingue = 0;
	var TotCanoneLingue = 0;
	var TotSetupAIC = 0;
	var TotCanoneAIC = 0;
	var mesicanone = mesirestanti;

	document.preventivo.tot_setup_serv.value = 0;
	document.preventivo.tot_canone_serv.value = 0;
	//document.preventivo.tot_setup_lingue.value = 0;
	//document.preventivo.tot_canone_lingue.value = 0;
	document.preventivo.tot_setup.value = 0;
	document.preventivo.tot_canone.value = 0;

	if (!parseInt(document.preventivo.qta1.value)) {
		document.preventivo.qta1.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta1.value.length; i++) {
			c = document.preventivo.qta1.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta1.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta2.value)) {
		document.preventivo.qta2.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta2.value.length; i++) {
			c = document.preventivo.qta2.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta2.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta3.value)) {
		document.preventivo.qta3.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta3.value.length; i++) {
			c = document.preventivo.qta3.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta3.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta4.value)) {
		document.preventivo.qta4.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta4.value.length; i++) {
			c = document.preventivo.qta4.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta4.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta5.value)) {
		document.preventivo.qta5.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta5.value.length; i++) {
			c = document.preventivo.qta5.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta5.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta6.value)) {
		document.preventivo.qta6.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta6.value.length; i++) {
			c = document.preventivo.qta6.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta6.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta7.value)) {
		document.preventivo.qta7.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta7.value.length; i++) {
			c = document.preventivo.qta7.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta7.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta8.value)) {
		document.preventivo.qta8.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta8.value.length; i++) {
			c = document.preventivo.qta8.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				doAcument.preventivo.q08.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta9.value)) {
		document.preventivo.qta9.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta9.value.length; i++) {
			c = document.preventivo.qta9.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta9.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta10.value)) {
		document.preventivo.qta10.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta10.value.length; i++) {
			c = document.preventivo.qta10.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta10.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta11.value)) {
		document.preventivo.qta11.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta11.value.length; i++) {
			c = document.preventivo.qta11.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta11.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta12.value)) {
		document.preventivo.qta12.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta12.value.length; i++) {
			c = document.preventivo.qta12.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta12.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta13.value)) {
		document.preventivo.qta13.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta13.value.length; i++) {
			c = document.preventivo.qta13.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta13.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta14.value)) {
		document.preventivo.qta14.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta14.value.length; i++) {
			c = document.preventivo.qta14.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta14.value = 0;
			}
		}
	}
	if (!parseInt(document.preventivo.qta15.value)) {
		document.preventivo.qta15.value = 0;
	} else {
		for (i=0; i< document.preventivo.qta15.value.length; i++) {
			c = document.preventivo.qta15.value.charAt(i);
			if ("0123456789.".indexOf(c,0) < 0) {
				document.preventivo.qta15.value = 0;
			}
		}
	}
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta1.value * document.preventivo.setup1.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta2.value * document.preventivo.setup2.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta3.value * document.preventivo.setup3.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta4.value * document.preventivo.setup4.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta5.value * document.preventivo.setup5.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta6.value * document.preventivo.setup6.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta7.value * document.preventivo.setup7.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta8.value * document.preventivo.setup8.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta9.value * document.preventivo.setup9.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta10.value * document.preventivo.setup10.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta11.value * document.preventivo.setup11.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta12.value * document.preventivo.setup12.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta13.value * document.preventivo.setup13.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta14.value * document.preventivo.setup14.value);
	TotSetupServ = TotSetupServ + eval(document.preventivo.qta15.value * document.preventivo.setup15.value);
	
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta1.value * document.preventivo.canone1.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta2.value * document.preventivo.canone2.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta3.value * document.preventivo.canone3.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta4.value * document.preventivo.canone4.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta5.value * document.preventivo.canone5.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta6.value * document.preventivo.canone6.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta7.value * document.preventivo.canone7.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta8.value * document.preventivo.canone8.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta9.value * document.preventivo.canone9.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta10.value * document.preventivo.canone10.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta11.value * document.preventivo.canone11.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta12.value * document.preventivo.canone12.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta13.value * document.preventivo.canone13.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta14.value * document.preventivo.canone14.value);
	TotCanoneServ = TotCanoneServ + eval(document.preventivo.qta15.value * document.preventivo.canone15.value);

	/*
	if (document.preventivo.lingue.options[document.preventivo.lingue.selectedIndex].value != "0")	
		{
		tot_lingue = document.preventivo.lingue.options[document.preventivo.lingue.selectedIndex].value
		if (document.preventivo.qta1.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua1.value * tot_lingue * document.preventivo.qta1.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua1.value * tot_lingue * document.preventivo.qta1.value);
			}
		if (document.preventivo.qta4.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua4.value * tot_lingue * document.preventivo.qta4.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua4.value * tot_lingue * document.preventivo.qta4.value);
			}
		if (document.preventivo.qta5.value != 0)
			{
			TotSetupLingue = TotSetupLingue + eval(document.preventivo.setup_lingua5.value * tot_lingue * document.preventivo.qta5.value);
			TotCanoneLingue = TotCanoneLingue + eval(document.preventivo.canone_lingua5.value * tot_lingue * document.preventivo.qta5.value);
			}
		
		}
	*/
	
	document.preventivo.tot_setup_serv.value = round(parseFloat(TotSetupServ));
	document.preventivo.tot_canone_serv.value = round(parseFloat(TotCanoneServ));
	//document.preventivo.tot_setup_lingue.value = round(parseFloat(TotSetupLingue));
	//document.preventivo.tot_canone_lingue.value = round(parseFloat(TotCanoneLingue));
	document.preventivo.tot_setup.value = round(parseFloat(TotSetupServ + TotSetupLingue));
	document.preventivo.tot_canone.value = round(parseFloat(TotCanoneServ + TotCanoneLingue));

	TotSetupAIC = TotSetupServ + TotSetupLingue;
	TotCanoneAIC = (TotCanoneServ + TotCanoneLingue)*mesicanone;

	document.preventivo.tot_setup_aic.value = round(parseFloat(TotSetupAIC));
	document.preventivo.tot_canone_aic.value = round(parseFloat(TotCanoneAIC));
	document.preventivo.tot_aic.value = round(parseFloat(TotSetupAIC + TotCanoneAIC));
}

function Popup(str) {
window.open(str,'printwindow','scrollbars=yes,resizable=yes,width=650,height=450,left=15,top=15,status=no,location=no,toolbar=yes');
}

function verEmail(stringa) {
   if (stringa.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
   return true;
   else return false;
}

function ControllaForm()
{
var msgErrore = '';
var nome = document.registra.nome;
var cognome = document.registra.cognome;
var ragsociale = document.registra.ragsociale;
var cf = document.registra.CodiceFiscale;
var pi = document.registra.PartitaIva;
var e1 = document.registra.mail_user;
var e2 = document.registra.mail_dominio;
var e3 = document.registra.mail_ext;
var mail = e1.value+'@'+e2.value+'.'+e3.value;
var FlagDatiPersonali = document.registra.FlagDatiPersonali;


if ( ragsociale.value != "" && ragsociale.value.replace(/\s*/gi,'') != '' && ragsociale.value.replace(/\x13\x10*/gi,'') != '' )
	{
//	if ( ( nome.value != "" || nome.value.replace(/\s*/gi,'') != '' || nome.value.replace(/\x13\x10*/gi,'') != '' ) || ( cognome.value != "" || cognome.value.replace(/\s*/gi,'') != '' || cognome.value.replace(/\x13\x10*/gi,'') != '' ) )
//	msgErrore+="Devono essere inseriti Cognome e Nome e/o Ragione Sociale\n";
	}
else
	{
	if ( ( nome.value == "" && nome.value.replace(/\s*/gi,'') == '' && nome.value.replace(/\x13\x10*/gi,'') == '' ) || ( cognome.value == "" && cognome.value.replace(/\s*/gi,'') == '' && cognome.value.replace(/\x13\x10*/gi,'') == '' ) )
	msgErrore+="Devono essere inseriti Cognome e Nome e/o Ragione Sociale\n";
	}

//if ( cf.value == "" && pi.value == "" ) 
//	{ msgErrore+="E' richiesto il codice fiscale oppure la partita IVA\n"; }

if ( (mail == "") || ( (mail.replace(/\s*/gi,'') == '') || (mail.replace(/\x13\x10*/gi,'') == '') ) )
	{ msgErrore+="E' richiesto l'indirizzo E-Mail dell'utente\n"; }

if (mail != "")
	{
	if (!verEmail(mail))
	{ msgErrore+="L'indirizzo E-Mail dell'utente deve essere in formato corretto\n"; }
	}

if (!FlagDatiPersonali[0].checked)
  {msgErrore = msgErrore + "Per procedere è necessario autorizzare il trattamento dei dati personali\n";} 

if (msgErrore != "")
  {
   msgErrore = "Sono stati rilevati i seguenti errori:\n\n" + msgErrore
   alert(msgErrore);
   return false
  }

else
	{
	document.registra.submit()
	}
}

function NEWControllaForm(f)
{
	var msgErrore = '';
	var nome = f.nome.value;
	var cognome = f.cognome.value;
	var ragsociale = f.ragsociale.value;
	var indirizzo = f.indirizzo.value;
	var cap = f.cap.value;
	var citta = f.citta.value;
	var telefono = f.telefono.value;
	var cf = f.CodiceFiscale.value;
	var pi = f.PartitaIVA.value;
	var mail = f.mail_user.value+'@'+f.mail_dominio.value+'.'+f.mail_ext.value;
	var FlagDatiPersonali = f.FlagDatiPersonali;
	
	if (cf != '')
	{
		cf = cf.replace(/\s/g,"");
		cf = cf.toUpperCase();
	}
	
	if ( ragsociale == "" && ragsociale.replace(/\s*/gi,'') == '' && ragsociale.replace(/\x13\x10*/gi,'') == '' )
	{
		if ( ( nome == "" && nome.replace(/\s*/gi,'') == '' && nome.replace(/\x13\x10*/gi,'') == '' ) || ( cognome == "" && cognome.replace(/\s*/gi,'') == '' && cognome.replace(/\x13\x10*/gi,'') == '' ) )
			msgErrore += "- Devono essere inseriti Cognome e Nome e/o Ragione Sociale.\n";
	}
	
	if ( indirizzo == "" && indirizzo.replace(/\s*/gi,'') == '' && indirizzo.replace(/\x13\x10*/gi,'') == '' )
		msgErrore += "- Indirizzo è obbligatorio e non può essere vuoto.\n";
	
	if (!cap.match(/^[0-9]{5}$/))
		msgErrore += "- CAP è obbligatorio e deve essere in formato valido.\n";

	if ( citta == "" && citta.replace(/\s*/gi,'') == '' && citta.replace(/\x13\x10*/gi,'') == '' )
		msgErrore += "- Città è obbligatorio e non può essere vuoto.\n";
	
	if ( telefono == "" && telefono.replace(/\s*/gi,'') == '' && telefono.replace(/\x13\x10*/gi,'') == '' )
		msgErrore += "- Telefono è obbligatorio e non può essere vuoto.\n";
	
	if ( ( cf == "" && cf.replace(/\s*/gi,'') == '' && cf.replace(/\x13\x10*/gi,'') == '' ) && ( pi == "" && pi.replace(/\s*/gi,'') == '' && pi.replace(/\x13\x10*/gi,'') == '' ) )
		msgErrore += "- Devono essere inseriti Codice Fiscale o Partita IVA.\n";
	else
	{
		if (cf != '' && (!cf.match(/^[0-9]{11}$/) && !cf.match(/^([A-Z]{6})+([0-9]{2})+([A-Z]{1})+([0-9]{2})+([A-Z]{1})+([0-9]{3})+([A-Z]{1})$/)))
			msgErrore += "- Codice Fiscale deve essere in formato valido.\n";
		if (pi != '' && !pi.match(/^[0-9]{11}$/))
			msgErrore += "- Partita IVA deve essere in formato valido.\n";
	}

	if (!verEmail(mail))
		msgErrore += "- L'indirizzo E-Mail deve essere in formato corretto.\n";
	
	if (!FlagDatiPersonali[0].checked)
	  {msgErrore = msgErrore + "Per procedere è necessario autorizzare il trattamento dei dati personali\n";} 
	
	if (msgErrore != "")
  {
	   msgErrore = "Sono stati rilevati i seguenti errori:\n\n" + msgErrore
	   alert(msgErrore);
	   return false
  }
	else
	{
		document.registra.submit() 
	}
}

function InviaModulo(f,lingua)
{
	var controlla;
	var msgErrore = '';
	switch(lingua)
	{
		case 'eng':
			msg_errore1 = ": is required and can\'t be empty.\n"
			msg_errore2 = ": insert a valid e-mail address.\n"
			tit_msg_errore = "Errors found:\n\n";
			break;
		default:
			msg_errore1 = ": è obbligatorio e non può essere vuoto.\n"
			msg_errore2 = ": inserire un indirizzo email valido.\n"
			tit_msg_errore = "Si sono verificati i seguenti errori:\n\n";
	}
	
	for(var i=0 ; i < f.elements.length ; i++)
	{
		controlla = f.elements[i].name.substring(f.elements[i].name.length-1);
		if (controlla != '0')
		{
			switch (controlla)
			{
				case '1':
					if ( (f.elements[i].value == '') || (f.elements[i].value.replace(/\s*/gi,'') == '' ) || (f.elements[i].value.replace(/\x13\x10*/gi,'') == '') )
						msgErrore += "- " + FormattaAlert(f.elements[i].name.substring(0,f.elements[i].name.length-2)) + msg_errore1;
					break;
				case '2':
					if (!verEmail(f.elements[i].value))
						msgErrore += "- " + FormattaAlert(f.elements[i].name.substring(0,f.elements[i].name.length-2)) + msg_errore2;
					break;
			}
		}
	}
	
	if (msgErrore != '')
	{
		alert(tit_msg_errore + msgErrore);
		return false;
	}
	else
		return true;
		
}

function FormattaAlert(nome)
{
	var testo = nome;
	testo = testo.replace("§"," ");
	testo = testo.replace("[a]","à");
	testo = testo.replace("[e]","è");
	testo = testo.replace("[i]","ì");
	testo = testo.replace("[o]","ò");
	testo = testo.replace("[u]","ù");

	return testo;
}

