Type.registerNamespace("Graph");

Graph.Errors = function (element) 
{
    Graph.Errors.FocusedOnFirst = false;

    Graph.Errors.Validate;
    Graph.Errors.Validate = Function.createDelegate(this, this.validate);

    this.toggleErrors;
    this.toggleErrors = Function.createDelegate(this, this.toggleErrors);

    Graph.Errors.initializeBase(this, [element]);
};

Graph.Errors.prototype =
{
    initialize: function()
    {
        Graph.Errors.callBaseMethod(this, "initialize");

        this._ctlDivErrorMessages = jQuery(this.get_element());
        this._ctlDivValidationsSummary = jQuery("#" + this.get_validationsSummaryId());
        this._ctlDivMessagesSummary = jQuery("#" + this.get_messagesSummaryId());

        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this.toggleErrors);

        setTimeout(this.toggleErrors, 5);
    },

    dispose: function()
    {
        Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this.toggleErrors);
        this._ctlDivErrorMessages.hide();

        Graph.Errors.callBaseMethod(this, "dispose");
    },

    get_validationsSummaryId: function()
    {
        return this._validationsSummaryId;
    },

    set_validationsSummaryId: function(value)
    {
        this._validationsSummaryId = value;
    },

    get_messagesSummaryId: function()
    {
        return this._messagesSummaryId;
    },

    set_messagesSummaryId: function(value)
    {
        this._messagesSummaryId = value;
    },

    validate: function(source, args)
    {
        setTimeout(this.toggleErrors, 5);
    },

    toggleErrors: function(sender, args)
    {
        var validationErrors = this.validationErrors();
        if (validationErrors > 0)
        {
            this._ctlDivMessagesSummary.hide();
        }

        if (validationErrors > 0 || this.messagesErrors() > 0)
        {
            jQuery("#instructions").hide();
            this._ctlDivErrorMessages.show();

            //if none of ValidationHighlightExtender set focus, scroll on top
            //if (Graph.Errors.FocusedOnFirst == false)
            {
                jQuery(window).scrollTop(0);
            }
        }
        else
        {
            this._ctlDivErrorMessages.hide();
        }

        //quick fix - reset flag that focus is on the first element after finished validation
        Graph.Errors.FocusedOnFirst = false;
    },

    validationErrors: function()
    {
        var none = false;
        var validationsSummary = this._ctlDivValidationsSummary.find("div");
        for (var i = 0; i < validationsSummary.length; i++)
        {
            if (jQuery(validationsSummary[i]).css("display") != "none")
            {
                none = true;
                break;
            }
        }

        if (none == false)
        {
            return 0;
        }

        return this._ctlDivValidationsSummary.find("li").length;
    },

    messagesErrors: function()
    {
        if (this._ctlDivMessagesSummary.css("display") == "none")
        {
            return 0;
        }

        return this._ctlDivMessagesSummary.find("li").length;
    }
};

Graph.Errors.registerClass("Graph.Errors", Sys.UI.Control);

if (typeof (Sys) != "undefined")
{
    Sys.Application.notifyScriptLoaded();
}

