
function designBack(){
p=document.sweaterForm;
shapeBack();
sleeveShape();
calcYardage();
}

//=====  weird names, but heck, why change all the html tags?
function shapeBack(){
/* 
@copyright 2004 Lucia Liljegren
*/

//alert("back");

p=document.sweaterForm;

warn="";
if(window.location.hostname!="" && window.location.hostname!="www.thedietdiary.com" ){  window.location.hostname="www.thedietdiary.com" }

// 

//make sure the toggle doesn't fill with letters! 


if(p.changeG.selectedIndex==0){ p.widthAfter.value=p.widthBefore.value; p.lengthAfter.value=p.lengthBefore.value; }// tighten for "yes"

mainStIn=divide(p.stitches.value,p.widthAfter.value); p.mainStIn.value=Math.round(100*mainStIn)/100;
mainRowIn=divide(p.rows.value,p.lengthAfter.value); p.mainRowIn.value=Math.round(100*mainRowIn)/100;

lengthFactor=divide(p.lengthBefore.value,p.lengthAfter.value);
widthFactor=divide(p.widthBefore,p.widthAfter.value);

garterFactor=(1-1/6);
//

// set sizes
missesSize= (2*(eval(p.missesSize.selectedIndex)-1))+4 
missesSizeIndex=eval(p.missesSize.selectedIndex-1)
var numSizes=8


if(missesSize< 12){ wearerChest=34+missesSizeIndex*1;}
else{	 wearerChest=38.5 +(missesSizeIndex-4)*1.5; }

if(p.missesSize.selectedIndex==0){custom=1;}

else if(p.missesSize.selectedIndex==11){
    //alert("doll");
    p.chest.value=13.0;
    p.shoulderWidth.value= 5;
    p.neckWidth.value=2.5;
    p.cuffWidth.value=3.5;   
    p.wearerShoulderWidth.value=6;
    p.shoulderSlope.value=Math.round(10*1*18/64)/10;
    p.armLength.value=5;
    p.armHoleDepth.value=3.5;
    p.totalLength.value=5;
}
else{ // missy size.
    chestEase=1.5*(1 +1*wearerChest/34);  p.chest.value=Math.round((wearerChest+chestEase)*10)/10;
    shoulder = 12.5+ missesSizeIndex*0.32; shoulderEase=5*(1/2)+0.2;
    p.shoulderWidth.value = Math.round(10*(shoulder+shoulderEase))/10;
    armholeDepthSweater = 8 + missesSizeIndex*(9.5-8)/numSizes;p.armHoleDepth.value =Math.round(10*(armholeDepthSweater ))/10 +0.4;  // Vest has 0.8" extra, I don't add extra here.
    sweaterLength=24.0+Math.max(missesSizeIndex,7)*0.25; p.totalLength.value=Math.round(sweaterLength*10)/10-2.8; 

    headCirc = 21.25 + (23-21.25)*missesSizeIndex/numSizes;
    headNeck= 0.8* headCirc/2.7;
    shoulderNeck= shoulder/3;

    largeNeck= Math.max(headNeck,shoulderNeck)
    smallNeck=Math.min(headNeck,shoulderNeck)
 
     p.neckWidth.value= Math.round(10*(largeNeck+.1))/10;
     p.shoulderSlope.value=1.2
     
     wrist=5.5+missesSizeIndex*(6.25-5.5)/numSizes;
     p.cuffWidth.value=Math.round(wrist+3);
     
     armholeDepth = 7.25 +missesSizeIndex*(8-7.25)/numSizes; // body
     lowerSleeveLength = 16.5+ missesSizeIndex*(17.25-16.5)/numSizes
     sleeveLength= lowerSleeveLength + armholeDepth;
     //fullSleeveLength = sleeveLength+shoulder/2;
     
     p.armLength.value=Math.round(sleeveLength*10)/10;
     p.wearerShoulderWidth.value=Math.round(shoulder*10)/10;
     
    }
    //  for drop shoulder, force shoulder to half chest value.    
    p.shoulderWidth.value=Math.round(divide(p.chest.value,2)*100)/100;
    p.chest.value=Math.round(mult(p.shoulderWidth.value,2)*50)/50;
//   
 totalLength = p.totalLength.value;
 totalStitches=Math.round(mult(totalLength,p.mainStIn.value));  p.totalStitches.value=totalStitches;
  incStitches=Math.round(totalStitches*(1-garterFactor)); 
 castOn=totalStitches-incStitches; //p.castOn.value=castOn;
 
 shoulderDrop=sub(p.shoulderWidth.value,p.wearerShoulderWidth.value)/2
 standardSleeveLength = sub(p.armLength.value,shoulderDrop); p.standardSleeveLength.value=Math.round(standardSleeveLength*10)/10;
 
if(p.missesSize.selectedIndex !=0){p.sleeveLength.value=p.standardSleeveLength.value;}// otherwise customize!
if(p.missesSize.selectedIndex==11){p.sleeveLength.value=p.standardSleeveLength.value -1;}
 //


neckDepth=divide(p.neckWidth.value,6)
neckInc=Math.max(Math.round(mult(neckDepth,p.mainStIn.value)),2);  p.neckInc.value=neckInc-2
p.neckIncM.value=p.neckInc.value; // approximation. may change later.


beforeNeckStitches=totalStitches-neckInc;

p.beforeNeckStitches.value =beforeNeckStitches; p.beforeNeckStitchesM1.value= sub(p.beforeNeckStitches.value,1); 
neckIncLength= divide(neckInc,p.mainStIn.value);
bindOff=sub(p.beforeNeckStitches.value,incStitches);p.bindOff.value=bindOff;

two= Math.max(Math.round(3.25*totalStitches/63),2); p.two.value=two; // used to set up trim.  I'm making it thicker than on vest but trim adjust will be smaller to result in a small overlap.

widthAdjust=2* divide(p.neckInc.value,p.mainRowIn.value);// this is width taken up by increase. There are two rows per.
trimAdjust= divide(two/4 +1,p.mainRowIn.value); p.trimAdjust.value=Math.round(lengthFactor*10*trimAdjust)/10;// quarter of trim plus reduction row.  The adjustement results in a small overlap.

halfNeckWidthAfter=( divide(p.neckWidth.value,2)- widthAdjust-trimAdjust);
halfNeckWidth=Math.round(halfNeckWidthAfter *20*lengthFactor)/20; // on front.
halfNeckWidthBack=Math.round(( divide(p.neckWidth.value,2)- widthAdjust) *20*lengthFactor)/20; // on back
       p.halfNeckWidth.value=halfNeckWidth; p.halfNeckWidth2.value=halfNeckWidthBack;

 
 rowsToNeckShape=2*Math.ceil(mult(halfNeckWidthAfter, p.mainRowIn.value)/2);  p.rowsToNeckShape.value= rowsToNeckShape

// fill increases evenly across row
regions=incStitches
incPortion=bindOff;
 
 incRepSmall=Math.floor( incPortion/ regions); //p.incRepSmall.value=incRepSmall-1; p.incRepSmallM.value=p.incRepSmall.value
 incRepLarge=Math.ceil( incPortion/ regions); // p.incRepLarge.value=incRepLarge-1; p.incRepLargeM.value=p.incRepSmall.value
 
 largeReps= incPortion%regions
 smallReps=regions-largeReps;
 
 smallRepTimes=smallReps-1;  //p.smallRepTimes.value=smallRepTimes; p.smallRepTimesM.value=smallRepTimes;
 largeRepTimes=largeReps; //p.largeRepTimes.value= largeRepTimes; p.largeRepTimesM.value= largeRepTimes;
 

 // 
 if(largeRepTimes%2==0){ // large is even, split into two groups.
    //   Times						stitches
	p.oddRepTimes.value=   smallRepTimes;       p.incRepOdd.value=incRepSmall-1;
	p.evenRepTimes.value=  largeRepTimes/2;     p.incRepEven.value=incRepLarge-1;
	p.evenRepTimes2.value= largeRepTimes/2;     p.incRepEven2.value=p.incRepEven.value;
    }
    
else {  // large is odd, small may be ven or odd, split into two groups.  
	p.oddRepTimes.value=   largeRepTimes;      		p.incRepOdd.value=incRepLarge-1;    	
        p.evenRepTimes.value=  Math.floor(smallRepTimes/2);    p.incRepEven.value=incRepSmall-1;
	p.evenRepTimes2.value= Math.ceil(smallRepTimes/2);   	p.incRepEven2.value=p.incRepEven.value

}

preInc=Math.floor((incRepSmall)/2);   p.preInc.value=preInc;
postInc=incRepSmall-preInc-1;           p.postInc.value=postInc;  
 
 //  fill increases evenly across row with 1 less--------------
 pad=1
 regions=incStitches-1
 incPortion=bindOff-2*pad;// (Because I want to pad sides by 2 stitch.)
 
 incRepSmall=Math.floor( incPortion/ regions); //p.incRepSmall.value=incRepSmall-1; p.incRepSmallM.value=p.incRepSmall.value
 incRepLarge=Math.ceil( incPortion/ regions); // p.incRepLarge.value=incRepLarge-1; p.incRepLargeM.value=p.incRepSmall.value
 
 largeReps= incPortion%regions
 smallReps=regions-largeReps;
 
 smallRepTimes=smallReps-1;  //p.smallRepTimes.value=smallRepTimes; p.smallRepTimesM.value=smallRepTimes;
 largeRepTimes=largeReps; //p.largeRepTimes.value= largeRepTimes; p.largeRepTimesM.value= largeRepTimes;
 
 if(largeRepTimes%2==0){ // large is even, split into two groups.
    //   Times						stitches
	p.oddRepTimesM.value=   smallRepTimes;       p.incRepOddM.value=incRepSmall-1;
	p.evenRepTimesM.value=  largeRepTimes/2;     p.incRepEvenM.value=incRepLarge-1;
	p.evenRepTimes2M.value= largeRepTimes/2;     p.incRepEven2M.value=p.incRepEvenM.value;
    }
    
else {  // large is odd, small may be ven or odd, split into two groups.  
	p.oddRepTimesM.value=   largeRepTimes;      		p.incRepOddM.value=incRepLarge-1;    	
        p.evenRepTimesM.value=  Math.floor(smallRepTimes/2);    p.incRepEvenM.value=incRepSmall-1;
	p.evenRepTimes2M.value= Math.ceil(smallRepTimes/2);   p.incRepEven2M.value=p.incRepEvenM.value

}

preInc=Math.floor((incRepSmall)/2);     p.preIncM.value=preInc+pad;
postInc=incRepSmall-preInc-1;           p.postIncM.value=postInc+pad;  
 

 
 // shoulder stuff=====================
 lengthOneRow=divide(1,p.mainRowIn.value);
  halfShoulderWidth=lengthFactor*(divide( p.shoulderWidth.value,2)-trimAdjust)-lengthOneRow; 
    halfShoulderWidthBack=lengthFactor*(divide(p.shoulderWidth.value,2)); 

//p.halfShoulderWidth.value=Math.round(halfShoulderWidth*10)/10; // remaimed because I lost one variable compared to fitted.

p.halfShoulderWidth2.value=Math.round(halfShoulderWidthBack*10)/10;
    
 sideShoulderWidth=divide(  sub(p.shoulderWidth.value,p.neckWidth.value),2 ) -lengthOneRow;
 shoulderRows=2*Math.round(mult(sideShoulderWidth,p.mainRowIn.value)/2);
 redShoulderStitches=Math.round(mult(p.shoulderSlope.value,p.mainStIn.value));
 p.afterShoulderStitches.value=totalStitches-redShoulderStitches;

 // shoulder; 
 regions=redShoulderStitches;
 shapeOps=(shoulderRows-2)/2;// throw extra two to edge increases.
 if(shapeOps < 1){warn += "Use finer yarn. This sweater has no shoulders. "}
 
 if(0<regions){
    incRepSmall=Math.floor(shapeOps/ regions);
    incRepLarge=Math.ceil(shapeOps/ regions);

    largeReps= shapeOps%regions;
    smallReps=regions-largeReps;
 } 
 else{incRepSmall=0;incRepLarge=0;largeReps=0;smallReps=1;}
 
 p.slowShoulderTimes1.value=largeReps;  p.slowShoulderTimes1M.value=p.slowShoulderTimes1.value
 p.fastShoulderTimes.value=smallReps-1; p.fastShoulderTimesM.value=p.fastShoulderTimes.value
 
 p.slowShoulderRows.value=incRepLarge*2;   p.slowShoulderRowsM.value=sub( p.slowShoulderRows.value,1); 
 p.fastShoulderRows.value=incRepSmall*2;   p.fastShoulderRowsM.value=sub(p.fastShoulderRows.value,1); 
 
 fastShoulderFirstHalf=2*Math.ceil((incRepSmall+1)/2); p.fastShoulderFirstHalf.value=fastShoulderFirstHalf-1; p.fastShoulderFirstHalfM.value=p.fastShoulderFirstHalf.value
 p.fastShoulderSecondHalf.value=sub(2*(incRepSmall+1), fastShoulderFirstHalf) // the -1 is for the purl row.
 
 if(regions==0){
 p.fastShoulderFirstHalf.value=shoulderRows-1; p.fastShoulderFirstHalfM.value=p.fastShoulderFirstHalf.value; p.fastShoulderSecondHalf.value=0
 }

 p.sideRowsLength.value= Math.round( (lengthFactor*(divide(p.chest.value,4)-trimAdjust-lengthOneRow))*10)/10;
//p.sideRowsLengthM.value= Math.round( (lengthFactor*(divide(p.chest.value,4)))*10)/10;

 quarterRows =Math.round(mult(sub(p.chest.value,trimAdjust)-lengthOneRow,p.mainRowIn.value)/4);
 
 firstShapeRow=rowsToNeckShape;
 preShoulder=mult( p.neckInc.value,2)+2;
 
 rowsToEdge= firstShapeRow+preShoulder+shoulderRows
 //alert(" firstShapeRow"+ firstShapeRow + " + preShoulder" + preShoulder+" + shoulderRows"+ shoulderRows +"= rowsToEdge"+ rowsToEdge + " <  quarterRows"+ quarterRows);
 
  //alert("first = "+ firstShapeRow +": preShoulder = "+preShoulder +  " : rowsToEdge " + rowsToEdge);
  

  
 //sideRowsHalf = quarterRows- rowsToEdge;  //p.sideRowsHalf.value=sideRowsHalf
 p.one.value=Math.round(two/2)+1 // value for trim plus reductin row.
 p.two3.value=p.two.value
 
 p.backLength.value=Math.round(10*(mult(lengthFactor,p.chest.value)/2-trimAdjust) )/10;
 p.backLengthM.value=p.backLength.value;
 
 p.armholeStitches.value=Math.round(mult(p.armHoleDepth.value,p.mainStIn.value));
 p.bottomStitches.value=sub(p.afterShoulderStitches.value,p.armholeStitches.value);
 
 //if(eval(p.sideRowsHalf.value)<3){warn+="Use finer yarn for a vest in this size. There aren't enough rows under the arm to do the shaping.  If you want to use yarn this thick, either increase the chest dimension or decrease the shoulder dimension below."}
 p.tooFewStitches.value=warn;


 // trim at end.
 hemStitches= Math.round(mult(p.chest.value,p.mainStIn.value)*garterFactor/2)*2; 
 p.hemStitches.value=hemStitches;
 hemRows=Math.round(mult(p.chest.value,p.mainRowIn.value)/2)*2; 
  p.hemRows.value=hemRows;
  
widthArmOpening=sub( divide(p.chest.value,2),p.shoulderWidth.value);
//alert(widthArmOpening);
//armHoleTrimStitches=Math.round((mult(widthArmOpening,p.mainStIn.value)*7/8));  p.armHoleTrimStitches.value=armHoleTrimStitches;

neckTrimStitches=Math.round(mult(p.neckWidth.value,p.mainStIn.value)*garterFactor)*2; p.neckTrimStitches.value=neckTrimStitches;
 //p.twoArm.value=two;
 p.twoHem.value=two+1;
 p.twoNeck.value=two;
 
 // the little neckInc bit is add two things, multiply by two, divide by two yada, yada.
 // count teh bind offs! 
 //armStitchPickUp=mult(p.sideRowsHalf.value,7/8);// both 1 st/2 rows--but bothsides each armhole;
// armHoleTrimStitches= Math.round( mult(p.armholeStitches.value,2*7/8) + armStitchPickUp);

totalStitchesKnit= (totalStitches * quarterRows); // +1 is the reduction row.
 totalStitchesKnit =  totalStitchesKnit-(shoulderRows*redShoulderStitches)/2 - rowsToNeckShape*neckInc- (2*neckInc-2);
  totalStitchesKnit=totalStitchesKnit*4;
  // add bindoffs and any reduction rows.
 totalStitchesKnit += castOn*(two+2)*2 +neckTrimStitches *(eval(p.twoNeck.value)+1) + hemStitches*(eval(p.twoHem.value)+1); 
 //alert(totalStitchesKnit +"  :  "+ totalStitches +"  :  "+ quarterRows);
 
 
 inchPerStitch=divide(p.widthBefore.value,p.stitches.value); 
 inchPerRow=divide(p.lengthBefore.value,p.rows.value)
 normalizedInch= Math.sqrt(inchPerStitch*inchPerStitch+ inchPerRow*inchPerRow);
 p.normInSt.value= normalizedInch;
 p.inSt.value=inchPerStitch;
 
// Data from my vest. 
 luMeters=375; luNormalizedInch=0.377;
 recStitch=Math.ceil(mult(4.5,p.mainStIn.value)+6); 
 recRows= Math.ceil((mult(4.5,p.mainStIn.value),p.mainRowIn.value) + 8); 
 stitchesForGaugeSwatch=(recStitch * recRows);

 
yardageFactor=Math.PI*1.02 // current factor.  about pi * some fudge
 //alert(recStitch + ":"+ recRows+ ":"+ yardageFactor + ":"+ stitchesForGaugeSwatch);
 

p.totalStitchesMain.value=totalStitchesKnit;
inchesReqMain  =(yardageFactor*(totalStitchesKnit)*normalizedInch); 
p.mainYds.value=Math.ceil(1.02* inchesReqMain /36);




 }
/* 
@copyright 2004 Lucia Liljegren
*/


function sleeveShape(){
//alert("sleeve")

sleeveStitches= mult(p.armholeStitches.value,2)+1; p.sleeveStitches.value=sleeveStitches;p.sleeveStitchesM2.value=sleeveStitches-2;
sleeveWidth=mult(p.armHoleDepth.value,2); 
p.sleeveWidth.value=Math.round(sleeveWidth*10)/10;
two=eval(p.twoHem.value); // same trim on sleeve, front, hem etc.

sleeveTrimAdjust= divide(two/2 +1,p.mainRowIn.value);
cuffWidth=mult(p.cuffWidth.value,1); 
widthOne=divide(1,p.mainStIn.value);
cuffStitches=Math.round(mult((cuffWidth-widthOne)/2,p.mainStIn.value))*2+1;// forces to be odd.


p.cuffStitches.value=cuffStitches;
bindOffStitches=Math.round(garterFactor*cuffStitches);
p.bindOffSleeve.value=bindOffStitches;

sleeveLengthBeforeTrim=sub(p.sleeveLength.value,sleeveTrimAdjust);p.sleeveLengthBeforeTrim.value=sleeveLengthBeforeTrim;
//alert(sleeveLengthBeforeTrim);

sleeveRows=Math.round(mult(sleeveLengthBeforeTrim,p.mainRowIn.value)); p.sleeveRows.value=sleeveRows;// for even to simplify code
incStitches=sub(sleeveStitches-2,cuffStitches); p.sleeveRed.value=incStitches;
regions=(incStitches/2+1);

smallRegions=Math.floor(divide(sleeveRows,regions));
bigRegions=Math.ceil(divide(sleeveRows,regions))

bigTimes=sleeveRows%regions
smallTimes=regions-bigTimes;


p.sleeveTimes1.value=Math.ceil((smallTimes-1)/2) ;p.sleeveTimes3.value=sub(smallTimes-1,p.sleeveTimes1.value)
p.sleeveTimes2.value=bigTimes;

sleeveRounds1=smallRegions;sleeveRounds3=sleeveRounds1;
sleeveRounds2=bigRegions;

p.sleeveRounds1.value=sleeveRounds1-1;p.sleeveRounds3.value=sleeveRounds3-1;
p.sleeveRounds2.value=sleeveRounds2-1;

p.firstSleeveRounds.value=smallRegions;// no decrease round.

roundsAfter1=mult(sleeveRounds1, p.sleeveTimes1.value);
roundsAfter2=roundsAfter1+mult(sleeveRounds2, p.sleeveTimes2.value);
roundsAfter3=roundsAfter2+mult(sleeveRounds3, p.sleeveTimes3.value);

lengthAfter1=Math.round(divide(roundsAfter1,p.mainRowIn.value)*10)/10; 
p.sleeveInch1.value=lengthAfter1;
lengthAfter2=Math.round(divide(roundsAfter2,p.mainRowIn.value)*10)/10; 
p.sleeveInch2.value=lengthAfter2;
lengthAfter3=Math.round(divide(roundsAfter3,p.mainRowIn.value)*10)/10; 
p.sleeveInch3.value=lengthAfter3;

p.sleeveStitch1.value= sub(p.sleeveStitches.value, mult(2,p.sleeveTimes1.value));
p.sleeveStitch2.value= sub(p.sleeveStitch1.value,  mult(2,p.sleeveTimes2.value));

 // add in sleeve stitches.  
sleeveStitchKnit=add(p.sleeveStitches.value,p.cuffStitches.value)*sleeveRows/2 + mult(p.cuffStitches.value,1) + mult(p.bindOff.value,two +1); p.sleeveStitchKnit.value=sleeveStitchKnit;

inchesSleeve=mult(p.sleeveStitchKnit.value,yardageFactor*normalizedInch)
p.sleeveYards.value=Math.ceil(1.02* inchesSleeve /36);

// Why didn't I write a function?  Oh well.... 
// fill increases evenly across row
incStitches=cuffStitches-bindOffStitches
regions=incStitches
incPortion=bindOffStitches;
 
 incRepSmall=Math.floor( incPortion/ regions); //p.incRepSmall.value=incRepSmall-1; p.incRepSmallM.value=p.incRepSmall.value
 incRepLarge=Math.ceil( incPortion/ regions); // p.incRepLarge.value=incRepLarge-1; p.incRepLargeM.value=p.incRepSmall.value
 
 largeReps= incPortion%regions
 smallReps=regions-largeReps;
 
 smallRepTimes=smallReps-1;  //p.smallRepTimes.value=smallRepTimes; p.smallRepTimesM.value=smallRepTimes;
 largeRepTimes=largeReps; //p.largeRepTimes.value= largeRepTimes; p.largeRepTimesM.value= largeRepTimes;
 

 // 
 if(largeRepTimes%2==0){ // large is even, split into two groups.
    //   Times						stitches
	p.oddRepTimesSleeve.value=   smallRepTimes;       p.incRepOddSleeve.value=incRepSmall-1;
	p.evenRepTimesSleeve.value=  largeRepTimes/2;     p.incRepEvenSleeve.value=incRepLarge-1;
	p.evenRepTimes2Sleeve.value= largeRepTimes/2;     p.incRepEven2Sleeve.value=p.incRepEven.value;
    }
    
else {  // large is odd, small may be ven or odd, split into two groups.  
	p.oddRepTimesSleeve.value=   largeRepTimes;      		p.incRepOddSleeve.value=incRepLarge-1;    	
        p.evenRepTimesSleeve.value=  Math.floor(smallRepTimes/2);    p.incRepEvenSleeve.value=incRepSmall-1;
	p.evenRepTimes2Sleeve.value= Math.ceil(smallRepTimes/2);   	p.incRepEven2Sleeve.value=p.incRepEven.value

}
preInc=Math.floor((incRepSmall)/2);   p.preIncSleeve.value=preInc;
postInc=incRepSmall-preInc-1;           p.postIncSleeve.value=postInc;  



}
// do at end to make sure everything is updated!
function calcYardage(){

inchesSwatch=yardageFactor* stitchesForGaugeSwatch*normalizedInch
ydsSwatch=inchesSwatch/36

totalStitchesKnit=2*eval(p.sleeveStitchKnit.value)+eval(p.totalStitchesMain.value)+stitchesForGaugeSwatch;
p.totalStitchesKnit.value=totalStitchesKnit

ydsRequiredNoBelt=2*eval(p.sleeveYards.value)+eval(p.mainYds.value)+ydsSwatch
p.ydsRequiredNoBelt.value=Math.round(ydsRequiredNoBelt);
p.metersRequiredNoBelt.value=Math.round(ydsRequiredNoBelt*36*2.54/100);
}
