function percent (percentage, min)
{	percentage = Math.round (percentage * 100);
	if (percentage < min) percentage = min;
	return percentage+"%";
}

function flags (readonly, align)
{	if (align == null) align = 'right';	
	readonlyflags = "STYLE='border-top: none; border-right: none; border-bottom: none; border-left: none; background-color: transparent; text-align: "+align+"' READONLY='READONLY' ALIGN='RIGHT'";
	readwriteflags = "STYLE='background-color: transparent; text-align: " +align+"'";

	if (readonly) return readonlyflags; else return readwriteflags;
}

function colour(k)
{	k = k % 10;
	if (k==0) return "#FFFFFF";
	if (k==1) return "#FFFFC0";
	if (k==2) return "#FFC0FF";
	if (k==3) return "#C0FFFF";
	if (k==4) return "#FFC0C0";
	if (k==5) return "#C0FFC0";
	if (k==6) return "#C0C0FF";
	if (k==7) return "#FF80C0";
	if (k==8) return "#80C0FF";
	if (k==9) return "#C0FF80";
}

function split_left (ph)
{	if ((i=ph.indexOf("/")) == -1) return ph+" 1"; else return ph.substring (0,i);
}

function split_right (ph)
{	if ((i=ph.indexOf("/")) == -1) return ph+" 2"; else return ph.substring (i+1);
}

function merge_both (ph1, ph2)
{	if (((l=ph1.length) == ph2.length) && ((s=ph1.substring (0,l-2)) == ph2.substring (0, l-2))) return s;
	return ph1 + "%2f" + ph2;
}


function draw_actual_lines (lines)
{	no_of_lines = lines.length;
	for (this_line=no_of_lines; this_line>0; --this_line) {
	
		rv1 = ""; rv2 = ""; rv3 = "";
		if (this_line > 1)
			this_line_data = lines[this_line-2].split(">");
		else
			this_line_data = ["","","",""];
	
	
		document.write ("<TABLE WIDTH='" + percent (((this_line+1)/(no_of_lines+1))*0.8 + 0.2) + "'><TR>");
		document.write ("<TD ALIGN='RIGHT'>");

			// the phase values - update the percentages
			document.write ("Value = <INPUT TYPE='TEXT' NAME='v" + this_line + "' SIZE='" + (4+2*this_line) + "' " + flags (this_line != no_of_lines, "left"));
			if (this_line == no_of_lines) 
				document.write ("VALUE='" + lines[this_line-1].split(">")[0] + "' ");
			document.write (" ONCHANGE='");
			if (this_line < no_of_lines) 
				document.write ("if (document.x.p"+(this_line+1)+(this_line+1)+".value!=0) tmp=Math.round(document.x.a"+(this_line+1)+(this_line+1)+".value*100/document.x.p"+(this_line+1)+(this_line+1)+".value);else tmp=document.x.v"+(this_line+1)+".value;if (value!=tmp){value=tmp;");
			else
				document.write ("{");
				// propagate the changes
				if (this_line > 1)
					document.write ("document.x.p"+this_line+this_line+".onchange();");
				else
					document.write ('document.x.b'+this_line+this_line+'.onchange();');
				for (j=1; j<this_line; ++j) 
					document.write ('document.x.b'+this_line+j+'.onchange();');
			document.write ("}'>");

		document.write ("</TD>");
		document.write ("</TR></TABLE>");

		
		document.write ("<TABLE WIDTH='100%'><TR>");
			document.write ("<TD WIDTH='10%'></TD>");
			document.write ("<TD WIDTH='80%'>");
				// b line (they come at the end of the phase)
				document.write ("<TABLE WIDTH='" + percent ((this_line+1)/(no_of_lines+1), 0) + "' BORDER='1'><TR>"); 
				for (k=1; k<=this_line; ++k) {
					document.write ("<TD ALIGN='RIGHT' BGCOLOR='" + colour(k) + "'>"); 
						document.write ("<INPUT TYPE='TEXT' NAME='b" + this_line + k + "' SIZE='" + (6+this_line) + "'" + flags (true, 'right'));
						if (k == this_line) {
							document.write (" ONCHANGE='");
							if (this_line > 1)
								document.write ("tmp=Math.round(document.x.a"+this_line+this_line+".value*(1+document.x.ROI"+this_line+".value/100)/(document.x.risk"+this_line+".value/100));");
							else
								document.write ("tmp=document.x.v"+this_line+".value;");
							document.write ("if (value!=tmp) {value=tmp; document.x.p"+this_line+this_line+".onchange();}'");
						}
						else {
							document.write (" ONCHANGE='value=Math.round(document.x.v"+this_line+".value*document.x.p"+this_line+k+".value/100);'");
						}
						document.write (">");
					document.write ("</TD>");
				}
				document.write ("</TR></TABLE>");	
			document.write ("</TD>");
			document.write ("<TD WIDTH='10%'></TD>"); 
		document.write ("</TR></TABLE>");
			
		// Phase control line
		if (this_line>1) {
			document.write ("Phase:" + "<INPUT TYPE='TEXT' NAME='ph" + this_line + "' VALUE='" + unescape(this_line_data[1]) + "' SIZE='20' STYLE='background-color: transparent' > &nbsp; &nbsp; ");
			document.write ("ROI:" + "<INPUT TYPE='TEXT' NAME='ROI" + this_line + "' VALUE='" + this_line_data[2] + "' SIZE='5' STYLE='background-color: transparent; text-align: right' ONCHANGE='document.x.b"+this_line+this_line+".onchange();'>% &nbsp; &nbsp; ");
			document.write ("Odds:" + "<INPUT TYPE='TEXT' NAME='risk" + this_line + "' VALUE='" + this_line_data[3] + "' SIZE='5' STYLE='background-color: transparent; text-align: right' ONCHANGE='document.x.b"+this_line+this_line+".onchange();'>% &nbsp; &nbsp;");
			// split button
			document.write ("<INPUT TYPE='BUTTON' VALUE='Split' SIZE='7' ONCLICK='location.search=");
				for (j=2; j<=no_of_lines; ++j) 
					if (j==this_line)
						document.write ('(document.x.a'+j+j+'.value/2)+">"+split_left(document.x.ph'+j+'.value)+">"+document.x.ROI'+j+'.value+">"+Math.round(Math.sqrt(document.x.risk'+j+'.value/100)*100)+">>"+(document.x.a'+j+j+'.value/2)+">"+split_right(document.x.ph'+j+'.value)+">"+document.x.ROI'+j+'.value+">"+Math.round(Math.sqrt(document.x.risk'+j+'.value/100)*100)+">>"+');
					else
						document.write ('document.x.a'+j+j+'.value+">"+document.x.ph'+j+'.value+">"+document.x.ROI'+j+'.value+">"+document.x.risk'+j+'.value+">>"+');
				document.write ('document.x.v'+no_of_lines+'.value');
				document.write (";'> &nbsp; &nbsp; ");
		}
				
		document.write ("<TABLE WIDTH='100%'>");
		document.write ("<TR>");
			document.write ("<TD WIDTH='10%'>");
			document.write ("</TD>");
			document.write ("<TD WIDTH='80%'>");
				if (this_line != 1) {
					document.write ("<TABLE WIDTH='" + percent ((this_line)/(no_of_lines+1), 0) + "' BORDER='1'><TR>");
					// the a line - where the phase starts
					for (k=1; k<=this_line; ++k) {
						document.write ("<TD ALIGN='RIGHT' BGCOLOR='" + colour(k) + "'>"); 
							document.write ("<INPUT TYPE='TEXT' NAME='a" + this_line + k + "' SIZE='" + (6+this_line) + "'"+ flags (k != this_line, 'right'));
							if (k == this_line) {
								document.write (" VALUE='" + this_line_data[0] + "' ONCHANGE='document.x.b"+this_line+this_line+".onchange();");
									if (this_line > 1)
										document.write ("document.x.v"+(this_line-1)+".onchange();");
									else
										document.write ("document.x.b"+(this_line-1)+(this_line-1)+".onchange();");
								document.write ("'");
							}
							else {
								document.write (" ONCHANGE='value=Math.round(document.x.p"+this_line+k+".value*document.x.v"+(this_line-1)+".value/100);'");
							}
							document.write (">");
						document.write ("</TD>");
					}
					document.write ("</TR></TABLE>");
				}
			document.write ("</TD>"); 
			document.write ("<TD WIDTH='10%'></TD>"); 
		document.write ("</TR>");
		document.write ("<TR>");
			document.write ("<TD WIDTH='10%'>");
			if (this_line>2) {
				// Merge button
				document.write ("<INPUT TYPE='BUTTON' VALUE='Merge' SIZE='7' ONCLICK='location.search=");
					for (j=2; j<=no_of_lines; ++j) 
						if (j==(this_line-1)) {
							document.write ('((1*document.x.a'+j+j+'.value)+(1*document.x.a'+(j+1)+(j+1)+'.value))+">"+merge_both(document.x.ph'+j+'.value,document.x.ph'+(j+1)+'.value)+">"+Math.round(((document.x.ROI'+j+'.value*document.x.a'+j+j+'.value)+(document.x.ROI'+(j+1)+'.value*document.x.a'+(j+1)+(j+1)+'.value))/((1*document.x.a'+j+j+'.value)+(1*document.x.a'+(j+1)+(j+1)+'.value)))+">"+Math.round(((document.x.risk'+j+'.value/100)*(document.x.risk'+(j+1)+'.value/100))*100)+">>"+');
							++j;
						}
						else document.write ('document.x.a'+j+j+'.value+">"+document.x.ph'+j+'.value+">"+document.x.ROI'+j+'.value+">"+document.x.risk'+j+'.value+">>"+');
					document.write ('document.x.v'+no_of_lines+'.value');	

				document.write (";'>");
			}
			document.write ("</TD>");
			document.write ("<TD WIDTH='80%'>");
				document.write ("<TABLE WIDTH='" + percent ((this_line)/(no_of_lines+1), 0) + "'><TR>");
				// the percentage lines 
				for (k=1; k<=this_line; ++k) {
	
					document.write ("<TD ALIGN='RIGHT'>");
						document.write ("<INPUT TYPE='TEXT' NAME='p" + this_line + k + "' VALUE='p" + this_line + k + "%' SIZE='6'"+ flags (true, 'right'));
						document.write (" ONCHANGE='");
								//document.write ("upstack();");
								//document.write ('debugout ("entered p'+this_line + k +' with value "+value);');
						if (k == this_line) {
							document.write ("tmp=Math.round(1000*document.x.b"+this_line+this_line+".value/document.x.v"+this_line+".value)/10;if(value!=tmp){value=tmp;");
							if (this_line > 1) document.write ("document.x.v"+(this_line-1)+".onchange();");
							for (j=1; j<k; ++j)
								document.write ("document.x.p"+this_line+j+".onchange();");
						}
						else {
							document.write ("tmp=Math.round(1000*document.x.p"+(this_line-1)+k+".value/100*(1-document.x.p"+this_line+this_line+".value/100))/10;if ((value!=tmp) && !(!isFinite(value) && !isFinite(tmp))) {value=tmp;");
							document.write ("document.x.a"+this_line+k+".onchange(); document.x.b"+this_line+k+".onchange();");
						}
						if (this_line < no_of_lines) 
							document.write ("document.x.p"+(this_line+1)+k+".onchange();");
						else
							document.write ('if (isFinite(value)) cellAt("g'+k+'").width=percent(value/100, 1);');
						document.write ("}if (tmp==0)document.x.v"+(this_line-1)+".onchange();");
								//document.write ('debugout ("exited p'+this_line+k+' with value "+value);');
								//document.write ("downstack();");
						document.write ("'");
						document.write ("><FONT SIZE='-2'>%</FONT>");
					document.write ("</TD>");
				}
				document.write ("</TR></TABLE>");
			document.write ("</TD>");
			document.write ("<TD WIDTH='10%'></TD>");
		document.write ("</TR>");
		document.write ("</TABLE>");
	}
}


function draw_lines (litlines)
{	if (dont_scroll=(litlines == "")) litlines = "1000>Prototype>30>50>>10000"
	
	level = 0; rvg="";	
	lines = litlines.split (">>"); no_of_lines = lines.length;
	for (k=1; k<=lines.length; ++k) rvg = rvg + "<TD BGCOLOR="+colour(k)+" id='g"+k+"'>&nbsp;</TD>";
	document.write ("<TABLE WIDTH='100%'><TR>");
		document.write ("<TD WIDTH='10%'></TD>");
		document.write ("<TD WIDTH='80%'><TABLE WIDTH='100%' BORDER='1' BGCOLOR='#202020' id='gg'><TR>"+rvg+"</TR></TABLE></TD>");
		document.write ("<TD WIDTH='10%' ALIGN='CENTER'>");
			document.write ("<INPUT TYPE='BUTTON' VALUE='Save' ONCLICK='");
				document.write ('location.search=');
				for (j=2; j<=no_of_lines; ++j) 
					document.write ('document.x.a'+j+j+'.value+">"+document.x.ph'+j+'.value+">"+document.x.ROI'+j+'.value+">"+document.x.risk'+j+'.value+">>"+');
				document.write ('document.x.v'+no_of_lines+'.value');	
				document.write (';alert ("Please select Add To Favorites or Bookmark now")');
			document.write ("'>");
		document.write ("</TD>");
	document.write ("</TR></TABLE>");
	draw_actual_lines (lines);
	for (k=1; k<=lines.length; ++k) cellAt ("b"+k+k).onchange();
	if (!dont_scroll) setTimeout ("window.scroll (0, 100000)", 1);
}


function cellAt (cn)
{	if (document.all)
		return (document.all[cn]);
	else if (t = document.getElementById (cn))
		return t;
	else
		return document.getElementsByName(cn)[0];
}

function upstack ()
{	if (level == null) level = 0; else level += 1;
}

function downstack ()
{	if (level == null) level = 0; else level -= 1;
}

function debugout (str)
{	alert (level + ": " + str);
}


