var TagCheck = new function() {
  this.ajax = null;
  this.form = null;
  this.button = null;

  this.cleanup = function() {
    var self = TagCheck;
    self.form = null;
    self.button = null;
  };

  this.init = function() {
    var self = TagCheck;
    self.ajax = new Ajax();
    self.form = document.getElementById('frmTagCheck');
    self.button = document.getElementById('btnTagCheck');
    self.form.tagcode.focus();
    self.button.onclick = self.submitData;
    self.form.onsubmit = function() { return false; }
  };
  
  this.setPrompt = function(restype, status, msg, tag) {
    var self = TagCheck;
    var msgSpan = document.getElementById('spnMessage');
    while (msgSpan.childNodes.length > 0) {
      msgSpan.removeChild(msgSpan.firstChild);
    }
    if (status != '') {
      var st = document.createElement('strong');
      st.appendChild(document.createTextNode('Status:'));
      msgSpan.appendChild(st);
      msgSpan.appendChild(document.createTextNode('\u00A0'));
      var sp = document.createElement('span');
      if (restype != '') {
        sp.className = restype;
      }
      sp.appendChild(document.createTextNode(status));
      msgSpan.appendChild(sp);
      msgSpan.appendChild(document.createElement('br'));
    }
    msgSpan.appendChild(document.createTextNode(msg));
    if ((restype == 'ok') || (restype == 'warn') || (restype == 'lostcode')) {
      var safe = document.createElement('p');
      safe.className = restype;
      if (restype == 'ok') {
        safe.appendChild(document.createTextNode('THIS INSTRUMENT IS SAFE TO BUY'));
      } else if (restype == 'warn') {
        safe.appendChild(document.createTextNode('THIS INSTRUMENT IS NOT SAFE TO BUY'));
      }
      msgSpan.appendChild(safe);
      var lnk = document.createElement('a');
      lnk.href = 'tagcheck.php?tagcode=' + urlencode(tag);
      lnk.appendChild(document.createTextNode('Found out what this means...'));
      msgSpan.appendChild(lnk);
    }
  };

  this.keyup = function(e) {
    var self = TagCheck;
    if (!e) {
      e = window.event;
      var src = e.srcElement;
    } else {
      var src = e.target;
    }
    if (src.id =='tagcode') {
      if (e.keyCode != 13) {
        self.setPrompt('', '', '');
      }
      else {
        if (self.enabled) {
          self.submitData();
        }
      }
    }
  };
  
  this.submitData = function() {
    var self = TagCheck;
    var postData = '';
    postData = formData2QueryString(self.form);
    self.ajax.doPost('ajaxtagcheck.php', postData, self.handleTagCheckResp);
    self.setPrompt('', '', 'Checking...', '');
  };	
	
  this.handleTagCheckResp = function(str) {
    var self = TagCheck;
    var respArr = str.split(',');
    var respType = respArr[0];
    var respStatus = respArr[1];
    var respMsg = respArr[2];
    var respTag = respArr[3];
    self.setPrompt(respType, respStatus, respMsg, respTag);
  };
};

window.onunload = TagCheck.cleanup;
window.onload = TagCheck.init;
document.onkeyup = TagCheck.keyup;