
var gLogger = LogFactory.getLog("global.js");

var xmlHttpRequestErrorPage = "/BrowserError.html";

// *********************************************************************
// Function to get an XML HTTP Object
// *********************************************************************
function getHTTPObject() {
    gLogger.debug("Enter getHTTPObject()");
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        window.location.replace(xmlHttpRequestErrorPage);
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      gLogger.warn("XMLHttpRequest not supported!");
      xmlhttp = false;
      window.location.replace(xmlHttpRequestErrorPage);
    }
  }
    gLogger.debug("Exit getHTTPObject()");
  return xmlhttp;
}

/*
 * Finds the source HTML Element for an event e.
 * From www.quirksmode.org
 */
function getEventSource(e) {
    gLogger.debug("Enter getEventSource()");
	var targ;
    if (!e) var e = window.event;
	if (e.target) {
        targ = e.target;
    } else if (e.srcElement) {
        targ = e.srcElement;
    }
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
    gLogger.debug("Exit getEventSource()");
    return targ;
}

/*
 * Function to format a number as US currency.
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
        num = num.substring(0,num.length-(4*i+3))+','+
              num.substring(num.length-(4*i+3));
    }
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// ***** Btn Rollover *****
function roll(imgName,imgState) {
   try{
      document.images[imgName].src = imgState
   }catch( e ){}
}
// ***********************************************************************

// ***** Preload Rollover Images ***************************************
function preloadImage( imageArray, imageSrc ) {
    var img = new Image();
    img.onerror = function() {
        alert("Image [" + imageSrc + "] not found!");
    }
    img.src = imageSrc;
    imageArray[ imageSrc ] = img;
}

// *********************************************************************

// *********************************************************************
// Function to create pop-up windows at a custom size.
// w = width
// h = height
// d = directories
// l = location
// m = menubar
// r = resizeable
// sc = scrollbars
// st = status
// t = toolbar
// EXAMPLE CALL: <a href="javascript:popUp('[URL]','[WINDOW NAME]',400,300,1,1,0,0,0,0,0);">LINK</a>
// *********************************************************************
function popUp(URL,name,w,h,d,l,m,r,sc,st,t) {
	var featureStr = "";
	featureStr = "width=" + w + ",height=" + h + ",directories=" + d + ",location=" + l + ",menubar=" + m + ",resizable=" + r + ",scrollbars=" + sc + ",status=" + st + ",toolbar=" + t;
	window.open(URL,name,featureStr);
}

// *********************************************************************
// Function to create pop-up window for Ballroom Floor Plans (ex: /AtlanticStation/events/Ballroom.do).
// w = width
// h = height
// d = directories
// l = location
// m = menubar
// r = resizeable
// sc = scrollbars
// st = status
// t = toolbar
// EXAMPLE CALL: <a href="javascript:popUp('/images/fp_ballroom_01.gif','[WINDOW NAME]',400,300,1,1,0,0,0,0,0);">LINK</a>
// *********************************************************************
function getkey(e)
{
if (window.event)
   return window.event.keyCode;
else if (e)
   return e.which;
else
   return null;
}

function checkChars(e, validChars)
{
var key, keychar;
key = getkey(e);
if (key == null) return true;

keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
checks = validChars.toLowerCase();

if (checks.indexOf(keychar) != -1)
	return true;

if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   return true;

return false;
}

function checkNumeric(e){
   return checkChars(e,'01234567890');
}