////SURNAME
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('surname','surnameChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=10&maxCount=10');}
//);
////CITY
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('city','cityChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=20&maxCount=10');}
//);
////INDUSTRY
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('industry','industryChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=30&maxCount=10');}
//);
////COMPANY
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('company','companyChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=40&maxCount=10');}
//);
////SCHOOL
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('school','schoolChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=50&maxCount=10');}
//);
////MAJOR
//document.observe('dom:loaded',function(){
//    new RemoteAutocompleter('major','majorChoices','/Profile.do?action=GET_AUTOCOMP&categoryType=60&maxCount=10');}
//);

var RemoteAutocompleter = Class.create(Ajax.Autocompleter,{
    initialize:function($super,element, update, array, options){
        this.autocomplist = null;
        $super(element, update, array, options);
        Event.observe(this.element, 'focus', this.onKeyPress.bindAsEventListener(this));
        Event.observe(this.element, 'input', this.onKeyPress.bindAsEventListener(this));
    },
  
    onComplete: function(transport){
        var json = null;
        if(transport.RETURN){
            json = transport;
        }
        else{
            json = transport.responseText.evalJSON(true);
        }
        //      var json = transport.responseText.evalJSON(true);
        if(!json.RETURN){
            return;
        }
        var list = json.list;
        this.autocomplist = list;

        if(list.length == 1 && (typeof list[0].chinese == 'undefined' || typeof list[0].english == 'undefined')){
            if(this.element.value == list[0].chinese || this.element.value == list[0].english) return;
        }
        var content = "<ul>";
        for(var i = 0; i < list.length; i++){
            content += "<li><table width='100%' border='0'><tr>";
            if(typeof(list[i].chinese) != 'undefined'){
                if(typeof(list[i].english) != 'undefined'){
                    content += "<td align='left'><strong>" + this.interceptStr(list[i].chinese, 12) + "</strong></td>";
                    content += "<td align='right'><strong>" + this.interceptStr(list[i].english, 25) + "</strong></td>";
                }
                else{
                    content += "<td align='left'><strong>" + this.interceptStr(list[i].chinese, 28) + "</strong></td>";
                }
            }
            else{
                content += "<td align='left'><strong>" + this.interceptStr(list[i].english, 55) + "</strong></td>";
            }
            content += "</tr></table></li>";
        }
        content += "</ul>";
        this.updateChoices(content);
    },
      
    getUpdatedChoices: function() {
        this.startIndicator();
        if(this.element.value == '' && (this.url.indexOf("categoryType=20") != -1)){
            var transport = {
                "RETURN":true,
                "list":[{
                    "id":349,
                    "chinese":"北京"
                },{
                    "id":932,
                    "chinese":"上海"
                },{
                    "id":1138,
                    "chinese":"广州"
                },{
                    "id":503,
                    "chinese":"天津"
                },{
                    "id":838,
                    "chinese":"青岛"
                },{
                    "id":1193,
                    "chinese":"深圳"
                },{
                    "id":8885,
                    "chinese":"香港"
                }]
                };
            this.onComplete(transport);
            return;
        }
        //服务器端取得表单数据不依赖于页面,this.getToken()做了trim()的结果
        var entry = encodeURIComponent("input") + '=' +
        encodeURIComponent(this.getToken());
        this.options.parameters = this.options.callback ?
        this.options.callback(this.element, entry) : entry;

        if(this.options.defaultParams)
            this.options.parameters += '&' + this.options.defaultParams;
        new Ajax.Request(this.url, this.options);
    },
  
    updateElement: function(selectedElement) {
        if (this.options.updateElement) {
            this.options.updateElement(selectedElement);
            return;
        }
        var value = '';
        //    value = typeof this.autocomplist[this.index].chinese == 'undefined' ? this.autocomplist[this.index].english : this.autocomplist[this.index].chinese;
        if(typeof(this.autocomplist[this.index].chinese) != 'undefined'){
            if(typeof(this.autocomplist[this.index].english) != 'undefined'){
                value = this.autocomplist[this.index].chinese + " (" + this.autocomplist[this.index].english + ")";
            }
            else{
                value = this.autocomplist[this.index].chinese;
            }
        }
        else{
            value = this.autocomplist[this.index].english;
        }
        this.element.value = value;
        this.oldElementValue = this.element.value;
        this.element.focus();
        if (this.options.afterUpdateElement)
            this.options.afterUpdateElement(this.element, selectedElement);
    },
  
    onObserverEvent: function() {
        this.changed = false;
        this.tokenBounds = null;
        this.getUpdatedChoices();
        this.oldElementValue = this.element.value;
    },
  
    getId: function(value){
        if(value == null || value == ""){
            return '0';
        }
        if(this.autocomplist == null){
            return '0';
        }
        if(this.autocomplist.length == 0 || this.autocomplist.length > 1){
            return '0';
        }
        

        var valuei = '';
        if(typeof(this.autocomplist[0].chinese) != 'undefined'){
            if(typeof(this.autocomplist[0].english) != 'undefined'){
                valuei = this.autocomplist[0].chinese + " (" + this.autocomplist[0].english + ")";
                if(value  == this.autocomplist[0].chinese || value  == this.autocomplist[0].english || value == valuei){
                    return this.autocomplist[0].id;
                }
            }
            else{
                if(value  == this.autocomplist[0].chinese){
                    return this.autocomplist[0].id;
                }
            }
        }
        else{
            if(value  == this.autocomplist[0].english){
                return this.autocomplist[0].id;
            }
        }
        return '0';

        if(this.autocomplist == null){
            return '0';
        }
    },
  
    interceptStr: function(str, length){
        if(str.length >= length){
            return str.substring(0, length-1) + "...";
        }
        else
            return str;
    }
})
