﻿
function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function appendQueryString(url, append) {
    return url + ((url.indexOf("?") != -1) ? (url.indexOf("?") != url.length - 1 ? "&" : "") : "?") + append;
}

RegExp.escape = function(text) {
    if (!arguments.callee.sRE) {
        var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
        arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
    }
    return text.replace(arguments.callee.sRE, '\\$1');
}




var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function absPath(url) {
    if (url.indexOf("http://") != -1) return url;
    var Loc = location.href;
    Loc = Loc.substring(0, Loc.lastIndexOf('/'));
    while (/^\.\./.test(url)) {
        Loc = Loc.substring(0, Loc.lastIndexOf('/'));
        url = url.substring(3);
    }
    return Loc + '/' + url;
}

function getOpenWindow() {
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow; //Will work in Moz in all cases, including classic dialog
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow; //IE (and Moz as well)
    return oWindow;
}


function decorateInputs() {
    
    // Form Decorator
    $("textarea, input[type=text], input[type=password]").each(
        function() {
            $(this).hover(function() {
                $(this).addClass("rhHover");
            }, function() {
                $(this).removeClass("rhHover");
            });
        }
    );
    $("textarea, input[type=text], input[type=password]").each(
    function(){
        $(this).focus(function() {
            $(this).addClass("rhFocused");
        });
    });

    $("textarea, input[type=text], input[type=password]").each(
    function() {
        $(this).blur(function() {
            $(this).removeClass("rhFocused");
        });
    });
    
}

var oldOverFlow;
var oldScroll;
function DisablePageScroll() {
    oldOverFlow = document.body.style.overflow;
    oldScroll = document.body.scroll;
    if (document.all) {
        document.body.scroll = "no";
        document.body.style.overflow = "hidden";
    }
    else {
        var oTop = document.body.scrollTop;
        document.body.style.overflow = "hidden";
        document.body.scrollTop = oTop;
    }
}

function EnablePageScroll() {    
    document.body.style.overflow = oldOverFlow;
    document.body.scroll = oldScroll;
}

function ScrollDimensions() {
    var dimensions = new Array(2);

    if (window.innerHeight && window.scrollMaxY) {
        dimensions[0] = document.body.scrollWidth;
        dimensions[1] = window.innerHeight + window.scrollMaxY;
    } else
        if (document.body.scrollHeight > document.body.offsetHeight) {
        dimensions[0] = document.body.scrollWidth;
        dimensions[1] = document.body.scrollHeight;
    } else {
        dimensions[0] = document.body.offsetWidth;
        dimensions[1] = document.body.offsetHeight;
    }

    return (dimensions);
}


// Document height/width getter:
function getDocDim(prop, m) {
    m = m || 'max';
    return Math[m](
            Math[m](document.body["scroll" + prop], document.documentElement["scroll" + prop]),
            Math[m](document.body["offset" + prop], document.documentElement["offset" + prop]),
            Math[m](document.body["client" + prop], document.documentElement["client" + prop])
	);
}


$.fn.centerElement = function() {
    var item = $(this);
   // if (item.css("display") != "none" && item.parent().css("display") != "none") {
        var xy = getScrollXY();             
        item.css("top", parseInt(xy[1] + (document.documentElement.clientHeight - item.height()) / 2));
        item.css("left", parseInt(xy[0] + (document.documentElement.clientWidth - item.width()) / 2));
   // } else {
     //   item.css("top", "-99999px");
   // }

}

$.fn.maxElement = function() {
    var item = $(this);
  
    item.width("100%");
    item.height(getDocDim("Height"));

}

