// Short Cut Functions

function $(id)
{
	return document.getElementById(id);	
}

function $t(tname)
{
	return document.getElementsByTagName(tname);
}

// Event Wireup Functions

function addEvent(elm, evType, fn, useCapture)
{
	if (elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent)
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		elm['on' + evType] = fn;
	}
}

function getElement(e)
{
	var el;
	if (window.event && window.event.srcElement)
	{
		el = window.event.srcElement;
	}
	if (e && e.target)
	{
		el = e.target;
	}
	
	if (!el)
	{
		return false;
	}
	else
	{
		return el;
	}
}

// Globals

var bodyTag;
var placeHolder;
var pageMask;

var popUp;
var popUpHead;
var popUpBtnClose;
var popUpBtnCloseTxt;
var popUpBody;

var contentRequest;

// Pop Up Functions

function initPopUps()
{
	createPlaceHolder();
	createPageMask();
}

function createPlaceHolder()
{
	bodyTag = $t("body")[0];
	placeHolder = document.createElement("div");
	placeHolder.id = "place-holder";
	bodyTag.appendChild(placeHolder);
}

function createPageMask()
{
	pageMask = document.createElement("div");
	pageMask.id = "page-mask";
	pageMask.style.position = "absolute";
	pageMask.style.left = "0px";
	pageMask.style.top = "0px";
	pageMask.style.width = "100%";
	pageMask.style.height = "100%";
	pageMask.style.zIndex = "1000";
	pageMask.style.display = "none";
	placeHolder.appendChild(pageMask);
}

function createPopUpWindow(id, width, height, bgColor, border, content)
{
	popUp = document.createElement("div");
	popUp.id = id;
	popUp.style.position = "absolute";
	popUp.style.left = "50%";
	popUp.style.top = "50%";
	popUp.style.width = width + "px";
	popUp.style.height = height + "px";
	popUp.style.margin = "-" + (height/2) + "px 0px 0px -" + (width/2) + "px";
	popUp.style.zIndex = "1001";
	popUp.style.backgroundColor = bgColor;
	popUp.style.border = border;
	
	popUpHead = document.createElement("div");
	popUpHead.id = id + "-head";
	popUpHead.style.padding = "5px";
	popUpHead.style.textAlign = "right";
	
	popUp.appendChild(popUpHead);
	
	popUpBtnClose = document.createElement("a");
	popUpBtnClose.setAttribute("href", "#");
	addEvent(popUpBtnClose, 'click', killMe, false);
	addEvent(pageMask, 'click', killPopUp, false);
	
	popUpHead.appendChild(popUpBtnClose);
	
	popUpBtnCloseTxt = document.createTextNode("Close");
	popUpBtnClose.appendChild(popUpBtnCloseTxt);
	
	popUpBody = document.createElement("div");
	popUpBody.id = id + "-body";
	popUpBody.style.padding = "5px";
	
	popUp.appendChild(popUpBody);
	
	pageMask.style.display = "block";
	placeHolder.appendChild(popUp);
	
	getPopUpContent(content);
}

function killMe(e)
{
	var popUpWindow = getElement(e).parentNode.parentNode;
	placeHolder.removeChild(popUpWindow);
	pageMask.style.display = "none";
	return false;
}

function killPopUp(e)
{
	placeHolder.removeChild(popUp);
	pageMask.style.display = "none";
	return false;
}

function getPopUpContent(content)
{
	contentRequest = createXMLHttpRequest();
	contentRequest.open("GET", content, true);
	contentRequest.onreadystatechange = handle_contentRequest_rsc;
	contentRequest.send(null);
}

function handle_contentRequest_rsc()
{
	if (contentRequest.readyState == 4)
	{
		if (contentRequest.status == 200)
		{
			popUpBody.innerHTML = contentRequest.responseText;
		}
		else
		{
			throw new Error("The XMLHttpRequest was unsuccessful:\n" + contentRequest.status + " :: " + contentRequest.statusText);
		}
	}
}

// Ajax Function

function createXMLHttpRequest() {
  var types = [
    'Microsoft.XMLHTTP',
    'MSXML2.XMLHTTP.5.0',
    'MSXML2.XMLHTTP.4.0',
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP'
   ];

  for (var i = 0; i < types.length; i++) {
    try {
      return new ActiveXObject(types[i]);
    } catch(e) {}
  }

  try {
    return new XMLHttpRequest();
  } catch(e) { }

  return false; // XMLHttpRequest not supported
}
