escSpace = "xBLANKx";

/*----  Looks for all cookies the page can see and displays the names in box---- */
function showCookieNames(box) {

if(document.cookie!=""){
  var thisCookie = document.cookie.split(";")
  var allCookieNames = "";
  for( var i=0; i<thisCookie.length; i++){ 
     var j=i+1
     var cookieName = thisCookie[i].split("=")[0]    // splits on the equal.
     allCookieNames +=" " + j + ")" + cookieName 
      } //end for loop
  box.value = "I can see cookies with these names: " + allCookieNames + 
  ". ";
  } // if cookie part.
else {box.value = "I found no cookies."}
}

//=====  Read and split a named cookie. Fill pForm.
function readCookAndFill(name,pForm,first){
if(document.cookieForm){
      document.cookieForm.cookieOut.value = 
      "I looked for a cookie called " + name + "."  }

var newName= removeWhiteSpace(name);
var valueCookie = Cookie.read(newName);      
    if(valueCookie){
                 
    var formTriads= valueCookie.split("|")
    
    if( first == "yes"){ 
       alert ("I found a cookie called " + name 
         + " and filled the design form with " 
         + formTriads.length +" design values.") }
         
   if(document.cookieForm){document.cookieForm.cookieOut.value +=
     " I filled the form to the left with these" + formTriads.length + "values: " }  
  
     for (var i=0; i<formTriads.length-1; i++){
         var cokNum = formTriads[i].split(":")[0]
         var type1 = formTriads[i].split(":")[1]
         var value1 = replaceSpaces(formTriads[i].split(":")[2],escSpace)
         
      if(document.cookieForm){     
           document.cookieForm.cookieOut.value +=  cokNum +":"+ type1 +":"+ value1 + "|"}       
       
     if(type1  == "H" || type1 == "T" ) { pForm[cokNum].value = value1 } 
     else if(type1 = "S"){pForm[cokNum].selectedIndex = value1}   
     }/* for */  
     } /* valueCookie if */
       
else if(document.cookieForm){ document.cookieForm.cookieOut.value += " The cookie was empty."}

} /* function ends */

/* ---- -----------------------   --------------------     ----------  ---- */
/* ---- Make a design cookie using my design format. ---- */
      // -- This also looks for the last relevent entry in the form. 

function makeCookie(cookieName,pForm,minutes,speak){
    // makes cooke out of the contents of pForm
   // expires in minutes.
   // has an option of printing out stuff

if(document.cookieForm){
document.cookieForm.cookieOut.value="Making cookie called " + cookieName 
+ " with "+ pForm.elements.length +" elements. The cookie will last "
+ minutes + " minutes.";
}

var numLeft = pForm.elements.length 
var wholeCookie= "";

 for (var i =0; i<numLeft; i++){    
          var CookPiece = pieceOfCookie(pForm[i],i)
        if(CookPiece == "XXLASTXX"){ i = numLeft; }
        else if(CookPiece != "No"){   wholeCookie +=   CookPiece; }
} 


// finish writing cookie.
if(document.cookieForm){ document.cookieForm.cookieOut.value+= "This is the whole cookie: "
+ wholeCookie +" ."}


createAndTestCookie(cookieName,wholeCookie,minutes,"create",speak)

}
// 1====================


function pieceOfCookie(entry,i){
  // ---------------------------
// make a piece of cookie. ------------------------
//  Cookie pieces are piece = name*type*value
// for text boxes, I need to take out the white space and stick in something
// else.  I'll stick in under scores if I can.

       var cokName = entry.name 
       var cokType = entry.type
       var cokVal = ""
  
        if( cokType == "hidden" ){ 
            cokVal = removeWeird(entry.value) ; cokType="H";} 
       else if(cokType == "text"){ 
         cokVal = removeWeird(entry.value) ; cokType="T";}
       else if( cokType == "select-one") { 
              cokVal = entry.selectedIndex; cokType="S";}
       else {  cokVal="wait"}

cokVal=fillSpaces(cokVal,escSpace)

var  cokPiece = "No"
 if(cokName == "XXLASTXX") {var cokPiece = "XXLASTXX"} 
else if (cokVal != "wait"){
  var cokPiece = i + ":" + cokType +":" + cokVal+"|"}
if(document.cookieForm){document.cookieForm.cookieOut.value += cokPiece + " * ";}

return cokPiece
}  



/*--------------------Print a cookie to the  box. ------*/
function printCookietoBox(cookieName,box){
if(document.cookie != ""){
var valueCookie = Cookie.read(cookieName);
   if(valueCookie){
   valueCookie = replaceSpaces(Cookie.read(cookieName),escSpace);
       box.value="cookie name = " +cookieName + "; cookie value = " + valueCookie;}
  else {box.value="I found some cookies, but I found no cookie named " + cookieName + ".";}
}
else{ box.value = "I found no cookies at all!"}
}


/*------------------------------General Cookie functions ----------  */
function killCookies(){
this.document.cookieForm.cookieOut.value = "Blank";
/*         learns cookie names, then kills them all.  */
var allCookies = document.cookie.split(";") //splits intocookies
var string = "";
  for( var i=0; i<allCookies.length; i++){ 
          var nameThisCookie = replaceSpaces(allCookies[i].split("=")[0])  
         createAndTestCookie(nameThisCookie,"",-30,"kill")
   string +=  nameThisCookie + " "
   } //end for loop
if(string != "") alert(" I found these cookies, and tried to delete them: " + string) ;
}
/*------------------------------Create a cookie with a given name ----------  */
function createAndTestCookie(cookieName,value,minutes,make,speak){
var newName = removeWhiteSpace(cookieName)
minutes = Math.max(5,minutes) // don't let it expire instantly.
minutes = Math.min(530000,minutes) // don't let it last more than a year.
var days = minutes/(60*24)

/* I think I need to make the path longer with the new site. Aug. 2002 */

if(make == "kill") {
     cookieMade = Cookie.make(newName, value, {expires: -1,path:'/knittingfiend/OrderForms/'}) ;}
else{
   cookieMade = Cookie.make(newName, value, {expires: days,path:'/knittingfiend/OrderForms/'});}

  if(make == "create" && speak == "yes"){
      var word = " days. "
      if (days < 0.1) { var word = " minutes.";  days = Math.round(days *60 *24)}
      else if (days < 1) { var word = " hours."; days = Math.round(days *24)}
      else if (days > 31) { var word = " months"; days = Math.round(days/30.4)}
      else{days = Math.round(days)}
      
      var firstString = 'Attempting to ' 
      var secondString = ' It will expire in ' + days + word
      var thirdString = ' Please adjust browser to accept cookies.'
      
         if(!cookieMade){alert( firstString + make +' a cookie called ' + newName +'.'+ secondString + thirdString) }
         else           {alert(' Thanks for taking a cookie called ' + newName +'. ' +  secondString) }
  }

}

/*------------------------------General Cookie functions ----------  */
// note: value is escaped when made and unescaped when read.  
// I don't need to repeat that elsewhere.
// I need to modify from original by NOT escaping the value 
// make:function(name, value, {other stuff})
// Otherwise, the cookies are too long because the escape sequences are too long.

var Cookie = {
pref:'',last:null,
cnfg:function(a1) {
        var Bool={secure:1}, text='';
        if (''+a1!==a1) for (var k in a1) if (a1[k]) text += '; '+(
                Bool[k] ? k : k+'='+(
                        k!='expires'||isNaN(a1[k]) ? a1[k] : this.date(a1[k])
                )
        );
        return this.pref = text||a1||text;
},
make:function(a1,a2,a3) {
        document.cookie = this.last=a1+'='+a2+this.cnfg(a3);
        return this.read(a1) == a2;
},
date:function(a1) {
        var D = new Date();
        D.setTime(D.getTime()+(a1||0)*86400000);
        return D.toGMTString();
},
read:function(a1) {
        var F=' '+document.cookie+';', S=F.indexOf(' '+(a1));
        return S==-1 ? null : 
                F.substring(a1=S+a1.length+2,F.indexOf(';',a1)
        );
}
}
/*--------------------------END General Cookie functions ----------  */
function removeWhiteSpace(theString){
var newString = ""
var pieces= theString.split(" ")

if(pieces.length < 2){ newString = theString}
else {
  for (var i =0; i<pieces.length; i++){ newString += pieces[i]} 
  } // for, if<2
   
return newString
}
//----------------------------------------
function replaceSpaces(theString,space){

var newString = ""
var pieces= theString.split(space)

if(pieces.length < 2){ newString = theString}
else {
  for (var i =0; i<pieces.length; i++){ 
          if(i <pieces.length -1){    newString += pieces[i] + " "}
          else {newString += pieces[i]}
   } } // for, if<2
 
return newString
}
//----------------------------------------
function removeWeird(theString) {
// This function removes lots of strange things 
// from theString.  charAt must remove everything
// The blank space isn't *supposed* to be there.  But, it exists on my home machine, so who knows?
//

var newString = ""
//var re = /\W/ ; // regular expression  - problem, this removes decimal places
// and lots of other things I don't necessarily want to remove.

var re = /[~!@$#$%&*|:;<>`+=_]/ ;

// This seems stupidly inefficient.  But if I cut and past
// some lines that work in for loop outside the look, they just don't work!
// (I don't mean because 'i' isn't defined either.
// it's as though theString isn't recognized as a string.
//

for (var i=0; i<theString.length; i++){
    var subString=theString.charAt(i);
    var junk2 = subString.search(re);
  
    if(junk2 == -1){newString +=  subString}
    else {newString += junk2} // this will generally replace the crud with a zero.

}
    
    
    return newString
}//removeWeird
//
//----------------------------------------
function fillSpaces(theString,space) {
// This function removes blank space from the string.
// The blank space isn't *supposed* to be there.  But, it exists on my home machine, 
// so who knows?
//

var newStringa = ""
    for (var i=0; i<theString.length; i++){
			       	if(theString.charAt(i) != " ") {newStringa += theString.charAt(i)} 
         else {newStringa += space} // end for
    }  // for
    return newStringa
}