function calcMasterValuesHK(){
//warning="no warning";



// these are the boundary values for the sweater.
p.buttonBandWidth.value = 0; // take out if I add button bands.

// for bolero 4 stitches.-- page more complicated back in for actual button band.

buttonBandWidth=divide(4,p.mainStIn.value); // 4 stitches.

// readjust sleeve to makes sure it's at least 2 stitches wide for hand knit.
// first adult because I may have changed the neck width for the cardigan.
chestWidth=divide(p.chest.value,2);
neckWidth=eval(p.neckWidth.value);

p.minSleeveWidth.value=chestWidth-neckWidth;
p.sleeveWidth.value=Math.max(p.minSleeveWidth.value,p.sleeveWidth.value);

origMinSleeveWidth=eval(p.minSleeveWidth.value)
extraWidth=divide(2,p.mainStIn.value)
p.minSleeveWidth.value=Math.ceil(10*  add(extraWidth,p.minSleeveWidth.value) )/10;
p.sleeveWidth.value=Math.max(eval(p.minSleeveWidth.value),eval(p.sleeveWidth.value));
p.backNeckDepth.value=divide(sub(p.sleeveWidth.value,origMinSleeveWidth),2);


    // rib values--  change later.
    bodyRibLength=eval(p.bodyTrim.value);
    sleeveRibLength=eval(p.cuffTrim.value);
    buttonBandWidth=eval(p.buttonBandWidth.value); //may be positive or negative.
    widthTwoRows=divide(2,p.mainRowIn.value);    

    bodyLength=sub(p.totalLength.value,bodyRibLength);  bodyLengthPrint=Math.round(bodyLength*10)/10;
    sleeveLength=sub(p.sleeveLength.value,sleeveRibLength);  sleeveLengthPrint=Math.round(sleeveLength*10)/10
    
    cuffRibRows= Math.round(mult(sleeveRibLength,p.mainRowIn.value));
    bodyRibRows = Math.round(mult(bodyRibLength,p.mainRowIn.value));

    //alert(cuffRibRows + ":" + bodyRibRows); // these may be being calculated elsewhere; if so I need to eliminate those lines of code.
    
    //alert(p.sleeveLength.value + "- " + sleeveRibLength + " = " + sleeveLength  );


    frontArmholeDepth=sub(p.armHoleDepth.value,p.backNeckDepth.value);frontArmholeDepthPrint=Math.round(frontArmholeDepth*10)/10;
    //alert("calc armholeDepth=" + p.armHoleDepth.value + "-"+ p.backNeckDepth.value +" front"+ frontArmholeDepth)
    
// note: I have a hidden "back neck depth, which is what you get if you don't short row. The real one is shaped.)
    backNeckDepthPrint=Math.round(mult(p.backNeckDepth.value,10))/10;
    p.frontNeckDepth.value = Math.max(eval(p.backNeckDepth.value),eval(p.frontNeckDepth.value));

    frontNeckDepth=sub(p.frontNeckDepth.value,p.backNeckDepth.value); // subtract because top sleeve value affect the front neck.

    lengthOneRow=divide(1,p.mainRowIn.value);

    frontNeckWidth=neckWidth;  
    frontChestWidth=divide(p.chest.value,2) -buttonBandWidth;
    backChestWidth=divide(p.chest.value,2)


    halfFrontChest=frontChestWidth/2; halfFrontChestPrint=Math.round(halfFrontChest*10)/10;
    halfBackChest=backChestWidth/2; halfBackChestPrint=Math.round(halfBackChest*10)/10;
    
     
    // calculate cast on for body.

    
    if(p.hemSnug.selectedIndex == 0){ ratioHem=1 } 
    else(ratioHem = 0.9);
    
    if(p.stitchMult.selectedIndex==0){		evenBody = 0;evenNeck = 1; multiple=2; }// neck odd, body even.
    else if(p.stitchMult.selectedIndex==1){	evenBody=1; evenNeck = 0; multiple=2; }   
	else{evenBody=1; evenNeck = 0; multiple=2; }// even-- for cardigan.
    
     // later set this with a toggle. 
    bodyFront=Math.round( (mult(frontChestWidth,p.mainStIn.value)-evenBody)/multiple)*multiple+evenBody;
    bodyBack= Math.round( (mult(backChestWidth,p.mainStIn.value)-evenBody )/multiple )*multiple+evenBody; 
    bodyStitches=bodyFront+bodyBack;
  
    // rib is 2 x 2 rib, always. (Also works for 1 x 1).
    castOnFront=	Math.floor( ratioHem*mult(frontChestWidth,p.mainStIn.value)/2)*2;
    castOnBack= 	Math.floor( ratioHem*mult( backChestWidth,p.mainStIn.value)/2)*2; 
    castOnBody=castOnFront+castOnBack;
    castOnBodyP1=castOnBody+1;
    
    incFront=bodyFront-castOnFront;
    incBack=bodyBack-castOnBack;
    
    // body length to underarm.
   lengthToUnderarm= sub(bodyLength,p.armHoleDepth.value);  // neither adjusted for back neck.
   
   //alert("actual armhole = " + p.armHoleDepth.value  +" : body without trim" +bodyLength+" : to underarm" + lengthToUnderarm + ": armhole on front" + frontArmholeDepth + " : backNeck = "+ p.backNeckDepth.value);
  
   rowsToUnderArm=Math.round( mult(lengthToUnderarm,p.mainRowIn.value)/2)*2;
    
    // sleeve stuff
    likelyTanTheta= divide(2,p.mainRowIn.value) / divide(1,p.mainStIn.value);
    likeTheta=Math.atan(likelyTanTheta);
    //likeTheta=likeTheta*180/Math.PI
    cosTheta=Math.cos(likeTheta);
    //alert(cosTheta);
    
    sleeveLengthToUnderarm= sleeveLength- frontArmholeDepth-mult(cosTheta, p.backNeckDepth.value);
    sleeveLengthToUnderarmM1=sleeveLengthToUnderarm- lengthOneRow;
    //alert(sleeveLength + " : " +sleeveLengthToUnderarm + " : "+ frontArmholeDepth)
    
    if(p.cuffSnug.selectedIndex == 0){ ratioHem=1; } 
    else if(p.cuffSnug.selectedIndex == 0){ratioHem = 0.9;}
    else{ratioCuff=0.8;}

	bicepStitches= Math.round( (mult(p.sleeveWidth.value,p.mainStIn.value)-evenBody)/multiple)*multiple+evenBody;    
    cuffStitches= Math.round( (mult(p.cuffWidth.value,p.mainStIn.value)-evenBody)/multiple)*multiple+evenBody;
   
	//alert("  cuffStitches= "+cuffStitches + "  bicepStitches=" + bicepStitches);
   
        
	incSleeveStitches=2*Math.floor( (bicepStitches-cuffStitches)/2);
    halfIncSleeve= incSleeveStitches/2;  
	cuffStitches=bicepStitches-incSleeveStitches;
	
	//alert("  cuffStitches= "+cuffStitches + "  bicepStitches=" + bicepStitches);
	
	maxCastOnCuff=2*Math.floor(cuffStitches/2); // forced increase after cuff.
	castOnCuff=	Math.round( (ratioCuff*mult(p.cuffWidth.value,p.mainStIn.value))/2)*2; 
	castOnCuff=Math.min(castOnCuff, maxCastOnCuff);
	
    castOnCuffP1=castOnCuff+1;
    incCuff=cuffStitches-castOnCuff;

    
    rowsSleeve=Math.round( mult(sleeveLengthToUnderarm,p.mainRowIn.value)/2)*2;
    
    extraRows= Math.round( (rowsSleeve/(halfIncSleeve+1) )/6 -1)*2 +2;
	
	
    incRowsSleeve= rowsSleeve-extraRows;// padding to let HKers have some slop.
    
    fastIncSleeve= Math.floor( incRowsSleeve/(halfIncSleeve+1) )-1;
    slowIncSleeve= Math.ceil( incRowsSleeve/(halfIncSleeve+1) )-1;
    printIncRowsSleeve= incRowsSleeve-fastIncSleeve-1;
    
    
    slowRegions= incRowsSleeve%(halfIncSleeve+1)
    fastRegions= (halfIncSleeve+1)-slowRegions;
    
    fastTimes=fastRegions-1; fastRows=(fastIncSleeve+1)*(fastTimes+1)
    slowTimes=slowRegions;   slowRows=(slowIncSleeve+1)*slowTimes;
    
	//alert('extra = '+ extraRows + "  rowsSleeve = " + rowsSleeve + " incRowsSleeve  "+  incRowsSleeve);
    //alert('fastRegions = '+ fastRegions + "  slowRegions = " + slowRegions + " halfIncSleeve  "+  halfIncSleeve);
	
    stitchesAfterFast= bicepStitches-slowTimes*2;
   
    //unnderarm

	
    underArmCut=divide(p.chest.value,40);   
    halfUnderArmStitches=  Math.round(mult(underArmCut,p.mainRowIn.value)-0.5);
    halfUnderArmStitchesP1 = halfUnderArmStitches+1
    underArmStitches=halfUnderArmStitches+halfUnderArmStitchesP1;  // always odd.
    
    frontYoke=bodyFront-underArmStitches;
    firstFrontYoke=frontYoke+halfUnderArmStitches;
    backYoke=bodyBack-underArmStitches;
	    
    sleeveYoke=bicepStitches-underArmStitches;
    firstSleeveYoke=sleeveYoke+halfUnderArmStitchesP1;
	
	
//  raglan shaping

    neckStitches=Math.round( (mult(frontNeckWidth,p.mainStIn.value)-evenNeck)/multiple)*multiple+evenNeck;
    frontNeckStitches=Math.ceil( (mult(frontNeckWidth,p.mainStIn.value)-evenNeck)/(2*multiple))*multiple+evenNeck ;
    
	//alert("sleeveYoke =" + sleeveYoke + " neckStitches " + neckStitches +  " underArmStitches= " +underArmStitches );
	
    edgeNeckStitches=(neckStitches-frontNeckStitches)/2;// needs to be odd.
	frontNeckStitches = neckStitches-2*edgeNeckStitches; 
	
    edgeNeckStitchesM1=edgeNeckStitches-1;     
    frontNeckShapeRows= edgeNeckStitches*2;  // virtually always because I add 2 st every 2 rows.
    begEngeNeckStitches=  edgeNeckStitches+edgeNeckStitches+1;// this needs to be odd because first pass doesn't have a neck decrease.

// these need to match. 
    frontNeckShapeStitches = frontNeckStitches+ 2*begEngeNeckStitches; // +2 so there are 3 left
    sleeveStitchesAtNeck = sleeveYoke - (backYoke-frontNeckShapeStitches);

    
    //alert(neckStitches +" : " + frontNeckStitches +" : edge" +edgeNeckStitches +" : rows =" +frontNeckShapeRows) 
    
    heightNeckEdge=divide(frontNeckShapeRows,p.mainRowIn.value);
    widthNeckEdge=divide(edgeNeckStitches,p.mainStIn.value);
    
    // add extra info to front neck edge.
    minfrontNeckDepth = Math.round(add(p.backNeckDepth.value, heightNeckEdge)*10)/10;
    //alert(frontNeckShapeRows + " rows = " + heightNeckEdge + " inches : width="+  widthNeckEdge); 
    placketStitches=0; /// needs to be defined.
	
// shape V neck and raglan.  Redo compared to bottom up because of the set up rows.
	sixRows=6; // six rows to set up;
	twoRagInc=2;
	oneNeckInc=1;
	
	 ragRowTot=Math.round( (mult(frontArmholeDepth,p.mainRowIn.value))/2 )*2;// On top down, neck neither ads nor subs
	 ragRows=ragRowTot-sixRows;  // adjust for what I actually have so I don't need to adjust differently later.
	 ragPairs=ragRows/2;
	 
//V Neck==========================	 


	if(cardigan=="yes") {  // for now, v neck cardigan.
		stitchesButtonBand= Math.round(mult(buttonBandWidth,p.mainStIn.value)/2)*2;
		subStitchesButtonBand= Math.max(stitchesButtonBand/2 -1,0);
		halfFrontYoke= Math.ceil(backYoke/2)- subStitchesButtonBand
		
		edgeNeckStitches=Math.floor((neckStitches-(2+oneNeckInc)*2)/2)-subStitchesButtonBand ;// 2 to start with pluse 2 on each edge.  Round up.
		
		adjFrontNeckDepth= sub(p.frontNeckDepth.value,p.backNeckDepth.value)
		neckRows=Math.floor( mult(adjFrontNeckDepth,p.mainRowIn.value)/2)*2-sixRows;// will always be smaller than raglan rows.
		neckPairs=(neckRows)/2;// - six because directions have six base rows.
		
		// right now, paris need to be 1 and 2; Later I can change this.
		smallVNeckPairs=Math.floor(neckPairs/edgeNeckStitches);
		largeVNeckPairs=smallVNeckPairs+1;
		
		smallVNeckRows=smallVNeckPairs*2;
		largeVNeckRows=largeVNeckPairs*2
				
		largeNeckTimes=neckPairs%edgeNeckStitches;
		smallNeckTimes=edgeNeckStitches-largeNeckTimes;
	
		//alert(" small Rows " + smallVNeckRows + " : times " + smallNeckTimes+"::  large Rows " + largeVNeckRows + " : times " + largeNeckTimes + ":: ragRows = " + ragRows);	
	}
    	
 // Raglan shaping on sleeve side.

 
     
    ragStitches=(frontYoke-neckStitches-twoRagInc*2)/2;
    //alert(frontYoke + " - "+ neckStitches + " = " +ragStitches);
    ragWidth=divide( ragStitches,p.mainStIn.value)
       
// first try-- this usually works.
	aveTanTheta= divide( frontArmholeDepth,ragWidth );
	aveTanTheta=Math.atan(aveTanTheta)*180/Math.PI;
    
	aveRowsBetRag = ragRows/ragStitches; // don't want this more than 2;

	if(2.0 < aveRowsBetRag){ 
	one1=2; greater='greater'; 
	} // forces 2,4 shaping instead of 2,3 or 3,4.
else{
    one1 = 1; greater='less'; 
    warning+=" Rag rows= "+ ragRows +" Rag Stitches = " + ragStitches + " --Armholes are shallow compared to chest.I haven't coded this yet.  When you read thedirections, they won't add up right..--";
 };// permits 1,2 and 2, 3 shaping under circumstances.
   
	shapeOps=ragRows/one1 ;
	shapeReg=ragStitches; 

	smallRegRag=one1*Math.floor(shapeOps/shapeReg)-1; smallRegRagP1=smallRegRag+1;
	largRegRag=one1*Math.ceil(shapeOps/shapeReg)-1;

	largeTimesRag=shapeOps%shapeReg;
	smallTimesRag=shapeReg-largeTimesRag;// top down doesn't need the - 1 because cast on automatically ads the space.

//=== angle checks
//  Test Angle and swap of the angle is bad.  This could happen for narrower necks, or fatter people.

slowTanTheta= divide((smallRegRag+1),p.mainRowIn.value) / divide(1,p.mainStIn.value); 
slowTheta= Math.round(Math.atan(slowTanTheta)*180/Math.PI) ;

fastTanTheta= divide((largRegRag+1),p.mainRowIn.value) / divide(1,p.mainStIn.value); 
fastTheta= Math.round(Math.atan(fastTanTheta)*180/Math.PI);


//alert("slow angle"+ slowTheta +": (  " + smallRegRag+ " rows , " + smallTimesRag+ " times). Fast angle"+ fastTheta + "(" + largRegRag  + " needles , " + largeTimesRag+ " times ) ; average = " + aveTanTheta + " ( "+ greater +" than 1)");


if( (smallRegRag ==0 ) && (slowTanTheta<45)){// swap for fat
    holdRag  =smallRegRag; 	 smallRegRag = largRegRag; 	largRegRag=holdRag;
    holdTimes=smallTimesRag;   smallTimesRag =largeTimesRag;   largeTimesRag=holdTimes

}

remBeforeSmall= neckStitches+smallTimesRag*2;
remSleeveBeforeSmall=sleeveYoke -largeTimesRag*2

remSleeveAfterSmall  =sleeveYoke-(backYoke-neckStitches);
halfRemSleeve = Math.ceil((remSleeveAfterSmall+1)/2);
raiseNeckRows=2/3*((remSleeveAfterSmall+1)-halfRemSleeve);


raiseNeckLength= divide(raiseNeckRows,p.mainRowIn.value);

//p.realBackNeckDepth.value= sub(p.backNeckDepth.value,raiseNeckLength);;



pickUpSlant=remSleeveAfterSmall-1; 
// (under shoots a bit, which is ok.)

//=== angle checks
//  Test Angle and swap of the angle is bad.  This could happen for narrower necks, or fatter people.

slowTanTheta= divide((smallRegRag+1),p.mainRowIn.value) / divide(1,p.mainStIn.value); 
slowTheta= Math.round(Math.atan(slowTanTheta)*180/Math.PI) ;

fastTanTheta= divide((largRegRag+1),p.mainRowIn.value) / divide(1,p.mainStIn.value); 
fastTheta= Math.round(Math.atan(fastTanTheta)*180/Math.PI);


//alert("slow angle"+ slowTheta +": (  " + smallRegRag+ " rows , " + smallTimesRag+ " times). Fast angle"+ fastTheta + "(" + largRegRag  + " needles , " + largeTimesRag+ " times ) ; average = " + aveTanTheta + " ( "+ greater +" than 1)");


if(smallRegRag<0){warning += "This sweater cannot be knit with the current specifications."}




// detects bad angles. 
if( 58 < slowTheta){ 
    warning +=" Your armholes must be awfully deep. I'll write you directions, but this sweater is almost certain to fit badly at the shoulders."    
}

//     Collar stuff.

	lengthSlantEdge=Math.sqrt(heightNeckEdge*heightNeckEdge +  widthNeckEdge*widthNeckEdge);
    pickUpSlant= Math.round( mult(lengthSlantEdge,p.mainStIn.value) );  // this is just the front neck.  


diagonal=Math.sqrt(frontArmholeDepth*frontArmholeDepth + neckWidth*neckWidth/4);
lengthFrontTrim=lengthToUnderarm+diagonal;
frontTrimStitches = Math.round(mult(lengthFrontTrim,p.mainStIn.value)*0.80)+3;
frontRows= Math.round(mult(lengthToUnderarm+frontArmholeDepth,p.mainRowIn.value))+4
stitchesToRows= Math.round((frontTrimStitches/frontRows)*10);



p.tooFewStitches.value =warning;

}

