// -- -------------------------------------------------------------------------
// -- 
// -- 市区町村取得 Ajax
// -- 
// -- @author http://www.clouds-inc.jp/
// -- 
// -- -------------------------------------------------------------------------
/**
 * 市区町村 select の id.
 */
var areaCityId;
/* #113 begin*/
var _prefId;
var _prefName;
/* #113 end*/
/**
 * 市区町村を取得する。
 * 
 * @param prefId 都道府県ID
 * @param _areaCityId 市区町村 select の id
 */
function showCity(prefId, _areaCityId) {
    areaCityId = _areaCityId;
    new Ajax.Request("/index.php", {
        "method" : "get",
        "parameters" : "act=public_getareacity&pref[]=" + prefId,
        "onComplete" : _setCities
    });
}

function showCitySync(prefId, _areaCityId) {
    areaCityId = _areaCityId;
	var myAjax = new Ajax.Request("/index.php", {
		method: "get",
		asynchronous:false,
        parameters : "act=public_getareacity&pref[]=" + prefId
	},false);
	_setCities(myAjax.transport);
	
}

/**
 * 市区町村をセットする。
 * 
 * @param request
 */
function _setCities(request) {
    var cities = eval(request.responseText);
    if(cities == null) return;
    _clearCities();
    $(areaCityId).options[$(areaCityId).options.length] = new Option("こだわらない", "no", false, true);
    for(var i = 0, l = cities.length; i < l; i++) {
        var city = cities[i];
        $(areaCityId).options[$(areaCityId).options.length] = new Option(city.city_name, city.city_id);
    }
}
/* #113 begin*/

function showCityNew(_prefId, _areaCityId, _prefName) {
    areaCityId = _areaCityId;
    prefId = _prefId;
    prefName = _prefName;
    new Ajax.Request("/index.php", {
        "method" : "get",
        "parameters" : "act=public_getareacity&new=1&pref[]=" + prefId,
        "onComplete" : _setCitiesNew
    });
}

function _setCitiesNew(request) {
	_ajax_prosessing = 0;
    var strCheckBoxCity = '';
    var cities = eval(request.responseText);
    if(cities == null) return;  
    strCheckBoxCity +='<input type="hidden" value="'+prefId+'" name="prefId" id="prefId">';
    strCheckBoxCity +='<input type="hidden" id="pref_name_popup" name="pref_name" value="'+prefName+'"/>';
    $('lable_check_all_city').innerHTML = prefSortName[$("pref").value]+'を全て選択';    
    var checkAllSelected = 0;
    
    if($('check_all_acc') != undefined) {
        checkAllSelected = 1;
    }
    
    var count = 0;
    for(var i = 0, l = cities.length; i < l; i++) {
        var city = cities[i];
        strCheckBoxCity += '<li><label><input type="checkbox" class="city" id="city_id_'+i+'"  onclick="CityBoxController.setCheckBox(\'city_id_'+i+'\')" ';
        $$('.form_city').each(function(ele){
           if( $(ele).value == city.city_id || checkAllSelected == 1) {       
               strCheckBoxCity += ' checked="checked" ';               
               count++;
           }
    	   });
    	   
        strCheckBoxCity += ' value="'+city.city_id+'" name="" title="'+city.city_name+'">'+city.city_name+'　<span>'+city.project_num+'</span></label></li>';      
    }
    
    if((cities.length && cities.length == count) || checkAllSelected == 1) {
        $('check_all_city').checked = "checked";
        $('lable_check_all_city').innerHTML = prefSortName[$("pref").value]+'の選択を全て外す';
    } else {
        $('check_all_city').checked = "";
    }
    
    $('all_city').value = ($('check_all_city').checked) ? 1 : 0;
    
    //fill strCheckBoxCity created above        
    $(areaCityId).innerHTML = strCheckBoxCity;
    Conditionpopup.showPopup(1);
}

/* #113 end*/
/**
 * 市区町村 select をクリアする。
 * 
 */
function _clearCities() {
    while($(areaCityId).options.length > 0) {
        $(areaCityId).options[$(areaCityId).options.length - 1] = null;
    }
}
