//**************************************************************************
function CheckDBValue(textc, campo, rexp, obbligo, nome_form)
{

	// campo, indica il nome del campo da valutare
	// rexp, indica quale espressione regolare utilizzare e quale messaggio di alert
	//obbligo, se 1 indica che il campo è obbligatorio, se 0 indica che può avere valore nullo
	//per accettare qualsiasi input e richiedere inserimento obbligatorio rexp='all' e obbligo=1
	
	all=/((^(((\s)|(\S)){1,}))$){1,1}/g
	numeri=/((^(([0-9])+))$){1,1}/g
	mail = /((^((([a-z]|[A-Z]|\d)+(\.|\-|\_){0,1})+([a-z]|[A-Z]|\d)+(\@){1,1}(([a-z]|[A-Z]|\d)+(\.|\-){0,1})+([a-z]|[A-Z]|\d)+(\.){1,1}([a-z]){2,3}))$){1,1}/g
	piva = /((^(([0-9]){11,11}|(([a-z]|[A-Z]|[0-9]){16,16})))$){1,1}/g
	
	if(document.formy.lang.value=='_ENG')
	{
		msgall=textc+" required"
		msgnumeri=textc+" enter numbers only"
		var msgmail = textc +" No valid e-mail address"
		var msgpiva = textc +" no valid format"
	}
	else
	{
		msgall=textc+" è richiesto"
		msgnumeri=textc+" inserire solo numeri"
		var msgmail = textc +" non contiene un indirizzo e-mail valido"
		var msgpiva = textc +" non è in un formato valido"
	}
	
	ok_r=null
	ok_o=false
	ok_r=eval("document."+nome_form+"."+campo+".value.match("+rexp+")")

	if (obbligo!=1)
	{
		if (eval("document."+nome_form+"."+campo+".value==''"))
		ok_o=true
	}
	if ((ok_r!=null)||(ok_o==true))
	{
		return 1;
	}
	else
	{
		eval("alert(msg"+rexp+")")
		return 0;
	}
}

//**************************************************************************
function submitart()
{	
	ok=1
	var list=document.formy.elements

	for (var i=0;i<list.length;i++)
	{
		var elem=list[i]
		if (elem.name.substr(0,2)=="tg")
		{
			if (! CheckDBValue('', elem.name, 'numeri', 0, 'formy'))
			{
				ok=0
				break;
			}
		}	
	}
	
	if (ok==1)
		{document.formy.submit();}
}
//**************************************************************************
function visualdest()
{document.all.destinazione.style.display=''}

//**************************************************************************
function salvacarrello()
{
	var prosegui=0
	
	if(CheckDBValue('Utente', 'utente', 'all', 1, 'formy'))
	if(CheckDBValue('Indirizzo', 'indirizzo', 'all', 1, 'formy'))
	if(CheckDBValue('Citta', 'citta', 'all', 1, 'formy'))
	if(CheckDBValue('Cap', 'cap', 'all', 1, 'formy'))
	if(CheckDBValue('Provincia', 'prov', 'all', 1, 'formy'))
	if(CheckDBValue('Telefono', 'telefono', 'all', 1, 'formy'))
	if(CheckDBValue('E-mail', 'mail', 'mail', 1, 'formy'))
	if(CheckDBValue('Partita IVA', 'partitaiva', 'piva', 0, 'formy'))
	{	
		if(document.formy.dest_utente.value!='' || document.formy.dest_indirizzo.value!='' || document.formy.dest_citta.value!='' || document.formy.dest_cap.value!='' || document.formy.dest_prov.value!='' || document.formy.dest_telefono.value!='')
		{
			if(CheckDBValue('Destinatario utente', 'dest_utente', 'all', 1, 'formy'))
			if(CheckDBValue('Destinatario indirizzo', 'dest_indirizzo', 'all', 1, 'formy'))
			if(CheckDBValue('Destinatario citta', 'dest_citta', 'all', 1, 'formy'))
			if(CheckDBValue('Destinatario cap', 'dest_cap', 'all', 1, 'formy'))
			if(CheckDBValue('Destinatario provincia', 'dest_prov', 'all', 1, 'formy'))
			if(CheckDBValue('Destinatario telefono', 'dest_telefono', 'all', 0, 'formy'))
			if(CheckDBValue('Partita IVA', 'dest_partitaiva', 'piva', 0, 'formy'))
			{prosegui=1}
		}
		else
		{prosegui=1}
		
		if (prosegui==1)
		{
			var chkPagamento = false
			var arrPagamento = document.formy.pagamento
			
			if (arrPagamento.length >= 1)
			{	
				for (i=0; i<arrPagamento.length; i++)
				{
					if (arrPagamento[i].checked)
					{
						chkPagamento = true
						break;
					}
				}
			}
		
			if (chkPagamento)
			{	
				document.formy.submit( );
			}
			else
				{alert("Scegliere un tipo di pagamento")}
		}		
	}
}
//**************************************************************************
// funzione pre il calcolo del trasporto
//****************************************
function cambia(tipopagam)
{	
	if (document.getElementById('idutente').value!="")
	{
		var piva = document.formy.iva.value.replace(',','')
		var importo = document.formy.totaleordine.value.replace(',','')
		var trasporto = '0.00'
		
		if (tipopagam == 'standard')
		{
			if (parseFloat(importo) < 2000)
				{trasporto = '25.00'}
		}
		document.getElementById('trasporto').innerHTML = trasporto
		document.getElementById('tot').innerHTML = (parseFloat(importo)+ parseFloat(piva) + parseFloat(trasporto)).toFixed(2)
	}
}
//**************************************************************************
//**************************************************************************
function SubmitFormUtente()
{
	if(CheckDBValue('Ragione sociale', 'ragionesociale', 'all', 1, 'formy'))
	if(CheckDBValue('Partita iva', 'piva', 'piva', 1, 'formy'))
	if(CheckDBValue('Telefono', 'telefono', 'all', 1, 'formy'))
	if(CheckDBValue('Email', 'email', 'mail', 1, 'formy'))
	if(CheckDBValue('Login', 'login', 'all', 1, 'formy'))
	if(CheckDBValue('Password', 'password', 'all', 1, 'formy'))
	{
		document.formy.submit()
	}
}
