var components = {};

Ext.onReady(function() {
    Ext.BLANK_IMAGE_URL = "/lib/ext-2.2/resources/images/default/s.gif";

    /*
     * Skin data entry controls.
     */

    var inputs = Ext.query("input");
    Ext.each(inputs, function(node) {
        var element = Ext.get(node);

        if (element.getAttributeNS("", "type") == "text") {
            if (element.hasClass("DateField")) {
                /*
                 * DateField
                 */
                var field = new Ext.form.DateField({
                    format: "n/j/Y",
                    applyTo: element
                });
                eval("components." + element.id + " = field");
            } else if (element.hasClass("TimeField")) {
                /*
                 * TimeField
                 */
                var field = new Ext.form.TimeField({
                    typeAhead: true,
                    applyTo: element
                });
                eval("components." + element.id + " = field");
            } else {
                /*
                 * TextField
                 */
                var field = new Ext.form.TextField({
                    inputType: "text",
                    applyTo: element
                });
                eval("components." + element.id + " = field");
            }
        } else if (element.getAttributeNS("", "type") == "password") {
            /*
             * TextField[inputType=password]
             */
            var field = new Ext.form.TextField({
                inputType: "password",
                applyTo: element
            });
            eval("components." + element.id + " = field");
        }
    });
    
    var selects = Ext.query("select");
    Ext.each(selects, function(node) {
        var element = Ext.get(node);

        var forceSelection = false;
        if (element.hasClass("ForceSelection")) {
            forceSelection = true;
        }

        var executeOnSelect = false;
        if (element.hasClass("ExecuteOnSelect")) {
            executeOnSelect = true;
        }

        /*
         * ComboBox
         */
        var field = new Ext.form.ComboBox({
            transform: element,
            typeAhead: true,
            forceSelection: forceSelection,
            triggerAction: "all",
            width: element.getWidth()
        });

        if (executeOnSelect) {
            field.on("select", function(e) {
                eval(element.id + "_Selected()");
            });
        }

        eval("components." + element.id + " = field");
    });

    var textareas = Ext.query("textarea");
    Ext.each(textareas, function(node) {
        var element = Ext.get(node);

        /*
         * TextArea
         */
        var field = new Ext.form.TextArea({
            applyTo: element
        });
        eval("components." + element.id + " = field");
    });

    /*
     * Initialize QuickTips.
     */
    Ext.QuickTips.init();

    /*
     * Display status message as a MessageBox.
     */
    var divMessage = Ext.get("statusMessage");
    if (divMessage != null) {
        Ext.MessageBox.show({
            icon: Ext.MessageBox.INFO,
            title: "Tennessee Gas Association",
            msg: divMessage.dom.innerHTML,
            buttons: Ext.MessageBox.OK,
            modal: true,
            minWidth: 220
        });
    }

    /*
     * Display validation summary as a MessageBox.
     */
    var divSummary = Ext.get("ctl00_vSummary");
    if (divSummary != null) {
        if (divSummary.isDisplayed()) {
            Ext.MessageBox.show({
                icon: Ext.MessageBox.ERROR,
                title: "Tennessee Gas Association",
                msg: "<div style=\"float: left;\"><p style=\"margin-bottom: 12px;\">Please correct the following form errors.</p>" + divSummary.dom.innerHTML + "</div>",
                buttons: Ext.MessageBox.OK,
                modal: true,
                minWidth: 320
            });
        }
    }

    var divLoading = Ext.get("loading");
    if (divLoading != null) {
        setTimeout(function() {
            divLoading.fadeOut({remove: true});
        }, 200);
    }
});

var extConfirmAllowSubmit = false;
function extConfirm(button, message) {
    if (!extConfirmAllowSubmit) {
        Ext.onReady(function() {
            Ext.MessageBox.show({
                icon: Ext.MessageBox.WARNING,
                title: "Tennessee Gas Association",
                msg: message,
                buttons: Ext.MessageBox.YESNO,
                modal: true,
                minWidth: 220,
                animEl: button,
                fn: function(confirmButton) {
                    if (confirmButton == "yes") {
                        extConfirmAllowSubmit = true;
                        button.click();
                    }
                }
            });
        });
    }

    return extConfirmAllowSubmit;
}

function showWaitingBar(button, message) {
    Ext.onReady(function() {
        Ext.MessageBox.wait(message, "Tennessee Gas Association");
    });
}
