// JavaScript Functions

/*--------------------------------------------------------------------------------------*\
 |Get a TAG's handle identified by their ID name.
 |
 | Argument(s):	whichID		- Tag ID
/*--------------------------------------------------------------------------------------*/
function getID(whichID)
{
	return document.getElementById(whichID);
}
//---------------------------------------------------------------------------------------


/*--------------------------------------------------------------------------------------*\
 |Toggle a TAG ON/OFF
 |
 | If the given tag ID is visible, this function will make it invisible and vice-versa.
 |
 | Argument(s):	whichID		- Tag ID to toggle
/*--------------------------------------------------------------------------------------*/
function toggleDIV(whichID)
{
	var dv = getID(whichID);
	
	if(dv != null)
	{
		if(dv.style.display == "none")
		{
			dv.style.display = "";
			return true;
		}
		else
		{
			dv.style.display = "none";
			return false;
		}
	}
}
//---------------------------------------------------------------------------------------


/*--------------------------------------------------------------------------------------*\
 |Show or Hide a TAG
 |
 | Argument(s):	whichID		- Tag ID to show / hide
 |							showFlag	- Show flag (0 = hide, 1 = show)
/*--------------------------------------------------------------------------------------*/
function displayDIV(whichID, showFlag)
{
	var dv = getID(whichID);
	
	if(dv != null)
	{
		if(showFlag == 0)
		{
			dv.style.display = "none";
			return false;
		}
		else
		{
			dv.style.display = "";
			return true;
		}
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------------------


/*--------------------------------------------------------------------------------------*\
 |Toggle one image with another
 |
 | Argument(s):	whichNAME - Image tag name to toggle
 |							img1      - Image file name 1
 |              img2      - Image file name 2
/*--------------------------------------------------------------------------------------*/
function toggleIMG(whichNAME, img1, img2)
{
	if(whichNAME != null)
	{
		var path = getPath(whichNAME.src);
		var img  = getFilename(whichNAME.src);

		if(whichNAME.src == path + img1)
			whichNAME.src = path + img2;
		else
			whichNAME.src = path + img1;
	}
}
//---------------------------------------------------------------------------------------


/*--------------------------------------------------------------------------------------*\
 |Return only the path from a URL
 |
 | Argument(s):	whichURL - URL to extract
/*--------------------------------------------------------------------------------------*/
function getPath(whichURL)
{
	if(trim(whichURL) != "" && whichURL != null)
	{
		var aa = whichURL.lastIndexOf('/');
		
		if(aa < 0)	// no slash was found
		{
			aa = whichURL.lastIndexOf('\\');
			
			if(aa < 0)
				return whichURL;
			else
				return whichURL.substr(0, aa) + "/";
		}
		else
		{
			return whichURL.substr(0, aa) + "/";
		}
	}
	else
	{
		return "";
	}
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 |Return only the file name from a URL
 |
 | Argument(s):	whichURL - URL to extract file name
/*--------------------------------------------------------------------------------------*/
function getFilename(whichURL)
{
	whichURL = trim(whichURL);
	
	if(whichURL != "" && whichURL != null)
	{
		var aa = whichURL.lastIndexOf('/');
		
		if(aa < 0)	// no slash was found
		{
			aa = whichURL.lastIndexOf('\\');
			
			if(aa < 0)
				return "";
			else
				return whichURL.substr(aa+1, whichURL.length);
		}
		else
		{
			return whichURL.substr(aa+1, whichURL.length);
		}
	}
	else
	{
		return "";
	}
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 | Function: trim ( string )
 |
 | Purpose: Trim leading and trailing spaces off a string and return
 |
 | Args: string - string to trim
/*--------------------------------------------------------------------------------------*/
function trim(what) {
	var aa = what;
	
	// Trim Leading Spaces
	while(aa.charAt(0) == ' ') {
		aa = aa.substring(1, aa.length);
	}
	
	// Trim Trailing Spaces
	while(aa.charAt(aa.length-1) == ' ') {
		aa = aa.substring(0, aa.length-1);
	}
	
	return aa;
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 |Return the current date in the format: 2006/01/01
 |
 | Argument(s):	divider - preferred divider character (default is '/')
/*--------------------------------------------------------------------------------------*/
function getDate(divider)
{
	var Today = new Date();
	
	if(divider == null) divider = "";
	
	return (Today.getFullYear() + divider + addZeros(2,Today.getMonth()+1) + divider + addZeros(2,Today.getDate()));
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 |Return the current time in the format: 01:01:01
 |
 | Argument(s):	none
/*--------------------------------------------------------------------------------------*/
function getTime() 
{
	var Time = new Date();
	
	return (addZeros(2,Time.getHours()) + ":" + addZeros(2,Time.getMinutes()) + ":" + addZeros(2,Time.getSeconds()));
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 |Pad a numeric value with zeros - such as 1 -> 001
 |
 | Argument(s):	count - number of zeros to pad (if count=2 & value = 6, returned is 06)
 |              value - value to add zeros to
/*--------------------------------------------------------------------------------------*/
function addZeros(count,value) 
{
	var zeros  = "";
	var NumLen = String(value).length;

	for(var aa = 1; aa <= (count - NumLen); aa++) {
		zeros += "0";
	}
	return zeros + String(value);
}
/*--------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------*\
 |Returns the total number of items in a <select> list
 |
 | Argument(s):	whichList - handle of list tag
/*--------------------------------------------------------------------------------------*/
function getTotalListItems(whichList)
{
	for(aa = 0; aa < 10000; aa++)
	{
		if(whichList.options[aa] == null) return aa;
	}
	
	return aa;
}
/*--------------------------------------------------------------------------------------*/

