var StationBoxController = function() {};


StationBoxController.checkAll = function() {
    var railName = StationBoxController.getNameRail();
    if($('check_all').checked) {
       $$('.rail').each(function(ele){
            $(ele).checked = "checked";
       });
       $('lable_check_all').innerHTML = railName+'の選択を全て外す';
    } else {
         $$('.rail').each(function(ele){
            $(ele).checked = "";
       });
       $('lable_check_all').innerHTML = railName+'を全て選択';
    }
}


StationBoxController.choice = function(){

	var flag = 0;
	var strStation = '';
    $$('.rail').each(function(ele){
       if( $(ele).checked )
       {
            flag = 1;
       }
    });
    if(flag==0)
    {
        $('message').innerHTML = '駅を選択してください';
        //document.location="#errs";
        return;
    }
    $('message').innerHTML = "";
    StationBoxController.closePopup();
    StationBoxController.displayAreaRailStation();
    var raiId = $('raiId').value;
    var railName = StationBoxController.getNameRail();
	var strHtml = '';
	var strName = "全て";
	var cnt = 0;
    if(!$('check_all').checked) {
        strName = '';
        $$('.rail').each(function(ele){
           if( $(ele).checked )
           {
               strStation += ','+$(ele).value;
               strName += ((strName) ? '、':'') + $(ele).title;
               cnt++;
           }
        });
	}
    else {
        cnt = 2;
    }
    
    $("stat_selected").value = cnt;
    
	var tagId = 'form_rail_'+raiId;
	var tagNameId = 'form_rail_name_'+raiId;
	
	if($(tagId) == undefined) {
	   strHtml = '<input type="hidden" id="form_rail_'+raiId+'" name="rail[]" value="'+raiId+strStation+'"/>';
	   strHtml += '<input type="hidden" class="form_rail_name_class" id="form_rail_name_'+raiId+'" name="rail_name[]" value="<strong>'+railName+'</strong>　'+strName+'"/>';
	} else {
	    $(tagId).value = raiId+strStation;
	    $(tagNameId).value = '<strong>'+railName+'</strong>　'+strName;
	}
    
    
    $('message').innerHTML = '';
    
    $('areaStation').innerHTML += strHtml;
    StationBoxController.setNameRail();
}


StationBoxController.setNameRail = function(){
    var strName = ''; // insert BR 
    //$('id_are_rail_station').innerHTML = "【現在選択されている路線】";
    
    var strHTML = '';
    if(_LIST_RAILROAD){
        var cnt = _LIST_RAILROAD.length;
        for(var idx = 0; idx < cnt; idx++ ) {
            var raiId = _LIST_RAILROAD[idx];
            if($('form_rail_name_' + raiId)) {
                var name = $('form_rail_name_' + raiId).value;
                strName += ((strName) ? '<br/>': '') + name;
                
                strHTML += '<input type="hidden" id="form_rail_'+raiId+'" name="rail[]" value="' + $('form_rail_' + raiId).value + '"/>';
                strHTML += '<input type="hidden" class="form_rail_name_class" id="form_rail_name_'+raiId+'" name="rail_name[]" value="' + name + '"/>';
            }
        }
    }
    $('areaStation').innerHTML = strHTML;
    
    /*
    $$('.form_rail_name_class').each(function(ele){
        strName += ((strName) ? '<br/>': '')+$(ele).value;
    });
    */
    $('common_label').style.display = '';
    $('image_more_choice').style.display = '';
    $('id_are_rail_station').innerHTML = strName;
}


StationBoxController.setCheckBox = function(id){
    
	if($(id).checked) {
	    
       StationBoxController.reSetCheckBox();
    } else {
        $('check_all').checked = "";
        var railName = StationBoxController.getNameRail();
        $('lable_check_all').innerHTML = railName+'を全て選択';
    }
}


StationBoxController.reSetCheckBox = function(){
	 var flag = 0;
       $$('.rail').each(function(ele){
           if(!$(ele).checked ) {
               
            flag = 1;
           }
       });
       if(flag==0) {
           $('check_all').checked = 'checked';
           var railName = StationBoxController.getNameRail();
           $('lable_check_all').innerHTML = railName+'の選択を全て外す';
           
       }
}


StationBoxController.closePopup = function(){
	 $('TB_overlay').style.display = 'none';
     $('TB_window').style.display = 'none';
     Conditionpopup.closePopup();
}

StationBoxController.getNameRail = function(){
    var railName = '';
    railName = $('rai_name_popup').value;
    return railName;
}


StationBoxController.displayAreaRailStation = function(){
     $('id_are_rail_station').style.display = 'inline';
     //$('default_image').style.display = 'inline';
     $('resetchange').style.display = 'inline';
     $('pref').style.display = 'none';
     $('rail_tab').removeClassName('default');
     $('rail_tab_btn').removeClassName('defaultBtn');
}

StationBoxController.resetAll = function() {
     $('id_are_rail_station').style.display = 'none';
     $('common_label').style.display = 'none';
     $('image_more_choice').style.display = 'none';
     $('pref').style.display = 'inline';
     $('rail_tab').addClassName('default');
     $('rail_tab_btn').addClassName('defaultBtn');
     $('resetchange').style.display = 'none';
     $('id_are_rail_station').innerHTML = "";
     $('areaStation').innerHTML = '';
     $("railways").selectedIndex = 0;
     last_rail_id = 0;
     $("stat_selected").value = 0;
}



function isIE (x) {
	// brower detection
	var ua = navigator.userAgent.toLowerCase();
	var isOpera = (ua.indexOf('opera') > -1);
	/*isSafari = (ua.indexOf('safari') > -1),
	isGecko = (!isOpera && !isSafari && ua.indexOf('gecko') > -1),*/
	var isIE = null;
	if(x && x > 0){
	    isIE = (!isOpera && ua.indexOf('msie '+x) > -1);
	}
	else {
	    isIE = (!isOpera && ua.indexOf('msie') > -1);
	}
	return isIE;
}
