function Update(){
  p=document.Hat
   setTension()



p.HatLength.value = Math.max(0.4 * eval(p.HeadDiam.value),p.HatLength.value);
p.HatDiam.value = Math.round(eval(p.HeadDiam.value)*(100-eval(p.Reduc.value))/10)/10;

// number of stitches in 3 regions:
//p.StitchGauge.value = p.StitchesMS.value ;

var ratio = eval(p.StitchGaugeSS.value)/eval(p.StitchGaugeRib.value);
if( eval(p.StitchGaugeSS.value)/eval(p.StitchGaugeRib.value) < 0.8 || 
    eval(p.StitchGaugeRib.value)/eval(p.StitchGaugeSS.value) < 0.9) {
    //alert ( "The stitch gauges for the two portions of the hat are incompatible. If you use them, your hat will be ugly.");
    }
//
p.StitchesMS.value= 4 * Math.round( eval(p.HatDiam.value)* eval(p.StitchGaugeSS.value)/4)+2 
p.CastOnStitches.value = 
   Math.min( 4 * Math.round(eval(p.HatDiam.value)* eval(p.StitchGaugeSS.value)/4)+2, 
             eval(p.StitchesMS.value) ) ;
             
//p.incStitch1.value = eval(p.StitchesMS.value)-p.CastOnStitches.value;

//var incRegions=eval(p.incStitch1.value)+1
//p.Space1.value = Math.floor(eval(p.CastOnStitches.value)/incRegions);
//p.Times2.value = eval(p.CastOnStitches.value) - eval(p.Space1.value)*incRegions;
//p.Space1_2.value = p.Space1.value;

//p.Space2.value = eval(p.Space1.value) +1 
//p.Times1.value = eval(p.incStitch1.value) - p.Times2.value ;


// Rows.
p.StripeRow.value = 2 * Math.round(eval(p.DesiredStripeWidth.value)*eval(p.RowGaugeSS.value)/2);
p.StripeRow2.value = p.StripeRow.value;
p.StripeWidth.value = 0.1* Math.round(10* eval(p.StripeRow.value)/eval(p.RowGaugeSS.value));

p.CastOnRows.value = Math.max( 2* Math.round(eval(p.RowGaugeRib.value)/4), 4) ; // 1/2" of ribbing.
var CastOnDepth = eval(p.CastOnRows.value)/eval(p.RowGaugeRib.value);

p.RowsPlain.value = Math.max( 2 * Math.round(eval(p.RowGaugeSS.value)/4), 2) ; // same of plain
var PlainDepth = eval(p.RowsPlain.value)/eval(p.RowGaugeSS.value);

var FoldDepth = 0.15* eval(p.HatDiam.value);  // scaled from my hat.
p.BrimDepth.value = FoldDepth
p.BrimDepth2.value = p.BrimDepth.value
p.RowsMS.value =  2*Math.round((FoldDepth -  CastOnDepth- PlainDepth )*eval(p.RowGaugeSS.value)/2);

p.RowsPlain2.value =p.RowsPlain.value;

p.UnShapedRows.value = 2*Math.round((eval(p.HatLength.value)-PlainDepth)*eval(p.RowGaugeSS.value)/2  ) ;
p.HatLength2.value = p.HatLength.value;
}
//----------------------------------------

function setTension(){ 
  p=document.Hat
  
p.TensionRib.value= eval(p.TensionSS.value)-1
p.TensionRib2.value=eval(p.TensionSS.value)-1
p.TensionMS2.value=eval(p.TensionSS.value);
p.TensionMS3.value=eval(p.TensionSS.value);


}

