// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Utility functions - not to be used by developers
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


//****************************************************************
// Description: Checks to see if character in string exists 
//				(normally indexOf() should be used).
// Parameters:
//  1. charTest (Character) = character to find
//	2. strInput (String) = string to test
// Returns: true if successful and false if not
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function charInString (charTest, strInput)
{   
	for (i = 0; i < strInput.length; i++){   
		if (strInput.charAt(i) == charTest) return true;
    }
    return false
}


//****************************************************************
// Description: Check to make sure all characters in input match
//				an acceptable range of characters
// Parameters:
//  1. strInput (String) = string to be tested
//	2. strValid (String) = string of valid characters
// Returns: true if successful and false if not
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function containsValidChar(strInput, strValid)
{
	var strTemp = "";
	for (var i = 0; i < strInput.length; i++) {
		strTemp = strInput.substring(i, i + 1);
		if (strValid.indexOf(strTemp) == -1) {
			return false;
		}
	}

	return true;
}


//****************************************************************
// Description: Count character occurance in string
// Parameters:
//  1. strInput (String) = string to be tested
//	2. charInput (Character) = character to be counted
// Returns: character count
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function countChar(strInput, charInput)
{
	var intCount = 0;
	for (var i = 0; i < strInput.length; i++) {
		strTemp = strInput.substring(i, i + 1);
		if (strTemp == charInput) intCount++;
	}
	
	return intCount;
}


//****************************************************************
// Description: Compare length of field to specified max length.
//				Use MAXLENGTH on text boxes, and countFieldLength()
//				on text areas.
// Parameters:
//  1. strFieldValue (String) = field to be tested
//	2. intMaxLength (Integer) = maximum length allowed
//  3. strFieldDesc (String) = display name associated with field
//	4. blnMessage (Boolean) = whether a message should be displayed
// Returns: character count
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function countFieldLength(strFieldValue, intMaxLength, strFieldDesc, blnMessage)
{
	// Check parameter(s)
	if (countFieldLength.arguments.length == 2){
		strFieldDesc = null;
		blnMessage = false;	
	} else {
		if(countFieldLength.arguments.length == 3)
			blnMessage = false;
	}
	
	// Check logic
	if (strFieldValue.length > intMaxLength) {
		if (blnMessage)
			displayMessage(blnMessage, strFieldDesc, "Some fields need to contain less than " + intMaxLength + " characters.", "'" + strFieldDesc + "' needs to contain less than " + intMaxLength + " characters.");
		return false;
	}
	return true;
}


//****************************************************************
// Description: Display message to user if specified
// Parameters:
//	1. blnMessage (Boolean) = should a message be displayed
//	2. strFieldDesc (String) = display name associated with field
//	3. strAllMsg (String) = message to display about all fields
//	4. strFieldMsg (String) = message to display about a particular field
// Returns: nothing
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function displayMessage(blnMessage, strFieldDesc, strAllMsg, strFieldMsg)
{
	if (blnMessage)
		if (isEmpty(strFieldDesc))
			alert(strAllMsg);
		else
			alert(strFieldMsg);
}


//****************************************************************
// Description: Gets the selected radio button group value
// Parameters:
//  1. rbGroup (Radio Button) = radio button group to be tested
// Returns: value of selected button
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function getSelectedRadioButtonValue(rbGroup)
{
	for (var i = 0; i < rbGroup.length; i++) {
		if (rbGroup[i].checked) {
			return i;
		}
	}
	return 0;	
}


//****************************************************************
// Description: Check to make sure value is not NULL or EMPTY
// Parameters:
//  1. strFieldValue (String) = field to be tested
//	2. strFieldDesc (String) = display name associated with field
//	3. blnMessage (Boolean) = should a message be displayed
// Returns: true if successful and false if not
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function isEmpty(strFieldValue, strFieldDesc, blnMessage)
{
	// Check parameter(s)
	if (isEmpty.arguments.length == 1){
		strFieldDesc = null;
		blnMessage = false;	
	} else {
		if(isEmpty.arguments.length == 2)
			blnMessage = false;
	}
	
	// Check logic
	var strFormatted = "";
	
	if (strFieldValue == null)
		return true;
	else
		strFormatted = trimLeadingSpaces(strFieldValue);
	
	if (strFormatted == "" || strFormatted.length == 0){
		// Check message
		displayMessage(blnMessage, strFieldDesc, "All required fields must contain values.", "'" + strFieldDesc + "' must contain a value.");
		return true;
	}
	return false;
}


//****************************************************************
// Description: Check to make sure value is between two values
// Parameters:
//  1. intInput (Integer) = value to be tested
//	2. intLow (Integer) = value must be equal to or greater than intLow
//	3. intHigh (Integer) = value must be equal to or less than intHigh
// Returns: true if successful and false if not
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function isIntegerInRange(intInput, intLow, intHigh)
{
    var intNum = parseInt (intInput);
    return ((intNum >= intLow) && (intNum <= intHigh));
}


//****************************************************************
// Description: Restrict the size of a TextArea element
// Parameters:
//  1. txaTarget (field) = text area field to restrict
//	 2. intSize (Integer) = maximum size allowed
// Returns: true if successful and false if not
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function restrictSize(txaTarget, intSize)
{
   if (txaTarget.value.length > intSize - 1) {
      txaTarget.value = txaTarget.value.substr(0, intSize - 1);
   }
}


//****************************************************************
// Description: Trims the leading and trailing spaces
// Parameters:
//  1. strInput (String) = value to be trimmed
// Returns: formatted string
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function trim(strInput)
{
	var strFormattedInput = strInput;
	strFormattedInput = trimLeadingSpaces(strFormattedInput);
	strFormattedInput = trimTrailingSpaces(strFormattedInput);
	
    return strFormattedInput;
}


//****************************************************************
// Description: Trims the leading spaces
// Parameters:
//  1. strInput (String) = value to be trimmed
// Returns: formatted string
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function trimLeadingSpaces(strInput)
{
	var i = 0;
	while ((i < strInput.length) && charInString(strInput.charAt(i), " "))
       		i++;

	return strInput.substring (i, strInput.length);
}


//****************************************************************
// Description: Trims the trailing spaces
// Parameters:
//  1. strInput (String) = value to be trimmed
// Returns: formatted string
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function trimTrailingSpaces(strInput)
{
	var i = strInput.length;
	
    while ((i > 0) && charInString(strInput.charAt(i-1), " "))
       i--;
    
    return strInput.substring (0, i);
}


//****************************************************************
// Description: Trims the white spaces in the string
// Parameters:
//  1. strInput (String) = value to be trimmed
// Returns: formatted string
//****************************************************************
//	IE4		IE5		Netscape 4+
//	---		---		-----------
//	Yes		Yes		Yes
//****************************************************************
function trimWhiteSpace(strInput) {
	
	var i = 0;
	var strReturnValue = "";
	
    while (i <= strInput.length) {
		if (strInput.charAt(i) != " ")
			strReturnValue = strReturnValue + strInput.charAt(i);
		i++;
    }
    return strReturnValue;

}