// Thanks to Bernhard Wagner for submitting this function
// Modified by Phil (Highresolution Enterprises)

function replace8a8(str) {
	str = str.toUpperCase();
	str = str.replace(/,/g, "");	//remove comma's
	var parts = str.split(" ");
	
	var num = Number(parts[0].valueOf());
	var ml = parts[1];
	
	if (ml == "KB")
		num *= 1024;
	else if(ml == "MB")
		num *= 1024 * 1024;
	else if (ml == "GB")
		num *= 1024 * 1024 * 1024;
	else if (ml == "TB")
		num *= 1024 * 1024 * 1024 * 1024;
	// B and no prefix

	return num;
}

function leftnumber(str) {
	str = str.toUpperCase();
	str = str.replace(/,/g, "");	//remove comma's
	var splitstr = "__________";
	var ar = str.replace(
		/(([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?)(.*)/,
	 "$1"+splitstr+"$4").split(splitstr);
	var num = Number(ar[0]).valueOf();
	return num;
}

function middlenumber(str) {
	var parts = str.split(" ");
	return leftnumber(parts[1]);
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

function parseMonth(str) {
	str = trim(str.toUpperCase());
	if (str=="JAN")
		return 1;
	if (str=="FEB")
		return 2;
	if (str=="MAR")
		return 3;
	if (str=="APR")
		return 4;
	if (str=="MAY")
		return 5;
	if (str=="JUN")
		return 6;
	if (str=="JUL")
		return 7;
	if (str=="AUG")
		return 8;
	if (str=="SEP")
		return 9;
	if (str=="OCT")
		return 10;
	if (str=="NOV")
		return 11;
	if (str=="DEC")
		return 12;
	return 0;
}

function numberk2(str) {
   var idx = str.indexOf(" ");
   str = str.substring(idx+1, str.length-(idx+1));
   var idx = str.indexOf(" (");
   str = str.substring(0, idx);
   return replace8a8(str);   
}

SortableTable.prototype.addSortType( "NumberK", replace8a8 );
SortableTable.prototype.addSortType( "LeftNumber", leftnumber );
SortableTable.prototype.addSortType( "MiddleNumber", middlenumber );
SortableTable.prototype.addSortType( "Month", parseMonth );
SortableTable.prototype.addSortType( "NumberK2", numberk2 );
