var rand_sentence = new Array();
var rand_question = new Array();
var pattern_array = new Array();
var bye_array = new Array();
var convert_you_me = new Array();
var synonym = new Array();
var word_array = new Array();

function fill_sentence()
{
  rand_sentence[0]="That's interesting. Please continue.";
  rand_sentence[1]="I'd like to know more about that.";
  rand_sentence[2]="So are there any other issues you'd like to raise?";
  rand_sentence[3]="Are you trying to convince me, or yourself?";
  rand_sentence[4]="That's not what I asked.";
  rand_sentence[5]="In this case, we have a problem.";
  rand_sentence[6]="I know.";
  rand_sentence[7]="I doubt it.";
  rand_sentence[8]="That's not the point.";
  rand_sentence[9]="Of course.";
  rand_sentence[10]="I'm sorry. I don't mean to laugh...but you have to admit...";
  rand_sentence[11]="I disagree.";
  rand_sentence[12]="I'm aware of that. So? ";
  rand_sentence[13]="Maybe.";
  rand_sentence[14]="Let's not get into this again.";
  rand_sentence[15]="Yes, you're right.";
  rand_sentence[16]="What do you mean?";
  rand_sentence[17]="Why?";
  rand_sentence[18]="So tell me about last night. "
  rand_sentence[19]="Why do you say that?";
  rand_sentence[20]="I want to know more";
  rand_sentence[21]="That's what you said when you first came to us.";
  rand_sentence[22]="I am busy. Would you get to the point?";
  rand_sentence[23]="What makes you think that?";
  rand_sentence[24]="Can you elaborate on that?";
  rand_sentence[25]="Why?";
}

function fill_question()
{ 
  rand_question[0]="The answer is highly classified.";
  rand_question[1]="What is it you really want to know?";
  rand_question[2]="It's a good question. Perhaps more profound than you know."
  rand_question[3]="Would knowing the answers make you perform your mission more efficiently?";
  rand_question[4]="Very good. It usually takes the new ones several years to ask that question."
  rand_question[5]="That's a question only you can answer.";
  rand_question[6]="You'll find out in the next twelve hours.";
  rand_question[7]="What kind of answer would you like?";
  rand_question[8]="Why don't you just ask me what you want to know?";
  rand_question[9]="I doubt if it's going to be that simple.";
  rand_question[10]="It's unclear.";
  rand_question[11]="It's not an easy call.";
  rand_question[12]="That’s the wrong question.";
  rand_question[13]="This isn't good, is it?";
  rand_question[14]="It’s not that simple.";
  rand_question[15]="Watch and see.";
  rand_question[16]="I beg your pardon?";
  rand_question[17]="We’ve asked that question many times.";
  rand_question[18]="We’ll see.";
  rand_question[19]="You'd better get ready.";
  rand_question[20]="I understand your confusion.";
  rand_question[21]="I'll have to study the question further.";
  rand_question[22]="You already have your answer."
  rand_question[23]="When?";
}

function fill_bye()
{
  bye_array[0]="You can go, but stand by, there's going to be a follow up mission.";
  bye_array[1]="That's all for now.";
  bye_array[2]="You'd better go now.";
  bye_array[3]="You can leave now.";
  bye_array[4]="That'll be all.";
  bye_array[5]="I'll see you in the morning. Thank you.";
}

function fill_patterns()
{
  pattern_array[0] = "i feel";
  pattern_array[1] = "How long have you felt*#1*?";
  pattern_array[2] = "i am";
  pattern_array[3] = "Would you like me to think you are*#1*?";
  pattern_array[4] = "nobody*s me";
  pattern_array[5] = "Who would you like to*#1* you?";
  pattern_array[6] = "don't"
  pattern_array[7] = "You are being negative.";
  pattern_array[8] = "i *your";
  pattern_array[9] = "Don't be a child.";
  pattern_array[10] = "i *you";
  pattern_array[11] = "Maybe in your dreams we *#1*each other.";
  pattern_array[12] = "talk about";
  pattern_array[13] = "I've been thinking a lot about*#1* lately.";
  pattern_array[12] = "what*you";
  pattern_array[13] = "We are not here to talk about me.";
  pattern_array[14] = "since*ago";
  pattern_array[15] = "What happened*#1*ago?";
  pattern_array[16] = "i want";
  pattern_array[17] = "Don't get impatient.";
  pattern_array[18] = "i heard";
  pattern_array[19] = "That is an interesting rumor.";
  pattern_array[20] = "i believe";
  pattern_array[21] = "What makes you think that?";
  pattern_array[22] = "i didn't";
  pattern_array[23] = "Why not?";
  pattern_array[24] = "i did";
  pattern_array[25] = "It wasn't enough.";
  pattern_array[26] = "i won't";
  pattern_array[27] = "You don't know it yet, but you will*#1*.";
  pattern_array[28] = "looks like";
  pattern_array[29] = "Perhaps so. But I hope not.";
  pattern_array[30] = "i usually don't";
  pattern_array[31] = "In this case, you might want to make an exception."
  pattern_array[32] = "the*is";
  pattern_array[33] = "It was inevitable."
  pattern_array[34] = "i * you";
  pattern_array[35] = "We are not here for each other.";
  pattern_array[36] = "you are";
  pattern_array[37] = "I will take that as a compliment.";
  pattern_array[38] = "why* you *talk";
  pattern_array[39] = "I wanted you to know how I feel.";
  pattern_array[40] = "regret";
  pattern_array[41] = "Don't go poetic on me.";
  pattern_array[42] = "i am*tired";
  pattern_array[43] = "Maybe you need a little down time."
  pattern_array[44] = "i want";
  pattern_array[45] = "Is that what you walked in here for?";
  pattern_array[46] = "tell me";
  pattern_array[47] = "I can't tell you *#1*.";
  pattern_array[48] = "don't like";
  pattern_array[49] = "Don't be a child!"
  pattern_array[50] = "you want";
  pattern_array[51] = "That would be a safe way to play it.";
  pattern_array[52] = "i don't want to";
  pattern_array[53] = "I didn't ask you to*#1*. I ordered you.";
  pattern_array[54] = "i *n't";
  pattern_array[55] = "Why not?"
  pattern_array[56] = "you*tell";
  pattern_array[57] = "That's not a good sign. Using a third party to mediate communications.";
  pattern_array[58] = "remember";
  pattern_array[59] = "What about*#1*?";
  pattern_array[60] = "i prefer";
  pattern_array[61] = "That's entirely up to you.";
  pattern_array[62] = "i'll";
  pattern_array[63] = "Why would you do that?";
  pattern_array[64] = "you have";
  pattern_array[65] = "Have I?";
  pattern_array[66] = "something";
  pattern_array[67] = "And what do you think that might be?";
  pattern_array[68] = "how*bad";
  pattern_array[69] = "It could have been worse.";
  pattern_array[70] = "somebody is";
  pattern_array[71] = "Can you trace them?";
  pattern_array[72] = "have no idea";
  pattern_array[73] = "Of course not.";
  pattern_array[74] = "said*that";
  pattern_array[75] = "Don't let this distract you.";
  pattern_array[76] = "you want";
  pattern_array[77] = "Is that a problem?";
  pattern_array[78] = "what*you";
  pattern_array[79] = "That information is classified."; 
  pattern_array[80] = "you*like";
  pattern_array[81] = "Thank you anyway.";
  pattern_array[82] = "won't*tell";
  pattern_array[83] = "Do you think there's anything I won't do to get this information from you?";
  pattern_array[84] = "what if * * not to";
  pattern_array[85] = "I'm sure *#1* will *#3*";
  pattern_array[86] = "i don't*understand";
  pattern_array[87] = "Somehow I don't think it's*#2* you're having trouble figuring out.";
  pattern_array[88] = "not sure";
  pattern_array[89] = "I know it's a difficult decision.";
  pattern_array[90] = "the";
  pattern_array[91] = "You say that *#0*the*#1*?";
  pattern_array[92] = "don't believe";
  pattern_array[93] = "I think it's pretty clear*#1*."
  pattern_array[94] = "i know";
  pattern_array[95] = "Do you?";
  pattern_array[96] = " at ";
  pattern_array[97] = "What else happened at *#1*?";
  pattern_array[98] = "don't* in";
  pattern_array[99] = "Then what do you*#1* in?";
  pattern_array[100] = "what could";
  pattern_array[101] = "I don't know, that's what worries me."; 
  pattern_array[102] = " didn't * it *before";
  pattern_array[103] = "What did *#0* *#1* before?";
  pattern_array[102] = " didn't * th* before";
  pattern_array[103] = "What did *#0* *#1* before?";
  pattern_array[104] = "talk to you";
  pattern_array[105] = "What do you want to talk about?";
  pattern_array[106] = "about";
  pattern_array[107] = "What about *#1*?";
  pattern_array[108] = "you are";
  pattern_array[109] = "Am I really*#1*?";
}

function fill_words()
{
  var hello_arr = new Array;

  hello_arr[0]="What's up?";
  hello_arr[1]="Hello. Is everything all right?";
  hello_arr[2]="Hello. Is there something you need?";
  hello_arr[3]="Hello. What is your problem?";
  hello_arr[4]="What is it?";
  hello_arr[5]="Hello. What do you want?";
  hello_arr[6]="Hello."

  word_array["hello"] = hello_arr;

  var thank_arr = new Array;

  thank_arr[0]="You're welcome.";
  thank_arr[1]="Don't mention it.";

  word_array["thank"] = thank_arr;

  var no_arr = new Array();
  no_arr[0] = "You are being negative.";
  no_arr[1] = "Why not?";

  word_array["no"] = no_arr;
  word_array["not"] = no_arr;

  var desire_arr = new Array();
  desire_arr[0] = "Desire is always weakness."
  desire_arr[1] = "It can never be what it was.";
  desire_arr[2]= "Let's not get into this again. ";

  word_array["desire"] = desire_arr;
  word_array["lust"] = desire_arr;
  word_array["wish"] = desire_arr;

  var you_arr = new Array();
  you_arr[0]="What about you?";
  you_arr[1]="We are not here to talk about me.";

  word_array["you"]=you_arr;
  word_array["madeleine"]=you_arr;
  word_array["madeline"]=you_arr;
  word_array["maddy"]=you_arr;

  var yes_arr = new Array();
  yes_arr[0] = "You are positive.";
  yes_arr[1] = "Are you sure?";

  word_array["yes"] = yes_arr;
}

function rand_answer(rand_array)
{
  var leng=rand_array.length;
  var i=intRandLim(0,leng-1);
  return rand_array[i];
}

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["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["he's"]="he is";
  synonym["she's"]="she is";
  synonym["hi"]="hello";
  synonym["thanks"]="thank you"
}

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=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[lcheck.length-1]))
    lcheck=removeLast(lcheck);
  return lcheck;
}

function check_pattern(to_check, pattern, back_pattern)
{
//  writeLn(pattern);
//  writeLn(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 i;
  for (i=0; pcheck && (i<pattern_nr); i++) {
    ncheck = lcheck.substring(start_ans[i], lngth);
    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 Array(2);
  answer[0]=pcheck;
  if (pcheck)
    answer[1]=construct_answer(lcheck, back_pattern, start_ans, end_ans);
  else
    answer[1]="";
  return answer;
}

//function find_pattern(to_check)
//{
//  var pattern_nr = (pattern_array.length)/2;
//  var start = intRandLim(0, pattern_nr-1);
//  var found = new Array(2);
//  found[0]=false;
//  for (j=0; (!found[0]) && (j<pattern_nr); j++) {
//    i = (start + j) % pattern_nr;
//    writeLn(i);
//    found = check_pattern(to_check, pattern_array[2*i], 
//                          pattern_array[2*i+1]);
//  }
//  return found;
//}

function find_pattern(to_check)
{
  found_arr = new Array();
  count = 0;
  var pattern_nr = (pattern_array.length)/2;
  var start = intRandLim(0, pattern_nr-1);
  var found = new Array(2);
  found[0]=false;
  var j;
  for (j=0; j<pattern_nr; j++) {
//    writeLn(j);
    found = check_pattern(to_check, pattern_array[2*j], pattern_array[2*j+1]);
    if (found[0]) {
//      writeLn(found[1]);
      found_arr[count] = found[1];
      count++;
    }
  }
  if (count > 0) {
    found[0] = true;
    which = intRandLim(0, count-1);
    found[1] = found_arr[which];
  }
  return found;
}

function find_word(to_check)
{
  var check_arr=to_check.split(" ");
  //writeLn(check_arr[0]);
  var found = new Array(2);
  var multi_word = new Array();
  found[0] = false;
  found[1] = "";
  var i=0;
  for (j in check_arr) {
    x = prepare(check_arr[j]);
    if (word_array[x]) {
      multi_word[i] = rand_answer(word_array[x]);
      i++;
      found[0] = true;
    }
  }
  if (found[0])
    if (multi_word.length >1) 
      found[1] = rand_answer(multi_word);
    else if (multi_word.length == 1)
      found[1] = multi_word[0];
  return found;
}

function maddy_chat(ans)
{
  var pcheck = new Array(2);
  pcheck[0]=false;

  //writeLn(ans);
  var maddy_says = "I don't understand.";
  if (Math.random() <= 0.95)
    pcheck = find_pattern(ans);
  //writeLn(maddy_says);
  //writeLn(ans);
  if ((!pcheck[0]) && (Math.random() <= 0.95))
    pcheck = find_word(ans);
  if (!pcheck[0])
    if (ans.charAt(ans.length-1) == '?')
      maddy_says=rand_answer(rand_question);
    else
      maddy_says=rand_answer(rand_sentence);
  else
    maddy_says=pcheck[1];
  //writeLn(maddy_says);
  return maddy_says;
}

function maddy_init()
{
  //document.write("Welcome to Madeleine's Quarters!<br>");
  //document.write("Please sit in this chair.<br>");

  fill_sentence();
  fill_question();
  fill_convert();
  fill_patterns();
  fill_bye();
  fill_synonym();
  fill_words();
}

function maddy_answer(ans, maddy_said)
{
  do {
    maddy = maddy_chat(you_to_me(prepare(ans),synonym));
  } while (maddy == maddy_said);
  //alert(maddy);
  return(maddy);
}

function maddy_bye()
{
  return (rand_answer(bye_array));
}
