function Update(){
  p=document.Sock
   setTension()
   
// Adjust column
p.LeftEdgePlus1.value = eval(p.LeftEdge.value)+1
p.LeftEdgePlus2.value = eval(p.LeftEdge.value)+2
p.LeftEdgePlus3.value = eval(p.LeftEdge.value)+3
p.LeftEdgePlus4.value = eval(p.LeftEdge.value)+4

p.RightEdgePlus1.value = eval(p.RightEdge.value)+1
p.RightEdgePlus2.value = eval(p.RightEdge.value)+2
p.RightEdgePlus3.value = eval(p.RightEdge.value)+3
p.RightEdgePlus4.value = eval(p.RightEdge.value)+4

// Find design lengths:
p.FootLength.value = 0.85* p.RealFootLength.value
p.AnkleCircumf.value = 0.90 * p.RealAnkleCircumf.value


p.SockLength2.value = p.SockLength.value
// p.Reduc.value = Math.min( 100 * (1 - (eval(p.AnkleCircumf.value)/eval(p.CalfCircumf.value))), eval(p.Reduc.value) )
p.HalfCalfCircumf.value = Math.max(eval(p.CalfCircumf.value) * (1- eval(p.Reduc.value)/100),eval(p.AnkleCircumf.value))

p.CalfNeedles.value = 
  2 * Math.round(p.HalfCalfCircumf.value * eval(p.StitchGauge.value) /2) +1
p.CalfNeedlesMinus1.value = eval(p.CalfNeedles.value) -1
p.CalfNeedlesHalf1.value = eval(p.CalfNeedlesMinus1.value)/2

p.RibbingLength.value = Math.min(eval(p.SockLength.value) - eval(p.AnkleHeight.value),eval(p.RibbingLength.value))
p.WeltRC.value = 2 * Math.round(1.12* eval(p.RibbingLength.value) * eval(p.RowGauge.value) /4) + 2


// need to force the stitch decrease to a correct multiple.
p.FrontFootStitches.value = Math.floor(eval(p.AnkleCircumf.value)*eval(p.StitchGauge.value) /2)
p.StitDecTot.value =  4 * Math.round((eval(p.CalfNeedlesHalf1.value) - eval(p.FrontFootStitches.value))/2)
p.FrontFootStitches.value = eval(p.CalfNeedlesHalf1.value) - p.StitDecTot.value/2
p.FootStitches.value = 2* eval(p.FrontFootStitches.value)

HeelShaping()

// Calculate decrease region

var AdjAnkleLength = eval(p.AnkleHeight.value) - 2*eval(p.HeelRows.value)/eval(p.StitchGauge.value)

 MagicDec() 
   
  p.ToeStitch.value = Math.round(4*eval(p.FrontFootStitches.value)/9)
  var DecToe = 4 * Math.round((eval(p.FrontFootStitches.value) - eval(p.ToeStitch.value))/2 )
  p.ToeStitch.value = eval(p.FrontFootStitches.value) - DecToe/2
 p.ToeRows.value = DecToe
  p.ToeRows2.value = 2*DecToe
  
  // foot  adjust for extra length in heel and toe.
  p.FootRows.value = 2 * Math.round((eval(p.FootLength.value) * eval(p.RowGauge.value))/2) - 
  2 * p.HeelRows.value - p.ToeRows.value 
  
  p.FootLength2.value = p.FootLength.value
 
 //
 fillChild();
}
//----------------------------------------

/*  */
function setTension(){
  p.Tension2.value=p.Tension.value
  p.RibTension.value = Math.max(eval(p.Tension.value) - 3,1)
  p.RibTension_2.value = Math.max(eval(p.Tension.value) - 3,1)
  p.TensionCircular.value =  Math.max(eval(p.Tension.value) - 4,1)
  p.TensionCircular_2.value =  Math.max(eval(p.Tension.value) - 4,1)
  p.TensionZigZag_2.value =  Math.max(eval(p.Tension.value) - 4,1)
  p.TensionZigZag.value =  Math.max(eval(p.Tension.value) - 4,1)

}
function HeelShaping(){
// Eileen suggested having 4/9 of the stitches in the heel. 
// many patterns have 1/2.  EZ suggests 2/3.  I'm going for 1/2 and rounding up
//
var DecHeel = 2 * Math.round(eval(p.FrontFootStitches.value)/4 + 0.5 )
 p.HeelStitch.value = eval(p.FrontFootStitches.value) - DecHeel
 p.HeelShortPasses.value =  (DecHeel-2)/2
 p.HeelRows.value = DecHeel
 
p.HeelRows2.value =  2 * eval(p.HeelRows.value)
 p.HeelShortPasses2.value= p.HeelShortPasses.value

}
function MagicDec(){
  p.HeelRows3.value =p.HeelRows.value
  p.LengthHeel.value = 2 * eval(p.HeelRows3.value)/eval(p.RowGauge.value)
 var LengthMain = 
   eval(p.SockLength.value) - Math.max(eval(p.AnkleHeight.value), eval(p.LengthHeel.value)) - eval(p.RibbingLength.value)  
 p.RowDecTot.value = 2 * Math.round(LengthMain * eval(p.RowGauge.value)/2)
 p.RowDecTot2.value =p.RowDecTot.value 
  


 // This is the first heel row. The sock length goes until the end of the heel turn.
p.HeelRC.value = 
2 * Math.round( (eval(p.SockLength.value)-eval(p.RibbingLength.value)) * eval(p.RowGauge.value)/2 ) -  2 * p.HeelRows.value

// Leg decreases

var NumDecOper = eval(p.StitDecTot.value)/4 // This is the number of times I decrease
var NumDecRegions = NumDecOper + 1

// I want to decrease slowly.
var two = 2;
var halfRowDec = eval(p.RowDecTot.value)/2

p.LegRow3.value = Math.floor( halfRowDec/NumDecRegions)

var Rem = halfRowDec - eval(p.LegRow3.value) * NumDecRegions

if (Rem == 0) { // no longer intervals.
    p.LegRow2.value = 0
     p.LegRow1.value = eval(p.LegRow3.value)
   p.LegTimes2.value = 0

}

else {
  p.LegRow3.value = p.LegRow3.value // because of rounding?
  
  p.LegRow2.value = eval(p.LegRow3.value) +1
  p.LegRow1.value = eval(p.LegRow3.value) +1 
  p.LegTimes2.value = Rem-1
 
}
 p.LegTimes3.value = NumDecOper - eval(p.LegTimes2.value)
 // normalize
 p.LegRow3.value = two * eval( p.LegRow3.value)
 p.LegRow2.value = two * eval( p.LegRow2.value)
 p.LegRow1.value = two * eval( p.LegRow1.value)
 p.IntRow.value = eval(p.LegRow1.value)+ eval(p.LegRow2.value)*eval(p.LegTimes2.value)
 
}