var HighLightColor 	= '#C7D5EF';	// Default Form Field Focused Color
var defBGColor 		= '#E5E5E7';	// Default Form Field Not Focused Color
var enabled_cell_color 	= '#F5F5F7';
var selColor 		   	= '#E5E5E7';
var unselColor		   	= '#000066';
var border_color	   	= '000066';		
var qstnr_cell_color   	= '#E5E5E7';
var denied_color	   	= 'silver';

function isBlank(s)
{   
	return ((s == null) || (s == "" ) || (s.length == 0))
}
function isNBlank(s)
{
	return (isBlank(s) || (s == "NULL"))
}


//
// isDigit(): Checks if a single digit is numeric
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is numeric
//				false if it is not numeric
//
function isDigit(character)
{   
	return ((character >= '0') && (character <= '9') && character.length == 1)
}


//
// isInt(): Detects integers
//
//	Params:		str - The str to be tested as an integer
//
//	Return:		true if the str represents an integer
//				false if not an integer
//
function isInt(str)
{
	var c;

	if (isBlank(str))
	{
		return false;		
	}

	for (i = 0; i < str.length; i++)
	{
		c = str.charAt(i);
		
		if (! isDigit(c))
		{
			return false;
		}
	}

	return true;
}


//
// isNumeric(): wrapper for isInt()
//
//	Params:		str - the str to be tested as an integer
//
//	Return: 	true if it the str represents a number
//				false if the str is not a number
//
function isNumeric(str)
{
	return isInt(str);
}


//
// isFloat(): detects floating-point numbers
//
//	Params: 	str - The str to be tested as a floating-point number
//
//	Return: 	true if str represents a floating-point number
//				false if not
//
function isFloat(str)
{
	var c;
	var i;
	var hasDecimal = false;

	if (isBlank(str))
	{
		if (isFloat.arguments.length == 1)
		{
			return NaN;
		}
		else 
		{
			return isFloat.arguments[1];
		}
	}

	if (str == '.')
	{
		return false;
	}

    for (i = 0; i < str.length; i++)
    {   
        c = str.charAt(i);

        if ((c == '.') && (! hasDecimal)) 
        {
        	hasDecimal = true;
        }
        else if (!isDigit(c))
        {
        	return false;
        }
    }

	return true;
}


//
// isDecimal(): Detects decimal numbers
//
//	Params: 	str - The str to be tested as a decimal 
//				(floating-point) number
//
//	Return:		true if str represents a decimal number
//				false if not
//
//
function isDecimal(str)
{
	return isFloat(str);
}


//
//
// isMoney(): Detects valid money values
//
//	Params: 	str - The str to be tested as a money value
//
//	Return:		true if str is an integer or is a floating-point number which 
//					 contains exactly 2 digits right of the decimal point
//				false if not
//
function isMoney(str)
{
	if (isInt(str))
	{
		return true;
	}
	else if (isFloat(str))
	{
		return (str.charAt(str.length - 3) == '.');
	}
	else
	{
		return false;
	}
}


//
//
// isAlpha(): Detects strs consisting solely of alphabetic characters
//
//	Params: 	str - The str to be tested
//
//	Return: 	true if str consists solely of alphabetic characters 
//				false if not
//
function isAlpha(str)
{
	var c;

	if (isBlank(str))
	{
		return false;	
	}

	for (var i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if (!isLetter(c))                                                   
		{
			return false; 
		}
	}

	return true;	
}

//
// isAlphaNumeric(): Detects alphanumeric strings 
//
//	Params: 	str - The str be tested
//
//	Return: 	true if str consists only of alphabetic and numeric characters
//				false if it does not
//
function isAlphaNumeric(str)
{
	var c;

	if (isBlank(str))
	{
		return false;	
	}

	for (var i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if ((! isLetter(c)) && (! isDigit(c)))
		{
			return false; 
		}
	}

	return true;	
}


//
// isLetter(): Checks if a single character is alphabetic
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is alphabetic
//				false if the character is not alphabetic or if a 
//				multi-character str is passed
//
function isLetter(c)
{
	if ((isBlank(c)) || (c.length != 1))
	{
		return false;
	}

	return (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
}

//
//	isDate(): Detects if a given date is valid under the given mask
//
//	Params:		date - The date to be tested
//				mask - Optional, assumed to be yyyy-mm-dd if not supplied
//
//	Return:		true if the date fits the mask and is a valid date
//				false for all other cases
//
function isDate(str)
{
	var mask;
	
	if (isBlank(str))
	{
		false;
	}
	
	if (isDate.arguments.length == 2)
	{
		mask = isDate.arguments[1];
	}
	else
	{
		mask = "yyyy-mm-dd";
	}
	
	var test = getGenericDate(str, mask);
	
	return (test != "BAD DATE");
}

//
//	getGenericDate(): Converts a date and mask to a yyyy-dd-mm format
//
//	Params:		date - The date string to be converted
//
//				mask -  A mask representing the format of the date. The
//						mask must use the character "y" to represent a space for the
//						year, the character "m" to represent a space for the month,
//						and the character "d" to represent a space for the date.
//
//						The year must consist of four characters.
//						The month may consist of either two or three characters. If
//						the month consists of three characters, it is assumed to
//						represent an abbreviation of the name; (e.g. Jan, Feb ...).
//						If the month consists of two characters, it is assumed to
//						be a numerical representation of the month. The date may
//						only consist of two charcaters. The year, month, and date
//						sections must be seperated by a one-character delimiter,
//						which may or may not be the the same in both places.
//
//	Return: 	The given date in the format yyyy-mm-yy if mask and date are valid
//				The str "BAD DATE" if either date or mask are not valid, 
//				or if the date does not fit the mask
//
function getGenericDate(str, mask)
{
	var returnDate = "BAD DATE";
	
	var c;
	var delimiter1		= '0';
	var delimiter2		= '0';
	var yearPosition;
	var yearLength		= 0;
	var monthPosition;
	var monthLength		= 0;
	var datePosition;
	var dateLength		= 0;
	var parsePosition	= 0;
	var currentElement	= "none";
	var element			= new Array();
	var n;
	
	for (n = 0; n < mask.length; n++)
	{
		c = mask.charAt(n);
		switch (c)
		{
			case 'y':
			case 'Y':
				if (currentElement != "year")
				{
					currentElement = "year";
					element[parsePosition] = "year";
					yearPosition = parsePosition++;
					yearLength = 0;
				
				}
				yearLength++;
				break;
			case 'd':
			case 'D':
				if (currentElement != "date")
				{
					currentElement = "date";
					element[parsePosition] = "date";
					datePosition = parsePosition++;
					dateLength = 0;
				
				}
				dateLength++;
				
				break;
			case 'm':
			case 'M':
				if (currentElement != "month")
				{
					currentElement = "month";
					element[parsePosition] = "month";
					monthPosition = parsePosition++;
					monthLength = 0;
				
				}
				monthLength++;
				
				break;
			default:
				if (delimiter1 == '0')
				{
					// initialize delimiter
					delimiter1 = c;
				}
				else if (delimiter2 == '0')
				{
					delimiter2 = c;
				}
				else
				{
					// c does not match a valid mask charcater
					// or previously found delimiter
					return returnDate;
			
				}
		}
	}
	
	if ((yearLength != 2 && yearLength != 4)   ||
		(monthLength != 3 && monthLength != 2) ||
		(dateLength != 2))
	{
		return returnDate;
	}
	
	var tempArray = new Array();
	var finalArray  = new Array();
	
	if (delimiter1 == delimiter2)
	{
		tempArray = str.split(delimiter1);
		finalArray = tempArray;
	
		if (finalArray.length != 3)
		{
			return returnDate;
		}
	}
	else
	{
		tempArray = str.split(delimiter1);
		
		if (tempArray.length != 2)
		{
			return returnDate;
		}
		else
		{
			finalArray[0] = tempArray[0];
			var tempArray2 = new Array(); //  first element parsed
			tempArray2 = tempArray[1].split(delimiter2);
			if (tempArray2.length != 2)
			{
				return returnDate;
			}
			else
			{
				finalArray[1] = tempArray2[0];
				finalArray[2] = tempArray2[1];
			}
		}
	}
	
	var year;
	var month;
	var date;
	
	for (n = 0; n < 3; n++)
	{
		switch (element[n])
		{
			case "year":
				year = finalArray[n];
				if ((!isInt(year)) || (year.length != yearLength))
				{
					return returnDate;
				}
				if (yearLength == 4)
				{
					if (year < "1890" || year > "2100")
					{
						return returnDate;
					}
				}
				else  //  Two digit year
				{
					if (year < "20")
					{
						year = "20" + year;
					}
					else
					{
						year = "19" + year;
					}
				}
				break;
			case "month":
				month = finalArray[n];
				if (monthLength == 3)
				{
					month = month.toUpperCase();
					switch (month)
					{
						case "JAN":
							month = "1";
							break;
						case "FEB":
							month = "2";
							break;
						case "MAR":
							month = "3";
							break;
						case "APR":
							month = "4";
							break;
						case "MAY":
							month = "5";
							break;
						case "JUN":
							month = "6";
							break;
						case "JUL":
							month = "7";
							break;
						case "AUG":
							month = "8";
							break;
						case "SEP":
							month = "9";
							break;
						case "OCT":
							month = "10";
							break;
						case "NOV":
							month = "11";
							break;
						case "DEC":
							month = "12";
							break;
						default:
							return returnDate;
					}
				}
				else
				{
					if (month.charAt(0) == '0')
					{
						month = month.charAt(1);
					}
					switch (month)
					{
						case "1":
						case "2":
						case "3":
						case "4":
						case "5":
						case "6":
						case "7":
						case "8":
						case "9":
						case "10":
						case "11":
						case "12":
							break;
						default:
							return returnDate;
					}
				}
				break;
			case "date":
				date = finalArray[n];
				if (date.charAt(0) == '0')
				{
					date = date.charAt(1);
				}
				break;
		}
	}
	
	if (validDate(month, date, year))
	{
		returnDate = year + "-" + month + "-" + date;
	}
	
	return returnDate;
}

//
// isUpperCase(): Detects strs consisting solely of upper-case letters
//
//	Params: 	str - The str to be tested
//
//	Return: 	true if the str consists only of uppercase alphabetic chars
//				false if it does not
//

//
//
//
function validDate(month, date, year)
{
	switch (month)
	{
		case "1": // Jan
		case "3": // Mar
		case "5": // May
		case "7": // Jul
		case "8": // Aug
		case "10":// Oct
		case "12": // Dec
			if ((date > 0) && (date <= 31))
			{
				return true;
			}
			break;
		case "4": // Apr
		case "6": // Jun
		case "9": // Sep
		case "11": // Nov
			if ((date > 0) && (date <= 30))
			{
				return true;
			}
			break;
		case "2": // Feb
			var daysInFeb;
			if (year % 4 != 0)
			{
				daysInFeb = 28;
			}
			else if (year % 400 == 0)
			{
				daysInFeb = 29;
			}
			else if (year % 100 == 0)
			{
				daysInFeb = 28;
			}
			else
			{
				daysInFeb = 29;
			}
			if ((date > 0) && (date <= daysInFeb))
			{
				return true;
			}
			break;
	}
	
	return false;
}

function isUpperCase(s)
{
	var c;

	if (isBlank(s))
	{
		return false;
	}

	for (var i = 0; i < s.length; i++)
	{
		c = s.charAt(i);

		if (! isUpper(c))
		{
			return false;
		}
	}

	return true;
}

//
// isUpper(): Checks if a single character is an upper-case letter
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is an upper-case letter
//				false if it is not an upper-case letter
//
function isUpper(c)
{
	if (isBlank(c) || (c.length != 1))
	{
		return false;
	}

	return ((c >= 'A') && (c <= 'Z'));
}

//
// isPunctuation(): Checks if a single character is a punctuation character
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if c is a punctuation character
//				false if c is not a single punctuation character 
//
function isPunctuation(c)
{
	if (isBlank(c) || (c.length != 1))
	{
		return false;	
	}

	return ((c == '`') 	|| (c == '~') || (c == '!')	 ||
			(c == '@') 	|| (c == '#') || (c == '$')	 ||
			(c == '%')	|| (c == '^') || (c == '&')	 ||
			(c == '*') 	|| (c == '(') || (c == ')')	 ||
			(c == '-') 	|| (c == '_') || (c == '=')	 ||
			(c == '+') 	|| (c == '[') || (c == '{')	 ||
			(c == ']') 	|| (c == '}') || (c == '\\') ||
			(c == ';') 	|| (c == ':') || (c == '\'') ||
			(c == '\"') || (c == ',') || (c == '<')	 ||
			(c == '.') 	|| (c == '>') || (c == '/')	 ||
			(c == '?'));
}

//
// stripNonNumeric(): Returns only numberic characters in a str
//
//	Params: 	s - the input str
//
//	Return: 	numeric character in input str or empty str
//              if s was null or empty
//
function stripNonNumeric(s)
{
	var returnstr = "";
	var c;

	if (s == null)
	{
		return "";		
	}

    for (var i = 0; i < s.length; i++)
    {
        c = s.charAt(i);

        if (isInt(c))
        {
        	returnstr += c;
        }
    }

    return returnstr;
}

//
// reformat: Very simple numeric string reformatter
//
//  Params: input - The String to be reformated
//			mask  - The mask to validate against. '#' represents numbers. 
//			        All non-'#' characters in mask are added to input
//					All non-numeric characters in input are stripped
//
// Returns: The formated string or "" on error
//
function reformat(input, mask)
{
	var inputPos = 0;
	var maskPos = 0;
	var outstr = "";
	var c;
	var in_c;
	
	input = stripNonNumeric(input);
	
	if ((input == "") && (mask.charAt(0) != '#'))
	{
		return "";
	}
	
	while ((inputPos <= input.length) && (maskPos < mask.length))
	{
		c = mask.charAt(maskPos);

		if (c != '#')
		{
			outstr += c;
		}
		else
		{
			in_c = input.charAt(inputPos);
			outstr += in_c;
			inputPos++;
		}

		maskPos++;
	}

	return outstr;
}

//
// setCursorPosition(): Sets the cursor location in a text box
//
// Params:	Control - the textbox object
//			Position - integer position to move cursor to	
//
// Returns:	void
//
function setCursorPosition(Control, Position)
{
	tr = Control.createTextRange();
	size = Control.value.length;
 
	tr.moveEnd("character", Position - size);
	tr.moveStart("character" , Position  );

	tr.select();
	tr.collapse(true);
}

//
// doReformat(): Used in a timer in autoFormat. Do not invoke directly.
//
function doReformat(el, mask)
{
	var inval		= el.value;
	var outval;

	outval = reformat(inval, mask);
	el.value = outval;
}

//
// autoFormat(): Event Handler to force formatted input in text boxes
//
//  Params:		mask - the mask to force (See reformat())
//
//  Returns:	void
//
//	Example: (Phone Number)
// 
//			<INPUT TYPE="text" SIZE=14 MAXLENGTH=14
//			 onKeyDown='autoFormat("(###) ###-####")'
//			 onClick='setCursorPosition(this, this.value.length + 1)'>
//
function autoFormat(mask)
{
	srcElement	= event.srcElement;
	var keyCode = event.keyCode;
	
	if ((keyCode == 39) || (keyCode == 37))
	{
		srcElement.keyCode = 0;
	}
	
	if (keyCode != 8)
	{
		setTimeout('doReformat(srcElement, "' + mask + '")', 0);
	}
}

function forceNumeric()
{
	var keyCode = event.keyCode;
	var char = String.fromCharCode(keyCode)

	return (isDigit(char))
}

function isEmail(address)
{
	var idx;
	var domain;

	if (isBlank(address))
	{
		return false;
	}

	idx = address.indexOf('@');

	if (idx == -1)
	{
		return false;
	}

	if (address.charAt(0) == "@")
	{
		return false;
	}

	if (address.indexOf('@', idx + 1) != -1)
	{
		return false;
	}

	domain = address.substr(idx + 1);

	if ((isBlank(domain)) || (domain.indexOf("..") != -1) || 
		(domain.indexOf(".") == -1) || (domain.charAt(0) == ".") || 
		(domain.charAt(domain.length - 1) == "."))
	{
		return false;
	}

	return true;
}

function formatPhone(inphone)
{
	if ((isBlank(inphone)) || (inphone.length != 10))
	{
		return inphone;
	}

	return "(" + inphone.substr(0, 3) + ") " + inphone.substr(3, 3) + "-" + 
		   inphone.substr(6, 4);
}


//
// getFormElementByName(): Returns the object reference to a named form
//                         element. Note that this method assumes that
//                         the form element is part of document.forms[0]
//
// Params: name - The name of the element
//
// Return: The object reference to the form element or null
//
function getFormElementByName(name)
{
	return document.forms[0].elements.item(name);
}


//
// getFormElement(): Returns the object reference to a named form
//                   element. Note that this method assumes that
//                   the form element is part of document.forms[0]
//
// Params: inObj - The name of the element or its DOM reference
//
// Return: The object reference to the form element or null
//
function getFormElement(inObj)
{
	if (typeof(inObj) == "string")
	{
		return getFormElementByName(inObj);
	}
	else
	{
		return inObj
	}
}


//
// getSelectIndex(): Returns index of selected option in a single select box
//
// Params: select - The DOM object reference of the SELECT tag - or -
//                  the name of a select tag in document.forms[0]
//
// Return: The index of the selected OPTION
//         -1 on error
//
function getSelectIndex(inObj)
{
	var select 	= getFormElement(inObj);
	var opts 	= select.options;
	
	for (var i = 0; i < opts.length; i++)
	{
		if (opts[i].selected)
		{
			return i;	
		}
	}
	
	return -1;
}


//
// getSelectValue(): Returns string value of selected option in a single select
//
// Params: select - The DOM object reference of the SELECT tag - or -
//                  the name of a select tag in document.forms[0]
//
// Return: The value of the selected OPTION
//         empty string on error
//
function getSelectValue(inObj)
{
	var select 	= getFormElement(inObj);
	var opts 	= select.options;
	
	for (var i = 0; i < opts.length; i++)
	{
		if (opts[i].selected)
		{
			return opts[i].value;
		}
	}
	
	return "";
}


//
// setSelectIndex(): Sets the highlighted OPTION in a SELECT
//
// Params: select - The DOM object reference of the SELECT tag - or -
//                  the name of a select tag in document.forms[0]
//         index  - The index of the OPTION to select
//
// Return: true on success
//         false on error (i.e. index does not exist)
//
function setSelectIndex(inObj, index)
{
	var select 	= getFormElement(inObj);
	
	if (select.length <= index)
	{
		return false;	
	}
	else
	{
		select.options[index].selected = true;
		return true;
	}
}


//
// setSelectValue(): Sets the highlighted OPTION in a SELECT
//
// Params: select - The DOM object reference of the SELECT tag - or -
//                  the name of a select tag in document.forms[0]
//         value  - The value of the OPTION to select
//
// Return: true on success
//         false on error (i.e. no option matches value)
//
function setSelectValue(inObj, value)
{
	var select 	= getFormElement(inObj);
	var opts 	= select.options;
	
	for (var i = 0; i < opts.length; i++)
	{
		if (opts[i].value == value)
		{
			opts[i].selected = true;
			return true;
		}
	}
	
	return false;
}


//
// insertSelectOption(): Adds an OPTION to a SELECT
//
// Params: select - The DOM object reference of the SELECT tag - or -
//                  the name of a select tag in document.forms[0]
//         value  - The value of the new OPTION 
//		   text   - The displayed text of the new OPTION
//         [pos]  - Index position of the new OPTION (defaults to end)
//
// Return: true on success
//         false on error
//
function insertSelectOption(inObj, value, text)
{
	var select 	= getFormElement(inObj);
	var newOpt 	= document.createElement("OPTION");
	
	newOpt.text = text;
	newOpt.value = value;

	if (arguments.length == 4)
	{
		select.add(newOpt, arguements[3]);
	}
	else
	{
		select.add(newOpt);
	}
	
	return true;
}


//
// deleteSelectOption(): Adds an OPTION to a SELECT
//
// Params: select     - The DOM object reference of the SELECT tag - or -
//                      the name of a select tag in document.forms[0]
//         critereon  - A string value or number index to delete
//
// Return: true on success
//         false on error
//
function deleteSelectOption(inObj, criterion)
{
	var select 	= getFormElement(inObj);

	if (typeof(criterion) == "number")
	{
		if (select.options.length >= criterion)
		{
			return false;
		}
		
		select.remove(criterion);
		return true;
	}
	else if (typeof(criterion) == "string")
	{
		for (var i = 0; i < select.options.length; i++)
		{
			if (select.options[i].value == criterion)
			{
				select.remove(i);
				return true;	
			}
		}

		return false;
	}
	else
	{
		return false;	
	}
}


function clearSelect(inObj)
{
	var select  = getFormElement(inObj);
	var numopts = select.options.length;

	for (var i = 0; i < numopts; i++)
	{
		select.remove(0);
	}

	return true;
}


//
// getRadioIndex(): Returns the index of the selected radio button
//
// Params: radio - The DOM object reference to an array of Radio Buttons
//
// Return: The index of the selected radio button
//         -1 if none are selected
//
function getRadioIndex(inObj)
{
	var radio = getFormElement(inObj);

	for (var i = 0; i < radio.length; i++)
	{
		if (radio[i].checked)
		{
			return i;	
		}
	}
	
	// Single element Radio Groups are not Arrays
	if ((! radio.length) && (radio.type.toUpperCase() == "RADIO") &&
		(radio.checked))
	{
		return 0;
	}
	
	return -1;
}


//
// getRadioValue(): Returns the value of the selected radio button
//
// Params: radio - The DOM object reference to an array of Radio Buttons
//
// Return: The value of the selected radio button
//         Empty String if none are selected
//
function getRadioValue(inObj)
{
	var radio = getFormElement(inObj);
	
	for (var i = 0; i < radio.length; i++)
	{
		if (radio[i].checked)
		{
			return radio[i].value;
		}
	}
	
	// Single element Radio Groups are not Arrays
	if ((! radio.length) && (radio.type.toUpperCase() == "RADIO") &&
		(radio.checked))
	{
		return radio.value;
	}
	
	return "";
}

//
// getRadioValue(): Returns the Text Label of the radio button
//
// Params: radio - The DOM object reference to an array of Radio Buttons
//
// Return: The value of the selected radio button text label
//         Empty String if none are selected
//
function getRadioLabel(inObj)
{
	var radio = getFormElement(inObj);
	
	for (var i = 0; i < radio.length; i++)
	{
		if (radio[i].checked)
		{
			return radio[i].outerText;
		}
	}
	
	return "";
}


//
// setRadioIndex(): Selects a radio button by index
//
// Params: radio - The DOM object reference to an array of Radio Buttons
//         index - The index of the radio button to select
//
// Return: true on success
//         false on failure (i.e. no such index)
//
function setRadioIndex(inObj, index)
{
	var radio = getFormElement(inObj);
	
	// Single element Radio Groups are not Arrays
	if ((! radio.length) && (radio.type.toUpperCase() == "RADIO"))
	{
		radio.checked = true;
		return true;
	}
	else if (radio.length <= index)
	{
		return false;	
	}
	else
	{
		radio[index].checked = true;
		return true;
	}
}


//
// setRadioValue(): Selects a radio button by value
//
// Params: radio - The DOM object reference to an array of Radio Buttons
//         value - The value of the radio button to select
//
// Return: true on success
//         false on failure (i.e. no matching value)
//
function setRadioValue(inObj, value)
{
	var radio = getFormElement(inObj);

	for (var i = 0; i < radio.length; i++)
	{
		if (value == radio[i].value)
		{
			radio[i].checked = true;
			return true;
		}
	}
	
	return false;
}


//
// getCheckboxValue(): Get the value of a checkbox
//
// Params: checkbox  - The DOM object reference to the checkbox
//         [boolean] - Flag for boolean return value (default: false)
//
// Return: 'T' or true if the checkbox is checked
//         'F' or false if the checkbox is unchecked
//
function getCheckboxValue(inObj)
{
	var checkbox		= getFormElement(inObj);
	var booleanRetVal 	= false;

	if (arguments.length == 2)
	{
		if (arguements[1] == true)
		{
			booleanRetVal = true;
		}	
	}
	
	if (booleanRetVal)
	{
		return checkbox.checked;
	}
	else
	{
		return (checkbox.checked) ? 'T' : 'F';
	}
}


//
// setCheckboxValue(): Set the value of a checkbox
//
// Params: checkbox - The DOM object reference to the checkbox
//         value    - The value to set to (boolean or string)
//
// Return: true on success
//         false on failure (i.e. bad input)
//
function setCheckboxValue(inObj, value)
{
	var checkbox	= getFormElement(inObj);
	
	if (typeof(value) == 'string')
	{
		value = value.toLowerCase();

		if ((value == 't')		||
			(value == 'y')  	||
			(value == 'true')	||
			(value == 'on')		||
			(value == 'yes'))
		{
			value = true;
		}
		else if ((value == 'f')		||
				 (value == 'n')  	||
				 (value == 'false')	||
				 (value == 'off')	||
				 (value == 'no'))
		{
			value = false;
		}
		else
		{
			return false;	
		}
	}

	checkbox.checked = value;
	return true;
}


//
// setMaxLength(): Event handler to restrict input size of TEXTAREA objects
//                 Use as the onKeyPress handler. 
//                 (ex: onKeyPress='return setMaxLength(this, 25)' )
//
//                 Note, this method can not block pastes yet.
//
// Params: txtarea   - The DOM object reference to the TEXTAREA
//         maxLength - The maximum number of characters allowed in TEXTAREA
//
// Return: false if character exceeds length (cancelling keypress)
//         true if keypress is ok
//
function setMaxLength(inObj, maxLength)
{
	var txtarea 	= getFormElement(inObj);

	if (txtarea.value.length >= maxLength)
	{
		// Take care of selection then retype
		if (document.selection.type == "None")
		{
			return false;
		}
	}
}


//
// clearTextArea(): Clears text in a TEXTAREA object
//
// Params: txtarea   - The DOM object reference to the TEXTAREA
//
// Return: Nothing
//
function clearTextArea(inObj)
{
	var txtarea 	= getFormElement(inObj);

	txtarea.innerText = "";	
}


//
// getValue(): Retrieves value of any form element
//
// Params: formElement   - The DOM object reference to a form element
//         [errorRetVal] - Alternate return value on error (default: "")
//
// Return: formElement's value on success
//         errorRetVal on failure
//
function getValue(inObj)
{
	var errorRetVal	= "";
	var formElement	= getFormElement(inObj);
	var elementType;

	if (typeof(formElement) != "object")
	{
		return errorRetVal;	
	}

	// Try to get the type of form element
	// this may fail if a bad element type
	// was passed in
	try
	{
		// Check for radio button group
		elementType = formElement.type;
		
		if ((formElement.length != null) &&
			(elementType == null))
		{
			elementType = formElement[0].type;
		}
	}
	catch (ex)
	{
		return errorRetVal;
	}

	switch (elementType)
	{
		case 'edit':		// Bad type. Common error. Renders as 'text'
		case 'text':
		case 'textarea':
		case 'password':
			return formElement.value;
			break;
		case 'select-one':
			return getSelectValue(formElement);
			break;
		case 'checkbox':
			return formElement.checked;
			break;
		case 'radio':
			return getRadioValue(formElement);
			break;
		default:
			return errorRetVal;
			break;
	}
}


//
// setValue(): Generic value setter for form elements
//
// Params: formElement - The DOM object reference to a form element
//         value       - The value to set the element to
//
// Return: true on success
//         false on failure
//
function setValue(inObj, value)
{
	var formElement	= getFormElement(inObj);
	var elementType;
	
	if (typeof(formElement) != "object")
	{
		return false;
	}

	// Try to get the type of form element
	// this may fail if a bad element type
	// was passed in
	try
	{
		// Check for radio button group
		elementType = formElement.type;
		
		if ((formElement.length != null) &&
			(elementType == null))
		{
			elementType = formElement[0].type;
		}
	}
	catch (ex)
	{
		return false;
	}

	switch (elementType)
	{
		case 'edit':		// Bad type. Common error. Renders as 'text'
		case 'text':
		case 'textarea':
		case 'password':
			formElement.value = value;
			return true;
			break;
		case 'select-one':
			return setSelectValue(formElement, value);
			break;
		case 'checkbox':
			return setCheckboxValue(formElement, value);
			break;
		case 'radio':
			return setRadioValue(formElement, value);
			break;
		default:
			return false;
			break;
	}
}

function setSpanValue(spanname, value)
{
	var elements 	= document.getElementsByName(spanname);
	var setone		= false;

	if (value == null)
	{
		return false;	
	}

	for (var i = 0; i < elements.length; i++)
	{
		if (elements[i].tagName.toUpperCase() == 'SPAN')
		{
			elements[i].innerText = value + " ";
			setone = true;
		}
	}
	
	return setone;
}

function hideElement(elementname)
{
	var elements 	= document.getElementsByName(elementname);

	for (var i = 0; i < elements.length; i++)
	{
		try
		{
			elements[i].style.display = 'none';
		}
		catch (err)
		{
			// Do nothing. Continue Looping
		}
	}
}


function showElement(elementname, displaytype)
{
	var elements;
	
	if (elementname == null)
	{
		return false;
	}

	if (displaytype == null)
	{
		displaytype = "inline";	
	}
	
	elements = document.getElementsByName(elementname);

	for (var i = 0; i < elements.length; i++)
	{
		try
		{
			elements[i].style.display = displaytype;
		}
		catch (err)
		{
			// Do nothing. Continue Looping
		}
	}
	
	return true;
}


function showInlineElement(elementname)
{
	return showElement(elementname, 'inline')
}


function showBlockElement(elementname)
{
	return showElement(elementname, 'block')
}


function allToUpper()
{
	var formobj;
	var form_el;
	var el_type;

	formobj = document.forms[0];

	for (i = 0; i < formobj.length; i++)
	{
		form_el = formobj.elements[i];
		el_type = form_el.type;

		// Edit isn't legal, but a common mistake (unknown types are
		// rendered as text types)
		if ((form_el.type == 'edit') || (form_el.type == 'text'))
		{
			form_el.value = form_el.value.toUpperCase();
		}
	}
}

function getCalendar(textobj_name)
{
	var args	= new Object();
	var value	= getValue(textobj_name);
	var month;
	var year;
	var retval;

	if (! isBlank(value))
	{
		try
		{
			month = value.split("/")[0];
			year = value.split("/")[2];

			if ((parseInt(month) < 12) && (parseInt(month) > 0) && 
				(year.length == 4))
			{
				args.month = month;
				args.year = year;
			}
		}
		catch (err)
		{
		}
	}


	retval = window.showModalDialog("/includes/calendar.htm", args,
			"dialogHeight: 250px; dialogWidth: 300px; status: no; " +
			"resizable: yes; edge: raised; scroll: no;");

	if ((retval == "") || (retval == null))
	{
		return;
	}

	setValue(textobj_name, retval);
}

function trim(str) 
{
	while (str.substring(0,1) == ' ')
	{
		str = str.substring(1, str.length);
	}
	while (str.substring(str.length-1, str.length) == ' ')
	{
		str = str.substring(0,str.length-1);
	}
	return str;
}


