
var wmRequest = null;
var yuiResponseWindows = new Array();
var yuiResponseWindowCallbacks = new Array();

function eolwmunload() {
  //alert('unload: '+wmid);
  wmRequest = initWMRequester(wmRequest);
  var start = new Date();
  wmRequest.open("GET", "wm.php?close="+wmid+"&ts="+start.getTime(), false);
  wmRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  wmRequest.onreadystatechange = function() {}
  wmRequest.send("close="+wmid);
  return true;
}

function initWMRequester(arequester) {
  if (arequester != null && arequester.readyState != 0 && arequester.readyState != 4) { 
    arequester.abort(); 
  } 
  arequester = null;    
  try { 
    arequester = new XMLHttpRequest();
    if(arequester.overrideMimeType){        
      arequester.overrideMimeType("text/xml");
    }
  } catch (error) { 
    try { 
      arequester = new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (error) { 
      arequester = null; 
    } 
  }
  return arequester;
}

function openWindow(windowlocation, windowname, windowproperties) {
  if (typeof handleAllCheckBoxControls != 'undefined') {
    handleAllCheckBoxControls();
  }
  var timestamp = Number(new Date());
  //var windowuid = "uid_" + timestamp;
  if (windowproperties == 'YUI') {
    return openYUIWindow(windowlocation, windowname, windowproperties);
  }
  if (windowproperties == null) {
    windowproperties = "resizable,width=900,height=600,scrollbars=yes";
  }
  if (windowname != null) {
    windowname = windowname.replace(/[\s\{\}-]/gi, '_');
  }
  var newwin = window.open(windowlocation + '&windowname=' + windowname, windowname, windowproperties);
  newwin.focus();
  return false;
}

function openYUIWindow(windowlocation, windowname, windowproperties) {
  createYUIResponseWindow(windowname);
  yuiResponseWindows[windowname].show();
  var callback = yuiResponseWindowCallbacks[windowname];
  YAHOO.util.Connect.asyncRequest('GET', windowlocation, callback, null);
  return false;
}

/**
 * Reload the root window of a popup stack (f.i. to show applied changes)
 */
function reloadRootWindow() {
  var root = window;
  while(root.opener) {
    root = root.opener;
  }
  root.location.reload();
}

/**
 * Reload the parent window of a popup  (f.i. to show applied changes)
 */
function reloadParentWindow() {
  if (window.opener) {
    window.opener.location.reload();
  }
}

function parentWindowFKCallback(fknewid, id, functionname) {
  if (window.opener) {
    callback = 'window.opener.xajax_'+fknewid + '_' + functionname+'("'+id+'");';
    try {
      eval(callback);
    }
    catch(err) {
      //callback not defined. 
    }
  }
}

function reloadRecursiveParents() {
  var root = window;
  while (root.opener) {
    root = root.opener;
    root.location.reload();
  }
}

/**
 *  returns the value of the requested get variable.
 */
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);  
  var vars = query.split("&");  
  for (var i=0;i<vars.length;i++) {  
    var pair = vars[i].split("=");    
    if (pair[0] == variable) {    
      return pair[1];    
    }  
  }   
  alert('Query Variable ' + variable + ' not found');
}

function showMessageInYUIWindow(windowname, msg) {
   createYUIResponseWindow(windowname);
   yuiResponseWindows[windowname].show();
   yuiResponseWindows[windowname].setBody(msg);
   yuiResponseWindows[windowname].center();
}

function createYUIResponseWindow(windowname) {
  if (windowname == null) {
    windowname = "Default";
  }
  if (document.getElementById(windowname)) {
    yuiResponseWindows[windowname].destroy();
  }
  
  // Create container div
  if (document.getElementById("yuiresponsewindows") == null) {
    var c = document.createElement('div');
    c.id = 'yuiresponsewindows';
    c.className = 'yui-skin-sam';
    var parentel = document.forms[formname];
    if (parentel == null) {
      parentel = document.body;
    }
    parentel.insertBefore(c, parentel.firstChild);
  }
  
  // Instantiate a Panel from markup
  yuiResponseWindows[windowname] = new YAHOO.widget.Panel(windowname, { 
    visible:false,
    resizeable:true
  } );
  yuiResponseWindows[windowname].setBody('<img src="lib/yui/assets/skins/sam/ajax-loader.gif" />');
  yuiResponseWindows[windowname].render("yuiresponsewindows");
  yuiResponseWindows[windowname].center();
  yuiResponseWindowCallbacks[windowname] = {
    cache:false,
    success: function(o) {
      yuiResponseWindows[windowname].setBody(o.responseText);
      yuiResponseWindows[windowname].center();
    },
    failure: function(o) {
      alert("AJAX doesn't work");
    }
  }
}

function doCallBack(id) {
  var uri={
    get:function(id){
      var res = location.search.match('(?:\\?|\&)' + id + '=([^\&]*)');
      if (res != null) {
        return unescape(res[1].replace(/\+/g, " "));
      }
    }
  }
  if (window.opener && !window.opener.closed) {
    var fckmode = uri.get('fckmode');
    if ((window.opener.SetUrl || window.opener.CKEDITOR) && fckmode) {
      if (fckmode == 'file') {
        url = '/faccess.php?id='+id;
      } else {
        url = '/getimage.php?id='+id+'&imagetable=un_files';
      }
      if (window.opener.SetUrl) {
        // FCKEditor
        window.opener.SetUrl(encodeURI(url));
      } else {
        // CKEditor
        var CKEditorFuncNum = uri.get('CKEditorFuncNum');
        window.opener.CKEDITOR.tools.callFunction(CKEditorFuncNum, encodeURI(url));
      }
    } else {
      var fknewid = uri.get('fknewid');
      parentWindowFKCallback(fknewid, id, 'selectCallback');
    }
  }
  window.close();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


