
function showDiagram(ev) {
if (!document.getElementById) return;
try {
	test=ev.target.tagName
	elt=ev.target
}
catch (ex) {
	elt=event.srcElement
}
parentTR=elt
counter=0;
while (parentTR.tagName!="TR") {
	parentTR=parentTR.parentNode
	counter++;
	if ((counter>20) || (parentTR.tagName=="BODY")) {
		alert("The parent table row not found!"); return false
	}
}
	tbody=parentTR.parentNode; 
	removeEmptyNodes(tbody)
	removeEmptyNodes(parentTR)
	try {
		dCaption=parentTR.firstChild.firstChild.nodeValue
	}
	catch (e) {dCaption=""}
	namesTR=parentTR.nextSibling;
	removeEmptyNodes(namesTR)
	valuesTR=namesTR.nextSibling;
	removeEmptyNodes(valuesTR)
	
	namesTD=namesTR.childNodes
	names=new Array();
	valuesTD=valuesTR.childNodes
	values=new Array();
	valuesFiltered=new Array();
	for (i=0;i<namesTD.length;i++) names[i]=namesTD[i].innerHTML;
	for (i=0;i<valuesTD.length;i++) values[i]=valuesTD[i].innerHTML;//alert(values[4])
	if (isNegative(valuesFiltered)) return;
	getFiltered(values,valuesFiltered)
	dDiv=document.createElement("DIV")
	xPos=ev.clientX
	yPos=ev.clientY+document.body.scrollTop
	dDiv.style.top=yPos; dDiv.style.left=xPos
	addCloseButton(dDiv,elt)
	dDiv.className="diagramDIV"
	document.body.appendChild(dDiv)	
	minWidth=20
	dWidth=Math.max(dDiv.offsetWidth,minWidth*names.length)
	dDiv.style.width=dWidth
	
	tab=document.createElement("TABLE")
	tab.setAttribute("width","100%")
	tab.setAttribute("cellSpacing","3")
	tab.setAttribute("cellPadding","0")	
	tab.setAttribute("border","0")
	dCapt=document.createElement("CAPTION")
	dCapt.className="diagramCAPTION"
	dCapt.innerHTML=dCaption
	tab.appendChild(dCapt);
	tb=document.createElement("TBODY")
	tab.appendChild(tb)
	tr1=document.createElement("TR")
	dHeight=parseInt((dDiv.offsetHeight-dCapt.offsetHeight)*0.75)
	tr1.style.height=dHeight+"px"	
	
	m=maxElt(valuesFiltered)
	if (m==0) 	alert("Error: non-numeric values found!");
	else {
		for (i=0;i<values.length;i++) {
			td=document.createElement("TD");
			td.setAttribute("vAlign","bottom")
			
			barD=document.createElement("DIV")
			barD.innerHTML="&nbsp;"
			barD.className="diagramBAR"
			h=parseInt(valuesFiltered[i]/m*dHeight)+"px"
			barD.style.lineHeight=h;// alert(barD.style.height)
			barD.style.height=h;
			barD.innerHTML="<span>"+values[i]+"</span>"
			td.appendChild(barD)
			tr1.appendChild(td)
		}
	}

	tb.appendChild(tr1)
	tr2=document.createElement("TR")
	for (i=0;i<names.length;i++) {
		td=document.createElement("TD");
		lab=document.createElement("DIV")
		lab.innerHTML=names[i];
		lab.style.width=parseInt((dDiv.offsetWidth-tab.getAttribute("cellSpacing")*names.length)/names.length)+"px"
		lab.className="diagramLABEL"
		td.appendChild(lab)
		tr2.appendChild(td)
	}
	tb.appendChild(tr2)
	
        dDiv.appendChild(tab)
        minWidth=tab.offsetWidth
        dWidth=Math.max(dDiv.offsetWidth,minWidth)
        dDiv.style.width=dWidth
          /**flip if too width or too high***/
        try {
            dWidth=dDiv.offsetWidth
            diff=document.body.clientWidth-xPos;
            if (dWidth>diff) dDiv.style.left=xPos-dWidth
            dHeight=dDiv.offsetHeight
            diff=document.body.clientHeight-yPos;//alert(document.body.clientHeight)
            if (dHeight>diff) dDiv.style.top=yPos-dHeight
        } catch(e) {}
         /***/
         tds=tr1.childNodes
         for (i=0;i<tds.length;i++) {
                             d=tds[i].firstChild
                             tx=d.firstChild
                             tx.style.position="relative"
                             h1=d.offsetHeight
                             h2=tx.offsetHeight
                             tx.style.bottom=parseInt(h1/2-h2)+"px" //
        }
        elt.style.visibility="hidden"
}

function isNegative(arr) {
	for (ar in arr) {
		if (arr[ar]<0) {
			alert("Error:\nthis version of TableDiagram works with positive values only!\nNegative value found: "+arr[ar])
			return true
		}
	}
return false
}

function maxElt(arr) {
b=arr.toString()
try {
a=eval("Math.max("+b+")")
}
catch (e) {
	return 0
}
return a
}

function addCloseButton(elt,pElt) {
	bar=document.createElement("DIV")
	bar.className="diagramCloseBAR"
	bar.setAttribute("align","right")
	bar.setAttribute("width","100%")
	
	x=document.createElement("SPAN")
	x.innerHTML="<a href='javascript:void(null)' style='color:transparent'>&nbsp;&times;&nbsp;</a>";
	x.onclick=function(){elt.parentNode.removeChild(elt);pElt.style.visibility="visible";}
	x.className="diagramX"
	bar.appendChild(x)
	elt.appendChild(bar)
}

function removeEmptyNodes(elt) {
 	for (i=0;i<elt.childNodes.length;i++) {
		try {
		a=elt.childNodes[i].innerHTML.length	;//alert(a)
		}
		catch (e){
		x=elt.childNodes[i];//alert(x)
		elt.removeChild(x); 
		i--}
		
	}
}

function getFiltered(init_arr,res_arr) {
 for (num in init_arr) {
  tmp=init_arr[num]
  while (tmp.indexOf(".")>0)
   tmp=tmp.replace(".","")
  while (tmp.indexOf(" ")>0)
   tmp=tmp.replace(" ","")
  res_arr[num]=tmp
 }
}