/**
 * クッキーのシリアライズ
 */

function FwCookieSerial(path, domain, secure)
{
	var THIS;
	
	this.action = false;
	this.delim = ',';
	this.nl    = "\t";
	
	this.name = '';
	
	this.path   = path;
	this.domain = domain;
	this.secure = secure;
	
	this.ids  = new Array;
	this.vals = new Array;
	
	// 初期化
	this.init = function(name)
	{
		THIS = this;

		if (typeof(name) == 'string') {
			THIS.name = name;
			THIS.action = true;
		}
		return THIS.action;
	}

	// 保存するIDとデフォルト値を追加
	this.add = function(id, val)
	{
		var flag = false;

		if (typeof(id) == 'string') {
			THIS.ids.push(id);
			THIS.vals.push(val);
			flag = true;
		}
		return flag;
	}
	
	// IDに値を設定
	this.set = function(id, val)
	{
		var flag = false;
		var i, icount;

		if (typeof(id) == 'string') {
			icount = THIS.ids.length;
			for (i=0; i<icount; i++) {
				if (id == THIS.ids[i]) {
					THIS.vals[i] = val;
					flag = true;
					break;
				}
			}
		}
		return flag;
	}
	
	// IDの値を取得
	this.get = function(id)
	{
		var i, icount;

		if (typeof(id) == 'string') {
			icount = THIS.ids.length;
			for (i=0; i<icount; i++) {
				if (id == THIS.ids[i]) {
					return THIS.vals[i];
				}
			}
		}
		return null;
	}
	
	// 読込
	this.read = function()
	{
		if (!THIS.action) {
			return false;
		}
		var str  = fwCookieRead(THIS.name);
		var row  = new Array;
		var rows = new Array;
		var i, icount;
		var j, jcount;

		if (str) {
			rows = str.split(THIS.nl);
		}
		icount = rows.length;
		jcount = THIS.ids.length;
		for (i=0; i<icount; i++) {
			row = rows[i].split(THIS.delim);
			THIS.set(row[0], row[1]);
		}
		return true;
	}
	
	// 書込
	this.write = function(day, hour, minute)
	{
		if (!THIS.action) {
			return false;
		}
		var str    = '';
		var expires = fwCookieExpires(day, hour, minute);
		
		icount = THIS.ids.length;
		for (i=0; i<icount; i++) {
			if (i > 0) {
				str += THIS.nl;
			}
			str += THIS.ids[i] + THIS.delim + THIS.vals[i];
		}
		return fwCookieWrite(THIS.name, str, expires, THIS.path, THIS.domain, THIS.secure);
	}
}

