﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AI.Common");

AI.Common.InvalidTextRegex = new RegExp("[^a-zA-Z0-9'\\-,()`+\\s]+", "gi");
AI.Common.SimpleTextRegex = new RegExp("^[a-zA-Z0-9'\\-,()`+\\s]{1,110}$", "gi");
AI.Common.HtmlIsHtmlRegex = new RegExp("\<html", "i");
AI.Common.HtmlIsLogonPageRegex = new RegExp("title[\\s\\S\\w\\W\\.]*log\\son[\\s\\S\\w\\W\\.]*title", "mi");
AI.Common.HtmlIsProblemPageRegex = new RegExp("A\\sproblem\\shas\\soccurred", "mi");
AI.Common.GetNamedControlAsJquery = function(elId) {
    return $('#' + this._id + '_' + elId);
};
AI.Common.FindScriptControl = function(el, select) {
    var curEl = el;
    if (!select) select = function(item) { return item.control; }
    while (!select(curEl)) {
        curEl = curEl.parentNode;
    }
    return select(curEl);
};
AI.Common.CheckData = function(data, onTimeout, onError, onFault) {
    //check data returned
    if (AI.Common.HtmlIsHtmlRegex.test(data)) {
        //if session timed out, then redirect to log on
        if (AI.Common.HtmlIsLogonPageRegex.test(data)) {
            if (onTimeout) onTimeout();
            location.href = "/Index.aspx";
            return;
        }
        //if error on Problem page, test here
        if (AI.Common.HtmlIsProblemPageRegex.test(data)) {
            if (onError) onError(data);
            location.href = "/Error";
            return;
        }
        //(only for dev) json data not returned so its probably debug error page
        //make this dev-frindly by doing ajax insert-close panel around error message.
        Sys.Debug.trace("Error in returned data: " + data);
        return;
    }

    //deserialise data
    var jsonData = Sys.Serialization.JavaScriptSerializer.deserialize(data);

    //if faultexception then logout {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
    if (jsonData.Message && jsonData.ExceptionType) {
        if (onFault) onFault(jsonData);
        if (jsonData.Message == "Authentication failed.") {
            location.href = "/Index.aspx";
        }
    }

    return jsonData;
};
AI.Common.ShowProgress = function(show, name, left, top) {
    //todo: fix with css instead of img
    var el = $('#progress_' + name);
    if (show) {
        if (el.length == 0)
            el = $(document).append('<div id="progress_' + name + '" class="pad10 brd white hidden" style="position:absolute;"><img src="/assets/images/common/progress.gif" /></div>');
        if (el.length != 0) {
            if (left) el.css('left', left + 'px');
            if (top) el.css('top', top + 'px');
            el.removeClass('hidden');
        }
    } else {
        if (el.length != 0) {
            if (left) //in hide option left is passed as 'true' to kill the instance altogether
                el.remove();
            else //retain the instance
                el.addClass('hidden');
        }
    }
}
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

