// ================================================================================
// クッキーの形式
// name = 名前
// password = パスワード
// ================================================================================

// クッキーからデータを取り出す
function GetCookie(keyword)
{
	// 変数宣言
	var buf;
	var i, j;
	var data;
	
	buf = document.cookie + ";";
	i = buf.indexOf(keyword, 0);
	
	if(i != -1)		// キーワードがあった場合
	{
		i += (keyword.length + 1);
		buf = buf.substring(i, buf.length);
		
		// &以下をカット
		j = buf.indexOf("&", 0);
		if(j != -1)
		{
			buf = buf.substring(0, j);
		}
		// ;以下をカット
		j = buf.indexOf(";", 0);
		if(j != -1)
		{
			buf = buf.substring(0, j);
		}
		
		return (unescape(buf));
	}
	
	return ('');	// キーワードがない場合は空文字
}

// ================================================================================
// クッキーにデータを保存する
// ●times : 訪問回数
// ●ID : 個人識別ID（初めてクッキーをセットした時の時間）
// ================================================================================
function SetCookie()
{
	// 変数宣言
	var id, times;
	var time, year, month, date, day, hour, minute, second;
	var days;
	var name, password, cookie, email, h_addr;
	var limit_time;
	
	// 名前とキーワードの取得
	name = GetCookie("name");
	password = GetCookie("password");
	
	// Cookie OnOffの取得
	cookie = GetCookie("cookie");
	if(cookie == "")
	{
		cookie = "off";
	}
	
	// メールアドレスとホームページアドレスの取得
	email = GetCookie("email");
	h_addr = GetCookie("h_addr");
	
	// 現在日時の取得
	time = new Date();
	
	year = time.getYear();
	
	// 訪問回数の取得
	times = GetCookie("times");
	if(times == "")
	{
		// 訪問回数とID値の設定
		month = time.getMonth();
		date = time.getDate();
		hour = time.getHours();
		minute = time.getMinutes();
		second = time.getSeconds();
		
		times = 1;
		id = year + "/" + month + "/" + date + "/" + hour + "/" + minute + "/" + second;
	}
	else
	{
		if(times == NaN)	// エラー処理
		{
			times = 1;
		}
		else
		{
			times++;
		}
		id = GetCookie("ID");
	}
	
	// クッキーの保存期間の設定（１年間）
	year++;
	time.setYear(year);
	month = time.getMonth();
	day = time.getDay();
	date = time.getDate();
	
	days = new Array("Sun", "Mon", "Tue", "Wnd", "Thu", "Fri", "Sat");
	limit_time = days[day] + ", " + date + "-" + month + "-" + year + " 00:00:00 GMT";
	
	document.cookie = "twclog=ID:" + id + "&times:" + times + "&name:" + name + "&password:" + password + "&email:" + email + "&h_addr:" + h_addr + "&cookie:" + cookie + ";expires=" + limit_time;
}