/**
 * 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 submituj()
{
	if (document.getElementById('user-input').value != '')
	{
	// dodatkowe zabezpieczenie wielokrotnego wysylania tego samego	
	if (document.getElementById('user-input').value != document.getElementById('last-user-input').value)
	{
	
	sendToBot(document.getElementById('user-input').value);
	}
	}
}

function wylosujpodpowiedz()
{	
	var podp = document.getElementById('podpowiedz1');
	if ('' == podp.innerHTML)	
	{	
		var i = Math.floor(Math.random()*3)
		
		if (1 == i)
		{
		podp.innerHTML = 'Która jest godzina?';
		}
		else
		{
		podp.innerHTML = 'Co wiesz?';
		}
	}	
	//document.getElementById('kropka1').innerHTML = '&bull; ';
}

/**
 * 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)
{
	// Usuwanie podpowiedzi	
	document.getElementById('podpowiedz1').innerHTML = '';
	document.getElementById('kropka1').innerHTML = '';

	if (message != '')
	{

	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else // for older IE 5/6
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	message = message.replace(/\s+/g, "_");

	xhttp.open("GET","bot4.php?q="+message,false);	
	xhttp.send("");
	xmlDoc=xhttp.responseXML;

	//document.write("XML document loaded into an XML DOM Object.");

	txt=xmlDoc.getElementsByTagName("output")[0].childNodes[0].nodeValue;


	//document.write(txt);

	// Wywolanie ostatniej funkcji
	displayResponse(txt);
	
	}
}
  
/**
 * Displays a response sent from the bot, filling/changing
 * the appropriate boxes on the page, and leaves the cursor
 * in the input box.
 */

//////////////

var czyzglos = 0;

var displayResponse = function(response)
{
	zrobionaAktywnosc();
	// Put the response into the last-bot-reply div.
	//DWRUtil.setValue('last-bot-reply', response);
	startTyping(response,150,'last-bot-reply');
	// Grab the history box.
	var history = document.getElementById('dialogue-history');

	// Get the input.
	var input = document.getElementById('user-input').value;
	
	
	// Don't bother with empty inputs.
	if (input != '')
	{
		// Put the input into the last-user-input div.
		///// Tu byla linia z DWR - podstawienie		
		//last-user-input = input;
		document.getElementById('last-user-input').innerHTML = input;
		document.getElementById('user-input').value = "";

		if (czyzglos > 0)
		{
			doZglosLink(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('Ty > '));
		userinput.appendChild(userinputLabel);
		
		// Append the user input text.
		userinput.appendChild(document.createTextNode(input));
		
		// Append this paragraph to the history.
		history.insertBefore(userinput,history.firstChild);
		//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');

	// We set it with (non-standard) .innerHTML so that HTML in the reply will be displayed.  Other ideas?
	botreply.innerHTML = response;
	
	// Make a label.
	var botreplyLabel = document.createElement('span');
	botreplyLabel.setAttribute('class', 'label');
	botreplyLabel.appendChild(document.createTextNode('Bot > '));
	
	// Insert the label before the already-inserted text/html.
	botreply.insertBefore(botreplyLabel, botreply.childNodes.item(0));
	
	// Append this paragraph.
	history.insertBefore(botreply,history.firstChild);

//	history.appendChild(botreply);
	
	// Scroll to the bottom (so the history will be visible).
	window.scrollTo(0, 1000000);

	wylosujpodpowiedz();

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


