function PHPGenerator(reqFile) {
        this.requestFile = typeof reqFile == 'undefined' ? "ac.php?mode=1" : reqFile;
        this.httpReq = null;
        this.lastResp = "";
        this.procdResp = false;
        this.control = null;

        try {
                this.httpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        this.httpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(sc) {
                        this.httpReq = null;
                }
        }

        if (!this.httpReq && typeof XMLHttpRequest!="undefined") {
                this.httpReq = new XMLHttpRequest();
        }
}

PHPGenerator.prototype.requestList = function (acControl) {
    var sTextboxValue = acControl.value;
    var me = this;
//    this.control = acControl;

    if (sTextboxValue.length > 1) {

                if(this.httpReq&&this.httpReq.readyState!=0) {
                        this.httpReq.abort();
                }

                if(this.httpReq) {
                        var meReq = this.httpReq;

                        this.httpReq.open("GET",this.requestFile+"qu="+this.convert(sTextboxValue),true);
                        this.httpReq.onreadystatechange=function() {
                                if(meReq.readyState==4&&meReq.responseText) {
                                        me.procdResp = false;
                                        me.lastResp = meReq.responseText;
                                        me.processResponse();
                                }
                        };

                        this.httpReq.send(null);
                }

        }

};

PHPGenerator.prototype.convert = function (s) {
        if(encodeURIComponent) return encodeURIComponent(s);
        if(escape) return escape(s);
}

PHPGenerator.prototype.processResponse = function () {
        var rows = this.lastResp.split(";");
        var ret = {};
	var cnt = 0;

        this.procdResp = false;
        for (var i in rows) {
                ret[rows[i].split(":")[0]] = rows[i].split(":")[1];
		cnt++;
        }
        this.procdResp = true;

        this.update(ret, cnt);
};

PHPGenerator.prototype.selectRange = function (start, end) {
    if (start == end) return;
    if (this.control.contentHolder.createTextRange) {
	var sel = this.control.contentHolder.createTextRange();
	sel.moveStart("character", start);
	sel.moveEnd("character", end - this.control.contentHolder.value.length);
	sel.select();
    } else {
	this.control.contentHolder.setSelectionRange(start, end);
    }
    this.control.contentHolder.focus();
}

PHPGenerator.prototype.update = function (ret, cnt) {
    if (cnt == 0) return;

    var start = this.control.contentHolder.value.length;
    for(var i in ret) {
	this.control.idHolder.value = i;
	this.control.contentHolder.value = ret[i];
	break;
    }
    this.selectRange(start, this.control.contentHolder.value.length);
}

var ajax = null;
function updateAC(content, idHolder, type, level) {
    if (ajax === null) {
        ajax = new PHPGenerator('html/ac/'+type+'.php?mode='+level+'&');
	ajax.control = new Array();
	ajax.control.contentHolder = content;
        ajax.control.idHolder = document.getElementById(idHolder);
	ajax.reqLength = content.value.length;
	setTimeout("doUpdate()", 200);
    }
}

function doUpdate() {
    if (ajax == null) return;
    if (ajax.reqLength != ajax.control.contentHolder.value.length) {
	ajax.reqLength  = ajax.control.contentHolder.value.length;
	setTimeout("doUpdate()", 200);
    } else {
	ajax.requestList(ajax.control.contentHolder);
	ajax = null;
    }
}