
function getFieldByID(ID)
{
	var fld = document.getElementById(ID);
	if (!fld)
		alert(ID + " field is missing - please contact us.");
	return fld;
}

var fldToSetFocus; 		// global
function setFieldFocus(fld)
{
	fldToSetFocus = fld;
	setTimeout("fldToSetFocus.focus();", 1);
	//setTimeout("fld.focus();", 1);	// this fails, must use reference to a global variable
}

var divNameOpen = ""; // id (name) of the "open" hidden div

// function to display specified hidden div (without gray overlay)
function showDiv(divName)
{
	showGrayOverlay = false;
	//alert("@showDiv(" + divName + ") : divOpen=" + divNameOpen);
	if (divNameOpen != "")
	{
		if (divName == divNameOpen)		// if div is already visible
			return; 					// don't do anything
		hideDiv(divNameOpen); 	// else hide the currently-open div
	}
	var divobj = getFieldByID(divName)
	if (divobj)
	{
		//$('#' + divobj.id).fadeIn();	-- requires jQuery
		divobj.style.display = "block"; 	// make it visible
		divNameOpen = divName; 	// remember name of open div
	}
}

// function to hide specified div and also hide the gray background overlay
function hideDiv(divName)
{
	var divobj = getFieldByID(divName)
	if (divobj)
	{
		//$('#' + divobj.id).fadeOut();		-- requires jquery
		divobj.style.display = "none";
		divNameOpen = "";
		//if (showGrayOverlay)
		//	showOverlay(false); // hide the overlay & reset the global variable: showGrayOverlay 
	}
}

function replaceAll(src,find,replace) 
{
	//alert("@replaceAll(" + src + "," + find + "," + replace + ")");
	ndx = 0;
	while (true)
	{
		ndx = src.indexOf(find, ndx);
		//alert("ndx=" + ndx);
		if (ndx < 0)
			break;
		left = src.substr(0,ndx);
		src = left + replace + src.substr( ndx + find.length);
		//alert("src=[" + src + "]");
		ndx += replace.length - find.length + 1;
	}
	//alert("@end of replaceAll, src=[" + src + "]");
	return src;	
}

function trim( strng )	// has an error?
{
	while (strng.length > 0)
	{
		if (strng.charAt(0) == ' ')
			strng = strng.substr(1);
	}
	var len = strng.length;
	while (len > 0)
	{
		if (strng.charAt(--len) == ' ')
			strng = strng.substr(0, len);
	}
	return strng;
}

// functions to open Google Search results in a new window
function newWindowGoogleSearch(searchText)
{
	//alert("@newWindowGoogleSearch('" + searchText + "')");
	searchText = replaceAll(searchText, " ", "+");
	url = "http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=L5q&q=" + searchText + "&btnG=Search";
	openWindow3(url, "google", 640);
}

function newWindowGoogleSearchXXbad(searchterms)
{
	var qsTag = "&q=xxxxx";
	searchterms = replaceAll(searchterms, " ", "+");
	//searchterms = encodeURI( trim( searchterms ) );
	searchterms = encodeURI(searchterms);
	var urlx = "http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=L5q&q=" & searchterms & "&btnG=Search";
	windo = window.open(urlx, "google", "top=20,left=20,scrollbars=1,title=0,address=0,status=0,toolbar=0,resizable=1,width=640,height=660");
	windo.focus();
}

//generic functions to open a page in a new browser window...
function openDocWindow(url) 
{
	openDocWindowWidth( url, 640)
}
function openDocWindowWidth(url, width) 
{
	openWindow3(url, "docwin", width);
}
function openWindow3(url, windowName, width) 
{
	openWindow6( url, windowName, 20, 20, width, 660);
}
function newWindow4(url, windowName, width, height)
{
	openNewWindow4p(url, windowName, width, height); 
}
function openNewWindow4p(url, windowName, width, height)
{
	openWindow6(url, windowName, 20, 20, width, height);
}
// this one does it (with some sanity checking)
function openWindow6(url, windowName, left, top, width, height) 
{
	if ( isNaN(left) ) left = 20;
	if (left < 0) left = 0;
	if (left > 600) left = 600;
	if ( isNaN(top) ) top = 20;
	if (top < 0) top = 0;
	if (top > 600) top = 600;
	if ( isNaN(width) ) width = 640;
	if (width < 500) width = 500;
	if (width > 1024) width = 1024;
	if ( isNaN(height) ) height = 600;
	if (height < 300) height = 300;
	if (height > 1024) height = 1024;
	chrome = "top=" + top + ",left=" + left;
	chrome += ",scrollbars=1,title=0,address=0,status=0,toolbar=0,resizable=1,width=" + width + ",height=" + height;
	windo = window.open(url, windowName, chrome)
	windo.focus();
}
