pageArray = new Array ("Done.html","head_neck.html" , "WeltDepth.html","Gauge_order.html","UTorso_order.html",
"ArmSleeve.html",
"Knitters_Notes.html","ButtonBands.html", "toggles.html","HowToCustomize.html","TorsoLengths.html",
"GuessMeasurements.html","CookieTest.html")

mssgBody = "The style form is not complete"

modColor= "#BBFFEE"
savedColor ="#FFFFEE"
filledColor = "#FFFFEE"
incompleteColor = "#FFcccc"
notRequired = "XXXXXX"

notSavedMssg = "You have not saved your modifications.   Please, click 'Save' to save new values or click 'Restore' to load your old saved values from memory.   "

blankMssg = 'This page has not been modified or filled. Saving will overwrite the values stored in memory.  If you want overwrite the values in memory, click save again. Otherwise, fill or edit the form.'

modMssg = "Modified. Click 'Save' to activate the changes.  Click 'Restore' to undo changes."

estMessage = "<b>Wait for the left hand panel to refresh!</b> <br>The 'function' button will toggle to 'final pattern', and the estimated values will be stored in memory.<p>Afterwards: <br>1) If you have not done so already, click the <em>Gauge</em> button, then fill out and save the <em>Gauge</em> form.<br> 2) Select, view and edit the <em>Measurement</em> forms.  (Optional)<br>3) Click Submit. <br> Your sweater pattern will appear in a new window."

patCreateMssg =" <b> Pattern Created </b>  <p> Find your pattern in a new window which should open on your screen.<p> Until you close the design form window, your design values are saved. You may edit and create a new pattern! "

noGaugeMssg="<b> Please edit and save the Gauge Form</b>"

estDirMssg ="<H3> Estimate </H3> You have set the toggle to <em> Estimate </em>, which is Phase I of sweater design.  The steps in Phase I are: <br>1) Click, fill out and save the <em>Style</em> form.  <br>2) Click <em>Submit</em> in the left hand frame. <br>3) The <em> Function</em> button will toggle to 'Pattern' . The left hand frame will be loaded with a large number of 'hidden' body measurements which the program selected based on your hip, bust and neck dimensions.  <br> This completes Phase I.  <p>Phase II is creating the final pattern.  The steps in Phase II are<br> 2) Toggle <em>Measurements</em> to <em>Gauge</em>. Fill out the form. <br>3) Optional: Examine and edit the other forms. (If you are a standard size, you don't need to edit these.)<br> 6) Click <em>Submit</em>.<HR> <p> If you don't want anymore 'how to' reminders, toggle the <em> Novice/Expert</em> menu to Expert!<br> Avoid using your browser's 'back' button or resizing the window!"

finalPatMssg = "<b>Create your final pattern.</b>  <p>You should have already used the <em>Estimate</em> function to fill all the <em>Measurement</em>forms with standard 'off the rack' values.  <p>The next steps are: <br>1) Click, fill out and save the <em>Gauge</em> form. (Required)<br> 2) If you are a non-standard size, or want to customize the style, then select, and edit the <em>Measurement</em> forms. When they are filled out to your satisfaction, save the measurement form. (Optional) <br>3) Click Submit. "

//---------------------lucia knitting fiend-------------------
function TransferValue(pForm,qForm,filled,p,q){
/*  transfers data from  pForm to qForm */
var p1= parent.content.document.sweater_data

if(p1.blank.value == "blank" && p == parent.content ){
     p1.blank.value = "warned"; alert(blankMssg) }
else { doTheTransferNew(pForm,qForm,filled,p,q) } 

} /*Transfer done */

/*------------------*/

function commonUpdate() {
var p = parent.content.document.sweater_data
var q = parent.left.document.ControlForm 

parent.content.document.bgColor =parent.left.modColor
p.filled.value = modMssg ;
p.blank.value = "changed"
q.status.value="modified"
}

//----------------------------------------
function checkEstSubmit(){

var q=parent.left.document.ControlForm
var ok = "ok"
var estEssential = new Array (q.chest, q.hip, q.neck_circ)
var num = estEssential.length 

for (var i =0; i<num; i++){   /* 1 */ 
      if( isNaN(parseFloat( estEssential[i].value )) &&  estEssential[i].value != notRequired ) { /* 2 */ 
             if( ok == "ok") { ok = "bad"  /* 3 */ 
             parent.content.document.write(mssgBody)
             parent.content.document.write ("Please click 'Style'  and provide numbers for the following:")  }  /* 3 */ 
  parent.content.document.write ("<li>" +estEssential[i].name + " is set to " +estEssential[i].value )       
      }/* 2 */  
}/* 1 */ 

if( ok != "ok") { parent.content.document.write("<p><b> Don't use the back button on your browser!</b>") 
parent.content.document.close() }
return ok
}
//-----------------------------------------

function checkFinalSubmit(){

var q=parent.left.document.ControlForm
var ok = "ok"

// make sure chest,head, neck are filled.
var ok = checkEstSubmit()  

// make sure gauge is filled in
if(q.whichScript.selectedIndex == 2 && ok == "ok") {/* 0 */ 
  setEssentials()
  var estEssential = gaugeEssentials
  var num=estEssential.length

   for (var i =0; i<num; i++){   /* 1 */ 
      if( (isNaN( estEssential[i].value ) &&  estEssential[i].value != notRequired ) ||
estEssential[i].value <=0 ) { /* 2 */ 
             if( ok == "ok") { ok = "bad"  /* 3 */ 
             parent.content.document.write(mssgBody)
             parent.content.document.write ("Please click the GAUGE menu and correct the following:")  }  /* 3 */ 
  parent.content.document.write ("<li>" +estEssential[i].name + " is set to " +estEssential[i].value )       
      }/* 2 */  
}/* 1 */ 

if( ok != "ok") { parent.content.document.write("<p><b> Don't use the back button on your browser!</b>") 
parent.content.document.close() }
}/* 0 */ 
return ok
}


//----------------------------------------
function setContent(thisPage) {
/* Main function: puts correct form in window.*/
var pLCf3=parent.left.document.ControlForm

//pLCf3.status.value="called this";

if(pLCf3.status.value == "modified" ){
   pLCf3.status.value = "warned2";
   pLCf3.SelectForm.selectedIndex=0;
   alert(notSavedMssg);
}
else { // *1*

// -- now actually change the page.

pLCf3.status.value = "RESTORE";

     // format of call: makeCookie(name,pForm,minutes,speak)
     // Make a cookie from the form.  Also make fill the cookieName cookie.
makeCookie(pLCf3.cookieName.value,pLCf3,pLCf3.minutes.value,"no")  // in GenCookie.js
createAndTestCookie('cookieName',pLCf3.cookieName.value,pLCf3.minutes.value,"create","no")

pLCf3.status.value = "Wrote";

if(window.location.hostname == "www.thedietdiary.com" ){
   qT="http://www.thedietdiary.com/knittingfiend/OrderForms/"
    return parent.content.document.location.href = qT+pageArray[thisPage] }

else{ 
 qT="http://www.thedietdiary.com/knittingfiend/OrderForms/"
return parent.content.document.location.href = pageArray[thisPage] }

} // *1*

}

//---------------------lucia knitting fiend-------------------
function doTheTransfer(pForm,qForm,filled,p,q){
 // Transfer the data from pForm to qForm.
 //
 

var pLCf1=parent.left.document.ControlForm
var p1= parent.content.document.sweater_data
var selectedLeft,nameRight

  var numLeft = pForm.elements.length 
  var numRight = qForm.elements.length

pLCf1.status.value = "Wait for transfer."

 for (var i =0; i<numLeft; i++){    /* 1 i loop */

       var nameLeft = pForm[i].name
       var valueLeft = pForm[i].value

     for (var j = 0; j<numRight; j++) {  /* 2 j loop */
       nameRight = qForm[j].name
        if(nameRight == nameLeft) {   /* 3  same name */
            var boxJ= ( (qForm[j].type == "text") || 
                        (qForm[j].type == "hidden") || (qForm[j].type == "textarea") )

            var boxI= ( (pForm[i].type == "text") || 
                      (pForm[i].type == "hidden") || (pForm[i].type == "textarea") )

            if( boxJ  && boxI ){ qForm[j].value = valueLeft }

            else if( (qForm[j].type == "select-one") && (pForm[i].type == "select-one") ){ 
              qForm[j].selectedIndex = pForm[i].selectedIndex  } 

          else if( (qForm[j].type == "select-one" ) && boxI ){    
            if( !isNaN(valueLeft) ){    qForm[j].selectedIndex = valueLeft  } 
            }

           else if(boxJ && pForm[i].type == "select-one"  ){
               qForm[j].value = pForm[i].selectedIndex }

        } /* 3  same name */

}}  /* 2,1 */

p.document.bgColor =savedColor
q.document.bgColor =filledColor

if(p == parent.content){pLCf1.status.value = "Saved"
           p.document.sweater_data.filled.value =filled}
if (p == parent.left){pLCf1.status.value = "Reverted"
           q.document.sweater_data.filled.value =filled} 

inOrCm()  
checkFormComplete() // will also change color.
}
//-----------liljegren-----------------------------
//----------------------------------------
function checkStatusOfMods(){
var pLCf2=parent.left.document.ControlForm

var proceed = "proceed";
if(pLCf2.status.value == "modified" ){pLCf2.status.value = "warned"
alert(notSavedMssg);
proceed = "false"; 
}
if(pLCf2.status.value == "neither" ){pLCf2.status.value = "warned"
// alert("Please do *something* before clicking submit!  I suggest you select 'Estimate' and follow the directions.")
proceed = "false"}

return proceed
}
//----------------------------------------
function hitSubmit(){
var q = parent.left.document.ControlForm 

     // format of call: makeCookie(name,pForm,minutes,speak)
     // Make a cookie from the form.  Also make fill the cookieName cookie.
makeCookie(q.cookieName.value,q,q.minutes.value,"no")  // in GenCookie.js
createAndTestCookie('cookieName',q.cookieName.value,q.minutes.value,"create","no")

/* which ever is zeroth choice targets the left.  All others target the right. */
var proceed = checkStatusOfMods()  // checks whether there are unsaved mod.
if(proceed == "proceed" ){ /* 1*/
     proceed = checkFinalSubmit()  // checks if neck,chest and hip are filled in and gauge
      if(proceed == "ok" ){ /* 2*/

       /* -----TRUE ONLY ----------*/
           q.action =q.whichScript.options[q.whichScript.selectedIndex].value
        if(q.whichScript.selectedIndex == 1) {/* 3*/
        q.target = "left"
parent.content.document.write('<body text="#000033" bgcolor="#CCCCFF" link="#8A0009" vlink="#8C8C8C" alink="#8A0009" font face="arial">')
parent.content.document.write(estMessage) 
parent.content.document.close()
return true
}/* 3*/

        else if(q.whichScript.selectedIndex == 2) {/* 3*/
         q.target ="blank";
         var allFilled = checkSubmitOk() // checks if everything is filled in. warns.
if(allFilled = "ok"){
     parent.content.document.write('<body text="#000033" bgcolor="#CCCCFF" link="#8A0009" vlink="#8C8C8C" alink="#8A0009" font face="arial">')
      parent.content.document.write(patCreateMssg) 
      parent.content.document.close()
     return true}
else {return false} }/* 3*/

       else{  /* 3*/
              q.target ="blank";
              alert(" Select 'Estimate' or 'Final Pattern' before hitting 'Submit'!  ")
           return false
      }/* 3*/
   } /* 2*//* -----close the 'ok' if----------*/
   else {return false}
} /* 1*//* -----close the Proceed if----------*/

else {return false}

}
//----------------------------------------
function estOrPatternDir(){
var q = parent.left.document.ControlForm 

if(q.status.value == "modified" ){
     alert(notSavedMssg); 
    q.status.value = "warned"
    q.SelectForm.selectedIndex = 0}
else{
     q.target = "left"
     if(q.whichScript.selectedIndex == 1 ){q.target = "left" }
     else if(q.whichScript.selectedIndex == 2){q.target ="content"}
     if(q.Beginner.selectedIndex==0){ beginnerMessg() } 
}}

//----------------------------------------
function clickStyle(value){
var q = parent.left.document.ControlForm 
var pLCf2=parent.left.document.ControlForm
if(pLCf2.status.value == "modified" ){alert(notSavedMssg); pLCf2.status.value = "warned"}
else{setContent(value); 
      q.SelectForm.selectedIndex = 0 }
}
//----------------------------------------
function beginnerMessg(){
/*--Write directions for beginners---*/

var q = parent.left.document.ControlForm 

parent.content.document.write('<body text="#000033" bgcolor="#FFFFDD" link="#8A0009" vlink="#8C8C8C" alink="#8A0009" font face="arial">')

if(q.whichScript.selectedIndex == 1 ){/*2*/
parent.content.document.write(estDirMssg)
}/*2 C*/

else if(q.whichScript.selectedIndex == 2)  {/*2*/
parent.content.document.write(finalPatMssg)
}/*2 C*/

else{ /*2*/
parent.content.document.write(" Please select 'Estimate' or 'Final Pattern'.  ")
}/*2 C*/
parent.content.document.close()

}

//-------------------------knittingfiend---------------
function checkFormComplete(){

var p = parent.content.document.sweater_data
var q=parent.left.document.ControlForm
pD = parent.content.document

//--- find last part of URL

var string = pD.location.pathname
var lastSlash = string.lastIndexOf('/')
var frag = string.substring(lastSlash+1,string.length)
            // p.filled.value = p.filled.value + frag 
      //----

var ok = "ok"
var numEmpty =0

setEssentials()
var estEssential = whichEstEssential(frag)
var num=estEssential.length

// note:blanks must all be 6 x's.
for (var i =0; i<num; i++){   /* 1 */ 
      if( isNaN( estEssential[i].value ) &&  estEssential[i].value != notRequired) { ok ="not"; numEmpty = numEmpty +1;
        //    p.filled.value = p.filled.value + " " + i + " " + estEssential[i].name
 }
}/* 1 */ 
if( ok != "ok") { p.filled.value = p.filled.value + 
" This form is missing " + numEmpty + " required entries. "   
parent.content.document.bgColor = incompleteColor  }

else {p.filled.value = p.filled.value + "The form is complete. "  }

}
function checkSubmitOk(){
setEssentials()
var ok = "ok"
var newOk ="ok"
var string = "These forms are not filled out: "

for(var i=0; i<headNeckEssentials.length; i++){
if( isNaN( headNeckEssentials[i].value ) &&  headNeckEssentials[i].value != notRequired) { var newOk ="not";}}
if(newOk == "not"){ newOk  = "ok", ok="not", string += "Neck, " }

for(var i=0; i<weltEssentials.length; i++){
if( isNaN( weltEssentials[i].value ) &&  weltEssentials[i].value != notRequired) { 
newOk ="not";  }}
if(newOk == "not"){ newOk  = "ok", ok="not", string += "Welts, "}

for(var i=0; i<gaugeEssentials.length; i++){
if( (isNaN( gaugeEssentials[i].value ) &&  gaugeEssentials[i].value != notRequired) || gaugeEssentials[i].value <= 0 ){ 
newOk ="not";}}
if(newOk == "not"){ newOk  = "ok", ok="not" ;string += "Gauge, "  }

for(var i=0; i<torsoWidthEssentials.length; i++){
if( isNaN( torsoWidthEssentials[i].value ) &&  torsoWidthEssentials[i].value != notRequired) { 
newOk ="not"; }}
if(newOk == "not"){ newOk  = "ok", ok="not"; string += "Width, " }

for(var i=0; i<armEssentials.length; i++){
if( isNaN( armEssentials[i].value ) &&  armEssentials[i].value != notRequired) { 
newOk ="not"; }}
if(newOk == "not"){ newOk  = "ok", ok="not"; string += "Sleeve, " }

for(var i=0; i<torsoLengthEssentials;i++ ){
if( isNaN( torsoLengthEssentials[i].value ) &&  torsoLengthEssentials[i].value != notRequired ) { 
newOk ="not"; }}
if(newOk == "not"){ newOk  = "ok", ok="not"; string += "Length,  " }

for(var i=0; i<styleEssentials.length; i++){
if( isNaN( styleEssentials[i].value ) &&  styleEssentials[i].value != notRequired) { 
newOk ="not"}}
if(newOk == "not"){ newOk  = "ok", ok="not"; string += "Styles. "  }

if(ok != "ok"){ string += "The button form may or may not be filled out.";
alert(string) }
return ok
}
//===================================lucia
function whichEstEssential(frag){
var estEssential = new Array ()

/*  <option  VALUE="1" >Neck <option VALUE="10">Torso Length
<option VALUE="4">Torso Width <option VALUE="5">Sleeve
<option VALUE="2">Welts <option VALUE="7">Button Band
<option VALUE="6">Notes   8 is style, 3 is gauge*/
         //----
   if(frag ==pageArray[1]) {estEssential = headNeckEssentials}
   else if(frag ==pageArray[2]) {estEssential = weltEssentials}
   else if(frag ==pageArray[3]) {estEssential = gaugeEssentials}
   else if(frag ==pageArray[4]) {estEssential = torsoWidthEssentials}
   else if(frag ==pageArray[5]) {estEssential = armEssentials}
   //-- 6 is always complete.
   else if(frag ==pageArray[6]) {estEssential = notesEssentials}
   else if(frag ==pageArray[7] ) {estEssential = buttonBandEssentials}
   else if(frag ==pageArray[8]){estEssential = styleEssentials}
   //-- 9 is always complete.
    else if(frag ==pageArray[9]) {}
   else if(frag ==pageArray[10]) {estEssential = torsoLengthEssentials}
   else {alert("Problem verifying whether page is complete = " + frag);}

return estEssential
}
//---------------knitting fiend -----------------------
function setEssentials(){
var p = parent.content.document.sweater_data
var q=parent.left.document.ControlForm

styleEssentials =  new Array (q.chest, q.hip, q.neck_circ)

armEssentials =  new Array (q.bicep, q.elbow_circ,q.wrist_circ,
q.e_wrist,q.e_elbow_circ,q.e_bicep,q.e_cuff,q.snug_scye,q.e_snug_scye_tot,
q.classic_armhole,q.e_armhole_depth,q.arm_length,q.e_arm_length,q.fore_arm,
q.e_shoulder)

gaugeEssentials = new Array (q.rows_rib, q.length_rib, q.stitches_rib,
q.width_rib,q.rows_main,q.length_main,q.stitches_main,q.width_main,
q.rows_sleeve,q.length_sleeve,q.stitches_sleeve,q.width_sleeve,
q.rows_button,q.length_button,q.stitches_button,q.width_button)

buttonBandEssentials =  new Array (q.button_diam, q.num_button,q.custom_band)

headNeckEssentials = new Array (q.neck_circ, q.e_neck_depth,
q.e_neck_depth,q.e_back_neck_depth,q.e_neck_width)

torsoLengthEssentials =  new Array (q.back_waist,
q.sweater_length, q.waist_to_hip,q.bust_depth)

torsoWidthEssentials = new Array (q.waist, q.e_waist, q.hip,q.e_hip,
q.chest,q.e_chest,q.e_hip_rib,
q.natural_shoulder,q.e_shoulder,q.shoulder_slope)

weltEssentials = new Array(q.neck_welt_depth,q.bottom_welt_depth,
q.wrist_welt_depth,q.sweater_length)

notesEssentials = new Array()

}

//------ prefills the lengths which are not in the cookie or left form.
function calcLength(){
var q = parent.left.document.ControlForm;
q.d_back_waist.value=parent.left.add(q.back_waist.value,0)
var neckwidth = parent.left.divide(q.neck_circ.value,q.width_fact.value)
var back_neck_depth = parent.left.mult(neckwidth,q.back_depth_fact.value)

var ShouldToWaist =parent.left.add(q.d_back_waist.value,back_neck_depth)

q.WaistToBust.value =  parent.left.sub(ShouldToWaist,q.bust_depth.value)
q.BelowWaist.value=parent.left.sub(q.sweater_length.value,ShouldToWaist)
q.AboveHip.value =parent.left.sub(q.waist_to_hip.value, q.BelowWaist.value)
}
