﻿//get all elements by classname
document.getElementsByClassName = function(cl) 
{
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) 
    {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
};

//trim all whitespace
function trim(str, chars) 
{
    return ltrim(rtrim(str, chars), chars);
}

//trim leading whitespace
function ltrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

//trim trailing whitespace
function rtrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//get browser width
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
//get browser height
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
//get scrollbar width
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
//get scrollbar height
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
//filter cross-browser result
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

//open new window
function OpenWindow(url, arguments, width, height, hidetooltip) {
    //open
    var oWnd = radopen(url, null);
    oWnd.setSize(width, height);
    
    //pass arguments to window
    if (arguments)
        oWnd.argument = arguments;
    
    //center window
    CenterWindow(oWnd);
    
    //hide active tooltip
    if (hidetooltip)
        HideActiveTooltip();
}

//center window
function CenterWindow(window) {
    var bounds = window.getWindowBounds();
    var X = bounds.x;
    var Y = f_scrollTop() + (bounds.height / 2);

    window.moveTo(X, Y);
}

//get current telerik window
function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}

//redirect timed out session (window) to parent page
function RedirectToParent() {
    var oWindow = GetRadWindow();
    oWindow.BrowserWindow.location.reload();
    oWindow.close();
}

//open date calendar
function ToggleDatePopup(popup) {
    $find(popup).showPopup();
}

function ResetPageMethodLocation() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

    PageMethods.set_path(sPage);
}