
jQuery.validator.addMethod("currencyeuro", function(value, element) {
	return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
},"Inserisci un valore in euro");

jQuery.validator.addMethod("fiscalcode", function(value, element) {
	return this.optional(element) || isCodiceFiscale(value);
}, "Inserisci un codice fiscale valido");

jQuery.validator.addMethod("vatid", function(value, element) {
	return this.optional(element) || isPartitaIva(value);
}, "Inserisci una partita iva valida");

jQuery.validator.addMethod("vatidorfiscalcode", function(value, element) {
	return this.optional(element) || isCodiceFiscale(value) || isPartitaIva(value);
}, "Inserisci un codice fiscale valido");

jQuery.validator.addMethod("selectnotfirst",function(value, element) {
	return this.optional(element) || element.selectedIndex>0 ;	
}, "Scegli un'opzione") ;

jQuery.validator.addMethod("checkboxchecked",function(value, element) {
	return this.optional(element) || element.checked ;	
}, "Il checkbox deve essere selezionato") ;

function isPartitaIva(pi) {
	pi = pi.toUpperCase();
	if (pi.substr(0,2)=='SM' && pi.length>=4) return true ;
	if (pi.substr(0,2)=='CH' && pi.length>=4) return true ;

	if( 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 ;
}

function isCodiceFiscale(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;
}
