/* 
@copyright 2004 Lucia Liljegren
*/

function guessSwatch(){

//cookie shuffling.
// set a cookie with a cookie name equal to the document  location.
p =document.swatchForm;
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"){
// 


//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

  if(eval(p.guessMainStIn.value) < 0.00001) {p.guessMainStIn.value = 0.001; }
  if(eval(p.guessMainRowIn.value) < 0.00001) {p.guessMainRowIn.value = 0.001; }
  
  p.ribNeedle.value = eval(p.mainNeedle.value)-1;
  p.mainNeedle2.value= p.mainNeedle.value;

 ribFraction= 1;  
if(p.ribType.selectedIndex == 0){ ribType="real"; ribLength = 1.5;  ribFraction= 1;}
else if(p.ribType.selectedIndex==1 ){ ribType="mock"; ribLength=3.0; ribFraction= 2/3; }  // 2 x 1 mock ribbing
else if(p.ribType.selectedIndex==2 ){ ribType="mock"; ribLength=3.0; ribFraction= 1/2;  } // 1 x 1 
else if(p.ribType.selectedIndex==3 ){ ribType="mock"; ribLength=3.0; ribFraction= 1;  }  // hung hem.

whose = "Lucia"
if(p.whoseSwatch.selectedIndex == 0){whose = "Lucia"}
else if(p.whoseSwatch.selectedIndex == 1) {whose = "notLucia"}


if(p.hkOrMk.selectedIndex==0){ hkOrMk="hk"; width=4; swatchLength = 4; }
else{                         hkOrMk="mk"; width=6; swatchLength=6;  }


mainStIn= eval(p.guessMainStIn.value);
mainRowIn=eval(p.guessMainRowIn.value);

p.castOn.value= castOn= 2* Math.ceil( (width * mainStIn + 4)/2 );
/* 
@copyright 2004 Lucia Liljegren
*/

p.ribRows.value= ribRows= 2* Math.ceil ( (ribLength*mainRowIn + 2)/2 ); 
p.stockingRows.value=stockingRows= 2* Math.ceil( (swatchLength*mainRowIn +4 )/2 );
finishRows=2*Math.ceil(mainRowIn/2);
p.finishRows.value=finishRows=Math.min(10,finishRows); // at most rows stitches or 1"


if(hkOrMk == "mk") {
    p.castOn.value= 	castOn = 		10* Math.ceil( castOn/10);
    p.ribRows.value= 	ribRows = 		10* Math.ceil( ribRows/10);
    p.stockingRows.value=stockingRows =		10* Math.ceil( stockingRows/10);
    }
    
    
 

// st/in  main.  
        
    if(whose == "Lucia") {
         p.stBetweenPins.value = stBetweenPins = castOn-4;
	p.actualStBetweenPins.value="XXX";
	realMainSwatchStitches = stBetweenPins;	
	realStitchInSwatch = castOn;
	}
    else { 
         p.stBetweenPins.value = "XXX";
	if(isNaN(p.actualStBetweenPins.value) ){ p.actualStBetweenPins.value="enter"; }
	realMainSwatchStitches=p.actualStBetweenPins.value;
	realStitchInSwatch = add(realMainSwatchStitches,4);	
	}
	    
    swatchWidth=eval(p.swatchWidth.value);
    p.mainStIn.value =mainStIn=divide(realMainSwatchStitches,swatchWidth);
    
    ribWidth =eval(p.ribWidth.value)
    p.ribStIn.value = ribStIn =divide(realMainSwatchStitches,ribWidth);
 

   
// rows /in 
    
        
       
    if(whose == "Lucia") {
		
	p.actualRibRows.value="XXX";
	p.actualMainRows.value="XXX"; 
	
	realMainRows=stockingRows;
	realRibRows = ribRows;	
	}
    else { 
         p.stBetweenPins.value = "XXX";
	 
	if(isNaN(p.actualMainRows.value) ){ p.actualMainRows.value="enter"; }
	if(isNaN(p.actualRibRows.value) ){ p.actualRibRows.value="enter"; }
	
	realMainRows=p.actualMainRows.value;
	realRibRows=p.actualRibRows.value;
	finishRows=0;
	}
	
    realSwatchLength=eval(p.realSwatchLength.value);
    p.mainStRowIn.value = mainStRowIn = divide(realMainRows,realSwatchLength);

    ribLength=eval(p.ribLength.value);
    p.ribRowIn.value= ribRowIn = divide(realRibRows,ribLength);

//  Yarn estimator
    totalRowsStockinet= add(realMainRows,finishRows);
    totalRowsRib = mult(ribFraction,realRibRows);
    
    totalRowsSwatch = add(totalRowsStockinet,totalRowsRib);
    p.numStitches.value= numStitches = mult(realStitchInSwatch, totalRowsSwatch);
    
    p.ouncePer100.value=ouncePer100=divide(p.weightSwatch.value,numStitches/1000)
    
    p.realRatio.value=ratio = divide(mainStRowIn,mainStIn);
    
}
/* 
@copyright 2004 Lucia Liljegren*/

//------------------------
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.mainStIn.value=0.01;
    p.mainRowIn.value=0.01;
}
//

}
/* 										@copyright 2004 Lucia Liljegren*/
