function LiveSearch(field, div) { this.construct(field, div) }
LiveSearch.prototype = {
  field:   null,
  div:     null,
  prevQ:   '',
  prevT:   null,
  timeout: null,
  
  construct: function(field, div) {
    this.field = field;
    this.div = div;
    this.prevT = new Date();
    
    var th = this;
    addEvent(field, 'onkeydown', function(e) {
      th.prevT = new Date();
      if (e.keyCode == 13) th.onchangeControl(this.value, 100);
      return true;
    })
    addEvent(field, 'onkeyup', function(e) { 
      if (e.keyCode == 13) return true;
      th.onchangeControl(this.value, e.keyCode==32? 1000 : null);
      return true;
    })
    addEvent(field, 'onfocus', function() {
      // stupid Mozilla sometimes loses focus on DIV repainting :-(
      th.focused = true;
      return true;
    })
    addEvent(field, 'onblur', function() {
      th.onchangeControl(field.value, 0);
      th.focused = false;
      return true;
    })
  },
  
  onchangeControl: function(text, dt) {
    var t = new Date();
    var wait = 0;
    if (dt == null) dt = 2000;
    
    if (t.getTime() - this.prevT.getTime() < dt) {
      this.prevT = t;
      wait = dt;
    }
    
    var th = this;
    if (this.timeout) { clearTimeout(this.timeout); this.timeout=null; }
    this.timeout = setTimeout(function() { th.prevT = t; th.timeout=null; th.onchange(text) }, wait);
  },
  
  onchange: function(n, force) {
  	if(this.field.id == 'fnic'){ // nic field
	  	if(this.checkNic(n) != null){
	    	if (n != this.prevQ && n != "") {
	      	this.prevQ = n;
	      	var req = new JsHttpRequest();
	      	req.onreadystatechange = function() {
	        	if (req.readyState == 4) {
	        		returnObjById('fnic').style.border = (req.responseJS.state == 1) ? "3px solid #00cc00" : "3px solid #cc0000";
	        		returnObjById('nic_name').value = (req.responseJS.state == 1) ? 1 : 0;
	        	}
	      	}
	      	req.open(null, '/new_nick.html', true);
	    		req.send( { nic: n} );
	    	}
	  	}else{
	        	returnObjById('fnic').style.border = "3px solid #cc0000";
	        	returnObjById('nic_name').value = 0;
	  	}
  	}else if(this.field.id == 'email'){ // email field
	  	if(this.checkEmail(n) != null){
	    	if (n != this.prevQ && n != "") {
	      	this.prevQ = n;
	      	var req = new JsHttpRequest();
	      	req.onreadystatechange = function() {
	        	if (req.readyState == 4) {
	        		returnObjById('email').style.border = (req.responseJS.state == 1) ? "3px solid #00cc00" : "3px solid #cc0000";
	        		returnObjById('email_value').value 	= (req.responseJS.state == 1) ? 1 : 0;
	        	}
	      	}
	      	req.open(null, '/new_email.html', true);
	    		req.send( { email: n} );
	    	}
	  	}else{
	        	returnObjById('email').style.border = "3px solid #cc0000";
	  	}
  	}
  },

  checkEmail: function(n){
  	return n.match(/^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i);
  },
    
  checkNic: function(n){
  	return n.match(/^[a-zA-Z-0-9]{2,15}$/i);
  }
};

