//////////////////////////////////////////////////////////////////
//
//================================================================
// KOKUYO --- JavaScript
//            Copyright 2005 CATWALK,Inc. All rights reserved.
//            http://www.web-kyoto.com
//================================================================
//
// COOKIE作成・読み込み・削除関数
// Created by CATWALK（三原比呂美）
// 2005年05月24日
// このスクリプトに対するお問い合わせは
// 三原　比呂美：hiromi@web-kyoto.com
// 宛に願います。
//
//////////////////////////////////////////////////////////////////

// Cookie削除
function removeCookie(key) {
	var cookie = this.prefix + key + "=";
	cookie += "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
	
	// 値の設定
	if(this.path)   cookie += "; path=" + this.path;
	if(this.domain) cookie += "; domain=" + this.domain;
	if(this.secure) cookie += "; secure=" + this.secure;
	
	// 空のCookieの設定
	this.doc.cookie = cookie;
}

// Cookieの取得
function getCookie(key) {
	var tmp1, tmp2, xx1, xx2, xx3, len;
	tmp1 = " " + this.doc.cookie + ";";
	xx1 = 0;
	xx2 = 0;
	len = tmp1.length;
	
	while(xx1 < len) {
		xx2 = tmp1.indexOf(";", xx1);
		tmp2 = tmp1.substring(xx1 + 1, xx2);
		xx3 = tmp2.indexOf("=");
		if(tmp2.substring(0,xx3) == (this.prefix + key)) {
			return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
		}
		xx1 = xx2 + 1;
	}
	return("");
}

// Cookieの設定
function setCookie(key, value) {
	var cookie = this.prefix + key + "=" + escape(value);
	
	// 値の設定
	if(this.expiration) cookie += "; expires=" + this.expiration.toGMTString();
	if(this.path)       cookie += "; path=" + this.path;
	if(this.domain)     cookie += "; domain=" + this.domain;
	if(this.secure)     cookie += "; secure=" + this.secure;
	
	this.doc.cookie = cookie;
}

// オブジェクト作成用関数
function Cookie() {
	var arg = arguments;
	
	// 値を取得
	this.prefix     = (arg[0] ? arg[0] : "");
	this.expiration = (arg[1] ? new Date(new Date().getTime() + arg[1] * 24 * 60 * 60 * 1000) : null);
	this.path       = (arg[2] ? arg[2] : null);
	this.domain     = (arg[3] ? arg[3] : null);
	this.secure     = (arg[4] ? arg[4] : false);
	this.doc        = (arg[5] ? arg[5] : document);
	
	// メソッド設定
	this.set       = setCookie ;
	this.get       = getCookie ;
	this.remove    = removeCookie ;
}

