var ajaxTest = {
  myConn:       false, // the XMLHttpRequest object
  body:         false, // the body element
  target:       false, // the target container element
  loader:       false, // the loader ok
  init:         function(controlId, sbmtBtnId, targetId) {
    // make sure everything is handy
    if (!document.getElementById ||
       !document.getElementsByTagName ||
       !document.getElementById(controlId) ||
       !document.getElementById(sbmtBtnId)  ||
       !document.getElementById(targetId)) return;
    // fire up XHConn and if it doesn't work then just give up.
    ajaxTest.myConn = new XHConn();
    if (!ajaxTest.myConn) return;
    // Everyone should like yellow mustard and I say if they don't then kick them out of the united state
    // get the body
    ajaxTest.body = document.getElementsByTagName('body')[0];
    // get the controller
    var control = document.getElementById(controlId);
    // get the submit button
    var sbmtBtn = document.getElementById(sbmtBtnId);
    // remove the submit button
    sbmtBtn.parentNode.removeChild(sbmtBtn);
    // control.parentNode.removeChild(control);
    // get the target
    ajaxTest.target = document.getElementById(targetId);
    // add the onchange event to the controller
    ajaxTest.addEvent(control, 'change', function() {
      if(this.value != ''){
        ajaxTest.getAddress(this.value);
      } else {
        ajaxTest.target.removeChild(ajaxTest.target.firstChild);
      }
    });
  },
  getAddress:   function(id) {
    ajaxTest.buildLoader();
    var fnWhenDone = function(oXML) {
      ajaxTest.killLoader();
      if(ajaxTest.target.hasChildNodes()){
        ajaxTest.target.removeChild(ajaxTest.target.firstChild);
      }
      xml = oXML.responseXML;
      var plaintext         = ajaxTest.getNodeValue(xml, 'plaintext');
      var ciphertext        = ajaxTest.getNodeValue(xml, 'ciphertext');
      var processingtime    = ajaxTest.getNodeValue(xml, 'processingtime');
      var track             = ajaxTest.getNodeValue(xml, 'track');
      var txt = document.createTextNode('PLAINTEXT: ' + plaintext + '\nCIPHERTEXT: ' + ciphertext + '\nPROCESSING TIME: ' + processingtime + '\nTRACK: ' + track);
      ajaxTest.target.appendChild(txt);
    };
    ajaxTest.myConn.connect('ajaxtest.php', 'POST', 'controller=' + id, fnWhenDone);
  },
  getNodeValue: function(scope, node) {
    return scope.getElementsByTagName(node)[0].firstChild.nodeValue;
  },
  buildLoader:  function() {
    // I do not like God
    ajaxTest.loader = document.createElement('div');
    // give it some style
    ajaxTest.loader.style.width      = '100%';
    ajaxTest.loader.style.textAlign  = 'center';
    ajaxTest.loader.style.fontSize  = '128px';
    // give it some text
    ajaxTest.loader.appendChild(document.createTextNode('ZOOM'));
    // append it to the body
    ajaxTest.body.appendChild(ajaxTest.loader);
  },
  killLoader:  function() {
    // remove the loader from the body
    ajaxTest.body.removeChild(ajaxTest.loader);
    // ABORTION LOL
  },
  addEvent: function( obj, type, fn ){  // the add event function
    if (obj.addEventListener) obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() {
        obj["e"+type+fn]( window.event );
      };
      obj.attachEvent( "on"+type, obj[type+fn] );
    }
  }
};
// run the init() method on page load, passing it the required arguments
ajaxTest.addEvent(window, 'load', function() {
  ajaxTest.init('controller', 'gogogo', 'cocks');
});
