﻿function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;    
    else if (window.frameElement && window.frameElement.radWindow) oWindow = window.frameElement.radWindow;    
    else if ( GetRadWindowManager && GetRadWindowManager().GetActiveWindow ) oWindow = GetRadWindowManager().GetActiveWindow();    
    return oWindow;
} 

function rwFindParentSize(w) 
{ 
    if( w == null ) w = window;
    
    // Window dimensions: 
    if (w.innerWidth) { 
        theWidth=w.innerWidth; 
    } 
    else if (document.documentElement && document.documentElement.clientWidth) { 
        theWidth=w.document.documentElement.clientWidth; 
    } 
    else if (document.body) { 
        theWidth=w.document.body.clientWidth; 
    } 
    if (window.innerHeight) { 
        theHeight=w.innerHeight - 10; 
    } 
    else if (document.documentElement && document.documentElement.clientHeight) { 
        theHeight=w.document.documentElement.clientHeight; 
    } 
    else if (document.body) { 
        theHeight=w.document.body.clientHeight; 
    } 
} 

function rwResizeChild() 
{ 
    rwFindParentSize(); 
    //Getting rad window manager 
    if(GetRadWindowManager){
        var oManager = GetRadWindowManager(); 
        var oActive = oManager.GetActiveWindow(); 
        if (oActive)
        { 
            rwSetWindowMaxArea(oActive);
        } 
    }
} 

function rwSetWindowMaxArea(radWindow) 
{ 
    //Getting rad window manager 
    if(radWindow)
    {
        rwFindParentSize(); 
        if( typeof(rwMaxHeight) != 'undefined' && theHeight > rwMaxHeight ) theHeight = rwMaxHeight;
        if( typeof(rwMaxWidth)  != 'undefined' && theWidth  > rwMaxWidth )  theWidth  = rwMaxWidth;
        radWindow.set_height(theHeight - 30); 
        radWindow.set_width(theWidth - 40); 
        radWindow.center(); 
    }
} 

function rwResizeToVGA() 
{ 
    //Getting rad window manager 
    var oActive = GetRadWindow();
    if (oActive && oActive.isVisible()){ 
        oActive.set_height(480); 
        oActive.set_width(640); 
        oActive.center(); 
    } 
} 

function rwResizeWindow(w,h) 
{ 
    //Getting rad window manager 
    var oActive = GetRadWindow();
    if (oActive && oActive.isVisible()){ 
        oActive.set_height(h); 
        oActive.set_width(w); 
        oActive.center(); 
    } 
} 

function rwResizeContentArea(id)
{
    var area = document.getElementById(id);
    if( area ) 
    {
        rwFindParentSize();
        area.style.height = theHeight - rwGetTop(area);
    }
}

function rwFitToWindow(radWindow) 
{ 
    rwResizeChild(); 
    // Inicializa Resize da Página            
    setTimeout( function() { window.onresize=rwResizeChild; }, 200);
} 

function rwNormalWindow(radWindow) 
{ 
    // Inicializa Resize da Página
    setTimeout( function() {window.onresize="";}, 200);
} 

function rwOpenRadWindow(clientID, url, width, height, title, status)
{
    var oWnd = $find(clientID);
    if( oWnd )
    {                
        oWnd.setUrl(url); 
        if (width && height) 
        {
            if( width == -1 && height == -1) 
                oWnd.maximize();
            else
                oWnd.setSize(width, height);       
        }     
        if (title) oWnd.set_title(title);
        if( status) oWnd.set_status(status);
        oWnd.show();  
        return oWnd;       
    }
    else
        alert('rwOpenRadWindow: WindowID ' + clientID + ' not found!');
}

function rwOpenMaximized(url, windowName)
{
  var oManager = null;    
  var p = self;
  while( 1 )
  {
    if( p.GetRadWindowManager ) oManager = p.GetRadWindowManager;
	
    if( (p == p.parent && p.parent == p.parent.parent) ) break;

    p = p.parent;
  }
  if( oManager == null )
  {
    alert('Cannot find GetRadWindowManager()');
  }
  else
  {
      var oWnd = oManager().open(url, windowName);
      if( oWnd ) oWnd.maximize();
      return oWnd;
  }
  return null;
}

function rwOpenWindow(url, windowName, width, height, title, status)
{
  var oManager = null;    
  var p = self;
  while( 1 )
  {
    if( p.GetRadWindowManager ) oManager = p.GetRadWindowManager;
	
    if( (p == p.parent && p.parent == p.parent.parent) ) break;

    p = p.parent;
  }
  if( oManager == null )
  {
    alert('Cannot find GetRadWindowManager()');
  }
  else
  {
    
      var oWnd = oManager().open(url, windowName);
      if( oWnd )
      {
        var oActive = oManager().getActiveWindow(); 
        if (oActive)
        { 
            if (width && height) oWnd.setSize(width, height);
            if (title) oWnd.set_title(title);
            if (status) oWnd.set_status(status);
            oActive.center(); 
        } 
      }
      return oWnd;
  }
  return null;
}

var rwExCurrentCTRL;
function rwExOpenWindow(ctrl, url, windowName, width, height, title, status)
{
    if( ctrl.processed == undefined )
	{
	    rwExCurrentCTRL = ctrl.id;
        rwOpenWindow(url, windowName, width, height, title, status)
    }
    else
        alert(hrefMessage);
    return false;
}

function rwExReturnAndCloseWindow(returnValue) 
{    

	var ctrl = top.document.getElementById(rwExCurrentCTRL);
	if( ctrl != null )
	{
		if( top.hrefMessage != "" ) ctrl.processed = true;
	
	    
		if( top.hideLineItem )
			ctrl.parentNode.parentNode.style.display = 'none';
		else if (top.isInsideTable)
		{
			ctrl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.background = top.markColor;
		}
		else
			ctrl.parentNode.parentNode.style.background = top.markColor;
		
		
		if( top.odlgCallBack ) top.odlgCallBack(ctrl, returnValue);
		
	}

	if( top.gPopupIsShown != null && top.gPopupIsShown == true ) 
		rwCloseWindow();
	else
	{
		rwReturnAndCloseWindow(returnValue);		
	}
}

function rwClearWindowArgs(radWindow)
{
    radWindow.arguments = '';
}
function rwCloseWindow()
{
	var oWindow = GetRadWindow();			
	if( oWindow ) oWindow.Close();
}
function rwReturnAndCloseWindow(value)
{
	var oWindow = GetRadWindow();			
	oWindow.arguments = value;
	oWindow.Close(value);
}
function rwCallBack(value)
{
    rwReturnAndCloseWindow(value)
//	var oWindow = GetRadWindow();
//	oWindow._invokeDialogCallBackFunction(value);
}
function rwRefreshParentPage()
{
    GetRadWindow().BrowserWindow.location.reload();
}
function rwCloseAndRefreshParentPage()
{
    GetRadWindow().BrowserWindow.location.reload();
    GetRadWindow().Close();
}
function rwGetTop(obj)
{
   return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+rwGetTop(obj.offsetParent) );
}

function rwNoScroll(radWindow)
{
    document.body.scroll = "no";
    document.body.style.overflow = "hidden";
} 

function rwRedirect(url)
{
    var win = GetRadWindow();
    win.SetUrl(url);
}

function rwSetSizeHeaderContentFooter(divH, divC, divF, hSize, cSize, fSize)
{
    var pospx;

    rwFindParentSize(); 

    var oFooter  = document.getElementById(divF);
    var oHeader  = document.getElementById(divH);
    var oContent = document.getElementById(divC);
    
    if( oFooter && fSize ) 
        oFooter.style.height = fSize;
    else if( oFooter )
    {
        fSize = oFooter.style.height;
      	pospx= fSize.indexOf("px");
	    fSize = parseInt(fSize.substr(0,pospx));
    }
        
    if( oHeader && hSize ) 
        oHeader.style.height = hSize;
    else if( oHeader )
    {
        hSize = oFooter.style.height;
      	pospx= hSize.indexOf("px");
	    hSize = parseInt(hSize.substr(0,pospx));
    }
    
    if( oContent && cSize ) 
        oContent.style.height = cSize;
    else if( oContent && oHeader && oFooter && hSize && fSize )
        oContent.style.height = theHeight - hSize - fSize - rwGetTop(oHeader);
    else if( oContent && oHeader && hSize )
        oContent.style.height = theHeight - hSize;
    else if( oContent && oFooter && fSize )
        oContent.style.height = theHeight - fSize;
}

/* Rad Ajax */
function raDisableControls(ajaxPanel, eventArgs)
{
	for (var i=0; i<document.forms[0].elements.length; i++)
	{			
		var obj = document.forms[0].elements[i];
		obj.disabled = true;
	}
}

function raEnableControls(ajaxPanel, eventArgs)
{					
	for (var i=0; i<document.forms[0].elements.length; i++)
	{		
		var obj = document.forms[0].elements[i];
		obj.disabled = false;
	}
}

function raPostBack(eventTarget, eventArgument)
{
    __doPostBack(eventTarget, eventArgument);
}


/* RadGRID */
function rgRowCreated(gridRow)
{
    if ((gridRow.ItemType == "Item") ||
        (gridRow.ItemType == "AlternatingItem"))
    {
        var cell = this.GetCellByColumnUniqueName(gridRow, "TemplateColumn");

        if (!cell)
            return;

        var checkBox = rgGetCheckBox(cell);

        if (!checkBox)
            return;

        gridRow.Selected ? checkBox.checked = true : checkBox.checked = false;

        var gridTable = this;
        checkBox.onclick = function()
        {
            gridTable.SelectRow(gridRow.Control, false);
        };
    }
}

function rgRowSelected(gridRow)
{
    var cell = this.GetCellByColumnUniqueName(gridRow, "TemplateColumn");

    if (!cell)
        return;

    var checkBox = rgGetCheckBox(cell);

    if (!checkBox)
        return;

    gridRow.Selected ? checkBox.checked = true : checkBox.checked = false;
}

function rgRowDeselected(gridRow)
{
    var cell = this.GetCellByColumnUniqueName(gridRow, "TemplateColumn");

    if (!cell)
        return;

    var checkBox = rgGetCheckBox(cell);

    if (!checkBox)
        return;

    gridRow.Selected ? checkBox.checked = true : checkBox.checked = false;
}

function rgGetCheckBox(control)
{
    if (!control)
        return;

    for (var i = 0; i < control.childNodes.length; i++)
    {
        if (!control.childNodes[i].tagName)
            continue;

        if ((control.childNodes[i].tagName.toLowerCase() == "input") &&
            (control.childNodes[i].type.toLowerCase() == "checkbox"))
        {
            return control.childNodes[i];
        }
    }
}

/* RadCalendar */
function rcPopupUpdating(dpID)
{         
   var dpWrapper    = document.getElementById(dpID).parentNode;                
   var dpOffsetTop  = rcGetElementPosition(dpID).y + dpWrapper.offsetHeight;
   var pageHeight   = document.body.offsetHeight;                               
   var calHeight    = rcGetCalendarHeight(dpID);
   if((dpOffsetTop + calHeight) > pageHeight)
   {
       shouldSkip = true;
       rcPopupAbove(dpID);                 
   }
   else
   {
       window[dpID].ShowPopup();
   }

   return false;            
}
    
function rcGetCalendarHeight(dpID)
{
    var datePicker = window[dpID]; 
    var popupElement = datePicker.GetPopupContainer();
    var dimensions = datePicker.GetElementDimensions(popupElement);
    return dimensions.height;
}

function rcGetElementPosition(dpID)
{
  var datePicker = window[dpID]; 
  var textBox = datePicker.GetTextBox();
  var position = datePicker.GetElementPosition(textBox);
  return position;
}

function rcPopupAbove(dpID)
{
    var datePicker = window[dpID];                
    var popupElementHeight = rcGetCalendarHeight(dpID);               
    var position = rcGetElementPosition(dpID);
    
    datePicker.ShowPopup(position.x, position.y - popupElementHeight);
}


/* RadTree */
function rtHasNodesChecked(tree)
{
    for(var i=0; i < tree.AllNodes.length; i++)
    {
        if( tree.AllNodes[i].Checked ) return true;
    }
    return false;
}

function rtUpdateChildrenNodes(nodes, checked)
{
    var i;
    for (i=0; i<nodes.length; i++)
    {
        if (checked)
            nodes[i].Check();
        else
            nodes[i].UnCheck();

        if (nodes[i].Nodes.length > 0) rtUpdateChildrenNodes(nodes[i].Nodes, checked);
    }
}

/* RadAjax */
function raRealPostBack(eventTarget, eventArgument, ajaxManager)
{
    $find(ajaxManager).__doPostBack(eventTarget, eventArgument);
}

