
function checkEmail(email) {

    var regEx = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    return ((regEx.test(email)) && (email != ''))
} // function


function IsDate(data) {

    var myreg = /^(((0[1-9]|[12][0-9]|3[01])([\-.\/])(0[13578]|10|12)([\-.\/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([\-.\/])(0[469]|11)([\-.\/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([\-.\/])(02)([\-.\/])(\d{4}))|((29)(\.|-|\/)(02)([\-.\/])([02468][048]00))|((29)([\-.\/])(02)([\-.\/])([13579][26]00))|((29)([\-.\/])(02)([\-.\/])([0-9][0-9][0][48]))|((29)([\-.\/])(02)([\-.\/])([0-9][0-9][2468][048]))|((29)([\-.\/])(02)([\-.\/])([0-9][0-9][13579][26])))$/;

    return myreg.test(data)

}

function savenewsletter() {

    var _url = '/web/SaveNewsLetter.aspx?tsr=' + Math.random().toString();
    var actError = 0;
    var msgError = '';



    if ($('#nome').val() == '') {
        msgError += 'Il campo Nome è obbligatorio!\r\n';
        //msgError += '<li>Il campo <b>UserName</b> &egrave; obbligatorio</li>';
        actError = 1;
    } //if

    if ($('#cognome').val() == '') {
        msgError += 'Il campo Cognome è obbligatorio!\r\n';
        //msgError += '<li>Il campo <b>UserName</b> &egrave; obbligatorio</li>';
        actError = 1;
    } //if

    // Controllo campi obbligatori //
    if (!checkEmail($('#email').val())) {
        msgError += 'Il campo Email è obbligatorio o errato!\r\n';
        //msgError += '<li>Controllare il campo <b>Email</b></li>';
        actError = 1;
    } //if

    if ($('#citta').val() == '') {
        msgError += 'Il Città è obbligatorio!\r\n';
        //msgError += '<li>Il campo <b>UserName</b> &egrave; obbligatorio</li>';
        actError = 1;
    } //if

    var giorno = $("#giorno").val();
    var mese = $("#mese").val();
    var anno = $("#anno").val();
    var bDate = giorno + '/' + mese + '/' + anno


    if ((giorno) == '' || (mese) == '' || (anno) == '') {
        $('input#datanascita').val('');
    } else {
        $('input#datanascita').val(bDate);
    } // if

    
    if (( bDate.length > 2) && !IsDate(bDate)) {
        msgError += 'Controllare il campo Data di Nascita!\r\n';
        // msgError += '<li>Controllare il campo <b>Data di Nascita</b></li>';
        actError = 1;
    }

    if (!$('#accetto').attr("checked")) {

        msgError += 'Bisogna accettare le condizioni di servizio!\r\n';
        // msgError += '<li>Controllare il campo <b>Data di Nascita</b></li>';
        actError = 1;
    
    }
    
    var usrName = $('#nome').val(); ;
    var usrSurname = $('#cognome').val();
    var usrEmail = $('#email').val();
    var usrCellulare = $('#cellulare').val();
    var usrIndirizzo = $('#indirizzo').val();
    var usrCap = $('#cap').val();
    var usrCitta = $('#citta').val();
    var usrProvincia = $('#provincia').val();
    var usrNazione = $('#nazione').val();
   
    var usrDataNascita = bDate;

  
  
    if (actError == 1) {
        alert(msgError);
        return false;
    }
    else {

        var obj = {};
        obj.output = 'JSON';

        obj.usrName = usrName;
        obj.usrSurname = usrSurname;
        obj.usrEmail = usrEmail;
        obj.usrCellulare = usrCellulare;
        obj.usrIndirizzo = usrIndirizzo;
        obj.usrCap = usrCap;
        obj.usrCitta = usrCitta;
        obj.usrProvincia = usrProvincia;
        obj.usrNazione = usrNazione;
         if(bDate.length > 2)  obj.usrDataNascita = anno + '-' + mese + '-' + giorno;


        $.getJSON(
          _url,
          obj,
         function(data) {
             if (data.Table[0].actSave == '') {

                 var okUrl = '/it/newsletter/regitrazioneok/registrazione-newsletter.html';
                 location.href = okUrl;

             } else {

                 var errText = 'Errore durante il salvataggio \r\n';
                 errText += data.Table[0].actSave;
                 alert(errText);

             }
         })//JSon


    }

} //end

    
    
