$(function() { $("#frmVendita").submit( function() { if( fCheckObbligatori() ) { fSaveData(); return true; } else return false; }); $(".currency").each(function() { $(this).val( fCurrency( $(this).val() ) ); }); $('#decRichiestaEconomica').change(function() { $(".currency").each(function() { $(this).val( fCurrency( $(this).val() ) ); }); }); }); function fSaveData() { $('submit').focus(); $('#submitBut').addClass('hide'); $('#submitLoad').removeClass('hide'); } // Controllo campi obbligatori function fCheckObbligatori() { var blnReturn = true; $( ".form-group" ).removeClass( "has-error" ); $( ".lbl" ).html(""); $('.obbligatori-alert').addClass('hide'); $('.required').each(function() { if ( $.trim( $(this).val() ) == "" ) { blnReturn = false; $( "#lbl" + $(this).attr("name") ).html( fHTMLErrore( "" ) ); $( ".fg-" + $(this).attr("name") ).addClass( "has-error" ); } }); // Controllo richiesta var richiesta_economica = ( $('#decRichiestaEconomica').val() ).replace( '.', '' ); richiesta_economica = ( richiesta_economica ).replace( ',', '.' ); richiesta_economica = parseFloat( richiesta_economica ); if ( ! ( richiesta_economica > 0 ) ) { blnReturn = false; $( "#lbldecRichiestaEconomica" ).html( fHTMLErrore( "Campo obbligatorio" ) ); $( ".fg-decRichiestaEconomica" ).addClass( "has-error" ); } // Controllo privacy if ( ! $('#privacy').prop('checked') ) { blnReturn = false; $('#lblprivacy').html( fHTMLErrore( "Accettazione obbligatoria." ) ); $( ".fg-privacy" ).addClass( "has-error" ); } // Controllo Email if ( $('#txtEmail').val() != '' ) { $('#lbltxtEmail').html( '' ); var intErrore = ''; $.ajax({ type: "POST", async: false, dataType: "json", url: "/tpl/default/assets/ajax/checkEmail.php", data: "action=checkEmail&pstrEmail=" + $("#txtEmail").val(), success: function(data) { intErrore = data.errore; if ( intErrore == 1 ) { // ERRORE : email NON corretta blnReturn = false; $('#lbltxtEmail').html( fHTMLErrore( "L'indirizzo non รจ corretto." ) ); $( ".fg-txtEmail" ).addClass( "has-error" ); } else if ( intErrore == 2 ) { // ERRORE : email NON esiste blnReturn = false; $('#lbltxtEmail').html( fHTMLErrore( "L'indirizzo non esiste" ) ); $( ".fg-txtEmail" ).addClass( "has-error" ); } else $('#lbltxtEmail').html( "" ); if ( data.status == 'errore' ) blnSaved = false; else if ( data.status == 'ok' ) blnSaved = true; else alert("Verifica non riuscita"); }, error: function(data) { alert("Procedura non completata."); } }); } if ( ! blnReturn ) { $('#submitLoad').addClass('hide'); $('#submitBut').removeClass('hide'); $('.obbligatori-alert').removeClass('hide'); } return blnReturn; } function fHTMLErrore( pstr ) { return '' + pstr + ''; } function Left(str, n) { if (n <= 0) return ""; else if (n > String(str).length) return str; else return String(str).substring(0,n); } function Right(str, n) { if (n <= 0) return ""; else if (n > String(str).length) return str; else { var iLen = String(str).length; return String(str).substring(iLen, iLen - n); } } // formatta valuta function fCurrency( pstr ) { var oCurrency = ( String( pstr ) ).replace(/[a-zA-Z-_*]/g, ''); if ( oCurrency == "" ) oCurrency = "0,00"; oCurrency = oCurrency.replace(".", ""); oCurrency = oCurrency.replace(",", "."); oCurrency = parseFloat( oCurrency ).toFixed(2); if ( oCurrency == "NaN" ) oCurrency = "0.00"; var parteIntera = new String( Math.floor( oCurrency ) ); var parteDecimale = Right( oCurrency, 2 ); var strCurrency = parteIntera; var strResult = ""; var str = ""; for ( i = 0 ; i <= parteIntera.length; i = i + 3 ) { str = Right( strCurrency, 3 ); if ( i > 0 && str.length > 0 ) strResult = "." + strResult; strResult = str + strResult; strCurrency = strCurrency.replace( str, "" ); } strResult = strResult + "," + parteDecimale; return strResult; }