// JavaScript Document

var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var heightArray = new Array();
var http = false;
var loadstatustext = "Requesting...";
var dragobj = null;
var moveobj = null;

function falsefunc() { return false; } // used to block cascading events

function init()
{
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();          

	if (window.XMLHttpRequest)
	{// if Mozilla, IE7, Safari etc
			http = new XMLHttpRequest()
	}
		else if (window.ActiveXObject){ // if IE6 or below
			try {
			http = new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e){
				try{
				http = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
			}
	}
  
}

function getAJAXContent(thisURL,thisDiv) {
// display wait message
document.getElementById(thisDiv).innerHTML = loadstatustext;

  http.open("GET", thisURL, true);
  http.onreadystatechange=function() {
	if(http.readyState == 4) {
	  document.getElementById(thisDiv).innerHTML = http.responseText;
	}
  }
  http.send(null);
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
	if (e.pageX || e.pageY)
	{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
	  mousex = e.pageX;
	  mousey = e.pageY;
	  algor = '[e.pageX]';
	  if (e.clientX || e.clientY) algor += ' [e.clientX] '
	}
	else if (e.clientX || e.clientY)
	{ // works on IE6,FF,Moz,Opera7
	  mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

	  algor = '[e.clientX]';
	  if (e.pageX || e.pageY) algor += ' [e.pageX] '
	}  
  }
}

function update(e)
{
  getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grab(context,thehead)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = document.getElementById(context);
  headobj = thehead;
  dragobj.style.zIndex = 10; // move it to the top
  document.onmousemove = drag;
  document.onmouseup = drop;
  grabx = mousex;
  graby = mousey;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  update();
}

function drag(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {
	elex = orix + (mousex-grabx);
	eley = oriy + (mousey-graby);
	dragobj.style.position = "absolute";
	dragobj.style.left = (elex).toString(10) + 'px';
	dragobj.style.top  = (eley).toString(10) + 'px';
  }
  update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop()
{
  if (dragobj)
  {
	dragobj.style.zIndex = 10;
	dragobj = null;
  }
  
  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}

function grab2(context,thenub)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = document.getElementById(context);
  moveobj = document.getElementById(thenub);
  dragobj.style.zIndex = 10; // move it to the top
  document.onmousemove = drag2;
  document.onmouseup = drop;
  grabx = mousex;
  graby = mousey;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  update();
}

function drag2(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {
	elex = mousex - orix;
	eley = mousey - oriy;
	dragobj.style.position = "absolute";
	if(elex > 100)
	{
		dragobj.style.width = (elex).toString(10) + 'px';
	}
	if (eley > 55)
	{
		dragobj.style.height  = (eley).toString(10) + 'px';
		heightArray[dragobj.id] = dragobj.style.height;
	}

	moveobj.style.position = 'absolute';
	moveobj.style.top = (dragobj.style.height.replace(/\D/g,"") - 10) + 'px';
	moveobj.style.left = (dragobj.style.width.replace(/\D/g,"") - 10) + 'px';

  }
  update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}


function minmax(thediv,context){
	var cnt = document.getElementById(context);            
	
	if (document.getElementById(thediv).style.display == 'block')
	{
		document.getElementById(thediv).style.display = 'none';
		cnt.style.height = '22px';
	}
	else
	{
		document.getElementById(thediv).style.display = 'block';
		if (heightArray[context])
			cnt.style.height = heightArray[context];
		else
			cnt.style.height = 'auto';
	}
}
