﻿//                                                        //
//            870.ne.jp Javascript関数定義                //
//                        [共通]                          //
//                      jQuery対応                        //
//                                                        //
//     Copyright 2011 e-pokke Inc. All rights reserved.   //
//                                                        //

var url_groupsite1 = 'http://www.870hanahana.ne.jp/';
var url_groupsite2 = 'http://www.870smile.ne.jp/';
var url_top        = 'http://www.870.ne.jp/';

// ----- ページ表示の初期化 -----
function init_page(Scode) {
  if (document.URL == url_top) {
    var agent = navigator.userAgent;
    if ( agent.search(/iPhone|iPod|BlackBerry|IEMobile/i) != -1 ||
        ( agent.search(/Android/i) != -1 && agent.search(/mobile/i) != -1 )){
      if (confirm('このサイトにはスマートフォン用のサイトがございます。\n表示しますか？')) {
        location.href = '/s/';
        return;
      }
    }
  }

  if (Scode == undefined) Scode = '';
  if ($('#imakara_box')) {
    $.ajax({
      url      : '/parts/imakara.cgi',
      type     : 'GET' ,
      cache    : false ,
      data     : {
        'Mode'  : 'viewAjax'
      } ,
      dataType : 'html',
      success  : function(data){
        $("#imakara_box").html(data);
      } ,
      error    : function(obj,status) {
        alert('エラーが発生しました(01)');
//        alert(obj.responseText);
      }
    })
  }

  if ($('#memberbar_box')) {
    update_memberbar(Scode);
  }

  if (Scode) {
    $.ajax({
      url      : '/parts/shop_nearby.cgi',
      type     : 'GET' ,
      cache    : false ,
      data     : {
        'Mode'  : 'viewAjax',
        'Scode' : Scode
      } ,
      dataType : 'html',
      success  : function(data){
        $("#nearby_box").html(data);
      } ,
      error    : function(obj,status) {
        alert('エラーが発生しました(03)');
//        alert(obj.responseText);
      }
    })

    if ($("hanamaru_favorites")) {
      set_hanamaru_favorites(Scode);
    }
  }

  if ($("#hanamaru_vote")) {
    set_hanamaru_vote(Scode);
  }

  if ($('#banner_box_header')) {
    $.ajax({
      url      : '/parts/banner.cgi',
      type     : 'POST',
      data     : {
        'Mode'  : 'viewAjax',
        'Stype' : 11 ,
        'Surl'  : document.URL
      } ,
      dataType : 'html',
      success  : function(data){
        $("#banner_box_header").html(data);
      } ,
      error    : function(obj,status) {
        alert('エラーが発生しました(20)');
//        alert(obj.responseText);
      }
    })
  }

  if ($('#banner_box_side2')) {
    $.ajax({
      url      : '/parts/banner.cgi',
      type     : 'POST',
      data     : {
        'Mode'  : 'viewAjax',
        'Stype' : 22 ,
        'Surl'  : document.URL
      } ,
      dataType : 'html',
      success  : function(data){
        $("#banner_box_side2").html(data);
      } ,
      error    : function(obj,status) {
        alert('エラーが発生しました(21)');
//        alert(obj.responseText);
      }
    })
  }

  if ($("#hanamaru_looks")) {
    set_hanamaru_looks(Scode);
  }


  $("#toTop a").click(function(){  
    $('html,body').animate({ scrollTop: $($(this).attr("href")).offset().top }, 'slow','swing');  
    return false;  
  })  

  $('a').click(function() {   
    jump_group_sites($(this));   
  });   
}

// ----- 姉妹サイトリンクの移動 -----
function jump_group_sites(link) {
  if (link.attr("href").indexOf(url_groupsite1) >= 0 || link.attr("href").indexOf(url_groupsite2) >= 0) {
//    alert(link.attr("href"));
    link.attr("href", '/member/send_link.cgi?url=' + encodeURIComponent(link.attr("href")));
  }
}

// ----- バナークリック受付け -----
function save_banner_click(type,code) {
  $.ajax({
    url      : '/parts/banner_click.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode' : 'viewAjax' ,
      'code' : code ,
    } ,
    dataType : 'html',
    success  : function(data){
      $('#banner_dummy' + type).html(data);
      return true;
    } ,
    error    : function(obj,status) {
      return true;
    }
  })
}

// ----- メンバーバーの更新 -----
function update_memberbar(Scode) {
  $.ajax({
    url      : '/parts/memberbar.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'viewAjax' ,
      'Scode' : Scode ,
    } ,
    dataType : 'html',
    success  : function(data){
      var agent = navigator.userAgent;
      if ( agent.search(/iPhone|iPad|iPod|Android|BlackBerry|IEMobile/i) != -1 ){
        $('#memberbar_box').hide();
      }
      $('#memberbar_box').html(data);

      if($('#login_status').val() == 1) {
        $('#btnarea_nologin').hide();
        $('#btnarea_login').show();
        $('#coupon_nologin').hide();
        $('#coupon_login').show();
        $('#BDcoupon_nologin').hide();
        $('#BDcoupon_login').show();
        if($('#birthday_status').val() == 1) {
          $('#BDcoupon').show();
        }
        else {
          $('#BDcoupon').hide();
        }
        $('#review_button').show();
      }
      else {
        $('#btnarea_nologin').show();
        $('#btnarea_login').hide();
        $('#coupon_nologin').show();
        $('#coupon_login').hide();
        $('#BDcoupon_nologin').show();
        $('#BDcoupon_login').hide();
        $('#BDcoupon').hide();
        $('#review_button').hide();
      }
    } ,
    error    : function(obj,status) {
//      alert('エラーが発生しました(02)');
//      alert(obj.responseText);
    }
  })
}

// ----- お気に入りの表示 -----
function set_hanamaru_favorites(Scode) {
  $.ajax({
    url      : '/parts/favorites.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'viewAjax' ,
      'Scode' : Scode ,
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_favorites").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(30)');
//      alert(obj.responseText);
    }
  })
}

// ----- お気に入りへ追加 -----
function add_hanamaru_favorites(Scode) {
  $.ajax({
    url      : '/parts/favorites.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'addAjax' ,
      'Scode' : Scode
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_favorites").html(data);
      update_memberbar();
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(31)');
//      alert(obj.responseText);
    }
  })
}

// ----- お気に入りの削除 -----
function delete_hanamaru_favorites(code) {
  if ( confirm('お気に入りから削除してもよろしいですか？') == false ) {
    return;
  }

  $.ajax({
    url      : '/parts/favorites.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'deleteAjax' ,
      'code'  : code
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_favorites").html(data);
      update_memberbar();
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(32)');
//      alert(obj.responseText);
    }
  })
}



// ----- はなまる１票の表示 -----
function set_hanamaru_vote(Scode) {
  $.ajax({
    url      : '/parts/vote.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'viewAjax' ,
      'Scode' : Scode
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_vote").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(40)');
//      alert(obj.responseText);
    }
  })
}

// ----- はなまる１票の投票 -----
function add_hanamaru_vote(Scode) {

  $.ajax({
    url      : '/parts/vote.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'addAjax' ,
      'Scode' : Scode
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_vote").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(41)');
//      alert(obj.responseText);
    }
  })
}


// ----- この店舗を見た人はの表示 -----
function set_hanamaru_looks(Scode) {
  $.ajax({
    url      : '/parts/shop_looks.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'viewAjax' ,
      'Scode' : Scode
    } ,
    dataType : 'html',
    success  : function(data){
      $("#hanamaru_looks").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(40)');
//      alert(obj.responseText);
    }
  })
}



// ----- フッターの追加 -----
function include_footor() {
  if ($('#footer')) {
    $.ajax({
      url      : '/parts/footor.html',
      type     : 'GET' ,
      cache    : false ,
      data     : {
        'Mode'  : 'viewAjax'
      } ,
      dataType : 'html',
      success  : function(data){
        $("#footer").html(data);
      } ,
      error    : function(obj,status) {
        alert('エラーが発生しました(20)');
//        alert(obj.responseText);
      }
    })
  }
}

// ----- いまから検索の開閉 -----
function toggle_imakara() {
  if ($('#imakara_box').css('right') == '0px') {
    $('#imakara_box').animate({ right: '-302px' }, 300, 'swing');
  }
  else {
    $('#imakara_box').animate({ right: '0px' }, 300, 'swing'); 
  }
}

// ----- いまから検索の実行チェック -----
function check_imakara_saerch() {
  if (document.imakara_form.Sct1.value == '') {
    alert('カテゴリ大を選択してください');
    return false;
  }
  if (document.imakara_form.Sct2.value == '') {
    alert('サブカテゴリを選択してください');
    return false;
  }
  if (document.imakara_form.Sdate.value == '') {
    alert('日付を選択してください');
    return false;
  }

  return true;
}

// ----- カテゴリーの切り替え -----
function change_imakara_category(ct1) {
  $.ajax({
    url      : '/parts/imakara.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'Mode'  : 'ct2Ajax' ,
      'Sct1'  : ct1
    } ,
    dataType : 'html',
    success  : function(data){
      $("#imakara_category").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(imakara:01)');
//      alert(obj.responseText);
    }
  })
}
// ----- カレンダーの表示 -----
function view_imakara_calendar(idno) {
  var ym = '';
  if ( $( 'Date' + idno ).value ) ym = $( 'Date' + idno ).value.substring(0,7);

  $( 'calendar_area' + idno ).innerHTML = '';

  change_imakara_calendar(ym,idno);
  change_visibility('imakara_calendar');
}

// ----- カレンダーの切替え -----
function change_imakara_calendar(ym,idno) {
  $.ajax({
    url      : '/parts/calendar.cgi',
    type     : 'GET' ,
    cache    : false ,
    data     : {
      'ym'    : ym ,
      'idno'  : idno
    } ,
    dataType : 'html',
    success  : function(data){
      $("#imakara_calendar").html(data);
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(imakara:02)');
//      alert(obj.responseText);
    }
  })
}

// ----- 日付のセット -----
function set_imakara_date(date,idno) {
  $('#Date' + idno ).val(date);
  change_visibility('imakara_calendar');
}



// ----- メンバーバーの開閉[ログイン前] -----
function toggle_memberbar() {
  if ($('#members_area').css('bottom') == '0px') {
    $('#members_area').animate({ bottom: '-107px' }, 300, 'swing');
  }
  else {
    $('#members_area').animate({ bottom: '0px' }, 300, 'swing');
  }
}

// ----- メンバーバーからのログイン -----
function login_member() {
  if (document.login_form.id.value == '') {
    alert('IDを入力してください');
    document.login_form.id.focus();
    return;
  }
  if (document.login_form.pswd.value == '') {
    alert('パスワードを入力してください');
    document.login_form.pswd.focus();
    return;
  }

  $.ajax({
    url      : '/parts/memberbar.cgi' ,
    type     : 'POST' ,
    cache    : false ,
    data     : {
      'Mode'  : 'loginAjax',
      'id'    : document.login_form.id.value ,
      'pswd'  : document.login_form.pswd.value ,
      'auto'  : (document.login_form.auto.checked) ? 1 : 0
    } ,
    dataType : 'html',
    success  : function(data,status){
      if (data == '030') {
        alert('IDまたはパスワードに誤りがあります。\n入力しなおしてください。');
      }
      else if (data == '040') {
        alert('メールが一定回数届かなかったため、\nログインを一時停止中です。');
      }
      else {
        update_memberbar();
        location.reload();
      }
    } ,
    error    : function(obj,status) {
      alert('エラーが発生しました(10)');
      alert(obj.responseText);
    }
  })

}

// ----- メンバーバーのお気に入り店舗開閉[ログイン後] -----
function toggle_favorites() {
  if ($('#members_area').css('bottom') == '0px') {
    $('#members_area').animate({ bottom: '-107px' }, 300, 'swing');
  }
  else {
    $('#members_area').animate({ bottom: '0px' }, 300, 'swing');
  }
}


// ----- 送信・変更時の確認 -----
var send_flag = 0;

function check_submit() {
  if ( send_flag == 1 ) {
    alert('ただいま送信中です。\n「OK」ボタンをクリックしてお待ちください。');
    return false;
  }

  send_flag = 1;

  return true;
}

// ----- 削除の確認 -----
function check_delete(url,name) {
  if ( confirm('「 ' + name + ' 」を削除してもよろしいですか？') == false ) {
    return false;
  }
  location.href = url;
}

// ----- ログインページのURLパラメータの取得 -----
function set_login_param() {
  if ( document.URL.indexOf("?") >= 0 ) {
    mid = document.URL.split("?");
    document.input_form.Referer.value = mid[1];
  }
}

// ----- アクセスログ記録 -----
function access (Scode) {
  var now = new Date();
  document.write("<img src='/cgi/ac.cgi?Scode=" + Scode + "&t=" + now.getSeconds() + "' width='1' height='1' border='0' />");
}

// ----- 別ウィンドウの表示 -----
function open_window(url,width,height,target) {
  if (url != '') {
    var x = parseInt( window.screen.width/2-width/2 );
    var y = parseInt( window.screen.height/2-height/2 );
    window.open(url,target,"toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height+",left="+x+",top="+y+",screenX="+x+",screenY="+y);
  }
}












// ----- オブジェクトの取得 -----
function get_object(id) {
  if (document.getElementById) {
    return document.getElementById(id);
  }
  else if (document.all) {
    return document.all(id);
  }
  return null;
}

// ----- クッキーの読み込み -----
function get_cookie(key){
  tmp = document.cookie+";";
  tmp1 = tmp.indexOf(key, 0);
  if( tmp1 != -1 ){
    tmp = tmp.substring( tmp1, tmp.length );
    start = tmp.indexOf("=", 0 ) + 1;
    end = tmp.indexOf( ";", start );
    return( unescape( tmp.substring( start, end )));
  }
  return("");
}


// ----- 指定エリアの表示・非表示(display) -----
function change_display(id) {
  obj = get_object(id);
  if ( obj ) {
    if ( obj.style.display == 'none' || obj.style.display == '' ) {
      obj.style.display = 'block';
    }
    else {
      obj.style.display = 'none';
    }
  }
}

// ----- 指定エリアの表示・非表示(visibility) -----
function change_visibility(id) {
  obj = get_object(id);
  if ( obj ) {
    if ( obj.style.visibility == 'hidden' || obj.style.visibility == '' ) {
      obj.style.visibility = 'visible';
    }
    else {
      obj.style.visibility = 'hidden';
    }
  }
}



// ---------- End of File ----------

