running = 0;
current_mode = "mul";
oper = " × ";
current_problem_number = 0;
number_of_problems = 12;
lastnum1 = 1;
lastnum2 = 1;
CongratString = "";
cookies_enabled = 0;
initials_request = "";


function preStartUp()
{
   if(testCookiesEnabled() == 1)
   {
      cookies_enabled = 1;
      initials_request = "Please enter your initials\(3\).";
   }
   else
   {
      alert("It would seem that cookies are not enabled in your browser. Until they are enabled, some scorekeeping features will not function.");
   }

   startUp();
}


function startUp()
{

   window.document.problemform.reset();
   window.document.answerform.reset();
   window.document.op.reset();

   window.document.problemform.q.value = "a + b = ";
   window.document.answerform.a.value = "c";
   window.document.op.mode[2].checked = true;


   if(cookies_enabled == 1)
   {
      //have cookies been set sometime in the past?
      if(getCookie("tina_flag") == "yeppers")
      {
         a1 = deCodeScore('tina_a1'); a2 = deCodeScore('tina_a2'); a3 = deCodeScore('tina_a3');
         s1 = deCodeScore('tina_s1'); s2 = deCodeScore('tina_s2'); s3 = deCodeScore('tina_s3');
         m1 = deCodeScore('tina_m1'); m2 = deCodeScore('tina_m2'); m3 = deCodeScore('tina_m3');
         d1 = deCodeScore('tina_d1'); d2 = deCodeScore('tina_d2'); d3 = deCodeScore('tina_d3');

         inita1 = deCodeInitial('tina_a1'); inita2 = deCodeInitial('tina_a2'); inita3 = deCodeInitial('tina_a3');
         inits1 = deCodeInitial('tina_s1'); inits2 = deCodeInitial('tina_s2'); inits3 = deCodeInitial('tina_s3');
         initm1 = deCodeInitial('tina_m1'); initm2 = deCodeInitial('tina_m2'); initm3 = deCodeInitial('tina_m3');
         initd1 = deCodeInitial('tina_d1'); initd2 = deCodeInitial('tina_d2'); initd3 = deCodeInitial('tina_d3');
      }
      else
      {
         a1 = 999999; a2 = 999999; a3 = 999999;
         s1 = 999999; s2 = 999999; s3 = 999999;
         m1 = 999999; m2 = 999999; m3 = 999999;
         d1 = 999999; d2 = 999999; d3 = 999999;
         inita1 = " "; inita2 = " "; inita3 = " ";
         inits1 = " "; inits2 = " "; inits3 = " ";
         initm1 = " "; initm2 = " "; initm3 = " ";
         initd1 = " "; initd2 = " "; initd3 = " ";

         setAllCookies();
      }
   }
   else
   {
      a1 = 999999; a2 = 999999; a3 = 999999;
      s1 = 999999; s2 = 999999; s3 = 999999;
      m1 = 999999; m2 = 999999; m3 = 999999;
      d1 = 999999; d2 = 999999; d3 = 999999;
      inita1 = " "; inita2 = " "; inita3 = " ";
      inits1 = " "; inits2 = " "; inits3 = " ";
      initm1 = " "; initm2 = " "; initm3 = " ";
      initd1 = " "; initd2 = " "; initd3 = " ";
   }

   normalizeAttributes();
   writeFloater();

}


function setAllCookies()
{
   setCookie("tina_flag", "yeppers", expire_date);

   enCode('tina_a1',a1,inita1)
   enCode('tina_a2',a2,inita2)
   enCode('tina_a3',a3,inita3)

   enCode('tina_s1',s1,inits1)
   enCode('tina_s2',s2,inits2)
   enCode('tina_s3',s3,inits3)

   enCode('tina_m1',m1,initm1)
   enCode('tina_m2',m2,initm2)
   enCode('tina_m3',m3,initm3)

   enCode('tina_d1',d1,initd1)
   enCode('tina_d2',d2,initd2)
   enCode('tina_d3',d3,initd3)
}



function removeAllCookies()
{
   if(cookies_enabled ==1)
   {
      if(confirm("This will wipe out all high scores. Are you sure you want to do that?"))
      {
         delCookie("tina_flag");

         delCookie("tina_a1");
         delCookie("tina_a2");
         delCookie("tina_a3");

         delCookie("tina_s1");
         delCookie("tina_s2");
         delCookie("tina_s3");

         delCookie("tina_m1");
         delCookie("tina_m2");
         delCookie("tina_m3");

         delCookie("tina_d1");
         delCookie("tina_d2");
         delCookie("tina_d3");

         startUp();
      }
   }
   else
   {
      a1 = 999999; a2 = 999999; a3 = 999999;
      s1 = 999999; s2 = 999999; s3 = 999999;
      m1 = 999999; m2 = 999999; m3 = 999999;
      d1 = 999999; d2 = 999999; d3 = 999999;

      startUp();
   }
}



function normalizeAttributes()
{
   color_a1 = "000000"; color_a2 = "000000"; color_a3 = "000000";
   color_s1 = "000000"; color_s2 = "000000"; color_s3 = "000000";
   color_m1 = "000000"; color_m2 = "000000"; color_m3 = "000000";
   color_d1 = "000000"; color_d2 = "000000"; color_d3 = "000000";
   Ba1 = "xb"; Ba2 = "xb"; Ba3 = "xb";
   Bs1 = "xb"; Bs2 = "xb"; Bs3 = "xb";
   Bm1 = "xb"; Bm2 = "xb"; Bm3 = "xb";
   Bd1 = "xb"; Bd2 = "xb"; Bd3 = "xb";
}




//getRandom(5) returns 0,1,2,3,4
function getRandom(max) {return (Math.floor(Math.random()*max));}


function nada(){;}


function setOp()
{
   if(running == 0)
   {
      if(window.document.op.mode[0].checked == true) { oper = " + "; current_mode = "add"; }
      if(window.document.op.mode[1].checked == true) { oper = " - "; current_mode = "sub"; }
      if(window.document.op.mode[2].checked == true) { oper = " × "; current_mode = "mul"; }
      if(window.document.op.mode[3].checked == true) { oper = " ÷ "; current_mode = "div"; }
   }
   else
   {
      window.document.op.mode[0].checked = false;
      window.document.op.mode[1].checked = false;
      window.document.op.mode[2].checked = false;
      window.document.op.mode[3].checked = false;

      if     (current_mode == "add") { window.document.op.mode[0].checked = true; }
      else if(current_mode == "sub") { window.document.op.mode[1].checked = true; }
      else if(current_mode == "mul") { window.document.op.mode[2].checked = true; }
      else if(current_mode == "div") { window.document.op.mode[3].checked = true; }

      alert("Cannot change mode while program is running. Finish this set, then change mode.");
      window.document.answerform.a.focus();
   }
}


function startProblems()
{
   if(running == 0)
   {
      running = 1;
      window.document.problemform.q.value = "";
      window.document.answerform.a.value = "";
      StartTimeObj = new Date();
      StartTime = StartTimeObj.getTime();
      makeProblems();
   }
   else
   {
      alert("Program is already started.");
      window.document.answerform.a.focus();
   }
}



function makeProblems()
{
   current_problem_number++;

   if(current_problem_number <= number_of_problems)
   {
      if(current_mode == "add")
      {
         num1 = getRandom(11);
         num2 = getRandom(11);
         if((num1 == lastnum1)||(num2 == lastnum2))
         {
            current_problem_number--;
            makeProblems();
         }
         else
         {
            lastnum1 = num1;
            lastnum2 = num2;
            correct_answer = num1 + num2;
            current_problem = num1 + oper + num2 + " = ";
            window.document.problemform.q.value = current_problem;
            window.document.answerform.a.focus();
         }
      }
      else if(current_mode == "sub")
      {
         num1 = getRandom(11);
         num2 = getRandom(11);
         if((num1 == lastnum1)||(num2 == lastnum2))
         {
            current_problem_number--;
            makeProblems();
         }
         else
         {
            lastnum1 = num1;
            lastnum2 = num2;
            sum = num1 + num2;
            correct_answer = num2;
            current_problem = sum + oper + num1 + " = ";
            window.document.problemform.q.value = current_problem;
            window.document.answerform.a.focus();
         }
      }
      else if(current_mode == "mul")
      {
         num1 = getRandom(11);
         num2 = getRandom(11);
         if((num1 == lastnum1)||(num2 == lastnum2))
         {
            current_problem_number--;
            makeProblems();
         }
         else
         {
            lastnum1 = num1;
            lastnum2 = num2;
            correct_answer = num1 * num2;
            current_problem = num1 + oper + num2 + " = ";
            window.document.problemform.q.value = current_problem;
            window.document.answerform.a.focus();
         }
      }
      else if(current_mode == "div")
      {
         num1 = (getRandom(10) + 1);
         num2 = (getRandom(10) + 1);
         if((num1 == lastnum1)||(num2 == lastnum2))
         {
            current_problem_number--;
            makeProblems();
         }
         else
         {
            lastnum1 = num1;
            lastnum2 = num2;
            product = num1 * num2;
            correct_answer = num2;
            current_problem = product + oper + num1 + " = ";
            window.document.problemform.q.value = current_problem;
            window.document.answerform.a.focus();
         }
      }
   }
   else
   {
      window.document.problemform.q.value = "";
      current_problem_number = 0;
      running = 0;
      lastnum1 = 1;
      lastnum2 = 1;

      FinishTimeObj = new Date();
      FinishTime = FinishTimeObj.getTime();
      ElapsedTime = (FinishTime*1) - (StartTime*1);
      ElapsedTime = ElapsedTime / 1000
      ElapsedTimeString = cent(ElapsedTime);

      checkRecords();
   }
}


function checkInitials(initstring)
{
   if     (initstring.length > 3) { return false; }
   else if(initstring.length < 1) { return false; }
   else if(validate(initstring) == false) { return false; }
   else { return true; }
}


function enCode(newslot,newscore,newinitial)
{
   if(cookies_enabled == 1)
   {
      newdata = newscore + "@" + newinitial;
      setCookie(newslot, newdata, expire_date);
   }
}

function deCodeScore(oldslot)
{
   if(cookies_enabled == 1)
   {
      grabbed = getCookie(oldslot);
      var i = 0;
      while (grabbed.charAt(i) != "@")
      {
         i++;
      }
      oldscore = grabbed.substring(0,i);
      return oldscore;
   }
   else
   {
      return "999999";
   }
}

function deCodeInitial(oldslot)
{
   if(cookies_enabled == 1)
   {
      grabbed = getCookie(oldslot);
      var i = 0;
      while (grabbed.charAt(i) != "@")
      {
         i++;
      }
      oldinitial = grabbed.substring((i+1*1),(grabbed.length+1*1))
      return oldinitial;
   }
   else
   {
      return " ";
   }
}


function validate(string)
{
var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

   for (var i=0; i<length; i++)
   {
      if (valid.indexOf(string.charAt(i)) < 0)
      {
         return false;
      }
   }
   return true;
} 



function checkAnswer()
{
   if(running == 1)
   {
      if(window.document.answerform.a.value == correct_answer)
      {
         window.document.answerform.a.value = "";
         makeProblems();
      }
      else
      {
         alert("Wrong!");
         window.document.answerform.a.focus();
         window.document.answerform.a.select();
      }
   }
   else
   {
      alert("No problem set in progress");
   }
}



function skipProblem()
{
   if(running == 1)
   {
      alert("Problem skipped.\n\nAnswer: " + current_problem + correct_answer);
      current_problem_number--;
      window.document.problemform.q.value = "";
      window.document.answerform.a.value = "";
      makeProblems();
   }
   else
   {
      alert("What\'s to skip? Program isn\'t running.");
   }

}


function stopProblems()
{
   if(running == 1)
   {
      running = 0;
      current_problem_number = 0;
      window.document.problemform.q.value = "";
      window.document.answerform.a.value = "";
      alert("Program stopped.");
   }
   else
   {
      alert("Stop? Program isn\'t running.");
   }
}



//  returns the amount in the .00 format - thanks irt.org!
function cent(amount)
{
   if(amount == 999999)
   {
      return "--";
   }
   else
   {
      amount -= 0;
      return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
   }
}



/* Generic cookie routines (most written by others)
=============================================================*/
// Use this function to retrieve a cookie.
function getCookie(name){
   var cname = name + "=";
   var dc = document.cookie;
   if (dc.length > 0) {
      begin = dc.indexOf(cname);
      if (begin != -1) {
         begin += cname.length;
         end = dc.indexOf(";", begin);
         if (end == -1) end = dc.length;
         return unescape(dc.substring(begin, end));
      }
   }
   return null;
}

// Use this function to save a cookie.
function setCookie(name, value, expires) {
   document.cookie = name + "=" + escape(value) + "; path=/" +
   ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

// Use this function to delete a cookie.
function delCookie(name) {
   document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

var expire_date = new Date();
expire_date.setTime(expire_date.getTime() + (1000 * 60 * 60 * 24 * 730));  // Last number is for number of days.

function testCookiesEnabled() {
   cookiesEnabled = 0;
   setCookie('bitstest', 'yeppers', expire_date);
   if (getCookie('bitstest') == 'yeppers'){cookiesEnabled = 1;}
   delCookie('bitstest');
   return cookiesEnabled;
}



function writeFloater()
{
   if(top.floater)
   {

      if(cookies_enabled == 0)
      {
         inita1 = " "; inita2 = " "; inita3 = " ";
         inits1 = " "; inits2 = " "; inits3 = " ";
         initm1 = " "; initm2 = " "; initm3 = " ";
         initd1 = " "; initd2 = " "; initd3 = " ";
      }

      top.floater.document.clear();
      top.floater.document.open();
      top.floater.document.writeln("<HTML>");
      top.floater.document.writeln("<HEAD>");
      top.floater.document.writeln("<TITLE></TITLE>");
      top.floater.document.writeln("</HEAD>");
      top.floater.document.writeln("<BODY BGCOLOR=\"#FFFFFF\" STYLE=\"margin:1px\">");
      top.floater.document.writeln("<TABLE CELLSPACING=\"4\" CELLPADDING=\"0\" BORDER=\"0\" ALIGN=\"center\">");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD COLSPAN=\"8\" ALIGN=\"center\" BGCOLOR=\"#CCCCCC\"><FONT SIZE=\"1\" FACE=\"verdana,arial,helvetica\">H I G H\&nbsp\;\&nbsp\;\&nbsp\;S C O R E S</FONT></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD COLSPAN=\"2\" ALIGN=\"center\" BGCOLOR=\"#FFDDDD\"><FONT SIZE=\"2\" FACE=\"arial,helvetica\"><B>Addition</B></FONT></TD>");
      top.floater.document.writeln("<TD COLSPAN=\"2\" ALIGN=\"center\" BGCOLOR=\"#DDFFDD\"><FONT SIZE=\"2\" FACE=\"arial,helvetica\"><B>Subtraction</B></FONT></TD>");
      top.floater.document.writeln("<TD COLSPAN=\"2\" ALIGN=\"center\" BGCOLOR=\"#FFEECC\"><FONT SIZE=\"2\" FACE=\"arial,helvetica\"><B>Multiplication</B></FONT></TD>");
      top.floater.document.writeln("<TD COLSPAN=\"2\" ALIGN=\"center\" BGCOLOR=\"#DDDDFF\"><FONT SIZE=\"2\" FACE=\"arial,helvetica\"><B>Division</B></FONT></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD><IMG SRC=\"empty.gif\" WIDTH=\"65\" HEIGHT=\"1\" BORDER=\"0\"></TD><TD><IMG SRC=\"empty.gif\" WIDTH=\"45\" HEIGHT=\"1\" BORDER=\"0\"></TD>");
      top.floater.document.writeln("<TD><IMG SRC=\"empty.gif\" WIDTH=\"65\" HEIGHT=\"1\" BORDER=\"0\"></TD><TD><IMG SRC=\"empty.gif\" WIDTH=\"45\" HEIGHT=\"1\" BORDER=\"0\"></TD>");
      top.floater.document.writeln("<TD><IMG SRC=\"empty.gif\" WIDTH=\"65\" HEIGHT=\"1\" BORDER=\"0\"></TD><TD><IMG SRC=\"empty.gif\" WIDTH=\"45\" HEIGHT=\"1\" BORDER=\"0\"></TD>");
      top.floater.document.writeln("<TD><IMG SRC=\"empty.gif\" WIDTH=\"65\" HEIGHT=\"1\" BORDER=\"0\"></TD><TD><IMG SRC=\"empty.gif\" WIDTH=\"45\" HEIGHT=\"1\" BORDER=\"0\"></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_a1 + "\"><" + Ba1 + ">" + cent(a1) + "</" + Ba1 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_a1 + "\" SIZE=\"1\"><" + Ba1 + ">" + inita1 + "</" + Ba1 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_s1 + "\"><" + Bs1 + ">" + cent(s1) + "</" + Bs1 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_s1 + "\" SIZE=\"1\"><" + Bs1 + ">" + inits1 + "</" + Bs1 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_m1 + "\"><" + Bm1 + ">" + cent(m1) + "</" + Bm1 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_m1 + "\" SIZE=\"1\"><" + Bm1 + ">" + initm1 + "</" + Bm1 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_d1 + "\"><" + Bd1 + ">" + cent(d1) + "</" + Bd1 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_d1 + "\" SIZE=\"1\"><" + Bd1 + ">" + initd1 + "</" + Bd1 + "></FONT></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_a2 + "\"><" + Ba2 + ">" + cent(a2) + "</" + Ba2 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_a2 + "\" SIZE=\"1\"><" + Ba2 + ">" + inita2 + "</" + Ba2 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_s2 + "\"><" + Bs2 + ">" + cent(s2) + "</" + Bs2 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_s2 + "\" SIZE=\"1\"><" + Bs2 + ">" + inits2 + "</" + Bs2 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_m2 + "\"><" + Bm2 + ">" + cent(m2) + "</" + Bm2 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_m2 + "\" SIZE=\"1\"><" + Bm2 + ">" + initm2 + "</" + Bm2 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_d2 + "\"><" + Bd2 + ">" + cent(d2) + "</" + Bd2 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_d2 + "\" SIZE=\"1\"><" + Bd2 + ">" + initd2 + "</" + Bd2 + "></FONT></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("<TR>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_a3 + "\"><" + Ba3 + ">" + cent(a3) + "</" + Ba3 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_a3 + "\" SIZE=\"1\"><" + Ba3 + ">" + inita3 + "</" + Ba3 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_s3 + "\"><" + Bs3 + ">" + cent(s3) + "</" + Bs3 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_s3 + "\" SIZE=\"1\"><" + Bs3 + ">" + inits3 + "</" + Bs3 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_m3 + "\"><" + Bm3 + ">" + cent(m3) + "</" + Bm3 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_m3 + "\" SIZE=\"1\"><" + Bm3 + ">" + initm3 + "</" + Bm3 + "></FONT></TD>");
      top.floater.document.writeln("<TD ALIGN=\"right\"><FONT FACE=\"arial\" COLOR=\"#" + color_d3 + "\"><" + Bd3 + ">" + cent(d3) + "</" + Bd3 + "></FONT></TD><TD><FONT FACE=\"verdana\" COLOR=\"#" + color_d3 + "\" SIZE=\"1\"><" + Bd3 + ">" + initd3 + "</" + Bd3 + "></FONT></TD>");
      top.floater.document.writeln("</TR>");
      top.floater.document.writeln("</TABLE>");
      top.floater.document.writeln("</BODY>");
      top.floater.document.writeln("</HTML>");
      top.floater.document.close();
   }
}


