
var make_regexp_validator = function(regular_expression, desired_result)
{
    return function(field_name, field_value) {
        var test_val = regular_expression.test(field_value);
        return test_val == desired_result;
    };
};

var only_name_characters = {
    validator: make_regexp_validator(/[^a-zA-Z\.\- ]/, false),
    error: "Only alphabetic characters, spaces, periods, and hyphens are allowed in names."
};

var full_name_requires_space = {
    validator: make_regexp_validator(/\s/, true),
    error: "Please provide both a first and a last name."
};

var no_links = {
    validator: make_regexp_validator(/http:\/\//, false),
    error: "Please do not include any links in your information."
};

var email_format = {
    validator: make_regexp_validator(/.+@.+\..+/, true),
    error: "Please provide a valid email address."
};

var only_phone_characters = {
    validator: make_regexp_validator(/[a-zA-Z]/, false),
    error: "Please do not include any alphabetic characters in your phone number."
};

var non_empty = {
    validator: make_regexp_validator(/.+/, true),
    error: "Please fill out all of the fields in the form."
};

var experience_choose_one = {
    validator: make_regexp_validator(/CHOOSE ONE/, false),
    error: "Please rate your experience in all areas."
};

var validation_rules = {
    name: [ non_empty, only_name_characters, full_name_requires_space ],
    first_name: [ non_empty, only_name_characters, no_links ],
    last_name: [ non_empty, only_name_characters, no_links ],
    email: [ non_empty, email_format, no_links ],
    company: [ non_empty, no_links ],
    phone: [ only_phone_characters ],
    application: [ no_links ],
    experience_c_plus_plus: [ experience_choose_one ],
    experience_object_oriented: [ experience_choose_one ],
    experience_embedded_systems: [ experience_choose_one ],
    experience_web_development: [ experience_choose_one ],
    experience_design_patterns: [ experience_choose_one ],
    experience_internet_protocols: [ experience_choose_one ],
    experience_relational_databases: [ experience_choose_one ],
    experience_technical_writing: [ experience_choose_one ],
    experience_assembly_language: [ experience_choose_one ]
};

var validate_form_element = function(name, value)
{
    var rules = validation_rules[name];
    var failed_rules = null;
    
    if (typeof rules != "undefined") {
        failed_rules = rules.filter(function(rule) {
            return !rule.validator(name, value);
        });
    }
    
    return failed_rules && failed_rules[0] ? failed_rules[0].error : null;
}

var validate_form = function(form)
{
    var i = 0;
    for (var i = 0; i < form.elements.length; ++i) {
        var message = validate_form_element(form.elements[i].name, form.elements[i].value);
        if (message)
        {
            alert(message);
            return false;
        }
        else if (form.elements[i].value == "") {
        	form.elements[i].value = " ";
        }
    }
    
    // If we got here, we know the form's being submitted, so commercify it.  Some forms may not
    // want to have commerce information associated with them -- if there's no commerce_item_id
    // field in the form, we just won't try.
    //
    if (form.commerce_item_id) {
        do_commerce_action(form.commerce_item_id.value);
    }
    
    return true;
};

