/*************************************
 *	formatNumericValue
 *	Accepts a numeric string and optionally formats it by 
 *	applying digit grouping separators. The formatted string 
 *	is returned.
 *************************************/
function formatNumericValue( text, grouping, decimal_places, allow_negatives ) {

	// set defaults
	if ( grouping == null ) grouping = true;
	if ( decimal_places == null ) decimal_places = 0;
	if ( isNaN( decimal_places ) ) decimal_places = 0;
	if ( allow_negatives == null ) allow_negatives = false;


	// make sure the text is a String
	text = new String( text );
	
	
	// remove any existing grouping characters
	var reg_exp = /,/gi;
	text = text.replace( reg_exp, '' );
	
	
	// build regular expression pattern based on formatting options
	// and extract number parts
	var pattern;
	var matches;
	var neg_char = '';
	var whole_num = '';
	var dec_char = '';
	var fraction_num = '';
	if ( decimal_places == 0 && !allow_negatives ) {
		pattern = "(\\d+)";
		reg_exp = new RegExp( pattern, "i" );
		matches = reg_exp.exec( text );
		if ( matches != null ) whole_num = RegExp.$1;
	} else if ( decimal_places == 0 ) {
		pattern = "(-?)(\\d*)";
		reg_exp = new RegExp( pattern, "i" );
		matches = reg_exp.exec( text );
		if ( matches != null ) {
			neg_char = RegExp.$1;
			whole_num = RegExp.$2;
		}
	} else if ( decimal_places > 0 && !allow_negatives ) {
		pattern = "-?(\\d*)(\\.?)(\\d{0," + decimal_places + "})";
		reg_exp = new RegExp( pattern, "i" );
		matches = reg_exp.exec( text );
		if ( matches != null ) {
			whole_num = RegExp.$1;
			dec_char = RegExp.$2;
			fraction_num = RegExp.$3;
		}
	} else {
		pattern = "(-?)(\\d*)(\\.?)(\\d{0," + decimal_places + "})";
		reg_exp = new RegExp( pattern, "i" );
		matches = reg_exp.exec( text );
		if ( matches != null ) {
			neg_char = RegExp.$1;
			whole_num = RegExp.$2;
			dec_char = RegExp.$3;
			fraction_num = RegExp.$4;
		}
	}		


	// if grouping is desired, insert comma delimiters
	if ( whole_num.length > 3 && grouping ) {
		var count = 0;
		var new_text = '';
		for ( var i=whole_num.length - 1; i>=0; i-- ) {
			count++;
			var cur_char = whole_num.substr( i, 1 );
			if ( count % 3 == 1 && count > 3 )
				new_text = "," + new_text;
			new_text = cur_char + new_text;
		}
		whole_num = new_text;
	}
	
		
	// concatenate and return formatted number
	return neg_char + whole_num + dec_char + fraction_num;

}




/*************************************
 *	formatSSNValue
 *	Accepts a string and formats it as a 
 *	Social Security Number.
 *************************************/
 function formatSSNValue( text ) {

	// make sure text is a String
	text = new String( text );
	
	// remove any non-digit characters
	var regExp = /\D/gi;
	text = text.replace( regExp, '' );
	
	// take first 9 digits
	if ( text.length > 9 ) text = text.substr( 0, 9 );
	
	// insert hyphens
	if ( text.length > 5 ) 
		text = text.substr( 0, 3 ) + '-' + text.substr( 3, 2 ) + '-' + text.substr( 5 );
	else if ( text.length == 5 )
		text = text.substr( 0, 3 ) + '-' + text.substr( 3, 2 ) + '-';
	else if ( text.length > 3 )
		text = text.substr( 0, 3 ) + '-' + text.substr( 3 );
	else if ( text.length == 3 )
		text = text.substr( 0, 3 ) + '-';
	
	// return ssn
	return text;
		
 }
 
 


/*************************************
 *	formatCurrencyValue
 *	Accepts a numeric string and optionally formats it by 
 *	applying currency symbol, decimal positioning, and digit 
 *	grouping separators. The formatted string is returned.
 *************************************/
function formatCurrencyValue( text, pad, grouping ) {

	// use grouping by default if not specified
	if ( grouping == null ) grouping = true;
	
	
	var formatted_text = new String( text );
	if ( formatted_text.length > 0 ) {
		
		// remove any non-digit, non-decimal characters
		var reg_exp = /\D\./gi;
		formatted_text = formatted_text.replace( reg_exp, '' );
		

		// only allow 2 decimal positions
		reg_exp = /\d*\.*\d{0,2}/gi;
		matches = reg_exp.exec( formatted_text );
		if ( matches.length > 0 ) 
			formatted_text = matches[0];
		if ( pad ) {
			var dec_pos = formatted_text.indexOf( '.' );
			if ( dec_pos < 0 ) {
				formatted_text += '.00';
			} else if ( dec_pos == formatted_text.length - 2 ) {
				formatted_text += '0';
			} else if ( dec_pos == formatted_text.length - 1 ) {
				formatted_text += '00';
			}
		}
		
		
		// add comma separators
		parts = formatted_text.split('.');
		var pre_dec = parts[0];
		if ( pre_dec.length > 3 && grouping ) {

			var count = 0;
			var new_text = '';
			for ( var i=pre_dec.length - 1; i>=0; i-- ) {
				count++;
				var cur_char = pre_dec.substr( i, 1 );
				if ( count % 3 == 1 && count > 3 ) 
					new_text = ',' + new_text;
				new_text = cur_char + new_text;
				
			}
			pre_dec = new_text;
		}
		if ( formatted_text.indexOf( '.' ) >= 0 )
			formatted_text = pre_dec + '.' + parts[1];
		else
			formatted_text = pre_dec;
		
	}
	
	return '$' + formatted_text;

}




/*************************************
 *	unformatNumericValue
 *	Accepts a formatted numeric string and unformats it
 *	by removing any non-digit characters. The unformatted 
 *	string is returned.
 *************************************/
function unformatNumericValue( text ) {

	var unformatted_text = text;
	if ( unformatted_text.length > 0 ) {
		
		// remove any non-digit characters
		var reg_exp = /\D/gi;
		unformatted_text = unformatted_text.replace( reg_exp, '' );

	}
	
	return unformatted_text;
}




/*************************************
 *	unformatCurrencyValue
 *	Accepts a formatted currency string and unformats it 
 *	by removing any non-digit and decimal characters.  The unformatted
 *	string is returned.
 *************************************/
function unformatCurrencyValue( text ) {

	var unformatted_text = text;
	if ( unformatted_text.length > 0 ) {
		
		// remove any non-digit, non-decimal characters
		var reg_exp = /[\D\.]/gi;
		unformatted_text = unformatted_text.replace( reg_exp, '' );
		
	}
	
	return unformatted_text;
}




