class FormChecker
{
    var input = null;
    var span = null;
    var checkFunList = null;
    
    function this(inputId, errorSpanId, eventList)
    {
        this.input = self.$("#" + inputId);
        this.span = self.$("#" + errorSpanId);
        this.checkFunList = [];
        
        if (typeof(eventList) == "array" && eventList.length) 
        {
            for (var i = 0; i < eventList.length; i++) 
            {
                var evt = typeof(eventList[i]) == "string" ? eventList[i].toLowerCase() : "";
                if (evt == "blur" || evt == "keyup") 
                {
                    var checkerObj = this;
                    if (evt == "blur") 
                    {
                        this.input.onFocus = function(e)
                        {
                            if (e.type == Event.LOST_FOCUS) 
                                checkerObj.check();
                        };
                    }
                    else 
                    {
                        this.input.onKey = function(e)
                        {
                            if (e.type == Event.KEY_UP) 
                                checkerObj.check();
                        };
                    }
                }
            }
        }
    }
    
    function addCheckerFun(fun)
    {
        if (typeof(fun) == "function") 
            this.checkFunList.push(fun);
    }
    
    //Get check result
    function check()
    {
        for (var i = 0; i < this.checkFunList.length; i++) 
        {
            if (!this.checkFunList[i](this)) 
                return false;
        }
        return true;
    }
}

class AjaxChecker
{
    var elem = null;
    var lastVal = "";
    var state = "-1"; //-1checking, 1ok, 0bad, 2error
    
	function this(inputId)
    {
        this.elem = self.$("#" + inputId);
    }
    
    function result()
    {
        this.check();
        return this.state;
    }
    
	//When the text changed call this function
    function check()
    {
        var val = this.elem.value;
        if (val != "" && val != this.lastVal) 
        {
            this.state = "-1";
            this.lastVal = val;
            typeof(this.checkStart) == "function" && this.checkStart(this.elem);
        }
    }
    
    function checkStart(elem)
    {
    
    }
    
    //This function must be called inside checkEnd callback function
    function checkEndSuper(data)
    {
        if (data == "1" || data == "0") 
            this.state = data;
        else 
            this.state = "2";
    }
    
    //This function write for c++
    function checkEnd(data)
    {
        this.checkEndSuper(data);
    }
}