var timer			= null;
var daysOfWeek		= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun'];

/*
FUNCTION: stopClock	
					
PARAMETERS: none	
*/
function stopClock() {
	clearTimeout(timer);
}
/*
FUNCTION: getDateTime	
						
PARAMETERS: none		
*/
function getDateTime() {
	var now			= new Date();
	var hours24		= now.getHours();
	var hours12;
	var ampm		= 'AM';
	var minutes		= now.getMinutes();
	var seconds		= now.getSeconds();
	var day			= now.getDay();
	
	if (hours24 >= 13) {
		hours12		= hours24 - 12;
		ampm		= 'PM';
	} else if (hours24 == 12) {
		hours12		= 12;
		ampm		= 'PM';
	} else if (hours24 == 0) {
		hours12		= 12;
	} else {
		hours12		= hours24;
	}
	
	if (seconds <= 9) {
		var tempSec	= seconds;
		seconds		= '0' + tempSec;
	}	
	
	if (minutes <= 9) {
		var tempMin	= minutes;
		minutes		= '0' + tempMin;
	}
	
	document.getElementById('datetime').innerHTML = daysOfWeek[day] + ' ' + hours12 + ':' + minutes + ':' + seconds + ' ' + ampm;
	timer 			= setTimeout('getDateTime()',1000);
}
/*
FUNCTION: moveSelectedItem						
												
PARAMETERS: 									
	formName									
		- string: the name of the form			
	selBox										
		- string: the name of the selection box	
	to											
		- number: either a +1 or -1				
	where										
		- up: moves the selected item up		
		- down: moves the selected item down	
*/
function moveSelectedItem(formName, selBox, to, where) {
	var selectionBox								= eval('document.forms[\'' + formName + '\'].' + selBox);
	var total										= selectionBox.options.length - 1;
	var currSelection								= selectionBox.selectedIndex;
	
	if (currSelection != -1) {
		var currSelText								= selectionBox[currSelection].text;
		var currSelValue							= selectionBox[currSelection].value;
		
		if (currSelection == -1) return false;
		if (to == +1 && currSelection == total) return false;
		if (to == -1 && currSelection == 0) return false;
		
		if (where == 'up') {
			var aboveCurrSelection					= currSelection - 1;
			var aboveCurrSelText					= selectionBox[aboveCurrSelection].text;
			var aboveCurrSelValue					= selectionBox[aboveCurrSelection].value;
			
			selectionBox[currSelection].text		= aboveCurrSelText;
			selectionBox[currSelection].value		= aboveCurrSelValue;
			
			selectionBox[aboveCurrSelection].text	= currSelText;
			selectionBox[aboveCurrSelection].value	= currSelValue;
			
			selectionBox.selectedIndex 				= aboveCurrSelection;
			
		} else if (where == 'down') {
			var belowCurrSelection					= currSelection + 1;
			var belowCurrSelText					= selectionBox[belowCurrSelection].text;
			var	belowCurrSelValue					= selectionBox[belowCurrSelection].value;
			
			selectionBox[currSelection].text		= belowCurrSelText;
			selectionBox[currSelection].value		= belowCurrSelValue;
			
			selectionBox[belowCurrSelection].text	= currSelText;
			selectionBox[belowCurrSelection].value	= currSelValue;
			
			selectionBox.selectedIndex				= belowCurrSelection;
		}
	}
}
/*
FUNCTION: getSelectedItem													
																			
PARAMETERS:																	
	selBox (required)														
		- string: the name of the select box to get the value				
	addToListName (required)		 										
		- string: the name of the select box to pass the selected value to	
*/
function getSelectedItem(selBox, addToListName, dontReplace) {
	var selectionBox	= document.getElementById(selBox);
	var selIndex		= selectionBox.selectedIndex;
	
	if (!dontReplace) {
		var dontReplace	= false;
	}
	
	if (selIndex != -1) {
		var selText		= selectionBox.options[selIndex].text;
		var selValue	= selectionBox.options[selIndex].value;
		
		if (selValue != '') {
			addSelectedItem(addToListName, selText, selValue, dontReplace);
		}
	}
}
/*
FUNCTION: addSelectedItem									
															
PARAMETERS:													
	selBox (required)										
		- string: the name of the select box to add to		
	text (required)											
		- string: the text that displays in the select box	
	value (required)										
		- string: the value that is associated with the text
*/
function addSelectedItem(selBox, text, value, dontReplace) {
	var selectionBox	= document.getElementById(selBox);
	var selBoxItems		= document.getElementById(selBox).options.length;
	var isMatch			= false;
		
	if (!dontReplace) {
		var dontReplace	= false;
	}
	
	for (i = 0; i < selBoxItems; i++) {
		if (document.getElementById(selBox).options[i].value == value) {
			isMatch		= true;
			break;
		}
	}
	
	if (!isMatch) {
		if (selectionBox.selectedIndex == -1) {
			selectionBox.options[selBoxItems]						= new Option(text, value);
		} else {
			if (!dontReplace) {
				selectionBox.options[selectionBox.selectedIndex]	= new Option(text, value);
			} else {
				selectionBox.options[selBoxItems]					= new Option(text, value);
			}
		}
		
		selectionBox.selectedIndex									= -1;
	}
}
/*
FUNCTION: removeSelectedItem								
															
PARAMETERS:													
	selBox (required)										
		- string: the name of the select box to remove from	
*/
function removeSelectedItem(selBox) {
	var selectionBox = document.getElementById(selBox);
	
	selectionBox.remove(selectionBox.selectedIndex);
}
/*
FUNCTION: editSelectedItem						
												
PARAMETERS:										
	formName									
		- string: name of the form				
	selBox										
		- string: the name of the select menu	
*/
function editSelectedItem(formName, selBox) {
	var frm						= document.forms[formName];
	var selectionBox			= eval('frm.' + selBox);
	var selectedValue			= '';
	
	if (selectionBox.selectedIndex != -1) {
		selectedValue			= selectionBox[selectionBox.selectedIndex].value;
		frm.description.value	= selectedValue;
	}
}
/*
FUNCTION: resetSelectMenu						
												
PARAMETERS:										
	formName									
		- string: name of the form				
	selBox										
		- string: the name of the select menu	
*/
function resetSelectMenu(formName, selBox) {
	var frm						= document.forms[formName];
	var selectionBox			= eval('frm.' + selBox);
	
	selectionBox.selectedIndex	= -1;
	
	if (frm.description) {
		frm.description.value	= '';
		frm.description.focus();
	}
}
/*
FUNCTION: showHide											
															
PARAMETERS:													
	elem													
		- string: the ID of the object to show or hide		
	bt														
		- string: the ID of the button element				
	useCustText												
		- true: change the default text in the 'bt' element	
		- false: keeps the default text in the 'bt' element	
	text													
		- string: the text to display in the 'bt' element	
*/
function showHide(elem, bt, useCustText, text) {
	if (document.getElementById(elem).style.display == 'none') {
		if (useCustText) {
			btnText = text;
		} else {
			btnText	= 'Hide' + text;
		}
		
		document.getElementById(elem).style.display = 'inline';
		document.getElementById(bt).innerHTML		= btnText;
	} else {
		if (useCustText) {
			btnText = text;
		} else {
			btnText	= 'Show' + text;
		}
		
		document.getElementById(elem).style.display	= 'none';
		document.getElementById(bt).innerHTML		= btnText;
	}	
}
/*
FUNCTION: generatePassword										
																
PARAMETERS:														
	formName													
		- string: the name of the form							
	formField													
		- string: the name of the field which gets the password	
	len															
		- numeric: the size of the password						
*/
function generatePassword(formName, formField, len) {
	if (len == null || len == '') {
		var length		= 8;
	} else {
		var length		= len;
	}
	
	var sPassword		= '';
	
	var noPunction		= true;
	var randomLength	= false;
	
	if (parseInt(navigator.appVersion) <= 3) {
		alert('Sorry this only works in 4.0+ browsers');
		return true;
	}
	
	if (randomLength) {
		length = Math.random();
	
		length = parseInt(length * 100);
		length = (length % 7) + 6;
	}

	for (i = 0; i < length; i++) {
		numI			= getRandomNum();
		if (noPunction) { 
			while (checkPunc(numI)) { 
				numI	= getRandomNum();
			}
		}
		sPassword		= sPassword + String.fromCharCode(numI);
	}
	
	document.forms[formName].elements[formField].value = sPassword;
	return true;
}
/*
FUNCTION: getRandomNum	
						
PARAMETERS: none		
*/
function getRandomNum() {
	var rndNum = Math.random();
	
	rndNum = parseInt(rndNum * 1000);
	
	rndNum = (rndNum % 94) + 33;
	return rndNum;
}
/*
FUNCTION: checkPunc							
											
PARAMETERS:									
	num										
		- number: the ascii number to check	
*/
function checkPunc(num) {
	if ((num >= 33) && (num <= 47)) {
		return true;
	}
	if ((num >= 58) && (num <= 64)) {
		return true;
	}
	if ((num >= 91) && (num <= 96)) {
		return true;
	}
	if ((num >= 123) && (num <= 126)) {
		return true;
	}
	return false;
}
/*
FUNCTION: newWindow									
													
PARAMETERS:											
	page											
		- string: the page (url) to load			
	name											
		- string: the name of the new window		
	w												
		- number: the width of the new window		
	h												
		- number: the height of the new window		
	props											
		- string: the properties of the new window	
*/
function newWindow(page, name, w, h, props) {
	var winL	= (screen.width - w) / 2;
	var winT	= (screen.height - h) / 2;
	winProps	= 'height=' + h + ',width=' + w + ',top=' + winT + ',left=' + winL + props;
	win			= window.open(page, name, winProps);
	
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}
/*
FUNCTION: submitForm					
										
PARAMETERS:								
	formName							
		- string: the name of the form	
*/
function submitForm(formName) {
	frm = document.forms[formName];
	frm.submit();
}
/*
FUNCTION: addErr								
												
PARAMETERS:										
	e2add										
		- string: the text display of the error	
*/
function addErr(e2add) {
	nextIndex				= aryErrors.length;
	aryErrors[nextIndex]	= e2add;
}
/*
FUNCTION: hasErrrors
					
PARAMETERS: none	
*/
function hasErrors() {
	if (aryErrors.length < 1) {
		return false;
	} else {
		errString		= '';
		
		for (i = 0; i < aryErrors.length; i++) {
			errString	= errString + aryErrors[i] + '\n';
		}
		
		alert('The following errors were encountered:\n\n' + errString);
		
		return true;
	}
}
/*
FUNCTION: hasSpecChar				
									
PARAMETERS:							
	sourceString					
		- string: the text to check	
*/
function hasSpecChar(sourceString) {
	specChars = '<>%*#';
	for (i = 0; i < sourceString.length; i++) {
		for (j = 0; j < specChars.length; j++) {
			if (sourceString.indexOf(specChars.charAt(j)) != -1) {
				return true;
				break;
			}
		}
	}
	return false;
}
/*
FUNCTION: stripNonNumeric								
														
PARAMETERS:												
	sourceString										
		- string: text to strip all non numeric chars	
*/
function stripNonNumeric(sourceString) {
	var i;
	var returnString	= '';
	var numeric			= '0123456789';
	for (i = 0; i < sourceString.length; i++) {
		var c			= sourceString.charAt(i);
		if (numeric.indexOf(c) != -1) {
			returnString += c;
		}
	}
	
	return returnString;
}
/*
FUNCTION: applyMask											
															
PARAMETERS:													
	theField												
		- string: the name of the text field to apply mask	
	mask													
		- string: how to mask the text field				
	domestic (not used)										
		- yes:												
		- no:												
	type													
		- alphamsk: strips all non numeric chars from text	
*/
function applyMask(theField, mask, domestic, type) {
	var mi;
	var si				= 0;
	var returnString	= '';
	
	if (type != 'alphamask') {
		var string		= stripNonNumeric(theField.value);
	} else {
		var string		= theField.value;
	}
	
	for (mi = 0; mi < mask.length && si < string.length; mi++) {
		var mc			= mask.charAt(mi);
		var sc			= string.charAt(si);
		
		if (mc != "#") {
			returnString += mc;
		} else {
			returnString += sc;
			si += 1 
		}
	}
	
	theField.value = returnString;
}
/*
FUNCTION: isEmail							
											
PARAMETERS:									
	sourceString							
		- string: the email address to check
*/
function isEmail(sourceString) {
	// make sure search function is available
	if(sourceString.search) {
		if (sourceString.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}
/*
FUNCTION: isNumeric						
										
PARAMETERS:								
	sourceString						
		- string: the string to check	
*/
function isNumeric(sourceString) {
	var validChars		= '0123456789.';
	var isNumber		= true;
	var char;
	
	for (i = 0; i < sourceString.length && isNumber == true; i++) {
		char = sourceString.charAt(i); 
		if (validChars.indexOf(char) == -1) {
			isNumber	= false;
		}
	}
	
	return isNumber;
}
/*
FUNCTION: checkdate										
														
PARAMETERS:												
	date												
		- string: the date to check in mm/dd/yyyy format
*/
function checkdate(date) {
	var validformat		= /^\d{1,2}\/\d{1,2}\/\d{2,4}$/; //Basic check for format validity
	
	if (!validformat.test(date)) {
		return false;
	} else {
		var monthField	= date.split('/')[0];
		var dayField	= date.split('/')[1];
		var yearField	= date.split('/')[2];
		var dayObj		= new Date(yearField, monthField - 1, dayField);
		
		if ((dayObj.getMonth() + 1 != monthField) || (dayObj.getDate() != dayField) || (dayObj.getFullYear() != yearField)) {
			return false;
		} else {
			return true;
		}
	}
}
/*
FUNCTION: changeWindowLocation		
									
PARAMETERS:							
	location						
		- string: the page to go to	
*/
function changeWindowLocation(location) {
	window.location					= location;
	
	if (window.event) {
		window.event.returnValue	= false;
	}
}
/*
FUNCTION: trimTextForDisplay							
														
PARAMETERS:												
	sourceString										
		- string: the string to trim					
	length												
		- numeric: the length trim by					
	ellipsisPlacement									
		- middle: put "..." in the middle of the string	
		- end: put "..." at the end of the string		
*/
function trimTextForDisplay(sourceString, length, ellipsisPlacement) {
	if (ellipsisPlacement == '' || ellipsisPlacement == null) {
		ellipsisPlacement	= 'end';
	}
	
	var leftOfEllipsis		= '';
	var rightOfEllipsis		= '';
	var trimCount			= Math.round(length/2)-3;
	var stringLength		= String(sourceString).length;
	
	if (stringLength > length) {
		if (ellipsisPlacement == 'middle') {
			leftOfEllipsis	= String(sourceString).substring(0, trimCount);
			rightOfEllipsis	= String(sourceString).substring(stringLength, stringLength - trimCount);
			
			return leftOfEllipsis + ' ... ' + rightOfEllipsis;
		} else if (ellipsisPlacement == 'end') {
			return String(sourceString).substring(0, length) + ' ...';
		}
	} else {
		return sourceString;
	}
}
/*
FUNCTION: showHideCFWindow							
													
PARAMETERS: 										
	action											
		- show: Shows the window					
		- hide: Hides the window					
	windowName										
		- string: The name of the window			
	confirmClose (can be blank)						
		- true: warns before closing window			
		- false: closes the window without warning	
*/
function showHideCFWindow(action, windowName, confirmClose) {
	if (confirmClose == null || confirmClose == '') {
		confirmClose = false;
	}
	
	if (action == 'show') {
		ColdFusion.Window.show(windowName);
	} else if (action == 'hide') {
		if (confirmClose) {
			if (confirm('Are you sure you want to close this window?')) {
				ColdFusion.Window.hide(windowName);
			}
		} else {
			ColdFusion.Window.hide(windowName);
		}
	}
}
/*
FUNCTION: createCFWindow						
												
PARAMETERS:										
	wName										
		- string: name of the window			
	wTitle										
		- string: title of the window			
	wURL										
		- string: location of the page to load	
	wResize										
		- true: window is resizable				
		- false: window is not resizable		
	wClose										
		- true: window is closable				
		- false: window is not closable			
	wHeight										
		- number: height of the window			
	wMinHeight									
		- number: min height of the window		
	wWidth										
		- number: width of the window			
	wMinWidth									
		- number: min width of the window		
	wModal										
		- true: window is modal					
		- false: window is not modal			
*/
function createCFWindow(wName, wTitle, wURL, wResize, wClose, wHeight, wMinHeight, wWidth, wMinWidth, wModal) {
	if (wResize) {
		ColdFusion.Window.create(wName, wTitle, wURL, {center:true,draggable:true,resizable:true,closable:wClose,height:wHeight,minheight:wMinHeight,width:wWidth,minwidth:wMinWidth,modal:wModal});
	} else {
		ColdFusion.Window.create(wName, wTitle, wURL, {center:true,draggable:true,resizable:false,closable:wClose,height:wHeight,width:wWidth,modal:wModal});
	}
}
/*
FUNCTION: changeCFWindowTitle			
										
PARAMETERS:								
	windowTitleName						
		- string: the name of the window
	titleString							
		- string: the title name		
*/
function changeCFWindowTitle(windowTitleName, titleString) {
	document.getElementById(windowTitleName).innerHTML = titleString;
}
/*
FUNCTION: checkFileUploadLength							
														
PARAMETERS:												
	fileName											
		- string: the name of the file					
	length												
		- number: the largest value the file name can be
*/
function checkFileUploadLength(fileName, length) {
	if (length == null || length == '') {
		length	= 60;
	}
	
	if (fileName.value.lastIndexOf('\'') != -1) {
		docFirstPos		= fileName.value.lastIndexOf('\'') + 1;
		docLastPos		= fileName.value.length - 4;
	} else {
		docFirstPos		= 0;
		docLastPos		= fileName.value.length - 4;
	}
	
	var file			= fileName.value.substring(docFirstPos, docLastPos);
	
	if (file.length > 60) {
		return true;
	}
	
	return false;	
}
/*
FUNCTION: getFileUploadExtension		
	fileName							
		- string: the name of the file	
*/
function getFileUploadExtension(fileName) {
	if (fileName.value.lastIndexOf('.') != -1) {
		extFirstPos		= fileName.value.lastIndexOf('.') + 1;
		extLastPos		= fileName.value.length
	
		return fileName.value.substring(extFirstPos, extLastPos);
	}
	
	return '';
}