
//LA FUNZIONE VA RICHIAMATA NEL TAG form IN QUESTO MODO:
//<form name="modulo" action="sendmail.asp" method="post" onsubmit="return controllo('modulo', 'nome,email,msg'); return false;">
//INDICANDO NELL' onsubmit= IL NOME DEL FORM STESSO E IL NOME DEI CAMPI DA CONTROLLARE SEPARATI DA VIRGOLA


function controllo(formnome, arr) {
	// Controllo dei campi form (text-textarea-radio-select-checkbox)
	 
	var col_array = arr.split(",");

	var part_num=0;
	
	while (part_num < col_array.length)
	{
		var nomecampo = col_array[part_num];
		//
		var tipocampo = document.forms[formnome].elements[nomecampo].type; 
				
// ############################################################

			// Campo testo
			if (tipocampo ==  'text' || tipocampo == 'password') {
			
				// Se campo testo email
				var valorecampo = document.forms[formnome].elements[nomecampo].value;
				//alert(valorecampo.length);
				if (nomecampo.indexOf("email") != -1 && (valorecampo.indexOf("@") == -1 || valorecampo.indexOf(".") == -1 || valorecampo < 8)) {
					alert('Formato email non valido');
					return false;
				
				} else {
					// Se campo testo non email
					if (!valorecampo) {
						if (nomecampo == 'foto') {
							alert('Devi caricare almeno una tua foto per continuare');
						} else {
							alert('Compila il campo ' + nomecampo);
						}
						return false;
					}
				}
						
			}
		

// ############################################################
				
		// Campo textarea
		else if (tipocampo ==  'textarea') {
				
				// Se campo textarea
				var valorecampo = document.forms[formnome].elements[nomecampo].value;
				if (!valorecampo) {
					alert('Compila il campo ' + nomecampo);
					return false;
				}
		
		}

// ############################################################
		
		// Campo checkbox
		else if (tipocampo ==  'checkbox') {
				
				// Se campo checkbox
				var valorecampo = document.forms[formnome].elements[nomecampo].checked;
				if (!valorecampo) {
					alert('Il Campo "' + nomecampo + '" deve essere obbligatoriamente selezionato per continuare');
					return false;
				}
						
		}

	
// ############################################################
		
		// Campo select
		else if (tipocampo ==  'select-one') {
			var valorecampo = document.forms[formnome].elements[nomecampo].value;
			if(!valorecampo){
				alert('Compila il campo ' + nomecampo);
				return false;
			}		
		}

		part_num+=1;
	}
}

// Apertura popup centrato
/*_________________________________________________POP UP*/
	function popUp(pg, w, h, scr, res) 
	{ 
		if (w == "" || h == "")
		{
			w = (screen.width - 250);
			h = (screen.height - 250);
		}

		larg = screen.width;
		alt = screen.height;

		larg = (larg - w)/2;
		alt = (alt - h)/2;

		window.open(pg, 'p', 'scrollbars='+scr+', resizable='+res+', width='+w+', height='+h+', top='+alt+', left='+larg+', menubar=no, titlebar=no, status=no, location=no, toolbar=no');
	}

// Apertura popup centrato per la selezione del prodotto
/*_________________________________________________POP UP*/
	function popUpSeleziona(tipologia_prodotto) 
	{
		
		var url;
		url = 'seleziona.asp?';

		var listino;
		listino = document.forms['modprods'].elements['listinoP'].value;
		
		var id;
		id = document.forms['modprods'].elements['idP'].value;

		var p;
		p = document.forms['modprods'].elements['pP'].value;

		var numero;
		numero = document.forms['modprods'].elements['numeroP'].value;

		var tipologia_prodotto;
		url = url + 'listino=' + listino + '&id=' + id + '&p=' + p + '&numero=' + numero + '&tipologia_prodotto=' + tipologia_prodotto;

		var w;
		var h;
		w = 790;
		h = 600;

		larg = screen.width;
		alt = screen.height;

		larg = (larg - w)/2;
		alt = (alt - h)/2;

		window.open(url, 'p', 'scrollbars=yes, resizable=no, width='+w+', height='+h+', top='+alt+', left='+larg+', menubar=no, titlebar=no, status=no, location=no, toolbar=no');
	}


// ###### AJAX AGGIUNGI PRODOTTO NEL PREVENTIVO ########################
function ajaxprod(id,listino,qt)
{
var id;
var listino;
var qt;


var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
		document.getElementById('pp').innerHTML = xmlHttp.responseText;
	  }
    }
  xmlHttp.open("GET","ajaxprod.asp?id=" + id + "&listino=" + listino + "&qt=" + qt,true);
  xmlHttp.send(null);
  }
// ########################

function numerico(valore,modulo,campo)
{
	if (isNaN(valore) || (!valore))
	{
		alert('Inserisci un formato numerico per continuare!');
		document.forms[modulo].elements[campo].value=1;
		return false;
	}
}

function numerico2(valore,modulo,campo)
{
	if (isNaN(valore))
	{
		alert('Inserisci un formato numerico per continuare!');
		document.forms[modulo].elements[campo].value=0;
		return false;
	}	
	/*
	// MANODOPERA
	if (campo=='manodopera' && !isNaN(valore) && valore!='' && !isNaN(document.forms['preventivo'].elements['totale'].value))
	{
		tot = document.forms['preventivo'].elements['totale2'].value;
		tot = parseInt(tot) + parseInt(valore);
		
		// SE ESISTE LO SCONTO
		sconto2 =  document.forms['preventivo'].elements['sconto2'].value;
		if (parseInt(sconto2))
		{
			tot = tot - parseInt(sconto2);
		}

		// SE ESISTE SCONTO %
		sconto1 =  document.forms['preventivo'].elements['sconto1'].value;
		if (parseInt(sconto1))
		{
			sconto = tot * parseInt(sconto1) / 100;
			tot = tot - sconto;
		}

		document.forms['preventivo'].elements['totale'].value = tot;
	}
	// SCONTO IN EURO
	if (campo=='sconto2' && !isNaN(valore) && valore!='' && !isNaN(document.forms['preventivo'].elements['totale'].value))
	{
		tot = document.forms['preventivo'].elements['totale2'].value;
		
		tot = parseInt(tot) - parseInt(valore);
		
		manodopera =  document.forms['preventivo'].elements['manodopera'].value;
		if (parseInt(manodopera))
		{
			tot = tot + parseInt(manodopera);
		}

		// SE ESISTE SCONTO %
		sconto1 =  document.forms['preventivo'].elements['sconto1'].value;
		if (parseInt(sconto1))
		{
			sconto = tot * parseInt(sconto1) / 100;
			tot = tot - sconto;
		}

		document.forms['preventivo'].elements['totale'].value = tot;
	}

	// SCONTO IN %
	if (campo=='sconto1' && !isNaN(valore) && valore!='' && !isNaN(document.forms['preventivo'].elements['totale'].value))
	{
		tot = parseInt(document.forms['preventivo'].elements['totale2'].value);

		// SE ESISTE LA MANODOPERA
		manodopera =  document.forms['preventivo'].elements['manodopera'].value;
		if (parseInt(manodopera))
		{
			tot = tot + parseInt(manodopera);
		}
		// SE ESISTE LO SCONTO
		sconto2 =  document.forms['preventivo'].elements['sconto2'].value;
		if (parseInt(sconto2))
		{
			tot = tot - parseInt(sconto2);
		}
		// ORA SCONTO IN %
		sconto = tot * valore / 100;
		tot = tot - parseInt(sconto);

		document.forms['preventivo'].elements['totale'].value = tot;
	}
	*/
}

function ricalcolaPerc(totale, sconto) // Percentuale
{
	var totale;
	var sconto;
	var sc;

	sc = totale * sconto / 100;
	totale = totale - sc;
	//totale = Math.round((totale*100)/100);
	

	//document.forms['preventivo'].elements['totale'].value = totale;
	
}

function ricalcolaSconto(totale, sconto) // Euro
{
	var totale;
	var sconto;

	totale = totale - sconto;
	//totale = Math.round((totale*100)/100); 

	//document.forms['preventivo'].elements['totale'].value = totale;
}
// Aggiungi Manodopera al totale
function ricalcolaManodopera(totale, sconto) // Euro
{
	var totale;
	var sconto;
	totale = parseInt(totale);
	sconto = parseInt(sconto);
	totale = parseInt(totale + sconto);
	//totale = Math.round((totale*100)/100); 

	//document.forms['preventivo'].elements['totale'].value = totale;
}


// Lancio creazione PDF
function pdfPrint(id,utente) // Euro
{
	var id;
	var utente;
	var w;
	var h;
	w = 1024;
	h = 768;

	larg = screen.width;
	alt = screen.height;

	larg = (larg - w)/2;
	alt = (alt - h)/2;

	window.open('fpdf_php_2004/pdf.php?id=' + id + '&utente=' + utente, 'PDFPRINT', 'titlebar=no,menubar=no,scrollbars=yes,toolbar=no,width='+w+',height='+h+',left=' + larg + ',top=' + alt);
}


function ricalcolatutto()
{
	// ricalcola tutto

	// TOTALE PREVENTIVO
	
	tot = document.forms['preventivo'].elements['totale2'].value;

	if (isNaN(tot))
	{
		tot = 0;
	}	else	{
		tot = parseInt(tot);
	}


	// PRODOTTO PERSONALIZZATI
	p_1 = document.forms['preventivo'].elements['p_prezzo1'].value;
	if (isNaN(p_1) || p_1=='')
	{
		p_1 = 0;
	}
	p_1 = parseInt(p_1);
	//alert(p_1);

	p_2 = document.forms['preventivo'].elements['p_prezzo2'].value;
	if (isNaN(p_2) || p_2=='')
	{
		p_2 = 0;
	}
	p_2 = parseInt(p_2);
	//alert(p_2);

	p_3 = document.forms['preventivo'].elements['p_prezzo3'].value;
	if (isNaN(p_3) || p_3=='')
	{
		p_3 = 0;
	}
	p_3 = parseInt(p_3);
	//alert(p_3);

	p_4 = document.forms['preventivo'].elements['p_prezzo4'].value;
	if (isNaN(p_4) || p_4=='')
	{
		p_4 = 0;
	}
	p_4 = parseInt(p_4);
	//alert(p_4);

	p_5 = document.forms['preventivo'].elements['p_prezzo5'].value;
	if (isNaN(p_5) || p_5=='')
	{
		p_5 = 0;
	}
	p_5 = parseInt(p_5);
	//alert(p_5);

	// SOMMA PREZZI PRODOTTI
	p_per = (p_1) + (p_2) + (p_3) + (p_4) + (p_5);
	//alert(p_per);

	/*if (isNaN(manodopera))
	{
		manodopera = 0;
	}
	manodopera = parseInt(manodopera);
	
	tot = tot + manodopera + p_per;

	if (isNaN(sconto1))
	{
		sconto1 = 0;
	}
	sconto1 = parseInt(sconto1);

	tot = tot - sconto1;

	if (!isNaN(sconto2))
	{
		sc = tot * sconto2 / 100;
		tot = tot - sc;
	}
	sconto2 = parseInt(sconto2);
	*/
	
	//document.forms['preventivo'].elements['totale'].value = tot + p_per;
	//document.forms['preventivo'].elements['totale2'].value = tot + p_per;
	
}

function ricalcolofinale()
{
	// TOTALE
	tot = document.forms['preventivo'].elements['totale2'].value;
	if (isNaN(tot) || (!tot))
	{
		tot = 0;	
	}	else	{
		tot = parseInt(tot);
	}

	// PREZZO 1
	p1 = parseInt(document.forms['preventivo'].elements['p_prezzo1'].value);
	if (isNaN(p1) || (!p1))
	{
		p1 = 0;	
	}	else	{
		p1 = parseInt(p1);
	}
	// PREZZO 2
	p2 = parseInt(document.forms['preventivo'].elements['p_prezzo2'].value);
	if (isNaN(p2) || (!p2))
	{
		p2 = 0;	
	}	else	{
		p2 = parseInt(p2);
	}

	// PREZZO 3
	p3 = parseInt(document.forms['preventivo'].elements['p_prezzo3'].value);
	if (isNaN(p3) || (!p3))
	{
		p3 = 0;	
	}	else	{
		p3 = parseInt(p3);
	}

	// PREZZO 4
	p4 = parseInt(document.forms['preventivo'].elements['p_prezzo4'].value);
	if (isNaN(p4) || (!p4))
	{
		p4 = 0;	
	}	else	{
		p4 = parseInt(p4);
	}

	// PREZZO 5
	p5 = parseInt(document.forms['preventivo'].elements['p_prezzo5'].value);
	if (isNaN(p5) || (!p5))
	{
		p5 = 0;	
	}	else	{
		p5 = parseInt(p5);
	}

	// MANODOPERA
	manodopera = parseInt(document.forms['preventivo'].elements['manodopera'].value);
	if (isNaN(manodopera) || (!manodopera))
	{
		manodopera = 0;	
	}	else	{
		manodopera = parseInt(manodopera);
	}

	// SCONTO2 EURO
	sconto2 = parseInt(document.forms['preventivo'].elements['sconto2'].value);
	if (isNaN(sconto2) || (!sconto2))
	{
		sconto2 = 0;	
	}	else	{
		sconto2 = parseInt(sconto2);
	}

	
	totale = tot + p1 + p2 + p3 + p4 + p5 + manodopera - sconto2;
	totale = parseInt(totale)

	// SCONTO1 PERCENTUALE DI SCONTO
	sconto1 = parseInt(document.forms['preventivo'].elements['sconto1'].value);
	if (isNaN(sconto1) || (!sconto1))
	{
		//  NULLA	
	}	else	{
		sconto1 = parseInt(sconto1);
		sc = totale * sconto1 / 100;
		totale = totale - sc;
	}

	// STAMPO TOTALE
	//alert(totale);
	document.forms['preventivo'].elements['totale'].value = totale;
}
