var item_type;

//都道府県変更に応じて市区町村を変更
function add_subload(elm){
var form = elm.form;
elm.blur();

  $(".addsub_span",form).html('<img height="16px" width="50px" src="./img/ajax-minibar.gif" />');
  
   jQuery.ajax({
      url : 'api.php' ,
      type : 'POST',
      dataType : "text",
	  data : "post=load_addsub&js=true&id="+form.adds.options[ form.adds.selectedIndex ].value+"&type="+item_type,
      success : function(res){ $(".addsub_span",form).html(res);},
      error   : function(xml, status, e){$(".addsub_span",form).attr('id','error_msg'); $(".addsub_span",form).html("通信エラー"); }
    });
    if( form.line1 || form.line2 || form.line3 || form.line10 ){
        line_load(elm);
    }
}

function line_load(elm){
  var form = elm.form;
  elm.blur();
  
  if(form.type){
    if(form.type.length){
      for(i=0;i<form.type.length;i++){
        if(form.type[i].checked){ item_type = (form.type[i].value); break; }
      }
    }else{
      item_type = form.type.value;
    }
  }
  
  var type_parm = "";
  if(item_type)
      type_parm = "&type=" + item_type;

  jQuery.ajax({
    url : 'api.php' ,
    type : 'POST',
    dataType : "json",
    data : "post=load_line&js=true&id="+form.adds.options[ form.adds.selectedIndex ].value+type_parm,
    success : function(res){
      if(form.line1){
          $(form.line1).removeOption(/./);
          $(form.line1).addOption(res, false);
          if(form.station1){
            $(form.station1).removeOption(/./);
            $(form.station1).addOption("blank","");
          }
      }
      if(form.line2){
          $(form.line2).removeOption(/./);
          $(form.line2).addOption(res, false);
          if(form.station2){
            $(form.station2).removeOption(/./);
            $(form.station2).addOption("blank","");
          }
      }
      if(form.line3){
          $(form.line3).removeOption(/./);
          $(form.line3).addOption(res, false);
          if(form.station3){
            $(form.station3).removeOption(/./);
            $(form.station3).addOption("blank","");
          }
      }
      if(form.line10){
          $(form.line10).removeOption(/./);
          $(form.line10).addOption(res, false);
          if(form.station10){
            $(form.station10).removeOption(/./);
            $(form.station10).addOption("blank","");
          }
      }
    },
    error   : function(xml, status, e){$(".addsub_span",form).attr('id','error_msg'); $(".addsub_span",form).html("通信エラー"); }
  });
  
}

function station_load(elm,s_name){
var form = elm.form;
elm.blur();

  if(form.type){
    for(i=0;i<form.type.length;i++){
      if(form.type[i].checked){ item_type = (form.type[i].value); break; }
    }
  }
  
  var type_parm = "";
  if(item_type)
      type_parm = "&type=" + item_type;
      
  jQuery.ajax({
    url : 'api.php' ,
    type : 'POST',
    dataType : "json",
    data : "post=load_station&js=true&id="+elm.options[ elm.selectedIndex ].value+"&adds_id="+form.adds.options[ form.adds.selectedIndex ].value+type_parm,
    success : function(res){
      $(s_name).removeOption(/./);
      $(s_name).addOption(res, false);
    },
    error   : function(xml, status, e){
        $(".addsub_span",form).attr('id','error_msg');
        $(".addsub_span",form).html("通信エラー");
    }
  });
}
