// Global Variables //
activeBot = "";
userName = "You";

function ChatBot(name, responsetime){
    this.name = name;
    this.responsetime = responsetime;
    this.debug = false;

    this.lastinput = "";
    this.lastresponse = "";
    this.transcript = "";
    this.thinking = false;
    
    // Functions //
    this.say = say;
    this.respond = respond;
    this.update = update;
    this.setMenu = setMenu;
    this.setText = setText;
    this.setResponse = setResponse;
    this.sendQuestion = sendQuestion;
    this.receiveQuestion = receiveQuestion;

    // Initialization //
    this.setResponse("Enter your response here...");
    setText('<p class="CBot">Welcome!  I am a chatbot created using the latest artifical intelligence technology on the net.<br /><br />I was developed by Casey using AJAX and a LAMP platform.  If you would like to chat, simply enter a comment or question below and hit enter.');
    setMenu('Currently running the <a href="http://en.wikipedia.org/wiki/Chomsky">Chomsky AIML set...</a>');
    activeBot = this;
    this.reset();

// DEBUG
if(this.debug) this.responsetime = 0;
if(this.debug) setMenu("DEBUG mode is on.");

}
function say(input){
    // AJAX code will go here for communicating with PHP script //
    //if(this.thinking) alert("Please give me a minute to type!  Sheez...");
    
    // botname = CaseyBot
    // session_name = $uid
    // input = whatever they type
    
    this.thinking = true;   
    this.lastinput = '<p class="CUser"><span class="CNametag">' + userName + '</span><br />' + input + '</p>';
    this.lastresponse = '';

    this.sendQuestion(input);
    this.update();
}
function respond(input){
    input = unescape(input);
    this.thinking = false;
    this.lastresponse = '<p class="CBot"><span class="CNametag">' + this.name + '</span><br />' + input + '</p>';
    this.update();
}
function update(){
    thinkingText = '<p><br /><span class="CStatusMsg">' + this.name + ' is typing...</span></p>';
    this.setText((this.thinking) ? this.lastinput + thinkingText : this.lastinput + this.lastresponse);
}
function setMenu(input){
    document.getElementById("CMenu").innerHTML = input;
}
function setText(input){
    document.getElementById("CTextbox").innerHTML = input;
}
function setResponse(input){
    document.getElementById("CResponse").value = input;    
}
function getName(){
    userName = prompt("Please enter your name:");
}

// Event Handlers
document.onkeyup = keyUp;
// ADD AN EVENT HANDLER TO CATCH MOUSING IN THE RESPONSE BOX

function keyUp(e){
    if (window.event) e = window.event;
    var target = (window.event) ? e.srcElement : e.target;
    var key = (window.event) ? e.keyCode : e.which;
    
    if(target == document.getElementById("CResponse") && (key == 13)) {
        activeBot.say(document.getElementById("CResponse").value);
        activeBot.setResponse("");
    }
}
function sendQuestion(input){
    
    var xmlhttp;
    if (window.XMLHttpRequest)
        { xmlhttp=new XMLHttpRequest(); }
    else if (window.ActiveXObject)
        { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    else
        { alert("Your browser does not support XMLHTTP!"); }
    
    activeBot = this;
    xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4) {
                setTimeout('activeBot.respond("'+escape(xmlhttp.responseText)+'");',activeBot.responsetime);
            }
    }
    xmlhttp.open("GET","chatgui.php?" + ((this.debug) ? "debug=1&" : "") + "botname=TestBot&input="+escape(input),true);
    xmlhttp.send(null);
}
function receiveQuestion(xmlhttp){

}
