//convertion to radwindow ajax 10/15/09
//see http://www.telerik.com/help/aspnet-ajax/window_overviewchanges.html for bug fixes

var lastClientCloseFunction = null;

// client side pop-in style window support
function WindowClosed(){
  //Override this function to capture window events
}

//pop-in style window
//height & width ignored if maximized is true
//specify a winName name to create a unique window, use same winName to reuse window
function WindowOpen(url, title, width, height, clientCloseFunction, maximized, winName, status) {
  var errMsg = '';
  if ((winName == null || winName == '')) {
    winName = 'winMain';
  }
  if(url==null){errMsg='A \'url\' must be provided to the \'WindowOpen\' call.';}
  //show any errors
  if(errMsg.length > 0){
    alert(errMsg);
    return;
  }
  //set defaults
  if (title == null) { title = url; }
  if (status == null) { status = ''; }
  if(width==null){width=400;}
  if(height==null){height=600;}
  if (clientCloseFunction == '') { clientCloseFunction = null; }
  if(maximized==null){maximized=false;}
  
  //Get window object, set properties and open
  var winMngr = GetRadWindowManager();
  var win;
  if(winMngr !=null)
    win = winMngr.GetWindowByName(winName);

  //make sure we are in the browser
  var brow = new MaxWindowSize(width, height);

  if (win == null) {
    win = radopen(url);
  }
  //set window properties  
  win.SetUrl(url);
  win.SetTitle(title);
  win.SetSize(brow.Width, brow.Height);

  //assign on close callback and remove previous one
  if (clientCloseFunction != null) {
    if (typeof clientCloseFunction == 'function') {
      if (lastClientCloseFunction != null) {
        win.remove_close(lastClientCloseFunction);
      }
      win.add_close(clientCloseFunction);
      lastClientCloseFunction = clientCloseFunction;
    }
    else
      alert('The clientCloseFunction parameter passed to WindowOpen is not a function.  Be sure to not include () in the call and do not enclose the function in \'\' characters.');
  }
  

  win.show();
  if (maximized) {
    win.Maximize();
  }

  if (status != '') {
    win.set_status(status);
  }
}

function GetRadWindow() {
  var oWindow = null;
  if (window.radWindow)
    oWindow = window.radWindow;
  else if (window != null && window.frameElement != null && window.frameElement.radWindow != null && window.frameElement.radWindow)
    oWindow = window.frameElement.radWindow;
  else
    oWindow = null;
  return oWindow;
}    


function Close(){
  var win = GetRadWindow();
  if (win != null) {
    win.close();
  }
}

//return parent window
function ParentWindow(){
  var win = GetRadWindow();
  if (win != null) {
    return win.BrowserWindow;
  } else {
    return null;
  }
} 

//refresh with post back
function RepostParent() {
  var parentWin = ParentWindow();
  if (parentWin != null)
    parentWin.location.reload(true);
}

//re-load url (no postback)
function RefreshParent(refreshLocalOnNoParent) {
  if (refreshLocalOnNoParent == null)
    refreshLocalOnNoParent = false;

  var parentWin = ParentWindow();
  if (parentWin != null) {
    parentWin.location.href = parentWin.location.href;
  } else {
    if (refreshLocalOnNoParent)
      Refresh();
  }
}

function RefreshParentAndClose(refreshLocalOnNoParent) {
  RefreshParent(refreshLocalOnNoParent)
  Close();
}

function RepostParentAndClose(){
  RepostParent();
  Close();
}

/*----------------------------
  Local Window Methods
----------------------------*/
function Repost(){
  document.location.reload(true);
}
//
function Refresh() {
  document.location.href=document.location.href;
}

