/* 
@copyright 2004 Lucia Liljegren
*/

//==========Needds to share form variables, but I'm going to pass some other stuff. This is just for organization sake.
function designSleeve(extraArmscye){
/* 
@copyright 2004 Lucia Liljegren
*/


if(pester == "no"){

// avoid dividing by zero.
maxbicep=Math.floor(10*(extraArmscye+0.97*((decArmhole)*2-seam)/mainStIn))/10; // -1 is for seams
if( maxbicep < eval(p.bicep.value) ){
p.bicep.value=Math.min(maxbicep,eval(p.bicep.value));
}

// load values
p.bicepPercentage.value = divide(p.chest.value,3);
p.armHoleDepthPercent.value = divide(p.chest.value,4);

bicep=eval(p.bicep.value)
cuffWidth=eval(p.cuffWidth.value);

cuffLength=eval(p.cuffLength.value);

    if(p.foldBack.selectedIndex==1){foldBack=1}
    else{			    foldBack=2}

// calculate cuff stuff.
q.cuffStitches.value=cuffStitches=Math.round(cuffWidth*ribStIn/2)*2+seam;

    if(method == 'hk'){afterCuffStitches = 2*Math.floor(1.10*cuffStitches/2);}
    else{afterCuffStitches=cuffStitches}

p.afterCuffStitches.value=afterCuffStitches;

p.cuffInc.value=cuffInc=afterCuffStitches-cuffStitches;
p.aboveCuffWidth.value= Math.round(10*afterCuffStitches/mainStIn)/10;

q.cuffRows.value=cuffRows=  Math.round(foldBack*cuffLength*ribRowIn/2)*2;
cuffKnitLength=cuffRows/ribRowIn;
cuffFoldLength=cuffKnitLength/foldBack;



fullSleeveLength=eval(p.fullSleeveLength.value);
sleevelength= fullSleeveLength-shoulderWidth/2 ;
    
 p.sleeveLength2.value=  p.sleeveLength.value=Math.round(sleevelength*100)/100;


//===== bicep stuff.

q.bicepStitches.value=bicepStitches=Math.round(bicep*mainStIn/2)*2+seam;

stockingSleeveLength=sleevelength-cuffLength;
knittingSleeveLength= stockingSleeveLength+cuffLength*foldBack;


q.sleeveTotRow.value=sleeveTotRow=Math.round(stockingSleeveLength*mainRowIn/2)*2+eval(q.cuffRows.value);

// cap stuff
q.capStitches.value=capStitches=bicepStitches-decArmhole*2

q.afterArmSleeveRows.value = '??'
capWidth=capStitches/mainStIn;
//    if(p.slimmerSleeve.selectedIndex==1){capX = 0.6;}// wider range
//    else{capX = 0.4};

alpha1For2 = Math.atan(2*mainStIn/mainRowIn);
alpha1For2Deg= alpha1For2*180/Math.PI;

alphaDeg=Math.min(0.90* alpha1For2Deg, 55);// to make there be two at the end.
alpha=alphaDeg*Math.PI/180
cosSecAlpha=1/Math.cos(alpha)
sdivW= extraArmscye/capWidth;

// finds best cap for most sleeves, but then bracket to permit large and small sleeves.
capX=((extraArmscye/capWidth)-cosSecAlpha)/(1-cosSecAlpha);
    capX= Math.min(0.5, capX);
    capX= Math.max(0.25,capX);
    

topWidth=capX*capWidth;
sideWidth=((1-capX)*capWidth)/2;

capDiagLength=0.5* (extraArmscye- topWidth);
sqrHeight= capDiagLength*capDiagLength -sideWidth*sideWidth
//------------
    if(sqrHeight <0){  // this shouldn't happen, but I'm keeping it to protect againts negative values.
	height=0;
	p.bicep.value=Math.floor(10*(extraArmscye+0.97*decArmhole*2/mainStIn))/10;
	
	        }
    else{ 
    height=Math.sqrt(sqrHeight);

    }
//------------------------

rowsCap = 2*Math.round(height*mainRowIn/2);
stDecCap=Math.round(sideWidth*mainStIn);


p.afterArmSleeveRows.value=afterArmSleeveRows=sleeveTotRow-rowsCap;
p.sleeveUArmRow.value=sleeveUArmRow=afterArmSleeveRows-totalArmholerows;// like back

decTimesCap=rowsCap/2;

oldBicep=p.bicep.value;
 
 // blank stuffout
    p.maxCapTimes.value=0;
    p.minCapTimes.value= 0;
    p.sleeveUArmLength.value=0
    q.minTaperIncRows2.value=p.minTaperIncRows.value= 0//-1 is for
    q.maxTaperIncRows.value=0;

    q.maxTaperIncTimes.value= 0
    q.minTaperIncTimes.value =0 // 1 is for the first dec.


extraRows= 2*Math.max(decTimesCap-stDecCap,0);
maxExtraRows=2*Math.round(0.15*rowsCap/2);


extraRows= Math.min(maxExtraRows,extraRows);
extraTimes=extraRows/2;

p.blankSleeveRows.value=extraTimes;
fewerTimes = extraRows/2;
decTimesCap=decTimesCap-fewerTimes;

//------------------------

    if(2 * (decTimesCap)-1 < stDecCap){ 
	// too wide.
	decFact=0.25
	decOverall = decFact*(stDecCap- 2 * (decTimesCap-1))/mainStIn;    
	p.bicep.value= sub(p.bicep.value,decOverall);
	 smaller = "larger" ;
	 narrow = "wide";
	 sleeveAlertMssg="yes"
	 series +=  p.bicep.value + " : ";
	      
    }
    
    else if( stDecCap < decTimesCap)  {
    incFactor=0.6;
    incOverall=incFactor*((decTimesCap)-stDecCap)/mainStIn;
    p.bicep.value= add(p.bicep.value,incOverall);
   smaller = "smaller"   ; 
   narrow = "narrow";
   sleeveAlertMssg="yes";
   series += ": " + p.bicep.value;
    }

    else { // calculate the cap and taper shaping.
   redo=redoMax+1;
    minCapDec=Math.floor(stDecCap/decTimesCap);
	if(minCapDec !=1){
	    if(minCapDec<1) {
		
		p.bicep.value=p.bicep.value*1.03}
	   if( 1< minCapDec ) {
	     
	    p.bicep.value=p.bicep.value*0.97}
	   }
	    
    q.maxCapTimes.value=maxCapTimes=Math.floor(stDecCap%decTimesCap);
    
    q.minCapTimes.value=minCapTimes= decTimesCap-maxCapTimes;
    
    q.minCapTimes2.value=0
    
     if(0<extraRows){	
	
	regMinCapTimes=minCapTimes-extraTimes;  // the extra times are reduced  more slowly and add the extra rows.
	q.minCapTimes.value = firstMinCap= Math.round(regMinCapTimes/2)
	q.minCapTimes2.value= secondMinCap = regMinCapTimes -firstMinCap;

	
    }
    
    midCapRow= minCapTimes*2+extraRows
    midCapDec=minCapTimes // single sided.
        
    sleeveUArmRow=eval(q.sleeveUArmRow.value);
    incSleeveRows= sleeveUArmRow-cuffRows;
    
    if(method == "hk"){incSleeveRows=Math.round(incSleeveRows*0.95/2)*2}
    
    p.sleeveEndTaperRow.value = cuffRows+incSleeveRows
    
    incSleeveOps=incSleeveRows/2;
    incSleeveStitches=bicepStitches-afterCuffStitches;
    
    incSleeveTimes=(incSleeveStitches/2);
    incSleeveRegions=incSleeveTimes+1
    
    minTaperIncRows=2*Math.floor(incSleeveOps/incSleeveRegions);
    maxTaperIncRows = minTaperIncRows+2;
    
    q.minTaperIncRows2.value=p.minTaperIncRows.value= minTaperIncRows-1//-1 is for
    q.maxTaperIncRows.value=maxTaperIncRows-1;
    
   
    
    /* 
@copyright 2004 Lucia Liljegren
*/

    
    q.maxTaperIncTimes.value= maxTaperIncTimes =incSleeveOps%incSleeveRegions
    q.minTaperIncTimes.value =minTaperIncTimes= incSleeveTimes- maxTaperIncTimes; // 1 is for the first dec.
    
    midShapeRows= (minTaperIncTimes*minTaperIncRows)  // for estimating stitches.
    midShapeStitches= (afterCuffStitches + 2*minTaperIncTimes)
    
    q.sleeveUArmLength.value = lengthFromCastOn(p.sleeveUArmRow.value);
    p.afterArmSleeveLength.value = lengthFromCastOn(p.afterArmSleeveRows.value);
    
    q.cuffLength2.value=lengthFromCastOn(p.cuffRows.value);
    q.sleeveTotKnittedLength.value = lengthFromCastOn(p.sleeveTotRow.value);
    
    numStXRowsSleeve   = (cuffStitches+2)*ribStitchFactor* (ribRows+2)    // cuffs  2 for cast on.
    // taper, include intermediate shape because I don't want to underestimate
    numStXRowsSleeve +=  0.5*(afterCuffStitches +midShapeStitches +2)*midShapeRows 
    numStXRowsSleeve += 0.5*(midShapeStitches +bicepStitches +2)*(incSleeveRows-midShapeRows)
    
    numStXRowsSleeve += bicepStitches*(sleeveUArmRow-incSleeveRows) // after taper.
    numStXRowsSleeve += (capStitches+2)*(rowsCap+1)  // as though unshaped.  2 for bind off.
    numStXRowsSleeve += -midCapDec*midCapRow  // two triangles-- works out ok even when there are blank rows.
    numStXRowsSleeve += -(stDecCap+midCapDec)*(rowsCap-midCapRow) // two trapezoid above.
    p.sleeveStitches.value = Math.round(numStXRowsSleeve/10)*10;
    weightSleeve=mult((numStXRowsSleeve/1000), p.ouncePer1000.value);
    
    p.weightSleeve.value =  Math.round(weightSleeve*100)/100;
    p.weightTotal.value = Math.ceil((weightBack+weightFront+2*weightSleeve+weightTrim)*100)/100 // trim weight is a gross approximation
      
    }// cap block
    
    // set alerts if necessary
if(sleeveAlertMssg=="yes"){
extraMsg=" xx";

    if(redoMax<= redo){
	extraMsg=" I've increased the value " + redo + " times guessing these values: (" +series+ ").  But it still doesn't fit!   ** You ** must  re-type a new value to make the program calculate the cap shaping. " ;}
        
    else {
	extraMsg=" I recalculated and I think it fits. (I  tried "+ redo +" times guessing these values: (" +series+ "). You could try a new value; a slightly "+ smaller +" one might fit.";
	}

sleeveAlertMssg= "The sleeve was too "+  narrow +" for this design. I've changed it to " +p.bicep.value +" inches. "+ extraMsg


}


// 
}
//   ======
else {
    p.mainStIn.value=0.01;
    p.mainRowIn.value=0.01;
     }

} // end function.
//==============================
function lengthFromCastOn(RC)
/* 
@copyright 2004 Lucia Liljegren
*/


{

var mainRowsLength= (eval(RC)-cuffRows)/mainRowIn;
var ribRowLength=cuffRows/ribRowIn;
return length= Math.round(10*(mainRowsLength+ribRowLength))/10;
}
