var dummyText = 'Любой';
var country = getCookie('countryReg');
var region = getCookie('regionReg');
var city = getCookie('cityReg');

var geoRegion = 0;
    
/**************************************
 *
 *  reg
 *
 *************************************/

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 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');	 
    }
    
}

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');
    }
}


/*********************************
 *
 *      search 
 *
 *
 ********************************/

function citySearchHandler(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('cityS');
		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 regionSearchHandler(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('regionS');
		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('cityS').disabled = false;
}

function loadRegionsSearch(id) {
    
    var c = document.getElementById('cityS');
    var r = document.getElementById('regionS');
    
    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, regionSearchHandler);   
    } else {
         while(r.firstChild) r.removeChild(r.firstChild);   
		 r.options[r.options.length] = new Option(dummyText, '0');	 
    }
    
}

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

/***************************************
 *
 *   geo 
 *
 **************************************/

var geoCity = new Array();
var currentPage = 1;

function showGeoPage(page){
    
    $('#cityG').empty();
    currentPage = parseInt(page);
    
    pageSize = 21;
    totalPage = Math.ceil(geoCity.length /  pageSize);        
    
    if (page <= totalPage) {

        // back
        if (page > 1 && totalPage != 1){
            $('#cityG').append('<li><span><a href="javascript:void(0);" id="backCity" class="feat2">&lt;&lt;&lt;</a></span></li>');
            $('#backCity').livequery('click', function() {
                showGeoPage(currentPage-1);
            });
        }               
        
        // values
        for (var i = (page-1)*pageSize; i < page*pageSize; i++){
            if (geoCity[i]){
                $('#cityG').append('<li><span><a href="javascript:void(0);" class="feat">'+geoCity[i]['name']+'</a></span></li>');            
            }
        }
                
        // next
        if (page < totalPage){
            $('#cityG').append('<li><span><a href="javascript:void(0);" id="nextCity" class="feat2">&gt;&gt;&gt;</a></span></li>');
            $('#nextCity').livequery('click', function() {
                showGeoPage(currentPage+1);                
            });
        }
     
        $('#cityG').show();   
        $('#geoLoad').hide();
    }
}

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

        var n;
        n = p.firstChild;
        
        geoCity = new Array();
		
		if (n.tagName == 'item'){

		    var i=0;
    		while (n != null)
    		{
    		    geoCity[i] = new Array();
    		    
    		    geoCity[i]['id'] =  n.getAttribute('id');
    		    geoCity[i]['name'] =  n.getAttribute('name');

    			n = n.nextSibling;
    			i++;
    		}
    		
    		showGeoPage(currentPage);
    	}
	}
}

function regionGeoHandler(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('regionG');
		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;
            }
            
            $('#regionG').val(geoRegion);
    	}
	}
	
	c.disabled = false;
}

function loadRegionsGeo(id) {
    $('#cityG').empty();
    
    if (id >0){        
        $('#regionG').attr('disabled', 'disabled');
        ajaxLoad('/app.php?q=get_regions&id='+id, regionGeoHandler);   
        
    } else {
         $('#regionG').empty();
		 $('#regionG').append('<option value="0">Регион</option>')
    }
    
}

function loadCityGeo(id) {
    if (id >0){
        $('#geoLoad').show();
        $('#cityG').hide();   
        ajaxLoad('/app.php?q=get_cities&id='+id, cityGeoHandler);   
    }else{         
		 $('#cityG').empty();
    }
}


