jQuery.validator.addMethod(
	"tipo_data",
	function(value, element) {
		var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
		if (element.disabled || (element.value=='') || (element.value.match(RegExPattern))) {
			return true;
		} else {
			return false;
		}
	},
	"La data inserita non è valida!"
);

jQuery.validator.addMethod(
	"tipo_cap",
	function(value, element) {
		var RegExPattern = /^\d{5}$/;
		if (element.disabled || (element.value=='') || element.value.match(RegExPattern)) {
			return true;
		} else {
			return false;
		}
	},
	"Il CAP inserito non è valido!"
);

jQuery.validator.addMethod(
	"tipo_numero",
	function(value, element) {
		var RegExPattern = /^\d*$/;
		if (element.disabled || (element.value=='') || (element.value.match(RegExPattern))) {
			return true;
		} else {
			return false;
		}
	},
	"Il valore inserito non è un numero valido!"
);

jQuery.validator.addMethod(
	"tipo_telefono",
	function(value, element) {
		var RegExPattern = /^\+{0,1}\d*$/;
		if (element.disabled || (element.value=='') || (element.value.replace(/\./g,"").replace(/ /,"").match(RegExPattern))) {
			return true;
		} else {
			return false;
		}
	},
	"Il valore inserito non è un numero di telefono valido!"
);

jQuery.validator.addMethod(
	"tipo_fax",
	function(value, element) {
		var RegExPattern = /^\+{0,1}\d*$/;
		if (element.disabled || (element.value=='') || (element.value.replace(/\./g,"").match(RegExPattern))) {
			return true;
		} else {
			return false;
		}
	},
	"Il valore inserito non è un numero di fax valido!"
);

jQuery.validator.addMethod(
	"tipo_codice_fiscale",
	function(value, element) {
		if ((element.value=='') || CheckCodiceFiscale(element.value)) {
			return true;
		} else {
			return false;
		}
	},
	"Il Codice Fiscale inserito non è valido!"
);

jQuery.validator.addMethod(
	"tipo_partita_iva",
	function(value, element) {
		if (element.disabled || (element.value=='') || ControllaPIVA(element.value)) {
			return true;
		} else {
			return false;
		}
	},
	"La Partita IVA inserita non è valida!"
);

jQuery.validator.addMethod(
	"tipo_codice_fiscale_azienda",
	function(value, element) {
		if (element.disabled || (element.value=='') || ControllaPIVA(element.value) || CheckCodiceFiscale(element.value)) {
			return true;
		} else {
			return false;
		}
	},
	"La Partita IVA inserita non è valida!"
);

function CheckCodiceFiscale(cfins){
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var 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;
}

function ControllaPIVA(pi)
{
    if( pi == '' )  return true;
    if( pi.length != 11 )
        return false; // La lunghezza della partita IVA non è corretta: la partita IVA dovrebbe essere lunga esattamente 11 caratteri.
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return false; // La partita IVA contiene un carattere non valido " pi.charAt(i) ". I caratteri validi sono le cifre.
    }
    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; // La partita IVA non è valida: il codice di controllo non corrisponde.
    return true;
}

