(function() {

var qs = {};
qs.form = document.forms['quicksearch'];
qs.kw = qs.form.elements['kw'];
qs.url_kw = function() { return encodeURIComponent(kw.value); };
qs.link_pattern = function(pattern) { location.href = pattern.replace(/%kw/g, qs.url_kw()) };
qs.mkbtn = function(caption, pattern) {
  var btn = document.createElement('input');
  btn.type = 'button';
  btn.value = caption;
  btn.style.marginLeft = '0.5em';
  if (pattern != null) btn.onclick = function() { qs.link_pattern(pattern); };
  qs.form.appendChild(btn);
  return btn;
};

(function(icq_button) {
  icq_button.style.backgroundRepeat = 'no-repeat';
  icq_button.style.backgroundPosition = '2px center';
  icq_button.onclick = function() {
    icq_button.style.backgroundImage = '';
    var uin = 0;
    var nick = '';
    var kw = qs.kw.value.match(/^([0-9\-]+)([^0-9].*|)$/);
    if (kw != null) {
      uin = kw[1].replace(/[^0-9]/g, '') - 0;
      nick = kw[2].replace(/^[\s\(:=]+/, '').replace(/[\s\):]+$/, '');
      if (nick == '') nick = uin;
    }
    if (uin > 0) {
      icq_button.style.paddingLeft = '20px';
      icq_button.style.backgroundImage = 'url(http://status.icq.com/online.gif?img=27&icq=' + uin + ')';
      icq_button.value = 'ICQ: ' + nick;
    } else {
      icq_button.style.paddingLeft = '';
      icq_button.value = 'ICQ: invalid UIN';
    }
  };
})(qs.mkbtn('ICQ Status'));

})();
