if (!SIGI) throw ("SIGI.Form depends on the following scripts. Please make sure these are included.\nSIGI.js, SIGI.Validation.js");

function FormObject(mainForm) {
    this.Base();
    this.InstanceID = null;
    this.oldDoPostBack = null;
    this.mainForm = mainForm;
    this.isDoPostBack = false;
    this.formChangedOverride = null;

    if (window.__doPostBack != null) this.oldDoPostBack = window.__doPostBack;

    window.__doPostBack = this.context(this.DoPostBack);

    if (this.mainForm != null) {
        if (this.mainForm.__InstanceID != null) {
            this.InstanceID = this.mainForm.__InstanceID.value;
        }

        this.allowBrowserNavigation = true;
        if (this.mainForm.__AllowBrowserNavigation != null) {
            if (this.mainForm.__AllowBrowserNavigation.value == "false")
                this.allowBrowserNavigation = false;
        }

        if (this.mainForm.__RequiredLabelClassName) {
            SIGI.Validation.RequiredLabelClassName = this.mainForm.__RequiredLabelClassName.value;
        }

        if (this.mainForm.__RequiredControlClassName) {
            SIGI.Validation.RequiredControlClassName = this.mainForm.__RequiredControlClassName.value;
        }

        SIGI.attachEvent(this.mainForm, "onsubmit", this.context(this._onsubmit));

        this.setScrollPosition();
    }
}
FormObject.inherits(SIGI.Base);
FormObject.prototype.saveScrollPosition = function() {
    var contentDiv = document.getElementById("PageContentArea");
    if (contentDiv != null) {
        var input = document.getElementById("__ScrollTop");
        if (input != null) {
            input.value = contentDiv.scrollTop;
        }
    }
}
FormObject.prototype.setScrollPosition = function() {
    var contentDiv = document.getElementById("PageContentArea");
    if (contentDiv != null) {
        var input = document.getElementById("__ScrollTop");
        if (input != null) {
            try {
                contentDiv.scrollTop = input.value;
            } catch (ex) { }
        }
    }
}
FormObject.prototype.saveFormChanged = function() {
    var input = document.getElementById("__FormChanged");
    if (input != null) {
        input.value = this.isChanged();
    }
}
FormObject.prototype._onsubmit = function() {
    //this.DoPostBack(this.mainForm.name, "");
    event.returnValue = false;
    return false;
}
FormObject.prototype.DoPostBack = function(eventTarget, eventArgument) {

    try {
        if (OnBeforeSubmit) {
            if (eval(OnBeforeSubmit) == false) {
                return;
            }
        }
    }
    catch (e) {
    }

    this.isDoPostBack = true;

    if (this.mainForm != null) {
        this.mainForm.action = SIGI.formatURL(this.mainForm.action);
    }

    this.enableElements(this.mainForm);
    this.saveFormChanged();
    this.saveScrollPosition();
    SIGI.showPageTransition();
    if (this.oldDoPostBack != null) {
        this.oldDoPostBack((eventTarget != null ? eventTarget : ""), eventArgument);
    } else {
        if (this.mainForm != null) this.mainForm.submit();
    }
}
FormObject.prototype.get_formChanged = function(value) {
    return this.isChanged();
}
FormObject.prototype.set_formChanged = function(value) {
    if ((value == true) || (value == false)) {
        this.formChangedOverride = value;
    }
}
FormObject.prototype.isChanged = function() {
    if (this.formChangedOverride != null) {
        return this.formChangedOverride;
    }
    else {
        return this.isFormChanged(this.mainForm);
    }
}
FormObject.prototype.isFormChanged = function(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (SIGI.isChanged(element)) return true;
    }
    return false;
}

//id=PageContentArea

FormObject.prototype.belongsToStackingPanel = function(control) {
    var ctl = control;
    var running = true;
    var retVal = false;
    while (running) {
        try {
            if (ctl.className.indexOf("StackingPanel") == 0) {
                running = false;
                retVal = true;
            }
            if (ctl.id == "PageContentArea") {
                running = false;
            }
        }
        catch (e) { }
        try {
            ctl = ctl.parentNode;
        }
        catch (e) {
            running = false;
        }

    }
    return retVal;
}


FormObject.prototype.enableElements = function(form) {
    var disabledOrNotDisplayed = "";

    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        var label = element.label;

        if (element.type != "button") {

            if (element.style.display == "none") {
                //handle 
                //handle calendar composite control
                if (element.id.substring(element.id.length - 5) == "_Text") {
                    if (disabledOrNotDisplayed.indexOf(element.id.substring(0, element.id.length - 5) + "|nodisplay;") == -1) {
                        disabledOrNotDisplayed += element.id.substring(0, element.id.length - 5) + "|nodisplay;"
                    }
                }
                if (disabledOrNotDisplayed.indexOf(element.id + "|nodisplay;") == -1) {
                    disabledOrNotDisplayed += element.id + "|nodisplay;"
                }
                if (label) {
                    var lbl = label;
                    if (label.id) {
                        lbl = label.id;
                    }
                    if (SIGI.Validation.hideLabelWithElement(document.getElementById(lbl))) {
                        disabledOrNotDisplayed += label + "|nodisplay;"
                    }
                }
            }
            if (element.disabled) {
                if (!this.belongsToStackingPanel(element)) {
                    if (element.id.substring(element.id.length - 5) == "_Text") {
                        if (disabledOrNotDisplayed.indexOf(element.id.substring(0, element.id.length - 5) + "|disabled;") == -1) {
                            disabledOrNotDisplayed += element.id.substring(0, element.id.length - 5) + "|disabled;"
                        }
                    }
                    if (disabledOrNotDisplayed.indexOf(element.id + "|disabled;") == -1) {
                        disabledOrNotDisplayed += element.id + "|disabled;"
                    }


                    if (label) {
                        var lbl = label;
                        if (label.id) {
                            lbl = label.id;
                        }
                        if (SIGI.Validation.hideLabelWithElement(document.getElementById(lbl))) {
                            disabledOrNotDisplayed += label + "|disabled;"
                        }
                    }
                }
            }
        }
        if (element.disabled) {
            if ((!element.enableOnSubmit) && (toBoolean(this.enableOnSubmit(form)))) {
                SIGI.enable(element);
            }

            if (toBoolean(element.enableOnSubmit)) {
                if ($SIGIfind(element.id)) {
                    try {
                        if ($SIGIfind(element.parentNode.id)) {
                            $SIGIfind(element.parentNode.id).set_enabled(true);
                        }
                        /*RTS 1301860
                        For composites, it is the associated (and hidden) value element that is used for postback,
                        and that is the element that gets picked up in this loop as the representative for the
                        composite control.  Problem is, that's not the part that users actually see and use in the UI.
                        So we check for a related component object, which would follow the naming convention reflected here
                        For every composite control, there is an element called ID_Value, and a related component with the name
                        ID_Composite.  This locates it and called its set_enabled, to disable all parts of the composite
                        */
                        if ($SIGIfind(element.id.replace("_Value", "_Composite"))) {
                            $SIGIfind(element.id.replace("_Value", "_Composite")).set_enabled(true);
                        }
                    }
                    catch (e) {
                    }
                    $SIGIfind(element.id).set_enabled(true);
                }
                else {
                    SIGI.enable(element);
                }
            }
        }
    }
    var input = document.getElementById("__DisabledOrNotDisplayed");
    if (input != null) {
        input.value = disabledOrNotDisplayed;
    }
    return false;
}

FormObject.prototype.enableOnSubmit = function(form) {

    return toBoolean(form.getAttribute("enableOnSubmit"));
}

SIGI.FormObject = FormObject;
FormObject = null;
