/* General utility functions for DataWeb */
/* $Id: utils.js,v 1.3 2008/02/06 19:47:39 max Exp $ */

/**********************************************************************/
/* Some date utilities */

var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

/* Return a number as four digits, e.g. 12 -> 0012 */
function fourdigits(number)
{
	return (number < 1000) ? number + 1900 : number;
}

today =  days[now.getDay()] + ", " +
			  months[now.getMonth()] + " " +
			   date + ", " +
				(fourdigits(now.getYear())) ;

/* Handle a selection for a drop-down */
function dropDownSelect(sel, url)
{
    if (sel.options[sel.selectedIndex].value == 'NULL')
        return;
    parent.location = url + sel.options[sel.selectedIndex].value;
    sel.selectedIndex = 0;
}

/********************************************************************/
/* Tab handling stuff */

/* Hide/show a tab entry */
function showTabEntry(id)
{
	hideAllTabEntries();
	div = document.getElementById(id);
	div.style.display = 'inline';
    return false;
}

function hideAllTabEntries()
{
	allDivs = document.body.getElementsByTagName("DIV");
	for (var i = 0; i < allDivs.length; i++)
		{
		div = allDivs[i];
		//alert('Looking at tab - class is : ' + div.className);
		if (div.className.substring(0,8) == 'TabEntry')
			div.style.display = 'none';
		}
}

/* Select a tab and unselect all other tabs */
function selectTab(theTab, entryId)
{
	allTabs = document.body.getElementsByTagName("A");
	for (var i = 0; i < allTabs.length; i++)
		{
		tab = allTabs[i];
		//alert('Looking at tab - class is : ' + div.className);
		if (tab.className.substring(0,3) == 'Tab')
			tab.className = 'Tab';
		}
	theTab.className = 'TabSelected';

	showTabEntry(entryId);
    return false;
}

/* In a table of photo numbers under a photo, set all numbers to normal style, and set the given cell to highlighted style */
function selectPhotoNumber(theAnchor)
{
    allNums = document.body.getElementsByTagName("A");
    for (var i = 0; i < allNums.length; i++)
        {
        if (allNums[i].className.substring(0,13) == 'PictureNumber')
            allNums[i].className = 'PictureNumber';
        }
    theAnchor.className = 'PictureNumberSelected';
}

/* This is called when the user selects a photo number. It changes the image source, width, height, abd the caption */
function setPhoto(newUrl, newCaption, newWidth, newHeight, selectedAnchor)
{
    img = document.getElementById("FacilityImage");
    img.src = newUrl;
    img.width = newWidth;
    img.height = newHeight;
    caption = document.getElementById("PhotoCaption");
    caption.value = newCaption;
    selectPhotoNumber(selectedAnchor);
}

/* Toggle a text area between large and small size */
function resizeTextArea(taid, img, minRows, maxRows)
{
	ta = document.getElementById(taid, img);
	if (ta.rows == minRows)
		{
		ta.rows = maxRows;
		img.src = 'images/minus.jpg';
		}
	else
		{
		ta.rows = minRows;
		img.src = 'images/plus.jpg';
		}
}

/* Find a word on the page and highlight it. Unfortunately, this only works on IE. */
var n = 0; // Must be outside of the function because of recursive calls
function findInPage(str)
{
	var win = window;  // By default search the current window
	var txt, i, found;
	if (str == "")
		return false;
	// Find next occurance of the given string on the page, wrap around to the
	// start of the page if necessary.
	var NS4 = (document.layers);
	if (NS4)
		{
		// Look for match starting at the current point. If not found, rewind
		// back to the first match.
		if (!win.find(str))
			while(win.find(str, false, true))
				n++;
		else
			n++;
		// If not found in either direction, give message.
		if (n == 0)
			alert("Not found.");
		}

	var IE4 = (document.all);
	if (IE4)
		{
		txt = win.document.body.createTextRange();
		// Find the nth match from the top of the page.
		for (i = 0; i <= n && (found = txt.findText(str)) != false; i++)
			{
			txt.moveStart("character", 1);
			txt.moveEnd("textedit");
		}
		// If found, mark it and scroll it into view.
		if (found)
			{
			txt.moveStart("character", -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			n++;
			}
		// Otherwise, start over at the top of the page and find first match.
		else
			{
			if (n > 0)
				{
				n = 0;
				findInPage(str);
				}
			// Not found anywhere, give message.
			else
				alert("Not found.");
			}
		}
	return false;
}

function ShowTimeoutWarning ()
{
    window.alert( "Your session is going to expire soon. If you have any unsaved work, now would be a good time to save it." );
}
