
function OpenWindow( theWin, theURL, winName, width, height )
{
	if ( theWin == null || gPasswordWindow.closed )
	{
		theWin = window.open( theURL, winName,
										"alwaysRaised=yes," +
										"dependent=yes," +
										"toolbar=no," +
										"location=no," +
										"directories=no," +
										"status=no," +
										"menubar=no," +
										"scrollbars=no," +
										"resizable=yes," +
										"copyhistory=yes," +
										"screenX=100," +
										"screenY=100," +
										"width=" + width + "," +
										"height=" + height );
	}
	else
	{
		theWin.location.href = pageName;
	}
	theWin.focus();
}

function OpenTutWindow( theWin, theURL, winName, width, height )
{
	if ( theWin == null || gPasswordWindow.closed )
	{
		theWin = window.open( theURL, winName,
										"alwaysRaised=yes," +
										"dependent=yes," +
										"toolbar=no," +
										"location=no," +
										"directories=no," +
										"status=no," +
										"menubar=no," +
										"scrollbars=yes," +
										"resizable=yes," +
										"copyhistory=yes," +
										"screenX=100," +
										"screenY=100," +
										"width=" + width + "," +
										"height=" + height );
	}
	else
	{
		theWin.location.href = pageName;
	}
	theWin.focus();
}

function OpenScrollingWindow( theWin, theURL, winName, width, height )
{
	if ( theWin == null || gPasswordWindow.closed )
	{
		theWin = window.open( theURL, winName,
										"toolbar=no," +
										"location=no," +
										"directories=no," +
										"status=no," +
										"menubar=no," +
										"scrollbars=yes," +
										"resizable=no," +
										"copyhistory=yes," +
										"width=" + width + "," +
										"height=" + height );
	}
	else
	{
		theWin.location.href = pageName;
	}
	theWin.focus();
	
	return theWin;
}

function checkURL( theURL, paramName, paramValue )
{
	var	sep = "?";
	
	if ( theURL.indexOf( "?" ) >= 0 ) {
		var	tmp		= theURL.split( "?" );
		var	nvPairs	= tmp[1].split( "&" );
		for ( var i = 0; i < nvPairs.length; i++ ) {
			var	thisPair	= nvPairs[i].split( "=" );
			if ( thisPair[0] == paramName ) {
				return theURL;		// theURL has param, nothing to fix!
			}
		}
		sep = "&";
	}
	return ( theURL + sep + paramName + "=" + paramValue );
}

function CheckLength ( textField, textName, maxLen )
{
	if ( textField.value.length > maxLen )
	{
		alert( textName + " must be less than " + maxLen + " characters." )
//		textField.value = textField.value.substring( 0, maxLen )
		textField.select()
		textField.focus()
		return false
	}
	
	return true
}


function ResizeListBox ( formListBox, initLength)
{
	var currentLength = formListBox.length;
	var i;
	var tmpString = "";
	for (i = 0; i < initLength; i++) {
		tmpString += "W"
	}

	var new_recipient = new Option(tmpString);

	formListBox.options[currentLength+1] = new_recipient;
	formListBox.length = currentLength;
}

function CompactSelectElement(menuList)
{
	// Compact down empty entries in a select element
	var len = menuList.length;
	var currentEmptyItem = 0;
	var numNonEmptyItems = 0;
	var itemsRemoved = 0;
	var i;

	for (i = 0; i < len; i++) {
		if (menuList.options[i].value == "" && menuList.options[i].text == "") {
			itemsRemoved++;
			continue;
		} else {
			menuList.options[currentEmptyItem].value = menuList.options[i].value;
			menuList.options[currentEmptyItem].text = menuList.options[i].text;
			menuList.options[currentEmptyItem].selected = menuList.options[i].selected;
			currentEmptyItem++;
			numNonEmptyItems++;
		}
	}
	
	menuList.length = numNonEmptyItems;
}


function FindItemDropDownListBox(itemValue, dropDownListBox)
{
	var i;
	var itemLocation = 0;

	dropDownListBox.length

	for (i = 0; i < dropDownListBox.length; i++) {
		if (dropDownListBox.options[i].value == itemValue) {
			itemLocation = i;
			break;
		}
	}

	return itemLocation;
}

function FindItemDropDownListBoxByText(itemText, dropDownListBox)
{
	var i;
	var itemLocation = 0;

	dropDownListBox.length

	for (i = 0; i < dropDownListBox.length; i++) {
		if (dropDownListBox.options[i].text == itemText) {
			itemLocation = i;
			break;
		}
	}
	if(i>=dropDownListBox.length)
		return -1;
	return itemLocation;
}

function AddSeparatorListBox(separatorText, separatorValue, listBox)
{
	var new_item = new Option(separatorText, separatorValue)
	var rl_length = listBox.length

	if ( (listBox.selectedIndex != -1) && ( rl_length != 0 )) {
		var currentSelection = listBox.selectedIndex;
		for (i = rl_length-1; i >= -1; i--) {
			if (i == currentSelection) {
				listBox.options[i+1] = new_item;
				listBox.selectedIndex = i+1;
				break;
			} else {
				var tempOption = new Option(listBox.options[i].text, listBox.options[i].value);
				listBox.options[i+1] = tempOption;
			}
		}
	} else {
		i = rl_length-1;
		listBox.options[i+1] = new_item;
		listBox.selectedIndex = i+1;
	}
}

function AddItemListBox(itemText, itemValue, listBox, itemMsgName, allowDupes, insertSorted)
{
	if ( itemValue == "" )
		itemValue = itemText
		
	var new_item = new Option(itemText, itemValue)
	var rl_length = listBox.length

	if (allowDupes == false) {
		// First Check to see if the item is already in the list
		for (i = rl_length-1; i >= -1; i--) {
			if (i == -1) {
			// do nothing.... let it pass through since we have an empty list
			} else if (listBox.options[i].text.toLowerCase() == new_item.text.toLowerCase()) {

//				alert ("The " + itemMsgName + " you entered is already in the list.");

				return;
			}
		}
	}

	if (insertSorted == true) {
		// Insert the item in the list in sorted order.
		for (i = rl_length-1; i >= -1; i--) {
			if (i == -1) {
				listBox.options[i+1] = new_item;
				listBox.selectedIndex = i+1;
				break;
			} else if (listBox.options[i].text.toLowerCase() > new_item.text.toLowerCase()) {
				var tempOption = new Option(listBox.options[i].text, listBox.options[i].value);
				listBox.options[i+1] = tempOption;
			} else if (listBox.options[i].text.toLowerCase() <= new_item.text.toLowerCase()) {
				listBox.options[i+1] = new_item;
				listBox.selectedIndex = i+1;
				break;
			}
		}
	} else {
		if ( (listBox.selectedIndex != -1) && ( rl_length != 0 ) ) {
			var currentSelection = listBox.selectedIndex;
			for (i = rl_length-1; i >= -1; i--) {
				if (i == currentSelection) {
					listBox.options[i+1] = new_item;
					listBox.selectedIndex = i+1;
					break;
				} else {
					var tempOption = new Option(listBox.options[i].text, listBox.options[i].value);
					listBox.options[i+1] = tempOption;
				}
			}
		} else {
			i = rl_length-1;
			listBox.options[i+1] = new_item;
			listBox.selectedIndex = i+1;
		}
	}
}


function RemoveItemFromListBox(checkItems, removeItems, listBox, elementMsgName, listMsgName)
{
	var rl_length = listBox.length
	var confirmValue = 0;
	var i;

	if (checkItems) {
		if (listBox.length == 0) {
			if (listMsgName.length > 0) {
				listMsgName += " ";
			}
//			alert("There are no " + elementMsgName + "s in the " + listMsgName + "list to delete.");
			return false;
		}

	    if (listBox.selectedIndex == -1 )
		{
//			alert("You must first select a " + elementMsgName + " item from the list to delete.");
			return false;
		}

		var currentCount = 0;
		for (i = 0; i < rl_length; i++) {
			if (listBox.options[i].selected == true) {
				currentCount++;
			}
		}

		if (currentCount > 1) {
//			confirmValue = confirm("Are you sure you want to delete these " + currentCount + " " + elementMsgName + "s?");
		} else {
//			confirmValue = confirm("Are you sure you want to delete this " + elementMsgName + "?");
		}
	} else {
		confirmValue = 1;
	}

	if (removeItems) {
		if (confirmValue)
		{
			var new_length = rl_length;
			var current_entry = 0;

			// Remove the items from the list by decrementing the total number of items
			// in the list
			for (i = 0; i < rl_length; i++) {
				if (listBox.options[i].selected == true) {
					new_length--;
				} else {
					var tempOption = new Option(listBox.options[i].text, listBox.options[i].value);
					listBox.options[current_entry] = tempOption;
					current_entry++;
				}
			}

			listBox.length = new_length;
			return true
		}
	} else {
		return confirmValue;
	}

	return confirmValue;
}


function DeselectSeparatorItem(selector, hiddenValue)
{
	var newSelect = selector[selector.selectedIndex].value
	var current = hiddenValue.value

	if ( newSelect == "" )
	{
		selector.selectedIndex = 0;
		hiddenValue.value = selector[selector.selectedIndex].value
		for ( i = 0; i < selector.length; i++ )
		{
			if ( selector[i].value == current )
			{
				selector.selectedIndex = i;
				hiddenValue.value = selector[i].value
				return
			}
		}
	}
	hiddenValue.value = selector[selector.selectedIndex].value
}

/* TrimString removes whitespace from the beginning and end of a given string.
 * The following characters are removed as whitespace: spaces (32), tabs (9), carriage returns (13), and line feeds (10)
 */
function TrimString( theStr )
{
	var i = 0
	var j = theStr.length - 1

	while( ( i < j ) &&  ( ( theStr.charAt( i ) == " " ) || ( theStr.charAt( i ) == "\t" ) || ( theStr.charAt( i ) == "\n" ) || ( theStr.charAt( i ) == "\r" ) ) ){
		i++;
	}
	
	while( ( j >= i ) && ( ( theStr.charAt( j ) == " " ) || ( theStr.charAt( j ) == "\t" ) || ( theStr.charAt( j ) == "\n" ) || ( theStr.charAt( j ) == "\r" ) ) ){
		j--;
	}
		
	return theStr.substring( i, j + 1 )
}

function CheckUsername( textField )
{
	if( textField.value == "" ){
		alert( "You forgot to enter your display name." );
		textField.select(); 
		textField.focus();
		return false;
	}


	var	firstChar	= textField.value.charAt(0);

	if ( firstChar >= "0" && firstChar <= "9") {	
		alert( "Your display name cannot start with a number." );
		textField.select();
		textField.focus();
		return false;
	}


	var iii = 0;
	var currentChar = "";

	for( iii = 0; iii < textField.value.length; iii++ ){
		currentChar = textField.value.charAt( iii );

		if (
			( currentChar < "0" || currentChar > "9") &&
			( currentChar < "a" || currentChar > "z") &&
			( currentChar < "A" || currentChar > "Z")
			){
			alert( "Your display name can only contain alphanumeric characters (i.e. 0-9, a-z, A-Z)." );
			textField.select();
			textField.focus();
			return false;
		}
	}


	return true;
}

function DotDotDotString( currentString, maxChars )
{
	if ((maxChars > 3) && (currentString.length > maxChars)) {
		currentString = currentString.substring(0, maxChars - 3) + "...";
	}

	return currentString;
}

function AlertIfEmpty( textField, alertMsg ){
	if( textField.value == "" ){
		alert( alertMsg );
		textField.select();
		textField.focus();
		return false;
	}
	return true;
}

function AlertIfNotChecked( checkbox, alertMsg ){
	if( checkbox.checked == false ){
		alert ( alertMsg );
		return false;
	}
	return true;
}

function ValidateDataNotEmpty( dataContainer, fieldName )
{
	// Only validate non-hidden fields
	if (dataContainer.type.toUpperCase() != "HIDDEN") {
		if (dataContainer.value.length == 0) {
			alert("The " + fieldName + " field is not allowed to be empty.\nPlease provide the appropriate information.");
			if(dataContainer.focus != null) {
				dataContainer.focus();
			}
			return false;
		}
	}

	return true;
}

function ValidateNumber(numberContainer, fieldName, start, end, emptyOK)
{
	// Only validate non-hidden fields
	if (numberContainer.type.toUpperCase() == "HIDDEN") {
		return true;
	} 
	if (numberContainer.value.length > 0) {
		// Make sure a text field contains only digits
		for (var i = 0; i < numberContainer.value.length; i++) {
			var digit = numberContainer.value.charAt(i);

			if ((i==0) && (digit == "-")) {
				// a negative number is ok
			} else if (digit < "0" || digit > "9") {	
				if (start == 0 && end == 0) {
					alert("Please make sure that the " + fieldName + " field contains only digits.");
				} else {
					if (end >= 999999999) {
						alert("Please make sure that the " + fieldName + " field contains a number greater than " 
							  + start + ".");
					} else {
						alert("Please make sure that the " + fieldName + " field contains a number between " 
							  + start + " and " + end + ".");
					}
				}

				SetFocusField(numberContainer);

				return false;
			}
		}
	}

	if (! emptyOK) {
		if (!ValidateDataNotEmpty(numberContainer, fieldName)) {
			return false;
		}
	}

	if ((start == 0) && (end == 0)) {

	} else if ((numberContainer.value.length == 0) && (emptyOK == 0)) {
		if (end == 999999999) {
			alert("Please make sure that the " + fieldName + " field contains an integer greater than " 
				  + start + ".");
		} else {
			alert("Please make sure that the " + fieldName + " field contains an integer between " 
				  + start + " and " + end + ".");
		}

		SetFocusField(numberContainer);

		return false;

	} else if ((numberContainer.value < start) || (numberContainer.value > end)) {
		if (end == 999999999) {
			alert("Please make sure that the " + fieldName + " field contains only integers greater than " 
				  + start + ".");
		} else {
			alert("Please make sure that the " + fieldName + " field contains only integers between " 
				  + start + " and " + end + ".");
		}

		SetFocusField(numberContainer);

		return false;
	}

	return true;
}

function ValidateDecimal(decNumberContainer, fieldName, start, end)
{
	// Only validate non-hidden fields
	if (decNumberContainer.type.toUpperCase() == "HIDDEN") {
		return true;
	}

	var theString = decNumberContainer.value;

	var commaIndex = -1;
	while( (commaIndex = theString.indexOf( ",", 0 ) ) > -1 ){
		if( commaIndex + 1 >= theString.length ){
			theString = theString.substring( 0, commaIndex );
		}
		else{
			theString = theString.substring( 0, commaIndex ) + theString.substring( commaIndex + 1, theString.length );
		}
	}

	if (theString.length > 0) {
		var decimalCount = 0;
		var decimalIndex = -1;

		for (var i = 0; i < theString.length; i++) {
			var digit = theString.charAt(i);

			if ( (digit < "0" || digit > "9") && (digit != ".") ){
				alert( fieldName + " may not contain '" + digit + "'.\n" +
						"Please enter something like: 1,234.56 or 12.3" );

				SetFocusField(decNumberContainer);

				return false;
			}

			// Make sure only one decimal point is supplied
			if (digit == ".") {
				decimalIndex = i;
				if (++decimalCount > 1) {
					alert ( fieldName + " contains too many decimal points!\n" +
							"Please enter something like: '1,234.56' or '12.3'" );
					SetFocusField(decNumberContainer);

					return false;
				}
			}
		}

		// make sure only two digits are available after the decimal point (if present)
		if ( (decimalIndex > -1) && (decimalIndex < (theString.length - 3)) ){
			alert ( fieldName + " contains too many digits after the decimal point.\n" +
							"Please enter something like: '1,234.56' or '12.3'" );
			SetFocusField(decNumberContainer);
			return false;
		}
	}

	if ((start == 0) && (end == 0)) {
		// no more checks needed.
	} else if (theString.length == 0) {
		alert( fieldName + " must be between " + start + " and " + end + ".\n" +
				"Please enter something like: '1,234.56' or '12.3'" );
		SetFocusField(decNumberContainer);

		return false;

	} else if ((theString < start) || (theString > end)) {
		if (end == 999999999) {
			alert( fieldName + " must be at least " + start + "." );
		} else {
			alert( fieldName + " must be at least " 
				  + start + " and no greater than " + end + ".");
		}
		SetFocusField(decNumberContainer);

		return false;
	}

	if ( theString.length > 0 ) {
		decNumberContainer.value = theString;
	}

	return true;
}

function ValidateEmailAddress(emailAddr)
{
	var addr_length = emailAddr.length;
	var i
	var at_check = 0;
	var dot_check = 0;
	var retVal = false;
	var errorCheck = false;
	var endCheck = true;

	var	error_msg = "The email address you entered ";
	
	// loop through and make sure the email address appears valid

	for (i = 0; i < addr_length; i++) {
		var c	= emailAddr.charAt(i)
		if (c == ' ') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a space."
			break;
		} else if (c == '\'') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a single quote."
			break;
		} else if (c == '\"') {
			retVal = false;
			errorCheck = true;
			break;
		} else if (c == '\\') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a double quote.";
			break;
		} else if (c == ',') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a comma.";
			break;
		} else if (c == ';') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a semi-colon.";
			break;
		} else if (c == ':') {
			retVal = false;
			errorCheck = true;
			error_msg += "contains a colon.";
			break;
		} else if (c == '@') {
			if (i == 0) {
				retVal = false;
				errorCheck = true;
				error_msg += "begins with an @ symbol."
				break;
			} else if (at_check) {
				// The @ symbol cannot appear more than once
				retVal = false;
				errorCheck = true;
				error_msg += "contains more than one @ symbol."
				break;
			}
			at_check = i+1;
		} else if (at_check && (c == '.')) {
			if (at_check == i) {
				retVal = false;
				errorCheck = true;
				error_msg += "contains an @ symbol immediately followed by a '.'."
				break;
			} else if (dot_check == i) {
				retVal = false;
				errorCheck = true;
				error_msg += "contains a '.' immediately followed by a '.'."
				break;
			} else if (i == addr_length-1) {
				retVal = false;
				errorCheck = true;
				error_msg += "may not end in a '.'."
				break;
			}
			dot_check = i+1;
		}
	}

	// There must be an at symbol followed by some set of characters followed
	// by a dot, which is followed by more characters.
	if (at_check && dot_check && (errorCheck == false)) {
		retVal = true;
	} else {
		if (errorCheck == false) {

			if (addr_length == 0) {
				error_msg = "You must first enter an email address."
				endCheck = false;
			} else if (at_check == false) {
				error_msg = error_msg + "does not contain an @ symbol followed by a domain name."
			} else {
				error_msg = error_msg + "does not contain a proper domain name."
			}
		}
	}

	if (endCheck == true) {
		error_msg = error_msg + "\nPlease verify that the email address is correct.";
	}

	// pop up the error message in an alert box	
	if (retVal == false) {
		alert (error_msg);
	}

	return retVal;
}

function isValidDate( year, month, day ){
	var leapyear = false;
	var months = new Array("NoMonth", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
					"November", "December");

	// make sure month is between 1 and 12
	if( month < 1 || month > 12 ) {
		alert( "Month must be between 1 and 12." );
		return false;
	}
	
	// determine leap year status
	if( (year % 4) == 0 ){
		leapyear = true;
		if( (year % 100) == 0 && (year % 400) != 0 ){
			leapyear = false;
		}
	}

	// check months with 30 days
	if( month == 4 || month == 6 || month == 9 || month == 11 ){
		if( day > 30 ){
			alert( "There are only 30 days in the month of " + months[month] + "." );
			return false;
		}
	}

	// check for feb & leap year
	if( month == 2 ){
		if( leapyear ){
			if( day > 29 ){
				alert( "There are only 29 days in the month of February in " + year + "." );
				return false;
			}
		}
		else{
			if( day > 28 ){
				alert( "There are only 28 days in the month of February in " + year + "." );
				return false;
			}
		}
	}
	return true;
}

function SetFocusField (field)
{
	if (field.focus != null) {
		field.focus();
		field.select();
	}
}

function FindElement( form, element )
{
	for ( iii = 0; iii < form.elements.length; iii++ ) {
		if ( form.elements[iii].name == element ) {
			return ( form.elements[iii] );
		}
	}

	return ( null );
}
