var DICT = "word/";
var SUB = "forms/newWDTForm.php";
var input, result, result2;
var http_request = false;
var http_request2 = false;
function makeRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { 
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { 
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      }
    }
  } 
  if (!http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }  
  http_request.onreadystatechange = alertContents;
  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}

function makeRequest2(url, parameters) {
  http_request2 = false;
  if (window.XMLHttpRequest) {
    http_request2 = new XMLHttpRequest();
    if (http_request2.overrideMimeType) {
      http_request2.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) {
    try {
      http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      }
    }
  } 
  if (!http_request2) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }  
  
  http_request2.onreadystatechange = suggestions;
  http_request2.open('GET', url + parameters, true);
  http_request2.send(null);
}


function alertContents() {
  if (input.length == 0) {
    document.getElementById('words').innerHTML = "";
    return;
  } 
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      result = http_request.responseText.split(',');
      var table = "<table id = 'tableWords'>"; 
      if (input.length == 0) {
        document.getElementById('words').innerHTML = "";
        return;
      }
      var match = false;
      for (i=0; i < result.length; i++) { 
        if (result[i].match(new RegExp("^" + input))) {
          var word = replace(result[i], " ", "+");
          table += "<tr><nobr>" + result[i].link(DICT + word) + "</nobr></tr>"; 
          match = true;
        }
      }  
      if (!match) {
        suggest(input);
      } else {
        table += "</table>";
          
        document.getElementById('words').innerHTML = table;        
      }    
    } else {
      alert('There was a problem with the request.');
    }
  }
}
function suggestions() {
  if (http_request2.readyState == 4) {
    if (http_request2.status == 200) {
      result2 = http_request2.responseText.split(',');
      var table1 = "<table id = 'notfound' width='400'>"; 
      table1 += "<tr>" + "No match found for <strong>" + input + "</strong>.</tr><tr> First try different spellings, then consider " +"submitting it".link(SUB) +  "</tr>";
      if (result2.length > 1) {
        table1 +="<tr>"+"You can also try the suggestions below" + "</tr>";
      }
      table1 += "</table>";
      var table2 = "<table id = 'tableWords'>";
      for (i=0; i < result2.length; i++) { 
        var word = replace(result2[i], " ", "+");
        table2 += "<tr><nobr>" + result2[i].link(DICT + word) + "</nobr></tr>";       
      }  
      table2 += "</table>";      
    }
    document.getElementById('notfound').innerHTML = table1;   
    document.getElementById('words').innerHTML = table2;            
  } 
}
 
function get(word) {
  word = word.toLowerCase();
  input = word;
  document.getElementById('notfound').innerHTML = "";
  document.getElementById('words').innerHTML = "";      
 
  if (word.charAt(0) >= 'a' && word.charAt(0) <= 'z') {
    makeRequest('get.php?w=' + word.charAt(0), '');
  } else {
    alertContents();
  }
}
function suggest(word) {
  word = word.toLowerCase();
  makeRequest2('suggest.php?w=' + word, '');
}

function replace(string,text,by) {
  var strLength = string.length, txtLength = text.length;
  if ((strLength == 0) || (txtLength == 0)) return string;
  var i = string.indexOf(text);
  if ((!i) && (text != string.substring(0,txtLength))) return string;
  if (i == -1) return string;
  
  var newstr = string.substring(0,i) + by;
  if (i+txtLength < strLength)
    newstr += replace(string.substring(i+txtLength,strLength),text,by);
  return newstr;
}
