$(function()
{
$("#frmContacts").submit( function()
{
if( fCheckObbligatori() ) fSaveData();
return false;
});
// 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('');
});
$(document).bind('keydown', 'Ctrl+n', function()
{
$('#frmContacts textarea').lorem({ type: 'paragraphs',amount:'4',ptags:false});
$('#frmContacts input[type=checkbox]').attr('checked',true);
$('#txtNominativo').val('Lorem Ipsum');
$('#txtCitta').val('Lorem ipsum dolor sit amet');
$('#txtTelefono').val('+39 000 00000000');
$('#txtEmail').val('info@dynform.it');
//fCheckObbligatori( 1 );
});
$(document).bind('keydown', 'Ctrl+k', function()
{
$('#frmContacts input[type!=submit]').val('');
$('#frmContacts textarea').html('');
$('#frmContacts input[type=checkbox]').attr('checked',false);
//fCheckObbligatori( 1 );
});
$('#txtNominativo').focus();
});
function fSaveData()
{
$('submit').focus(); // focus sul submit in modo da lanciare onchange sui vari campi
fLoading(1);
var data;
var strDestination = '';
var blnSaved = false;
$.ajax({
type: "POST",
async: false,
dataType: "json",
url: "/tpl/default/ajax/sendContatti.php",
data: $("#frmContacts").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 fLoading(0);
}
// Controllo campi obbligatori
function fCheckObbligatori()
{
var blnReturn = true;
$('.required').each(function()
{
if ( $.trim( $(this).val() ) == "" )
{
blnReturn = false;
$( "#lbl" + $(this).attr("name") ).html("Campo obbligatorio");
}
else $( "#lbl" + $(this).attr("name") ).html("");
});
// Controllo privacy
if( ! $('#privacy').attr('checked') )
{
blnReturn = false;
$('#lblprivacy').html("Accettazione obbligatoria.");
}
else $('#lblprivacy').html('');
// Controllo Email
if ( $('#txtEmail').val() != '' )
{
$('#lbltxtEmail').removeClass("conferma").removeClass("errore").html('');
var intErrore = '';
$.ajax({
type: "POST",
async: false,
dataType: "json",
url: "/tpl/default/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').removeClass('conferma').addClass('errore').html("L'indirizzo non è corretto.");
}
else if ( intErrore == 2 )
{
// ERRORE : email NON esiste
blnReturn = false;
$('#lbltxtEmail').removeClass('conferma').addClass('errore').html("L'indirizzo non esiste");
}
else
{
// OK : email corretta
$('#lbltxtEmail').removeClass('errore').addClass('conferma').html(""); // OK : Email unica e corretta
}
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').hide();
$('#submitResult').show();
}
return blnReturn;
}