﻿/* Handles form reset to default values */
jQuery.fn.resetForm = function(options) {
    var options = $.extend({ wrapper: this, errorDivWrapperId: '', errorDivId: '' }, options);

    return this.each(function() {
        jQuery(this).click(function() {
            jQuery(options.wrapper).each(function() {
                jQuery(this).removeClass("input-error");
                switch (this.tagName) {
                    case "INPUT":
                        if (this.type == 'text') { this.value = ''; }
                        break;
                    case "SELECT":
                        (this.type == 'select-one') ? this.selectedIndex = 0 : false;
                        (this.type == 'select-multiple') ? this.selectedIndex = -1 : false;
                        break;
                    default: break;
                }
                (options.errorDivWrapperId) ? jQuery(options.errorDivWrapperId).hide() : false;
                (options.errorDivId) ? jQuery(options.errorDivId).html('') : false;
            });
            return false;
        });
    });

}

/* Handles the enter key event within a form */
jQuery.fn.addReturnEvent = function(options) {
    var options = jQuery.extend({ formButtonId: '' }, options);
    return this.each(function() {
        jQuery(this).keypress(function(e) {
            try {
                if (this.tagName == 'INPUT') {
                    if (e.keyCode == 13) {
                        jQuery(this).blur();
                        jQuery(options.formButtonId).click();
                        return false;
                    }
                }
            } catch (err) { }
        });
    });
}