var _ctrycity_rctrydescription="description";
var _ctrycity_rctrycode="ctrycode";
var _ctrycity_rcitydescription="description";
var _ctrycity_rcitycode="citycode";

function CtryCity(container){
    this.container = container;
    this.container.handler = this;

    this.ctryarray = ctry_arr;
    this.cityarray = city_arr;
    this.ctryval = null;
    this.cityval = null;
    this.ctryname = null;
    this.cityname = null;
    this.cityothername = null;
    this.cityothervalue = null;
    this.country = null;
    this.city = null;
    this.cityother = null;
    this.readonly = null;
    this.theTable = null;
    this.tableRow = null;

    if (typeof container.ctryval != "undefined"){
        this.ctryval = container.ctryval;
    }

    if (typeof container.cityval != "undefined"){
        this.cityval = container.cityval;
    }

    if (typeof container.ctryname != "undefined"){
        this.ctryname = container.ctryname;
    }

    if (typeof container.cityname != "undefined"){
        this.cityname = container.cityname;
    }

    if (typeof container.cityothername != "undefined"){
        this.cityothername = container.cityothername;
    }

    if (typeof container.cityothervalue != "undefined"){
        this.cityothervalue = container.cityothervalue;
    }

    if (typeof container.country != "undefined"){
        this.country = container.country;
    }

    if (typeof container.city != "undefined"){
        this.city = container.city;
    }

    if (typeof container.cityother != "undefined"){
        this.cityother = container.cityother;
    }

    if (typeof container.readonly != "undefined"){
        this.readonly = container.readonly;
    }

    this.createTable();
    this.createCtryElem();
    this.createCityElem();
    this.createCityOtherName();

    if(this.readonly == "false" && this.cityname != null){
        this.country.attachEvent("onchange", CtryCity_relatedCombox);
        this.city.attachEvent("onchange", CtryCity_relatedCityOther);
        this.city.attachEvent("onclick", CtryCity_pushEmptyCityOther);
        this.city.attachEvent("onblur", CtryCity_pushEmptyCityOther);
    }
}

CtryCity.prototype.createTable = function(){
    this.theTable = document.createElement("TABLE");
    this.theTable.width = "100%";
    this.theTable.cellPadding = 0;
    this.theTable.cellSpacing = 0;

    this.tableRow = this.theTable.insertRow();
    this.container.appendChild(this.theTable);
}

CtryCity.prototype.createCtryElem = function(){
    var aCell = this.tableRow.insertCell();
    if(this.cityothername!=null){
        aCell.width="30%";
    }else{
        aCell.width="50%";
    }

    if(this.readonly == "false"){
        var ctry=document.createElement("SELECT");
        aCell.appendChild(ctry);
        ctry.name = this.ctryname;
        var oOption = document.createElement("OPTION");
        ctry.options.add(oOption);
        oOption.value = "";
        oOption.innerText = "";

        //get pos
        var indexCtry;
        var indexDesc;
        var firstPos;
        if(this.ctryarray){
            firstPos = this.ctryarray[0];
            for(var i=0; i<firstPos.length; i++){
                if(firstPos[i]==_ctrycity_rctrycode){
                    indexCtry=i;
                }
                if(firstPos[i]==_ctrycity_rctrydescription){
                    indexDesc=i;
                }
            }

            //create other option element
            var optArray;
            for(var i=1; i<this.ctryarray.length; i++){
                optArray = this.ctryarray[i];
                oOption = document.createElement("OPTION");
                ctry.options.add(oOption);
                oOption.value = optArray[indexCtry];
                oOption.innerText = optArray[indexDesc];


                if(this.ctryval == optArray[indexCtry]){
                    oOption.selected="true";
                }
            }
        }

        //return obj
        this.country = ctry;
    }else {
        //create hidden value
        var hid=document.createElement("INPUT");
        hid.type="hidden";
        hid.value=this.ctryval;
        hid.name=this.ctryname;
        aCell.appendChild(hid);

        //find hidden description
        var indexDesc,indexCtry;
        if(this.ctryarray){
             firstPos = this.ctryarray[0];
        }
        for(var i=0; i<firstPos.length; i++){
            if(firstPos[i] == _ctrycity_rctrydescription){
                indexDesc = i;
            }

            if(firstPos[i] == _ctrycity_rctrycode){
                indexCtry = i;
            }
        }

        var txt=document.createElement("SPAN");
        txt.className = "readonlyStyle";
        for(var i=0; i<this.ctryarray.length; i++){
            optArray=this.ctryarray[i];
            if(this.ctryval==optArray[indexCtry]){
                txt.innerText=optArray[indexDesc];
            }
        }

        aCell.appendChild(txt);
        this.country = txt;
    }
    this.country.handler = this;
}

CtryCity.prototype.createCityElem = function(){
    if (this.cityname != null){
        var aCell = this.tableRow.insertCell();
        if(this.cityothername != null){
            aCell.width="30%";
        }else{
            aCell.width="50%";
        }

        if(this.readonly == "false"){
            var city = document.createElement("SELECT");
            city.name = this.cityname;
            aCell.appendChild(city);
            this.city = city;
            this.reflushCity();
        }else {
           var hid = document.createElement("INPUT");
           hid.type = "hidden";
           hid.name = this.cityname;
           hid.value = this.cityval;
           aCell.appendChild(hid);
           this.city=hid;

           //create text
           var txt = document.createElement("SPAN");
           txt.className = "readonlyStyle";
           txt.innerText = this.getCityDesc();
           aCell.appendChild(txt);
       }

       this.city.handler = this;
   }
}

CtryCity.prototype.createCityOtherName = function(){
    if(this.cityothername != null){
        var aCell = this.tableRow.insertCell();
        aCell.width="40%";

        if(this.readonly == "false"){
            var input = document.createElement("INPUT");
            input.type = "text";
            input.className = "cssText";
            input.name = this.cityothername;
            input.value = this.cityothervalue;
            input.maxlength = 35;
            aCell.appendChild(input);
            this.cityother = input;
        } else {
            var hid = document.createElement("INPUT");
            hid.name = this.cityothername;
            hid.value = this.cityothervalue;
            hid.type = "hidden";
            aCell.appendChild(hid);
            this.cityother=hid;

            var txt = document.createElement("SPAN");
            txt.className = "readonlyStyle";
            txt.innerText = this.getCityDesc();
            aCell.appendChild(txt);
        }

        this.cityother.handler = this;
    }
}

CtryCity.prototype.reflushCity = function(){
    if(this.city == null || !this.city){
        return;
    }

    //remove all options
    if(this.city.options != null){
        for(var i=this.city.options.length-1; i>=0; i--){
            this.city.options.remove(i);
        }
    }

    //create blank select
    var oOption = document.createElement("OPTION");
    this.city.options.add(oOption);
    oOption.value = "";
    oOption.innerText = "";

    //create other option element
    if(this.cityarray){
        //get city code and description array by country value;
        var optArray = this.filter();
        if(optArray != null && optArray.length > 0){
            for(var i=0; i<optArray.length; i++){
                var oOption = document.createElement("OPTION");
                this.city.options.add(oOption);
                oOption.value = optArray[i][0];
                oOption.innerText = optArray[i][1];

                //auto selected
                if(this.cityval == oOption.value){
                    oOption.selected = "true";
                }
            }
        }
    }
}

CtryCity.prototype.filter = function(){
    var retlist=new Array()
    var idx1,idxCode,idxDesc,len=0;
    var idxlist=this.cityarray[0];

    for(i=0;i<idxlist.length;i++){
        if(_ctrycity_rctrycode == idxlist[i]){
            idx1 = i;
        }
        if(_ctrycity_rcitycode == idxlist[i]){
            idxCode = i;
        }
        if(_ctrycity_rcitydescription == idxlist[i]){
            idxDesc = i;
        }
    }

    var ctryvalue = this.country.value;
    for(j=1; j<this.cityarray.length; j++){
        idxlist = this.cityarray[j];
        if(idxlist[idx1] == ctryvalue){
            retlist[len] = new Array();
            retlist[len][0] = idxlist[idxCode]
            retlist[len][1] = idxlist[idxDesc]
            len++;
        }
    }

    return retlist;
}

CtryCity.prototype.release = function(){
    this.container = undefined;

    this.ctryarray = undefined;
    this.cityarray = undefined;
    this.ctryval = undefined;
    this.cityval = undefined;
    this.ctryname = undefined;
    this.cityname = undefined;
    this.cityothername = undefined;
    this.cityothervalue = undefined;
    this.country = undefined;
    this.city = undefined;
    this.cityother = undefined;
    this.readonly = undefined;
    this.theTable = undefined;
    this.tableRow = undefined;
}


CtryCity.prototype.getCityDesc = function(){
    var idx1 = -1;
    var idxCode = -1;
    var idxDesc = -1;
    var rtDesc="";
    var idxlist = this.cityarray[0];

    for(var i=0; i<idxlist.length; i++){
        if(_ctrycity_rctrycode == idxlist[i]){
            idx1=i;
        }
        if(_ctrycity_rcitycode == idxlist[i]){
            idxCode=i;
        }
        if(_ctrycity_rcitydescription == idxlist[i]){
            idxDesc=i;
        }
    }

    var val1 = this.ctryval;
    var val2 = this.cityval;
    if(idx1>-1 && idxCode>-1 && idxDesc>-1){
        for(var j=1; j<this.cityarray.length; j++){
            idxlist = this.cityarray[j];
            if(idxlist[idx1] == val1 && idxlist[idxCode] == val2){
                rtDesc = idxlist[idxDesc];
            }
        }
    }
    return rtDesc + " ";
}


CtryCity_relatedCombox = function(){
    var handler = event.srcElement.handler;
    handler.reflushCity();
    if(handler.cityother != null){
        handler.cityother.value = "";
    }
}

CtryCity_relatedCityOther = function(){
    var handler = event.srcElement.handler;
    if(handler.cityother != null){
        handler.cityother.value = handler.city.options[handler.city.selectedIndex].innerText;
    }
}

CtryCity_pushEmptyCityOther = function(){
    var handler = event.srcElement.handler;
    if(handler.cityother != null && (handler.cityother.value == null || handler.cityother.value == '')){
        handler.cityother.value = handler.city.options[handler.city.selectedIndex].innerText;
    }
}