
function randColour() {
	colour = "#";
	for (i = 0; i < 6; i++) {
		temp = Math.floor(Math.random() * 16);
		      if (temp == 10) {	colour += "A";
		}else if (temp == 11) {	colour += "B";
		}else if (temp == 12) {	colour += "C";
		}else if (temp == 13) {	colour += "D";
		}else if (temp == 14) {	colour += "E";
		}else if (temp >= 15) {	colour += "F";
		}else{
			colour += temp;
		}
	}
	return colour;
}

function makeBarGraph(align) {

	  var total = 0;
	var numcats = 0;
	var largest = 0;

	for (z = 0; catname[z]; z++) {
		total += cattotal[z];
		if (cattotal[z] > largest) {
			largest = cattotal[z];
		}
		numcats++;
	}

	 catwidth = new Array();
	catheight = new Array();

	document.write("<div style=\"position:relative; width:" + (width + 4) + "px; height:" + (height + 4) + "px;\">\n");
	document.write("<div style=\"position:absolute; top:" + 0 + "px; left:" + 0 + "px; height:" + (height + 4) + "px; width:" + (width + 4) + "px; background:" + bordercolor + ";\"></div>");
	document.write("<table style=\"position:absolute; top:2px; left:2px; height:" + height + "px; width:" + width + "px;\" border=0 bordercolor=black cellpadding=0 cellspacing=0>\n");

	if (align == "v") {
		document.write("<tr><td style=\"width:" + width + "px; height:" + titleheight + "px; border:0; background:" + titlebgcolor + ";font-weight:bold; \" align=center valign=center><font color=\"" + titlecolor + "\">" + title + "</font></td>\n");
		document.write("<tr><td style=\"width:" + width + "px; border:0; background:" + mainbgcolor + ";\"></td>\n");
		document.write("<tr><td style=\"width:" + width + "px; height:" + labelheight + "px; border:0; background:" + labelbgcolor + ";font-size:xx-small; font-weight:bold; \"></td>\n");
	}else{
		document.write("<tr><td style=\"width:" + labelwidth + "px; border:0; background:" + labelbgcolor + ";\"></td><td style=\"width:" + (width-labelwidth) + "px; height:" + titleheight + "px; border:0; background:" + titlebgcolor + ";\" align=center valign=center><font color=\"" + titlecolor + "\">" + title + "</font></td>\n");
		document.write("<tr><td style=\"width:" + labelwidth + "px; border:0; background:" + labelbgcolor + ";\"></td><td style=\"width:" + (width-labelwidth) + "px; border:0; background:" + mainbgcolor + ";\"></td>\n");
	}

	document.write("</table>");

	for (z = 0; catname[z]; z++) {
		if (align == "v") {
			if (catcolor[z] == "" || !catcolor[z]) {
				catcolor[z] = randColour();
			}
			if (catcolor[z] == mainbgcolor) { catcolor[z] = randColour();}
			 catwidth[z] = (width / numcats) - gap;
			catheight[z] = (cattotal[z] / largest) * (height - titleheight - labelheight) ;
			document.write("<img src=\"clear.gif\" style=\"position:absolute; top:" + (height + 2 - catheight[z] - labelheight)  + "px; left:" + (((catwidth[z] + gap) * z) + (gap/2) + 2) + "px; height:" + catheight[z] + "px; width:" + catwidth[z] + "px; background:" + catcolor[z] + ";\" border=0>\n");
		}else{
			if (catcolor[z] == "" || !catcolor[z]) {
				catcolor[z] = randColour();
			}
			if (catcolor[z] == mainbgcolor) { catcolor[z] = randColour();}
			 catwidth[z] = (cattotal[z] / largest) * (width - labelwidth) ;
			catheight[z] = ((height - titleheight) / numcats) - gap;
			document.write("<img src=\"clear.gif\" style=\"position:absolute; top:" + (titleheight + (z * (catheight[z] + gap)) + (gap/2) + 2)  + "px; left:" + (labelwidth + 2) + "px; height:" + catheight[z] + "px; width:" + catwidth[z] + "px; background:" + catcolor[z] + ";\" border=0>\n");
		}
	}

	if (align == "v") {
		document.write("<table style=\"position:absolute; top:" + (height - labelheight) + "px; left:" + (1) + "px; height:" + labelheight + "px; width:" + width + "px;\" border=0 cellpadding=" + (gap/2) + " cellspacing=0><tr>");
		for (z = 0; catname[z]; z++) {
			document.write("<td width=" + (catwidth[z]+gap) +" align=center valign=top><font color=\"" + labelcolor + "\">" + catname[z] + "</font></td>\n");
		}
		document.write("</tr></table>\n");

		document.write("<table style=\"position:absolute; top:" + (titleheight) + "px; left:" + (1) + "px; height:" + labelheight + "px; width:" + width + "px;\" border=0 cellpadding=" + (gap/2) + " cellspacing=0><tr>");
		for (z = 0; catname[z]; z++) {
			document.write("<td width=" + (catwidth[z]+gap) +" align=center valign=top><font color=\"" + percentcolor + "\">");
			if (disppercent == 1) {
				document.write(Math.round((cattotal[z] / total) * 100) + "%<br>\n");
			}
			if (disptotals == 1) {
				document.write("<small>" + cattotal[z] + " / " + total + "</small>\n");
			}
			document.write("</font></td>\n");
		}
		document.write("</tr></table>\n");
	}else{
		document.write("<table style=\"position:absolute; top:" + (titleheight) + "px; left:" + (1) + "px; height:" + (height - titleheight) + "px; width:" + labelwidth + "px;\" border=0 cellpadding=" + (gap/2) + " cellspacing=0>");
		for (z = 0; catname[z]; z++) {
			document.write("<tr><td style=\"height:" + (((height - titleheight) / numcats) - gap) + "px; width:" + labelwidth + "px;\" align=right valign=center><font color=\"" + labelcolor + "\">" + catname[z] + "</font></td></tr>\n");
		}
		document.write("</table>\n");

		document.write("<table style=\"position:absolute; top:" + (titleheight) + "px; left:" + (width - (labelwidth + 30)) + "px; height:" + (height - titleheight) + "px; width:" + (labelwidth + 30) + "px;\" border=0 cellpadding=" + (gap/2) + " cellspacing=0>");
		for (z = 0; catname[z]; z++) {
			document.write("<tr>");
			if (disptotals == 1) {
				document.write("<td style=\"height:" + (((height - titleheight) / numcats) - gap) + "px; width:" + labelwidth + "px;\" align=right valign=center><font color=\"" + percentcolor + "\">");
				document.write("<small>(" + cattotal[z] + " / " + total + ")</small></font></td>");
			}
			if (disppercent == 1) {
				document.write("<td style=\"height:" + (((height - titleheight) / numcats) - gap) + "px; width:30px;\" align=right valign=center><font color=\"" + percentcolor + "\">");
				document.write(Math.round((cattotal[z] / total) * 100) + "%</font></td>\n");
			}
			document.write("</tr>\n");
		}
		document.write("</table>\n");

	}
	document.write("</div>\n");
}

 catname = new Array();
cattotal = new Array();
catcolor = new Array();
