//************************************************************************
//
//  JavaScript Global Variables and Common Functions
//
//************************************************************************

//************************************************************************
// GLOBAL VARIABLES
//************************************************************************
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = y2k(today.getYear());
var whichOne;
var myWindow;
var gButton;
//
//************************************************************************

//************************************************************************
//Functions:
// function areYouSure(sText)
// function checkEmail(emailAddress)
// function doButton(sPageName, sButton)
// function javaTrim(value)
// function longDate()
// function newWindow(target, options)
// function ShowProgress()
// function thisForm_Submit()
// function tick(sLevel)
// function validateField(string, addvalid)

// calendar functions:
//  function y2k(number)
//  function padout(number)
//  function calrestart()
//  function calWindow(field)
//************************************************************************

//************************************************************************
//	Function:	areYouSure
//	Purpose:	Presents confirmation alert to user and returns value
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	03/14/2002
//	Input:		alert message string
//	Output:		true or false
//	Notes:		
//************************************************************************
function areYouSure(sText) {
	var sMsg = 'Are you sure you want to ' + sText;
	sMsg = sMsg + '\n\nIf yes, press OK, otherwise, press Cancel';
	var truthBeTold = window.confirm(sMsg);
	if (truthBeTold) {
		return true;
	}
	else {
		return false;
	}
}
//************************************************************************
//	end function areYouSure(sText)
//************************************************************************

//************************************************************************
//	Function:	checkEmail
//	Purpose:	Checks email address for standard format.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	01/02/2001
//	Input:		email address string
//	Output:		true or false
//	Notes:		
//************************************************************************
function checkEmail(emailAddress) {
	var i = 1;
	var sLength = emailAddress.length;

	//look for @ after the first character
	while (( i < sLength) && (emailAddress.charAt(i) != '@')) {
		i++;
	}
	if ((i >= sLength) || emailAddress.charAt(i) != '@') {
		return false;
	}
	else {
		i += 2;
	}
	//look for . after @
	while ((i < sLength) && (emailAddress.charAt(i) != '.')) {
		i++;
	}
	//there must be at lease one character after the .
	if ((i >= sLength - 1) || (emailAddress.charAt(i) != '.')) {
		return false;
	}
	else {
		return true;
	}
}
//************************************************************************
//	end function checkEmail(emailAddress)
//************************************************************************

//************************************************************************
//	Function:	doButton
//	Purpose:	Determine which button/action to take
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	02/26/2002
//	Input:		button name/action
//	Output:		n/a
//	Notes:		
//************************************************************************
function doButton(sPageName, sButton) {
	gButton = sButton;
	if (thisForm_Submit(sPageName)) document.thisForm.submit();
}
//************************************************************************
//	end function doButton(sPageName, sButton)
//************************************************************************

//************************************************************************
//	Function:	javaTrim
//	Purpose:	Removes leading/trailing spaces from a string.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	n/a
//	Input:		value
//	Output:		value without leading or trailing spaces
//	Notes:		
//************************************************************************
function javaTrim(value) {
	
	/* Strip leading spaces from input */
	
	var startposn=0;
	while((value.charAt(startposn) == ' ') && (startposn < value.length)) {
		startposn++;
	}
	
	if(startposn == value.length) {
		value = '';
	}
	else {

		/* if anything left of string after stripping leading spaces, */
		/* strip trailing spaces as well */
		value = value.substring(startposn, value.length);
		endposn = (value.length) - 1;
		while(value.charAt(endposn) == ' ') {
			endposn--;
		}
		value = value.substring(0, endposn + 1);
	}
	return(value);
}
//************************************************************************
//	end function javaTrim
//************************************************************************

//************************************************************************
//	Function:	longDate
//	Purpose:	creates string containing current date in long format
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	03/11/2002
//	Input:		n/a
//	Output:		string with current date in long format
//	Notes:		
//************************************************************************
function longDate() {
	var d, s, dayName, monthName;
	dayName = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	monthName = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

	d = new Date();
	s = dayName[d.getDay()] + ', ';
	s += monthName[d.getMonth()] + ' ';
	s += d.getDate() + ', ';
	s += d.getFullYear();

	return(s);
}
//************************************************************************
//	end function longDate()
//************************************************************************

//************************************************************************
//	Function:	newWindow
//	Purpose:	Creates a new browser window
//	Developer:	Matt Kaiser
//	Updated:	07/07/2000
//	Input:		target (page), options (browser window parameters)
//	Output:		none
//	Notes:		
//************************************************************************
function newWindow(target, options) {
	var today = new Date();
	var pointer;
	var newWindow = 'child' + today.getHours() + today.getMinutes() + today.getSeconds();

	if (options == '') {
		//defaults: 'channelmode=no,directories=yes,fullscreen=no,height=100,left=0,location=yes,menubar=yes,resizable=yes,scrollbars=no,status=yes,titlebar=yes,toolbar=yes,top=0,width=100';
		options = 'channelmode=no,directories=no,fullscreen=no,height=400,left=100,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no,top=100,width=400';
	}

	pointer = self.open(target, newWindow, options);
}
//************************************************************************
//	end function newWindow(target, options)
//************************************************************************

//************************************************************************
//	Function:	ShowProgress
//	Purpose:	Presents upload progress bar
//	Developer:	ASPUpload
//	Updated:	03/22/2002
//	Input:		n/a
//	Output:		progress bar asp page
//	Notes:		ASPUpload 3.0
//************************************************************************
function ShowProgress() {
	strAppVersion = navigator.appVersion;
	if (document.thisForm.postFile.value != '') {
		if (document.thisForm.barRef.value != '') {
			if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4) {
				winstyle = 'dialogWidth=375px; dialogHeight:130px; center:yes';
				window.showModelessDialog(document.thisForm.barRef.value + '&b=IE',null,winstyle);
			}
			else {
				window.open(document.thisForm.barRef.value + '&b=NN','','width=370,height=115', true);
			}
		}
	}
	return true;
}
//************************************************************************
//	end function ShowProgress()
//************************************************************************

//************************************************************************
//	Function:	thisForm_Submit
//	Purpose:	Main function upon submision of a form
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	02/26/2002
//	Input:		n/a
//	Output:		n/a
//	Notes:		
//************************************************************************
function thisForm_Submit(sPageName) {
	var bResult = eval('validate_' + sPageName + '()');
	if (sPageName == 'upload') {
		if (bResult) var p = ShowProgress();
	}
	return bResult;
}
//************************************************************************
//	end function thisForm_Submit()
//************************************************************************

//************************************************************************
//	Function:	tick
//	Purpose:	creates running time clock text for <div id=Clock></div>
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	03/11/2002
//	Input:		sLevel = granularity of clock (whether or not to show seconds)
//	Output:		string with current time to the second
//	Notes:		
//************************************************************************
function tick(sLevel) {
	var hours, minutes, seconds, ap;
	var iHours, iMinutes, iSeconds;
	var today;
	var iInterval = 5000;

	today = new Date();

	iHours = today.getHours();
	iMinutes = today.getMinutes();
	iSeconds = today.getSeconds();

	switch(iHours) {
		case 0 :
			iHours = 12;
			hours = iHours + ':';
			ap = 'A.M.';
			break;
		case 12 :
			hours = iHours + ':';
			ap = 'P.M.';
			break;
		case 24 :
			iHours = 12;
			hours = iHours + ':';
			ap = 'A.M.';
			break;
		default :
			if (iHours > 12) {
				iHours = iHours - 12;
				hours = iHours + ':';
				ap = 'P.M.';
				break;
			}
			if(iHours < 12) {
				hours = iHours + ':';
				ap = 'A.M.';
			}
	} //switch(iHours)


	if (iMinutes < 10) {
		minutes = '0' + iMinutes + ':';
	}
	else {
		minutes = iMinutes + ':';
	}

	if (iSeconds < 10) {
		seconds = '0' + iSeconds + ' ';
	}
	else {
		seconds = iSeconds + ' ';
	}

	switch (sLevel) {
		case 's' :
			timeString = hours + minutes + seconds + ap;
			iInterval = 500;
			break;
		default :
			timeString = hours + minutes + ap;
			break;
	}

	Cal.innerHTML = longDate();
	Clock.innerHTML = timeString;
	var bRet = eval("window.setTimeout('tick(\"" + sLevel + "\");', " + iInterval + ");");
}
//************************************************************************
//	end function tick(sLevel)
//************************************************************************

//************************************************************************
//	Function:	validateField
//	Purpose:	Checks string for invalid characters.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	09/14/2000
//	Input:		string, additional valid characters
//	Output:		true if only valid characters, false if any not valid
//	Notes:		
//************************************************************************
function validateField(string, addValid) {
	var valid = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
	valid = valid + addValid;

	for (var i = 0; i < string.length; i++) {
		if (valid.indexOf(string.charAt(i)) < 0) {
			//allow line feed/carriage return
			if (string.charCodeAt(i) != 10 && string.charCodeAt(i) != 13) {
				return false;
			}
		}
	}
	return true;
} 
//************************************************************************
//	end function validateField(string, addvalid)
//************************************************************************

//************************************************************************
//	Function:	numDecPlaces
//	Purpose:	Limits decimals to nDigits digits right of point
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	03/29/2002
//	Input:		number, number of digits
//	Output:		truncated number
//	Notes:		
//************************************************************************
function numDecPlaces(sNumber, nDigits) {
	if (nDigits == 0) {
		var decLoc = sNumber.indexOf(".");
		if (decLoc == -1) return sNumber
		return sNumber.slice(0, ((sNumber.indexOf(".") + 0) + nDigits));
	}
	else {
		return sNumber.slice(0, ((sNumber.indexOf(".") + 1) + nDigits));
	}
} 
//************************************************************************
//	end function numDecPlaces(nNumber, nDigits)
//************************************************************************

//************************************************************************
//	Function:	padDecPlaces
//	Purpose:	Pads decimals to nDigits digits right of point
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	04/15/2002
//	Input:		number, number of digits
//	Output:		padded number
//	Notes:		
//************************************************************************
function padDecPlaces(sNumber, nDigits) {

//per team, return blank not zeros	if (sNumber == '') return '0.0000';
	if (sNumber == '') return '';
	
	var decLoc = sNumber.indexOf(".");
	if (nDigits == 2) {

		if (decLoc == -1) {
			return sNumber + '.00';
		} //if (decLoc == -1)

		var decStr = sNumber.substr(decLoc + 1, sNumber.length - decLoc)
		switch (decStr.length) {
			case 0:
				sNumber = sNumber + '00';
				break;
			case 1:
				sNumber = sNumber + '0';
				break;
			default:
				sNumber = sNumber.substr(0, decLoc + 3)
		} //switch (decStr.length)

	} //if (nDigits == 2)
	else { //if (nDigits == 2)

		if (decLoc == -1) {
			return sNumber + '.0000';
		} //if (decLoc == -1)

		var decStr = sNumber.substr(decLoc + 1, sNumber.length - decLoc)
		switch (decStr.length) {
			case 0:
				sNumber = sNumber + '0000';
				break;
			case 1:
				sNumber = sNumber + '000';
				break;
			case 2:
				sNumber = sNumber + '00';
				break;
			case 3:
				sNumber = sNumber + '0';
				break;
			default:
				sNumber = sNumber.substr(0, decLoc + 5)
		} //switch (decStr.length)

	} //if (nDigits == 2) {

	if (decLoc == 0) sNumber = '0' + sNumber;

	return sNumber;
} 
//************************************************************************
//	end function padDecPlaces(nNumber, nDigits)
//************************************************************************

//************************************************************************
//	Function:	y2k, padout, calrestart, calWindow
//	Purpose:	popup calendar functions
//	Developer:	Matt Kaiser
//	Updated:	10/31/2000
//	Input:		n/a
//	Output:		date
//	Notes:		
//************************************************************************
function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

function padout(number) {
	return (number < 10) ? '0' + number : number;
}

function calrestart() {
	var field = eval('document.thisForm.' + whichOne);
	field.value = '' + padout(month - 0 + 1) + '/' + padout(day) + '/' + year;
	myWindow.close();
}

function calWindow(field) {
    whichOne = field;
    myWindow = open('utility/cal.htm', 'myname', 'resizable=no, width=350, height=270');
}
//************************************************************************
//	end function y2k, padout, calrestart, calWindow
//************************************************************************

//************************************************************************
//	Function:		checkAll
//	Purpose:		Sets all check boxes to state of master checkbox.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:		01/02/2001
//	Input:			category
//	Output:			n/a
//	Notes:			
//************************************************************************
function checkAll(category) {
//function checkAll() {
  for (var i = 0; i < document.thisForm.elements.length; i++) {
    var e = document.thisForm.elements[i];
		if (e.type == 'checkbox') {
			if (e.name.substring(0, 8) != 'chkAllBox') {
				switch (category) {
					case 'P' :
						if (e.name.charAt(3) == 'P') {
							e.checked = document.thisForm.chkAllBoxP.checked;
						}
						break;

					case 'U' :
						if (e.name.charAt(3) == 'U') {
							e.checked = document.thisForm.chkAllBoxU.checked;
						}
						break;

					case 'O' :
						if (e.name.charAt(3) == 'O') {
							e.checked = document.thisForm.chkAllBoxO.checked;
						}
						break;

					case 'T' :
						if (e.name.charAt(3) == 'T') {
							e.checked = document.thisForm.chkAllBoxT.checked;
						}
						break;

					case 'R' :
						if (e.name.charAt(3) == 'R') {
							e.checked = document.thisForm.chkAllBoxR.checked;
						}
						break;

					case 'BP' :
						if ((e.name.charAt(3) + e.name.charAt(4)) == 'BP') {
							e.checked = document.thisForm.chkAllBoxBP.checked;
						}
						break;

					case 'BU' :
						if ((e.name.charAt(3) + e.name.charAt(4)) == 'BU') {
							e.checked = document.thisForm.chkAllBoxBU.checked;
						}
						break;

					case 'SP' :
						if ((e.name.charAt(3) + e.name.charAt(4)) == 'SP') {
							e.checked = document.thisForm.chkAllBoxSP.checked;
						}
						break;

					case 'SU' :
						if ((e.name.charAt(3) + e.name.charAt(4)) == 'SU') {
							e.checked = document.thisForm.chkAllBoxSU.checked;
						}
						break;

					default :
						e.checked = document.thisForm.chkAllBox.checked;
				}
		  }
		}
  }
}
//************************************************************************
//	End Function:	checkAll(category)
//************************************************************************

