summaryrefslogtreecommitdiff
path: root/lfpe.js
blob: bf7aa0eefb625c9bd6347eced150e0cc3caa3413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function CreateCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

function submit(e) {
  if (e.keyCode == 13) {
    e.preventDefault();
    Next();
  }

  return false;
}

function Next()
{
  url = "http://coltrane.cl.uni-heidelberg.de:60666/next";
  var pe = document.getElementById("trgt").value;
  if (pe != "") {
    var src = document.getElementById("src").value;
    url += "?example="+src+" %7C%7C%7C "+pe;
  }
  document.getElementById("translating_status").style.display = "block";
  var xhr = CreateCORSRequest('get', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }

  xhr.onload = function() {
    var x = xhr.responseText.split("\t");
    if (x == "fi") {
      document.getElementById("src").style.display = "none";
      document.getElementById("trgt").style.display = "none";
      document.getElementById("translating_status").style.display = "none";
      document.getElementById("next").innerHTML = "Thank you!";
      document.getElementById("next").disabled = true;
    } else {
      document.getElementById("src").value = x[0];
      document.getElementById("src").cols = x[0].length;
      document.getElementById("trgt").value = x[1];
      document.getElementById("trgt").cols = x[1].length;
      document.getElementById("translating_status").style.display = "none";
      document.getElementById("trgt").focus();
      document.getElementById("trgt").selectionStart = 0;
      document.getElementById("trgt").selectionEnd = 0;
    }
  };

  xhr.onerror = function() {
    alert('Error');
  };

  xhr.send();
}