var xmlhttpreq = null;
 
function GetHTTPRequest() {
  var xmlhttp = false;
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          xmlhttp = false;
        }
      }
    }
  return xmlhttp;
}
 
xmlhttpreq = GetHTTPRequest();
 
function StartRequest(worteingabe) {
	
	//muss wegen russisch und umlaute etc.
	function encode_utf8( s ) { return unescape( encodeURIComponent( s ) );}
	worteingabe = encode_utf8(worteingabe);
	
  // Persistente Verbindung �ffnen
  xmlhttpreq.open('POST', 'auswertung.php', true);
  xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttpreq.onreadystatechange = handle_response;
  
  var queryStr = 'wortspeicher=' + escape(worteingabe);
  xmlhttpreq.send(queryStr);
    
}

function change_username() {
  //muss wegen russisch und umlaute etc.
  function encode_utf8( s ) { return unescape( encodeURIComponent( s ) );}
  username = encode_utf8(document.getElementById('input_benutzername').value);
	
  // Persistente Verbindung �ffnen
  xmlhttpreq.open('POST', 'change_username.php', true);
  xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttpreq.onreadystatechange = handle_response_username;
 
  var queryStr = 'username=' + escape(username);
  xmlhttpreq.send(queryStr);
}

function update_highscore()
{
  // Persistente Verbindung �ffnen
  xmlhttpreq.open('POST', 'highscore.php', true);
  xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttpreq.onreadystatechange = handle_response_highscore;
  xmlhttpreq.send(null);
}
	
function handle_response_highscore() {
	document.getElementById('highscore').innerHTML = xmlhttpreq.responseText;
}


function handle_response_username() {
	document.getElementById('user_name').innerHTML = xmlhttpreq.responseText;
} 
 
function handle_response() {
	document.getElementById('auswertung').style.display = 'block';
	document.getElementById('auswertung').innerHTML = xmlhttpreq.responseText;
 	document.getElementById('restart').innerHTML = '<a href="http://schnell-schreiben.de/stv2/">Nochmal versuchen</a>';
	
	document.getElementById('zeitfenster').style.display = 'none';
	document.getElementById('eingabe').style.display = 'none';
	document.getElementById('vorgabe').style.display = 'none';
	document.getElementById('vorgabe2').style.display = 'none';
}
