function mkBeret(){
p =document.hatForm;

pester = document.location;

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

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

if(document.cookie ==""){
/* @copyright 2004 Lucia Liljegren
*/
    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(pesterAway == "www.thedietdiary.com"){pester="no"}
else if(pesterAway == "thedietdiary.com"){pester="no"}
 else{ pester = "yes"; 
   p.mainStIn.value=0.01;
    p.mainRowIn.value=0.01;
    alert(" Please run my programs from my site at www.thedietdiary.com! " + " xaway  =" + pesterAway +":" ) } ;

//
p.tooFewStitches.value = "javascript is on";
p.diameterMultiple.value = Math.max(1.2, eval(p.diameterMultiple.value));
p.tightnessFactor.value = Math.max(p.tightnessFactor.value, 0);

p.hatBand.value= Math.round(mult(p.headCircumference.value, 1-eval(p.tightnessFactor.value)/100)*10)/10;
 circumference = mult(p.headCircumference.value, p.diameterMultiple.value);
 headCircumference = eval(p.headCircumference.value);
 
 p.hatCircumference.value =  Math.round(circumference*10)/10;
 
  hatDiameter=  circumference/Math.PI;
  headDiameter = headCircumference/Math.PI;
  

  p.hatDiameter.value= Math.round(hatDiameter*10)/10;

//baseStitches = Math.round(mult(p.mainStIn.value,hatDiameter)/8)*4;

hatRadius= hatDiameter/2;
headRadius=headDiameter/2;
difRadius= hatRadius-headRadius

//alert(hatRadius + " - " + headRadius +  " = " +difRadius)
//I want the left needles to come 2/3 of the extra radius in.  that reduced the hat diameter some, but not completely.

fullLeftNeedle = Math.round(1.05*mult(difRadius,p.mainStIn.value)); // factor to take care of the spirals.
rightSideNeedle= Math.round(1.05*mult(hatRadius,p.mainStIn.value));
castOn= fullLeftNeedle + rightSideNeedle;

unusedNeedle = Math.round(rightSideNeedle/12);
leftSideNeedle=fullLeftNeedle - unusedNeedle; // this is the number actually short rowed

   
p.castOn.value=castOn;


// find best shaping

numWedge=10;
p.repeatTimes.value = numWedge-1;
p.numWedge.value = numWedge;

betaRad=2* Math.PI/numWedge;

wedgeShape= new turningWedge(betaRad)
p.shortRow1.value=wedgeShape.repeatNeedle;
p.shortRow2.value=wedgeShape.nonrepeatNeedle;

blankNeedles = castOn-(leftSideNeedle+rightSideNeedle);
idealStitches= wedgeShape.idealStitchesTurn; 
//dif= blankNeedles-idealStitches
//if(dif < 1){ leftSideNeedle= leftSideNeedle+dif+1 }
p.leftSideNeedle.value=leftSideNeedle;
p.rightSideNeedle.value=rightSideNeedle;

p.leftSideNeedle2.value=p.leftSideNeedle.value;
p.rightSideNeedle2.value=p.rightSideNeedle.value;

p.bandNeedles.value = Math.round(mult(p.hatBand.value,p.mainStIn.value));
p.bandRows1.value= Math.round(mult(p.mainRowIn.value,p.bandDepth.value));
//p.bandRows2.value=p.bandRows1.value


rowsPerPass= rowsTurnDart(rightSideNeedle, wedgeShape.idealStitchesTurn,  wedgeShape.turnRowsTotal,wedgeShape.highNeedles);

totalRows = numWedge*rowsPerPass;
knittedCircumference= divide(totalRows,p.mainRowIn.value);
dif = sub(knittedCircumference, circumference)*headCircumference/circumference; // approximately at head band
difRowPerPass=mult(dif,p.mainRowIn.value)/numWedge;

// note dif should generally be negativ because I used a ceilingw when calculating the needl return rate.
// add in half as much as required to make up.
// this is a compromise. I add needles to ge an extra row.
// note: adding "idealStitchesTurn" would add turnRowsTotal rows.  So, I've coded it such that if dif = 8, I add 
// 8 rows per pass.
//alert(difRowPerPass)

    extraStitches= -Math.ceil(difRowPerPass*wedgeShape.idealStitchesTurn/wedgeShape.turnRowsTotal)+1;
   // extraStitches= -Math.ceil(difRowPerPass*wedgeShape.idealStitchesTurn/wedgeShape.turnRowsTotal)-blankNeedles+1;
    
    extraStitches= extraStitches -blankNeedles
    if( 0 < extraStitches){
	extraStitches= Math.max(extraStitches, 0);  // 
	extraLeft=Math.ceil(mult(extraStitches,difRadius/hatRadius));
    
	
	//p.leftSideNeedle.value=leftSideNeedle;  // don't add here, because you want this width at the hem.
	p.rightSideNeedle.value=rightSideNeedle +extraStitches;
	p.castOn.value=castOn+extraStitches+extraLeft; 
	p.rightSideNeedle2.value=p.rightSideNeedle.value;
    }

}
//======================

function turningWedge(baseThetaRad){
// this is written for small angle turns.
// I should pass realThetaRad  That's the only variabe I  ned.

// work out ideal number of stitches to take out of work per 12 rows knit while turning for cape.
    var turnRowsTotal=4
	var idealStitchesTurn = Math.ceil(divide(p.mainStIn.value,p.mainRowIn.value)  * turnRowsTotal / Math.tan(baseThetaRad)  )// don't divide by two.
	var turnNeedleTotal;
	turnNeedleTotal= idealStitchesTurn;
	
	var turnOperations=turnRowsTotal/2;
	//alert( "in TurnWedge totalStitches=" + idealStitchesTurn + ":" + realThetaRad + ": " + turnOperations)
	
	 lowNeedles=Math.floor(divide(idealStitchesTurn,turnOperations));
	 highNeedles=lowNeedles+1;
		
	 timesHigh=idealStitchesTurn%turnOperations;
	 timesLow=turnOperations - timesHigh
	 	
    
    // reset turn rows so instructions will be out of six not 12
    //   later on, code to permit an every 4 rows repeat if the angle matches better that way.
    nonrepeatNeedle=lowNeedles;
    repeatNeedle=highNeedles;  // this is the possibly repeated value.
    
    //alert( "low=" + lowNeedles + " : high= " + highNeedles )
     //alert( "timesLow=" + timesLow + " :  timesHigh " +  timesHigh )
    
    if(timesHigh == 0) {  // nothing repeated.
	nonrepeatNeedle=lowNeedles ;
	repeatNeedle=lowNeedles;
    }
  
    
     //alert( "nonrepeatNeedle" + nonrepeatNeedle + " :  repeatNeedle " +  repeatNeedle )
     
    // recalculate real theta and real shoulder angle.
    realThetaRad =  divide(turnRowsTotal,p.mainRowIn.value)/divide(idealStitchesTurn,p.mainStIn.value);
    realThetaDeg= realThetaRad*180/Math.PI;
    baseThetaDeg = baseThetaRad*180/Math.PI;
//alert(" compare thetas " + realThetaDeg + " :" + baseThetaDeg)
 
this.turnRowsTotal = turnRowsTotal;
this.idealStitchesTurn = idealStitchesTurn;
this.highNeedles= highNeedles;
this.repeatNeedle= repeatNeedle
this.nonrepeatNeedle= nonrepeatNeedle

}

//================
function rowsTurnDart(stitches, stitchesPerBlock, turnRows,highNeedles){
    var numComplete = Math.floor(divide(stitches,stitchesPerBlock));
    var extraLeft = sub(stitches,numComplete*stitchesPerBlock)
    var threeBreak = stitchesPerBlock - highNeedles
    // there are 1 or 2 extra passees left.
    if(extraLeft==0){extraPass=0}
    else if( extraLeft < highNeedles){extraPass=1}
    else {extraPass=2}
    msg= "  highNeedles " +  highNeedles +" numComplete=" + numComplete + "   extraLeft="+  extraLeft + "  extraPass=" +  extraPass 
    //alert("inrowsTurnDart " + msg);
    //alert("Turn: stitches " + stitches + "/"+ stitchesPerBlock + " = " +  numComplete + "=>" + " minus" + "(" + numComplete + "*" + stitchesPerBlock + ") = " +  extraLeft + " =>extraPass " + extraPass)
    
   var rowsTurnDart = numComplete*turnRows + 2*extraPass +2
   return rowsTurnDart;
 
} 


