// JavaScript Document
var PrevKey, chatDIV,messageBox,sendFlag = false,statusBar,Frame1;
function begin()
{
Frame1 = window.frames[0];
chatDIV = (Frame1.document.getElementById("chatDIV"));
messageBox = document.getElementsByName("sendBox")[0];
statusBar = document.getElementsByName("statusBar")[0];
welcome();
}
function welcome()
{
//Get Answer 2 and Answer 3
fetchAnswer("_CLIENT_WELCOME_MESSAGE");
setTimeout("fetchAnswer('_CLIENT_EMAIL_MESSAGE')",3000);
}
function sendBox_KeyPress(e)
{
	
	if (PrevKey ==17 && e.keyCode == 13)
	{
		//CTRL + ENTER
		messageBox.value += '\n';
	}
	else
	if (PrevKey !=17 && e.keyCode == 13)
{
PrevKey = 0;
showMessage(messageBox.value,1);


try{
	e.keyCode = "8";
}catch(ee){}
messageBox.value = "";
}
else
{
PrevKey = e.keyCode;
}
}

function showMessage(str,ctrl)
{
	switch (ctrl)
	{
	case 1:
			//FROM CLIENT
			if (str.length != 0)
			{
			chatDIV.innerHTML += "<br /><br /><strong>ME&nbsp;:&nbsp;</strong>" + str;
			fetchAnswer(messageBox.value);
			}
			break;
	case 2:
			//FROM VIRTUAL ASSISTANT
			chatDIV.innerHTML += "<br /><strong>Joe&nbsp;:&nbsp;</strong>" + str;
			break;
	}
	
}

function fetchAnswer(question)
{
	//Create AJAX Object
	xmlHttp = createAJAX();
	var url = "virtual/ajax.asp?msg=" + question ;
	xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {			
		  reply = xmlHttp.responseText;
		  try{
			statusBar.innerHTML = "Joe has entered text";
			setTimeout("clear_statusbar()",3000);
		  }catch(e){}
			if (reply.substr(0,4) == "@@@@")
			{
				showMessage(reply.substr(4),2);	
				Frame1.window.Scroll_Bottom();
			}
			else
			{
				showMessage(reply);
				//showMessage("Connection Problem",2);	
			}
		}
	  }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	//Show Status Bar Message
	try{
	statusBar.innerHTML = "Joe is typing a message";
	setTimeout("clear_statusbar()",3000);
	}catch(e){}
}
function clear_statusbar()
{
statusBar.innerHTML = "";	
}
function show_message_button()
{
showMessage(messageBox.value,1);
messageBox.value = "";
	
}
function createAJAX() {
var xmlHttp;
  try
    {    // Firefox, Opera 8.0+, Safari  
	 xmlHttp=new XMLHttpRequest();    }
  catch (e)
    {    // Internet Explorer    
	try
      {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
    catch (e)
      {      try
        {        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        }
      catch (e)
        {  
		   alert("Your browser does not support AJAX!");    
		   return false;        
	  }
	}
} // Create and return ActiveX Object
return xmlHttp;
}

