
var rexUSZip = /^[0-9]{5}(-[0-9]{4})?$/;
var rexCAZip = /^[A-Z][0-9][A-Z]\s*[0-9][A-Z][0-9]$/i;
var gKeyHandler = null;
var gClickHandler = null;
var gDefaultKeyHandler = null;
var gDefaultClickHandler = null;

function Null() {}

//---------------------------------------------------------
function MM_preloadImages() {
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}}

//---------------------------------------------------------
function stopEvents(e)
{
    e = e || window.event;
    Event.stop(e);
}

function stopEventPropagation(e) 
{
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
}

//---------------------------------------------------------
function reloadPage()
{
	try {
		window.location.reload(true);
	}
	catch(err) {
		document.open(window.location.href,"_self","",true);
	}
}

//---------------------------------------------------------
function redirectPage(sUrl)
{
	window.location.href = sUrl;
}

//---------------------------------------------------------
function cookiesEnabled()
{   
    var bEn = false;

    try
    {
        document.cookie = "testcookie"
        bEn = (document.cookie.indexOf("testcookie") != -1) ? true : false
		document.cookie = "";
    }
    catch (e) { }
    
    return bEn;
}

//---------------------------------------------------------
function AddBookmark() 
{
	var text = window.title; 
	var url = window.location.href;

	if (window.sidebar) { // Mozilla Firefox Bookmark
	  window.sidebar.addPanel(text, url,"");
	} else if( window.external ) { // IE Favorite
	  window.external.AddFavorite( url, text); }
	else if(window.opera && window.print) { // Opera Hotlist
	  return true; }
}

//---------------------------------------------------------
function getElementPosition(el)
{
	// Initializes the Coordinates object that will be returned by the function.
	var c = { x:0, y:0 } ;
	
	//go through the offset chain.
	while(el)
	{
		c.x += el.offsetLeft ;
		c.y += el.offsetTop ;
		el = el.offsetParent ;
	}

	// Return the Coordinates object
	return c;
}

//--------------------------------------------------------
function validateZipCode(sZip, sCntryCode) 
{
	sCntryCode = sCntryCode.toUpperCase();

	//check zip
	if( (sZip.length == 0) ||
		(sCntryCode == "US" && sZip.search(rexUSZip) < 0) ||
		(sCntryCode == "CA" && sZip.search(rexCAZip) < 0) )
	{
		return false;
	}

	return true;
}

//---------------------------------------------------------
function formatCurrency(num, sSymbol)
{
	try
	{
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num)) num = "0";
		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 (sSymbol + num + '.' + cents);
	}
	catch(err)
	{
		return (sSymbol + "0.00");
	}
}
		
//---------------------------------------------------------
function onloadNS()
{
	try
	{
	  var ns = rover.createNSTracker();
	  if (ns.isDroppingImage())
	  {
		var sUrl = ns.getUrl();
		if(sUrl)
		{
		  var nsImg = new Image;
		  nsImg.src = sUrl;
		}
	  }
	}
	catch(err) {}
}

//--------------------------------------------------------
// JSON based AJAX calling helpers
//--------------------------------------------------------
function oJSONScriptReq(sURL)
{
	this.sSource = sURL;
	this.oObject = null;
	this.sId = "JSONScriptReq_" + oJSONScriptReq.sIdCounter;
	oJSONScriptReq.sIdCounter++;
}

oJSONScriptReq.sIdCounter = 1;

oJSONScriptReq.prototype.inject = function ()
{
	this.oObject = document.createElement("SCRIPT");
	this.oObject.setAttribute("type", "text/javascript");
	this.oObject.setAttribute("charset", "utf-8");
	this.oObject.setAttribute("src", this.sSource);
	this.oObject.setAttribute("id", this.sId);
  
  //inject
  document.getElementsByTagName("head").item(0).appendChild(this.oObject);
}

function injectScript(sSource)  
{ 
	var oReq = new oJSONScriptReq(sSource);
	oReq.inject();
} 

//-----------------------------------------------------------------------------
// General Event Calling / Handling code
//-----------------------------------------------------------------------------
cEventListener = function(fCallback, oObject)
{
	this.fCallback = fCallback;
	this.oObject = oObject ? oObject : null;
}

//---------------------------------------------------------
cEvents = function()
{
	this.aListeners = new Object();
}

cEvents.prototype.attachEvent = function(sName, oListener)
{
	if(!this.aListeners[sName]) 
	{
		//create array to hold handlers
		this.aListeners[sName] = new Array();
	}

	this.aListeners[sName].push(oListener);
}

cEvents.prototype.detachEvent = function(sName, oListener)
{
	var idx=-1;
	
	if(this.aListeners[sName]) 
	{
		//create array to hold handlers
		for(var i=0; i<this.aListeners[sName].length; i++)
		{
			if(this.aListeners[sName][i] == oListener)
			{
				idx = i;
			}
		}
	}

	if(idx >= 0)
	{
		this.aListeners[sName].splice(idx,1);
	}
}

cEvents.prototype.fireEvent = function(sName, oParam1, oParam12)
{
	var bReturnValue = true;
	var aCalls = this.aListeners[sName];

	if(aCalls)
	{
		for(var idx = 0 ; idx < aCalls.length ; idx++)
		{
			if(aCalls[idx].oObject)
			{
				//call with object reference
				bReturnValue = (aCalls[idx].fCallback.call(aCalls[idx].oObject, oParam1, oParam12) && bReturnValue);
			}
			else
			{
				//call without object reference
				bReturnValue = (aCalls[idx].fCallback(oParam1, oParam12) && bReturnValue);
			}
		}
	}

	return bReturnValue;
}

cEvents.prototype.clearAllEvents = function()
{
	this.aListeners = new Object;
}

//create global instance
var gEvents = new cEvents;


//---------------------------------------------------------
function initDocEventHandlers()
{
	Event.observe(document,"click",onDocumentClickEvent);
	Event.observe(document,"keypress",onDocumentKeyEvent);
}

//---------------------------------------------------------
function onDocumentClickEvent(evt)
{
	var bRes = false;
	if(gClickHandler) bRes = gClickHandler();
	//input wasnt handled, default handler
	if(!bRes && gDefaultClickHandler) bRes = gDefaultClickHandler();
	//if it was handled somewhere, then stop the propagation
	if(bRes) Event.stop(evt);
}

//---------------------------------------------------------
function onDocumentKeyEvent(evt)
{
	var iKey = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));		
	var sKey = String.fromCharCode(iKey);
	var bShift = evt.shiftKey;
	var bAlt = evt.altKey;
	var bCtrl = evt.ctrlKey;
	
	var bRes = false;
	
	if(gKeyHandler) bRes = gKeyHandler(iKey, sKey, bShift, bAlt, bCtrl);
	//input wasnt handled, default handler
	if(!bRes && gDefaultKeyHandler) bRes = gDefaultKeyHandler(iKey, sKey, bShift, bAlt, bCtrl);
	//if it was handled somewhere, then stop the propagation
	if(bRes) Event.stop(evt);
}

//---------------------------------------------------------
function setDefaultKeyHandler(fHandler)
{
	gDefaultKeyHandler = fHandler;
}

//---------------------------------------------------------
function setDefaultClickHandler(fHandler)
{
	gDefaultClickHandler = fHandler;
}

//---------------------------------------------------------
function setDocumentKeyHandler(fHandler)
{
	gKeyHandler = fHandler;
}

//---------------------------------------------------------
function setDocumentClickHandler(fHandler)
{
	gClickHandler = fHandler;
}

//---------------------------------------------------------
function clearDocumentKeyHandler()
{
	gKeyHandler = null;
}

//---------------------------------------------------------
function clearDocumentClickHandler()
{
	gClickHandler = null;
}

//---------------------------------------------------------
function disableScrollbars()
{
	if (document.documentElement && document.documentElement.style)
		document.documentElement.style.overflow="hidden";
	if (document.body)
		document.body.style.overflow="hidden";	
}

//---------------------------------------------------------
function enableScrollbars()
{
	if (document.documentElement && document.documentElement.style)
		document.documentElement.style.overflow="";
	if (document.body)
		document.body.style.overflow="";
}

//---------------------------------------------------------
function getScrollTop()
{
	if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	else if (document.body)
		return document.body.scrollTop;
	else
		return 0;
}

//---------------------------------------------------------
function setScrollTop(iY)
{
	if (document.documentElement && document.documentElement.scrollTop)
		document.documentElement.scrollTop = iY;
	else if (document.body)
		document.body.scrollTop = iY;
}

//---------------------------------------------------------
function getDocHeight()
{
	if (document.documentElement && document.documentElement.offsetHeight)
		return document.documentElement.offsetHeight;
	else if (document.body)
		return document.body.offsetHeight;
	else
		return 0;
}

//---------------------------------------------------------
function getDocWidth()
{
	if (document.documentElement && document.documentElement.offsetWidth)
		return document.documentElement.offsetWidth;
	else if (document.body)
		return document.body.offsetWidth;
	else
		return 0;
}

var gTabIndexes = new Array;
var gBlockTab = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
var gPrevKeyPress = null;
var gIsModal = false;

//---------------------------------------------------------
function startModal(bAllowScroll)
{
	try
	{
		bAllowScroll = (bAllowScroll) ? true : false;
		
		//awlays position below current dialog but above previous
		$("gbh_modal").show();
		
		if(!gIsModal)
		{
			if(!bAllowScroll) disableScrollbars();
			//disableTabIndexes();
		}
		
		positionModal();
		setScrollTop(0);
		
		gIsModal =  true;
		gEvents.fireEvent("StartModal");
	}
	catch (err) {}
}

//---------------------------------------------------------
function endModal()
{
	try
	{
		//awlays position below current dialog but above previous
		$("gbh_modal").hide();
		//restoreTabIndexes();
		gIsModal = false;
		enableScrollbars();
		gEvents.fireEvent("EndModal");
	}
	catch (err) {}
}

//---------------------------------------------------------
function positionModal()
{
	try
	{
		$("gbh_modal").style.top = "0px";
		$("gbh_modal").style.left = "0px";
		$("gbh_modal").style.height = getDocHeight() + "px";
	}
	catch (err) { }
}

//---------------------------------------------------------
// For IE.  Go through predefined tags and disable 
// tabbing into them.
function disableTabIndexes() 
{
	try
	{
		if(document.all) 
		{
			var i=0;
			gTabIndexes.length = 0;

			for(var j=0; j<gBlockTab.length; j++) 
			{
				var tagElements = $("gbh_pagediv").getElementsByTagName(gBlockTab[j]);
				for(var k=0 ; k<tagElements.length; k++) 
				{
					gTabIndexes.push(tagElements[k].tabIndex);
					tagElements[k].tabIndex = "-1";
					i++;
				}
			}
		}
		else
		{
			//if not IE, just override
			Event.observe($("gbh_pagediv"), "keypress", blockPageTab);
		}	
	}
	catch (err) { }
}

//---------------------------------------------------------
function blockPageTab(e)
{
	e = e || window.event;
	Event.stop(e);
}

//---------------------------------------------------------
function restoreTabIndexes() 
{
	try
	{
		
		if (document.all) 
		{
			var i = 0;
			for(var j=0; j<gBlockTab.length; j++)
			{
				var tagElements = $("gbh_pagediv").getElementsByTagName(gBlockTab[j]);
				for(var k=0 ; k<tagElements.length; k++) 
				{
					tagElements[k].tabIndex = gTabIndexes[i];
					tagElements[k].tabEnabled = true;
					i++;
				}
			}
		}
		else
		{
			Event.stopObserving($("gbh_pagediv"), "keypress", blockPageTab);
		}

	}
	catch (err) { }
}

//-----------------------------------------------------------------------------
function makeRequest(url, parameters, callback) 
{
  var http_request = false;

  // document.getElementById(spanid).innerHTML = 'Retrieving item info. Please wait...';
 
  if (window.XMLHttpRequest) 
  { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) 
    {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      http_request.overrideMimeType('text/html');
    }
  } 
  else if (window.ActiveXObject) 
  { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) 
  {
    alert('Cannot create XMLHTTP instance');
    return false;
  }

  http_request.open('GET', url + parameters, true);
  http_request.onreadystatechange = function () {
    if (http_request.readyState == 4) {
      var response = http_request.responseText;
      if(callback) callback(response);
    }
  }

  http_request.send(null);  
}

//-----------------------------------------------------------------------------
function grabContent(url, parameters, container, callback) 
{
  var http_request = false;
 
  if (window.XMLHttpRequest) 
  { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) 
    {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      http_request.overrideMimeType('text/html');
    }
  } 
  else if (window.ActiveXObject) 
  { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) 
  {
    alert('Cannot create XMLHTTP instance');
    return false;
  }

  http_request.open('GET', url + parameters, true);
  http_request.onreadystatechange = function () {
    if (http_request.readyState == 4) {
      var response = http_request.responseText;
      if(container) container.innerHTML = response;
      container = null;
      if(callback) callback();
    }
  }

  http_request.send(null);  
}

