/** Controllo textfield */
	function validaField(id_elemento) {
		var elemento;
		value = prendiElementoDaId(id_elemento).value;
		value = trim(value);
		if (value == null || value == "") {
			return false;
		} else {
			return true;
		}
	}

/**
* @author Emanuel 
* 
* js di utility per la validazione dei campi 
*
*/


/** VARIABILI */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 1;



/** Funzione trim 
	Elimina gli spazi all'interno di una stringa
*/
function trim(stringa) {
	if (stringa != null) {
		while (stringa.indexOf(" ") != -1) {
			stringa = stringa.replace(" ", "");
		}
	}
	return stringa;
}


/** Controllo textfield */
function validaField(id_elemento) {
	var elemento;
	value = prendiElementoDaId(id_elemento).value;
	value = trim(value);
	if (value == null || value == "") {
		return false;
	} else {
		return true;
	}
}


/** Controllo radiobutton */
function validaRadio(id_elemento) {
	if (!prendiElementoDaId(id_elemento).checked == true) {
		return false;
	} else {
		return true;
	}
}


/** Controllo indirizzo Email */
function validaEmail(mail) {
	var emailStr = prendiElementoDaId(mail).value;
	if (emailStr == null || trim(emailStr) == "") {
		return false;
	}
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "[^\\s" + specialChars + "]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + "+";
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		/* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
//		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) {
    // user is not valid
//		alert("The username doesn't seem to be valid.");
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
    // this is an IP address
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
//				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var domainArray = domain.match(domainPat);
	if (domainArray == null) {
//		alert("The domain name doesn't seem to be valid.");
		return false;
	}
	var atomPat = new RegExp(atom, "g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3) {
   // the address must end in a two letter or three letter word.
//		alert("The address must end in a three-letter domain, or two letter country.");
		return false;
	}

// Make sure there's a host name preceding the domain.
	if (len < 2) {
		return false;
	}
	return true;
}


/** Controllo numeri */
function validaNumber(id_elemento) {
	var s = prendiElementoDaId(id_elemento).value;
	var i;
	for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) {
			return false;
		}
	}
	return true;
}

/* Controllo Formato valuta*/
function validaValuta(id_elemento) {
	var s = prendiElementoDaId(id_elemento).value;
	var i;
	for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9")) && c != ".") {
			return false;
		}
	}
	return true;
}

/** Controllo numero di telefono internazionale*/
function stripCharsInBag(s, bag) {
	var i;
	var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) {
			returnString += c;
		}
	}
	return returnString;
}
function validaNumberForPhoneNumber(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) {
			return false;
		}
	}
	return true;
}
function validaPhoneNumber(id_elemento) {
	var strPhone = prendiElementoDaId(id_elemento).value;
	s = stripCharsInBag(strPhone, validWorldPhoneChars);
	return (validaNumberForPhoneNumber(s) && s.length >= minDigitsInIPhoneNumber);
}


/** Controllo partita iva*/
function validaPIVA(id_elemento) {
	var pi = prendiElementoDaId(id_elemento).value;
	if (pi == "" || pi.length != 11) {
		return false;
	}
	validi = "0123456789";
	for (i = 0; i < 11; i++) {
		if (validi.indexOf(pi.charAt(i)) == -1) {
			return false;
		}
	}
	s = 0;
	for (i = 0; i <= 9; i += 2) {
		s += pi.charCodeAt(i) - "0".charCodeAt(0);
	}
	for (i = 1; i <= 9; i += 2) {
		c = 2 * (pi.charCodeAt(i) - "0".charCodeAt(0));
		if (c > 9) {
			c = c - 9;
		}
		s += c;
	}
	if ((10 - s % 10) % 10 != pi.charCodeAt(10) - "0".charCodeAt(0)) {
		return false;
	}
	return true;
}


/** Controllo codice fiscale */
function validaCF(id_elemento) {
	var validi, i, s, set1, set2, setpari, setdisp;
	var cf = prendiElementoDaId(id_elemento).value;
	
	if (cf == "") {
		return false;
	}
	
	cf = cf.toUpperCase();
	if (cf.length != 16) {
		return false;
	}
	
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for (i = 0; i < 16; i++) {
		if (validi.indexOf(cf.charAt(i)) == -1) {
			return false;
		}
	}
	
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for (i = 1; i <= 13; i += 2) {
		s += setpari.indexOf(set2.charAt(set1.indexOf(cf.charAt(i))));
	}
	for (i = 0; i <= 14; i += 2) {
		s += setdisp.indexOf(set2.charAt(set1.indexOf(cf.charAt(i))));
	}
	if (s % 26 != cf.charCodeAt(15) - "A".charCodeAt(0)) {
		return false;
	}
	return true;
}


/** Funzioni che producono un alert prima dell'eliminazione e poi eseguono Link.js*/
function DeleteAndLink(question,classPHP,valore){
	var answer = confirm(question);
	if (answer == true) {
		Link(classPHP,valore);
	}
}
		
function DeleteAndLinkDato(question,classPHP,valore,dato){
	var answer = confirm(question);
	if (answer == true) {
		LinkDato(classPHP,valore,dato);
	}
}
		
function DeleteAndLinkDatoParziale(question,classPHP,divdinamico,valore,dato){
var answer = confirm(question);
	if (answer == true) {
		LinkDatoParziale(classPHP,divdinamico,valore,dato);
	}
}
		

function PromptAndLinkDato(question,classPHP,valore,dato){
	var answer = prompt(question);
	if (answer != null) {
		dato += '&note='+answer;
		LinkDato(classPHP,valore,dato);
	}
}
		
function PromptAndLinkDatoParziale(question,classPHP,divdinamico,valore,dato){
	var answer = prompt(question);
	if (answer != null) {
		dato += '&note='+answer;
		LinkDatoParziale(classPHP,divdinamico,valore,dato);
	}
}
		<!--FUNZIONE PER VALIDARE I CAMPI DELLA FORM-->
		function validate(azienda){
			var valido = true;
			if(azienda == false){
				clearDivPazError();
				if(!validaField('nome')){
					prendiElementoDaId('error_nome').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaField('cognome')){
					prendiElementoDaId('error_cognome').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaRadio('sesso_M') && !validaRadio('sesso_F')){
					prendiElementoDaId('error_sesso').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaField('telefono')){
					prendiElementoDaId('error_telefono').innerHTML = 'Campo obbligatorio';
					valido = false;
				}else if(!validaPhoneNumber('telefono')){
					prendiElementoDaId('error_telefono').innerHTML = 'Riempire il campo con dei numeri';
					valido = false;
				}
				if (prendiElementoDaId('cellulare').value != null && trim(prendiElementoDaId('cellulare').value) != "") {
					if(!validaPhoneNumber('cellulare')){
						prendiElementoDaId('error_cellulare').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('fax').value != null && trim(prendiElementoDaId('fax').value) != "") {
					if(!validaPhoneNumber('fax')){
						prendiElementoDaId('error_fax').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('email').value != null && trim(prendiElementoDaId('email').value) != "") {
					if(!validaEmail('email')){
						prendiElementoDaId('error_email').innerHTML = 'Formato Email non valido Es.: info@foxweb.it';
						valido = false;
					}
				}
			}
			
			// valido la nuova azienda da inserire
			if (azienda == true || prendiElementoDaId('azienda').disabled == true){
				clearDivAZError();
				if(!validaField('a_ragione_sociale')){
					prendiElementoDaId('error_a_ragione_sociale').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if (prendiElementoDaId('a_telefono').value != null && trim(prendiElementoDaId('a_telefono').value) != "") {
					if(!validaPhoneNumber('a_telefono')){
						prendiElementoDaId('error_a_telefono').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_fax').value != null && trim(prendiElementoDaId('a_fax').value) != "") {
					if(!validaPhoneNumber('a_fax')){
						prendiElementoDaId('error_a_fax').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_email').value != null && trim(prendiElementoDaId('a_email').value) != "") {
					if(!validaEmail('a_email')){
						prendiElementoDaId('error_a_email').innerHTML = 'Formato Email non valido Es.: info@foxweb.it';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_cap').value != null && trim(prendiElementoDaId('a_cap').value) != "") {
					if(!validaPhoneNumber('a_cap')){
						prendiElementoDaId('error_a_cap').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if(!validaField('a_piva')){
					prendiElementoDaId('error_a_piva').innerHTML = 'Campo obbligatorio';
					valido = false;
				}else if(!validaPIVA('a_piva')){
					prendiElementoDaId('error_a_piva').innerHTML = 'Partita iva non valida';
					valido = false;
				}
			}
			
			if(!valido){
				return false;	
			}			 
		}
				function clearDivPazError(){
			prendiElementoDaId('error_nome').innerHTML = '';
			prendiElementoDaId('error_cognome').innerHTML = '';
			prendiElementoDaId('error_sesso').innerHTML = '';
			prendiElementoDaId('error_n_comune').innerHTML = '';
			prendiElementoDaId('error_n_provincia').innerHTML = '';
			prendiElementoDaId('error_via').innerHTML = '';
			prendiElementoDaId('error_comune').innerHTML = '';
			prendiElementoDaId('error_cap').innerHTML = '';
			prendiElementoDaId('error_provincia').innerHTML = '';
			prendiElementoDaId('error_telefono').innerHTML = '';
			prendiElementoDaId('error_fax').innerHTML = '';
			prendiElementoDaId('error_cellulare').innerHTML = '';
			prendiElementoDaId('error_codf').innerHTML = '';
			prendiElementoDaId('error_email').innerHTML = '';
		}
		function clearDivAZError(){	
			prendiElementoDaId('error_a_ragione_sociale').innerHTML = '';
			prendiElementoDaId('error_a_fax').innerHTML = '';
			prendiElementoDaId('error_a_telefono').innerHTML = '';
			prendiElementoDaId('error_a_piva').innerHTML = '';
			prendiElementoDaId('error_a_email').innerHTML = '';
			prendiElementoDaId('error_a_cap').innerHTML = '';
		}
		<!--FUNZIONE PER VALIDARE I CAMPI DELLA FORM-->
		function validate(azienda){
			var valido = true;
			if(azienda == false){
				clearDivPazError();
				if(!validaField('nome')){
					prendiElementoDaId('error_nome').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaField('cognome')){
					prendiElementoDaId('error_cognome').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaRadio('sesso_M') && !validaRadio('sesso_F')){
					prendiElementoDaId('error_sesso').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if(!validaField('telefono')){
					prendiElementoDaId('error_telefono').innerHTML = 'Campo obbligatorio';
					valido = false;
				}else if(!validaPhoneNumber('telefono')){
					prendiElementoDaId('error_telefono').innerHTML = 'Riempire il campo con dei numeri';
					valido = false;
				}
				if (prendiElementoDaId('cellulare').value != null && trim(prendiElementoDaId('cellulare').value) != "") {
					if(!validaPhoneNumber('cellulare')){
						prendiElementoDaId('error_cellulare').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('fax').value != null && trim(prendiElementoDaId('fax').value) != "") {
					if(!validaPhoneNumber('fax')){
						prendiElementoDaId('error_fax').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('email').value != null && trim(prendiElementoDaId('email').value) != "") {
					if(!validaEmail('email')){
						prendiElementoDaId('error_email').innerHTML = 'Formato Email non valido Es.: info@foxweb.it';
						valido = false;
					}
				}
			}
			
			// valido la nuova azienda da inserire
			
				clearDivAZError();
				if(!validaField('a_ragione_sociale')){
					prendiElementoDaId('error_a_ragione_sociale').innerHTML = 'Campo obbligatorio';
					valido = false;
				}
				if (prendiElementoDaId('a_telefono').value != null && trim(prendiElementoDaId('a_telefono').value) != "") {
					if(!validaPhoneNumber('a_telefono')){
						prendiElementoDaId('error_a_telefono').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_fax').value != null && trim(prendiElementoDaId('a_fax').value) != "") {
					if(!validaPhoneNumber('a_fax')){
						prendiElementoDaId('error_a_fax').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_email').value != null && trim(prendiElementoDaId('a_email').value) != "") {
					if(!validaEmail('a_email')){
						prendiElementoDaId('error_a_email').innerHTML = 'Formato Email non valido Es.: info@foxweb.it';
						valido = false;
					}
				}
				if (prendiElementoDaId('a_cap').value != null && trim(prendiElementoDaId('a_cap').value) != "") {
					if(!validaPhoneNumber('a_cap')){
						prendiElementoDaId('error_a_cap').innerHTML = 'Riempire il campo con dei numeri';
						valido = false;
					}
				}
				if(!validaField('a_piva')){
					prendiElementoDaId('error_a_piva').innerHTML = 'Campo obbligatorio';
					valido = false;
				}else if(!validaPIVA('a_piva')){
					prendiElementoDaId('error_a_piva').innerHTML = 'Partita iva non valida';
					valido = false;
				}
			
			
			if(!valido){
				return false;	
			}			 
		}
		
