
var birky_last = [-1, -1, -1, -1, -1];


function prepare_colors(a_window)
{
  a_window.document.writeln("<html>");
  a_window.document.writeln("<body text=\"#FFFFCC\" bgcolor=\"#191970\"");
  a_window.document.writeln("link=\"#CCFFFF\" vlink=\"#FFCCCC\" alink=\"#FF6666\">");
}

function page_end(a_window)
{
  a_window.document.writeln("</body>\n</html>");
}


function introduction(a_window)
{
  prepare_colors(a_window);

  a_window.document.writeln("<center>");
  a_window.document.writeln("<h2>Welcome to Birkoff's Workstation");
  a_window.document.writeln("</h2>");
  a_window.document.writeln("</center>");
}

function decode_right(some_string)
{
  var z=some_string.split("=");
  var lefts = z[1];
  var splits=(""+unescape(lefts)).split("+");
  return splits.join(" ");
}

function decode_left(some_string)
{
  var z=some_string.split("=");
  return "" + z[0];
}

function encode_hist(arr)
{
  str = "";
  for (i=0; i<arr.length; i++) {
    str = str + arr[i];
    if (i < arr.length -1)
      str = str + "a";
  }
  return str;
}

function decode_hist(str)
{
  pieces = str.split("a");
  for (i=0; i<pieces.length; i++) {
    birky_last[i] = parseInt(pieces[i])
    if (isNaN(birky_last[i]))
      birky_last[i] = -1;
  }
}

function update_last(number)
{
  for (i=0; i<birky_last.length-1; i++)
    birky_last[i] = birky_last[i+1];
  birky_last[birky_last.length-1] = number;
}

function process_ansr() 
{
  x=document.URL.split("?");
  loc=x[0];
  if (x.length > 1) {
    x=x[1];
    y=x.split("&");
    hist_new="";
    for (i in y) {
      var_name=decode_left(y[i]);
      value=decode_right(y[i]);
      if (var_name == "history_text") 
        hist_new = value + "*";
      if (var_name == "answer")
        your_ans = value;
      if (var_name == "birky_ans")
        decode_hist(value);
    }
    if ((prepare(your_ans) == "bye") || 
        (prepare(your_ans) == "goodbye") || 
        (prepare(your_ans) == "good bye") || 
        (prepare(your_ans) == "by")) {
      birky_ansr = birky_bye();
      bye = 1;
    }
    else
      birky_ansr = birky_answer(your_ans, birky_last);
    hist_new = hist_new + your_ans;
    update_last(birky_ansr.number);
  }
}

function write_history(where, text)
{
  var split_text = text.split("*");
  for (var i=0; i<split_text.length; i++) 
    if (i % 2 == 0)
      where.document.writeln(split_text[i]+"\n<br>\n");
    else
      where.document.writeln('<font color="FF77DD">'+
                             split_text[i]+"</font>\n<br><br>\n");
}

function display_all()
{
  parent.frames["pic_talk"].frames["the_talk"].document.open();
  introduction(parent.frames["pic_talk"].frames["the_talk"]);
  
  process_ansr();

  //hist_new = hist_new + '<font color="FF77DD">' 
  //                    + birky_ansr.string + "</font>\n<br>\n";
  hist_new = hist_new + '*' + birky_ansr.string;
  //document.form1.history_text.value = hist_new;
  document.form1.birky_ans.value = encode_hist(birky_last);
  //parent.frames["pic_talk"].frames["the_talk"].document.writeln(hist_new);
  write_history(parent.frames["pic_talk"].frames["the_talk"], hist_new);
  parent.frames["pic_talk"].frames["the_talk"].window.scrollBy(500000, 500000);
  page_end(parent.frames["pic_talk"].frames["the_talk"]);
  parent.frames["pic_talk"].frames["the_talk"].document.close();

  parent.frames["birky_says"].document.open();
  prepare_colors(parent.frames["birky_says"]);
  parent.frames["birky_says"].document.writeln("<center><h3>");
  parent.frames["birky_says"].document.writeln(birky_ansr.string);
  parent.frames["birky_says"].document.writeln("</h3></center>");
  page_end(parent.frames["birky_says"]);
  parent.frames["birky_says"].document.close();

  random_pic(parent.frames["pic_talk"].frames["picture"]);

  document.form1.history_text.value=hist_new;
  if (bye != 1) 
    document.form1.answer.focus();
  else
    document.location="./end_page.html";
}
