// geo dictionares

function cityHandler(handler, params) {
    if (handler.status == 200 && handler.responseXML && (p = handler.responseXML.documentElement) && p.tagName == 'result') {

        var n;
        
        ids = new Array();
        names = new Array();
        
        var c = document.getElementById('city');
		while(c.firstChild) c.removeChild(c.firstChild);   

		n = p.firstChild;
		
		if (n.tagName == 'item'){

		    var i=0;
    		while (n != null)
    		{
    			ids[i] = n.getAttribute('id');
    			names[i] = n.getAttribute('name');

    			n = n.nextSibling;
    			i++;
    		}
    
    		c.options[c.options.length] = new Option(dummyText, '0');
    		
    		for (var j = 0; j < names.length; j++)
    		{
    		    var newOption =  new Option(names[j], ids[j]);    		    
    		    c.options[c.options.length] = newOption;
                if (ids[j]==city){
                    newOption.selected = true;
                }
            }
            
            // unset old city
            city = 0;
    	}        
	}
	
	c.disabled = false;
}

function loadCity(id) {
    
    var c = document.getElementById('city');    
    if (id >0){
        c.disabled = true;
        ajaxLoad('/app.php?q=get_cities&id='+id, cityHandler);   
    }else{         
		 while(c.firstChild) c.removeChild(c.firstChild);   
		 c.options[c.options.length] = new Option(dummyText, '0');
    }
}

function regionHandler(handler, params) {
    if (handler.status == 200 && handler.responseXML && (p = handler.responseXML.documentElement) && p.tagName == 'result') {

        var n;
        
        ids = new Array();
        names = new Array();
        
        var c = document.getElementById('region');
		while(c.firstChild) c.removeChild(c.firstChild);   

		n = p.firstChild;
		
		if (n.tagName == 'item'){

		    var i=0;
    		while (n != null)
    		{
    			ids[i] = n.getAttribute('id');
    			names[i] = n.getAttribute('name');

    			n = n.nextSibling;
    			i++;
    		}
    		
    		c.options[c.options.length] = new Option(dummyText, '0');
    
    		for (var j = 0; j < names.length; j++)
    		{
    		    var newOption =  new Option(names[j], ids[j]);    		    
    		    c.options[c.options.length] = newOption;
                if (ids[j]==region){
                    newOption.selected = true;
                }
            }
            
            // unset old region 
            region = 0;
    	}
	}
	
	c.disabled = false;
	document.getElementById('city').disabled = false;
}

function loadRegions(id) {
    
    var c = document.getElementById('city');
    var r = document.getElementById('region');
    
    while(c.firstChild) c.removeChild(c.firstChild);  
    c.options[c.options.length] = new Option(dummyText, '0');
    
    if (id >0){
        c.disabled = true; 
        r.disabled = true; 
        ajaxLoad('/app.php?q=get_regions&id='+id, regionHandler);   
    } else {
         while(r.firstChild) r.removeChild(r.firstChild);   
		 r.options[r.options.length] = new Option(dummyText, '0');	 
    }
    
}
