//include ddl.js

//create select tag for select city

var cityId=0; //save the selected city id

//starter function
function createSelctCity(idplaceholder,src){
    
    var odivplaceholder=$m(idplaceholder);
    $(odivplaceholder).addClass('divselcityc');
    
   _loadCityFromServer(0,odivplaceholder,src);
    
}



//load cities info from server
//create the select and so on
//this function create an full child
function _loadCityFromServer(parentcityid,odiv,src){//odiv is place holder
    
    //cleare odiv
    odiv.innerHTML='';
    
    var oselect=$c('select');
    odiv.appendChild(oselect);
    if($m('selCity')==null){oselect.name= oselect.id='selCity';}
    
    oselect.phDiv=$c('div');//place holder for child select city
    odiv.appendChild(oselect.phDiv);
    
    
    ddl(oselect);//jslib/control/ddl.js
    
    
    //get info from server jquery
     oselect.addItem('loading...',parentcityid,true);
     $.get(src, { parentcityid: parentcityid },function(data){pf(oselect,data);} );
    
    
        var pf=function(oselect,data){
            
                var cities;
                cities=eval( '('+ data+')' ); //return text that is responded
                                                
                //clear all option in select
                oselect.clearItem();
                                 
                //if city have no child , only return
                if(cities.id.length<=0){
                    oselect.addItem('شهر دیگری ثبت نشده است',0);
                     oselect.style.visibility='hidden';
                    return;
                }   
                
                //add first item
                oselect.addItem('انتخاب کنید',parentcityid,true);
                
                //add other city                         
                for(var i=0;i<cities.id.length;i++){
                    oselect.addItem(cities.name[i],cities.id[i]);
                    //if(i==0){toption.selected='selected';}
                }                
                changeToDdls(oselect);
            
        };                

    var pfOnChange=function(){
        
        $m('selCity').name=''; $m('selCity').id='';
        this.name=this.id='selCity';
    
        cityId=this.value;
        this.phDiv.innerHTML='';
        _loadCityFromServer(this.value,this.phDiv,src);
        
         try{getObjsBrief(1);}catch(e){}
    };
    oselect.onchange=pfOnChange;
    
}