﻿var ST_KEY_NAME="history";
var ST_SPLIT_LNG1="|";
var ST_SPLIT_LNG2=":";
var ST_VIEW_CNT=3;

//=============================================================================
//クッキーセット
//=============================================================================
function fncSetCookie(strKey,strValue,strExpires){

//  alert(document.cookie + "クッキーセット前");

  if(strExpires==""){
    strExpires="expires=Tue, 31-Dec-2030 23:59:59";
  }

  document.cookie =  strKey + "=" + escape(strValue) + "; path=/;" + strExpires + ";";

//  alert(strKey + "strKey");
//  alert(strValue + "strValue");
//  alert(strExpires + "strExpires");

//  alert(document.cookie + "クッキーセット後");

}

//=============================================================================
//クッキーゲット
//=============================================================================
function fncGetCookie(strKey){
  var intStartLength,intEndLength;

//  alert("OK");

  var strValue=document.cookie + ";";

//  alert(strValue);  
//  alert(document.cookie);

  if(strValue.indexOf(strKey,0)==-1){
    return("");
  }

//  alert("OK");
  
  strValue=strValue.substring(strValue.indexOf(strKey,0),strValue.length);
  intStartLength=strValue.indexOf("=",0) + 1;
  intEndLength=strValue.indexOf(";",intStartLength);
  
  return(unescape(strValue.substring(intStartLength,intEndLength)));
}

//=============================================================================
//履歴セット
//=============================================================================
function fncSetHistory(strName,strText,strUrl){
  
  var strValue=strName + ST_SPLIT_LNG2 + strText +  ST_SPLIT_LNG2 + escape(strUrl) + ST_SPLIT_LNG1;
  
  //履歴取得
  strHistory=fncGetCookie(ST_KEY_NAME);
  
  //同じ文字列があったら削除
  if(strHistory.indexOf(strValue,0)!=-1){
    strHistory=strHistory.replace(strValue,"");
  }

//  alert(strValue + "同じ文字列があったら削除後");  
//  alert(strHistory + "同じ文字列があったら削除後");
  
  //先頭に履歴を追加
  strHistory=strValue + strHistory;

//  alert(strHistory);

  //クッキーに書き込み
  fncSetCookie(ST_KEY_NAME,strHistory,"");
}

//=============================================================================
//履歴ゲット
//=============================================================================
function fncGetHistory(){
  
  var strHistory;
  var aryTemp1;
  var aryTemp2;
  var strValue="";
  var intViewCnt=0;

  //履歴取得
  strHistory=fncGetCookie(ST_KEY_NAME);

//  alert(strHistory);

  
  //値有無確認
  if(strHistory.indexOf(ST_SPLIT_LNG1,0)==-1){
    strValue="<p class='no-history'>最近閲覧した製品情報の上位"+ST_VIEW_CNT+"件が表示されます</p>";
    //document.write(strValue);
    //return("");
    return strValue;
  }

  //情報分解
  aryTemp1=strHistory.split(ST_SPLIT_LNG1);

  intViewCnt=ST_VIEW_CNT;
  if(intViewCnt==0 || intViewCnt>(aryTemp1.length-1)){
    intViewCnt=aryTemp1.length-1;
  }
	
  for(i=0;i<intViewCnt;i++){
    aryTemp2=aryTemp1[i].split(ST_SPLIT_LNG2);
	
    //HTMLタグ生成
    strValue+="<dl class='get-history'>\n";
    
    strValue+="<dt>";
    strValue+="<a class='product-name' href='" + aryTemp2[2] + "' title='"+aryTemp2[0]+"'>";
    strValue+=aryTemp2[0];
    strValue+="</a>";
    strValue+="</dt>\n";

    strValue+="<dd>";
    strValue+="<a href='" + aryTemp2[2] + "' title='"+aryTemp2[0]+"'>";
    strValue+=aryTemp2[1];
    strValue+="</a>";
    strValue+="</dd>\n";

    strValue+="</dl>";
  }

  //document.write(strValue);
  return strValue;
}

