//       | | |
//       ~   ~
//       0   0
//   oooO ( ) Oooo
/***********************
*   Author: RCH        *
*     File: jsLib.js   *
* Language: Javascript *
***********************/

/*
browserDetection  ( )
confirmAction     (message)
hideSubMenuToolTip(fieldObject)
killTimeout       ( )
loadImages        (imgs)
loadSlideshow     (slides, fieldObject)
playSlideshow     (fieldObject)
popUpWindow       (url, left, top, width, height)
showHideSubMenu   (fieldObject, state)
showHideToolTip   (fieldObject, state)
swapImage         (id, source)
todaysDate        ( )
updateClass       (fieldObject, classes)
validateForm      (myForm, numElements)
*/

// ----------------- browserDetection( ) -------------------
/***********************************************************
 This function detects what kind of browser the user is
 using, detects only Internet Explorer, and Netscape based
 browsers.
 ***********************************************************/
function browserDetection( )
{
	//Variable to store browser type
	var browserType;

	//Checks if its Internet Explorer or Netscape based browsers
	if (document.all)
		browserType = "Internet Explorer";
	else if (document.getElementById && !document.all)
		browserType = "Netscape";

	//Returns browser type as string
	return browserType;
}

// ----------------- confirmAction( ) ----------------------
/***********************************************************
 This function will prompt user for confirmation.
 ***********************************************************/
function confirmAction(message)
{
	var answer;

	answer = confirm(message);

	if(answer == 1)
		return true;
	else
		return false;
}

// --------------- hideSubMenuToolTip( ) -------------------
/***********************************************************
 This function will hide the sub menu or tool tip from the
 user's view.
 ***********************************************************/
function hideSubMenuToolTip(fieldObject)
{
	//Checks what browser type it is, and hides it accordingly
	if (browserDetection( ) == "Internet Explorer")
		document.all(fieldObject).style.display = "none";
	else if (browserDetection( ) == "Netscape")
		document.getElementById(fieldObject).style.display = "none";
}

// ------------------- killTimeout( ) ----------------------
/***********************************************************
 This function simply cancels and ends the timeout.
 ***********************************************************/
function killTimeout( )
{
	//Clears timeout function
	clearTimeout(timeoutID);
}

// -------------------- loadImages( ) ----------------------
/***********************************************************
 This function is responsible for preloading images, which
 allows for faster load up time when page loads up.
 ***********************************************************/
function loadImages(imgs)
{
	//Array variable to store all the preloaded pics
	var loadedImgs = new Array( );

	//Preloads all the pics for faster transition
	for (i = 0; i < imgs.length; i++)
	{
		loadedImgs[i]     = new Image( );
		loadedImgs[i].src = imgs[i];
	}
}

// ------------------ loadSlideshow( ) ---------------------
/***********************************************************
 This function prepares and loads the slideshow for faster
 transition time.
 ***********************************************************/
var loadedSlides = new Array( );
 
function loadSlideshow(slides, fieldObject)
{
	//Preloads all the pics for faster load time
	for (i = 0; i < slides.length; i++)
	{
		loadedSlides[i]     = new Image( );
		loadedSlides[i].src = slides[i];
	}

	playSlideshow(fieldObject);
}

// ------------------- playSlideshow( ) --------------------
/***********************************************************
 This function is responsible for playing the slideshow, it
 changes the picture every few secs.
 ***********************************************************/
var j = -1;

function playSlideshow(fieldObject)
{
	//Cycles through the pics again if it reaches the end
	if (j >= loadedSlides.length - 1)
		j = -1;

	//Checks for browser and runs code according to browser info
	if (browserDetection( ) == "Internet Explorer")
	{
		//Sets up the blend transition filter
		document.all(fieldObject).style.filter = "blendTrans(duration = 2)";
		document.all(fieldObject).filters.blendTrans.Apply( );
	
		//Increments counter and displays the picture
		j++;
		document.all(fieldObject).src = loadedSlides[j].src;
	
		//Plays the blend transition filter
		document.all(fieldObject).filters.blendTrans.Play( );
	}
	else if (browserDetection( ) == "Netscape")
	{
		//Increments counter and displays the picture
		j++;
		document.getElementById(fieldObject).src = loadedSlides[j].src;
	}

	//Recursively calling the function
	slideshowTimerID = setTimeout("playSlideshow('"+ fieldObject +"')", 5000);
}

// ------------------- popUpWindow( ) ----------------------
/***********************************************************
 This function is responsible for opening up a pop up window,
 with the user specifying the address, and the dimensions of
 the window.
 ***********************************************************/
var popUpWin = 0;

function popUpWindow(url, left, top, width, height)
{
	//Checks if a window is already popped up
	if(popUpWin)
	{
		if(!popUpWin.closed)
			popUpWin.close( );
	}

	//Pops up the new window
	popUpWin = open(url, 'popUpWin', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width='+width+', height='+height+', left='+left+', top='+top+', screenX='+left+', screenY='+top+'');
}

// ----------------- showHideSubMenu( ) --------------------
/***********************************************************
 This function will reveal sub menu items if the state is
 show.  Otherwise it will hide the sub menu.
 ***********************************************************/
function showHideSubMenu(fieldObject, state)
{
	if (state == "show")
	{
		if (browserDetection( ) == "Internet Explorer")
			document.all(fieldObject).style.display = "";
		else if (browserDetection( ) == "Netscape")
			document.getElementById(fieldObject).style.display = "";
	}
	else
	{
		timeoutID = setTimeout("hideSubMenuToolTip('" + fieldObject + "')", 200);
	}
}

// ----------------- showHideToolTip( ) --------------------
/***********************************************************
 This function will reveal tool tip items if the state is
 show.  Otherwise it will hide the tool tip.
 ***********************************************************/
function showHideToolTip(fieldObject, state)
{
	var posX, posY;

	if (state == "show")
	{
		//Checks for browser type
		if (browserDetection( ) == "Internet Explorer")
		{
			//Internet Explorer way of getting mouse position
			document.all(fieldObject).style.left = posX;
			document.all(fieldObject).style.top = posY;

			document.all(fieldObject).style.display = "";
		}
		else if (browserDetection( ) == "Netscape")
		{
			//Netscape way of getting mouse position
			posX = window.event.clientX + document.body.scrollLeft;
			posY = window.event.clientY + document.body.scrollTop;

			document.getElementById(fieldObject).style.display = "";
		}
	}
	else
	{
		timeoutID = setTimeout("hideSubMenuToolTip('" + fieldObject + "')", 200);
	}
}

// -------------------- swapImage( ) -----------------------
/***********************************************************
 This function is responsible for swapping images to create
 a rollover technique.
 ***********************************************************/
function swapImage(id, source)
{
	//Updates the source
	document.getElementById(id).src = source;
}

// ------------------- todaysDate( ) -----------------------
/***********************************************************
 This function simply returns the current date in a
 YYYY-MM-DD format.
 ***********************************************************/
function todaysDate( )
{
	var today        = new Date( );
	var currentDate  = today.getDate( );
	var currentMonth = today.getMonth( ) + 1;	//add 1 because getMonth returns from 0-11
	var currentYear  = today.getFullYear( );

	return currentYear + "-" + currentMonth + "-" + currentDate;
}

// ------------------- updateClass( ) ----------------------
/***********************************************************
 This function will update the class attribute and replace
 the class attribute with the string being sent in.
 ***********************************************************/
function updateClass(fieldObject, classes)
{
	//Updates the class attribute to the paramater provided
	fieldObject.className = classes;
}

// ------------------ validateForm( ) ----------------------
/***********************************************************
 This function goes through each element contained within
 the form and checks if its empty.
 ***********************************************************/
function validateForm(myForm, numElements)
{
	var i;

	for(i = 0; i < numElements; i++)
	{
		if(myForm.elements[i].value == "")
		{
			alert("Please enter a value for " + myForm.elements[i].name);
			myForm.elements[i].focus( );
			return false;
		}
	}

	return true;
}
