/*
 * PopupWindows.js
 * Javascripts used for producing popup windows
 *
 * @copyright Copyright 2007-2009 Tenchi Marketing Pte Ltd
 * @version $Id: PopupWindows.js 2010-04-16
 */

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if(document.getElementById) {
  window.alert = function(txt) {
    createCustomWindow(txt,"alert");
  }
}

// create infomation popup
function info(txt,action) {
  createCustomWindow(txt,"info",action);
}

// create message popup
function msg(txt) {
  createCustomWindow(txt,"msg");
}

function createCustomWindow(txt,type,action) {
  // constants to define the title of the alert and button text.
  var TITLE = "";
  var BUTTON_TEXT = "Ok";
  var ID = "";

  // setup variable value
  if(type == "alert") {
    TITLE = "Alert!";
    ID = "alertBox";
  } else if(type == "info") {
    TITLE = "Information";
    ID = "infoBox";
  } else {
    ID = "msgBox";
  }

  // shortcut reference to the document object
  d = document;

  // if the modalContainer object already exists in the DOM, bail out.
  if(d.getElementById("modalContainer")) return;

  // create the modalContainer div as a child of the BODY element
  mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  mObj.id = "modalContainer";
  // make sure its as tall as it needs to be to overlay all the content on the page
  mObj.style.height = document.documentElement.scrollHeight + "px";

  // create the DIV that will be the alert 
  alertObj = mObj.appendChild(d.createElement("div"));
  alertObj.id = ID;
  // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  if(!is_IE()) alertObj.setAttribute('style','position: fixed;');
  if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  // center the alert box
  alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

  // create an H1 element as the title bar
  if(type == "alert" || type == "info") {	
    h1 = alertObj.appendChild(d.createElement("h1"));
    h1.appendChild(d.createTextNode(TITLE));
  }

  // create a paragraph element to contain the txt argument
  msg = alertObj.appendChild(d.createElement("p"));
  msg.innerHTML = txt.replaceAll("\n","<br>");
	
  // create an anchor element to use as the confirmation button.
  if(type == "alert" || type == "info") {	
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "closeBtn";
    btn.appendChild(d.createTextNode(BUTTON_TEXT));
    btn.href = "#";

    // set up the onclick event to remove the alert when the anchor is clicked
    if(type == "alert") btn.onclick = function() { removeCustomWindow(); if(action="submit") d.form[0].submit(); else return false; }
    else if(type == "info") btn.onclick = function() { removeCustomWindow(); if(action="submit") d.forms[0].submit(); else return true; }
  }
}

// removes the custom window from the DOM
function removeCustomWindow() {
  document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

function wait(msecs)
{
  var start = new Date().getTime();
  var cur = start
  while(cur - start < msecs) {
    cur = new Date().getTime();
  }
}

// is IE?
// @return	boolean		Running IE?
function is_IE()
{
	return (navigator.appName=='Microsoft Internet Explorer');
}

