function getNewRailway(prefId, _railwaysId) {
    railwaysId = _railwaysId;
  //#130 begin change requirement date 2012-01-17 10:34:51 
    var new_ads = getParamURLForNewAdv();
  //#130 end
    new Ajax.Request("/index.php", {
        "method" : "get",
        "parameters" : "act=public_getrailway&new=1&pref[]=" + prefId + new_ads,
        "onComplete" : _setNewRailways
    });
}

//#130 begin change requirement date 2012-01-17 10:34:51 
function getParamURLForNewAdv() {
    var oNewAds = document.getElementsByName('new_ads');
    var new_ads = '';
    if(oNewAds[0] && oNewAds[0].value) {
        new_ads = '&new_ads=' + oNewAds[0].value;
    }
    return new_ads;
}
//#130 end

function _setNewRailways(request) {
    var railways = eval(request.responseText);
    
    if(railways == null) return;
    _clearRailways();
    
    addOptions($(railwaysId), "沿線を選択してください", "no", false, "");
    
    var haveProject = false;
    _LIST_RAILROAD = new Array();
    for(var i = 0, l = railways.length; i < l; i++) {
        var railway = railways[i];
        var objOption = null;
        var selected = (parseInt(railway.rai_id, 10) == last_rail_id);
        
        _LIST_RAILROAD.push(railway.rai_id);
        addOptions($(railwaysId), railway.rai_name+"("+railway.numProject+")", railway.rai_id, selected, railway.rai_name);
        
        if(selected) {
               haveProject = true;
        }
    }
    if(!haveProject) {
        $("stat_selected").value = 0;
    }
}
function addOptions (obj, text, value, selected, text_name) {
    var objOption = new Option(text, value);
    objOption.text_name  = text_name;
    
    var index = obj.options.length;
    obj.options[obj.options.length] = objOption;
    obj.options[index].selected = selected;
}

function AddForcus() {
    if($("btn_newfocus")){
        $("btn_newfocus").style.display = 'none';
    }
    
    document.getElementById("ext_newfocus").value = 1;
    var aryObj = $$(".search_newForcus");
    var length = aryObj.length;

    for(var i = 0; i < length; i++) {
        var obj = aryObj[i];
        obj.style.display = '';
    }
    onchangeFocus();
}

function onchangeFocus() {

    var isCheck = ($('form[1]').checked && !$('form[2]').checked);
    if($("btn_newfocus") && $("btn_newfocus").style.display) {
        if(isCheck) {
            $('newForcus_last').style.display = 'none';
        }
        else {
            $('newForcus_last').style.display = '';
        }
    }
    if(isCheck) {
        for(var i = 30; i <= 33; i++){
            if($('newfoc_label_' + i)) {
                $('newfoc_label_' + i).checked = false;
            }
        }
    }
}

function ChangePref(_prefId) {
	if(_prefId) {
		$("pref").value = _prefId;
	}
    if($("search_mode").value == 'station') {
        getNewRailway($("pref").value, "railways"); 
    }
    StationBoxController.resetAll();
    CityBoxController.resetAll();
    
    //Hidden messages
    hiddenAreaMsg ();
    $("is_error_city").value = 0;
    $("is_error_rail").value = 0;
};
function _tabCityOnOrOff(isOn) {
    
    if(isOn) {
        $("tub01img").src = "/img/tab_area_on.gif";
        $("tub01img").style.cursor = '';
        $("city_tab").style.display = "";
        changeCssFloat("common_label", "left");
        $("common_label").innerHTML = "【現在選択されている地域】";

        $("search_mode").value = 'city';
    }
    else {
        $("tub01img").src = "/img/tab_area_off.gif";
        $("tub01img").style.cursor = 'pointer';
        $("city_tab").style.display = "none";
    }
}

function changeCssFloat(objId, strValue) {
    if(typeof $(objId).style.cssFloat == 'string'){
        $(objId).style.cssFloat = strValue;
    }
    else if(typeof $(objId).style.styleFloat == 'string') {
        $(objId).style.styleFloat = strValue;
    }
}
function _tabStationOnOrOff(isOn) {
    if(isOn) {
        $("tub02img").src = "/img/tab_line_on.gif";
        $("tub02img").style.cursor = '';
        $("rail_tab").style.display = "";
        $("rail_tab_btn").style.display = "";
        changeCssFloat("common_label", '');
        $("common_label").innerHTML = "【現在選択されている路線】";

        if($("search_mode").value != 'station') {
            $("search_mode").value = 'station';
            getNewRailway($("pref").value, "railways");
        }
    }
    else {
        $("tub02img").src = "/img/tab_line_off.gif";
        $("tub02img").style.cursor = 'pointer';
        $("rail_tab").style.display = "none";
        $("rail_tab_btn").style.display = "none";
    }
}

function viewDefaultSelectedData() {
    var isCity = ($("search_mode").value == 'city');
    var isStation = ($("search_mode").value == 'station');
    
    if(isCity) {//City
        _cityViewDefault();
    }
    else if (isStation) { //Station
        _stationViewDefault();
    }
}

function _cityViewDefault() {
    
    var citySelValue = $("city_selected").value;
    if(citySelValue > 0) {
        $('common_label').style.display = '';
        $('id_area_city_name').style.display = '';
        $('pref').style.display = 'none';
        $('id_area_city_name').style.display = '';
        $('image_more_choice').style.display = '';
        $('pref_name_selected').style.display = '';
        $('resetCitySelected').style.display = '';
        $('resetchange').style.display = 'none';
        $('id_are_rail_station').style.display = 'none';
    }
    else {
        $('common_label').style.display = 'none';
        $('id_area_city_name').style.display = 'none';
        $('id_are_rail_station').style.display = 'none';
        $('pref').style.display = '';
        $('id_area_city_name').style.display = 'none';
        $('image_more_choice').style.display = 'none';
        $('pref_name_selected').style.display = 'none';
        $('resetCitySelected').style.display = 'none';
        $('resetchange').style.display = 'none';
    }
}

function _stationViewDefault() {
    var statSelValue = $("stat_selected").value;
    if(statSelValue > 0) {
        $('common_label').style.display = '';
        $('pref').style.display = 'none';
        $('image_more_choice').style.display = '';
        $('id_are_rail_station').style.display = '';
        $('id_area_city_name').style.display = 'none';
        $('resetCitySelected').style.display = 'none';
        $('resetchange').style.display = '';
        $('pref_name_selected').style.display = 'none';
    }
    else {
        $('common_label').style.display = 'none';
        $('pref').style.display = '';
        $('image_more_choice').style.display = 'none';
        $('id_are_rail_station').style.display = 'none';
        $('id_area_city_name').style.display = 'none';
        $('resetCitySelected').style.display = 'none';
        $('resetchange').style.display = 'none';
        $('pref_name_selected').style.display = 'none';
    }
}

function ChangeTab(no){
    
  //hiddenAreaMsg();
  if(no == 1) {
      _tabCityOnOrOff(true);
      _tabStationOnOrOff(false);
      
      hiddenAreaMsg ();
      if($("is_error_city").value == 1){
          hiddenAreaMsg (1, true, true);
      }
  } else {
      _tabCityOnOrOff(false);
      _tabStationOnOrOff(true);
      
      hiddenAreaMsg ();
      if($("is_error_rail").value == 1){
          hiddenAreaMsg (2, true, true);
      }
  }
  viewDefaultSelectedData();
}

function SelectSal(no){
  if(no == 1){
       if($('form[1]').checked) {
            $("fullTimeYear").style.display = "inline";
            $("fullTimeMonth").style.display = "inline";
            $("errMessage").style.display = "none"; 
       } else {
             $("fullTimeYear").style.display = "none";
             $("fullTimeMonth").style.display = "none";
       }
       if(!$('form[1]').checked) {
           for(var i = 1; i <= 2; i++) {
               reSelected($("s_salary_" + i), 0)
           }
       }
  }
  if(no == 2){
      if($('form[2]').checked) {
           $("partTimeDay").style.display = "inline";    
           $("partTimeHrs").style.display = "inline";    
           $("partTimeUnit").style.display = "inline";    
           $("errMessage").style.display = "none";
      } else {
           $("partTimeDay").style.display = "none";    
           $("partTimeHrs").style.display = "none";    
           $("partTimeUnit").style.display = "none";    
      }
      if(!$('form[2]').checked) {
          for(var i = 3; i <= 5; i++) {
              reSelected($("s_salary_" + i), 0)
          }
      }
  }
  if(!$('form[1]').checked && !$('form[2]').checked) {
    $("errMessage").style.display = "inline";   
  }
  
  onchangeFocus();
}

function reSelected(objSelect, indexSelected) {
    var cnt = objSelect.options.length;
    for(var i = 0; i < cnt; i++) {
        if(i == indexSelected){
            objSelect.options[i].selected = true;
        }
        else {
            objSelect.options[i].selected = false;
        }
    }
}

var _ajax_prosessing = 0;
function show(id) {
	if(_ajax_prosessing) return;
	_ajax_prosessing = 1;
     if(id==2) {
        if($('railways').value == 'no') {
            hiddenAreaMsg (1, false);
            hiddenAreaMsg (2, true, true);
            $("is_error_rail").value = 1;
            _ajax_prosessing = 0;
            return;
        } 
        else{
            $("is_error_rail").value = 0;
            hiddenAreaMsg (2, false);
        }
         
        var index = $("railways").selectedIndex;
        var nameDisplay = $("railways").options[index].text;
        nameDisplay = nameDisplay.replace(/\(\s*[0-9,]+\s*\)/, '');
        
        $('message').innerHTML = "";
        getStationNew($("railways").value, "station_popup", nameDisplay, $("pref").value);
    } else {
        
        if($('pref').value < 1) {
            hiddenAreaMsg (1, true, true);
            hiddenAreaMsg (2, false);
            $("is_error_city").value = 1;
            _ajax_prosessing = 0;
            return;
        }
        else{
            $("is_error_city").value = 0;
            hiddenAreaMsg (1, false);
        }
        
        var indexPref = $("pref").selectedIndex;
        $('messageErrCity').innerHTML = "";
        showCityNew($("pref").value, "cityCheckbox", $("pref").options[indexPref].innerHTML);
    }
    
    //hiddenAreaMsg();
    
    //Conditionpopup.showPopup(id);
    if(isIE(6)) {
        var list_id = (id==2) ? 'list_item_station' : 'list_item_city';
        if($(list_id)) $(list_id).style.height = "540px";
    }
};


function hiddenAreaMsg (tab_id, isOn, isMess) {
    if(tab_id == 1 || !tab_id){
        if(isOn){
            $('msg_eror_city').style.display = '';
        }
        else{
            $('msg_eror_city').style.display = 'none';
        }
        if(isMess){
            $('msg_eror_city').innerHTML = "都道府県を選択してください";
        }
        else {
            $('msg_eror_city').innerHTML = "";
        }
    }
    
    if(tab_id == 2 || !tab_id) {
        if(isOn){
            $('msg_eror_rail').style.display = '';
        }
        else{
            $('msg_eror_rail').style.display = 'none';
        }
        if(isMess){
            $('msg_eror_rail').innerHTML = "沿線を選択してください";
        }
        else {
            $('msg_eror_rail').innerHTML = "";
        }
    }
}

function initForm() {
    //Form
    for(var i = 1; i <=2; i++) {
        if($("form[" + i + "]").checked) {
            SelectSal($("form[" + i + "]").value);
        }
    }
}

function initRail() {
    var objSelect = $("railways");
    var cnt = objSelect.options.length;
    _LIST_RAILROAD = new Array();
    for(var i = 0; i < cnt; i++) {
        _LIST_RAILROAD.push(objSelect.options[i].value);
    }
}
function init() {
    
    //Init list railroad id
    initRail();
    
  //Set selected default
    $("city_selected").value = intCity;
    $("stat_selected").value = intStat;

    //Select default railway
    if(last_rail_id) $('railways').value = last_rail_id;

    //Form
    initForm();

    //Selected default tab
    if($("search_mode").value != 'station') ChangeTab(1);
    
}

