var alerts = 20;
function limalert(message){
	if(alerts > 0){
		alerts --;
		alert(message);
	}
}


function priceFormatter(value, isAjax, name){
	var currentValue = parseFloat(value);
    return formatValue('right', '', currentValue.toFixed(2), null, isAjax, name);
}
function changeFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
    return formatValue('right', getFieldChangeClass(numericValue), signNumber(numericValue.toFixed(2)), null, isAjax, name);
}
function volumeFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
    return formatValue('right', '', commatizeNumber(numericValue.toFixed(0)), null, isAjax, name);
}
function doubleCenterFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
	return formatValue('center', '', commatizeNumber(numericValue.toFixed(parseInt(2))), null, isAjax, name);
}
function floatFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
	return formatValue('right', '', commatizeNumber(numericValue.toFixed(parseInt(2))), null, isAjax, name);
}
function percentageChangeFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
    return formatValue('right', getFieldChangeClass(numericValue), signNumber(numericValue.toFixed(2)) + '%', null, isAjax, name);
}
function percentageFormatter(value, isAjax, name){
	var numericValue = parseFloat(value);
	return formatValue('right', '', numericValue.toFixed(parseInt(2)) + '%', null, isAjax, name);
}
function ratioFormatter(value, isAjax, name){
	return formatValue('center', '', ""+value, null, isAjax, name);
}
function stringFormatter(value, isAjax, name){
	return formatValue('left', '', value, null, isAjax, name);
}
function intFormatter(value, isAjax, name){
	var numericValue = parseInt(value);
	return formatValue('right', '', ""+numericValue, null, isAjax, name);
}
var months = new Array();
months.push("N/A");
months.push("Jan");
months.push("Feb");
months.push("Mar");
months.push("Apr");
months.push("May");
months.push("Jun");
months.push("Jul");
months.push("Aug");
months.push("Sep");
months.push("Oct");
months.push("Nov");
months.push("Dec");
function monthFormatter(value, isAjax, name){
	var numericValue = 0;
	if(value != "N/A")
		try{
			numericValue = parseInt(value);
		}catch (e){
			numericValue = 0;
		}
		//limalert(months[numericValue]);
	return formatValue('right', '', months[numericValue], null, isAjax, name);
}
function linkFormatter(value, isAjax, name){
	var align = 'left';
	if(value != null && value)
		align = 'center';
	var link = value;
	if(value == "none")
		return formatValue(align, '', '', null, isAjax);
	var linkvalue = value;
	if(value == "row")
		linkvalue = value;
	else if(value == "field")
		linkValue = value;
	else
		linkValue = value;
	value = "<a href='"+link+linkvalue+ "'>"+value+"</a>";
	if(value.indexOf("Symbol") > -1){
		value = "<table width=100%><tr><td align=left>"+value +"</td><td align=right>"+ 
			getTradeLink(value) + "</td></tr></table>";
	}
	return formatValue(align, '', value, null, isAjax, name);
}
function centeredLinkFormatter(value, isAjax, name){
	return linkFormatter(true, isAjax, name);
}

function chartFormatter(){
	// PlaceHolder for now.
}
function isNA(v){
	if(v == null) v = "N/A";
    if((""+v).indexOf("NaN") > -1)
    	v = "N/A";
	if(v.indexOf("NaN") > -1)
	   	v = "N/A";
	return v;
}
function formatValue(alignment, className, v, bgcolor, isAjax, name){
	if(isAjax == null) isAjax = false;
	var width = 80;
	if(v == null) v = "N/A";
    if((""+v).indexOf("NaN") > -1)
    	v = "N/A";
	if(v.indexOf("NaN") > -1)
	   	v = "N/A";
    if(bgcolor != null){
		bgcolor = 'bgcolor:'+bgcolor+";";
	}
	var style = new Array();
	style.push(' style="text-align:' + alignment+'; ');
	style.push((bgcolor != null)? bgcolor+';' : '');
	style.push('"');
	var cq = "";
	if(isAjax)
		cq = "CQ ";
	cellClass = (className=='')? ' class="'+cq+'" ' : ' class="'+cq+ ' '+ className + '" ';

	cellHtml = v;
	
	var ret = new Array();
	ret.push('<td nowrap=nowrap>');// '+cellStyle+'">'); 
	if(name != null)
		name = 'name="'+name+'" ';
	else
		name = '';
	ret.push('<');
	ret.push('span '+name);
	ret.push(cellClass);
	ret.push(style.join(""));
	ret.push('>');
	ret.push(cellHtml);
	ret.push('</span></td>');
	
	return ret.join("");
}

function getFormatter(type){
	if(type == 'Price')
		return priceFormatter;
	else if (type == 'Month')
		return monthFormatter;
	else if (type == 'Change')
		return changeFormatter;
	else if (type == 'PercentageChange')
		return percentageChangeFormatter;
	else if (type == 'Volume')
		return volumeFormatter;
	else if (type == 'Percentage')
		return percentageFormatter;
	else if (type == 'Snapshot')
		return 'Snapshot';
	else if (type == "Options")
		return 'Options';
	else if (type == 'String')
		return stringFormatter;
	else if (type == 'Ratio')
		return ratioFormatter;
	else if (type == 'Tagged')
		return 'Tagged';
	else if (type == 'Integer')
		return intFormatter;
	else if (type == 'Chart')
		return chartFormatter;
	else if (type == 'DoubleCenter')
		return doubleCenterFormatter;
	else return floatFormatter;
}

function commatizeNumber(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;
}
