/**
 * a javascript popup class
 *
 * @author	Damien Merenne <dmerenne@ytcompany.com>
 */
    
function Popup(url, name)
{
  this._url = url;
  this._name = name;
  this._window = null;
 
  this._resizable = false;
  this._status = false;
  this._toolbar = false;
  this._location = false;
  this._menubar = false;
  this._fullscreen = false;
  this._height = null;
  this._width = null;
  this._top = null;
  this._left = null;
  this._scrollbars = "auto";
  this._content = "";

  this.url = function (u)
  {
    if (u != null) {
      this._url = u;
    }
    return this._url;
  }

  /**
   * the size of the window
   */
  this.size = function (w, h) 
  {
    if (w != null || h != null) {
      this._width = w;
      this._height = h;
      if (this._window != null)
	this._window.resizeTo(w, h);
    }
    return {'width': this._width, 'height': this._height };
  };

  /**
   * set the position of the window
   */
  this.position = function (l, t) 
  {
    if (l != null || t != null) {
      this._top = t;
      this._left = l;
      if (this._window != null)
	this._window.moveTo(l, t);
    }
    return {'left': this._left, 'top': this._top };
  };

  /**
   * center the popup
   */
  this.center = function ()
  {
    var l; var t;
    if (!document.all) {
      // Center on the main window.
      l = window.screenX + ((window.outerWidth - this._width) / 2);
      t = window.screenY + ((window.outerHeight - this._height) / 2);
    } else {
      // The best we can do is center in screen.
     l = (screen.width - this._width) / 2;
     t = (screen.height - this._height) / 2;
    }
    this.position(l, t);
  }

  /**
   * set resizable
   */
  this.resizable = function (b)
  {
    if (b != null)
      this._resizable = b;
    return this._resizable;
  };

  /**
   * set status
   */
  this.status = function (b)
  {
    if (b != null)
      this._status = b;
    return this._status;
  };
   
  /**
   * set toolbar
   */
  this.toolbar = function (b)
  {
    if (b != null)
      this._toolbar = b;
    return this._toolbar;
  };

  /**
   * set location
   */
  this.location = function (b)
  {
    if (b != null)
      this._location = b;
    return this._location;
  };
 
  /**
   * set menubar
   */
  this.menubar = function (b)
  {
    if (b != null)
      this._menubar = b;
    return this._menubar;
  };
 
  /**
   * set fullscreen
   */
  this.fullscreen = function (b)
  {
    if (b != null)
      this._fullscreen = b;
    return this._fullscreen;
  };

  /**
   * set the scrollbar   
   */
  this.scrollbars = function (b)
  {
    if (b != null)
      this._scrollbars = b;
    return this._scrollbars;
  };

  /**
   * show the popup
   */
  this.open = function ()
  {
    var param = "";

    if (this._resizable)
      param += "resizable=yes";
    else
      param += "resizable=no";

    if (this._status)
      param += ",status=yes";
    else
      param += ",status=no";

    if (this._toolbar)
      param += ",toolbar=yes";
    else
      param += ",toolbar=no";

    if (this._location)
      param += ",location=yes";
    else
      param += ",location=no";

    if (this._menubar)
      param += ",menubar=yes";
    else
      param += ",menubar=no";

    if (this._fullscreen)
      param += ",fullscreen=yes";
    else
      param += ",fullscreen=no";

    if (this._scrollbars == "auto")    
      param += ",scrollbars=auto";
    else if(this._scrollbars)
      param += ",scrollbars=yes";
    else
      param += ",scrollbars=no";
    
    if (this._width != null)
      param += ",width=" + this._width;
    if (this._height != null)
      param += ",height=" + this._height;
    if (this._left != null)
      param += ",left=" + this._left;
    if (this._top != null)
      param += ",top=" + this._top;

    this._window = window.open(this._url, this._name, param);
    if (this._content != "")
      this.write(this._content);
  };

  /**
   * show a pre-filled popup
   */
  this.openDefault = function (title, head, body)
  {
    this._url = 'javascript: '
      +'    document.open();'
      +'    document.write("<html><head>");'
      if (title)
	this._url += '      document.write("<title>' + title + '</title>");';
    if (head)
      this._url += '      document.write("'+head+'");';
    this._url +='    document.write("</head>");'
      +'    document.write("<body>");';
    if (body)
      this._url += '      document.write("'+body+'");';
    if (this._content != "")
      this._url += '      document.write("'+this._content +'");';
    this._url += '    document.write("</body></html>");'
      +'    document.close();';
    this.open();
  }

  /**
   * close the popup
   */
  this.close = function () 
  {
    this._window.close();
    this._window = null;
  };

  /**
   * write to the window
   */
  this.write = function (s)
  {
    if (this._window == null) {
      this._content += s;
    } 
    else {
      if (this._window.document.body)
	this._window.document.body.innerHTML += s;
      else
	this._window.document.write(s);
    }

  };

  /**
   * return the window
   */
  this.window = function (s)
  {
    return this._window;
  };


  /**
   * return the document
   */
  this.document = function (s)
  {
    return this._window.document;
  };

  /**
   * return the body
   */
  this.body = function (s)
  {
    return this._window;
  };

  /**
   * return an element by it's id
   */
  this.getElementById = function (id)
  {
    return this._window.document.getElementById(id);
  }
}

function PopupModal(url, name, ret, freeze)
{
  this.temp = Popup;
  this.temp(url, name);
  delete this.temp;

  this._returnFunc = ret;
  this._freeze = freeze;

  this.open = function () {
    var param = "";
    
    if (this._resizable)
      param += "resizable=yes";
    else
      param += "resizable=no";
    
    if (this._status)
      param += ",status=yes";
    else
      param += ",status=no";
    
    if (this._toolbar)
      param += ",toolbar=yes";
    else
      param += ",toolbar=no";

    if (this._location)
      param += ",location=yes";
    else
      param += ",location=no";

    if (this._menubar)
      param += ",menubar=yes";
    else
      param += ",menubar=no";

    if (this._fullscreen)
      param += ",fullscreen=yes";
    else
      param += ",fullscreen=no";
    
    if (this._width != null)
      param += ",width=" + this._width;
    if (this._height != null)
      param += ",height=" + this._height;
    if (this._left != null)
      param += ",left=" + this._left;
    if (this._top != null)
      param += ",top=" + this._top;
    
    this._window = window.open(this._url, this._name, param);
    this._window.focus();

  };    

  /**
   * show a pre-filled popup
   */
  this.openDefault = function (title, head, body)
  {
    this._url = 'javascript: '
      +'document.open();'
      +'document.write("<html><head>");'
      if (title)
	this._url += 'document.write("<title>' + title + '</title>");';
    if (head)
      this._url += 'document.write("'+head+'");';
    this._url +='document.write("</head>");'
      +'document.write("<body onload=\\\"window.opener.'+this._name+'.blockEvents();\\\" ");'
      +'document.write("onunload=\\\"if (window.opener && !window.opener.closed && window.opener.'+this._name+'._returnFunc) window.opener.'+this._name+'._returnFunc(returnValue);window.opener.'+this._name+'.unblockEvents();\\\" ");'  
      +'document.write(">");';
    if (body)
      this._url += 'document.write("'+body+'");';
    if (this._content != "")
      this._url += 'document.write("'+this._content +'");';
    this._url += 'document.write("</body></html>");'
      +'document.close();';
    this.open();
  }
  
  /**
   * block events
   */
  this.blockEvents = function () {
    var popup = this;
    if (!document.all) {
      window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
      window.onclick = function (event) { popup._window.focus() };
      window.onfocus = function (event) { popup._window.focus() };
    } else {
      window.document.body.onclick = function (event) { popup._window.focus() };
      this._disableForms();
      if (this._freeze) {
	window.document.body.style.filter = 'filter: progid:DXImageTransform.Microsoft.Alpha(opacity=30)';
	window.document.body.filters(0).enabled = 1;
      }
    }
    window.onfocus = function (event) {  popup._checkModal(); };
  };
  
  /**
   * unblock events
   */
  this.unblockEvents = function () {
    if (!document.all) {
      window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
      window.onclick = null;
      window.onfocus = null;
    } else {
      window.onclick = null;
      window.document.body.onclick = null;
      this._enableForms();
      if (this._freeze) {
	window.document.body.filters(0).enabled = 0;
      }
    }
    window.onfocus = null;
  };
  
  /**
   * check modal
   */
  this._checkModal = function () {
    if (this._window && !this._window.closed) {
      this._window.focus();
    }
  };
  
  /**
   * capture events
   */
  this._deadend = function () 
  {
    if (this._window && !this._window.closed) {
      this._window.focus();
      return false;
    }
  };

  this.ieLinkClicks = null;

  this._disableForms = function () 
  {
    this.ieLinkClicks = new Array();
    var popup = this;
    for (var h = 0; h < frames.length; h++) {
      for (var i = 0; i < frames[h].document.forms.length; i++) {
	for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
	  frames[h].document.forms[i].elements[j].disabled = true;
	}
      }
      ieLinkClicks[h] = new Array();
      for (i = 0; i < frames[h].document.links.length; i++) {
	this.ieLinkClicks[h][i] = frames[h].document.links[i].onclick;
	frames[h].document.links[i].onclick = function () { popup._deadend(); };
      }
    }
  };

  this._enableForms = function () {
    for (var h = 0; h < frames.length; h++) {
      for (var i = 0; i < frames[h].document.forms.length; i++) {
	for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
	  frames[h].document.forms[i].elements[j].disabled = false;
	}
      }
      for (i = 0; i < frames[h].document.links.length; i++) {
	frames[h].document.links[i].onclick = ieLinkClicks[h][i];
      }
    }
  }
}


function agb(url) {
    var pop = new Popup(url, 'agb');
    pop.size(400, 500);
    pop.center();
    pop.resizable(false);
    pop.open();
}
