/*
 * key    =  キーワード
 * value  =  データ
 * expire =  保存期間（日単位）
 */
function writeCookie(key, value, expire){

	if(!navigator.cookieEnabled){
		//alert("Cookieへの書き込みができません");
		return;
	}

	sday = new Date();
	sday.setTime(sday.getTime() + (expire * 1000 * 60 * 60 * 24));
	s2day = sday.toGMTString();
	document.cookie = key + "=" + escape(value) + ";expires=" + s2day;
}

/*
 * key    =  キーワード
 */
function readCookie(key){

	if(typeof(key) == "undefined") {
		return "";
	}
	
	key = key + "=";
	data = "";
	
	// クッキー情報を読み込む
	scookie = document.cookie + ";";
	// キーワードを検索
	start = scookie.indexOf(key);
	
	// キーワードと一致するものあり
	if (start != -1){
		// 情報の末尾位置を検索
	    end = scookie.indexOf(";", start);
	    // データ取り出し
	    data = unescape(scookie.substring(start + key.length, end));  
	}
	
	return data;
}

function delCookie(key){
	document.cookie = key + "=;expires=Thu,01-Jan-70 00:00:01 GMT";
}

function save2ProductsHistory(croot, fileSv, sid, pid, imgPath, price, name) {
	var html = sid + '->' + pid + '->' + fileSv + '->' + imgPath + '->' + price + '->' + name + '<E>';
	var htmlBuffer = readCookie('_p');
	
	if (htmlBuffer != null) {
		var productsHtmlArray = htmlBuffer.split('<E>');
		var token = sid + '->' + pid;
		var newHtmlBuffer = '';
		
		if (productsHtmlArray != null && productsHtmlArray.length > 1) {
			if (productsHtmlArray.length > 10) {
				productsHtmlArray[0] = '';
			}

			for (i = 0; i < productsHtmlArray.length && i < 11; i++) {
				var productHtml = productsHtmlArray[i];
				if (productHtml != null && productHtml != '' && productHtml.indexOf(token, 0) == -1) {
					newHtmlBuffer = newHtmlBuffer + productsHtmlArray[i] + '<E>';
				}
			}
			
			newHtmlBuffer = newHtmlBuffer + html;
			writeCookie('_p', newHtmlBuffer, 365);
		} else {
			htmlBuffer = htmlBuffer + html;
		
			writeCookie('_p', htmlBuffer, 365);
		}
	} else {
		writeCookie('_p', html, 365);
	}
}

function displayProductsHistory(croot) {
	var htmlBuffer = readCookie('_p');

	if (htmlBuffer != null) {
		var productsHtmlArray = htmlBuffer.split('<E>');

		if (productsHtmlArray != null && productsHtmlArray.length > 1) {
			document.write('<tr>');
    		document.write('<td width="1">');
    		document.write('<img src="' + croot + '/img/sp.gif" width="1" height="10" alt="">');
    		document.write('</td>');
    		document.write('</tr>');
    		
			document.write('<tr><td align="left" width="195" border="1" bgcolor="#E0DBBE" height="30" style="padding-left : 5px;" valign="middle">||| 最近チェックした商品</td></tr>');
			document.write('<tr>');
			document.write('<td width="195" ALIGN="center" bgcolor="#ffffff" style="padding-top : 5px ;">');
			document.write('<table width="195" cellspacing="0" cellpadding="0" border="0">');
			
			for (i = productsHtmlArray.length-1; i > -1; i--) {
				if(productsHtmlArray[i] == '' || productsHtmlArray[i] == null) {
					continue;
				}
				
				var paramArray = productsHtmlArray[i].split('->');
				var sid = paramArray[0];
				var pid = paramArray[1];
				var fileSv = paramArray[2];
				var imgPath = paramArray[3];
				var price = paramArray[4];
				var name = paramArray[5];
				
				var html = '<tr>' + 
						   '<td align="left" width="90" style="padding-left : 10px">' +
						   '<a href="JavaScript:subWin(\'' + sid + '\',\'' + pid + '\')">' + 
						   '<img src="' + croot + '/cnt/' + fileSv + '/' + sid + '/itm/' + pid + '-000' + '/' + imgPath + '" width="90" height="67" border="0"/></a>' +
						   '</td>' +
						   '<td align="right" style="padding-right : 5px;">' + price + '円' + '</td>' + 
						   '</tr>' + 
						   '<tr>' + 
						   '<td colspan="2" align="center" style="padding-top : 3px;padding-bottom : 15px"><strong>' + 
						   '<a href="JavaScript:subWin(\'' + sid + '\',\'' + pid + '\')">' + 
						   name + 
						   '</a></strong></td>' +
						   '</tr>';
						   
				document.write(html);
			}
			
			document.write('</table>');
			document.write('</td>');
			document.write('</tr>');
		}
	}
}