
function ngWindowInclude(title, url, x, y) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }

  element = document.createElement("span");
  if (!element) {
   alert("Bad id " + id + 
    "passed to clientSideInclude." +
    "You need a div or span element " +
    "with this id in your page.");
   return;
  }

  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }

  new ngWindow(title, element, x, y);
}

function ngWindow(title, oContent, x, y)
{
	var divs = element.getElementsByTagName("DIV");
	w = divs.item(0).style.width + "px";

	// save arguments
	this.title = title;
	this.oContent = oContent;

	// initialization	
	this.mx = 0;
	this.my = 0;
	this.w = w;
	
	// create table for window with title-bar and content
	this.oTable = document.createElement("table");
        this.oTable.className = "ngWindowTableStyle";
	this.oTable.border = 0;
	this.oTable.width =  w + "px";

	// set the position of the window
	this.oTable.style.position = "absolute";
	this.oTable.style.left = x + "px";
	this.oTable.style.top = y + "px";

	// set background to white (default is transparent)
	this.oTable.style.backgroundColor = "gray";

	// link from the table to the ngWindow object
	this.oTable.ngWindow = this;

	// if anywhere in the table are is clicked, bring the window to front.
	this.oTable.onmousedown = ngWindow.prototype.onBringToFront;

	// append to document body
	document.body.appendChild(this.oTable);

	// add row for title bar
	var oTR = this.oTable.insertRow(0);
	oTR.className = "ngWindowTitleStyle";

	// title	
	this.oTDt = oTR.insertCell(0);
	this.oTDt.background = "imagenes/ngBackgroundTitle.jpg";
	this.oTDt.innerHTML = '<img src="imagenes/topo.gif" width="3" height="7"> ' + title;
	this.oTDt.className = "ngWindowTitleStyle";
	this.oTDt.ngWindow = this;
	this.oTDt.onmousedown = ngWindow.prototype.tdOnMouseDown;
	
	// minimize
	this.oTDm = oTR.insertCell(1);
	this.oTDm.className = "ngWindowCellButtonStyle";
	this.oTDm.innerHTML = '<img src="imagenes/ngBtnMin.gif" width="14" height="14">';
	this.oTDm.width = "14px";
	this.oTDm.onmousedown = ngWindow.prototype.onMinimize;
	this.oTDm.ngWindow = this;
	
	// close
	this.oTDc = oTR.insertCell(2);
	this.oTDc.className = "ngWindowCellButtonStyle";
	this.oTDc.innerHTML = '<img src="imagenes/ngBtnCls.gif" width="14" height="14">';
	this.oTDc.ngWindow = this;	
	this.oTDc.onmousedown = ngWindow.prototype.onClose;
	
	// add row for window content
	// a single cell the same width as the title bar row
	oTR = this.oTable.insertRow(1);
	var oTDo = oTR.insertCell(0);
	oTDo.className = "ngWindowCellContentStyle";
	oTDo.colSpan = 3;
	oTDo.appendChild(oContent);

	// rehacer tamaņos de caption
	this.oTDt.width = w + "px";
	this.oTDm.width = "14px";
	this.oTDc.width = "14px";
}

ngWindow.prototype.onBringToFront = function()
{
	this.ngWindow.bringToFront();
}

ngWindow.prototype.bringToFront = function()
{
	// if not already the last child of the document.body, make it so
	if ( document.body.childNodes[document.body.childNodes.length-1] !== this.oTable )
	{
		// move to bottom of document
		document.body.appendChild(this.oTable);
	}
}

ngWindow.prototype.tdOnMouseDown = function()
{
	this.ngWindow.onMouseDown();
}

ngWindow.prototype.onMouseDown = function()
{
	// record that an onmousedown has just occurred
	this.bDown = true;
	
	// link from body to this ngWindow object
	document.body.ngWindow = this;

	// save body mouse handlers
	this.saveMouseMove = document.body.onmousemove;
	this.saveMouseUp = document.body.onmouseup;

	// set new handlers.
	document.body.onmousemove = ngWindow.prototype.bodyOnMouseMove;
	document.body.onmouseup = ngWindow.prototype.bodyOnMouseUp;
}

ngWindow.prototype.bodyOnMouseMove = function(evt)
{
	var e = window.event ? window.event : evt;
	this.ngWindow.onMouseMove(e);
}

ngWindow.prototype.onMouseMove = function(evt)
{
	// if mouse not down, stop the move (for IE only)
	if ( (document.all) && !(evt.button & 1) )
	{
		this.onMouseUp();
		return;
	}
	if ( this.bDown )
	{
		this.dx = parseInt(this.oTable.style.left, 10) - evt.clientX;
		this.dy = parseInt(this.oTable.style.top, 10) - evt.clientY;
		this.bDown = false;
	}
	else
	{
		this.oTable.style.left = Math.max((this.dx + evt.clientX),0) + "px";
		this.oTable.style.top = Math.max((this.dy + evt.clientY),0) + "px";
	}
}

ngWindow.prototype.bodyOnMouseUp = function()
{
	this.ngWindow.onMouseUp();
}

ngWindow.prototype.onMouseUp = function()
{
	document.body.onmouseup = this.saveMouseUp;
	document.body.onmousemove = this.saveMouseMove;
	document.body.ngWindow = null;
}

ngWindow.prototype.onMinimize = function()
{
	this.ngWindow.minimize();
}

ngWindow.prototype.minimize = function()
{
	// hide the content
	this.oContent.style.visibility = "hidden";
	this.oContent.style.position = "absolute";
	this.oContent.style.left = "0px";
	this.oContent.style.top = "0px";
	document.body.appendChild(this.oContent);
	
	this.oTable.deleteRow(1);
	
	// save current position
	this.saveX = this.oTable.style.left;
	this.saveY = this.oTable.style.top;
	
	// get the "window bar"
	if ( !window.ngWindowBar )
	{
		window.ngWindowBar = document.createElement("span");
		document.body.appendChild(window.ngWindowBar);
	}
	
	window.ngWindowBar.appendChild(this.oTable);
	this.oTable.style.position = "static";
	this.oTable.style.left = "0px";
	this.oTable.style.top = "0px";
	
	this.oTDm.innerHTML = '<img src="imagenes/ngBtnMax.gif" width="14" height="14">';
	this.oTDm.onmousedown = ngWindow.prototype.onMaximize;

	// rehacer tamaņos de caption
	this.width = "140px";
	this.oTable.width = "132px";
	this.oTDt.width = "100px";
	this.oTDm.width = "14px";
	this.oTDc.width = "14px";
}

ngWindow.prototype.onMaximize = function()
{
	this.ngWindow.maximize();
}

ngWindow.prototype.maximize = function()
{
	document.body.appendChild(this.oTable);
	this.oTable.style.position = "absolute";
	
	this.oTable.style.left = this.saveX;
	this.oTable.style.top = this.saveY;
	
	// add the content again.
	oTR = this.oTable.insertRow(1);
	oTD = oTR.insertCell(0);
	oTD.className = "ngWindowCellContentStyle";
	oTD.colSpan = 3;

	oTD.appendChild(this.oContent);

	this.oContent.style.position = "static";
	this.oContent.style.visibility = "visible";
	
	this.oTDm.innerHTML = '<img src="imagenes/ngBtnMin.gif" width="14" height="14">';
	this.oTDm.onmousedown = ngWindow.prototype.onMinimize;

	// rehacer tamaņos de caption
	this.oTDt.width = this.w + "px";
	this.oTDm.width = "14px";
	this.oTDc.width = "14px";
}

ngWindow.prototype.onMaximize = function()
{
	this.ngWindow.maximize();
}

ngWindow.prototype.close = function()
{	
	// remove content from browser document
	this.oContent.parentNode.removeChild(this.oContent);
	
	// remove from browser document
	this.oTable.parentNode.removeChild(this.oTable);
}

ngWindow.prototype.onClose = function()
{
	this.ngWindow.close();
}

function ngWindowObjText(o)
{
	var s = new Array();
	s.push('{<br>');
	for ( var i in o )
		s.push("   " + i + ": " + o[i] + "<br>");
	s.push('}');
	return s.join('');
}

function ngWindowDebug(msg)
{
	document.getElementById('debug').innerHTML += msg + "<br>";
}