
function ComboAdapter()
{
    this.select = function(code, name, args) {
        if (this.onselectBef != null) {
            if (this.onselectBef(code, name, this.currentCtrl, args) == false)
                return;
        }

        try {
            var row = this.currentCtrl.parentNode.parentNode;

            row.all[this.nameId].value = name;

            var temp = row.all[this.codeId];
            temp.value = code;
            temp.title = name;
        }
        catch (e) {
            $(this.nameId).value = name;

            var temp = $(this.codeId);
            temp.value = code;
            temp.title = name;
        }


        if (this.onselect != null)
            this.onselect(code, name, this.currentCtrl, args);
    }
    this.tag = "";
    this.select1 = function(province,city,county)
    {
        try
        {
            $(this.tag + "_Province").value = province[1];
            $(this.tag + "_City").value     = city[1];            
        }
        catch(ex){}
        try
        { 
            $(this.tag + "_Area_Code").value= county[0];
            $(this.tag + "_County").value   = county[1];            
        }
        catch(ex){}
                
        if(this.onselect != null)
            this.onselect();
    }

    this.onselect = null;
    this.onselectBef = null;
    
    this.list = null;
    this.locked = false;
    
    this.currentCtrl = null;
    this.nameId = null;
    this.codeId = null;
    this.src    = "";
    this.width  = 0;
    this.height = 0;
    
    this.showed = false;
    this.show = function(ctrl, isUp)
    {            
        if(isUp == null)
            isUp = true;
                        
        var frm = this.list;
        
        if(frm.src == null || frm.src.length == 0)
            frm.src = this.src;
            
        var p = Point.Get(ctrl);
                
        var body = document.body;
        
        if(body.scrollHeight < p.Y + this.height)
            isUp = false;
        
        if(isUp)
            frm.style.top  = p.Y + 21;
        else
            frm.style.top  = p.Y - this.height;
            
        
        if(parseInt(frm.style.top) < 0)
            frm.style.top = 0;
            
        //style.posWidth
        //
        if(body.scrollWidth < (p.X + this.width))
            frm.style.left = p.X + ctrl.offsetWidth - this.width - 2;
        else
            frm.style.left = p.X;
        
            
        frm.style.display = "";
        
        this.currentCtrl  = ctrl;
        
        if(ComboAdapter.CurrentAdp != this)
            ComboAdapter.CurrentAdp.hide();
        
        ComboAdapter.CurrentAdp = this;
        this.showed = true;
        
        if(this.onshow != null)
            this.onshow();
    }
    
    this.hide = function()
    {
        this.list.style.display = "none";
        this.showed = false;
    }

    this.bind = function(codeId, nameId, width, height, src, ctrlId) {
        this.codeId = codeId;
        this.nameId = nameId;
        this.width = width;
        this.height = height;
        this.src = src;

        if (!ctrlId)
            ctrlId = this.nameId;

        this._buildFrm(codeId + "_" + nameId, src);

        var ctrl = $(ctrlId);

        ctrl.oncontextmenu = function() {
            $(nameId).value = '';
            $(codeId).value = '';
            return false;
        };

        this._dobind(ctrl);
    }
    
    this.bind1 = function(tag, width, height, src)
    {
        this.tag = tag;
        this.width  = width;
        this.height = height;
        this.src    = src;
        
        this._buildFrm(tag + "_pcc", src);
        
        var ctrl = $(tag + "_County");
        
        this._dobind(ctrl);
        
    }
    this._dobind = function(ctrl)
    {
        var my    = this;
        ctrl.onclick = function()
        {
            if(my.locked == false)
            {
                my.locked = true;
                
                if(my.showed)
                    my.hide();
                else
                    my.show(ctrl);
                    
                my.locked = false;
            }            
        }
        
        ComboAdapter.CurrentAdp = this;
    }
    
    this._buildFrm = function(name, src)
    {
        if(this.list != null)
        {
            if(this.showed)
            {
                if(this.list.src != src)
                    this.list.src = src;
            }
            else
                this.list.src = "";
            
            return;
        }
            
        this.list = document.createElement("<iframe name='"+name+"' id='"+name+"'></iframe>"); 
        var frm = this.list;
        
        frm.name = name;
        frm.id   = frm.name;  
        
        frm.frameborder = 0;
        frm.scrolling   = "no";
        
        frm.style.cssText =  "border:solid 1px black; position:absolute;z-index:98;";
       
        frm.style.width  = this.width + "px";
        frm.style.height = this.height + "px";
        frm.style.display = "none";  
        
        document.forms[0].appendChild(frm);
    }
}
ComboAdapter.CurrentAdp = null;
ComboAdapter.Selected   = function(code, name, args)
{
    if(ComboAdapter.CurrentAdp != null)
        ComboAdapter.CurrentAdp.select(code, name, args);
}
ComboAdapter.Selected1 = function(province,city,county)
{
    if(ComboAdapter.CurrentAdp != null)
        ComboAdapter.CurrentAdp.select1(province,city,county);
}

ComboAdapter.Hide = function()
{
    if(ComboAdapter.CurrentAdp != null)
        ComboAdapter.CurrentAdp.hide();
}

