function checkRegFields() {
    var cango = true;
    var regtxt = /^[a-zA-Z\'\ ]+$/;
    var regemail = /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/;
    
    var first_name = document.forms['reg_form'].elements['first_name'].value;
    var last_name = document.forms['reg_form'].elements['last_name'].value;
    var day = document.forms['reg_form'].elements['day'].selectedIndex;
    var month = document.forms['reg_form'].elements['month'].selectedIndex;
    var year = document.forms['reg_form'].elements['year'].selectedIndex + 1969;
    var sex = document.forms['reg_form'].elements['sex'];
    var email = document.forms['reg_form'].elements['email'].value
    var pwd_1 = document.forms['reg_form'].elements['password_1'].value;
    var pwd_2 = document.forms['reg_form'].elements['password_2'].value;
    var privacy = document.forms['reg_form'].elements['privacy'];
    
    $('.field_error').css('margin-left', '0');
    
    if (!regtxt.test(first_name)) {
        $('#first_name_error').show('blind', 800, function() {
            $('#first_name_error').css('margin-left', '190px');
        });
        cango = false;
    } else if ($('#first_name_error').css('display') != 'none') {
        $('#first_name_error').hide('blind', 800);
    }
    if (!regtxt.test(last_name)) {
        $('#last_name_error').show('blind', 800, function() {
            $('#last_name_error').css('margin-left', '190px');
        });
        cango = false;
    } else if ($('#last_name_error').css('display') != 'none') {
        $('#last_name_error').hide('blind', 800);
    }
    var date = new Date(year, month - 1, day);
    if (!checkDate(day, month, year, date)) {
        $('#birth_date_error').show('blind', 800, function() {
            $('#birth_date_error').css('margin-left', '190px');
        });
        cango = false;
    } else if ($('#birth_date_error').css('display') != 'none') {
        $('#birth_date_error').hide('blind', 800);
    }
    if (!sex[0].checked && !sex[1].checked) {
        $('#sex_error').css('margin-left', '190px');
        $('#sex_error').show('blind', 800, function() {
            $('#sex_error').css('margin-left', '190px');
        });
        cango = false;
    } else if ($('#sex_error').css('display') != 'none') {
        $('#sex_error').hide('blind', 800);
    }
    if (!regemail.test(email)) {
        $('#email_error').show('blind', 800, function() {
            $('#email_error').css('margin-left', '190px');
        });
        cango = false;
    } else if ($('#email_error').css('display') != 'none') {
        $('#email_error').hide('blind', 800);
    }
    if (document.forms['reg_form'].elements['action'].value != 'Aggiorna dati' || (pwd_1.length > 0 || pwd_2.length > 0)) {
        if (pwd_1.length < 8) {
            $('#password_1_error').show('blind', 800, function() {
                $('#password_1_error').css('margin-left', '190px');
            });
            cango = false;
        } else if ($('#password_1_error').css('display') != 'none') {
            $('#password_1_error').hide('blind', 800);
        }
        if (pwd_1 != pwd_2) {
            $('#password_2_error').show('blind', 800, function() {
                $('#password_2_error').css('margin-left', '190px');
            });
            cango = false;
        } else if ($('#password_2_error').css('display') != 'none') {
            $('#password_2_error').hide('blind', 800);
        }
        if (cango && !privacy.checked) {
            $('#box_modal_background').css('display', 'block');
            $('#box_modal_privacy').css('display', 'block');
            cango = false;
        }
    }
    
    return cango;
}

function checkCompFields() {
    var cango = true;
    
    var title = document.forms['comp_form'].elements['title'].value;
    var news_text = document.forms['comp_form'].elements['news_text'].value;
    
    if (title == '') {
        $('#title_error').show('blind', 800);
        cango = false;
    } else {
        $('#title_error').hide('blind', 800);
    }
    if (news_text == '') {
        $('#text_error').show('blind', 800);
        cango = false;
    } else {
        $('#text_error').hide('blind', 800);
    }
    
    return cango;
}

function checkFaqFields() {
    var cango = true;
    var regtxt = /^[a-zA-Z\'\ ]+$/;
    var regnum = /^[0-9]{2}$/;
    var regemail = /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/;
    
    var name = document.forms['faq_form'].elements['name'].value;
    var email = document.forms['faq_form'].elements['email'].value;
    var age = document.forms['faq_form'].elements['age'].value;
    var sex = document.forms['faq_form'].elements['sex'];
    var question = document.forms['faq_form'].elements['question'].value;
    
    if (!regemail.test(email)) {
        cango = false;
        alert('L\'indirizzo email inserito non e\' corretto.');
    }
    if (!regtxt.test(name) && cango) {
        cango = false;
        alert('Il nome inserito non e\' valido.');
    }
    if (!regnum.test(age) && cango) {
        cango = false;
        alert('L\'eta\' inserita non e\' valida.');
    }
    if (!sex[0].checked && !sex[1].checked && cango) {
        cango = false;
        alert('E\' necessario specificare il sesso.');
    }
    if (question.length == 0 && cango) {
       cango = false;
       alert('La domanda inserita non e\' valida.')
    }
    
    return cango;
}

function checkDate(day, month, year, date) {
    if (date.getFullYear() == year && 
        date.getMonth() + 1 == month && 
        date.getDate() == day) {
        return true;
    }else{
        return false;
    }
}

