/* 
 * 検索共通JavaScript ※要jQuery
 */

//指定セレクトメニュー(jQueryオブジェクト)のoptinos(選択肢)を空にする
function clearSearchSelect(select) {
  select.empty();
  select.append(
    jQuery(document.createElement('option'))
      .attr('value','')
      .append('----------------')
  );
}

//指定セレクトメニュー(jQueryオブジェクト)にoptinos(選択肢)を追加する
function addOptionToSearchSelect(select,value,name) {
  select.append(
    jQuery(document.createElement('option'))
      .attr('value',value)
      .append(name)
  );
}

//指定クラスを持つセレクトメニューのoptinos(選択肢)を空にして、隠す
function resetCategorySearchSelect(classname) {
  $('.'+classname).hide();
  clearSearchSelect($('.'+classname+' select:input'));
}


/* 
 * クイックサーチ用JavaScript ※要jQuery
 */

//JSONデータをもとに、フォームをinitする
function initQuickSearchForm(search_json){
  initQuickSearchSelectForm(search_json);
  
  //検索ボタン
  $('#ds2_quicksearch_commit').click( function(){
    submitQuickSearchForm();
  });
  
  //条件のクリア
  $('#ds2_quicksearch_clear').click( function(){
    clearQuickSearchForm();
  });
}
function initQuickSearchSelectForm(search_json){
  
  //下層のカテゴリセレクトメニューを隠す。
  $('#ds2_quicksearch_shop_select').show();
  clearSearchSelect($('#ds2_quicksearch_shop_id'));
  resetCategorySearchSelect('ds2_quicksearch_shop_category');
  
  //ショップデータをセレクトメニューに投入
  for (shop_id in search_json.category_tree) {
    addOptionToSearchSelect($('#ds2_quicksearch_shop_id'),shop_id,search_json.shop_names[shop_id]);
  }
  
  //ショップ選択したらカテゴリ第一階層更新
  $('#ds2_quicksearch_shop_id').change(function(){
    changeShopQuickSearch(search_json);
  });
  
  //カテゴリ第一階層選択したらカテゴリ第二階層更新
  $('#ds2_quicksearch_shop_category1_id').change(function(){
    changeCategory1QuickSearch();
  });
  
  //カテゴリ第二階層選択したらカテゴリ第三階層更新
  $('#ds2_quicksearch_shop_category2_id').change(function(){
    changeCategory2QuickSearch();
  });
}
function changeShopQuickSearch(){
  var shop_id = $('#ds2_quicksearch_shop_id').val();
  
  resetCategorySearchSelect('ds2_quicksearch_shop_category');
  var has_value = false;
  if(search_json.category_tree[shop_id]) {
    for (category1_code in search_json.category_tree[shop_id]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_quicksearch_shop_category1_id'),category1_code,search_json.category_names[category1_code]);
    }
  }
  if (has_value) {
    $('#ds2_quicksearch_shop_category1_select').show();
  }
}
function changeCategory1QuickSearch(){
  var category1_code = $('#ds2_quicksearch_shop_category1_id').val();
  var shop_id = $('#ds2_quicksearch_shop_id').val();
  
  clearSearchSelect($('#ds2_quicksearch_shop_category2_id'));
  clearSearchSelect($('#ds2_quicksearch_shop_category3_id'));
  $('#ds2_quicksearch_shop_category2_select').hide();
  $('#ds2_quicksearch_shop_category3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code]) {
    for (category2_code in search_json.category_tree[shop_id][category1_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_quicksearch_shop_category2_id'),category2_code,search_json.category_names[category2_code]);
    }
  }
  if (has_value) {
    $('#ds2_quicksearch_shop_category2_select').show();
  }
}
function changeCategory2QuickSearch(){
  var category2_code = $('#ds2_quicksearch_shop_category2_id').val();
  var category1_code = $('#ds2_quicksearch_shop_category1_id').val();
  var shop_id = $('#ds2_quicksearch_shop_id').val();
  
  clearSearchSelect($('#ds2_quicksearch_shop_category3_id'));
  $('#ds2_quicksearch_shop_category3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code][category2_code]) {
    for (category3_code in search_json.category_tree[shop_id][category1_code][category2_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_quicksearch_shop_category3_id'),category3_code,search_json.category_names[category3_code]);
    }
  }
  if (has_value) {
    $('#ds2_quicksearch_shop_category3_select').show();
  }
}

//クイックサーチフォームを実行する。
function submitQuickSearchForm() {
  if ($('.ds2_quicksearch_hidden')) {
    $('.ds2_quicksearch_hidden').remove();
  }
  
  //カテゴリコードの指定
  var shop_category3_code = $('#ds2_quicksearch_shop_category3_id').val();
  var shop_category2_code = $('#ds2_quicksearch_shop_category2_id').val();
  var shop_category1_code = $('#ds2_quicksearch_shop_category1_id').val();
  if (shop_category3_code) {
    var shop_category_code1 = shop_category3_code;
  }else if (shop_category2_code) {
    var shop_category_code1 = shop_category2_code;
  }else if (shop_category1_code) {
    var shop_category_code1 = shop_category1_code;
  }
  if (shop_category_code1) {
    $('#ds2_quicksearch').prepend(
      //カテゴリは最下層コードのみ指定する
      jQuery(document.createElement('input'))
        .attr('name','shop_category_code1')
        .attr('type','hidden')
        .val(shop_category_code1)
        .addClass('quicksearch_hidden')
    );
  }
  $('#ds2_quicksearch').submit();
  return false;
}

//クイックサーチフォームの検索条件をリセットする。
function clearQuickSearchForm() {
  //フォーム値をリセット
  $('.ds2_quicksearch_input:input').each( function(){
    $(this).val('');
  });
  
  //カテゴリセレクトメニューの初期化
  $('#ds2_quicksearch_shop_select').show();
  resetCategorySearchSelect('ds2_quicksearch_shop_category');
}

/* 
 * フルサーチ用JavaScript ※要jQuery
 */

//JSONデータをもとに、フォームをinitする
function initFullSearchForm(search_json){
  initFullSearchSelectForm(search_json);
  
  //検索ボタン
  $('#ds2_fullsearch_commit').click( function(){
    submitFullSearchForm();
  });
  
  //条件のクリア
  $('#ds2_fullsearch_clear').click( function(){
    clearFullSearchForm();
  });
}
function initFullSearchSelectForm(search_json){
  
  //下層のカテゴリセレクトメニューを隠す。
  $('#ds2_fullsearch_shop_select').show();
  clearSearchSelect($('#ds2_fullsearch_shop_id'));
  resetCategorySearchSelect('ds2_fullsearch_shop_category');
  
  //ショップデータをセレクトメニューに投入
  for (shop_id in search_json.category_tree) {
    addOptionToSearchSelect($('#ds2_fullsearch_shop_id'),shop_id,search_json.shop_names[shop_id]);
  }
  
  //ショップ選択したらカテゴリ第一階層更新
  $('#ds2_fullsearch_shop_id').change(function(){
    changeShopFullSearch();
  });
  
  //カテゴリ第一階層選択したらカテゴリ第二階層更新
  $('#ds2_fullsearch_shop_category1_id').change(function(){
    changeCategory1FullSearch();
  });
  
  $('#ds2_fullsearch_shop_category_other1_id').change(function(){
    changeCategoryOther1FullSearch();
  });
  
  //カテゴリ第二階層選択したらカテゴリ第三階層更新
  $('#ds2_fullsearch_shop_category2_id').change(function(){
    changeCategory2FullSearch();
  });
  $('#ds2_fullsearch_shop_category_other2_id').change(function(){
    changeCategoryOther2FullSearch();
  });
}
function changeShopFullSearch(){
  var shop_id = $('#ds2_fullsearch_shop_id').val();
  
  resetCategorySearchSelect('ds2_fullsearch_shop_category');
  var has_value = false;
  if(search_json.category_tree[shop_id]) {
    for (category1_code in search_json.category_tree[shop_id]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category1_id'),category1_code,search_json.category_names[category1_code]);
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category_other1_id'),category1_code,search_json.category_names[category1_code]);
    }
  }
  if (has_value) {
    $('#ds2_fullsearch_shop_category1_select').show();
    $('#ds2_fullsearch_shop_category_other1_select').show();
  }
}
function changeCategory1FullSearch(){
  var category1_code = $('#ds2_fullsearch_shop_category1_id').val();
  var shop_id = $('#ds2_fullsearch_shop_id').val();
  
  clearSearchSelect($('#ds2_fullsearch_shop_category2_id'));
  clearSearchSelect($('#ds2_fullsearch_shop_category3_id'));
  $('#ds2_fullsearch_shop_category2_select').hide();
  $('#ds2_fullsearch_shop_category3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code]) {
    for (category2_code in search_json.category_tree[shop_id][category1_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category2_id'),category2_code,search_json.category_names[category2_code]);
    }
  }
  if (has_value) {
    $('#ds2_fullsearch_shop_category2_select').show();
  }
}
function changeCategory2FullSearch(){
  var category2_code = $('#ds2_fullsearch_shop_category2_id').val();
  var category1_code = $('#ds2_fullsearch_shop_category1_id').val();
  var shop_id = $('#ds2_fullsearch_shop_id').val();
  
  clearSearchSelect($('#ds2_fullsearch_shop_category3_id'));
  $('#ds2_fullsearch_shop_category3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code][category2_code]) {
    for (category3_code in search_json.category_tree[shop_id][category1_code][category2_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category3_id'),category3_code,search_json.category_names[category3_code]);
    }
  }
  if (has_value) {
    $('#ds2_fullsearch_shop_category3_select').show();
  }
}
function changeCategoryOther1FullSearch(){
  var category1_code = $('#ds2_fullsearch_shop_category_other1_id').val();
  var shop_id = $('#ds2_fullsearch_shop_id').val();
  
  clearSearchSelect($('#ds2_fullsearch_shop_category_other2_id'));
  clearSearchSelect($('#ds2_fullsearch_shop_category_other3_id'));
  $('#ds2_fullsearch_shop_category_other2_select').hide();
  $('#ds2_fullsearch_shop_category_other3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code]) {
    for (category2_code in search_json.category_tree[shop_id][category1_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category_other2_id'),category2_code,search_json.category_names[category2_code]);
    }
  }
  if (has_value) {
    $('#ds2_fullsearch_shop_category_other2_select').show();
  }
}
function changeCategoryOther2FullSearch(){
  var category2_code = $('#ds2_fullsearch_shop_category_other2_id').val();
  var category1_code = $('#ds2_fullsearch_shop_category_other1_id').val();
  var shop_id = $('#ds2_fullsearch_shop_id').val();
  
  clearSearchSelect($('#ds2_fullsearch_shop_category_other3_id'));
  $('#ds2_fullsearch_shop_category_other3_select').hide();
  var has_value = false;
  if (search_json.category_tree[shop_id][category1_code][category2_code]) {
    for (category3_code in search_json.category_tree[shop_id][category1_code][category2_code]) {
      has_value = true;
      addOptionToSearchSelect($('#ds2_fullsearch_shop_category_other3_id'),category3_code,search_json.category_names[category3_code]);
    }
  }
  if (has_value) {
    $('#ds2_fullsearch_shop_category_other3_select').show();
  }
}

//フルサーチフォームを実行する。
function submitFullSearchForm() {
  if ($('.ds2_fullsearch_hidden')) {
    $('.ds2_fullsearch_hidden').remove();
  }
  
  //カテゴリコードの指定 - メイン
  var shop_category3_code = $('#ds2_fullsearch_shop_category3_id').val();
  var shop_category2_code = $('#ds2_fullsearch_shop_category2_id').val();
  var shop_category1_code = $('#ds2_fullsearch_shop_category1_id').val();
  if (shop_category3_code) {
    var shop_category_code1 = shop_category3_code;
  }else if (shop_category2_code) {
    var shop_category_code1 = shop_category2_code;
  }else if (shop_category1_code) {
    var shop_category_code1 = shop_category1_code;
  }
  if (shop_category_code1) {
    $('#ds2_fullsearch').prepend(
      //カテゴリは最下層コードのみ指定する
      jQuery(document.createElement('input'))
        .attr('name','shop_category_code1')
        .attr('type','hidden')
        .val(shop_category_code1)
        .addClass('fullsearch_hidden')
    );
  }
  
  //カテゴリコードの指定 - サブ
  var shop_category_other3_code = $('#ds2_fullsearch_shop_category_other3_id').val();
  var shop_category_other2_code = $('#ds2_fullsearch_shop_category_other2_id').val();
  var shop_category_other1_code = $('#ds2_fullsearch_shop_category_other1_id').val();
  if (shop_category_other3_code) {
    var shop_category_code2 = shop_category_other3_code;
  }else if (shop_category_other2_code) {
    var shop_category_code2 = shop_category_other2_code;
  }else if (shop_category_other1_code) {
    var shop_category_code2 = shop_category_other1_code;
  }
//  if (shop_category_code2) {
//    $('#ds2_fullsearch').prepend(
//      //カテゴリは最下層コードのみ指定する
//      jQuery(document.createElement('input'))
//        .attr('name','shop_category_code2')
//        .attr('type','hidden')
//        .val(shop_category_code2)
//        .addClass('fullsearch_hidden')
//    );
//  }
  
  $('#ds2_fullsearch').submit();
  return false;
}

//フルサーチフォームの検索条件をリセットする。
function clearFullSearchForm() {
  //フォーム値をリセット
  $('.ds2_fullsearch_input:input').each( function(){
    $(this).val('');
  });
  
  //カテゴリセレクトメニューの初期化
  $('#ds2_fullsearch_shop_select').show();
  resetCategorySearchSelect('ds2_fullsearch_shop_category');
}

