var CyloTable=
{
	CurrentHeaderCell:0,   
	CurrentHeaderCellY:0, 
	CurrentOnSelectCode:"",
	HandleText:"&nbsp;&nbsp;&nbsp;",
	ParTable:0,

	NumOnly:function (n)
	{
		n=n+"";
		ret="";
		var ok = "1234567890";
		for(x=0;x<n.length;x++)
		{
			if(ok.indexOf(n.charAt(x))>-1)
			{
				ret=ret+""+n.charAt(x);
			}
		}
		if(ret=="")ret=0;
		return parseInt(ret);
	},

	makeTableDragable:function (t,type)
	{

		if(typeof(t)=="undefined" || t=="")return "";
		if(typeof(type)=="undefined" || type=="")type="fixed";//type|fixed|loose
		t = document.getElementById(t);
		
		if(typeof(t)=="undefined" || t=="" || typeof(t)!="object" || t==null)return "";
		w = t.width;
		sw = t.style.width;
		osw = t.offsetWidth;

		W = Math.max(w,sw);
		if (W==0||W==""|| typeof(W)=="undefined" || isNaN(W))
		{
			W = osw;
		}
		if(CyloTable.isIE()==false)
		t.style.width = W+"px";//IF FF
		
		t.style.tableLayout="fixed";
		cln = t.className;
		
		if(typeof(cln)=="undefined" || cln=="")
		{
			t.className=type+"CYLOSTYLE";
		}else{
			t.className+=" "+type+"CYLOSTYLE";
		}
		
		tr = t.getElementsByTagName("tr");
		th = tr[0].getElementsByTagName("th");
		
		for(x=0;x<th.length;x++)
		{
			//if(type=="fixed" || CyloTable.isIE()==true)
			//{
				cellW = 0;
				
				if(th[x].offsetWidth!="")cellW=th[x].offsetWidth;
				if(th[x].width!="")cellW=th[x].width;
				if(th[x].style.width!="")cellW=th[x].style.width;
				
				th[x].style.width=cellW;

				//EVEN th[x].style.width=parseInt(W/th.length);
			//}
			cln = th[x].className;
			th[x].style.height=25;
			
			if(typeof(cln)=="undefined" || cln=="")
			{
				th[x].className="thcell";
			}else{
				th[x].className+=" thcell";
			}
		
			div = document.createElement("span");
			div.className="handelCYLO";
			
			div.innerHTML = CyloTable.HandleText;
			div.style.cursor="col-resize";
			div.onmousedown=new Function("CyloTable.handleMouseDown(this,'"+x+"')");
			div.ondblclick=new Function("CyloTable.spreadColumnMax(this,'"+x+"')");
			if(x==(th.length-1) && type=="fixed"){}else{
			th[x].appendChild(div);}
			
		}
		if(CyloTable.isIE()==false)
		{
			for(x=0;x<tr.length;x++)
			{
				td = tr[x].getElementsByTagName("td"); 
				for(c=0;c<td.length;c++)
				{
					if(typeof(td[c].getAttribute('class'))=="undefined" || td[c].getAttribute('class')==""|| td[c].getAttribute('class')=="null"|| td[c].getAttribute('class')==null)
					{
						//td[c].style.className='tdcell';
						td[c].setAttribute('class','tdcell');
						
					}else{
						td[c].setAttribute('class',td[c].getAttribute('class')+' tdcell');
					}	
				}
			}
		}
		if(CyloTable.isIE()==true)
		{
			for(x=1;x<tr.length;x++)
			{
				td = tr[x].getElementsByTagName("td"); 
				for(c=0;c<td.length;c++)
				{
					
					if(typeof(td[c].className)=="undefined" || td[c].className==""|| td[c].className=="null"|| td[c].className==null)
					{
						//td[c].style.className='tdcell';
						td[c].className ='tdcell';
						
					}else{
						td[c].className = td[c].className+' tdcell';
					}	
				}
			}
		}
	},

	getPosIE:function (Element)
	{
		var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0 ;
		while (Element.offsetParent)
		{
			CalculatedTotalOffsetLeft += Element.offsetLeft ;
			CalculatedTotalOffsetTop += Element.offsetTop ;
			Element = Element.offsetParent ;
		} ;

		//OffsetXForNS6 = evt.pageX - CalculatedTotalOffsetLeft ;
		//OffsetYForNS6 = evt.pageY - CalculatedTotalOffsetTop ;
		arr = new Array(2);
		arr["x"] = CalculatedTotalOffsetLeft;
		arr["y"] = CalculatedTotalOffsetTop;
		return arr;
	},
	getPosFF:function (elemento)
	{
		var x=0;
		while(elemento.offsetParent)
		{
		x += elemento.offsetLeft;
		elemento=elemento.offsetParent;
		}
		return x;
	},
	handleMouseMove:function ()
	{
		
		arr = CyloTable.getPosIE(CyloTable.ParTable);
		var oEvent = EventUtil.getEvent();
		tabHolder = CyloTable.ParTable.parentNode;
		
		inFromLeft = oEvent.clientX - tabHolder.offsetLeft + 15;//in from left
		inFromLeft = oEvent.clientX - arr["x"] + 10;//in from left
		CyloTable.sub(inFromLeft);
		//CyloTable.CurrentHeaderCellY = Y positioning of header
		toStrech = inFromLeft - CyloTable.CurrentHeaderCellY;
		//CyloTable.sub(toStrech);
		if(toStrech<21)
		{
			return;
		}
		
		toStrech = parseInt(toStrech - 15);
		//get table classname
		
		tabType = CyloTable.ParTable.className;
		
		if(tabType.indexOf("fixedCYLOSTYLE")>-1)
		{
			//get table width
			table = CyloTable.CurrentHeaderCell.parentNode.parentNode.parentNode.parentNode;
			tabWidth = CyloTable.ParTable.offsetWidth;
			tr = table.getElementsByTagName("tr");
			th = tr[0].getElementsByTagName("th");
			
			if(CurrentHeaderCellIndex < th.length - 1)
			{//ok to push next coloumn up
				testInd = CurrentHeaderCellIndex;
				testInd++;
				totalWidth = 0;
				for(x=0;x<th.length;x++)
				{
					www = th[x].style.width;
					www = www.replace("px","");
					www++;
					www--;
					totalWidth = totalWidth + www;
				}
			
				for(x=0;x<th.length;x++)
				{
					if(x==testInd)
					{
						
						CW = CyloTable.CurrentHeaderCell.parentNode.style.width;
						
						CW = parseInt(CW.replace("px",""));
						DIF = CW - toStrech;
						
						CWThis = th[x].style.width;
						CWThis = parseInt(CWThis.replace("px",""));
						
						WONN = 0;
						
						if(DIF<0)
						{
							DIF = -DIF;
							WONN = (CWThis-DIF);
							//CyloTable.sub(CW + " - " + toStrech + " = " + DIF + " So " + CWThis + " - " +  DIF + " = " + WONN);
							
						}else{
							WONN = (CWThis+DIF);
							//CyloTable.sub(CW + " - " + toStrech + " = " + DIF + " So " + CWThis + " + " +  DIF + " = " + WONN);
						}
						if(WONN>7)
						{
							//set next colounm down
							th[x].style.width = WONN+"px";
							toStrech=toStrech+"px";
							CyloTable.CurrentHeaderCell.parentNode.style.width = toStrech;
						}
					}
				}
			}
		}else{
			//if(CyloTable.isIE()==false)
			//{
				CW = CyloTable.CurrentHeaderCell.parentNode.style.width;		
				CW = parseInt(CW.replace("px",""));
				DIF = CW - toStrech;
				
				CTW = CyloTable.ParTable.style.width;
				
				CTW = parseInt(CTW.replace("px",""));
				
				if(DIF<0)
				{
					DIF = -DIF;
					CTW = CTW + DIF;
				}else{
					CTW = CTW - DIF;
				}
				if(!isNaN(CTW))
				CyloTable.ParTable.style.width = CTW +"px";
				
				
			//}
			toStrech=toStrech+"px";
			CyloTable.CurrentHeaderCell.parentNode.style.width = toStrech;
		}
	},
			   
	handleMouseDown:function (el,ind)
	{
		CyloTable.CurrentHeaderCell = el;
		CyloTable.CurrentHeaderCellY = el.parentNode.offsetLeft;
		CurrentHeaderCellIndex = ind
		CyloTable.ParTable = CyloTable.CurrentHeaderCell.parentNode.parentNode.parentNode.parentNode;
		if(CyloTable.isIE()==true)document.body.onselectstart = function(){return false;}
		if(CyloTable.isIE()==false)document.body.setAttribute('class',document.body.getAttribute('class')+' nosel');
		var oEvent = EventUtil.getEvent();
		var oDiv = el;
		moved = oEvent.clientX;
		//iDiffX = oEvent.clientX - oDiv.offsetLeft;
		//iDiffY = oEvent.clientY - oDiv.offsetTop;
		EventUtil.addEventHandler(document.body, "mousemove", CyloTable.handleMouseMove);
		EventUtil.addEventHandler(document.body, "mouseup", CyloTable.handleMouseUp);
	},

	handleMouseUp:function ()
	{
		if(CyloTable.isIE()==true)document.body.onselectstart = function(){return true;}
		if(CyloTable.isIE()==false)
		{
			cl = document.body.getAttribute('class');
			cl = cl.replace("nosel","");
			document.body.setAttribute('class',cl);
		}
		EventUtil.removeEventHandler(document.body, "mousemove", CyloTable.handleMouseMove);
		EventUtil.removeEventHandler(document.body, "mouseup", CyloTable.handleMouseUp);
	},
	sub:function (w)
	{
		//document.getElementById("CyloTable.sub").innerHTML = w;
	},
	isIE:function ()
	{
		var browserName=navigator.appName; 
		if(browserName.toUpperCase().indexOf("MICROSOFT")>-1)
		{
			return true;
		}else{
			return false;
		}
	},

	spreadColumnMax:function (el,ind)
	{
		tr = CyloTable.ParTable.getElementsByTagName("tr");
		l = tr.length;
		th = tr[0].getElementsByTagName("th");
		maxW = CyloTable.makeDivGetWidth(th[ind].innerHTML);
		
		for(x=0;x<l;x++)
		{
			td = tr[x].getElementsByTagName("td");
			if(td.length>0)
			{
				if(td[ind])
				{
					sw = CyloTable.makeDivGetWidth(td[ind].innerHTML);
					if(sw>maxW)maxW=sw;
				}
			}
		}
		
		CyloTable.CurrentHeaderCell.parentNode.style.width = maxW +"px";
	},

	makeDivGetWidth:function (cont)
	{
		if(CyloTable.isIE()==true)
			spareDiv = document.createElement("div");
		if(CyloTable.isIE()==false)
			spareDiv = document.createElement("span");

		spareDiv.innerHTML = cont;
		if(CyloTable.isIE()==true)
			spareDiv.className = "spareTestDivWidthCyloIE";
		if(CyloTable.isIE()==false)
			spareDiv.setAttribute("class","spareTestDivWidthCyloFF");
		document.body.appendChild(spareDiv);
		
		sw = spareDiv.offsetWidth;	
		sh = spareDiv.offsetHeight;

		spareDiv.parentNode.removeChild(spareDiv);
		return sw;
	}
	,

	getColumnWidths:function (t)
	{
		//pass a table ID and get back an array with the table Column widths
		t = document.getElementById(t);
		tr = t.getElementsByTagName("tr");
		th = tr[0].getElementsByTagName("th");
		if(th.length==0)
		{
			th = tr[0].getElementsByTagName("td");
		}
		widths=[];
		//tabHolder = CyloTable.ParTable.parentNode;
		for(x=0;x<th.length;x++)
		{
			widths[x]=th[x].offsetWidth;
		}
		return widths;
	}
	,setColumnWidths:function (t,widths)
	{
		//pass table ID and an array with numbers (integer)
		try
		{
			t = document.getElementById(t);
			tr = t.getElementsByTagName("tr");
			th = tr[0].getElementsByTagName("th");
			if(th.length==0)
			{
				th = tr[0].getElementsByTagName("td");
			}
			for(x=0;x<widths.length;x++)
			{
				th[x].style.width=widths[x];
			}
		}catch(e)
		{
				//width count does not match Column count
		}
	}
}
