

/**
 * 
 * @param amount  
 * @return string Formatted currency amount
 */
function currencyFormatted(amount,limit)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	if(  Math.abs(i) < ( 5/ Math.pow(10,limit+1) ) ) { i = 0.00 }
	return i.toFixed( limit );
	/*
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;	
	s = new String(i.toFixed(limit));
//	if(s.indexOf('.') < 0) { s += '.00'; }
//	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;	
	return s;
	*/
};
// end of function CurrencyFormatted()

/**
 * 
 * @param amount  
 * @return  string Formatted currency amount with comma(s) in the middle 
 */
function commaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = typeof(a[1]) != 'undefined' ? a[1] : '';
	var i = a[0];
	var threshold = typeof( parseInt( i.substr(-1) ) ) != 'number' ?   i.substr(-1) : '';	
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';	
	if(amount.substr(0,1) == '-') { minus = '-'; }	
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(  d.length < 1) { amount = n + threshold ; }
	else { amount = n + '.' + d; }
	amount = minus + amount;	
	return amount;
};
// end of function CommaFormatted()


/**
 * 
 * @param amount  
 * @param threshold Char 'K'|'M'|'B' to represent how much is counted as a big number (1K/1M/1B)
 * @return string Formatted number with unit(K,M,B,T) in behind 
 */
function bigNumberFormatted(amount, threshold,limit)
{
	var i = parseFloat(amount);
	var unit = '';
	var factor = 1;

	if (!threshold) threshold = 'M';
	var value = Math.abs(i);
	
	switch (threshold.toUpperCase()) {
		case 'K':
			if (value >= 1000000000)	{	// billion
				unit = 'B';
				factor = 1000000000.0;

			} else if (value >= 1000000) {	// million
				unit = 'M';
				factor = 1000000.0;
			} else if (value >= 1000) {		// thousand
				unit = 'K';
				factor = 1000.0;
			}
			break;
			
		case 'M':
			if (value >= 1000000000)	{	// billion
				unit = 'B';
				factor = 1000000000.0;

			} else if (value >= 1000000) {	// million
				unit = 'M';
				factor = 1000000.0;
			}
			break;
			
		case 'B':
			if (value >= 1000000000)	{	// billion
				unit = 'B';
				factor = 1000000000.0;
			}
			break;
	}
	
	
	i = i / factor;
	var s = currencyFormatted(i,limit);
	s = s + unit;
	return s;
};
// end of function bigNumberFormatted()

/**
 * 
 * @param amount without dollar sign  
 * @return string The amount with the symbol in the front 
 */
function localAddMoneySymbol(amount)
{	
	var symbol = '$';
	return   amount.indexOf ('-') < 0 ?  symbol + amount : '-'+ symbol + amount.substr(1);
};

/**
 * @param amount
 * @param threshold Char ''|'K'|'M'|'B' to represent how much is counted as a big number (1K/1M/1B)
 * 						Default = 'M'
 * @return string Formatted number amount 
 */
function localNumberFormatted(amount, threshold, limit )
{	
	if ( typeof( numberConvert(amount)) != 'number' ) 	return numberConvert(amount);	
	if (!threshold) threshold = 'M';
	if ( typeof(limit) == "undefined" ) limit = 2;	
//	return commaFormatted(bigNumberFormatted(currencyFormatted(amount,limit), threshold,limit));
	return commaFormatted( bigNumberFormatted(amount, threshold,limit) );

};
//end of function localNumberFormatted()

function localIntegerFormatted ( amount,threshold ){	
	return localNumberFormatted(amount, false,0);
}
/**
 * 
 * @param amount  
 * @param threshold Char ''|'K'|'M'|'B' to represent how much is counted as a big number (1K/1M/1B)
 * 						Default = 'M'
 * @param bigNumber true|false True means to take it as big number, and hence will add B, M, K in behind
 * 								(Default=true)  
 * @return string Formatted currency amount 
 */
function localCurrencyFormatted(amount, threshold, limit)
{	
	if ( typeof( numberConvert(amount)) != 'number' ) 	return numberConvert(amount);
	if (!threshold) threshold = 'M';
	return localAddMoneySymbol(localNumberFormatted(amount, threshold, limit));	
};
//end of function localCurrencyFormatted()

/**
 * 
 * @param {Object} amount
 * @param {Object} threshold
 */
function localPercentFormatted ( amount, threshold, symbol,limit ){
	
	if ( typeof( numberConvert(amount)) != 'number' ) 	return numberConvert(amount);
//	if (!threshold) threshold = 'M';
	return localAddPercerSignSymbol(localNumberFormatted(parseFloat(amount)*100, threshold,limit),symbol);
}

function getNumberColorClass( amount ){
	if( typeof(amount) == "undefined" ) return "positive_number negative_number"
	var amount = numberConvert(amount);
	if ( typeof( amount ) != 'number' ) return "" ;
	if( amount > 0 ) return "positive_number";
	if( amount < 0 ) return "negative_number";
	if( amount == 0 ) return "";
}

function getNumberColorClassByType( typeStr ){
	if( typeStr == "risk" ) return "risk_number";
}

/***
 * 
 * @param {number} amount
 * @param {string} size:value--> b(big),m(middle),s(small)
 */
function getNumberPostion(amount,size){
	if( typeof(amount) == "undefined" ) return ""
	var amount = numberConvert(amount);
	if ( typeof( amount ) != 'number' ) return "" ;
	if( amount > 0 ) return "up_ico_"+size;
	if( amount < 0 ) return "down_ico_"+size;
	if( amount == 0 ) return "";
}

/**
 * 
 * @param {Object} amount
 */
function localAddPercerSignSymbol( amount,symbol ){
	if( typeof(symbol) == 'undefined') 	 symbol = '%';
//	var symbol = '%';
	return amount + symbol;
}


/**
 * 
 * @param {Object} amount
 */
function numberConvert( amount ) {		
		switch (amount) {
			case null:
			case 'null':
				return '-';
				break;
			case NaN:
			case 'NaN':
				return '-';
				break;
			case Infinity:
			case 'Infinity':
				return 'inf';
				break;
			case -Infinity:
			case '-Infinity':
				return "-inf";
				break;
			default:
				return parseFloat(amount);
		}
}


function uniqueArray(data){
    data = data || [];
    var a = {};
    for (var i=0; i<data.length; i++) {
        var v = data[i];
        if (typeof(a[v]) == 'undefined'){
                               a[v] = 1;
                               }
    };
    data.length=0;
      for (var i in a){
               data[data.length] = i;
         }
        return data;
}

/**
 * 
 * @param {Object} inputText
 */
function splitStringToArray( assetsTickerListStr ){
		 var assetArry = [];		 
		 if( assetsTickerListStr ){
			  assetsTickerListStr = assetsTickerListStr.replace( /\,+/g, " " ).replace( /\s+/g, " " ).replace( /(^\s*)|(\s*$)/g, "" );		
			  assetArry = assetsTickerListStr.split(" ");
		 }
//		  $("#tickerSo").data("ref_asset",true);
		  return assetArry;
} //end of splitStringToArray


