// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + "; path=/";
}

function inset(elmnt)
{
	elmnt.style.border="inset 2"
}

function outset(elmnt)
{
	elmnt.style.border="outset 2"
}

function binSearch(x, numArray)
// find x in numArray[0..numArray.length-1]
{
	 var low, high, mid, round;
	 low = 0;
	 high = numArray.length - 1;
	 round = 0;
	 while (low <= high) {
		 round++;
		 mid = Math.floor((low + high)/2);
		 
		 if (x < numArray[mid])
			 high = mid - 1;
		 else if (x > numArray[mid])
			 low = mid + 1;
		 else // found in location mid
				return mid;
	 }
	 return -1; // not found
}
 
//printArray(sel);
function linearSearch(x, aArray)
 // find x in array A[0..n-1]
{
		for (var i = 0; i < aArray.length; i++)
			 if (x == aArray[i])
				 return i;
		return -1;
}

function trim(x) {
 // skip leading and trailing whitespace
 // and return everything in between
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}


// Example: showHideLayers(Layer1,'','show',Layer2,'','hide');
//MM_
function showHideLayers()
{ 
  var i, visStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 'show') visStr = 'visible';
        else if(visStr == 'hide') visStr = 'hidden';
      }
      obj.visibility = visStr;
    }
  }
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function printArray(ar) {
	if (isArray(ar)) {
		var txt='';
		var i;
		for(i = 0; i < ar.length; i++) 
		{
			txt = txt + " | " + ar[i];
		}
		alert(txt);
	}
}

var hideLayer;
var hideIDArray = new Array();
var showArray = new Array();

function delayedShow(layer) {
	while (hideIDArray.length > 0 ) {
		window.clearTimeout(hideIDArray.pop());
	}
	
	var la;
	while (showArray.length > 0 ) {
			la = showArray.pop();
			if (layer != la )
				showHideLayers(la, '', 'hide');
	}
	
	showArray.push(layer);
	showHideLayers(layer, '', 'show');
}

function delayedHide(layer) {
	var ID;
	ID = window.setTimeout("showHideLayers('" + layer + "', '', 'hide');", 800);
	hideIDArray.push(ID);
} 

function clearText(box){
	var textBox;
	textBox = findObj(box);

	if ( textBox != null ) {
		textBox.value = '';
	}
}

function addSelectToBox(sel, box) {
	var selMenu, textBox, txt;
	
	selMenu = findObj(sel);
	textBox = findObj(box);
	
	if (selMenu != null && textBox != null ) {
		txt = selMenu.options[selMenu.selectedIndex].text
		if (textBox.value == '')
			textBox.value = txt;
		else
			textBox.value = textBox.value + '; ' + txt;
	}

}

// Example: obj = findObj("image1");
//MM_findObj
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function onlyNumbers(inputString)
{
  var searchForNumbers = /\D+\_+\W+\s+\S+/;
  if (inputString) {
			if (! searchForNumbers.test(inputString))
				return true;
	}
	return false;
} 

function isNumberFloat(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}

//var newwindow;
function popupSelWin(selPage)
{
		var newwindow;
		newwindow=window.open(selPage, 'selection', 'left=400, top=0, height=550, width=500, scrollbars=yes, resizable=yes, status=yes ');	
		if (window.focus) newwindow.focus();		
}

function popupWin(url, winName)
{
		var newwindow;
		newwindow=window.open(url, winName, 'left=100, top=50, height=700, width=1024, scrollbars=no, resizable=yes, status=yes ');	
		if (window.focus) newwindow.focus();
}

function exitNTL(link){
		var newwindow;
		
		newwindow=window.open(link, "NTL_Link");

		if (window.focus) {
			newwindow.focus();
		}
		
		return false;
}

function defaultURL(docid) {
	if (docid != 0)
		return "http://ntl.bts.gov/lib/" + docid + "";
	else 
		return '';
}

function itemInfo(url){
		var newwindow;
		newwindow=window.open(url, 'information', 'left=100, top=50, height=480, width=640, scrollbars=yes, resizable=yes, status=yes ');	
		if (window.focus) newwindow.focus();
}

function trtTop(){
		var newwindow;
		newwindow=window.open('trtlookup.do', 'information', 'left=100, top=50, height=480, width=640, scrollbars=yes, resizable=yes, status=yes ');	
		if (window.focus) newwindow.focus();
}

//Trt Hierarchy
function trtH(notation){		

		var trtInfoUrl = 'information.do?f=htid&d=&t=' + notation;		

		if ( typeof(selBox) != "undefined" ) {
			trtInfoUrl = trtInfoUrl + '&i=' + selBox;
		}

		document.location.href = trtInfoUrl;
}

//Trt Hierarchy in main
function trtMH(notation){
	var trtInfoUrl = 'trthi.do?nn=' + notation;		
	document.location.href = trtInfoUrl;
}


function gotoURL(url){
	document.location.href = url;
}

function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}


function viewUrl(ibox){
	var textBox = findObj(ibox);
		
	if ( textBox != null ) {
			window.open(textBox.value);
	}
}

function openUrlInParent(link){
	try 
	{
		var parentDoc = self.opener.window.document;
		parentDoc.location.href = link;
		return false;	
	}
	catch(err) {
	}
	
	window.open(link);

	return false;	
}

function Toggle(item) {
   obj=findObj(item);
   
   visible=(obj.style.display!="none");
      
   imgButton =findObj("x_" + item);
   
   if (visible) {
     obj.style.display="none";
     imgButton.src="/inc/images/plus.gif";
     imgButton.alt="Click to expand";
   } else {
      obj.style.display="block"; 
      imgButton.src="/inc/images/minus.gif";
	  imgButton.alt="Click to collapse";
   }
}

function getElements(name){
	var elements = findObj(name);
	if (! isArray(elements)) {
		var elementsArray = new Array(1); 
		elementsArray[0] = elements;
		elements = elementsArray;
	}
	return elements;
}	

function Expand() {
   divs=document.getElementsByTagName("DIV");
   for (i=0;i<divs.length;i++) {
     divs[i].style.display="block";
     key=document.getElementById("x" + divs[i].id);
      key.src="/inc/images/minus.gif";
	  key.alt="Click to collapse";
   }
}

function Collapse() {
   divs=document.getElementsByTagName("DIV");
   for (i=0;i<divs.length;i++) {
     divs[i].style.display="none";
     key=document.getElementById("x" + divs[i].id);
     key.src="/inc/images/plus.gif";
	 key.alt="Click to expand";
   }
}