var popopWin2;
var popupWin;
// --------------------------------------------------------------
// Popup with Scroll bars
function openSizedWithScroll(in_uri,theWidth,theHeight) {
	var theTop=((screen.height/2)-(theHeight/2))/2;
	var theLeft=(screen.width/2)-(theWidth/2);
	var features='height=' + theHeight + ',width=' + theWidth + ',top='+theTop+',left='+theLeft+",toolbar=0,Location=0,Directories=0,Status=0,menubar=0,Scrollbars=1,Resizable=1";
	popupWin = window.open(in_uri,'ss',features);
	popupWin.focus();
}
// --------------------------------------------------------------
// Popup without Scroll bars
function openSized(in_uri,theWidth,theHeight) {
	var theTop=((screen.height/2)-(theHeight/2))/2;
	var theLeft=(screen.width/2)-(theWidth/2);
	var features='height=' + theHeight + ',width=' + theWidth + ',top='+theTop+',left='+theLeft+",toolbar=0,Location=0,Directories=0,Status=0,menubar=0,Scrollbars=0,Resizable=0";
	popupWin2 = window.open(in_uri,'ss',features);
	popupWin2.focus();
}

// --------------------------------------------------------------
// Open Rich Text
function openRichText(FieldName, FormName) {
	openSized("rte/RichText.asp?ReturnForm=" + FormName + "&ReturnField=" + FieldName,650,400);
}

// --------------------------------------------------------------
// Move row from one select to another
function MoveRow(inSource, inDest) {
	var s = inSource;
	var d = inDest;

	if (s.selectedIndex < 0 ) {
		alert('You must select a Row to move first.\n');
		return false;
	} else {
		for (looper = 0; looper < s.length; looper++) {
			var item = s.options[looper];
			//alert(item.selected);
			if (item.selected) {
				d.options[d.length] = new Option(item.text, item.value);
				s.options[looper] = null;
			}
		}
	}
}

// --------------------------------------------------------------
// Move all rows from one select to another
function MoveAllRows(inSource, inDest) {
	var s = inSource;
	var d = inDest;
	for (looper = 0; looper < s.length; looper++) {
		var item = s.options[looper];
		d.options[d.length] = new Option(item.text, item.value);
	}
	while (s.options[0]) {
		s.options[0] = null;
	}
}

// --------------------------------------------------------------
// Date Difference
function DateDiff( start, end, interval, rounding ) {

	var iOut = 0;

	// Create 2 error messages, 1 for each argument.
	var startMsg = "Check the Start Date and End Date\n"
		startMsg += "must be a valid date format.\n\n"
		startMsg += "Please try again." ;

	var intervalMsg = "Sorry the dateAdd function only accepts\n"
		intervalMsg += "d, h, m OR s intervals.\n\n"
		intervalMsg += "Please try again." ;

	var bufferA = Date.parse( start ) ;
	var bufferB = Date.parse( end ) ;

	// check that the start parameter is a valid Date.
	if ( isNaN (bufferA) || isNaN (bufferB) ) {
		alert( startMsg ) ;
		return null ;
	}

	// check that an interval parameter was not numeric.
	if ( interval.charAt == 'undefined' ) {
		// the user specified an incorrect interval, handle the error.
		alert( intervalMsg ) ;
		return null ;
	}

	var number = bufferB-bufferA ;

	// what kind of add to do?
	switch (interval.charAt(0))
	{
		case 'd': case 'D':
			iOut = parseInt(number / 86400000) ;
			if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
			break ;
		case 'h': case 'H':
			iOut = parseInt(number / 3600000 ) ;
			if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
			break ;
		case 'm': case 'M':
			iOut = parseInt(number / 60000 ) ;
			if(rounding) iOut += parseInt((number % 60000)/30001) ;
			break ;
		case 's': case 'S':
			iOut = parseInt(number / 1000 ) ;
			if(rounding) iOut += parseInt((number % 1000)/501) ;
			break ;
			default:
			// If we get to here then the interval parameter
			// didn't meet the d,h,m,s criteria.  Handle
			// the error.
			alert(intervalMsg) ;
			return null ;
	}

	return iOut ;
}

// --------------------------------------------------------------
// Date Add
function dateAdd( start, interval, number ) {

	// Create 3 error messages, 1 for each argument.
	var startMsg = "Sorry the start parameter of the dateAdd function\n"
		startMsg += "must be a valid date format.\n\n"
		startMsg += "Please try again." ;

	var intervalMsg = "Sorry the dateAdd function only accepts\n"
		intervalMsg += "d, h, m OR s intervals.\n\n"
		intervalMsg += "Please try again." ;

	var numberMsg = "Sorry the number parameter of the dateAdd function\n"
		numberMsg += "must be numeric.\n\n"
		numberMsg += "Please try again." ;

	// get the milliseconds for this Date object.
	var buffer = Date.parse( start ) ;

	// check that the start parameter is a valid Date.
	if ( isNaN (buffer) ) {
		alert( startMsg ) ;
		return null ;
	}

	// check that an interval parameter was not numeric.
	if ( interval.charAt == 'undefined' ) {
		// the user specified an incorrect interval, handle the error.
		alert( intervalMsg ) ;
		return null ;
	}

	// check that the number parameter is numeric.
	if ( isNaN ( number ) )	{
		alert( numberMsg ) ;
		return null ;
	}

	// so far, so good...
	//
	// what kind of add to do?
	switch (interval.charAt(0))
	{
		case 'd': case 'D':
			number *= 24 ; // days to hours
			// fall through!
		case 'h': case 'H':
			number *= 60 ; // hours to minutes
			// fall through!
		case 'm': case 'M':
			number *= 60 ; // minutes to seconds
			// fall through!
		case 's': case 'S':
			number *= 1000 ; // seconds to milliseconds
			break ;
			default:
			// If we get to here then the interval parameter
			// didn't meet the d,h,m,s criteria.  Handle
			// the error.
			alert(intervalMsg) ;
			return null ;
	}
	return new Date( buffer + number ) ;
}


// --------------------------------------------------------------
// Clock Functions
var clockID = 0;

function UpdateClock() {
	if(clockID) {
		clearTimeout(clockID);
		clockID  = 0;
	}

	var tDate = new Date();
	tDate = dateAdd(tDate,"s", TimeOffset);
	var tHours = tDate.getHours();
	var tAmPm = "AM";
	if (tHours > 12) {
		tHours = tHours - 12;
		tAmPm = "PM";
	}
	var tMinutes = tDate.getMinutes().toString();
	var tSeconds = tDate.getSeconds().toString();
	if (tMinutes.length == 1) {
		tMinutes = "0" + tMinutes;
	}
	if (tSeconds.length == 1) {
		tSeconds = "0" + tSeconds;
	}

	document.theClock.theTime.value = ""
		+ tHours + ":"
		+ tMinutes + ":"
		+ tSeconds + " "
		+ tAmPm;

	clockID = setTimeout("UpdateClock()", 1000);
}

function StartClock() {
	clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
	if(clockID) {
		clearTimeout(clockID);
		clockID  = 0;
	}
}

