var blnSubmit = false;
$( function()
{
// Cattura il submit
$('#formRecord').bind('submit',function(event)
{
event.preventDefault();
if ( ! blnSubmit ) fCheckObbligatori( 1, 0 );
});
// Cattura i change
$("input,select,textarea").change(function()
{
if ( ! blnSubmit ) fCheckObbligatori( 0, 1 );
});
// Cattura i blur
$("input.email").blur(function()
{
if ( ! blnSubmit ) fCheckObbligatori( 0, 1 );
});
// Mostra domanda
$('.showdomanda').click(function()
{
var campo = $(this).attr('id').substr(12);
$('#box_'+campo).hide();
$('#boxlista_'+campo).show();
});
// Mostra lista domande
$('.listadomande').change(function()
{
var campo = $(this).attr('id').substr(13);
$('#'+campo).val( $(this).val() );
$('#boxlista_'+campo).hide();
$('#lbl'+campo).removeClass('errore').addClass('conferma').html('');
$('#box_'+campo).show();
});
// Evidenzia privacy
$('#showprivacy').click(function()
{
if ( $('#box_privacy').hasClass('fprivacy') )
$('#box_privacy').hide().removeClass('fprivacy').addClass('fprivacyon').fadeIn('slow');
else $('#box_privacy').hide().removeClass('fprivacyon').addClass('fprivacy').fadeIn('slow');
if( ! $('#privacy').attr('checked') )
{
blnReturn = false;
$('#lblPrivacy').html("Accettazione obbligatoria.");
} else $('#lblPrivacy').html('');
});
$('#nazione').change(function()
{
if ( $(this).val() == 'IT' )
$('.provincia_label_obb').show();
else $('.provincia_label_obb').hide();
});
// Controllo campi
fCheckObbligatori( 0, 1 );
$("#nome").focus();
});
//--- Controllo campi obbligatori
function fCheckObbligatori( pintSubmit, pintHideAlert )
{
var blnReturn = true;
var intHideAlert = ( pintHideAlert > 0 ? 1 : 0 );
var strAlert = "Verifica i seguenti campi:\n";
// Campi obbligatori
$(".obbligatorio").each(function()
{
if ( $(this).val() == "" )
{
$( "#lbl" + $(this).attr("name") ).removeClass("conferma").addClass("errore").html("Campo obbligatorio");
blnReturn = false;
strAlert += "- " + $( "#lbl" + $(this).attr("name") ).attr("title") + " \n";
}
else $( "#lbl" + $(this).attr("name") ).html( "" );
});
// controlla codice_fiscale e partita_iva
if ( $('#ragione_sociale').val() != '' )
{
$('#codice_fiscale_obb').hide();
$('#lblcodice_fiscale').removeClass("errore").removeClass("conferma").html('');
$('#partita_iva_obb').show();
if ( $('#partita_iva').val() == '' )
{
blnReturn = false;
strAlert += '- ' + $( "#lblpartita_iva" ).attr("title") + ' \n';
$('#lblpartita_iva').removeClass("conferma").addClass("errore").html("Campo obbligatorio");
}
else
{
$('#lblpartita_iva').removeClass("errore-box").removeClass("conferma-box").html('');
}
}
else
{
$('#partita_iva_obb').hide();
$('#lblpartita_iva').removeClass("errore").removeClass("conferma").html('');
$('#codice_fiscale_obb').show();
if ( $('#codice_fiscale').val() == '' )
{
blnReturn = false;
strAlert += '- ' + $( "#lblcodice_fiscale" ).attr("title") + ' \n';
$('#lblcodice_fiscale').removeClass("conferma").addClass("errore").html("Campo obbligatorio");
}
else
{
$('#lblcodice_fiscale').removeClass("errore").removeClass("conferma").html('');
}
}
// Nazione obbligatoria
if ( $.trim( $("#nazione").val() ) == "" )
{
$( "#lblnazione" ).removeClass("conferma").addClass("errore").html("Campo obbligatorio");
blnReturn = false;
strAlert += "- Nazione \n";
}
else $( "#lblnazione" ).html( "" );
// Nazione spedizione obbligatoria
if ( $.trim( $("#sp_indirizzo").val() ) != "" && $.trim( $("#sp_nazione").val() ) == "" )
{
$( "#lblsp_nazione" ).removeClass("conferma").addClass("errore").html("Campo obbligatorio");
blnReturn = false;
strAlert += "- Nazione Spedizione \n";
}
else $( "#lblsp_nazione" ).html( "" );
// Provincia obbligatoria
if ( $("#nazione").val() == "IT" )
{
$("#provincia_tr").show();
}
else
{
$("#provincia_tr").hide();
}
if ( $("#nazione").val() == "IT" && $.trim( $("#provincia").val() ) == "" )
{
$( "#lblprovincia" ).removeClass("conferma").addClass("errore").html("Campo obbligatorio");
blnReturn = false;
strAlert += "- Provincia \n";
}
else $( "#lblprovincia" ).html( "" );
// Provincia spedizione obbligatoria
if ( $.trim( $("#sp_indirizzo").val() ) != "" && $("#sp_nazione").val() == "IT" && $.trim( $("#sp_provincia").val() ) == "" )
{
$( "#lblsp_provincia" ).removeClass("conferma").addClass("errore").html("Campo obbligatorio");
blnReturn = false;
strAlert += "- Provincia Spedizione \n";
}
else $( "#lblsp_provincia" ).html( "" );
if ( $("#sp_nazione").val() == "IT" )
{
$("#sp_provincia_tr").show();
}
else
{
$("#sp_provincia_tr").hide();
}
// Controllo password
if ( $('.showpassword').length )
{
$('.showpassword').each(function()
{
var campo = $(this).attr('id').substr(5);
if ( campo.substring(0,8) != 'conferma' )
{
if ( $('#'+campo).val() != $('#conferma_'+campo).val() )
{
$('#lblconferma_'+campo).removeClass("conferma").addClass("errore").html("La password non è uguale.");
blnReturn = false;
strAlert += "- " + $('#lbl'+campo).attr("title") + " \n";
}
}
});
}
// 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').attr('checked') )
{
$('#lblprivacy').html("Accettazione obbligatoria.");
blnReturn = false;
strAlert += "- Privacy - Accettazione obbligatoria. \n";
}
else $('#lblprivacy').html('');
// AJAX
var strAjaxData = '&id_contatto=' + $('#id_cliente').val();
// Controllo codice_fiscale
if ( $('#codice_fiscale').val() != '' )
{
strAjaxData += '&codice_fiscale=' + $('#codice_fiscale').val();
$('#lblcodice_fiscale').removeClass("conferma").removeClass("errore").html('');
}
// Controllo partita_iva
if ( $('#partita_iva').val() != '' )
{
strAjaxData += '&partita_iva=' + $('#partita_iva').val();
$('#lblpartita_iva').removeClass("conferma").removeClass("errore").html('');
}
// Controllo email
if ( $('#email').val() != '' )
{
strAjaxData += '&email=' + $('#email').val();
$('#lblemail').removeClass("conferma").removeClass("errore").html('');
}
// Controllo username
if ( $('#username').val() != '' )
{
strAjaxData += '&username=' + $('#username').val();
$('#lblusername').removeClass("conferma").removeClass("errore").html('');
}
// Chiamata Ajax unica se ci sono campi unici o email
if ( $('#codice_fiscale').val() || $('#partita_iva').val() || $('#email').val() || $('#username').val() )
{
$.ajax({
url: 'include/ajax/checkContatto.php'
,data: 'command=checkContatto' + strAjaxData
,success: function(data)
{
//--- Ajax Results : Inizio
update = data.split('|');
if( update[0] == "checkContatto" )
{
// Controllo codice_fiscale
if ( $('#codice_fiscale').val() != "" )
{
if ( update[2] == 1 )
{
// ERRORE : valore NON unico
$('#lblcodice_fiscale').removeClass('conferma').addClass('errore').html("Il codice inserito risulta già iscritto al sito.");
blnReturn = false;
strAlert += "- Valore gia' in archivio \n";
}
else
{
// OK : valore unico
$('#lblcodice_fiscale').removeClass('errore').addClass('conferma').html("Il codice inserito è unico. Ok.");
}
}
// Controllo email
if ( $('#email').val() != "" )
{
if ( update[4] == 0 )
{
// ERRORE : email NON corretta
$('#lblemail').removeClass('conferma').addClass('errore').html("L'indirizzo non è corretto.");
blnReturn = false;
strAlert += "- Email non corretta.\n \n";
}
else
{
// OK : email corretta
if ( update[5] == 1 )
{
// ERRORE : email non unica
$('#lblemail').removeClass('conferma').addClass('errore').html("Il valore è già in archivio.");
blnReturn = false;
strAlert += "- Email e' gia' in archivio.\n \n";
}
else
{
// OK : Email unica e corretta
$('#lblemail').removeClass('errore').addClass('conferma').html("L'indirizzo sembra corretto. Ok.");
}
}
}
// Controllo username
if ( $('#username').val() != "" )
{
if ( update[7] == 1 )
{
// ERRORE : valore NON unico
$('#lblusername').removeClass('conferma').addClass('errore').html("Il valore è già in archivio.");
blnReturn = false;
strAlert += "- Username e' gia' in archivio.\n \n";
}
else
{
// OK : valore unico
$('#lblusername').removeClass('errore').addClass('conferma').html("Il valore è unico. Ok.");
}
}
// Controllo partita_iva
if ( $('#partita_iva').val() != "" )
{
if ( update[9] == 1 )
{
// ERRORE : valore NON unico
$('#lblpartita_iva').removeClass('conferma').addClass('errore').html("Il valore è già in archivio.");
blnReturn = false;
strAlert += "- Valore gia' in archivio \n";
}
else
{
// OK : valore unico
$('#lblpartita_iva').removeClass('errore').addClass('conferma').html("Il valore è unico. Ok.");
}
}
}
if ( ! blnReturn && ! pintHideAlert )
{
alert( strAlert );
$("html,body").animate({scrollTop: 0}, 1500, "bounceout");
}
if ( blnReturn && pintSubmit )
{
blnSubmit = true;
$('#formRecord').unbind('submit');
$('#formRecord').submit();
}
return blnReturn;
//--- Ajax Results : Fine
}
});
}
else
{
if ( ! blnReturn && ! intHideAlert )
{
alert( strAlert );
$("html,body").animate({scrollTop: 0}, 1500, "bounceout");
}
if ( blnReturn && pintSubmit )
{
blnSubmit = true;
$('#formRecord').unbind('submit');
$('#formRecord').submit();
}
return blnReturn;
}
}