/**
 * Puts the cursor into the input field.
 */
function focusInput()
{
	var input = document.getElementById('user-input').focus();
}

/**
 * Sends whatever is in the user-input field to the bot.
 */
function submit()
{
	sendToBot(DWRUtil.getValue('user-input'));
}

/**
 * Sends a non-empty message to the bot, and sets up
 * a callback (thanks to DWR) that will receive the
 * response and display it.
 */
function sendToBot(message)
{
	if (message != '')
	{
		bot.getResponse(displayResponse, message);
	}
}

/**
 * Displays a response sent from the bot, filling/changing
 * the appropriate boxes on the page, and leaves the cursor
 * in the input box.
 */
var displayResponse = function(response)
{
	// Put the response into the last-bot-reply div.
	//DWRUtil.setValue('last-bot-reply', response);
	
	// Grab the history box.
	var history = document.getElementById('div_chat');

	// Get the input.
	var input = DWRUtil.getValue('user-input');
	
	// Don't bother with empty inputs.
	if (input != '')
	{
		// Put the input into the last-user-input div.
		//DWRUtil.setValue('last-user-input', input);
		
		// Create a paragraph that will display the user input in the dialogue history.
		var userinput = document.createElement('p');
		userinput.setAttribute('class', 'user-input');

		// Make a label and append it.
		var userinputLabel = document.createElement('span');
		userinputLabel.setAttribute('class', 'label');
		userinputLabel.appendChild(document.createTextNode('you says: '));
		userinput.appendChild(userinputLabel);
		userinput.appendChild(document.createElement('br'));
		
		// Append the user input text.
		var userinputText = document.createElement('span');
		userinputText.setAttribute('class', 'chat_text');
		userinputText.appendChild(document.createTextNode(input));
		
		userinput.appendChild(userinputText);
		
		// Append this paragraph to the history.
		history.appendChild(userinput);
		
		// Blank out the user input.
		/*
		 * TODO:
		 * Need some way to notify the field that it has changed
		 * (so onchange will work right if the same value is
		 * typed again).
		 */
		DWRUtil.setValue('user-input', '');
	}
	
	// Create a paragraph that will display the reply in the dialogue history.
	var botreply = document.createElement('p');
	botreply.setAttribute('class', 'bot-reply');
	/**
 * Puts the cursor into the input field.
 */
function focusInput()
{
	var input = document.getElementById('user-input').focus();
}

/**
 * Sends whatever is in the user-input field to the bot.
 */
function submit()
{
	sendToBot(DWRUtil.getValue('user-input'));
}

/**
 * Sends a non-empty message to the bot, and sets up
 * a callback (thanks to DWR) that will receive the
 * response and display it.
 */
function sendToBot(message)
{
	if (message != '')
	{
		bot.getResponse(displayResponse, message);
	}
}

/**
 * Displays a response sent from the bot, filling/changing
 * the appropriate boxes on the page, and leaves the cursor
 * in the input box.
 */
var displayResponse = function(response)
{
	// Put the response into the last-bot-reply div.
	//DWRUtil.setValue('last-bot-reply', response);
	
	// Grab the history box.
	var history = document.getElementById('div_chat');

	// Get the input.
	var input = DWRUtil.getValue('user-input');
	
	// Don't bother with empty inputs.
	if (input != '')
	{
		// Put the input into the last-user-input div.
		//DWRUtil.setValue('last-user-input', input);
		
		// Create a paragraph that will display the user input in the dialogue history.
		var userinput = document.createElement('p');
		userinput.setAttribute('class', 'user-input');

		// Make a label and append it.
		var userinputLabel = document.createElement('span');
		userinputLabel.setAttribute('class', 'label');
		userinputLabel.appendChild(document.createTextNode('you says: '));
		userinput.appendChild(userinputLabel);
		userinput.appendChild(document.createElement('br'));
		
		// Append the user input text.
		var userinputText = document.createElement('div');
		userinputText.setAttribute('class', 'chat_text');
		userinputText.appendChild(document.createTextNode(input));
		
		userinput.appendChild(userinputText);
		
		// Append this paragraph to the history.
		history.appendChild(userinput);
		
		// Blank out the user input.
		/*
		 * TODO:
		 * Need some way to notify the field that it has changed
		 * (so onchange will work right if the same value is
		 * typed again).
		 */
		DWRUtil.setValue('user-input', '');
	}
	
	// Create a paragraph that will display the reply in the dialogue history.
	var botreply = document.createElement('p');
	botreply.setAttribute('class', 'bot-reply');
	
	// Make a label.
	var botreplyLabel = document.createElement('span');
	botreplyLabel.setAttribute('class', 'label');
	botreplyLabel.appendChild(document.createTextNode(botName + ' says: '));
        botreply.appendChild(botreplyLabel);
	botreply.appendChild(document.createElement('br'));
	
	var botreplyText = document.createElement('div');
	botreplyText.setAttribute('class', 'chat_text');
	botreplyText.innerHTML = response;
	
	botreply.appendChild(botreplyText);
	
	// Append this paragraph.
	history.appendChild(botreply);
	
	history.scrollTop = history.scrollHeight;
	
	// Scroll to the bottom (so the history will be visible).
	//window.scrollTo(0, 1000000);

	// Put the cursor back in the input field.
	focusInput();
}

function loadIframe(url) {
    var iframe = document.getElementById('ncbi_display');
    iframe.setAttribute('src', url);
}
	
	// Make a label.
	var botreplyLabel = document.createElement('span');
	botreplyLabel.setAttribute('class', 'label');
	botreplyLabel.appendChild(document.createTextNode(botName + ' says: '));
	botreply.appendChild(botreplyLabel);
	botreply.appendChild(document.createElement('br'));
	
	var botreplyText = document.createElement('span');
	botreplyText.setAttribute('class', 'chat_text');
	botreplyText.innerHTML = response;
	
	botreply.appendChild(botreplyText);
	
	// Append this paragraph.
	history.appendChild(botreply);
	
	history.scrollTop = history.scrollHeight;
	
	// Scroll to the bottom (so the history will be visible).
	//window.scrollTo(0, 1000000);

	// Put the cursor back in the input field.
	focusInput();
}

function loadIframe(url) {
    frames['ncbi_display'].location.href=url;
}