function calcPoncho(){
/* 
@copyright 2004 Lucia Liljegren
*/


//cookie shuffling.
// set a cookie with a cookie name equal to the document  location.
p =document.ponchoForm;
pester = document.location;

expireDate = new Date;
expireDate.setMonth(expireDate.getMonth()+6);
username = pester;

document.cookie="pester=" + username + "expires="+ expireDate.toGMTString(); 

if(document.cookie ==""){
    alert("Please set your browser to accept cookies. You must accept cookies to run this page.");
}
else {
    pesterZero=document.cookie.split("/")[0];  //figure out if local or away.    
    pesterRead = document.cookie.split("/")[4];
    pesterAway=document.cookie.split("/")[2];    
}
pester = "yes";
    if(pesterZero == "pester=file" || pesterRead =="lucia"){pester="no"}
else if(pesterZero == "pester=http:" || pesterAway == "www.thedietdiary.com"){pester="no"}
 
else{ pester = "yes"; alert(" Please run my programs from my site at www.thedietdiary.com!"  )} ;

if(pester == "no"){
// 

/* 
@copyright 2004 Lucia Liljegren
*/


//make sure the toggle doesn't fill with letters! 
//Should be redundant: coding of HTML page should also give text box and toggles different names.


// fill flare angle and shoulder dart information-- later from form.
// from arm/sleeve how to pick select values! type =p2.sleeve_cap.options[p2.sleeve_cap.selectedIndex].value

var flareType ="slim";   
shoulderDarts="yes";

  if(eval(p.mainStIn.value) < 0.00001) {p.mainStIn.value = 0.001; }
  if(eval(p.mainRowIn.value) < 0.00001) {p.mainRowIn.value = 0.001; }

// the following aren't working under Jscript!!
//flareType = p.ponchoFlare.options[p.ponchoFlare.selectedIndex].value;
//shoulderDarts = p.darts.options[p.darts.selectedIndex].value;

if(p.ponchoFlare.selectedIndex == 0){ flareType="fullEZest"; EZ="yes2";    flareGroup = "full";}
if(p.ponchoFlare.selectedIndex == 1){ flareType="fullEZ";    EZ="yes";     flareGroup = "full";}
if(p.ponchoFlare.selectedIndex == 2){ flareType="full";      EZ="no";      flareGroup = "full";}

if(p.ponchoFlare.selectedIndex == 3){ flareType="wideEZest"; EZ="yes2";     flareGroup = "wide";}
if(p.ponchoFlare.selectedIndex == 4){ flareType="wideEZ";    EZ="yes";     flareGroup = "wide";}
if(p.ponchoFlare.selectedIndex == 5){ flareType="wide";      EZ="no";      flareGroup = "wide";}

if(p.ponchoFlare.selectedIndex == 6){ flareType="mediumEZest"; EZ="yes2";    flareGroup = "medium"}
if(p.ponchoFlare.selectedIndex == 7){ flareType="mediumEZ";    EZ="yes";    flareGroup = "medium"}
if(p.ponchoFlare.selectedIndex == 8){ flareType="medium";      EZ="no";     flareGroup = "medium";}

if(p.ponchoFlare.selectedIndex == 9){ flareType="slimEZest";  EZ="yes2";    flareGroup = "slim";}
if(p.ponchoFlare.selectedIndex == 10){ flareType="slimEZ";    EZ="yes";    flareGroup = "slim";}
if(p.ponchoFlare.selectedIndex == 11){ flareType="slim";      EZ="no";     flareGroup = "slim";}


if(p.darts.selectedIndex == 0){ shoulderDarts="yes"; }
if(p.darts.selectedIndex == 1){ shoulderDarts="no"; }


point="down";  // default

var numPieces;

if( flareGroup =="medium" ){
    targetAlpha= 45* Math.PI/180;  //45 deg.
//    shoulderDarts="yes"; // optional
    numPieces = 2;
    point="down"; 
    
}
else if(flareGroup =="slim" ) {
    targetAlpha = 30 * Math.PI/180; 
    shoulderDarts="yes"; // NEED these.
     numPieces = 2;
     point="down"; 
}

else if(flareGroup =="wide"  ) {
    targetAlpha = 30 * Math.PI/180; 
    numPieces = 4;
    point="side"; // if there are darts, worn to the side.
    // don't overide darts!
}

else if(flareGroup =="full" ) {
    targetAlpha = 42 * Math.PI/180; 
    shoulderDarts="no"; // can't have.
    numPieces = 4;
    point="side";  // if there are darts, worn to the side.
}
p.numPieces.value = numPieces;
p.numPieces2.value = p.numPieces.value;


//=========================


if (shoulderDarts == "yes"){	
    if( (flareType == "wide") || (flareType == "full") ||(flareType == "wideEZ") || (flareType == "fullEZ") ){
    p.skipShoulderDarts.value ="Knit 2 pieces with darts and 2 pieces without darts."}
    else {p.skipShoulderDarts.value ="These are REQUIRED for your design."}
}
    
else{  	p.skipShoulderDarts.value ="Skip: Darts are NOT required for your design"}




// first guess for increase pattern over 6 rows.
 baseIncRows = 24;
baseIncStitchExact= 1/(Math.tan(targetAlpha))  * baseIncRows*eval(p.mainStIn.value)/eval(p.mainRowIn.value)  ;
baseIncStitches =   Math.round(baseIncStitchExact)  ; // better to increase slowly

baseIncStitches =(Math.max(1,baseIncStitches));


// find best baseIncRows.

dif6= baseIncStitchExact - 4*Math.round(baseIncStitchExact/4); // difference if I do every 6
dif4= baseIncStitchExact - 6*Math.round(baseIncStitchExact/6);  // difference if I do every 4.

if(Math.abs(dif6) < Math.abs(dif4)){ baseIncRows= 6; }
else{ 				     baseIncRows= 4; }

if(EZ == "yes"){baseIncRows =4;}
if(EZ == "yes2"){baseIncRows =2;}
//alert( " flare " +flareType + " : baseRows " + baseIncRows +" : " )

if(baseIncRows== 6){       p.skip.value ="Repeat procedure rows 3 & 4"; p.skip2.value="Repeat procedure rows 1 & 2";}
else{ 			   p.skip.value ="Skip these. "; p.skip2.value ="Skip these."}

baseIncStitches = Math.round((1/Math.tan(targetAlpha)) * baseIncRows*eval(p.mainStIn.value)/eval(p.mainRowIn.value));// want to round down
baseIncStitches =(Math.max(1,baseIncStitches));

// now that found, multiply by 2 if baseIncRows= 2

if(baseIncRows==2){baseIncRows =4; baseIncStitches=2*baseIncStitches;}

tanAlpha = 1/(divide(baseIncStitches*eval(p.mainRowIn.value), baseIncRows*eval(p.mainStIn.value)) );
alpha = Math.atan(tanAlpha);
alphaDeg = alpha * 180/Math.PI;
p.sideFlareAngle.value = (numPieces/2)*alphaDeg; 

//alert( 'diagonal: '+ baseIncStitches + '  stitches : ' + baseIncRows + ' rows :   ' + alphaDeg +'deg');
//=============

 // calculate shoulder darts:
// Make this simpler than center shaping... best over 4 rows.  
// Beta is shoulder angle when worn. Gamma is shoulder angle when knit.
bestBetaDeg = eval(p.bestBetaDeg.value);

bestGammaDeg = 90-(numPieces/2)*alphaDeg-bestBetaDeg;  
bestGamma= bestGammaDeg * Math.PI/180;

baseIncRowsDart= 4;
baseIncStitchesDart = Math.round((1/Math.tan(bestGamma))* baseIncRowsDart*eval(p.mainStIn.value)/eval(p.mainRowIn.value));
    // second shaping has gamma/3. I tried  but too point at finish.
baseSecIncRowsDart = 2;
baseSecIncStitchesDart =Math.round((1/Math.tan(bestGamma/3))* baseSecIncRowsDart*eval(p.mainStIn.value)/eval(p.mainRowIn.value));

tanGamma=1/divide(baseIncStitchesDart*eval(p.mainRowIn.value), baseIncRowsDart*eval(p.mainStIn.value)) ;
gamma = Math.atan(tanGamma);
gammaDeg = gamma * 180/Math.PI;


if(shoulderDarts=="no"){gamma = 0; gammaDeg = 0;baseIncStitchesDart=0; baseSecIncStitchesDart=0;}
//alert('dart :'+ baseIncStitchesDart + '  stitches : ' + baseIncRowsDart + ' rows :   '  + gammaDeg +'deg');
        
// reference: slant if shoulder dart is alpha
beta = Math.PI/2 - (numPieces/2)*alpha - gamma;
betaDeg = beta * 180/Math.PI;

p.betaDeg.value = justRound(betaDeg);
p.alphaDeg.value=justRound(alphaDeg);
p.gammaDeg.value = justRound(gammaDeg);


//=============Start finding stitches/rows  per repeat.

p.baseIncStitches.value =baseIncStitches;
    p.shortIncPerRep.value=p.baseIncStitches.value;
    p.shortIncPerRep2.value=p.baseIncStitches.value;

p.baseIncRows.value = baseIncRows;
    p.baseIncRowsRep.value=p.baseIncRows.value;
    p.baseIncRowsRep2.value=p.baseIncRows.value;

baseIncTimes = baseIncRows/2; // this is times increased per increase repeat 2 or 3
minStitch = Math.floor(baseIncStitches/baseIncTimes);
maxStitch = minStitch+1;

maxTimes = baseIncStitches%baseIncTimes;
if(maxTimes ==0)(maxStitch=minStitch);

p.preShort.value = maxStitch;
p.preShort2.value = p.preShort.value
p.postShort.value = p.preShort.value;

  
  p.bindOffTable.value = p.preShort.value
   //p.castOnTable.value = p.preShort.value
   
   /* 
@copyright 2004 Lucia Liljegren
*/

    
if(maxTimes == 1){  shortInc2 = minStitch; shortInc1=maxStitch;}
else{		    shortInc2 = maxStitch;  shortInc1=minStitch; }

p.shortInc1.value=shortInc1;
p.shortInc2.value=shortInc2;

p.shortInc12.value=shortInc2;
p.shortInc22.value=shortInc1;

// calculate DART shaping rows/stitches.=======================
minShouldStitch = Math.floor(baseIncStitchesDart/2);
maxShouldStitch = minShouldStitch+1;
if(baseIncStitchesDart%2 ==0){maxShouldStitch=minShouldStitch}

p.dartDec2.value = maxShouldStitch;
p.dartDec1.value = minShouldStitch;

p.dartDec22.value = minShouldStitch;
p.dartDec12.value = maxShouldStitch;

p.secondDartDec1.value = baseSecIncStitchesDart;
p.secondDartDec2.value =p.secondDartDec1.value;
//===================
    // in x, y as worn.
    // point="side";
     
// 4 extra rows are added in the center of the dart.
// two are by virtue of the short row process.
// two are the seal row I elected to add.

// note that 4 more extra rows are added by knitting the long row after casting on.
// then 2 more rows are in the neck due to the short rows making the big diagonal. But, these are compensated for by seaming.

if(shoulderDarts == "yes"){twoExtra = 4;}
else {                     twoExtra = 0};

castOnExtra = 4;
totalExtra = twoExtra+castOnExtra;

widthTwoRows = divide(totalExtra,p.mainRowIn.value);

topTrap= eval(p.headOpening.value)/numPieces ;// inches

if(shoulderDarts == "yes"){// convert to rows.
    topTrap = 2*Math.round(  mult(topTrap-widthTwoRows,p.mainRowIn.value)/2 ) ; // rows
    topTrap = divide(topTrap,p.mainRowIn.value) + widthTwoRows;
}  
else {
    topTrap = 2*Math.round(  mult(topTrap -widthTwoRows,p.mainRowIn.value)/2 );
    topTrap = divide(topTrap,p.mainRowIn.value) + widthTwoRows;
} 
// convert back to inche
    
   p.neckWidth.value = topTrap; // for display.  top trap is a better name for calculations.

halfShoulderWidth = 1.05*eval(p.shoulderWidth.value)/2; // dart height-- add ease to shoulders

// only coded for 2 or 4 pieces. If there were 6 pieces, there would be a 3 alpha contribution.
// however, if there were 6 pieces, there would never be shoulder darts (unless I code for alpha <30) 
// so I'll never code it. 
    // down happnes for 2 or 4 pieces, so there are 1 or 2 contributins. 

    
if(point== "down"){ neckContribution = (numPieces/2)*(topTrap/2)*Math.cos(alpha);}
    // side only happens when there are 4 pieces.
if(point =="side"){ neckContribution = topTrap/2 + (topTrap/2)*Math.cos(2*alpha) }  

unSlantRemHalfShoulder = halfShoulderWidth - neckContribution;
slantRemHalfShould = unSlantRemHalfShoulder/Math.cos(beta);  // This is the slanted 

if(unSlantRemHalfShoulder<0.1){
    p.tooBigHead.value = "Neck opening is wider than shoulders. Please make the head size smaller or the shoulder larger. "}
else {p.tooBigHead.value ="" }

    // put into as knitted coordinate system and divide by two for half shoulder.
yRem = slantRemHalfShould*Math.cos(gamma); // stitches
xRem = slantRemHalfShould*Math.sin(gamma); // rows


firstGuessShouldRows = mult(xRem,p.mainRowIn.value);

//alert('p.neckWidth.value =' + p.neckWidth.value +' fullShoulderWidth=' + fullShoulderWidth +' neckContribution=' + neckContribution+' remShoulderHalfWidth=' + remShoulderHalfWidth+' xRem=' + xRem+' firstGuessShouldRows=' + firstGuessShouldRows +'rows'  ) 

//alert(' fullShoulderWidth ' + fullShoulderWidth +' neckContribution  ' + neckContribution +'remShoulderHalfWidth ' + remShoulderHalfWidth +' slantRemHalfShould' + slantRemHalfShould + ' xRem ' + xRem +  '  firstGuessShouldRows ' +  firstGuessShouldRows);

//varDartReps // these are for the "main" dart that makes it to the shoudler.
//secondDartRep // these are for the "smoothing" dart;

if(shoulderDarts == "no"){
    firstGuessShouldRows =0;
    varDartReps=0;
    secondDartRep=0;
    } 
else {
    varDartReps = Math.ceil( firstGuessShouldRows/4); // make sure wide enough.
    if(22.5 < gammaDeg ){
         secondDartRep=Math.round(varDartReps*2/3); }
    else{secondDartRep=0; }
    }
    
totalDartRows = 4*varDartReps + 2*secondDartRep;
totalStitchesDart = varDartReps*(maxShouldStitch+minShouldStitch);

//alert('flareType = ' +flareType + ' : ' + p.ponchoFlare.selectedIndex+ ' : darts ' + p.darts.selectedIndex +': '+ shoulderDarts + ':  p.neckWidth.value= ' + p.neckWidth.value + " : " + widthTwoRows + ': totalDartRows =' +totalDartRows + ':halfShoulderWidth' + halfShoulderWidth +" : point =" + point + ": firstGuessShouldRows ="+ firstGuessShouldRows );

p.dartRep.value=varDartReps
    p.dartRep2.value=p.dartRep.value
p.secondDartRep.value = secondDartRep;
    p.secondDartRep2.value=p.secondDartRep.value;
    
p.heldStitches.value = varDartReps*(maxShouldStitch+minShouldStitch) + secondDartRep*baseSecIncStitchesDart
    p.heldStitchesPost.value = p.heldStitches.value 
    
//--------------Calculate length and increases etc.
widthPreShort = divide(p.preShort.value,p.mainRowIn.value);
sideLength = mult(p.diagonalLength.value, Math.sin(alpha));
fullLength = add(p.neckWidth.value, 2*sideLength);

    // Calculate the number for rows in teh diagonal.
numDiagRows = 2*Math.round(mult(sideLength-widthPreShort,p.mainRowIn.value)/2);


numShortShapes = Math.round((numDiagRows-2)/baseIncRows);
numShapeRows= baseIncRows*numShortShapes;  // The first two are for the cast on.


// find how many stitches refind widths.
p.numShortShapes.value =numShortShapes;
p.numShortShapes2.value=p.numShortShapes.value;

numShapeStitches = numShortShapes*baseIncStitches;
totalStitches = numShapeStitches+ eval(p.preShort.value);

//alert('totalStitches = '+ totalStitches + ' ; numShapeStitches = '+ numShapeStitches + ' ; baseIncStitches = ' + baseIncStitches  + ' ; numShapeStitches = '+ numShapeStitches )

p.totalStitches.value = totalStitches;
    p.neckStitchTable1.value=totalStitches
    p.neckStitchTable2.value=totalStitches

width = divide(totalStitches,p.mainStIn.value);
 p.height.value=width;// (this is sideways-- knit width.)
    p.endNeckWidthTable.value=p.height.value;
    p.begNeckWidthTable.value=p.height.value;

//----------These should mostly echo.-------------------------------
var rowsToNeck=numShapeRows+4;  // two full rows plus first short row.
p.begNeckRowsTable.value=rowsToNeck;
    p.begNeckRows.value = rowsToNeck;
p.begNeckLength.value=divide(rowsToNeck,p.mainRowIn.value);


adjNeckWidth=eval(p.neckWidth.value)-widthTwoRows;// takes off the cast on 4 rows plus the extra from the dart.
    
if(shoulderDarts == "yes"){
   neckRowsWithoutDart = 4*Math.round(mult(adjNeckWidth, p.mainRowIn.value)/4);
   neckRowsWithDart = neckRowsWithoutDart+twoExtra + 2* totalDartRows ;
   p.neckRowsPreDart.value=neckRowsWithoutDart/2;
   p.neckRowsPostDart.value =neckRowsWithoutDart/2 ;
   p.cumNeckRowsPreDart.value=add(p.neckRowsPreDart.value,rowsToNeck);
   p.centerRow.value =  add(p.cumNeckRowsPreDart.value,totalDartRows+1);
   p.cumNeckRowsPostDart.value = add(p.cumNeckRowsPreDart.value,2* totalDartRows+4); /* 
      The extra two rows wcome from extra passes that arise due to dart shaping. I am adjusting the knitted neck width for these.  I'm not accounting for the seal rows-- so the neck will actually be a little big.  My reasoning is that glitches arise 
      */
   p.cumNeckRows.value=add(neckRowsWithDart,rowsToNeck);
   
    
}
else {
    neckRowsWithoutDart = 2*Math.round(mult(adjNeckWidth, p.mainRowIn.value)/2);
    totalDartRows = 0;
    neckRowsWithDart=neckRowsWithoutDart;
    p.neckRowsPreDart.value=neckRowsWithoutDart; 
    p.neckRowsPostDart.value =0;   
    p.cumNeckRowsPreDart.value=add(neckRowsWithoutDart,rowsToNeck);
    p.cumNeckRowsPostDart.value = p.cumNeckRowsPreDart.value;   
    p.cumNeckRows.value=p.cumNeckRowsPreDart.value;
    p.centerRow.value = p.cumNeckRows.value
    
}
    
p.neckWidthPreDart.value = divide(p.neckRowsPreDart.value,p.mainRowIn.value);
    p.neckWidthPostDart.value =  p.neckWidthPreDart.value;
if(shoulderDarts == "no"){p.neckWidthPostDart.value = p.neckWidthPreDart.value}


    p.cumNeckRowsPreDartTable.value=p.cumNeckRowsPreDart.value
    p.cumNeckRowsPostDartTable.value=p.cumNeckRowsPostDart.value
    p.centerRowTable.value = p.centerRow.value
//---------


p.endNeckRowsTable.value =p.cumNeckRows.value;
p.endNeckLength.value=divide(p.cumNeckRows.value,p.mainRowIn.value);

p.finalTotalRows.value=add(p.cumNeckRows.value,rowsToNeck-2);
p.endIncDiagRows.value = sub(p.finalTotalRows.value,2);
p.lastShortRowTable.value =p.endIncDiagRows.value;
    p.totalRowsTable.value = p.finalTotalRows.value
p.finalTotalLength.value=divide(p.finalTotalRows.value,p.mainRowIn.value);
    p.hemWidth.value =p.finalTotalLength.value;
    

//p.lengthTable.value = p.finalTotalLength.value;
    //p.castOnStitchTable.value = p.castOn.value;
    //p.bindOffStitches.value = p.castOn.value;
p.totalStitches.value=totalStitches;
    p.castOn.value = p.totalStitches.value
    p.bindOff.value =p.totalStitches.value
 

p.begDartLength.value = divide(p.cumNeckRowsPreDartTable.value,p.mainRowIn.value);
p.endDartLength.value = divide(p.cumNeckRowsPostDartTable.value,p.mainRowIn.value);

p.mainStName2.value = p.mainStName.value;
p.heightEcho.value = p.height.value;
p.fullHemWidth.value = mult(numPieces,p.hemWidth.value);
if(numPieces == 4){ 
    widthDarts = divide(2* totalDartRows, p.mainRowIn.value) 
    p.fullHemWidth.value = sub(p.fullHemWidth.value,2*widthDarts); }
    
p.heldStitchesTable.value = p.heldStitches.value;

if( eval(p.totalStitches.value)< (eval(p.heldStitchesTable.value) +3) ){
   p.tooFewStitches.value = "Warning! This poncho doesn't cover your shoulders.  Please make the diagonal longer or the shoulders smaller."}
else {p.tooFewStitches.value=""}
p.tooFewStitches2.value=p.tooFewStitches.value
p.neckTrimHigh.value = Math.round(numPieces* eval(p.neckWidth.value)*eval(p.mainStIn.value));
p.neckTrimLow.value = Math.round(numPieces* 0.90*eval(p.neckWidth.value)*eval(p.mainStIn.value));

p.neckWidth.value = justRound(p.neckWidth.value);
//==== close function

p.totalLengthTable.value = p.finalTotalLength.value;
p.numPieces2.value = p.numPieces.value
p.numPieces3.value=p.numPieces.value


p.widestAtHem.value = mult(p.fullHemWidth.value,Math.cos(alpha)/2);

// find real diagonal length
begNeckLength = eval(p.begNeckLength.value);
begNeckWidthTable = eval(p.begNeckWidthTable.value);

realDiag = Math.sqrt(begNeckWidthTable*begNeckWidthTable+begNeckLength*begNeckLength);
p.realDiag.value =justRound(realDiag);

p.neckRowsPreDartNoDart.value = p.neckRowsPreDart.value 
p.cumNeckRows3.value =p.cumNeckRows.value
p.neckRowsPostDartNoDart.value = add(p.neckRowsPreDartNoDart.value,p.cumNeckRowsPreDart.value)+4;
/* 
@copyright 2004 Lucia Liljegren
*/
//============END calcPoncho


}

else {
    alert("Sorry! I didn't fill the boxes. Please run this page from my site at www.thedietdiary.com!")
    p.tooFewStitches.value = "Sorry! I'm not going to fill all your boxes correctly.  Please load this page from my web site at www.thedietdiary.com!"; 
    p.tooBigHead.value = "Please run this from my site!" ;
    p.skipShoulderDarts.value ="This shaping data is probably wrong. Please run from my site."
    p.skip.value = "Invalid" 
    p.skip.value = "Invalid" 
    p.mainStIn.value=0.01;
    p.mainRowIn.value=0.01;
}
//
}

/* 
@copyright 2004 Lucia Liljegren
*/

