﻿function OpenWindow(name, url)
{
    var manager = GetRadWindowManager();
    var oWindow = manager.GetWindowByName(name);

    if (oWindow == undefined)
        throw 'Unable to find window named: ' + name;
    
    oWindow.SetUrl(url);
    
    oWindow.Show();
}

function OnClientShow(sender, eventArgs)
{
    var windowWidth = getWindowWidth();
    var childWindowWith = sender.GetWidth();
    
    var left = (windowWidth - childWindowWith) / 2;

    sender.MoveTo(left, GetVisibleTop() + 20);
}

function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  
  return myWidth;
}

function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  
  return myHeight;
}


function GetVisibleTop()
{
    var top = 0;
    
    var appVersion = navigator.appVersion;
    if (appVersion.indexOf('MSIE') > 1)
    {
        if (document.compatMode=="CSS1Compat")
        {
            top = document.documentElement.scrollTop;
        }
        else
        {
            top = document.body.scrollTop;
        }
    }
    else
    {
        top = window.pageYOffset;
    }
    
    return top;
}

function GetVisibleLeft()
{
    var left = 0;
    
    var appVersion = navigator.appVersion;
    if (appVersion.indexOf('MSIE') > 1)
    {
        if (document.compatMode=="CSS1Compat")
        {
            left = document.documentElement.scrollLeft;
        }
        else
        {
            left = document.body.scrollLeft;
        }
    }
    else
    {
        left = window.pageXOffset;
    }
    
    return left;
}

function CloseWindow(result, pageRedirect, arg)       
{           
    var args = new Object();
    args.Result = result;
    args.PageRedirect = pageRedirect;
    args.Argument = arg;

    GetRadWindow().close(args);       //closes the window       
}

function CloseWindowOnly()
{
    GetRadWindow().close();
}

function CloseWindowOneArg(arg) {
    GetRadWindow().close(arg);
}

function GetRadWindow()   //Get reference to window    
{       
    var oWindow = null;       
    if (window.radWindow)       
         oWindow = window.radWindow;       
    else if (window.frameElement.radWindow)       
         oWindow = window.frameElement.radWindow;       
    return oWindow;       
}       

function OnCloseRedirect(sender, args)
{
    var arg = args.get_argument();
    
    if (arg)
    {
        if (arg.Result == "ok")
        {
            var redirectArgument = arg.Argument;
            var appendedRedirectArgument = "";
            
            if (redirectArgument != "")
            {
                var queryString = document.location.search.substring(1);
                
                if (queryString != "")
                    queryString = "&" + queryString;
                
                appendedRedirectArgument = "?redirectArgument=" + redirectArgument + queryString;
            }
            else
                appendedRedirectArgument = document.location.search;
            
            window.location.href = arg.PageRedirect + appendedRedirectArgument;
        }
    }
}
