

var financialReport_regTable = function( sectionId, siteId, regId, locale, cols, data, sectionProps, columnCount )
{
	this.sectionId = sectionId;
	this.siteId = siteId;
	this.regId = regId;
	this.locale = locale;
	
	this.cols = cols;
	this.data = data;
	
	this.sectionProps = sectionProps;
	
	this.columnCount = columnCount;
}

/* Marks a column as selected.  
 * Distinguishes between toplevel and sublevel column headers  
 * (toplevel gets class="selected", sublevel gets class="subSelected"),
 * since IE can't handle multi-class CSS selectors.
 */
financialReport_regTable.prototype.selectColumn = function(col)
{
	var th = col.getThEl();
	if ( YAHOO.util.Dom.hasClass( th, "subHeader" ) )
	{
		YAHOO.util.Dom.addClass( th, "subSelected" );
	} else {
		YAHOO.util.Dom.addClass( th, "selected" );
	}
}
financialReport_regTable.prototype.unselectColumn = function(col)
{
	var th = col.getThEl();
	YAHOO.util.Dom.removeClass( th, "subSelected" );
	YAHOO.util.Dom.removeClass( th, "selected" );
}
financialReport_regTable.prototype.isSelectedColumn = function(col)
{
	return YAHOO.util.Dom.hasClass( col.getThEl(), "selected" ) ||
		YAHOO.util.Dom.hasClass( col.getThEl(), "subSelected" );
}


financialReport_regTable.prototype.init = function()
{
	this.dataSource = new YAHOO.util.DataSource(this.data);
	this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
    this.dataTable = new YAHOO.widget.DataTable("regTable_"+this.sectionId, this.cols, this.dataSource); 
	
	this.dataTable.regTable = this;
	
	this.highlightCell = function(oArgs) {
        var elCell = oArgs.target;
        this.highlightRow(elCell.parentNode);
    };
	
	this.dataTable.subscribe("cellMouseoverEvent", this.highlightCell);
    this.dataTable.subscribe("cellMouseoutEvent", this.dataTable.onEventUnhighlightRow);
    
    for ( key in this.sectionProps )
    {
    	if ( key.indexOf("col_") == 0 && this.sectionProps[key] == "selected" )
    	{
    		var col = this.dataTable.getColumn(key.substr(4));
    		if ( col != null )
    			this.selectColumn( col );
    	}
    }
    
    this.dataTable.refreshView();
}


/* Shows the popup for notes. 
 * @param not simply the note number
 */
function notPopup(not)
{
	var myPanel = new YAHOO.widget.Panel("notPopup",   
		{ width: "620px", 
		  height: "450px", 
          fixedcenter: true, 
          close: true, 
          draggable: true, 
          zindex:4,
          modal: false,
		  underlay: "none",	
          visible: false
        } 
 	);
	myPanel.setHeader( ""+not );
	myPanel.setBody('<iframe src="/noter/not-'+not+'?layoutmode=popup"></iframe>');
	
	myPanel.render(document.body);
	myPanel.show();
	
	return false;
}



/* Formatter for the note column. Generates links from a comma-separated strings, that popups note pages.
 */
var notFormatter = function(elCell, oRecord, oColumn, oData)                 
{
	elCell.setAttribute("align", "center");
	elCell.style.textAlign = "center";
	
	var not = oRecord.getData("not");  
	var notes = not.replace(/,/g,".").replace(".0",'').split(".");
	
	if ( notes.length == 1 && notes[0] == " " )
		notes = [];
	var content = "";
	for ( var i=0; i<notes.length; i++ )
	{
		content += '<a class="noteLink" id="noteLink_'+notes[i]+'" note="'+notes[i]+'" onclick="return notPopup('+notes[i]+')" >' + notes[i] + '</a> ' + ( i==notes.length-1 ? "":", " );
	}
	
	generalCellFormatter(elCell, oRecord, oColumn, oData);
	
	elCell.innerHTML = content;
};

/* Formatter for the post-* column, that checks if the record has a non-empty 'footnote' column, 
 * in which case it adds superscript version of them to this first column.
 */
var footnoteFormatter = function(elCell, oRecord, oColumn, oData)                 
{
	var footnote = oRecord.getData("footnote");  
	footnote = parseInt(footnote);
	
	if ( /^\s*$/.test(oData) )
		oData = "&nbsp;";
	
	if ( !isNaN(footnote) )
		oData = oData + "<sup>"+footnote+"</sup>";
	
	generalCellFormatter(elCell, oRecord, oColumn, oData);
	
	elCell.innerHTML = oData;
};  

/* Our own formatter for numbers. Uses the YAHOO.util.Number.format(), but ignores decimal numbers and accepts non-numbers.
 */
var numberFormatter = function(elCell, oRecord, oColumn, oData)                 
{
	// oData = oData.replace(/[,\.][0-9]*$/, "");
	
	var numOpts = clone(oColumn.numberOptions || this.get("numberOptions"));
	
	if ( numOpts.decimalPlaces == 0 )
	{
		var index = oData.indexOf( "." );
		if ( index > -1 )
		{ 
			var decString = oData.substr(index+1);
			var regex = new RegExp("^0+$");
			if ( !regex.test(decString) ) 
			{
				numOpts.decimalPlaces = decString.length;
			}
		}
	}
	
	var data = YAHOO.util.Number.format(oData, numOpts);
	if ( isNaN(parseFloat(data)) )
		data = oData;
	
	if ( oColumn.key.indexOf("post-") != 0 )
	{
		elCell.style.textAlign = "right";
	}
		

	
	if ( /^\s*$/.test(oData) )
		elCell.innerHTML = oData;
	else
		elCell.innerHTML = data;


		
	generalCellFormatter(elCell, oRecord, oColumn, oData);
};  

/* General formatter that handles the bold, topline, underline, header, italic attributes of each row, and selected columns.
 */
var generalCellFormatter = function(elCell, oRecord, oColumn, oData)
{
	var bold = oRecord.getData("bold") || "";
	if ( bold.indexOf("x") > -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode.parentNode, "bold" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode.parentNode, "bold" );
	
	var topline = oRecord.getData("topline") || "";
	if ( topline.indexOf("x") > -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode.parentNode, "topline" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode.parentNode, "topline" );

	var underline = oRecord.getData("underline") || "";	
	if ( underline.indexOf("x") > -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode.parentNode, "underline" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode.parentNode, "underline" );

	var underline = oRecord.getData("header") || "";	
	if ( underline.indexOf("x") > -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode.parentNode, "header" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode.parentNode, "header" );

	var underline = oRecord.getData("italic") || "";	
	if ( underline.indexOf("x") > -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode.parentNode, "italic" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode.parentNode, "italic" );
	
	if ( financialReport_regTable.prototype.isSelectedColumn(oColumn) && oColumn.key.indexOf("post-") == -1 )
		YAHOO.util.Dom.addClass( elCell.parentNode, "selected" );
	else
		YAHOO.util.Dom.removeClass( elCell.parentNode, "selected" );
	
	
	if ( elCell.innerHTML.trim() == "" )
		elCell.innerHTML = "&nbsp;";
}






