﻿
// Was built off the BAIweb design and this one is on ALL pages, so be carefull what you put in here
// Like NO Sys.<somefunction> will not work, is scriptmanager is not on page.

function tester() {
    alert("hey");
}

// create the BAI namespace if it does not exist yet
if (!BAIweb) var BAIweb = {};
if (!BAIweb.MessageBox) BAIweb.MessageBox = {};
if (!BAIweb.HandleError) BAIweb.HandleError = {};

//anonymous namespace/function
// ID - Is the Control ID given to the MessageBox, this will help to 
//      find the correct MsgBox on the page is multiple boxes exist.
(function() {

    //was intialized at top of page.
    BAIweb.MessageBox = {
        ID: "",
        Show: function(id, msgType, msg) {
            BAIweb.MessageBox.ID = id;
            BAIweb.MessageBox.SetMessageType(msgType);
            BAIweb.MessageBox.SetMessage(msg);
            jQuery("[id$='divMessageCont_" + BAIweb.MessageBox.ID + "']").show();
            BAIweb.MessageBox.GoToTop();
        },
        Hide: function(id) {
            BAIweb.MessageBox.ID = id;                        
            BAIweb.MessageBox.Clean();
            jQuery("[id$='divMessageCont_" + BAIweb.MessageBox.ID + "']").hide();
        },
        GoToTop: function() {
            scroll(0, 0);
        },
        SetMessageType: function(msgType) {
            var divCont = jQuery("[id$='divMessageCont_" + BAIweb.MessageBox.ID + "']");

            //check for current class and keep order for css to work properly
            BAIweb.MessageBox.SetClass(divCont, msgType);
        },
        SetMessage: function(msg) {
            jQuery("[id$='spanMessageText_" + BAIweb.MessageBox.ID + "']").html(msg);
        },
        Clean: function() {
            jQuery("[id$='spanMessageText_" + BAIweb.MessageBox.ID + "']").html("");
        },
        SetClass: function(obj, cls) {
            if (obj.hasClass('error') == true) {
                obj.removeClass('error');
            }
            if (obj.hasClass('notice') == true) {
                obj.removeClass('notice');
            }
            if (obj.hasClass('success') == true) {
                obj.removeClass('success');
            }
            obj.addClass(cls);
        }
    };
} ());


//anonymous namespace/function
(function() {

    //was intialized at top of page.
    //the first variables are propetie and can be set when using this object.
    //Default setting for properties.
    // ID - Is the Control ID given to the MessageBox, this will help to 
    //      find the correct MsgBox on the page is multiple boxes exist.
    BAIweb.HandleError = {
        ID: "",
        msgCont: jQuery("[id$='divMessageCont_" + BAIweb.HandleError.ID + "']"),
        msgtext: jQuery("[id$='spanMessageText_" + BAIweb.HandleError.ID + "']"),
        msgType: "error",
        msg: "",
        HandlePageMethodError: function(id, error, userContext, methodName) {
            //broke this method out for future possibly logging thses errors
            BAIweb.MessageBox.ID = id;
            if (BAIweb.HandleError.msg == "") { BAIweb.HandleError.msg = error.get_message(); }

            //show error
            BAIweb.MessageBox.Show(BAIweb.HandleError.msgType, BAIweb.HandleError.msg);
            BAIweb.HandleError.msg = "";
        },
        HandleAsyncError: function(id, sender, args) {
            //broke this method out for future possibly logging thses errors
            BAIweb.MessageBox.ID = id;
            if (BAIweb.HandleError.msg == "" && args.get_error())
            { BAIweb.HandleError.msg = args.get_error().message.replace(/Sys.WebForms.PageRequestManagerServerErrorException:/, ""); }
            //show error
            BAIweb.MessageBox.Show(BAIweb.HandleError.msgType, BAIweb.HandleError.msg);

            //used to stop error from continuing to script message
            args.set_errorHandled(true);
            BAIweb.HandleError.msg = "";
        },
        WindowsErrorHandler: function(id, message, url, lineNumber) {
            BAIweb.MessageBox.ID = id;
            //broke this method out for future possibly logging thses errors
            if (BAIweb.HandleError.msg == "") { BAIweb.HandleError.msg = message; }

            //show error
            BAIweb.MessageBox.Show(BAIweb.HandleError.msgType, BAIweb.HandleError.msg);
            BAIweb.HandleError.msg = "";
            //** IMPORTANT ** change this to true to stop errors from reaching the browser window.           
            return false;
        }
    };
} ());


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();