﻿/// <reference path="jquery-1.2.6.min.js"/>

function closeEditToolTop(sender, args) {
    location.reload(true);
}

// jQuery bootstrap
$(document).ready(function () {
    Timeout.setup();
    Timeout.SetEventListners(); // to detect mouse clicks on the page and readjust the timeout
});

// Timeout manager
if (!Timeout) var Timeout = {};
Timeout = {

    interval: 5 * 60 * 1000,
    logoutUrl: "/Logout.aspx",
    defaultUrl: "/ScreenSaver.aspx",
    timeoutID: -1,

    SetEventListners: function () {
        $(document).mouseup(function () {
            clearTimeout(Timeout.timeoutID);
            Timeout.setup();
        });
    },

    logout: function () {
        window.location.pathname = Timeout.logoutUrl;
    },

    setup: function () {

        var redirect = true;

        if (typeof (window['timeoutInterval']) != "undefined") {
            Timeout.interval = parseInt(timeoutInterval);
            Timeout.interval = Timeout.interval * 60 * 1000;
        }
        else {
            // if the interval is not set do nothing.
            redirect = false;
        }

        if (typeof (window['timeoutLogoutUrl']) != "undefined") {
            Timeout.logoutUrl = timeoutLogoutUrl;
        }
        if (typeof (window['timeoutDefaultUrl']) != "undefined") {
            Timeout.defaultUrl = timeoutDefaultUrl;
        }

        if (redirect == true) {
            if (!(window.location.pathname.match(Timeout.logoutUrl)) && !(window.location.pathname.match(Timeout.defaultUrl))) {
                Timeout.timeoutID = window.setTimeout(Timeout.logout, Timeout.interval);
            }
        }

    },

    __typeName: "Timeout"
};


function validateEmail(email) {
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    str = email
    if (str.match(emailRegEx)) {
        return true
    } else {

        return false;
    }
}


jQuery.fn.extend({
    isInteger: function isInteger(s) {
        var i;

        if (isempty(s))
            if (isInteger.arguments.length == 1) return 0;
            else return (isInteger.arguments[1] == true);

        for (i = 0; i < s.length; i++) {
            var c = s.charAt(i);

            if (!isdigit(c)) return false;
        }

        return true;
    }
});

function isempty(s) {
    return ((s == null) || (s.length == 0))
}

function isdigit(c) {
    return ((c >= "0") && (c <= "9"))
}
function callservice(servicename, json, datatype) {
    var dtype = "json";
    if (datatype)
        dtype = datatype;

    var data = null;
    $.ajax({
        url: "/Services/" + servicename,
        global: false,
        type: "POST",
        data: (json),
        async: false,
        dataType: dtype,
        success: function (d) {
            data = d;
            return data
        }
    }
);
    return data;
}

function showError(error) {
    $(".sysMsg:last").fadeIn(1000);
    $(".sysMsg:last > p").html(error);
    $(".sysMsg:last").fadeOut(5000);
}

function EncodeSpecialCharacters(text) {
    while (1) {
        if (text.search("&") > -1 || text.search("\"") > -1 || text.search("'") > -1 || text.search("<") > -1 || text.search(">") > -1) {
            text = text.replace("&", "amp;");
            text = text.replace("\"", "quot;");
            text = text.replace("'", "apos;");
            text = text.replace("<", "lt;");
            text = text.replace(">", "gt;");
        }
        else {
            return text;
        }

    }

}
