﻿function getJqueryObjectByServerControlId(id) {
    return $("[id$='" + id + "']");
}

function getElementByServerControlId(id) {
    var array = getJqueryObjectByServerControlId(id);
    return array.length > 0 ? array[0] : null;
}

function getScrollBarWidth() {
    var inner = document.createElement('p');
    inner.style.width = "100%";
    inner.style.height = "200px";
    var outer = document.createElement('div');
    outer.style.position = "absolute";
    outer.style.top = "0px";
    outer.style.left = "0px";
    outer.style.visibility = "hidden";
    outer.style.width = "200px";
    outer.style.height = "150px";
    outer.style.overflow = "hidden";
    outer.appendChild(inner);

    document.body.appendChild(outer);
    var w1 = inner.offsetWidth;
    outer.style.overflow = 'scroll';
    var w2 = inner.offsetWidth;
    if (w1 == w2) w2 = outer.clientWidth;

    document.body.removeChild(outer);

    return (w1 - w2);
};

function getPageHeight() {
    return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

function deSelect() {
    if (document.selection && document.selection.createRange().text) document.selection.empty();
    else if (!document.all && window.getSelection()) window.getSelection().removeAllRanges();
}

function isEncapsulationRowAlt(element) {
    return $(element).closest('tr').is('.alt-row')
}

function calculateColorOverlay(foregroundColor, backgroundColor, opacity) {
    if (foregroundColor.substr(0, 1) == '#') foregroundColor = foregroundColor.substr(1);
    if (backgroundColor.substr(0, 1) == '#') backgroundColor = backgroundColor.substr(1);
    
    function calcColorPart(f, b, o) {
        return parseInt(b + (f - b) * o / 100);
    }
    return '#' +
           decimalToHex(calcColorPart(hexToDecimal(foregroundColor.substr(0, 2)), hexToDecimal(backgroundColor.substr(0, 2)), opacity), 2) +
           decimalToHex(calcColorPart(hexToDecimal(foregroundColor.substr(2, 2)), hexToDecimal(backgroundColor.substr(2, 2)), opacity), 2) +
           decimalToHex(calcColorPart(hexToDecimal(foregroundColor.substr(4, 2)), hexToDecimal(backgroundColor.substr(4, 2)), opacity), 2);
}

function decimalToHex(d, padding) {
    var hex = Number(d).toString(16);
    padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
    while (hex.length < padding) {
        hex = "0" + hex;
    }
    return hex;
}

function hexToDecimal(h) {
    return parseInt(h, 16);
}

function urlEncode(value) {
    var o = ''; var x = 0; value = value.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < value.length) {
        var m = r.exec(value.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } 
        else {
            if (value[x] == ' ') o += '+'; else {
                var d = value.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        }
    } return o;
}

function urlDecode(value) {
    var o = value; var binVal, t; var r = /(%[^%]{2})/;
    while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
        b = parseInt(m[1].substr(1), 16);
        t = String.fromCharCode(b); o = o.replace(m[1], t);
    } return o;
}

function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

function stopEvent(e) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

function fadeTo(element, value) {
    if (value > 1) value /= 100;
    $(element).fadeTo('fast', value);
}

function isNullOrEmpty(string) {
    return string == null || string === '';
}

function jumpToElement(id) {
    if (getElementViewArea(id) < 1) window.location = String(window.location).replace(/\#.*$/, '') + '#' + id;
}

function getElementViewArea(id) {
    //-- get necessary viewport dimensions
    var winHeight = $(window).height(),
	    winTop = self.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop,
	    winBottom = winHeight + winTop;

    //-- get element top offset and height
    var elTop = $('#' + id).offset().top,
		elHeight = parseInt($('#' + id).css('height')),
		elBottom = elTop + elHeight,
		hiddenTop = 0,
		hiddenBottom = 0;

    //-- get percentage of unviewable area
    if (elTop < winTop)             //-- area above the viewport
        hiddenTop = winTop - elTop;
    if (elBottom > winBottom)       //-- area below the viewport
        hiddenBottom = elBottom - winBottom;

    return 1 - ((hiddenTop + hiddenBottom) / elHeight);
}

function getRootPages() {
    var PAGES = '/Pages/'
    var result = '';
    var index = location.pathname.indexOf(PAGES);
    if (index > 0) {
        var count = location.pathname.count('/', index + PAGES.length);
        for (var i = 0; i < count; i++) result += '../';
    }
    return result;
}

function setCookie(name, value, expiry) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiry);
    var c_value = escape(value) + ((expiry == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = name + "=" + c_value;
}

function getCookie(name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == name) {
            return unescape(y);
        }
    }
}


