// this could be put in application.properties in the errors.header value...
// but it would have to be one huge line...
function setErrorFocus() {
	var errors;
	var ctlName;
	var i, j;
	var frm = document.forms['errorForm'];

	if (frm.errorMessages != null) {
		errors = frm.errorMessages;
	}

	for (i=0;i<errors.options.length;i++) {
		if (errors.options[i].selected) {
			ctlName = errors.options[i].value;
			break;
		}
	}

	if (ctlName!=" ") {
		for (j=0;j<document.forms.length;j++) {
			frm = document.forms[j];
			for (i=0;i<frm.elements.length;i++) {
				if (frm.elements[i].name == ctlName) {
					frm.elements[i].focus();
					//frm.elements[i].select();
					break;
				}
			}
		}
	}
}




/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS:
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid
  numeric value.  
*************************************************/
function phoneNumber( strValue ) {
	//replace all parens, hyphens, and spaces with empty strings
	var nonNumericRegExp = /\(|\)|\-| /g; 
	strValue = strValue.replace(nonNumericRegExp,'');

	var phone10RegExp = /^(\d{3})(\d{3})(\d{4})$/;
	if (phone10RegExp.test(strValue)) {
		strValue = strValue.replace(phone10RegExp, '($1)$2-$3');
	}
	return strValue;
}


/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS:
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid
  numeric value. If not, returns original value.
*************************************************/
function addCurrency( strValue ) {

	//replace all matches with empty strings
	var commaRegExp = /,/g; 
	strValue = strValue.replace(commaRegExp,'');
	//replace dollar sign with empty string
	var dollarRegExp = /\$/g; 
	strValue = strValue.replace(dollarRegExp,'');

	var twoOrMoreDecRegExp = /(\d+)\.(\d\d)\d*/;
	var oneDecRegExp = /(\d+)\.(\d)/;
	var noDecRegExp = /([0-9]+)\.?/;

	if (twoOrMoreDecRegExp.test(strValue)) {
		strValue = strValue.replace(twoOrMoreDecRegExp, '$1');
	} else if (oneDecRegExp.test(strValue)) {
		strValue = strValue.replace(oneDecRegExp, '$1');
	} else if (noDecRegExp.test(strValue)) {
		strValue = strValue.replace(noDecRegExp, '$1');
	}
	
	var objRegExp = /-?[0-9]+$/;

	if( objRegExp.test(strValue)) {
		strValue = addCommas(strValue);
		return '$' + strValue;
	} else {
		return strValue;
	}
}


/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
  strValue - source string containing commas.

RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is
  returned.

REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
function addCommas( strValue ) {
	var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

	//check for match to search criteria
	while(objRegExp.test(strValue)) {
	   //replace original string with first group match,
	   //a comma, then second group match
	   strValue = strValue.replace(objRegExp, '$1,$2');
	}
	return strValue;
}

