// Maps dialog id to the yahoo dialog object.
// Used to keep track of active/shown dialogs.
var hhDialogMap = new Object();

HhDialog = function(el, userConfig) {
    HhDialog.superclass.constructor.call(this, el, userConfig);

    // This is part of a work-around to a FireFox problem (see bug #457)
    if (this.browser == "gecko") {
        this.renderEvent.subscribe(function() {
            YAHOO.util.Dom.addClass(this.form, "caretfix");
        });
    }
};

YAHOO.extend(HhDialog, YAHOO.widget.Dialog);

HhDialog.prototype.initDefaultConfig = function() {
	HhDialog.superclass.initDefaultConfig.call(this);
    this.cfg.addProperty("taHelpTopic",    { value:"", suppressEvent:true } );
	this.cfg.addProperty("taResetForm",    { value:true, suppressEvent:true } );
    this.cfg.addProperty("taParentDialog", { value:null, suppressEvent:true } );
}

HhDialog.prototype.validate = function() {
    // defined in validation.js; validation.js is required for HhDialogs
    return validate(this.form);
};

HhDialog.prototype._onFormKeyDown = function(p_oEvent) {

    var oTarget = YAHOO.util.Event.getTarget(p_oEvent),
        nCharCode = YAHOO.util.Event.getCharCode(p_oEvent);

    if (
        nCharCode == 13 &&
        oTarget.tagName &&
        oTarget.tagName.toUpperCase() == "INPUT"
    ) {

        var sType = oTarget.type;

        if(
            sType == "text" || sType == "password" || sType == "checkbox" ||
            sType == "radio" || sType == "file"
        ) {

            // Fire the "click" event on the dialog's default button
            // 7/07 added a check for null button which is missing from yahoo 2.2.2 code. Since we don't
            // use yahoo buttons there is no default button. However without the check for null the
            // default buttons click handler will be called which will generate a javascript error.
            if (this.defaultHtmlButton != null) {
                this.defaultHtmlButton.click();
            }

        }

    }
}

HhDialog.prototype.help = function() {
    openHelpWindow("/help/" + this.cfg.getProperty("taHelpTopic"));
};

HhDialog.prototype.show = function() {
    hhDialogMap[this.id] = this;

    if (this.cfg.getProperty("taResetForm")) {
        this.form.reset();
    }

    // defined in validation.js; validation.js is required for HhDialogs
    clearErrors();

    this.center();
	if (this.cfg.getProperty("y") < 0)
	{
		this.cfg.setProperty("y", 0);
	}

    // Check for nested dialog
    var parentDlgId = this.cfg.getProperty("taParentDialog");
    var parentDlg = (parentDlgId != null) ? hhDialogMap[parentDlgId] : null;

    if (parentDlg) {
        // This solves the focus problem we saw in Firefox
        // (i.e., the input boxes on the child dialog wouldn't take focus)
        parentDlg.hideMask();

        // Make sure that the parent dialog appears greyed-out when the child dialog is shown
        this.cfg.setProperty("zIndex", parentDlg.cfg.getProperty("zIndex") + 10);
    }

    HhDialog.superclass.show.call(this);

    // This is part of a work-around to a FireFox problem (see bug #457)
    if (this.browser == "gecko") {
        YAHOO.util.Dom.setStyle(this.form, "display", "none");
        setTimeout(fixCaretDisplay, 0, this);
    }
};

HhDialog.prototype.hide = function() {
    hhDialogMap[this.id] = null;

    var formElements = this.form.elements;

    for (var i = 0; i < formElements.length; i++) {

        if (formElements[i].nodeName == "BUTTON") {
            formElements[i].blur();
        }
    }

    HhDialog.superclass.hide.call(this);

    var parentDlgId = this.cfg.getProperty("taParentDialog");
    var parentDlg = (parentDlgId != null) ? hhDialogMap[parentDlgId] : null;
    if (parentDlg) {
        parentDlg.showMask();
    }
 };

// This is part of a work-around to a FireFox problem (see bug #457)
function fixCaretDisplay(theDlg) {
    YAHOO.util.Dom.setStyle(theDlg.form, "display", "block");
    try {
        theDlg.firstFormElement.focus();
    } catch (e) {
        // Not related to the workaround, I just try/catch focus calls
        // do avoid testing for the various conditions in which they could
        // fail.
    }
}
