var rand_sentence;
var rand_question;
var rand_where;
var rand_what;
var rand_why;
var rand_who;
var rand_when;
var bye_array = new Array();
var convert_you_me = new Array();
var synonym = new Array();

function fill_sentence()
{
  rand_sentence = [4, 10, 17, 23, 43, 68, 69, 76, 83, 84, 96, 
                   99, 102, 156, 164, 178, 186, 189, 202, 208,
                   228, 254, 277, 292, 295, 296, 365, 383, 449,
                   504, 505, 510, 513, 522, 543, 561, 579, 623,
                   664, 665, 696, 712, 732, 756, 783, 787, 813,
                   831, 877, 892, 894, 914, 923, 938, 939];
}

function fill_question()
{ 
  rand_question = [14, 34, 38, 64, 77, 82, 109, 132, 289, 290,
                   304, 322, 347, 373, 397, 405, 467, 598, 683,
                   710, 964];
  rand_where = [88, 103, 112, 113, 129, 144, 194, 232, 249, 253,
                286, 483, 496, 656, 708, 726, 848, 865];
  rand_what = [20, 25, 29, 83, 165, 172, 177, 308, 310, 344, 
               416, 443, 671, 681, 689, 837];
  rand_why = [11, 90, 124, 126, 180, 210, 216, 269, 329, 380, 
              444, 458, 461, 516, 731, 748, 771, 816];
  rand_who = [17, 31, 72, 91, 162, 168, 333, 341, 366, 396, 421,
              715, 795, 915];
  rand_when = [225, 260, 293, 328, 330, 323, 381, 401, 412, 470,
               549, 632, 827, 852, 885];
  rand_howru = [38, 99, 458, 459, 535, 543, 664, 775, 937];
  rand_how = [30, 38, 43, 60, 76, 81, 83, 84, 99, 102, 109, 132, 152, 253, 266,
              288, 317, 347, 405, 416, 420, 425, 458, 467, 478, 497, 510, 
              523, 543, 598, 664, 676, 685, 693, 704, 707, 766, 775, 806, 824,
              829, 831, 937];
}

function fill_bye()
{
  bye_array = [87, 130, 281, 300, 311, 323, 387, 423, 426, 507, 
               555, 707, 786, 854, 955];
}

function fill_convert()
{
  convert_you_me["i"]="you";
  convert_you_me["you"]="I";
  convert_you_me["me"]="you";
  convert_you_me["am"]="are";
  convert_you_me["are"]="am";
  convert_you_me["i'm"]="you are";
  convert_you_me["my"]="your";
  convert_you_me["your"]="my";
  convert_you_me["mine"]="yours";
  convert_you_me["yours"]="mine";
}

function fill_synonym()
{
  synonym["i'm"]="i am";
  synonym["i'd"]="i would";
  synonym["i've"]="i have";
  synonym["i'll"]="i will";
  synonym["we're"]="we are";
  synonym["we'd"]="we would";
  synonym["we've"]="we have";
  synonym["we'll"]="we will";
  synonym["you're"]="you are";
  synonym["you'd"]="you would";
  synonym["you've"]="you have";
  synonym["you'll"]="you will";
  synonym["it's"]="it is";
  synonym["that's"]="that is";
  synonym["there's"]="there is";
  synonym["he's"]="he is";
  synonym["she's"]="she is";
  synonym["hi"]="hello";
  synonym["thanks"]="thank you";
  synonym["wanna"]="want to";
  synonym["gonna"]="going to";
  synonym["gotta"]="got to";
  synonym["wana"]="want to";
  synonym["gona"]="going to";
  synonym["gota"]="got to";
  synonym["don't"]="do not";
  synonym["didn't"]="did not";
  synonym["doesn't"]="does not";
  synonym["won't"]="will not";
  synonym["wouldn't"]="would not";

}

function you_to_me(sub_str, convert_array)
{
  var words=sub_str.split(' ');
  var convert="";
  var i;
  var j;
  for (i in words) 
    for (j in convert_array) {
      if (words[i] == j) {
        words[i]=convert_array[j];
        break;
      }
    }
  return words.join(' ');
}

function construct_answer(lcheck, back_pattern, start_ans, end_ans)
{
  var split_bpt=back_pattern.split("*");
  var i;
  var ln = split_bpt.length;
  var answer_str="";
  for (i=0; i<ln; i++) {
    if (split_bpt[i].charAt(0) == '#') {
      nstr=parseInt(removeFirst(split_bpt[i]));
      answer_str += you_to_me(lcheck.substring(start_ans[nstr], end_ans[nstr]),convert_you_me);
    }
    else 
      answer_str += split_bpt[i];
  }
  return answer_str;
}

function prepare(to_check)
{
  var lcheck = to_check.toLowerCase();
  if (!isAlpha(lcheck.charAt(lcheck.length-1)))
    lcheck=removeLast(lcheck);
  return lcheck;
}

function check_pattern(to_check, pattern, back_pattern)
{
  var lcheck=prepare(to_check);
  var lngth=lcheck.length;
  var split_pt=pattern.split("*");
  pattern_nr=split_pt.length;
  start_ans = new Array(pattern_nr+1);
  end_ans = new Array(pattern_nr+1);
  start_ans[0]=0;
  end_ans[pattern_nr]=lngth;
  pcheck=true;
  var j;
  for (var i=0; pcheck && (i<pattern_nr); i++) {
    ncheck = lcheck.substring(start_ans[i], lngth);
    if (isDigit(split_pt[i].charAt(0))) {
      nr_words=parseInt(split_pt[i]);
      ncheck_words=ncheck.split(" ");
      if (ncheck_words.length < nr_words) 
        pcheck = false;
      else {
        nw = [];
        for (j=0; j<nr_words; j++) 
          nw = nw.concat(ncheck_words[j]);
        nw = nw.join(" ");
        end_ans[i] = nw.length+start_ans[i]+1;
        if (nr_words == ncheck_words.length || 
            nr_words == ncheck_words.length - 1 &&
            ncheck_words[ncheck_words.length-1] == "") {
          if (split_pt[i].length > 1)
            return false;
          else
            start_ans[i+1]=end_ans[i]+1;
        }
        else {
          nck = [""];
          for ( ; j<ncheck_words.length; j++)
            nck = nck.concat(ncheck_words[j]);
          check=removeFirst(split_pt[i]);
          if (check[0]==" ")
            check=removeFirst(check);
          nck = nck.join(" ");
          pos = nck.indexOf(check);
          if (pos == 0 || pos == 1) 
            start_ans[i+1]=end_ans[i]+1;
          else 
            pcheck=false;
        }
      }
    }
    else {
      start_pos=ncheck.indexOf(split_pt[i]);
      if (start_pos >= 0) {
        end_ans[i]=start_pos + start_ans[i];
        start_ans[i+1]=start_pos+start_ans[i]+split_pt[i].length;
      }
      else
        pcheck=false;
    }
  }
  answer=new Result(false, -1, "");
  answer.state=pcheck;
  if (pcheck) 
    answer.string=construct_answer(lcheck, back_pattern, start_ans, end_ans);
  return answer;
}

function find_pattern(to_check)
{
  var pattern_nr = pattern_array.length;
  var start = intRandLim(0, pattern_nr-1);
  var found = new Result(false, -1, "");
  for (j=0; (!found.state) && (j<pattern_nr); j++) {
    i = (start + j) % pattern_nr;
    found = check_pattern(to_check, pattern_array[i], template_array[i]);
    if (found.state) 
      found.number = i;
  }
  return found;
}

function find_word(to_check)
{
  var check_arr=to_check.split(" ");
  //writeLn(check_arr[0]);
  var found = new Result(false, -1, "");
  var multi_word = new Array();
  for (j in check_arr) {
    word = prepare(check_arr[j]);
    word = strip_affixes(word);
    if (word != "" && word.length > 4) {
      word_fill(word);
      if (word_array[word]) {
        multi_word = multi_word.concat(rand_answer(word_array[word]));
        found.state = true;
      }
    }
  }
  if (found.state) {
    if (multi_word.length >1) 
      found.number = rand_answer(multi_word);
    else if (multi_word.length == 1)
      found.number = multi_word[0];
    sence_fill(found.number);
    found.string = sentence[found.number];
  }
  return found;
}

function birky_question(ans, pcheck)
{
  var ind_b = ans.indexOf("birkoff,", 0);
  var birky_says = -1;
  if (ind_b > 0) {
    ans = ans.substring(ind_b+8, ans.length);
    if (ans[0] == " ")
      ans = removeFirst(ans);
  }
  if (ans.indexOf("where", 0) == 0)
    birky_says=rand_answer(rand_where);
  else if (ans.indexOf("what", 0) == 0)
    birky_says=rand_answer(rand_what);
  else if (ans.indexOf("why", 0) == 0)
    birky_says=rand_answer(rand_why);
  else if (ans.indexOf("who", 0) == 0)
    birky_says=rand_answer(rand_who);
  else if (ans.indexOf("when", 0) == 0)
    birky_says=rand_answer(rand_when);
  else if (ans.indexOf("how are you", 0) == 0)
    birky_says=rand_answer(rand_howru);
  else if (ans.indexOf("how", 0) == 0)
    birky_says=rand_answer(rand_how);
  else
    birky_says=-1;
  if (birky_says > -1) {
    pcheck.state = true;
    sence_fill(birky_says);
    pcheck.string = sentence[birky_says];
    pcheck.number = birky_says;
  }  
  return pcheck;
}

function birky_chat(ans, last)
{
  var pcheck = new Result(false, 0, "");

  var birky_says = "I don't understand.";
  // Find a pattern if there is one.
  if (Math.random() <= 0.7) {
    pattern_init();
    pcheck = find_pattern(ans);
  }
  if ((!pcheck.state) && (Math.random() <= 0.80) && (last == '?')) 
    pcheck = birky_question(ans, pcheck);
  if (!pcheck.state && (Math.random() <= 0.90))
    pcheck = find_word(ans);
  if (!pcheck.state) {
    if (last == '?') 
      birky_says=rand_answer(rand_question);
    else 
      birky_says=rand_answer(rand_sentence);
    pcheck.state = true;
    sence_fill(birky_says);
    pcheck.string = sentence[birky_says];
    pcheck.number = birky_says;
  }
  return pcheck;
}

function birky_init()
{
  fill_sentence();
  fill_question();
  fill_convert();
  fill_bye();
  fill_synonym();
  fill_keys();
  fill_synonyms();
  fill_birky_facts();
  fill_bchar();
}

function birky_answer(ans, birky_said)
{
  do {
    birky = new Result(false, -1, "");
    last = ans.charAt(ans.length-1);
    if (last == "?") {
    // Find the fact file first.
      var r = find_data(prepare(ans));
      if (r.state) {
	r.string = toSentence(r.string);
        birky = r;
      }
    }
    if ((!birky.state) || (in_array(birky_said, birky.number) != -1)) 
      birky = birky_chat(you_to_me(prepare(ans),synonym),last);
    found = in_array(birky_said, birky.number);
    if (found == -1)
      found = false;
    else
      found = true;
  } while (found);
  return birky;
}

function birky_bye()
{
  index = rand_answer(bye_array);
  sence_fill(index);
  return (new Result(true, index, sentence[index]));
}
