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";

  circumference = eval(p.hatCircumference.value);
  p.hatCircumference.value =  Math.round(circumference*10)/10;
  hatDiameter=  circumference/Math.PI;
  

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

hatRadius= hatDiameter/2;

//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.
depthBrimFact=1; 
rightSideNeedle= Math.floor(mult(hatRadius,p.mainStIn.value));// extra added later if required.
leftSideNeedle = Math.round(depthBrimFact* mult(hatRadius,p.mainStIn.value));
castOn=rightSideNeedle+leftSideNeedle;

p.castOn.value=castOn;
p.rightSideNeedle.value=rightSideNeedle;
p.rightSideNeedle2.value=p.rightSideNeedle.value

//alert(hatRadius +":"+ p.castOn.value + ":  rightSideNeedle, " +p.rightSideNeedle.value)

// how much gets to be in the front for bill.
rowsBrim=Math.round(mult(depthBrimFact*hatRadius,p.mainRowIn.value)/2)*2+1-3 // 3 for I cord thickness.
shortRowedBrimStitches= 2*(rowsBrim-2-2-1) + 3*2;// 2 per 2 rows, subtract per cast on then 2 rows with 3.
//alert("rowsBrim=" + rowsBrim);

frontNeedles = Math.ceil(mult(circumference,p.mainStIn.value)/2); // odd ok.
centerFrontNeedles=frontNeedles-shortRowedBrimStitches;
sideNeedles= (frontNeedles-centerFrontNeedles)/2;

p.frontNeedles.value = frontNeedles;// for bill part of hat.
p.centerFrontNeedles.value = centerFrontNeedles;
p.outBill1.value= sideNeedles;
//alert(centerFrontNeedles +": side="+ sideNeedles); 


// this is for hanging wedges
raiseLeft = Math.round(frontNeedles/3);
raiseRight=frontNeedles-raiseLeft;
p.raiseRight.value=raiseRight;
p.raiseLeft.value=raiseLeft;

bandNeedles= frontNeedles+raiseRight+raiseLeft;

p.Icord1.value = 2*(3*centerFrontNeedles/2) + 2*Math.ceil(2*4*sideNeedles/3) + 12;// I'm going to need some more.
p.Icord2.value = 2*bandNeedles+12;


// find best shaping

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

betaRad=2* Math.PI/numWedge;

wedgeShape= new turningWedge(betaRad/2);// I'm using code from beret, these are  symmetric, so halve the angle.
p.shortRow1.value=wedgeShape.repeatNeedle;
p.shortRow2.value=wedgeShape.nonrepeatNeedle;

idealStitches= wedgeShape.idealStitchesTurn; 


rowsPerPass= rowsTurnDart(rightSideNeedle, wedgeShape.idealStitchesTurn,  wedgeShape.turnRowsTotal,wedgeShape.highNeedles);
p.rcMid.value= rowsPerPass
p.rcEnd.value= 2*(rowsPerPass-1)

totalRows = numWedge*(2*rowsPerPass-1);
knittedCircumference= divide(totalRows,p.mainRowIn.value);
dif = sub(knittedCircumference, circumference); // approximately at head band
difRowPerPass=mult(dif,p.mainRowIn.value)/(2*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.round(difRowPerPass*wedgeShape.idealStitchesTurn/wedgeShape.turnRowsTotal);
    //alert(extraStitches);
   // extraStitches= -Math.ceil(difRowPerPass*wedgeShape.idealStitchesTurn/wedgeShape.turnRowsTotal)-blankNeedles+1;
    //alert("difRowPerPass= "+   + difRowPerPass+ "; extraStitches =" +extraStitches)
//   Don't do this. Just tell them how big it is. 
    if( 0 < extraStitches){
	extraStitches= Math.max(extraStitches, 0);  // 
	extraLeft=Math.ceil(mult(extraStitches,depthBrimFact));
	    
	
	//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;
	
	rowsPerPass= rowsTurnDart(eval(p.rightSideNeedle.value), wedgeShape.idealStitchesTurn,  wedgeShape.turnRowsTotal,wedgeShape.highNeedles);
	totalRows = numWedge*(2*rowsPerPass-1);

    }

p.hatCircumference2.value=Math.round(divide(totalRows,p.mainRowIn.value)*10)/10
}
//======================

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;
	highNeedles=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){

msg= "stitches= " +stitches+" : stitchesPerBlock= "+ stitchesPerBlock+" : turnRows= "+ turnRows+" highNeedles: "+ 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(msg);
    
   var rowsTurnDart = numComplete*turnRows + 2*extraPass +1
   return rowsTurnDart;
 
} 


