﻿//--------------------------------------------------------------------------------

String.prototype.IsNullOrEmptyOrUndefined = function() {
    if (this == undefined || this == null || this == '')
        return true;
    else
        return false;
}

function IsNullOrEmptyOrUndefined(object) {
    if (object == undefined || object == null || object == '')
        return true;
    else
        return false;
}

function Format(text) {
    for (i = 1; i < arguments.length; i++) {
        text = text.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return text;
}

function CreateCookie(cookieName, cookieValue) {
    CreateCookie(cookieName, cookieValue, 0);
}

function CreateCookie(cookieName, cookieValue, expiresInDays) {
    if (expiresInDays > 0) {
        var expiresDate = new Date();
        expiresDate.setTime(expiresDate.getTime() + (expiresInDays * 24 * 60 * 60 * 1000));
        document.cookie = Format('{0}={1}; expires={2}; path=/', cookieName, cookieValue, expiresDate.toGMTString());
    }
    else
        document.cookie = Format('{0}={1}; path=/', cookieName, cookieValue);
}

function EraseCookie(cookieName) {
    var expiresDate = new Date();

    document.cookie = Format('{0}={1}; expires={2}; path=/', cookieName, '', expiresDate.toGMTString());
}

function ReadCookie(cookieName) {
    var nameEQ = cookieName + "=";
    var cookies = document.cookie.split(';');

    for (var i = 0; i < cookies.length; i++) {
        while (cookies[i].charAt(0) == ' ')
            cookies[i] = cookies[i].substring(1, cookies[i].length);

        if (cookies[i].indexOf(nameEQ) == 0)
            return cookies[i].substring(nameEQ.length, cookies[i].length);
    }
    return null;
}
//--------------------------------------------------------------------------------

var INTERVAL_HANDLER_ID;
function CheckCookie(cookieName, intervalInMillisecinds) {
    if (IsNullOrEmptyOrUndefined(INTERVAL_HANDLER_ID))
        INTERVAL_HANDLER_ID = setInterval('CheckCookie(\'' + cookieName + '\',' + intervalInMillisecinds + ');', intervalInMillisecinds);

    var cookieValue = ReadCookie(cookieName);

    switch (cookieValue) {
        case 'ReloadWithoutQuery':
            EraseCookie(cookieName);
            clearInterval(INTERVAL_HANDLER_ID);
            INTERVAL_HANDLER_ID = null;
            window.location.replace(document.location.href.split("?")[0]);
            break;

        case 'Reload':
            EraseCookie(cookieName);
            clearInterval(INTERVAL_HANDLER_ID);
            INTERVAL_HANDLER_ID = null;
            window.location.replace(document.location.href);
            break;

        case 'Alert':
            EraseCookie(cookieName);
            clearInterval(INTERVAL_HANDLER_ID);
            INTERVAL_HANDLER_ID = null;
            alert('ok');
            break;

        case 'Close':
            EraseCookie(cookieName);
            clearInterval(INTERVAL_HANDLER_ID);
            INTERVAL_HANDLER_ID = null;
            window.location.replace(document.location.href);
            break;

        default:
    }
}
//--------------------------------------------------------------------------------

function IfKeyEnterToClickElement(btnClientID, e) {
    if (e.keyCode == 13) {

        var btnHref = $("#" + btnClientID).attr('href')
        if (btnHref != undefined) {
            document.location.href = btnHref;
        }
        else {
            var btnName = $("#" + btnClientID).attr('name')
            if (btnName != undefined) {
                document.location.href = "javascript:__doPostBack('" + btnName + "','')"
            }
        }

        return StopPropagation(e);
    }
}

function StopPropagation(event) {
    // cancel the default submit
    //event.cancelBubble is supported by IE - this will kill the bubbling process.
    event.cancelBubble = true;
    event.returnValue = false;

    //event.stopPropagation works only in Firefox.
    if (event.stopPropagation) {
        event.stopPropagation();
        event.preventDefault();
    }
    return false;
}

function CheckMaxLength(btnClientID, event, maxLength) {
    if ($(btnClientID).val().length >= maxLength && event.keyCode != 8 && event.keyCode != 46) {
        return StopPropagation(event);
    }
}