// Copyright Deb Shepherd, Catch 22, 2008



// --------------------------------------------------------------------
// COOKIE FUNCTIONS
// --------------------------------------------------------------------

	// TEMPORAILY SAVE THE CONTENTS OF A TEXT FIELD IN A FORM AS A COOKIE
	// requires a FORM name, a field name, and the cookie text as a string
	// returns nothing
		function setCookie(formName, fieldName, cookieName)
		{	var cookieText = extractValue(formName, fieldName, cookieText);
			document.cookie = cookieName + "=" + escape(cookieText);
		}

	// RETRIVE A PREVIOUSLY SAVED COOKIE
	// requires the name of the cookie to be retrieved
	// returns nothing
		function getCookie(cookieName)
		{	var cookieValue = "";
			if (document.cookie.length > 0)
			{	cookieStart = document.cookie.indexOf(cookieName + "=");
				cookieEnd = document.cookie.indexOf(";", cookieStart);
				if ((cookieStart > -1)&&(cookieEnd > -1))
				{	cookieStart = cookieStart + cookieName.length+1;
					cookieValue = unescape(document.cookie.substring(cookieStart, cookieEnd));
				} else
				{	cookieStart = cookieStart + cookieName.length+1;
					cookieValue = unescape(document.cookie.substring(cookieStart));
				}
			} return cookieValue;
		}



// --------------------------------------------------------------------
// DATE AND TIME FUNCTIONS
// --------------------------------------------------------------------

	// PROVIDES THE CURRENT DAY OF THE WEEK AS A TEXT STRING
	// requires a date object
	// returns the current day as a text string
		function dayOfWeek(date)
		{	var day = "";
			if (date.getDay() == 0)
			{	day = "Sunday";
			} else if (date.getDay() == 1)
			{	day = "Monday";
			} else if (date.getDay() == 2)
			{	day = "Tuesday";
			} else if (date.getDay() == 3)
			{	day = "Wednesday";
			} else if (date.getDay() == 4)
			{	day = "Thursday";
			} else if (date.getDay() == 5)
			{	day = "Friday";
			} else if (date.getDay() == 6)
			{	day = "Saturday";
			}
			return day;
		}
		
	// PROVIDES THE CURRENT MONTH OF THE YEAR AS A TEXT STRING
	// requires a date object
	// returns the current month as a text string
		function monthOfYear(date)
		{	var month = "";
			switch (date.getMonth())
			{	case 0:
					month = "January";
					break;
				case 1:
					month = "February";
					break;
				case 2:
					month = "March";
					break;
				case 3:
					month = "April";
					break;
				case 4:
					month = "May";
					break;
				case 5:
					month = "June";
					break;
				case 6:
					month = "July";
					break;
				case 7:
					month = "August";
					break;
				case 8:
					month = "September";
					break;
				case 9:
					month = "October";
					break;
				case 10:
					month = "November";
					break;
				case 11:
					month = "December";
					break;
			}
			return month;
		}
		
	// PROVIDES THE CORRECT DATE POSTFIX AS A TEXT STRING
	// requires a date object
	// returns the correct date postfix as a text string
		function datePostfix(date)
		{	var postfix = "";
			if ((date.getDate() == 1) || (date.getDate() == 21) || (date.getDate() == 31))
			{	postfix = "st";
			} else if ((date.getDate() == 2) || (date.getDate() == 22))
			{	postfix = "nd";
			} else if ((date.getDate() == 3) || (date.getDate() == 23))
			{	postfix = "rd";
			} else
			{	postfix = "th";
			}
			return postfix;
		}
		
/*	// TURNS HOURS IN TO 12 HOUR CLOCK
	// ADDS AM OR PM
	// requires a date object
	// returns the current month as a text string
		function twelveHourClock(date)
		{	var month = "";
			if (now.getHours() < 13)
			{	var currentHour = now.getHours();
				var ampm = "am";
			}
			else
			{	var currentHour = now.getHours() - 12;
				var ampm = "pm";
			}		
			return month;
		}
		
		// add leaqding 0 to minutes less than 10
		// store result in a variable
	// PROVIDES THE CURRENT MONTH OF THE YEAR AS A TEXT STRING
	// requires a date object
	// returns the current month as a text string
		function monthOfYear(date)
		{	var month = "";
			if (now.getMinutes() < 10)
			{	var currentMinute = "0" + now.getMinutes();
			}
			else
			{	var currentMinute = now.getMinutes();
			}		
			return month;
		}
*/


// --------------------------------------------------------------------
// FORM FUNCTIONS
// --------------------------------------------------------------------

	// JUMP TO ANOTHER PAGE USING A SELECT
	// requires a FORM name, a field name, and an error message as a text string
	// returns nothing
		function Jump(formName, fieldName, errorMessage)
		{	var fieldNameEval = eval("document." + formName + "." + fieldName);
			fieldValue = fieldNameEval[fieldNameEval.selectedIndex].value;
			if (fieldValue == "")
				{	alert(errorMessage);
				} else
				{	location.href = fieldValue;
				}
		}


	//FIELD CONTENTS EXTRACTION
	// requires a FORM name, and a field name
	// returns the field contents

	// generic value extraction
		function extractValue(formName, fieldName)
		{	var fieldNameEval = eval("document." + formName + "." + fieldName);
			contents = fieldNameEval.value;
			return contents;
		}

	// CHECKBOX or RADIO value extraction
		function extractCheckboxRadioValues(formName, fieldName)
		{	var fieldNameEval = eval("document." + formName + "." + fieldName); 
			for (i = 0; i < fieldNameEval.length; i++)
			{	if  (fieldNameEval[i].checked)
				{	contents = fieldNameEval[i].value;
					return contents;
				}
			}
			return "";
		}

	// SELECT value extraction
		function extractSelectValue(formName, fieldName)
		{	var fieldNameEval = eval("document." + formName + "." + fieldName);
			contents = fieldNameEval[fieldNameEval.selectedIndex].value;
			return contents;
		}


	// FIELD CONTENTS VALIDATION
	// requires a FORM name, and a field name
	// returns TRUE if the field is empty
	// OR
	// returns TRUE if the field contains the specified content
	// otherwise returns FALSE

	// function field is empty validation
		function isEmpty(formName, fieldName)
		{	var fieldValue = extractValue(formName, fieldName);
			if ((fieldValue == null) || (fieldValue == ""))
			{	return true;
			}
			return false;
		}
		
	// CHECKBOX or RADIO field is empty validation
		function isEmptyCheckboxRadio(formName, fieldName)
		{	var fieldValue = extractCheckboxRadioValues(formName, fieldName);
			if ((fieldValue == null) || (fieldValue == ""))
			{	return true;
			}
			return false;
		}

	// SELECT field is empty validation
		function isEmptySelect(formName, fieldName)
		{	var fieldValue = extractSelectValue(formName, fieldName);
			if ((fieldValue == null) || (fieldValue == ""))
			{	return true;
			}
			return false;
		}
		
	// value is a number validation
		function isNumber(formName, fieldName)
		{	var fieldValue = extractValue(formName, fieldName);
			if (!isEmpty(formName, fieldName))
			{	theRegex = new RegExp("^[\\d]+$");
				theMatchArray = theRegex.exec(fieldValue);
				return (theMatchArray != null);
			}
			return false;
		}

	// value is an email address validation
		function isEmail(formName, fieldName)
		{	var fieldValue = extractValue(formName, fieldName);
			if (!isEmpty(formName, fieldName))
			{	theRegex = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
				theMatchArray = theRegex.exec(fieldValue);
				return (theMatchArray != null);
			}
			return false;
		}



// --------------------------------------------------------------------
// ROLLOVER BUTTON FUNCTIONS
// --------------------------------------------------------------------

	// CHANGE FROM ORIGINAL TO ROLLOVER 
	// requires the rollover image id
	// returns nothing
	function rolloverImage(imageName)
	{	imgLoc = eval(imageName + "_rollover.src");
		document[imageName].src = imgLoc;
	}
	
	// CHANGE FROM ROLLOVER BACK TO ORIGINAL
	// requires the original image id
	// returns nothing
	function defaultImage(imageName)
	{	imgLoc = eval(imageName + "_default.src");
		document[imageName].src = imgLoc;
	}
	
	// CHANGE FROM ORIGINAL TO ROLLOVER AND SET THE STATUS BAR
	// requires the the rollover image id and the required text
	// returns true or false
	function rollover(imageName, statusBarText)
	{	rolloverImage(imageName);
		return setStatusBar(statusBarText);
	}
	
	// CHANGE FROM ROLLOVER BACK TO ORIGINALAND CLEAR THE STATUS BAR
	// requires the the original image id and an empty string
	// returns true or false
	function rollout(imageName, statusBarText)
	{	defaultImage(imageName);
		return setStatusBar(statusBarText);
	}




// --------------------------------------------------------------------
// SLIDE SHOW FUNCTIONS
// --------------------------------------------------------------------

	// SHOW A SINGLE IMAGE IN THE SLIDE SHOW
	// requires the image number
	// return nothing
		function showImage(whichImg)
		{	currentImg = whichImg;
			document.images.slideImg.src = "slides/" + whichImg + ".jpg";
		}

	// START / STOP A SLIDE SHOW
	// require nothing
	// return nothing
		function startShow()
		{	slideTimer = setTimeout("startShow()",timer);
			if (currentImg >= lastImg)
				showImage(1);
			else
				showImage(++currentImg);
		}
		function stopShow()
		{	clearTimeout(slideTimer);
		}

	// GO TO THE PREVIOUS / NEXT IMAGE IN A SLIDE SHOW
	// require nothing
	// return nothing
		function previousSlide() 
		{	if (currentImg <= firstImg)
				return;
			else
				showImage(--currentImg);
		}

		function nextSlide() 
		{	if (currentImg >= lastImg)
				return;
			else
				showImage(++currentImg);
		}



// --------------------------------------------------------------------
// STATUS BAR  FUNCTIONS
// --------------------------------------------------------------------

	// SET THE STATUS BAR
	// requires the required text
	// returns true or false
	function setStatusBar(statusBarText)
	{	window.status = statusBarText;
		return true;
	}



// --------------------------------------------------------------------
// TEXT HANDLING FUNCTIONS
// --------------------------------------------------------------------

	// CHANGE THE TEXT OF AN ELEMENT
	// requires the new text
	// returns nothing
	function changeText(newText)
	{	var headingElement = document.getElementById("heading");
		headingElement.innerHTML = newText;
	}
