// Main subroutine
document.writeln('<h2>Calculator</h2>');
if (AREqnMin > 0) {
	if (AREqnMax == -1) {
		document.writeln('<p>Note: This calculator is designed for rings with ARs greater than ' + AREqnMin.toFixed(2) + '.</p>');		
	} else {
		document.writeln('<p>Note: This calculator is designed for rings with ARs between ' + AREqnMin.toFixed(2) + ' and ' + AREqnMax.toFixed(2) + '.</p>');		
	}
}
document.writeln('<form name="myForm" method="post" action="' + document.URL + '">');
document.writeln('<fieldset>');
document.writeln('<legend><strong>Ring Quantity Calculator</strong></legend>');

InputSize('wd','Wire diameter');
InputSize('id','Ring I.D.');
InputSize('l','Length');

document.writeln('<p><input type="button" name="calc" value="Calculate" onclick="CalcRingQty()" /> <input type="button" name="ResetCalc" value="Reset" onclick="Reset()" /></p>');
document.writeln('<p><label for="answer"></label><textarea name="answer" id="answer" cols="40" rows="5"></textarea></p>');
	
document.writeln('</fieldset>');
document.writeln('</form>');

// Helper functions
function InputSize(ID, Description)
{
	document.writeln('<p><label for "' + ID + '">' + Description + '</label>: <input type="text" name="' + ID + '" id="' + ID + '" />');
	document.writeln('<label for "' + ID + 'unit"></label><select size="1" name="' + ID + 'unit" id="' + ID + 'unit">');
	document.writeln('<option>inch</option>');
	document.writeln('<option>mm</option>');
	document.writeln('</select>');
	document.writeln('</p>');
}

function ConvertToInternalUnit(size, unit)
{
//	alert('ConvertToInternalUnit.size = ' + size);
//	alert('ConvertToInternalUnit.unit = ' + unit);
	switch (unit) {
		case 'inch': { return size; }
		case 'mm':   { return size / 25.4; }
	}
}

// Button functions
function CalcRingQty()
{
	// WeaveName, rpu, RoundingMultiple, NumOfConnectors and a0 thru a5 are defined by the calling page.
	var wd = ConvertToInternalUnit(document.myForm.wd.value,document.myForm.wdunit[document.myForm.wdunit.selectedIndex].text);
	var id = ConvertToInternalUnit(document.myForm.id.value,document.myForm.idunit[document.myForm.idunit.selectedIndex].text);
	var l  = ConvertToInternalUnit(document.myForm.l.value,document.myForm.lunit[document.myForm.lunit.selectedIndex].text);
	var ar = id / wd;
	// ipnu = inches / normalized unit
	var ipnu = (a5*Math.pow(ar,5) + a4*Math.pow(ar,4) + a3*Math.pow(ar,3) + a2*ar*ar + a1*ar + a0);
	// rpl = rings / length
	var rpl = l * rpu / (ipnu * wd);
//	alert(wd);
//	alert(id);
//	alert(l);
//	alert(ar);
//	alert(ipnu);
//	alert(rpl);	
	
	// Perform rounding
	rpl = rpl - NumOfConnectors;
        rpl = RoundingMultiple * Math.round( rpl / RoundingMultiple );
	rpl = rpl + NumOfConnectors;
	
  	var txt = "A " + WeaveName + " chain made from\n";
  	txt = txt + document.myForm.wd.value + ' ' + document.myForm.wdunit.value + " x ";
  	txt = txt + document.myForm.id.value + ' ' + document.myForm.idunit.value + ' I.D. rings and\nmeasuring ';
  	txt = txt + document.myForm.l.value + ' ' + document.myForm.lunit.value+ ' long ';
   	txt = txt + 'would require\n' + rpl + ' rings.';
   	if (isNaN(rpl)) {
   		document.myForm.answer.value = 'Invalid data - unable to calculate.'; 
   		} else {
   		document.myForm.answer.value = txt;
   	}
}

function Reset()
{
	document.myForm.wd.value = "";
	document.myForm.id.value = "";
	document.myForm.l.value = "";
	document.myForm.answer.value = "";
}
