/**
*
*  Scrollable HTML table
*  http://www.webtoolkit.info/
*  -----------------------------
*  * Modified *
**/

function ScrollableTable (tableEl, tableHeight, tableWidth,callbackfunction, ajaxScrollObject) {
	
	this.initIEengine = function () {
		var i=0;
		this.containerEl.style.overflowY = 'auto';
		 
		if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) {
			this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
		} else {
			this.containerEl.style.overflowY = 'hidden';
			this.tableEl.style.width = this.newWidth +'px';
		}

		this.containerEl.style.overflowX = "hidden";
		this.containerEl.id = "scrollableDiv" + this.tableEl.id + (i++);
		
		if (this.thead) {
			var trs = this.thead.getElementsByTagName('tr');
			
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
				
			}
		}

		if (this.tfoot) {
			var trs = this.tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
			}
		}
		

		if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) 
		{
			this.containerEl.style.borderBottom = "1px solid #CECBD6";
		}

		var cbFunction = this.callbackfunction;
		var lAjaxScrollbarObject = this.ajaxScrollObj;

		if (cbFunction != null)
		{
			this.containerEl.onscroll = function()
			{
				cbFunction(this.scrollTop, this.scrollHeight,this,lAjaxScrollbarObject);
			}
		}
		
		this.containerEl.style.height = tableHeight;
		this.containerEl.style.overflowY = "auto";


		eval("window.attachEvent('onresize', function () { document.getElementById('" + this.tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + this.tableEl.id + "').style.visibility = 'visible'; } )");
	};


	this.initFFengine = function (isIE8) {
	
		var i=0;
		//this.containerEl.style.overflowY = 'hidden';
		
		/*if (this.tableEl.parentNode.clientHeight - this.tableEl.offsetHeight < 0) {
			this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
		} else {
			this.containerEl.style.overflowY = 'auto';
			this.tableEl.style.width = this.newWidth +'px';
		}*/
		this.tableEl.style.width = this.newWidth + 'px';

		this.containerEl.style.overflowX = "hidden";
		this.containerEl.id = "scrollableDiv" + this.tableEl.id + (i++);
		this.containerEl.style.padding = "1px";
		
		var headHeight = (this.thead) ? this.thead.clientHeight : 0;
		var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
		var bodyHeight = this.tbody.clientHeight;
		var trs = this.tbody.getElementsByTagName('tr');
		if ( (bodyHeight >= (this.newHeight - (headHeight + footHeight)) ) || isIE8) {
			
			//this.tbody.style.overflowX = 'hidden';
			if (isIE8)
				this.containerEl.style.overflowY = 'auto';
			else
				this.tbody.style.overflow = '-moz-scrollbars-vertical';
			this.containerEl.style.borderBottom = "1px solid #CECBD6";
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
//				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
				var newW; 
				if (tds.length-1 < 0) continue;
				if (tds[tds.length-1].style.paddingRight == "")
					newW = this.scrollWidth;
				else
					newW = parseInt((parseInt(tds[tds.length-1].style.paddingRight.substring(0,2)) + this.scrollWidth));
				
				tds[tds.length-1].style.paddingRight = newW;
			}
		} else {
			this.tbody.style.overflow = 'hidden';
		}


		if (this.tfoot) {
			var trs = this.tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.bottom = (trs[x].parentNode.parentNode.offsetHeight - trs[x].parentNode.parentNode.parentNode.clientHeight - this.parentNode.parentNode.parentNode.scrollTop) + 'px';
			}
		}
		
		var cellSpacing = (this.tableEl.offsetHeight - (this.tbody.clientHeight + headHeight + footHeight)) / 4;
		//this.tbody.style.width = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2));
		if (bodyHeight >= (this.newHeight - (headHeight + footHeight)))
		{
			this.tbody.style.height = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';
			this.containerEl.style.borderBottom = "1px solid #CECBD6";
		}

		var cbFunction = this.callbackfunction;
		var lAjaxScrollbarObject = this.ajaxScrollObj;
		var thead = this.thead;
		
		if (!isIE8)
		{
			if (cbFunction != null)
			{
				this.tbody.onscroll = function()
				{
			
					cbFunction(this.scrollTop, this.scrollHeight,this,lAjaxScrollbarObject);
				}
			}
		}
		else
		{
		
			if (cbFunction != null)
			{
				this.containerEl.onscroll = function()
				{
			
					cbFunction(this.scrollTop, this.scrollHeight,this,lAjaxScrollbarObject);
				}
			}
			
		}
		
		
		this.tableEl.style.width = this.tableEl.style.width ; 


	};

	
	
	var j=0;
	this.callbackfunction = callbackfunction;
	this.tableEl = tableEl;
	this.scrollWidth = 18;
	this.ajaxScrollObj = ajaxScrollObject;

	this.originalHeight = this.tableEl.clientHeight;
	this.originalWidth = this.tableEl.clientWidth;

	this.newHeight = parseInt(tableHeight);
	this.newWidth = tableWidth ? parseInt(tableWidth) : this.originalWidth;

	this.tableEl.style.height = 'auto';
	this.tableEl.removeAttribute('height');

	this.containerEl = this.tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
	this.containerEl.setAttribute('id', 'scrollableDiv' + this.tableEl.id +  (j++));
	this.containerEl.appendChild(this.tableEl);
	

	
	this.containerEl.style.width = this.newWidth + 'px';
	
	
	var thead = this.tableEl.getElementsByTagName('thead');
	this.thead = (thead[0]) ? thead[0] : null;

	var tfoot = this.tableEl.getElementsByTagName('tfoot');
	this.tfoot = (tfoot[0]) ? tfoot[0] : null;

	var tbody = this.tableEl.getElementsByTagName('tbody');
	this.tbody = (tbody[0]) ? tbody[0] : null;

	
	if (!this.tbody) return;

	var headHeight = (this.thead) ? this.thead.clientHeight : 0;
	var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
	var bodyHeight = this.tbody.clientHeight;
	
	
	var ie8 = false;

	if (document.all && document.getElementById && !window.opera) 
		if (getInternetExplorerVersion()>=8.0)
			ie8 = true;
	

	if ( (bodyHeight >= (this.newHeight - (headHeight + footHeight)) ) || ie8) 
		this.containerEl.style.height = this.newHeight + 'px';
	else 
		this.containerEl.style.height = "auto";
			
	
	if (document.all && document.getElementById && !window.opera) 
	{
		if (getInternetExplorerVersion()>=8.0)
		{
			ie8 = true;
			this.initFFengine(true);
		}

		else
			this.initIEengine();
	}
	
	if (!document.all && document.getElementById && !window.opera) this.initFFengine(false);


	

}


function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}