/* ====================================================================== */
/* === CLIMACT PROGRESS BAR ============================================= */
/* ====================================================================== */


function findObj(theObj, theDoc) {
  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}


function renderProgressBar(value1, value2, value3, value4, value5, average, text) {
	tPos = parseFloat((parseFloat(value1) + parseFloat(value2) + parseFloat(value3)));
	tNeg = parseFloat((parseFloat(value4) + parseFloat(value5)));
	footPrint = tPos - tNeg;
	if (footPrint<0) footPrint=0;
	
	if (tPos>=0 && tPos<=20) pbMaxValue = 20;
	else if (tPos>=0 && tPos<=40) pbMaxValue = 40;
	else if (tPos>=0 && tPos<=80) pbMaxValue = 80;
	else if (tPos>=0 && tPos<=160) pbMaxValue = 160;
	else if (tPos>=0 && tPos<=320) pbMaxValue = 320;
	else if (tPos>=0 && tPos<=640) pbMaxValue = 640;
	else if (tPos>=0 && tPos<=1280) pbMaxValue = 1280;
	else if (tPos>=0 && tPos<=2560) pbMaxValue = 2560;
	else if (tPos>=0 && tPos<=5120) pbMaxValue = 5120;
	else if (tPos>=0 && tPos<=10240) pbMaxValue = 10240;
	else {
		alert ("ProgressBar: Scale error: total is greater than 320");
		return false;
	}
	scale = 305 / pbMaxValue;
																							 
	if (tNeg > tPos) {
		if (parseInt(value4) > tPos) { value4 = tPos; value5 = 0; }
		else { value5 = tPos-parseFloat(value4); }
		tNeg = tPos;
	}
		
	// Compute positive bars
	posBar1 = Math.floor(value1 * scale);
	posBar2 = Math.floor(value2 * scale);
	posBar3 = Math.floor(value3 * scale);
	posBarWidth = posBar1 + posBar2 + posBar3;
	
	// Compute negative bars
	negBar1 = Math.floor(value4 * scale);
	negBar2 = Math.floor(value5 * scale);
	negBarWidth = negBar1 + negBar2;
	negBarLeft = posBarWidth - negBarWidth + 5;
	
	// Positive
	progressBarValue1 = findObj("ProgressBarValue1");
	progressBarValue1.style.width = (posBar1) +"px";
	progressBarValue2 = findObj("ProgressBarValue2");
	progressBarValue2.style.width = (posBar2)+"px";
	progressBarValue3 = findObj("ProgressBarValue3");
	progressBarValue3.style.width = (posBar3)+"px";
	
	// Negative
	progressBarNegValues = findObj("ProgressBarNegValues");
	progressBarNegValues.style.width = (negBarWidth)+"px";
	progressBarNegValues.style.left = (negBarLeft)+"px";
	progressBarValue5 = findObj("ProgressBarValue5");
	progressBarValue5.style.width = (negBar1)+"px";
	progressBarValue4 = findObj("ProgressBarValue4");
	progressBarValue4.style.width = (negBar2)+"px";
	
	// Average
	progressBarAverageText = findObj("ProgressBarAverageText");
	
	if (average!='' && text!='' && (parseFloat(average) <= pbMaxValue))  {
		progressBarAverageText.style.display = "block";
		progressBarAverageText.style.left = (Math.floor(average * scale))+"px";
		progressBarAverageText.innerHTML = text;
	} else {
		progressBarAverageText.style.display = "none";
	}
	
	// Total
	progressBarTotal = findObj("ProgressBarTotalFigure");
	progressBarTotal.innerHTML = footPrint.toFixed(2);
	
}