function clearForms()
{
  document.forms[0].reset();
  document.forms[1].reset();
}

function onHeadBlur()
{
  document.forms[1].head.value=document.forms[0].head.value;
  onShoulderBlushBlur();
}

function onNeckBlur()
{
  neckHole=document.forms[0].neck.value;
  if (!document.forms[0].biasTapeYes.checked)
    neckHole-=3;
  document.forms[1].neckHole.value=neckHole/2;
  onShoulderBlushBlur();
}

function onBiasTapeYes()
{
  document.forms[0].biasTapeNo.checked=false;
  onNeckBlur();
}

function onBiasTapeNo()
{
  document.forms[0].biasTapeYes.checked=false;
  onNeckBlur();
}

function onChestBlur()
{
  document.forms[1].shortBody.value=Math.max(document.forms[0].chest.value,
	Math.max(document.forms[0].tummy.value,document.forms[0].hips.value))
	/2+3;
}

function onNeckWristBlur()
{
  document.forms[1].sleeveLength.value=
	( document.forms[0].neckWrist.value
	 -((document.forms[1].shortBody.value)/2)
	);
}

function onUpperArmBlur()
{
  document.forms[1].sleeveWidth.value=document.forms[0].upperArm.value*1+3;
}

function onHandBlur()
{
  if (document.forms[0].hand.value=="")
    document.forms[1].cuff.value="";
  else
    document.forms[1].cuff.value=document.forms[0].hand.value*1+1;
}

function onShoulderBlushBlur()
{
  document.forms[1].slitLength.value=
	Math.max(document.forms[0].shoulderBlush.value,
		 (document.forms[1].head.value-document.forms[1].neckHole.value)/2
		);
}

function onShoulderWaistBlur()
{
  document.forms[1].shoulderWaist.value=document.forms[0].shoulderWaist.value;
}

function onWaistHemBlur()
{
  document.forms[1].centerSkirtSlits.value=
	( document.forms[0].waistHem.value*1
	 +document.forms[0].hemFoldover.value*1
	 );
}

function onShoulderHemBlur()
{
  document.forms[1].longBody.value=
	2*( document.forms[0].shoulderHem.value*1
	   +document.forms[0].hemFoldover.value*1
	   );
}

function onHemFoldoverBlur()
{
  onWaistHemBlur();
  onShoulderHemBlur();
}

function writeTableLine(doc,tag,value)
{
  doc.write("<tr><td>"+tag+":</td><td>"+value+"</td></tr>");
}

function recalc()
{
  onHeadBlur();
  onNeckBlur();
  onChestBlur();
  onChestBlur();
  onChestBlur();
  onNeckWristBlur();
  onUpperArmBlur();
  onHandBlur();
  onShoulderBlushBlur();
  onShoulderWaistBlur();
  onWaistHemBlur();
  onShoulderHemBlur();
  onHemFoldoverBlur();
}

function formatForPrinting()
{
  recalc();
  
  printingWindow=window.open("about:blank","printingWindow");
  title="Measurements";
  if (document.forms[0].personName.value!="")
    title+=" for "+document.forms[0].personName.value;
  doc=printingWindow.document;
  doc.open();
  doc.write("<html><head><title>"+title+"</title></head>");
  doc.write("<body>");
  doc.write("<h1>"+title+"</h1>");
  doc.write("<table>");
  doc.write("<tr><td colspan=2 align=center><b>Raw measurements</b></td></tr>");

  writeTableLine(doc,"Head",document.forms[0].head.value);
  writeTableLine(doc,"Neck",document.forms[0].neck.value);
  writeTableLine(doc,"Chest",document.forms[0].chest.value);
  writeTableLine(doc,"Tummy",document.forms[0].tummy.value);
  writeTableLine(doc,"Hips",document.forms[0].hips.value);
  writeTableLine(doc,"Back of neck to wrist",document.forms[0].neckWrist.value);
  writeTableLine(doc,"Upper arm circumference",document.forms[0].upperArm.value);

  hand=document.forms[0].hand.value;
  if (hand=="")
    hand="(not present; must not be a tapered sleeve)";
  writeTableLine(doc,"Hand circumference",hand);

  writeTableLine(doc,"Shoulder to level where you want the slit to end",document.forms[0].shoulderBlush.value);
  writeTableLine(doc,"Shoulder to waist",document.forms[0].shoulderWaist.value);
  writeTableLine(doc,"Waist to hem",document.forms[0].waistHem.value);
  writeTableLine(doc,"Shoulder to hem",document.forms[0].shoulderHem.value);

  doc.write("<tr><td colspan=2 align=center><b>Tunic choices</b></td></tr>");

  writeTableLine(doc,"Bias tape on neck?",
	((document.forms[0].biasTapeYes.checked) ? "Yes" : "No"));
  writeTableLine(doc,"Hem foldover",document.forms[0].head.value);

  doc.write("<tr><td colspan=2 align=center><b>Tunic measurements</b></td></tr>");

  writeTableLine(doc,"Head",document.forms[0].head.value);

  writeTableLine(doc,"Head",document.forms[1].head.value);
  writeTableLine(doc,"Neck hole",document.forms[1].neckHole.value);
  writeTableLine(doc,"Short dimension of body piece",document.forms[1].shortBody.value);
  writeTableLine(doc,"Sleeve length",document.forms[1].sleeveLength.value);
  writeTableLine(doc,"Sleeve width",document.forms[1].sleeveWidth.value);

  cuff=document.forms[1].cuff.value;
  if (cuff=="")
    cuff="(not present; must not be a tapered sleeve)";
  writeTableLine(doc,"Cuff",cuff);

  writeTableLine(doc,"Slit length",document.forms[1].slitLength.value);
  writeTableLine(doc,"Shoulder to waist",document.forms[1].shoulderWaist.value);
  writeTableLine(doc,"Center skirt slits",document.forms[1].centerSkirtSlits.value);
  writeTableLine(doc,"Long dimension of body piece",document.forms[1].longBody.value);
  doc.write("</table>");
  doc.write("</body></html>");
  doc.close();
}
