//  first some variables that we will use from a few places

var loc;
var radius;
var runits;
var cunits;
var measureunits;
var orientation;
var LinesPerColumn=50;
var dirstring;
var diastring;
var lenstring;
var increment;
var NumDec;

var pi=Math.PI;
var ChartWin;
var ChartDoc;

//  Client Specific Branding Info
//  set this to an empty string for no logo in top frame
  var LogoFileName="";
//var LogoFileName="http://www.petroleumtechinc.com/images/main/logo400.jpg";
//  define how many columns in your chart
//  2 seems to work best for printing overall
var MaxColumns=4;

//  Set this to 0 if you want the charts in the same
//  Window, and not in a popup window.
//  When you do this, it'll break in Mozilla
//  but ie processes it fine.
var UsePopup=0;

// define which units of measure we will display
var ShowUs=1;
var ShowImperial=0;
var ShowMetric=0;


//  A function to easily place your info in the
//  top left frame of the chart header
function WriteBrandingInfo()
{
	writeln("Petroleum Technologies Inc");
	writeln("8351 Roswell Road, Suite 222");
	writeln("Atlanta, GA. 30350");
	writeln("678-520-2501 or 770-310-6558")
	writeln("<a href=\"http://www.petroleumtechinc.com\">www.petroleumtechinc.com</a>");
}


//  General purpose write function that will write
//  to our window in which the chart pops up

function write(a)
{
	ChartDoc.write(a);
}

function writeln(a)
{
	write(a);
	write("<br>");
}


function WriteHeaderTable()
{

	if (LogoFileName=="") tw="\"50%\"";
	else tw="\"33%\"";


	write("<center>");

	write("<table width=\"80%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
	write("<td align=\"center\"  width="+tw+">");
	WriteBrandingInfo();
	write("</td>");

	write("<td width="+tw+">");
	write("<center>Dip Table for<br>");
	write(loc);
	write("</td>");

	if (LogoFileName != "") {
		write("<td width="+tw+">");
		write("<img href=\"");
		write(LogoFileName);
		write("\" alt=\"Logo goes here\">");
		write("</td>");
		write("</table>");
	}

}



function GenTankChart()
{
	var diameter;
	var length;
	var inc;

	

	length=Number(document.tankform.length.value);
	diameter=Number(document.tankform.diameter.value);
	loc=document.tankform.location.value;
	//LinesPerColumn=Number(document.tankform.PrintRows);
	if ((length==length)&&(diameter==diameter)&&(length>0)&&(diameter>0)) {

		//  we got numbers, go do the calculations and write a table

		increment=1.0;
		NumDec=0;

		inc=document.tankform.increment[0].checked;
		if(inc) {
			increment=0.25;
			NumDec=2;
		}
		inc=document.tankform.increment[1].checked;
		if(inc) {
			increment=0.50;
			NumDec=1;
		}
		inc=document.tankform.increment[3].checked;
		if(inc) increment=2.0;

	

		if(increment < 1/MaxColumns) increment=1/MaxColumns;


		//if(Number(increment.toFixed(0))==increment) NumDec=0;

		dunits=document.tankform.dunits.value;
		lunits=document.tankform.lunits.value;
		runits=document.tankform.runitsvalue;
		measureunits=document.tankform.cunits.value;

		orientation=0;
        inc=document.tankform.orientation[0].checked;
		if(inc) orientation=1;

		radius=diameter/2;

		if (lunits==1) length=length*2.54;
		if (dunits==1) radius=radius*2.54;

		//  Open a window to write in, and get it's document
		if (UsePopup==1) {
			
			ChartWin=window.open("about:blank","tankwin");
			ChartDoc=ChartWin.document.open();

			//write("Increment is "+inc);
			//write("Orientation is "+orientation);

		} else {
			ChartDoc=window.document;
		}


		WriteHeaderTable();
		WriteDipChart(length,radius,orientation,measureunits);

		//  close the document
		ChartDoc.close();
		return 0;

	}

	//  got bad values
	alert("Invalid values entered");

	return;
}


function CcToImpGal(number)
{
	return number/1000/4.54609;
}

function CcToAmGal(number)
{
	return number/1000/3.75;
}

function CcToLiters(number)
{
	return number/1000;
}

//  A function to place the headers at the top of each
//  column of the output.  Plug it in a function by itself
//  so we dont have to do this many times, just call this
//  function
function WriteChartHeaders()
{

	write("<p><table border=1 cellpadding=\"0\" cellspacing=\"0\">");
	write("<tr>");
	write("<th>Depth<br>");
	if (measureunits==2) write("cm");
	else write("inches");
	write("</th>");
	if (ShowMetric==1) {
		
		write("<th>&nbsp;Litres&nbsp;</th>");
	}
	if (ShowImperial==1) {
		
		write("<th>&nbsp;Canadian&nbsp;<br>Gallons</th>");
	}
	if (ShowUs==1) {
		
		write("<th>&nbsp;American&nbsp;<br>Gallons</th>");
	}
	write("</tr>");

}

function WriteChartFooters()
{
	write("</table>");
}


function WriteOneRow(volume,step)
{
	write("<tr>");
	write("<td align=\"right\">"+step.toFixed(NumDec)+"</td>");
	if (ShowMetric==1) {
		
		write("<td align=\"right\">&nbsp;"+CcToLiters(volume).toFixed(1)+"&nbsp;</td>");
	}
	if (ShowImperial==1) {
		
		write("<td align=\"right\">&nbsp;"+CcToImpGal(volume).toFixed(1)+"&nbsp;</td>");
	}
	if (ShowUs==1) {
		
		write("<td align=\"right\">&nbsp;"+CcToAmGal(volume).toFixed(1)+"&nbsp;</td>");
	}
	write("</tr>");
}

function WriteTableHeader(direction,radius,length)
{

	//  header the chart with a 3 element table
	//  first a branding element
	//  then the tank info element
	//  and then a final element

	//  null stub this function as we have moved this
	//  to the raw html now, so it's easier to drop in
	//  branding graphics etc
	//  instead of emitting html here, now we just set the inline variables
	dirstring=String(direction);
	diastring=String((radius*2/2.54).toFixed(2));
	lenstring=String((length/2.54).toFixed(2));


	return 0;
	write("<center>");
	write("<table border=\"1\" width=\"90%\" cellpadding=\"0\" cellspacing=\"0\">");
	write("<td width=\"33%\" valign=\"top\" align=\"left\">");
	WriteBrandingInfo();
	write("</td><td width\"33%\">");
	write("<center>");
	writeln("Tank Dip Table for <br>"+loc);
	writeln(direction+" Tank<br>Diameter "+(radius*2/2.54).toFixed(2)+" inches<br>Length "+(length/2.54).toFixed(2)+" inches");
	write("</td>");
	write("<td width=\"33%\">&nbsp;</td>");

}

//  This is the meat and potatoes for a tank laid
//  horizontally.  Calculate the volume occupied for
//  various levels of the dip
//  format the output in a 'nice' way
function WriteHorizontalChart(length,radius,measureunits)
{
	var numsteps;
	var cmperstep;
	var volume;
	var step;
	var depth;
	var TotalVolume;


	//WriteTableHeader("Horizontal",radius,length);

	//  We actually do all calcs in metric
	//  but if the dipstick is calibrated in imperial
	//  then lets step 2.54 centimeters at a time
	//  and call it an inch
	if (measureunits==1) {
		cmperstep=2.54*increment;
	} else {
		cmperstep=1*increment;
	}

	TotalVolume=radius*radius*length;
	//write("Total ="+TotalVolume);




	//  set up our loop, then loop thru
	//  doing a printout for each depth level
	//  until the depth reaches the top of the tank

	numsteps=radius/cmperstep*2;
	LinesPerColumn=(numsteps/MaxColumns).toFixed(0);

	//write("LinesPer "+LinesPerColumn+"  numsteps "+numsteps);

	write("<table border=0 ><td valign=\"top\">");
	WriteChartHeaders();


	depth=cmperstep;
	step=increment;
	while (depth <= radius*2) {
		var volume;
		var val;
		var ac;
		var rad;
		var sin;
		var cos;

		//  The commented out prints were useful in debugging the math
		//  I've left them in place commented so you can see how it's
		//  actually pretty easy to play with all this


		ac=((radius-depth)/radius);
		//write("<td>ac="+ac+"</td>");
		rad=Math.acos(ac);
		//write("<td>rad="+rad+"</td>");
		sin=Math.sin(rad);
		cos=Math.cos(rad);
		//write("<td>sin="+sin+"</td>");
		volume=TotalVolume*(rad-(sin*cos));
		//write("<td>vol="+volume+"</td>");

		//  Once we have the volume, spit it out in all the different
		//  units of measure
		WriteOneRow(volume,step);

		//  if we have done enough rows to fill this column, and there
		//  is still more depth to deal with, lets start the next column
		if (((step/increment)%LinesPerColumn==0)&&(depth <= radius*2-cmperstep)) {
			write("</table>");
			write("</td><td>&nbsp</td><td valign=\"top\">");
			WriteChartHeaders();
		}


		depth=depth+cmperstep;
		step=step+increment;
	}
	write("</td></table>");
}

//  This is virtually identical to the horizontal case, except it
//  loops on a different maximum depth, and it does a different
//  volume calculation, otherwise, it's a repeat of the above
//  function
function WriteVerticalChart(length,radius,measureunits)
{
	var numsteps;
	var cmperstep;
	var volume;
	var step;
	var depth;

	//WriteTableHeader("Vertical",radius,length);


	if (measureunits==1) {
		cmperstep=2.54*increment;
	} else {
		cmperstep=1*increment;
	}
	numsteps=length/cmperstep;
	LinesPerColumn=(numsteps/MaxColumns).toFixed(0);


	//  Lets make this a multi column table that fits neatly on
	//  one page

	write("<center><table border=0 ><td valign=\"top\">");


	WriteChartHeaders();


	depth=cmperstep;
	step=increment;
	while (depth <= length) {
		var volume;
		var val;
		volume=pi*radius*radius*depth;

		WriteOneRow(volume,step);


		if (((step/increment)%LinesPerColumn==0)&&(depth <= length-cmperstep)) {
			write("</table>");
			write("</td><td>&nbsp</td><td valign=\"top\">");
			WriteChartHeaders();
		}
		depth=depth+cmperstep;
		step=step+increment;
	}
	write("</td></table>");
}

function WriteDipChart(length,radius,orientation,measureunits)
{
	//  This is the main dispatcher, decides which type of
	//  tank is selected, and branches accordingly
	if (orientation==1) {
		WriteHorizontalChart(length,radius,measureunits);
	} else {
		WriteVerticalChart(length,radius,measureunits);
	}

	WriteChartFooters();

}



