/*==============================================================================
$Id: $
Copyright (c) 2009 aatranslations

General purpose functions.
==============================================================================*/


/*------------------------------------------------------------------------------
Checks if a string can be converted into a 32-bit unsigned integer.
'str' -- is the string representing the numeric value with the format (i),
where 'i' is the integer part in the range 0 - 4294967295.
Returns a numeric value or null if the string cannot be converted.
NOTE: Keep in sync with CheckIntUnsigned() in common/inc/fn.php.
------------------------------------------------------------------------------*/

function checkIntUnsigned(str){
	var n;
	if(str.match(/^\d+$/)){
		n = str - 0;
		if(n <= 4294967295)
	 		return n;
 	}
	return null;
}

/*------------------------------------------------------------------------------
Checks if a string can be converted into a numeric value.
'str' -- is the string representing the numeric value with the format
([-](i[.f] | .f)), where 'i' is the integer part, 'f' is the decimal part and
'.' is the decimal separator. Integer part can contain thousands separators,
but in that case all of them must appear and must be situated in the correct
position every three digits.
'thousands_sep' -- is the thousands separator (single character).
'decimal_sep' -- is the decimal separator (single character).
Returns a numeric value or null if the string cannot be converted.
NOTE: Keep in sync with CheckNumber() in common/inc/fn.php.
------------------------------------------------------------------------------*/

function checkNumber(str, thousands_sep, decimal_sep){
	if(str.match(new RegExp("^-?((\\d+|(\\d{1,3}(\\" + thousands_sep + "\\d{3})+))(\\" + decimal_sep + "\\d+)?|(\\" + decimal_sep + "\\d+))$")))
		return str.replace(new RegExp("\\" + thousands_sep, "g"), "").replace(new RegExp("\\" + decimal_sep, "g"), ".") - 0;
	return null;
}

/*------------------------------------------------------------------------------
Formats a numeric value to show.
'number' -- is the number to format.
'thousands_sep' -- is the thousands separator.
'decimal_sep' -- is the decimal separator.
'min_decimals' -- is the minimum number of decimals to show.
'max_decimals' -- is the maximum number of decimals to show.
------------------------------------------------------------------------------*/

function formatNumber(number, thousands_sep, decimal_sep, min_decimals, max_decimals){
	var s, n, j, k;
	s = round(number, max_decimals) + "";
	n = s.indexOf(".");
	if(n == -1){
		n = s.length;
		if(min_decimals)
			s += decimal_sep;
	}else
		s = s.substring(0, n) + decimal_sep + s.substring(n + 1);
	for(j = Math.max(s.length - (n + 1), 0); j < min_decimals; s += "0", j++);
	for(j = n - 3, k = (number < 0) - 0; j > k; j -= 3)
		s = s.substring(0, j) + thousands_sep + s.substring(j);
	return s;
}

/*------------------------------------------------------------------------------
Returns the value of the selected button in a group of radio buttons or null
if none is selected.
------------------------------------------------------------------------------*/

function getRadioValue(group){
	var j;
	for(j = 0; j < group.length; j++)
		if(group[j].checked)
			return group[j].value;
	return null;
}

/*------------------------------------------------------------------------------
Returns 1 (0 otherwise) if the specified string value is found in a string set
separated by commas (mimics MySQL FIND_IN_SET() string function).
NOTE: Keep in sync with FindInSet() in common/inc/fn.php.
------------------------------------------------------------------------------*/

function findInSet(val, set){
	return set.match(new RegExp("(^|,)" + val + "(,|$)"));
}

/*------------------------------------------------------------------------------
Rounds the same way as PHP (Math.round rounds negative values down).
'val' -- is the value to round.
'decimals' -- (optional) is the number of decimal positions to preserve. Zero
if ommited.
------------------------------------------------------------------------------*/

function round(val, decimals){
	var n;
	n = decimals != undefined ? Math.pow(10, decimals) : 1;
	return (val < 0 ? -Math.round(-val * n) : Math.round(val * n)) / n;
}

