
/*
 * Function: addCommas
 * Description: utility function for formatting a number to include commas
*/
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

/*
 * Function: addCommas
 * Description: utility function for rounding a number up to the nearest even number
*/
function evenNumber(nInt)
{
	if(nInt % 2 == 0)
		return nInt;
	else
		return nInt + 1;
}

/*
 * Function: GetColumnIntoArray
 * Description: Push a given column from a multidimensional array into a single row array
 * 				The column to take is a zero-based index
*/
function GetColumnIntoArray(data, columnToTake)
{
		
	result = new Array(data.length);
	
	for(i = 0; i < data.length; i++)
	{
		result[i] = data[i][columnToTake];
	}
	
	return result;
}

/*
 * Function: RowIndexForValue
 * Description: Returns the index of the row containing the given value, by doing a look up in the provided array in the
 *              column specified.
*/

function RowIndexForValue(value, data, column)
{
	for(i = 0; i < data.length; i++)
	{
		if(data[i][column] == value)
			return i;
	}
	
	return -1;
}

/*
 * Function: Median
 * Description: calculate the median value from a given array
*/
function Median(data)
{
	/* sort the data in ascending order */
	data.sort(Ascend);

	/* After sorting the data, we can get the median of the array which is the middle of it. */
	var middle = Math.floor(data.length / 2);
	if ((data.length % 2) != 0)
	{
        median = data[middle];
    }
    else
	{
        median = (data[middle - 1] + data[middle]) / 2;
    }

	return median;
}

/*
 * Function: Average
 * Description: calculate the average value from a given array
*/
function Average(data)
{
	if(data.length == 0)
		return 0;
	
	var sum = 0;
	for(i = 0; i < data.length; i++)
	{
        sum += data[i];
    }

	return sum / data.length;
}


/*
 * Function: Ascend
 * Description: compare two objects
*/
function Ascend(a, b) {
    if (a < b)
        return -1;
    else if (a > b)
        return 1;
    return 0;
}

