  // Online Flashcards JavaScript application
  // 
  // Please click the 'back' button on your browser right now.
  //
  // (c)1997, Andy Lyons, http://nersp.nerdc.ufl.edu/~alyons
  // All Rights Reserved
  // Feel free to use but maintain this portion for proper creditting

  function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) { 
     this[i] = "" }
     return this
  }

  function ReadyMove() {
    if (document.myform.viewfirst.checked) {
      if ((document.myform.displayo[0].checked) && (document.myform.csent.value=="")) {
        ShowC()
        return false
      } else if ((document.myform.displayo[1].checked) && (document.myform.esent.value=="")) {
        ShowE()
        return false
      } else {
        return true
      }
    } else {
      return true
    }
  }

  function NextSent() {
   if (ReadyMove()) {
     if (CurNum < MaxSent) {
      CurNum = CurNum + 1    
      ShowSent(true)
     }
     else {
      alert("Sorry, can't move forward. That's the last one.")
     }    
   }
  }

  function TagAndNext() {
   if (ReadyMove()) {
    if (CurNum < MaxSent) {
     Tagged[Ord[CurNum]] = !Tagged[Ord[CurNum]]
     if (Tagged[Ord[CurNum]]) {
       TotalTagged += 1
     } else {
       TotalTagged -= 1
     }
     CurNum = CurNum + 1
     ShowSent(true)
    }
    else {
     alert("Sorry, can't move forward. That's the last one.")
    }    
   }
  }

  function PrevSent() {
   if (ReadyMove()) {
    if (CurNum > 1) {
     CurNum = CurNum - 1    
     ShowSent(true)
    }
    else {
     alert("Sorry, can't go back. You're at the first one.")
    }    
   }
  }

  function FirstSent() {
    CurNum = 1    
    ShowSent(true)
  }

  function LastSent() {
    CurNum = MaxSent    
    ShowSent(true)
  }

  function ShowC() {
    if ((document.myform.displayo[1].checked) || (document.myform.displayo[2].checked)) {
      alert("The Swahili translation is already displayed.")
    } else {
      document.myform.csent.value = q[Ord[CurNum]]
    }
  }

  function ShowE() {
    if ((document.myform.displayo[0].checked) || (document.myform.displayo[2].checked)) {
      alert("The English translation is already displayed.")
    } else {
      document.myform.esent.value = a[Ord[CurNum]]
    }
  }

  function ShowSent(wipe) {
    if (document.myform.displayo[1].checked) {
      document.myform.csent.value=q[Ord[CurNum]]
      document.myform.esent.value="" 
    }

    else if (document.myform.displayo[0].checked) {
      document.myform.csent.value=""
      document.myform.esent.value=a[Ord[CurNum]]
    }

    else {
      document.myform.csent.value=q[Ord[CurNum]]
      document.myform.esent.value=a[Ord[CurNum]]
    }

    if (wipe) {
      document.myform.goodtry.value = ""
    }    

    document.myform.txt_counter.value=CurNum + " of " + MaxSent 
    document.myform.tagged.checked = Tagged[Ord[CurNum]]
    document.myform.txt_tagged.value = TotalTagged
  }

  function Alphabetize(LeftComparison) {
    document.myform.esent.value="Alphabetizing, please wait..."
    document.myform.esent.focus()
    document.myform.esent.select()
    document.myform.csent.focus()
    var Inserted = false
    var Comparison = false
    Ord[1] = 1

    for (var i = 2; i <= MaxSent; i++) { 
      document.myform.csent.value = (MaxSent - i)
      document.myform.csent.select()
      InsertAfterThis = 0
      for (var j = 1; j <= (i-1); j++) {
        if (LeftComparison) {
          Comparison = (q[i].toUpperCase() > q[Ord[j]].toUpperCase())
        } else {
          Comparison = (a[i].toUpperCase() > a[Ord[j]].toUpperCase())
        }

        if (Comparison) {
          InsertAfterThis = j
        }     
      }
        
      Inserted = false
      for (var j = (i+1); j >= 1; j--) { 
        if (!Inserted) {
          if (j == (InsertAfterThis + 1)) {
            Ord[j] = i
            Inserted = true
          } else {
            Ord[j] = Ord[j-1]
          }
        }          
      }
    }

    // If we're resorting all the words, then reset OrdTemp
    if (MaxSent == NumWords) {
      for (var i = 1; i <= MaxSent; i++) { 
        OrdTemp[i] = Ord[i]
      }
    }
    CurNum = 1
    document.myform.csent.value = "DONE"
    document.myform.csent.select()
    ShowSent(true)
  }

  function Randomize() {
    document.myform.esent.value="Shuffling, please wait..."
    document.myform.esent.focus()
    document.myform.esent.select()
    document.myform.csent.focus()

     for (var i = 1; i <= MaxSent; i++) { 
       document.myform.csent.value = (MaxSent*2) - i
       document.myform.csent.select()
       NumTaken[i] = 0
       OrdTemp2[i] = Ord[i]
     }

     today = new Date();
     seed = today.getTime();

     for (var i = 1; i <= MaxSent; i++) { 
       seed = Math.round(Math.abs(Math.sin (seed)*1000000)) 
       OrdBuff[i] = 1 + seed % MaxSent
       NumTaken[OrdBuff[i]] += 1
     }
 
     var CurOrd = 1
     var CurCount = 1

     while (CurCount <= MaxSent) {
       document.myform.csent.value = (MaxSent - CurCount)
       document.myform.csent.select()

       if (NumTaken[CurCount] > 0) { 
       
         for (var k = 1; k <= MaxSent; k++) { 
           if (OrdBuff[k] == CurCount) {
             // added Ord[CurOrd] below
             Ord[k] = OrdTemp2[CurOrd]
             CurOrd += 1
           }
         }   
       }
       CurCount += 1
     }

     if (MaxSent == NumWords) {
       for (var i = 1; i <= MaxSent; i++) { 
         OrdTemp[i] = Ord[i]
       }
     }

     CurNum = 1
     document.myform.csent.value = "DONE"
     document.myform.csent.select()
     ShowSent(true)
  }

  function TagMe() {
    Tagged[Ord[CurNum]] = document.myform.tagged.checked
    if (document.myform.tagged.checked) {
      TotalTagged += 1 
    } else {
      TotalTagged -= 1
    }
    document.myform.txt_tagged.value = TotalTagged
  }

  function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
      endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  }

  function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break; 
    }
    return null;
  }

  function SetCookie (name,value,expires,path,domain,secure) {
    document.cookie = name + "=" + escape (value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  }

  function SupportCookies() {
    exp_date = new Date()
    exp_date.setTime(exp_date.getTime() + (6 * 30 * 24 * 60 * 60 * 1000))
    SetCookie ("test","OK",exp_date,location.pathname);
    if (GetCookie("test")) {
      return true
    } else {
      return false
    }
  }

  function SaveToDisk() {
    ToSaveOrNot = true
    if (!SupportCookies()) {
      alert("Sorry, your web browser and/or network configuration does not seem to support the save to disk feature.")
      ToSaveOrNot = false

    } else if (TotalTagged == 0) {
      alert("Sorry, there are no tagged flashcards to save right now.")  
      ToSaveOrNot = false
    } 

    else if (GetCookie("tags")) {
      if (!confirm("There are some flashcard tags from this web page which have already been saved to disk. Replace those ones with these new ones?")) {
        ToSaveOrNot = false        
      }         
    }

    if (ToSaveOrNot) {
      tagbuild = ""
      for (var i = 1; i <= MaxSent; i++) { 
        if (Tagged[i]) {
          tagbuild += i + "_"
        }
      }
      tagbuild = tagbuild.substring(0,tagbuild.length-1)
      exp_date = new Date()
      exp_date.setTime(exp_date.getTime() + (6 * 30 * 24 * 60 * 60 * 1000))
      SetCookie ("tags",tagbuild,exp_date, location.pathname);
      alert(TotalTagged + " flashcards tags have been saved to disk. Now, when you come back to this web page another time (on this same computer), you can retag these flashcards again by clicking on 'Load tags from disk'.");
    }
  }

  function LoadFromDisk() {
    ToTagOrNot = true
    tagbuild = GetCookie("tags")
    if (!SupportCookies()) {
      alert("Sorry, your web browser and/or network configuration does not seem to support the load from disk feature.")
      ToSaveOrNot = false
    } else if (!tagbuild) {
      alert("Sorry, no tags from this web page have been previously saved to disk (on this computer).")
      ToTagOrNot = false
    }

    else if (TotalTagged != 0) {
      if (!confirm("There are already " + TotalTagged + " tagged flashcards on this web page. Do you want to untag these and then retag the ones that have been previously saved on disk?")) {
        ToTagOrNot = false;
      } else {
        SetTags(false)
        if (MaxSent != NumWords) {
          ShowAll()
        }
      }
    }

    if (ToTagOrNot) {
      tagsarray = tagbuild.split("_");
      for (var i = 0; i <= MaxSent-1; i++) { 
        if (tagsarray[i] == null) {
          break
        } else {
          TotalTagged += 1
          Tagged[tagsarray[i]] = true
        }
      }
      ShowSent(false)
      alert(TotalTagged + " flashcards have been retagged from disk. Click 'View tagged only' to go through them.")
    }
  }

  function ToggleTags() {
    for (var i = 1; i <= MaxSent; i++) { 
      Tagged[Ord[i]] = !Tagged[Ord[i]]
    }
    TotalTagged = MaxSent - TotalTagged
    ShowSent(false)
  }

  function ShowTags() {
    if (TotalTagged == 0) {
       alert("There must be a least one tagged flashcard before you can hide all the others.")
    } else {

    // Reset the Ord array according to the Tagged setting
    var j = 0
    for (var i = 1; i <= MaxSent; i++) { 
      if (Tagged[Ord[i]]) {
        j += 1
        OrdBuff[j] = Ord[i]
      }
    }
    MaxSent = j
    for (var i = 1; i <= MaxSent; i++) { 
      Ord[i] = OrdBuff[i]
    }

    CurNum = 1
    ShowSent(true)
  } // TotalTagged <> 0
  }

  function ShowAll() {
    if (MaxSent == NumWords) {
      alert("All the flashcards are already visible.")
    } else {
      MaxSent = NumWords
      for (var i = 1; i <= MaxSent; i++) { 
        Ord[i] = OrdTemp[i]
      }
      CurNum = 1
      ShowSent(true)
    }
  }

  function SetTags(tf) {
    for (var i = 1; i <= MaxSent; i++) { 
      Tagged[Ord[i]] = tf
    }
    if (tf) {
      TotalTagged = MaxSent
    } else {
      TotalTagged = 0
    }
    ShowSent(false)
  }

  function WindowList() {
    if (TotalTagged == 0) {
       alert("This function displays all the tagged words as a list. You must have at least one record tagged. Tag one or more words and then try again.")
    } else {
       msg=open("","TaggedWords"+NumWindows, "toolbar=yes,menubar=yes,scrollbars=yes,location=yes,status=yes");
       NumWindows++
       var QuotChar = '"'
       msg.document.writeln("<HEAD><TITLE>Tagged Words</TITLE></HEAD><BODY BGCOLOR=#FFFF80>");
       msg.document.writeln("<p><b>" + TotalTagged + " of " + NumWords + " records from <A HREF=" + QuotChar + "javascript:alert('To go back to the flashcard page, close this window.')" + QuotChar + ">" + document.title + "</A>.</b></p><TABLE BORDER=1 WIDTH=100% CELLPADDING=2>");

       for (var i = 1; i <= MaxSent; i++) { 
         if (Tagged[Ord[i]]) {
           msg.document.writeln("<TR VALIGN=TOP><TD>" + q[Ord[i]] + "</TD><TD>" + a[Ord[i]] + "</TD></TR>");    
         }
       }
       msg.document.writeln("</TABLE></BODY>");
       msg.document.close()
    }
  }

  var MaxSent = NumWords
  var NumWindows = 1
  q = new MakeArray(MaxSent)
  a = new MakeArray(MaxSent)
  InitVars()

  var CurNum = 1
  var TotalTagged = 0

  NumTaken = new MakeArray(MaxSent)
  Ord = new MakeArray(MaxSent)
  Tagged = new MakeArray(MaxSent)
  OrdTemp = new MakeArray(MaxSent) 
  OrdTemp2 = new MakeArray(MaxSent)
  OrdBuff = new MakeArray(MaxSent)

  for (var i = 1; i <= MaxSent; i++) { 
    Ord[i] = i
    Tagged[i] = false
    OrdTemp[i] = i
  }

