//
// TOPページ用 JavaScriptファイル
//
// SEO対策で外部ファイルにする。
// 2008-06-25 
//
  var https_host = location.hostname;
  if (https_host != '') {
    https_host = 'https://' + https_host;
  }
  
  function getValueFromCookie(key) {
    var cookies = document.cookie;
    var cValue = '';
    var index   = cookies.indexOf(key);
    if (index > -1) {
      if (cookies.indexOf(';',index) > 0) {
        cValue =  cookies.substring(
        cookies.indexOf('=',index) + 1, cookies.indexOf(';',index));
      } else {
        cValue =  cookies.substring(
        cookies.indexOf('=',index) + 1, cookies.length);
      }
    }
    return cValue;
  }

  function isLogin() {
    if (getValueFromCookie('A_USERSTATUS').length > 0 || getValueFromCookie('P_USERSTATUS').length > 0) {
      return true;
    } else {
      return false;
    }
  }
  
  function showLoginIcon() {
    if (isLogin()) {
      document.write('<ul id="subNavLogin">');
      document.write('<li class="members"><a href="' + https_host + '/avx/sv/top">マイページ</a></li>');
      document.write('<li class="cart"><a href="/avx/sv/cart2">カート</a></li>');
      document.write('<li class="confirm"><a href="' + https_host + '/avx/sv/mypordhist02">注文確認</a></li>');
      document.write('<li class="logout"><a href="/avx/sv/Logout">ログアウト</a></li>');
      document.write('</ul>');
    } else {
      document.write('<ul id="subNav">');
      document.write('<li class="login"><a href="' + https_host + '/avx/sv/Login">ログイン</a></li>');
      document.write('<li class="members"><a href="/st/registinfo.html">会員登録</a></li>');
      document.write('<li class="cart"><a href="/avx/sv/cart2">カート</a></li>');
      document.write('<li class="confirm"><a href="' + https_host + '/avx/sv/mypordhist02">注文確認</a></li>');
      document.write('</ul>');
    }
    return; 
  }
  
  function decodeNickname() {
    var dispNickname = getValueFromCookie('A_DISP_NICKNAME');
    var nickname = getValueFromCookie('A_NICKNAME');

    if (isLogin() && dispNickname.length > 0) {
      dispNickname = dispNickname.substring(dispNickname.indexOf("%u"), dispNickname.length);
      document.write("「"+unescape(dispNickname)+"」さん、こんにちは！&nbsp;本人でない場合は<a href=\"/avx/sv/Logout\">コチラ（ログアウト）</a>");
    }else if (isLogin() && nickname.length > 0) {
      nickname = nickname.substring(nickname.indexOf("%u"), nickname.length);
      nickname = unescape(nickname);
      if (isLogin() && nickname.indexOf("@") == -1) {
        document.write("「"+nickname+"」さん、こんにちは！&nbsp;本人でない場合は<a href=\"/avx/sv/Logout\">コチラ（ログアウト）</a>");
      }else{
        document.write('「ゲスト」さん、こんにちは！<br>無料会員登録は<a href="/st/registinfo.html">コチラ！</a>&nbsp;すでに会員の方は<a href="' + https_host + '/avx/sv/Login">ログイン</a>してね。');
      }
    }else{
      document.write('「ゲスト」さん、こんにちは！<br>無料会員登録は<a href="/st/registinfo.html">コチラ！</a>&nbsp;すでに会員の方は<a href="' + https_host + '/avx/sv/Login">ログイン</a>してね。');
    }
    return; 
  }
  
  function decodeClkMailSendSt() {
   var encoded = document.cookie;
   beginIndex = encoded.indexOf("A_CLKMAILSENDST=");
   if (isLogin() && beginIndex != -1) {
     encoded = encoded.substring(beginIndex + 16, beginIndex + 18);
     if(encoded == "NG"){
	document.write('<br>ご登録メールアドレスにメルマガが配信できません。<a href="' + https_host + '/avx/sv/mod01s?jsiteid=mumo&emailtype=pc">コチラ</a>からご変更ください。');
	 }
   }
    return; 
  }

  function decodeUserShopPointSt() {
    var e = getUserShopPointSt();
    if (!e) {
      return;
    }

    var now = (new Date()).getTime();
    var checkInterval = 3600 * 1000;
    var update = (now - e.lastModified > checkInterval);
    if (!update) {
      var msg = buildUserShopPointStHtml(e);
      $('#userShopPointSt').html(msg);

    } else {
      $.ajax({
        async: true,
        timeout: 20000,
        dataType: 'html',
        data: {},
        cache: false,
        url: '/avx/sv/userShopPointSt',
        success: function (data) {
          var e = getUserShopPointSt();
          if (e) {
            var msg = buildUserShopPointStHtml(e);
            $('#userShopPointSt').html(msg);
          }
        }
      });
    }
  }

  function buildUserShopPointStHtml(e) {
    var msg = '';
    var mypUrl = 'https://' + window.location.host + '/avx/sv/pointhists';
    if (e.remainTotal < 0) {
      e.remainTotal = 0;
    }

    var rt = formatComma(e.remainTotal);
    
    msg += '現在の保有ゴールドは' + rt + 'Gです。';
    msg += 'ご確認は <a href=' + mypUrl + '>コチラ</a>から。<br>';

    var DAY_MS = 24 * 3600 * 1000;
    var warnExpireDayMs = e.warnExpireDay * DAY_MS;
    var now = (new Date()).getTime();
    var limit = e.minUseEndDate + DAY_MS;
    if (e.minExpireRemain > 0 && now < limit && (now + warnExpireDayMs) >= limit) {
      var re = ("" + e.minExpireRemain).replace(/,/g, '');
      re = re.replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, "$1,");
      var r = Math.floor((limit - now) / DAY_MS);
      msg += (r == 0 ? '本日いっぱいで' : ('残り' + r + '日で')) + re + 'Gが失効してしまいます。急いで!!<br>';
    }

    return msg;
  }

  function getUserShopPointSt() {
    if (!isLogin()) {
      return null;
    }
    var s = unescape(getValueFromCookie('A_USERSHOPPOINTST'));
    if (!s) {
      return null;
    }
    return eval('(' + s + ')');
  }

  function readRecommendHtml(eria, recType) {
      var i;
      var randset = Math.floor(Math.random()*20);
      if(recType == 9) {
			document.getElementById('recomendSell' + eria + '_' + randset).style.display = 'block'; 
      } else {
			document.getElementById('recomendMumo' + eria + '_' + randset).style.display = 'block'; 
      }
  }
  function checkMumoRecommend(e) {

      var n = 0;
      var cookie_key;
      if (e.area1RecType == 2) {
          cookie_key = "r_lc_k_";
      } else if (e.area1RecType == 3) {
          cookie_key = "r_tr_k_";
      } else if (e.area1RecType == 9) {
          return 1;
      } 
      
      var i;
      i = 0;
      while (i < 10) {
        var cookie = getValueFromCookie(cookie_key + i);
        cookie = decodeURI(cookie);
        cookie = cookie.replace(/%2C/gi, ',').replace(/%3A/gi, ':').replace(/%2F/gi, '/');
    
        if(cookie != "" && cookie.indexOf(";") == -1) {
	        n=1;
       		break;
		}
    	i+=1;
      }
    return n; 
  }
  function showRecommend(eria, imgsvurl) {
  
    var e = getRecommendRecType();
    if (!e) {
      if (eria == 1) {
	      readRecommendHtml(eria, 2);
	  }
      return; 
    }
    
    var httpServletPath = 'http://' + window.location.host + '/avx/sv';
    
    var flg = 1;
    var recType;
    if(eria == 1) {
        recType = e.area1RecType;
    } else {
        recType = e.area2RecType;
        flg = checkMumoRecommend(e);
    }
    var cookie_key;
    if (recType == 2) {
        cookie_key = "r_lc_k_";
    } else if (recType == 3) {
        cookie_key = "r_tr_k_";
    } else if (recType == 9) {
        readRecommendHtml(eria, recType);
        return; 
    } else if (flg == 1) {
        readRecommendHtml(eria, 2);
        return; 
    }

    var n = 0;
    for (var i = 0; i < 10 && n < 5; i++) {
      var cookieName = cookie_key + (i + 1);
      var cookie = getValueFromCookie(cookieName);
      cookie = decodeURIComponent(cookie);
      cookie = cookie.replace(/%2C/gi, ',').replace(/%3A/gi, ':').replace(/%2F/gi, '/');
      cookie = cookie.replace (/^\"/, '');
      cookie = cookie.replace (/\"$/, '');    
      if(cookie != "" && cookie.indexOf(";") == -1) {
          var cookie_val = cookie.split('\t');
          if (cookie_val.length >= 6) {
              rt = formatComma(cookie_val[5]);
          }
          var siteid = cookie_val.length >= 7 ? cookie_val[6] : '';
          if (siteid != '' && siteid != 'mumo') {
            continue;
          }

          var write1;
          if(n == 0) {
              if(recType == 2) {
                  write1 = '<div class="subTitle clearFix"><img src="/image/recommend/hd_recommend1_2.gif"alt="最近チェックした商品"></div><ul class="clearFix recommendList">';
              } else {
                  write1 = '<div class="subTitle clearFix"><img src="/image/recommend/hd_recommend1_3.gif"alt="あなたにオススメの商品"></div><ul class="clearFix recommendList">';
              }
	          document.write(write1);
          }

　　　　　pos = cookie.indexOf('\"',0);
          if(pos == 0) {
              cookie = cookie.substr(1, cookie.length -2);
          }
          write1 = '<li><div class="recommendImg">';
          var write2 = '<a href=' + httpServletPath + cookie_val[2] + '?seq_exhibit_id=' + cookie_val[0] + ' target=\"_self\">';
          var write3 = '<img src=' + imgsvurl + cookie_val[1] + ' alt=' + cookie_val[4] + '></a></div>';
          var write4 = '<p class="recommendTitle">';
          var write5 = cookie_val[3] + '<br>';
          var write6 = '<a href=' + httpServletPath + cookie_val[2] + '?seq_exhibit_id=' + cookie_val[0] + ' target=\"_self\">' + cookie_val[4] + '<br></a>' + '&yen;' + rt +'(税込)</a></p></li>';
          document.write(write1 + write2 + write3 + write4 + write5 + write6);
	      n++;
	  }
    }
    if(n > 0) {
        document.write('</ul>');
	} else {
	    if(eria == 1 || (eria == 2 && flg == 1)) {
           readRecommendHtml(eria, 2);
        }
    }
    return; 
  }  

  function formatComma(num) {
      var rt;
      if (num) {
          rt = ("" + num).replace(/,/g, '');
          rt = rt.replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, "$1,");
      } else {
          rt = '0';
      }
      return rt;
  }
  
  function getRecommendRecType() {
    var s = unescape(getValueFromCookie('A_TOPRECOMMENDRECTYPE'));
    if (!s) {
      $.ajax({
        async: false,
        timeout: 20000,
        dataType: 'html',
        data: {},
        cache: false,
        url: '/avx/sv/recommendRecType',
        success: function (data) {
          s = unescape(getValueFromCookie('A_TOPRECOMMENDRECTYPE'));
        }
      });
    } else {
      var e = eval('(' + s + ')');
	  var now = (new Date()).getTime();
	  var checkInterval = 3600 * 1000;
	  var update = (now - e.lastModified > checkInterval);
	  if (update) {
	    $.ajax({
	      async: true,
	      timeout: 20000,
	      dataType: 'html',
	      data: {},
	      cache: false,
	      url: '/avx/sv/recommendRecType',
	      success: function (data) {
            s = unescape(getValueFromCookie('A_TOPRECOMMENDRECTYPE'));
	      }
	    });
	  }
    }
    if (!s) {
      return null;
    }
    return eval('(' + s + ')');
  }
  


//
// レイヤーを表示するスクリプト
//
  function showUpLayer() {
    timerID = setTimeout("closeDownLayer()", 30000);

}

//
// レイヤーを閉じるスクリプト
//
  function closeDownLayer() {

    var objID1=document.getElementById('Back_layer1');
    objID1.style.display='none';

    var objID2=document.getElementById('Front_layer');
    objID2.style.display='none';

  }


