/**
*  Author: Dheeraj Chugh
*  Date: 5th January 2005.
*  Version: 1.0
*  Purpose: This js file contains functions for converting a displaytag generated table into a Fixed Header Row Table.
*  Main Features:
*  (a) Supports multiple tables on a single page.
*  (b) Table width can be specified in % (percentage) or pixels.<b> 
*/

function getOnScroll(from) {
     return function () {
          from.scrollLeft = event.srcElement.scrollLeft;
     };
}

function addSyncScroll(from, to) {
     removeSyncScroll(from);
     from.syncScroll = getOnScroll(from);
     from.syncTo = to;
     to.attachEvent("onscroll", from.syncScroll);
}

function removeSyncScroll(from) {
     if (from.syncTo != null) {
          from.syncTo.detachEvent("onscroll", from.syncScroll);
     }

     from.syncTo = null;
     from.syncScroll = null;
}
  


function fnSetColumnAlignment(tableName)
	{
		alert("inside fnSetColumnAlignment");
		dataObj=document.getElementById(tableName+'_scrollTableData');
		
		headerobj=document.getElementById(tableName+'_scrollTableHeaders');
		for(i=0;i<headerobj.rows[0].cells.length;i++)
		{
			e = dataObj.rows[0].cells[i];			
			dataWidth = parseFloat(dataObj.rows[0].cells[i].clientWidth) - 
						parseInt(e.currentStyle.paddingLeft) - 
						parseInt(e.currentStyle.paddingRight);
			headerobj.rows[0].cells[i].innerHTML = 
							'<span style="width:' + dataWidth + ';">' + 
							headerobj.rows[0].cells[i].innerHTML + '</span>';
	 	}
	}

function scrollableTableInit(tableID) {
	
	
	
	
	if(!document.all) {

		
			
	if((document.getElementById(tableID+"_rowHeaderHeadDiv")) 
		&& (document.getElementById(tableID+"_rowHeaderDataDiv")) 
		&& (document.getElementById(tableID+"_headDiv")) 
		&& (document.getElementById(tableID+"_dataDiv"))) {

		var rowHeaderHeadDiv = document.getElementById(tableID+"_rowHeaderHeadDiv");	//Row Header for the table in Head Div.
		
		var rowHeaderDataDiv = document.getElementById(tableID+"_rowHeaderDataDiv");	//Row Header for the table in Data Div.
		var dataDiv = document.getElementById(tableID+"_dataDiv");						//Data Div.
		var headDiv = document.getElementById(tableID+"_headDiv");						//Head Div.
       //alert("headDiv   :"+ headDiv);
	   //var test=headDiv.offsetTop;
		//alert("offsetTop   :"+test);
		
		headDiv.style.top = headDiv.offsetTop;
		
		headDiv.style.left = headDiv.offsetLeft;
       
		//headDiv.style.setExpression("width", tableID+"_dataDiv.offsetWidth-(" +tableID+"_dataDiv.offsetWidth - " +tableID+"_dataDiv.clientWidth)");
		var width = parseInt(document.getElementById(tableID+"_dataDiv").offsetWidth) 
					- (parseInt(document.getElementById(tableID+"_dataDiv").offsetWidth) 
					- parseInt(document.getElementById(tableID+"_dataDiv").clientWidth));
		headDiv.style.width = width;
       
		headDiv.style.visibility='visible';
		
     
	  
		
		
		
		
		/* changes start*/

		
		dataObj=document.getElementById(tableID+'_scrollTableData');
		
		
		headerobj=document.getElementById(tableID+'_scrollTableHeaders');
		
		for(i=0;i<headerobj.rows[0].cells.length;i++)
		{
			e = dataObj.rows[0].cells[i];			
			dataWidth = parseInt(dataObj.rows[0].cells[i].clientWidth); 
						//-parseInt(e.currentStyle.paddingLeft) - 
						//parseInt(e.currentStyle.paddingRight);
			//alert("dataWidth"+dataWidth);
			headerobj.rows[0].cells[i].style.width =dataWidth;
				/*	'<span style="width:' + dataWidth + ';color:"#3A73BA">' + 
							headerobj.rows[0].cells[i].innerHTML + '</span>';*/
		
		}
		

		/* changes end  */
		
		
		
		
		
		
		/*for( i =0; i < rowHeaderHeadDiv.childNodes.length; i++ ) {
			alert("HI");
			var exp1=parseInt(document.getElementById(tableID+"_rowHeaderDataDiv.childNodes[" + i + "]").clientWidth);
			//alert("exp1 is"+exp1);
			rowHeaderHeadDiv.childNodes[i].style.width=exp1;
			
			//rowHeaderHeadDiv.childNodes[i].style.setExpression("width", tableID+"_rowHeaderDataDiv.childNodes[" + i + "].clientWidth");
			var exp2=parseInt(document.getElementById(tableID+"_rowHeaderDataDiv.childNodes[" + i + "]").clientHeight);
			rowHeaderHeadDiv.childNodes[i].style.height=exp2;
			//rowHeaderHeadDiv.childNodes[i].style.setExpression("height", tableID+"_rowHeaderDataDiv.childNodes[" + i + "].clientHeight");
		
		} */
		addSyncScroll(headDiv, dataDiv );
		
	}
} else
	{
     
if((document.getElementById(tableID+"_rowHeaderHeadDiv")) && (document.getElementById(tableID+"_rowHeaderDataDiv")) && (document.getElementById(tableID+"_headDiv")) && (document.getElementById(tableID+"_dataDiv"))) {
		var rowHeaderHeadDiv = document.getElementById(tableID+"_rowHeaderHeadDiv");	//Row Header for the table in Head Div.
		var rowHeaderDataDiv = document.getElementById(tableID+"_rowHeaderDataDiv");	//Row Header for the table in Data Div.
		var dataDiv = document.all(tableID+"_dataDiv");						//Data Div.
		var headDiv = document.all(tableID+"_headDiv");						//Head Div.
		
		headDiv.style.top = dataDiv.offsetTop;
		headDiv.style.left = dataDiv.offsetLeft;
		headDiv.style.setExpression("width", tableID+"_dataDiv.offsetWidth-(" +tableID+"_dataDiv.offsetWidth - " +tableID+"_dataDiv.clientWidth)");

		headDiv.style.visibility='visible';


		for( i =0; i < rowHeaderHeadDiv.childNodes.length; i++ ){
			rowHeaderHeadDiv.childNodes[i].style.setExpression("width", tableID+"_rowHeaderDataDiv.childNodes[" + i + "].clientWidth");
			
			rowHeaderHeadDiv.childNodes[i].style.setExpression("height", tableID+"_rowHeaderDataDiv.childNodes[" + i + "].clientHeight");
		}

		addSyncScroll(headDiv, dataDiv);
	}

	}

}

function makeFixedHeaderScrollableTable(tableID, tableHeight, tableWidth) {
	
	convertDisplayTagTable(tableID, tableHeight, tableWidth);
}

function convertDisplayTagTable(tableID, tableHeight, tableWidth) {
	try {
		var table = document.getElementById(tableID);
		if(!table) {
			alert('No table found with ID: ' + tableID);
			return;
		}
		
		var width = table.style.width;
		if(!width) {
			width = table.width;
			if(!width) {
				alert('No width defined for the table with ID: ' + tableID);
				return;
			}
		}

		var bWidthSpecified = false;

		if (typeof tableWidth != 'undefined') {
			if(tableWidth.length != 0) {
				bWidthSpecified = true;
			}
			else {
				bWidthSpecified = false;
			}
		}
		else {
			bWidthSpecified = false;
		}


		//Width Adjustments.
		//START.
		if(bWidthSpecified == false) {
			if(width.indexOf('%') == -1) {
				tableWidth = parseFloat(width) + 16;
			}
			else {
				tableWidth = '100%';
			}
		}
		else {		//Div Width Specified.
			if(parseFloat(tableWidth) >= parseFloat(width)) {
				//If the width is in pixels then add 16 to it for scrollbar.
				if(width.indexOf('%') == -1) {
					tableWidth = parseFloat(width) + 16;
				}
				else {
					tableWidth = width;
				}
			}
		}
		//END.
		//Width Adjustments.

		//var tableCode = table.outerHTML;
		
		if(!document.all)
		{
		var tableCode = table.parentNode.innerHTML;
		var originalTableCode = tableCode;

		var tHeadPos = tableCode.indexOf('thead');
		var tBodyPos = tableCode.indexOf('tbody');

		var tableTagHTML1 = tableCode.substring(0,tHeadPos-1);
		var tHeadHTML = tableCode.substring(tHeadPos-1, tBodyPos-1);
		tHeadHTML = tHeadHTML.replace("<thead>","");
		tHeadHTML = tHeadHTML.replace("</thead>","");
		tHeadHTML = tHeadHTML.replace("<tr","<tr id='" +tableID +"_rowHeaderHeadDiv'");

		var tBodyHTML = tableCode.substr(tBodyPos-1);
		tBodyHTML = tBodyHTML.replace("<tbody>","");
		tBodyHTML = tBodyHTML.replace("</tbody>","");
		tBodyHTML = tBodyHTML.replace("</tbody>","");

		var headDiv = '<div style="width:' +tableWidth +';overflow:hidden;z-index=3;visibility:hidden;position:absolute;" id="' +tableID +'_headDiv">';
		tableTagHTML1 = tableTagHTML1.replace(tableID, tableID+"_scrollTableHeaders");
		var closingTags1 = "</table></div>"
       				
		var headPartHTML = headDiv + tableTagHTML1 + tHeadHTML + closingTags1;
        
		var dataDiv = '<div style="width:' +tableWidth +';height:' +tableHeight +';overflow:auto;z-index=2; border:1px" id="' +tableID +'_dataDiv">';
		var tableTagHTML2 = tableTagHTML1;
		tableTagHTML2 = tableTagHTML2.replace(tableID+"_scrollTableHeaders", tableID+"_scrollTableData");

		var tableFirstRow = tHeadHTML;
		tableFirstRow = tableFirstRow.replace("id='" +tableID +"_rowHeaderHeadDiv'","id='" +tableID +"_rowHeaderDataDiv'");

		var bodyPartHTML = dataDiv + tableTagHTML2 + tableFirstRow + tBodyHTML;
        var closingTags2 = "</div>";
		var fullTableCode = headPartHTML + bodyPartHTML + closingTags2;
        

		table.parentNode.innerHTML = fullTableCode;

		positionHeadDiv(tableID);
        
		
		adjustDataDivHeight(tableID);
		
		//fnSetColumnAlignment(tableID);
		}
		else
		{
        var tableCode = table.outerHTML;
		var originalTableCode = tableCode;

		var tHeadPos = tableCode.indexOf('THEAD');
		var tBodyPos = tableCode.indexOf('TBODY');

		var tableTagHTML1 = tableCode.substring(0,tHeadPos-1);
		var tHeadHTML = tableCode.substring(tHeadPos-1, tBodyPos-1);
		tHeadHTML = tHeadHTML.replace("<THEAD>","");
		tHeadHTML = tHeadHTML.replace("</THEAD>","");
		tHeadHTML = tHeadHTML.replace("<TR","<TR id='" +tableID +"_rowHeaderHeadDiv'");

		var tBodyHTML = tableCode.substr(tBodyPos-1);
		tBodyHTML = tBodyHTML.replace("<TBODY>","");
		tBodyHTML = tBodyHTML.replace("</TBODY>","");
		tBodyHTML = tBodyHTML.replace("</TBODY>","");

		var headDiv = '<div style="width:' +tableWidth +';overflow:hidden;z-index=3;visibility:hidden;position:absolute;" id="' +tableID +'_headDiv">';
		tableTagHTML1 = tableTagHTML1.replace(tableID, tableID+"_scrollTableHeaders");
		var closingTags1 = "</table></div>"

		var headPartHTML = headDiv + tableTagHTML1 + tHeadHTML + closingTags1;

		var dataDiv ="";
		var actualHeight = parseInt(document.getElementById(tableID).clientHeight);

		//Getting the border color of the table from style sheet
		var styleSheet = null;
		var Rules = new Array();
		var styleCount = 0;
		var borderColor = "";
		topForLoop:
		for (var ssIndex=0; ssIndex<document.styleSheets.length; ssIndex++){
			styleSheet=document.styleSheets[ssIndex];
			for (var rIndex=0; rIndex<styleSheet.rules.length; rIndex++){
				rule=styleSheet.rules[rIndex];
				if(rule.selectorText == "."+table.className){
					borderColor = rule.style.borderColor;
					break topForLoop;
				}
				styleCount++;
			}
		}

		//Setting border for data div in case of scroll bar.
		if(actualHeight > tableHeight){
			dataDiv = '<div style="width:' +tableWidth +';height:' +tableHeight +';overflow:auto;z-index=2;';
			dataDiv = dataDiv + 'BORDER: ' + borderColor + ' solid;BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 1px; BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px;"	id="' +tableID +'_dataDiv">';
		}
		else{
			dataDiv = '<div style="width:' +tableWidth +';height:' +tableHeight +';overflow:auto;z-index=2;" id="' +tableID +'_dataDiv">';			
		}

		var tableTagHTML2 = tableTagHTML1;
		tableTagHTML2 = tableTagHTML2.replace(tableID+"_scrollTableHeaders", tableID+"_scrollTableData");

		var tableFirstRow = tHeadHTML;
		tableFirstRow = tableFirstRow.replace("id='" +tableID +"_rowHeaderHeadDiv'","id='" +tableID +"_rowHeaderDataDiv'");

		var bodyPartHTML = dataDiv + tableTagHTML2 + tableFirstRow + tBodyHTML;

		var closingTags2 = "</div>";
		var fullTableCode = headPartHTML + bodyPartHTML + closingTags2;

		table.outerHTML = fullTableCode;

		positionHeadDiv(tableID);

		adjustDataDivHeight(tableID);

			

		}

	}
	catch(e) {
		table.parentNode.innerHTML = originalTableCode;
		alert('Error while Converting table to Scrollable Table: convertDisplayTagTable() -> ' +e);
	}
}

function positionHeadDiv(tableID) {
	if((document.getElementById(tableID+"_headDiv")) && (document.getElementById(tableID+"_dataDiv"))) {
		scrollableTableInit(tableID);
		
		document.getElementById(tableID+"_headDiv").style.left = document.getElementById(tableID+"_dataDiv").style.left;
		document.getElementById(tableID+"_headDiv").style.top = document.getElementById(tableID+"_dataDiv").style.top;
				
	}
}

function adjustDataDivHeight(tableID) {
	if((document.getElementById(tableID+"_dataDiv")) && (document.getElementById(tableID+"_scrollTableData")) && (document.getElementById(tableID+"_scrollTableHeaders"))) {
		var divHeight = parseFloat(document.getElementById(tableID+'_dataDiv').style.height);
		var dataTableHeight = parseFloat(document.getElementById(tableID+'_scrollTableData').clientHeight);
				
		if(divHeight > dataTableHeight) {
			document.getElementById(tableID+'_dataDiv').style.height = dataTableHeight + 20;
		}
		
		try {
			window.status = "";
			jsfSetDivHeight();
		}
		catch(e) {
			//Ignore the Error if function is not able to adjust the height of the layout div.
			//This may happen if jsfSetDivHeight() is missing in the layout (could be possible because people might be using the old layout which does not have this function).
			window.status = 'Please update your layout file. Current layout does not contain jsfSetDivHeight function.';
		}
	}
}
function jsfSetDivHeight()
{
	var divHeight = parseInt(document.body.clientHeight) - 100;

	if(document)
		if(document.all)
			if(document.all["myScroller"])
				document.all["myScroller"].style.height = divHeight;
}