﻿/* 
====================================================================
    Javascript Library For TELERIK Control Functions
    v1.1, 9/2006
====================================================================
*/   


/* Rad Ajax Manager*/

function raiseAjaxEvent(ajaxManager, argument)   
{   
    window[ajaxManager].ajaxRequest(argument);  
} 

function raiseAjaxEventWithTarget(ajaxManager, targetControl, argument)   
{   
    window[ajaxManager].ajaxRequestWithTarget(targetControl, argument);  
} 

/* Rad Panelbar */

function hidePanelScrollbars(id)
{
    //var panel = document.getElementById(id).parentNode.lastChild.getElementsByTagName("UL")[0];
    //panel.style.overflow = "hidden";
    //alert('id: ' + id + ' panel: ' + panel);
//    var current = panel;

//    do
//    {
//        current = current.firstChild;
//        if (current.nodeType != 1)
//            current = current.nextSibling;

//        current.style.height = "100%";
//    }
//    while (current.className != "rpTemplate")
}

/* Rad Menu */

/* Rad Treeview */

function ExpandNode(tree, nodeValue)  
{  
    var node = tree.findNodeByValue(nodeValue);  
    
    if (node != null){  
        var parentNode = node;
        while (parentNode != null)
        {
            
            //alert('Parent: ' + parentNode.get_text());
            if (parentNode != node.get_treeView()) {
                parentNode.expand();
                parentNode = parentNode.get_parent();
            }
            else {
                parentNode=null;
            }
            
        }   
    }

}  
        
//        function ExpandNode(tree, nodeValue) {
//    var node = tree.FindNodeByValue(nodeValue);
//    
//    if (node != null) {
//        if (node.Parent) {
//            ExpandNode(tree, node.Parent.Value)
//        }
//        else {
//            if (node.Category != "Root") {
//               //alert('Expanding Node2: ' + node.Value + ' cat: ' + node.Category);
//               node.Expand();
//            }
//        }
//    }
//}

function CheckAncestor(node1, node2)  
{  
    var node = node1;
    for (var i=0;i<node.get_nodes().length;i++){
        if (node.get_nodes()[i].get_value() == node2.get_value()) {
            return true;
        }
        if (node.get_nodes()[i].get_nodes().length > 0) {
            return CheckAncestor(node.get_nodes()[i], node2);
        }
    }
    return false;
}  
        
//        function ExpandNode(tree, nodeValue) {
//    var node = tree.FindNodeByValue(nodeValue);
//    
//    if (node != null) {
//        if (node.Parent) {
//            ExpandNode(tree, node.Parent.Value)
//        }
//        else {
//            if (node.Category != "Root") {
//               //alert('Expanding Node2: ' + node.Value + ' cat: ' + node.Category);
//               node.Expand();
//            }
//        }
//    }
//}

function CollapseNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.collapse();
}

function ToggleNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.toggle();
}

function HighlightNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    //alert('Highlighting: ' + tree + ' Node: ' + node.Value);
    node.highlight();
}

function SelectNode(tree, nodeValue) {
   // alert('SelectTree ' + tree + ' NodeValue ' + nodeValue);
    var node = tree.findNodeByValue(nodeValue);
    //alert('Selected' + ' ' + node)
    node.select();
}

function UnselectNode(tree, nodeValue) {
   // alert('UNselectTree ' + tree + ' NodeValue ' + nodeValue);
    var node = tree.findNodeByValue(nodeValue);
   //  alert('UnSelected' + ' ' + node)
   node.unselect();
}

function EnableNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.enable();
}

function DisableNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.disable();
}

function CheckNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.check();
}

function UncheckNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.uncheck();
}

function StartEditNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.startEdit();
}

function EndEditNode(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.endEdit();
}

function NodeScrollIntoView(tree, nodeValue) {
    var node = tree.findNodeByValue(nodeValue);
    node.scrollIntoView();
}

/* Rad Window */


function getSelectedWindow(){
    var oManager = get_WindowManager();
    return oManager.getActiveWindow();        
}

function getParentRadWindow()   
{   
    var oWindow = null;   
    if (window.radWindow) oWindow = window.radWindow;   
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;   
    return oWindow;   
}  

function openRadWindow(url, name) {
    return openRadWindowPositioned(window, url, name, 100, 100, null, null, null)
}

function openRadWindow(windowObject, url, name) {
    return openRadWindowPositioned(windowObject, url, name, 100, 100, null, null, null)
}

function openRadWindow(windowObject, url, name, closeAfterDuration) {
    return openRadWindowPositioned(windowObject, url, name, 100, 100, null, null, closeAfterDuration)
}

function openRadWindow(windowObject, url, name, width, height) {
    return openRadWindowPositioned(windowObject, url, name, width, height, null, null, null)
}

function openRadWindow(windowObject, url, name, width, height, closeAfterDuration) {
    return openRadWindowPositioned(windowObject, url, name, width, height, null, null, closeAfterDuration)
}

function openRadWindowPositioned(windowObject, url, name, width, height, xPos, yPos, closeAfterDuration) {
    if (url.indexOf('?') < 0) {
        url += '?rnd=' + Math.random(); ;
    }
    else {
        url += '&rnd=' + Math.random();
    }
    var oWin = windowObject.radopen(url, name);
    oWin.setActive(true);
    
    if (screen.availWidth < width) {
        width = screen.availWidth;
    }

    if (screen.availHeight < height) {
        height = screen.availHeight;
    }
    
    resizeRadWindow(oWin, width, height);
    if (xPos != null && yPos != null) {
        oWin.moveTo(xPos, yPos);
    }
    else {
        oWin.center();
    }
    if (closeAfterDuration != null) {
        window.setTimeout(function() {   
            oWin.close();
            }, closeAfterDuration);
    }
    return oWin;
}

function closeActiveWindow() {
    var oWin=getParentRadWindow();
    oWin.close();
}

function refreshRadOpener() {
    var oWin=getParentRadWindow();
    oWin.BrowserWindow.location.reload();
}

function resizeRadWindow(oWin, width, height) {
    if (!oWin) oWin = getSelectedWindow();            
    oWin.setSize(width, height);    
}

function resizeParentRadWindow(width, height) {
    var oWin=getParentRadWindow()
    oWin.setSize(width, height);
}

function sizeToFit()
{
    var oWin = getParentRadWindow();
    oWin.autoSize();
}

function refreshRadOpener() {
    var oWin=getParentRadWindow();
    oWin.BrowserWindow.location.reload();
}

function redirectRadOpener(newUrl) {
    var oWin=getParentRadWindow();
    oWin.BrowserWindow.document.location.href = newUrl;
}



function callRadOpenerFunction(functionName, args) {
    var oWin = getParentRadWindow();
    
    //if (!(oWin.BrowserWindow[functionName] == null)) {
        //alert('Owin: ' + oWin + ' func: ' + functionName + ' args: ' + args + ' a: ' + oWin.BrowserWindow[functionName] + ' b: ' + typeof(oWin.BrowserWindow[functionName]))
        if (oWin.BrowserWindow[functionName] && typeof(oWin.BrowserWindow[functionName]) == "function")
	    {
	        oWin.BrowserWindow[functionName](args);
        }
//    }
//    else {
//        
//        var func = functionName + '(\'' + args + '\')';
//        alert('func: ' + func)
//        var winObj = top;
//        if (winObj.execScript) {
//            winObj.execScript(func, 'javascript');
//            return null; 
//        }
//        winObj.eval(func);
//    }
    //var oWin3 = window.opener;
     //alert('Owin3: ' + oWin3 + ' func: ' + functionName + ' args: ' + args + ' a: ' + oWin3[functionName] + ' b: ' + typeof(oWin3[functionName]))
    
}

function SetWindowStatus(text) {
    var oWin = getParentRadWindow();
    oWin.set_status(text);
}

function confirmCallBack(arg)
{
    alert("Confirm returned the following result: " + arg);
    return arg;
}

function promptCallBack(arg)
{
    alert ("Prompt returned the following result: " + arg);
    return arg;
}

function RadAlertAndRedirect(msg, width, height, url) {
    function redirect(oWin, arg) {
        window.location.href = url;
    }

    var oWin = top.radalert(msg, width, height);
    oWin.add_close(redirect);
}

////Replace old radconfirm with a changed version.   
//        var oldConfirm = radconfirm;   
//  
//        window.radconfirm = function(text, mozEvent)   
//        {   
//            var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually   
//            //Cancel the event   
//            ev.cancelBubble = true;   
//            ev.returnValue = false;   
//            if (ev.stopPropagation) ev.stopPropagation();   
//            if (ev.preventDefault) ev.preventDefault();   
//               
//            //Determine who is the caller   
//            var callerObj = ev.srcElement ? ev.srcElement : ev.target;   
//  
//            //Call the original radconfirm and pass it all necessary parameters   
//            if (callerObj)    
//            {   
//                //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again.   
//                var callBackFn = function (arg)   
//                {              
//                    if (arg)   
//                    {              
//                        callerObj["onclick"] = "";             
//                        if (callerObj.click) callerObj.click(); //Works fine every time in IE, but does not work for links in Moz   
//                        else if (callerObj.tagName == "A") //We assume it is a link button!   
//                        {                                                          
//                            try  
//                            {   
//                                eval(callerObj.href)   
//                            }   
//                            catch(e){}   
//                        }   
//                    }   
//                }          
//                oldConfirm(text, callBackFn, 300, 100, null, null);      
//            }   
//            return false;   
//        }  

/* Rad Grid */

//function GridRowSelected(grid) {
//    return (grid.MasterTableView.SelectedRows.length>0) 
//}

/* Rad Grid Promethius*/

var selGrid = Array;
 
function GridRowSelected(grid) {
   return (grid.get_masterTableView().get_selectedItems().length>0) 
}

function CancelNonInputSelect(sender, args)   
{   
        
    var e = args.get_domEvent();
    //IE - srcElement, Others - target
    var targetElement = e.srcElement || e.target;
    
    //is the clicked element an input checkbox? <input type="checkbox"...>
    if(targetElement.tagName.toLowerCase() != "input" && 
        (!targetElement.type || targetElement.type.toLowerCase() != "checkbox"))// && currentClickEvent)
    {                    
    
        //cancel the event
        args.set_cancel(true);
    }
}   


var currentRow, movedRow = null;
    
function RowCreated(row)
{
    row.Control.RowIndex = row.RealIndex;
    var mouseDownHandler = function(e)
	{
		if (!e)
			var e = window.event;

		if (!currentRow)
		{
			currentRow = document.createElement("div");
			currentRow.innerHTML = "<table><tbody><tr>"+row.Control.innerHTML+"</tr></tbody><table>";
			document.body.appendChild(currentRow);
			currentRow.style.position = "absolute";
			currentRow.style.display = "none";
			
			movedRow = row;
		}
		
		ClearDocumentEvents();				
	};
	var mouseUpHandler = function(e)
	{
		if (!e)
			var e = window.event;

		if (currentRow)
		{
			if (movedRow && currentRow.style.display != "none")
			{				
			    var targetElement = e.srcElement ? e.srcElement : e.target;				    
			    var droppedRowIndex;
			    while(typeof(droppedRowIndex) == "undefined")
			    {
			        droppedRowIndex = targetElement.parentNode.RowIndex;
			        targetElement = targetElement.parentNode;	//in case of controls in the row			        
			    }
			    
                ReorderRows(movedRow.RealIndex, droppedRowIndex);
			}
			document.body.removeChild(currentRow);
			currentRow = null;
			movedRow = null;
		}
		RestoreDocumentEvents();
	};
	var mouseMoveHandler = function(e)
	{
		if (!e)
			var e = window.event;
		if (currentRow)
		{
			currentRow.style.display = "";
			currentRow.style.top =  e.clientY + 
									document.documentElement.scrollTop + 
									document.body.scrollTop + 3 + "px";

			currentRow.style.left = e.clientX + 
									document.documentElement.scrollLeft + 
									document.body.scrollLeft + 3 + "px";
		}
		
	};
	
	if (row.Control.attachEvent)
	{
		row.Control.attachEvent("onmousedown", mouseDownHandler);
		document.body.attachEvent("onmouseup", mouseUpHandler);
		document.body.attachEvent("onmousemove", mouseMoveHandler);
	}

	if (row.Control.addEventListener)
	{
		row.Control.addEventListener("mousedown", mouseDownHandler, true);
		document.body.addEventListener("mouseup", mouseUpHandler, true);
		document.body.addEventListener("mousemove", mouseMoveHandler, true);
	}
}

function ClearDocumentEvents()
{
	if (document.onmousedown != this.mouseDownHandler)
	{
		this.documentOnMouseDown = document.onmousedown;
	}

	if (document.onselectstart != this.selectStartHandler)
	{
		this.documentOnSelectStart = document.onselectstart;
	}

	this.mouseDownHandler = function(e){return false;};
	this.selectStartHandler = function(){return false;};

	document.onmousedown = this.mouseDownHandler;
	document.onselectstart = this.selectStartHandler;

};

function RestoreDocumentEvents()
{
	if ((typeof(this.documentOnMouseDown) == "function") &&
		(document.onmousedown != this.mouseDownHandler))
	{
		document.onmousedown = this.documentOnMouseDown;
	}
	else
	{
		document.onmousedown = "";
	}
	
	if ((typeof(this.documentOnSelectStart) == "function") &&
		(document.onselectstart != this.selectStartHandler))
	{
		document.onselectstart = this.documentOnSelectStart;
	}
	else
	{
		document.onselectstart = "";
	}
};
    



  






