/**
 * Validates the add form.
 */
function validateForm() {

    var err = "";

    /* Check if name was provided */
    if(document.register.user_name.value == "") {
        err += "- You have not provided your name\n";
    }

    /* Check if valid e-mail was provided */
    if(document.register.user_email.value == "") {
        err += "- You have not provided your e-mail address\n";
    }
    else {
        var email = document.register.user_email.value;
        var invalidChars = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
        var validChars = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

        if (!(!invalidChars.test(email) && validChars.test(email))) {
            err += "- You have not provided a valid e-mail address\n";
        }
    }

    /* Check if a password was provided */
    if(document.register.user_pass.value == "") {
        err += "- You have not provided a password\n";
    }
    else if(document.register.user_pass.value.length < 6) {
        err += "- The password should be at least six characters\n";
    }
    else if(document.register.user_pass.value != document.register.user_pass_confirm.value) {
        err += "- The two password entries do not match\n";
    }

    /* Check if gender was provided */
    if(document.register.sex.options[document.register.sex.selectedIndex].value == "") {
        err += "- You have not provided your sex\n";
    }

    /* Check if age was provided */
    if(document.register.age.options[document.register.age.selectedIndex].value == "") {
        err += "- You have not provided your age\n";
    }

    /* Check if education was provided */
    if(document.register.education.options[document.register.education.selectedIndex].value == "") {
        err += "- You have not provided your level of education\n";
    }

    /* Check if nationality was provided */
    if(document.register.nationality.value == "") {
        err += "- You have not provided your nationality\n";
    }

    /* Check if country of residence was provided */
    if(document.register.countryOfResidence.options[document.register.countryOfResidence.selectedIndex].value == "") {
        err += "- You have not provided your country of residence\n";
    }

    /* Check if a valid SMS number was provided */
    var phoneNumber = document.register.phone.value;
    var len = phoneNumber.length;
    var phoneChars = "0123456789";

    for(var i = 0; i < len; i++) {
        if (phoneChars.indexOf(phoneNumber.charAt(i)) == -1) {
            err += "- You have an invalid character '"+ phoneNumber.charAt(i) +"' in your phone number\n";
        }
    }

    if(err) {
        alert("Please correct the following error(s):\n\n" + err);
        return false;
    }
    else {
        return true;
    }
}


/**
 * Validates the update form.
 */
function validateUpdateForm() {

    var err = "";

    /* Check if name was provided */
    if(document.register.user_name.value == "") {
        err += "- You have not provided your name\n";
    }

    /* Check if valid e-mail was provided */
    if(document.register.user_email.value == "") {
        err += "- You have not provided your e-mail address\n";
    }
    else {
        var email = document.register.user_email.value;
        var invalidChars = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
        var validChars = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

        if (!(!invalidChars.test(email) && validChars.test(email))) {
            err += "- You have not provided a valid e-mail address\n";
        }
    }

    /* Check if a new password was provided */
    if(document.register.user_pass.value != document.register.user_pass_confirm.value) {
        err += "- The two password entries do not match\n";
    }

    /* Check if gender was provided */
    if(document.register.sex.options[document.register.sex.selectedIndex].value == "") {
        err += "- You have not provided your sex\n";
    }

    /* Check if age was provided */
    if(document.register.age.options[document.register.age.selectedIndex].value == "") {
        err += "- You have not provided your age\n";
    }

    /* Check if education was provided */
    if(document.register.education.options[document.register.education.selectedIndex].value == "") {
        err += "- You have not provided your level of education\n";
    }

    /* Check if nationality was provided */
    if(document.register.nationality.value == "") {
        err += "- You have not provided your nationality\n";
    }

    /* Check if country of residence was provided */
    if(document.register.countryOfResidence.options[document.register.countryOfResidence.selectedIndex].value == "") {
        err += "- You have not provided your country of residence\n";
    }

    /* Check if a valid SMS number was provided */
    var phoneNumber = document.register.phone.value;
    var len = phoneNumber.length;
    var phoneChars = "0123456789";

    for(var i = 0; i < len; i++) {
        if (phoneChars.indexOf(phoneNumber.charAt(i)) == -1) {
            err += "- You have an invalid character '"+ phoneNumber.charAt(i) +"' in your phone number\n";
        }
    }

    if(err) {
        alert("Please correct the following error(s):\n\n" + err);
        return false;
    }
    else {
        return true;
    }
}

