// limitare il numero di caratteri nella textarea
var maxchars=160;

function CheckLength()
  {
  with (document.prodotto)
    {
    chars=descrizione.value
    if (chars.length > maxchars)
      {
      descrizione.value=chars.substr(0,maxchars);
      descrizione.blur();
      }
    chr.value=maxchars-descrizione.value.length;
    }
  }


// funzioni semplici
function Trim(stringa)
	{
	reTrim=/\s+$|^\s+/g;
	return stringa.replace(reTrim,"");
	}

function controlla_mail()
{
	var re = /^[\w\-]+(\.?\-?\w+)*@[a-zA-Z0-9]+(\.?\-?[a-zA-Z0-9]+){2,}\.[a-zA-Z]{2,}$/;
	Codice=Trim(x.email.value.toUpperCase());
	if (re.test(Codice) == false)
	{
	alert("Indirizzo email non valido/email address not valid");
	x.email.focus();
	return (false);
	}
}

function confirmAction()
{
  action = confirm('Ok per eliminare il record, Annulla per uscire/ok to delete, Annulla to go out');
  if (action==true)
  	return true;
  if (action==false)
	return false;
}

function controlla_inserimento_codice()
{
if (x.codice.value == "")
	{
	alert("Inserisci il codice articolo/enter product code");
	x.codice.focus();
	return (false);
	}
}

function controlla_nome()
{
	if (x.nome.value == "")
	{
	alert("Inserisci il nome/enter your name");
	x.nome.focus();
	return (false);
	}
}

function controlla_cognome()
{
	if (x.cognome.value == "")
	{
	alert("Inserisci il cognome/enter your surname");
	x.cognome.focus();
	return (false);
	}
}

function controlla_ragsoc()
{
	if (x.ragsoc.value == "")
	{
	alert("Inserisci la ragione sociale/enter your firm name");
	x.ragsoc.focus();
	return (false);
	}
}

function controlla_specializzazione()
{
	if (x.specializzazione.value == "")
	{
	alert("Inserisci la specializzazione/enter your specialization ");
	x.specializzazione.focus();
	return (false);
	}
}

function controlla_telefono()
{
	if (x.telefono.value == "")
	{
	alert("Inserisci il numero di telefono/enter your phone number");
	x.telefono.focus();
	return (false);
	}
}

function inserimento_piva_codice()
{
	if ((x.piva.value == "") && (x.codicefiscale.value == ""))
	{
	alert("Inserisci la partita iva o il codice fiscale/ enter your partita iva or codice fiscale");
	x.piva.focus();
	return (false);
	}
}

function inserimento_username()
{
	if (x.user_login.value == "")
	{
		alert("Inserisci lo username/enter your username");
		x.user_login.focus();
		return (false);
	}
}

function inserimento_user_login()
{
	if (x.login.value == "")
	{
		alert("Inserisci lo username/enter your username");
		x.login.focus();
		return (false);
	}
}
function inserimento_user_password()
{
	if (x.password.value == "")
	{
		alert("Inserisci la password/enter your password");
		x.password.focus();
		return (false);
	}
}

function inserimento_password()
{
	if (x.user_password.value == "") {
		alert("Inserisci la password/enter your password");
		x.user_password.focus();
		return (false);
	}
	else {
		if (!x.user_password.value.match(/.{8}/)	) {
			alert("La password deve avere almeno 8 caratteri/password must be at least 8 alphanumerics");
			x.user_password.focus();
    		return (false);
		} else {
			if (x.user_password.value != x.user_confermapassword.value) {
				alert("Le 2 password devono coincidere/passwords must be the same");
				x.user_confermapassword.focus();
    			return (false);
			}
		}
	}	
}

//funzioni complesse
function controlla_anagrafica()
{
	x=document.form1;
	if (controlla_ragsoc()==false){
	return (false);
	}
}

function controlla_evento()
{
	x=document.form1;	
	if (controlla_nome()==false){
	return (false);
	}

}

function controlla_categoria()
{
	x=document.form1;
	if (controlla_nome()==false){
	return (false);
	}

}

function controlla_sottocategoria()
{
	x=document.form1;
	if (controlla_nome()==false){
	return (false);
	}

}

function controlla_form_registrazione()
{
	x=document.registrazione
	if (controlla_nome()==false){
		return (false);
	}
	if (controlla_cognome()==false){
		return (false);
	}
	if (controlla_ragsoc()==false){
		return (false);
	}
	if (inserimento_piva_codice()==false){
		return (false);
	}
	if (controlla_telefono()==false){
		return (false);
	}
	if (controlla_mail()==false){
		return (false);
	}
	if (inserimento_username()==false){
		return (false);
	}
	if (inserimento_password()==false){
		return (false);
	}
}

function controlla_registrazione_newsletter()
{
	x=document.registrazione_newsletter
	if (controlla_mail()==false){
		return (false);
	}
}
function controlla_inserimento_email()
{
	x=document.inserimento_email
	if (controlla_mail()==false){
		return (false);
	}
}

function controlla_inserimento_prodotto()
{
	x=document.prodotto;
	if (controlla_nome()==false){
	return (false);
	}
	controlla_inserimento_codice();
}

function controlla_inserimento_nome()
{
	x=document.prodotto;
	if (controlla_nome()==false){
	return (false);
	}
}

function controlla_form_contattaci()
{	
	x=document.marina
	if (controlla_nome()==false){
		return (false);
	}
	if (controlla_cognome()==false){
		return (false);
	}
	if (controlla_telefono()==false){
		return (false);
	}
	if (controlla_mail()==false){
		return (false);
	}
}

function controlla_login_ar()
{
	x=document.form_login
	if (inserimento_user_login()==false){
		return (false);
	}
	if (inserimento_user_password()==false){
		return (false);
	}
}
function controlla()
{
x=document.registrazione
at=x.email.value.indexOf("@")
	
if (x.nome.value == "")
	{
	alert("Inserisci il nome/enter your name");
	x.nome.focus();
	return (false);
	}
	if (x.cognome.value == "")
	{
	alert("Inserisci il cognome/enter your surname");
	x.cognome.focus();
	return (false);
	}
	if (x.ragsoc.value == "")
	{
	alert("Inserisci la ragione sociale/enter your firm name");
	x.ragsoc.focus();
	return (false);
	}
	if ((x.piva.value == "") && (x.codicefiscale.value == ""))
	{
	alert("Inserisci la partita iva o il codice fiscale/enter your partita iva or codice fiscale");
	x.piva.focus();
	return (false);
	}
	
	if (x.telefono.value == "")
	{
	alert("Inserisci il numero di telefono/enter your phone number");
	x.telefono.focus();
	return (false);
	}
	
	var re = /^[\w\-]+(\.?\-?\w+)*@[a-zA-Z0-9]+(\.?\-?[a-zA-Z0-9]+){2,}\.[a-zA-Z]{2,}$/;
	Codice=Trim(document.registrazione.email.value.toUpperCase());
	if (re.test(Codice) == false)
	{
		alert("Indirizzo email non valido/email address not valid.");
		document.registrazione.email.focus();
		return (false);
	}
	
	if (x.user_login.value == "")
	{
		alert("Inserisci lo username/enter your username");
		x.user_login.focus();
		return (false);
	}
	
	if (x.user_password.value == "") {
		alert("Inserisci la password/enter your password");
		x.user_password.focus();
		return (false);
	}
	else {
		if (!x.user_password.value.match(/.{8}/)	) {
			alert("La password deve avere almeno 8 caratteri/password must be at least 8 alphanumerics");
			x.user_password.focus();
    		return (false);
		} else {
			if (x.user_password.value != x.user_confermapassword.value) {
				alert("Le 2 password devono coincidere/passwords must be the same");
				x.user_confermapassword.focus();
    			return (false);
			}
		}
	}	
}
