/*
TABS
Prerequisiti:
- Ogni tabs deve essere una lista (
o )
- Ogni "titolo" dei tabs deve essere dentro un list item (- ) e ad un anchor () con il suo attributo href
- Ogni tab panel da mostrare deve avere un id corrispondente all'href del suo titolo.
Options:
- position: Quale tab deve essere mostrato di default se i valori non sono ['first','last'] prende il primo
Se esiste un li con classe ui_tab_active ha la precedenza rispetto a "position"
- selected: classe che indica il selected.
*/
(function($) {
$.fn.tabs = function(options) {
var set = $.extend ({selected:'selected'}, options);
if ($(this).find('.ui_tab_active').length == 0)
$(this).find('>li')['first']().addClass ('ui_tab_active');
return this.each(function() {
//Funziona solo su liste (ul/ol)
var tabs = $(this).find('> li > a');
//per ogni link
tabs.click(function(e) {
//resetto tutto
tabs.each(function() {
$(this).closest('li').removeClass(set.selected);
$($(this).attr('href')).hide();
});
//Seleziono quello che mi interessa
$(this).closest('li').addClass(set.selected);
$($(this).attr('href')).show();
e.preventDefault();
return false;
}).parents('li.ui_tab_active').find('a').click();
});
};
})(jQuery);
/*
TOGGLE DETAILS
Prerequisiti:
l'html deve avere una forma del tipo
MUSEI
Ovvero al titolo che voglio usare per aprire e chiudere devono seguire subito i suoi dettagli.
Il blocco immagine puo' esserci o non esserci. Se c'e' viene usato quello esistente. Se non c'e' viene creato.
Options:
- start: Indica se inizialmente devo visualizzare o mostrare i details. valori ['show','hide'] con default = hide
- minus: Immagine per il -
- plus: Immagine per il +
*/
(function($) {
$.fn.details = function(options) {
return this.each(function() {
var settings = $.extend ({start:'hide',
minus:hostServerImage+'wms_images/minus.png',
plus:hostServerImage+'wms_images/plus.png'}, options);
var toggleBtn = $(this);
var img = toggleBtn.find('img.toggle');
if (img.length == 0) {
img = $('');
toggleBtn.append(img);
}
var details = toggleBtn.siblings('.details');
if (settings.start == 'show'){
img.attr("src",settings.minus);
details.show();
}
else{
img.attr("src",settings.plus);
details.hide();
}
toggleBtn.css('cursor','pointer');
toggleBtn.click(function() {
if (details.is(':visible')) img.attr("src",settings.plus);
else img.attr("src",settings.minus);
details.toggle();
return false;
});
});
};
})(jQuery);
/** FORM VALIDATOR **/
NOTpattern_strNum = /[\x00-\x1F\!\"\#\$\%\&\(\)\*\\\:<=>?\[\]^`{\|}~\r\n\f\@]/;
NOTpattern_str = /[\d\x00-\x1F\!\"\#\$\%\&\(\)\*\\\:<=>?\[\]^`{\|}~\r\n\f\@]/;
jQuery.validator.setDefaults({
errorClass:'invalid', errorElement:"span",
errorPlacement: function(label, element) {
if ($(element).closest('tr').find('.error').length)
$(element).closest('tr').find('.error').html(label);
else if ($(element).closest('div.cell').find('.error').length)
$(element).closest('div.cell').find('.error').html(label);
else if ($(element).closest('div.row').find('.error').length)
$(element).closest('div.row').find('.error').html(label);
else
label.insertAfter(element);
}
});
jQuery.validator.addMethod("exactlength", function(v, e, p) {
return this.optional(e) || v.length == p;
}, "Please enter exactly {0} characters.");
//Sostituita da "pattern"
jQuery.validator.addMethod("regex", function(v, e, regexp) {
return this.optional(e) || regexp.test(v);
}, "Please check your input.");
jQuery.validator.addMethod("NOTpattern", function(v, e, regexp) {
return this.optional(e) || !regexp.test(v);
}, "Please check your input.");
jQuery.validator.addMethod("phone", function(v, e) {
return this.optional(e) || /^[\d \-\/\+\.]+$/.test(v);
}, "Please specify a valid phone number.");
jQuery.validator.addMethod("codiceFiscale", function(value, element) {
var cf = value.toUpperCase();
//Controllo Formato
// i numeri posso essere sostituiti da lettere
// SPIEGAZIONE: una procedura già prevista e consiste nel sostituire uno o più dei sette numeri del codice,
// a partire da quello più a destra, con delle lettere corrispondenti. Per l’esattezza:
// 0 = L | 1 = M | 2 = N | 3 = P | 4 = Q
// 5 = R | 6 = S | 7 = T | 8 = U | 9 = V
// NOI PER ORA GESTIAMO SOLO SUGLI ULTIMI 3 NUMERI
var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z][0-9LMNPQRSTUV]{3}[A-Z]$/;
if (!cfReg.test(cf)) return false;
//Controllo Codice di Controllo
var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
var s = 0;
for( var 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;
//Controllo Data
var charMese = "ABCDEHLMPRST";
var anno = eval(cf.substr (6,2));
var today = new Date ();
if (anno > (today.getYear()-100)) anno +=1900;
else anno +=2000;
var mese = charMese.indexOf(cf.substr (8,1));
var giorno = cf.substr (9,2);
if (giorno > 40 ) giorno -= 40;
var bday = new Date(anno, mese, giorno, 12, 0, 0);
if (anno != bday.getFullYear() || mese != (bday.getMonth()) || giorno != bday.getDate()) return false;
return true;
}, "Please specify a valid codice fiscale");
jQuery.validator.addMethod("partita_iva", function(value, element) {
var pattern = /^[\d]{11}$/;
if (!pattern.test (value)) return false;
s = 0;
for( var i = 0; i <= 9; i += 2 )
s += value.charCodeAt(i) - '0'.charCodeAt(0);
for( i = 1; i <= 9; i += 2 ){
c = 2*( value.charCodeAt(i) - '0'.charCodeAt(0) );
if( c > 9 ) c = c - 9;
s += c;
}
if( ( 10 - s%10 )%10 != value.charCodeAt(10) - '0'.charCodeAt(0) ) return false;
return true;
}, "Please specify a valid VAT number");
/* TIMER COUNTDOWN
Utilizzo
H M S millesimi
var ts = (new Date()).getTime() + 60*60*60*1000;
$('#countdown').countdown({ timeend: te, timestart: ts, timeDivision: ['M','S']});
fa vedere il countdown solo con minuti : secondi
*/
(function($){
// Secondi per ogni divisione temporale
var days = 24*60*60,
hours = 60*60,
minutes = 60;
// Creo il plugin
$.fn.countdown = function(prop){
var _this = this;
var options = $.extend({
timeend: 0,
timestart: new Date(),
timeDivision: ['D','H','M','S'],
redCD: 10 * 60,
msgGood: '',
msgBad: ''
},prop);
var left, timeleft, d, h, m, s;
// Inizializzazione
init(this, options);
// Time left
left = timeleft = Math.floor((options.timeend - options.timestart) / 1000);
(function tick(){
// riaggiorno il timer
left = timeleft;
if(timeleft < 0) { left = timeleft = 0; }
// Numero di giorni rimasti
if ($.inArray('D', options.timeDivision) != -1 ) {
d = Math.floor(left / days);
update('D',d);
left -= d*days;
}
// Numero di ore rimaste
if ($.inArray('H', options.timeDivision) != -1 ) {
h = Math.floor(left / hours);
update('H',h);
left -= h*hours;
}
// Numero di minuti rimasti
if ($.inArray('M', options.timeDivision) != -1 ) {
m = Math.floor(left / minutes);
update('M',m);
left -= m*minutes;
}
// Numero di secondi rimasti
if ($.inArray('M', options.timeDivision) != -1 ) {
s = left;
update('S',s);
}
if (timeleft < options.redCD ) {
_this.addClass('redCountdown');
}
// Schedulo la prossima chiamata della funzione tra 1 secondo
if (timeleft == 0) {
$(options.msgGood).hide();
$(options.msgBad).show();
}
else {
// decremento il contatore
timeleft -=1;
$(options.msgGood).show();
$(options.msgBad).hide();
setTimeout(tick, 1000);
}
})();
function update (element, number) {
var digit = _this.find('.count'+element);
//E' lo stesso numero
if (digit.data == number) return false;
//Numero inferiore a 10 aggiunto lo 0 per avere sempre due cifre
if (number < 10) number = '0'+number;
digit.html(number);
}
return this;
};
function init(elem, options){
elem.addClass($(elem).attr('id')+'Holder');
// pulisco l'elemento per non crearlo piu' volte nell'aggiornamento dell'header
elem.empty();
// Creo l'html dentro il container dato
$.each(options.timeDivision, function(i){
$('').appendTo(elem);
if(this != options.timeDivision[options.timeDivision.length-1] ){
elem.append(':');
}
});
}
})(jQuery);