VALIDATE = {
    required_message:
        "<p>These fields are required:</p>",
        
    match_message: function(field) { 
        return VALIDATE.label_text(field) + " is in invalid format"
    },
    
    equals_message: function(field, other) { 
        return VALIDATE.label_text(field) + " must equal " + VALIDATE.label_text(other);
    },

    mark_invalid: function(id) {
        $('#'+id).addClass("er");
        VALIDATE.label(id).addClass("er");
    },
    
    mark_valid: function(el) {
        el.removeClass("er");
        VALIDATE.label(el.attr("id")).removeClass("er");
    },
    
    label: function(id) {
        return $("label[for='" + id + "']")
    },
    
    label_text: function(id) {
        return (VALIDATE.label(id)) ? VALIDATE.label(id).html().replace(/:\s*$/, '') : id;
    },
    
    has_data: function(form, id) {
        var el = form[id];
        return (el) ? el.value : false;
    },
    
    match: function(re, form, id) {
        var el = form[id];
        
        var matches = re.match(/^\/(.*?)\/(.*?)$/);
        var r = new RegExp(matches[1], matches[2]);
        return el.value.match(r);
    },
   
    required_if: function() {
    
    },
    
    assert: function(id) {
        if ($(id) == null) {
            alert("Missing Element: " + id);
        }
    },

    validate_form: function(form, validation_hash) {
        var is_valid = true;
        var error_message = new Array();
        var missing = new Array();
        
        // check fields
        $(':input').each(function(index) { 
            var field = $(this).attr('id');
            VALIDATE.mark_valid($(this)) 
            for (val in validation_hash[field]) {
                switch (val) {
                    case 'required':
                        if (!VALIDATE.has_data(form, field)) {
                            is_valid = false;
                            VALIDATE.mark_invalid(field);
                            missing.push(VALIDATE.label_text(field));
                        }
                    break;
        
                    case 'required_if':
                        if (VALIDATE.has_data(form, validation_hash[field][val]) && !VALIDATE.has_data(form, field)) {
                            is_valid = false;
                            VALIDATE.mark_invalid(field);
                            missing.push(VALIDATE.label_text(field));
                        }
                    break;

                    case 'match':
                        if (!VALIDATE.match(validation_hash[field][val], form, field)) {
                            is_valid = false;
                            VALIDATE.mark_invalid(field);
                            var msg = validation_hash[field][val + '_message'] || VALIDATE.match_message(field);
                            error_message.push('<b>' + VALIDATE.label_text(field) + ' error:</b><br/>' + msg);
                        }
                    break;

                    case 'equals':
                        var other = validation_hash[field][val];
                        VALIDATE.assert(other);
                        if ( form[field].value != form[other].value ) {
                            is_valid = false;
                            VALIDATE.mark_invalid(field);
                            VALIDATE.mark_invalid(other);
                            var msg = validation_hash[field][val + '_message'] || VALIDATE.equals_message(field, other);
                            var error = '<b>' + VALIDATE.label_text(field) + ' error</b>:<br />' + msg;
                            error_message.push(error);
                        }
                    break;
                }
            }
        });
        
        // alert user if invalid
        if (!is_valid) {
            var error_div_id = 'error_divness';
            var error_div = $('#'+error_div_id);
            if (!error_div.size()) {
                error_div = $('<div id="' + error_div_id + '" class="error"></div>');
                $(form).prepend(error_div);
            }
            error_div.empty();
            if (missing.length) {
                error_div.append($(VALIDATE.required_message));
                var ul = $('<ul></ul>');
                $(missing).each(function(index,value) { ul.append('<li>' + value + '</li>') });
                error_div.append(ul);
            }
            $(error_message).each(function(index,value) { error_div.append('<p>' + value + '</p><br/><br/>') });
        }
        return is_valid;
    }
};

