$( function() { $("form").submit( function() { if( fCheckObbligatori() ) fSaveData(); return false; }); // Mostra domanda $('.showdomanda').click(function() { var campo = ( $(this).attr('id') ).replace( 'showdomanda_', '' ); $( '#box_' + campo ).addClass('hide'); $( '#boxlista_' + campo ).removeClass('hide'); }); // Mostra lista domande $('.listadomande').change(function() { var campo = ( $(this).attr('id') ).replace( 'listadomande_' , '' ); $( '#' + campo ).val( $(this).val() ); $( '#boxlista_' + campo ).addClass('hide'); $( '#lbl' + campo ).html(''); $( '#box_' + campo ).removeClass('hide'); $( '#' + campo ).focus(); }); // Controllo URL $('.url').change(function() { var campo = $(this).attr('id'); if ( $( '#' + campo ).val().substr(0,7) != 'http://' && $( '#' + campo ).val() != '' ) $( '#' + campo ).val( 'http://' + $( '#' + campo ).val() ); }); // Evidenzia privacy $('#showprivacy').click(function() { if( ! $('#privacy').prop('checked') ) { blnReturn = false; $('#lblPrivacy').html("Accettazione obbligatoria."); } else $('#lblPrivacy').html(''); }); $('#cmbNazione').change(function() { if ( $(this).val() != 'IT' ) $('.fg-cmbProvincia').addClass('hide'); else $('.fg-cmbProvincia').removeClass('hide'); }); $('#cmbSpNazione').change(function() { if ( $(this).val() != 'IT' ) $('.fg-cmbSpProvincia').addClass('hide'); else $('.fg-cmbSpProvincia').removeClass('hide'); }); $('#form-spedizione-but').click(function() { $('.form-spedizione-but-box').addClass('hide'); $('.form-spedizione-hide-box').removeClass('hide'); $('#form-spedizione-box').removeClass('hide'); }); $('#form-spedizione-hide').click(function() { var blnSpHide = false; var blnSpFull = ( $.trim( $('#txtSpIndirizzo').val() ) != '' ); if ( blnSpFull ) blnSpHide = confirm( "Vuoi cancellare i campi per la spedizione ed utilizzare solo l'indirizzo principale?" ); else blnSpHide = true; if ( blnSpHide ) { $('.form-spedizione-hide-box').addClass('hide'); $('.form-spedizione-but-box').removeClass('hide'); $('#form-spedizione-box').addClass('hide'); $('.sp-input').val(''); $('.sp-select option:first').attr('selected', true); } }); }); function fSaveData() { $('submit').focus(); $('#submitBut').addClass('hide'); $('#submitLoad').removeClass('hide'); var data; var strDestination = ''; var blnSaved = false; $.ajax({ type: "POST", async: false, dataType: "json", url: "/tpl/default/assets/ajax/sendSubscription.php", data: $("#frmOggetto").serialize(), success: function(data) { strDestination = data.destination; if ( data.status == 'ok' ) blnSaved = true; else alert("Registrazione dati non riuscita"); }, error: function(data) { alert("Procedura non completata."); } }); if ( blnSaved ) { location.href = strDestination; } else { $('#submitLoad').addClass('hide'); $('#submitBut').removeClass('hide'); } } // Controllo campi obbligatori function fCheckObbligatori() { var blnReturn = true; $('submit').focus(); $('#submitBut').addClass('hide'); $('#submitLoad').removeClass('hide'); $( ".form-group" ).removeClass( "has-error" ); $( ".lbl" ).html(""); $('.required').each(function() { if ( $.trim( $(this).val() ) == "" ) { blnReturn = false; $( "#lbl" + $(this).attr("name") ).html( fHTMLErrore( "Campo obbligatorio" ) ); $( ".fg-" + $(this).attr("name") ).addClass( "has-error" ); $( "#show_" + $(this).attr("name") + ", #showdomanda_" + $(this).attr("name") ).addClass('hide'); } else { $( "#lbl" + $(this).attr("name") ).html(""); $( "#show_" + $(this).attr("name") + ", #showdomanda_" + $(this).attr("name") ).removeClass('hide'); } }); // Controllo password if ( $('#txtPassword').val() != '' || $('#txtConfermaPassword').val() != '' ) { if ( $('#txtPassword').val() != $('#txtConfermaPassword').val() ) { blnReturn = false; $('#lbltxtConfermaPassword').html( fHTMLErrore( "La password non e' uguale." ) ); $( ".fg-txtConfermaPassword" ).addClass( "has-error" ); } else $('#lbltxtConfermaPassword').html(""); } // Controllo URL if ( $('.url').length ) { $('.url').each(function() { var campo = $(this).attr('id'); if ( $( '#' + campo ).val().substr(0,7) != 'http://' && $( '#' + campo ).val() != '' ) $( '#' + campo ).val( 'http://' + $( '#' + campo ).val() ); }); } // Controllo privacy if( ! $('#privacy').prop('checked') ) { blnReturn = false; $('#lblprivacy').html( fHTMLErrore( "Accettazione obbligatoria." ) ); $( ".fg-privacy" ).addClass( "has-error" ); } else $('#lblprivacy').html(""); // Controllo codice categoria if ( $('.categoria:checked').length ) { if ( $.trim( $('#txtCodiceCategoria').val() ) == "" ) { blnReturn = false; $( "#lbltxtCodiceCategoria" ).html( fHTMLErrore( "Campo obbligatorio" ) ); $( ".fg-txtCodiceCategoria" ).addClass( "has-error" ); } else { $( "#lbltxtCodiceCategoria" ).html(""); } } var strID_Contatto = ( $('#id_cliente').length ? $('#id_cliente').val() : '' ); // Controllo Codice fiscale if ( $('#txtCodiceFiscale').val() != '' ) { $('#lbltxtCodiceFiscale').html( '' ); var data; var intExists = 0; var blnSaved = false; $.ajax({ type: "POST", async: false, dataType: "json", url: "/tpl/default/assets/ajax/checkContatto.php", data: "action=checkCodiceFiscale&pstrCodiceFiscale=" + $("#txtCodiceFiscale").val() + "&pstrID_Contatto=" +strID_Contatto, success: function(data) { intExists = data.codice_fiscale_exists; if ( intExists == 1 ) { // ERRORE : valore NON unico blnReturn = false; $('#lbltxtCodiceFiscale').html( fHTMLErrore( "Il valore è già in archivio." ) ); $( ".fg-txtCodiceFiscale" ).addClass( "has-error" ); } else { // OK : valore unico $('#lbltxtCodiceFiscale').html(""); } if ( data.status == 'ok' ) blnSaved = true; else alert("Verifica non riuscita"); }, error: function(data) { alert("Procedura non completata."); } }); } // Controllo Email if ( $('#txtEmail').val() != '' ) { $('#lbltxtEmail').html( '' ); var data; var intCheck = 0; var intExists = 0; var intErrore = 0; var blnSaved = false; $.ajax({ type: "POST", async: false, dataType: "json", url: "/tpl/default/assets/ajax/checkContatto.php", data: "action=checkEmail&pstrEmail=" + $('#txtEmail').val() + "&pstrID_Contatto=" +strID_Contatto, success: function(data) { intCheck = data.email_check; intExists = data.email_exists; intErrore = data.email_errore; if ( intCheck == 0 ) { // Email non corretta if ( intErrore == 2 ) { // ERRORE : email NON esiste blnReturn = false; $('#lbltxtEmail').html( fHTMLErrore( "L'indirizzo non esiste" ) ); $( ".fg-txtEmail" ).addClass( "has-error" ); } else if ( intErrore == 1 ) { // ERRORE : email NON corretta blnReturn = false; $('#lbltxtEmail').html( fHTMLErrore( "L'indirizzo non è corretto." ) ); $( ".fg-txtEmail" ).addClass( "has-error" ); } } else { // Email corretta if ( intExists == 1 ) { // ERRORE : email non unica blnReturn = false; $('#lbltxtEmail').html( fHTMLErrore( "" ) ); $( ".fg-txtEmail" ).addClass( "has-error" ); } else { // OK : Email unica e corretta $('#lbltxtEmail').html(""); } } if ( data.status == 'ok' ) blnSaved = true; else alert("Verifica non riuscita"); }, error: function(data) { alert("Procedura non completata."); } }); } $('#submitLoad').addClass('hide'); $('#submitBut').removeClass('hide'); return blnReturn; } function fHTMLErrore( pstr ) { return '' + pstr + ''; }