/*
    client side validation for enm_mailer
    - http://enure.net/mailer/
*/

function l(m) {
    if (typeof(console) !== 'undefined' && console != null) {
        console.log(m);
    }
}
function $(id) {
    return document.getElementById(id);
}

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function is_email_valid(email) {
    // not the greatest email validator!
    var emailFilter = /^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/; // regex matches the regex in the php.
    return (emailFilter.test(email)) ? true : false;
}
        
var validate = {
    form_is_invalid: false,
    reqd_fields: ['email', 'telHome', 'telWork'],
    parents: ['wSurname', 'wFirst', 'hSurname', 'hFirst'],
    email_fields: ['email'],
    email_is_invalid: false,
    msgs: "",
    init: function() {
        this.restart();
        this.check_reqd_fields();
        this.check_sorta_reqd_fields();
        this.check_email();
        this.try_to_submit_form();
        return false;
    },
    restart: function() {
        this.msgs = '';
        this.form_is_invalid = false;
        this.email_is_invalid = false;
        this.msgs = '';
        $('form_msgs').style.display = 'none';
        $('form_intro').style.display = 'block';
    },
    check_reqd_fields: function() {
        var f = this.reqd_fields;
        for (var i = f.length - 1; i >= 0; i--){
            if (trim($(f[i]).value) === '') {
                this.mark_invalid(f[i]);
                this.form_is_invalid = true;
            } else {
                this.mark_valid(f[i]);
            }
        };
        if (this.form_is_invalid) this.set_msg('Complete all of the required fields.');
        // l('reqd'+this.form_is_invalid);
    },
    check_sorta_reqd_fields: function() {
        var w = $(this.parents[0]).value + $(this.parents[1]).value;
        var h = $(this.parents[2]).value + $(this.parents[3]).value;

        if (this.is_empty(w) && this.is_empty(h)) {
            this.form_is_invalid = true;
            this.set_msg('Either the Wife&rsquo;s or Husband&rsquo;s name must be filled out.');
            this.error_parents();
        } else {
            this.unerror_parents();
        }
    },
    error_parents: function() {
        var p = this.parents;
        for (var i = p.length - 1; i >= 0; i--){
            if (this.is_empty($(p[i]).value)) {
                this.mark_invalid(p[i]);
            }
        };
    },
    unerror_parents: function() {
        var p = this.parents;
        for (var i = p.length - 1; i >= 0; i--){
            this.mark_valid(p[i]);
        };
    },
    check_email: function() {
        var e = this.email_fields;
        for (var i = e.length - 1; i >= 0; i--){
            if (!this.is_empty($(e[i]).value)) {
                if (!is_email_valid(trim($(e[i]).value))) {
                    this.mark_invalid(e[i]);
                    this.email_is_invalid = true;
                } else {
                    this.mark_valid(e[i]);
                }
            }
        };
        if (this.email_is_invalid) {
            this.set_msg('One of the email addresses you provided is not a valid.');
        }
        // l('email'+this.email_is_invalid);
    },
    mark_invalid: function(field) {
        $(field).setAttribute('class', 'error');
    },
    mark_valid: function(field) {
        $(field).setAttribute('class', '');
    },
    is_empty: function(val) {
        return (trim(val) === '') ? true : false;
    },
    set_msg: function(msg) {
        this.msgs = this.msgs + '<li>'+msg+'</li>';
    },
    display_msgs: function() {
        $('form_msgs').innerHTML = this.msgs;
        $('form_msgs').setAttribute('class', 'error');
        $('form_msgs').style.display = 'block';
        $('form_intro').style.display = 'none';
    },
    try_to_submit_form: function() {
        // l('this.form_is_invalid'+this.form_is_invalid);
        // l('this.email_is_invalid'+this.email_is_invalid);
        if (this.form_is_invalid || this.email_is_invalid) {
            window.scrollTo(0,0);
            this.display_msgs();
            // l('this form is invalid');
        } else {
            // l('this form is valid');
            $('mailer_form').submit();
        }
    }
};