// ArrayRouteCookie.js
//
// 越後妻有アートナビゲーション整備事業
// オンデマンドマップ
// JavaScript共通「クッキー管理(配列)クラス」
// 
// 機能: 目的地ID(登録No.)の配列を１つのクッキーに格納および配列からの取出しを行ないます
//
var len = 0;  //登録予定の目的地数
var len2 = 0;  //登録済み目的地数
var maxroute = 20; //周遊ルートに登録可能な目的地の最大数

	// 目的地IDのクッキー
	// strCookieName: クッキーの名称 例) "routepoint" 
	ArrayRouteCookie = function(strCookieName) {
		this.arrayId = new Array();	// 目的地IDの配列
		this.arrayId2 = new Array();	// テスト
		this.strDlm = ",";
		this.maxCount = 100;	// 格納する目的地IDの最大数
		this.strCookieName = strCookieName;
		this.expireAfter = (12*60*60*1000);	// クッキーの有効期限(12時間後まで)
		this.path = "/";		// URLのパスを指定する。省略すると documentのパスになる。
		this.domain = "";	// ドメインを指定する。省略するとサーバのホスト名になる。
	};

	// URLのパスを設定
	ArrayRouteCookie.prototype.setPath = function(path) {
		this.path = path;
	};

	// ドメインを設定
	ArrayRouteCookie.prototype.setDomain = function(domain) {
		this.domain = domain;
	};

	// クッキー配列の読み出し
	ArrayRouteCookie.prototype.readCookie = function() {

		var arrayCookie = document.cookie.split(" ");	// クッキーの配列
		var maxCookieCount = arrayCookie.length;		// クッキーの配列の要素数
		var temp = null;
		var temp2 = null;
		len =0; //データ数を初期化
		len2 =0;

		// クッキーを探索
		var strKeyCheck = this.strCookieName + "=";
		for(var ix = 0; ix < maxCookieCount; ix++){
			// クッキーIDがキーの文字列で開始していた場合
			if (arrayCookie[ix].indexOf(strKeyCheck, 0) == 0) {
				temp = arrayCookie[ix].split("=");

				// 末尾の「;」があったら削除
				if ((temp[1].length -1) == temp[1].lastIndexOf(";")) {
				    temp[1] = temp[1].slice(0 , temp[1].length - 1);
				}

				// URLデコード、文字コード変換(UTF-8→EUC)
				temp[1]  = decodeURIComponent(temp[1]);

				// 区切り文字で分割し、目的地IDの配列に格納
				this.arrayId = temp[1].split(this.strDlm);
				len = this.arrayId.length;
            }
		}
		//クッキーテスト
		var strKeyCheck = "RouteEdit=";
		for(var ix = 0; ix < maxCookieCount; ix++){
			// クッキーIDがキーの文字列で開始していた場合
			if (arrayCookie[ix].indexOf(strKeyCheck, 0) == 0) {
				temp2 = arrayCookie[ix].split("=");

				// 末尾の「;」があったら削除
//				if ((temp2[1].length -1) == temp2[1].lastIndexOf(";")) {
//				    temp2[1] = temp2[1].slice(0 , temp2[1].length - 1);
//				}

				// URLデコード、文字コード変換(UTF-8→EUC)
				temp2[1]  = decodeURIComponent(temp2[1]);

				// 区切り文字で分割し、目的地IDの配列に格納
				this.arrayId2 = temp2[1].split(this.strDlm);
				len2 = this.arrayId2.length;
//				alert(len2);
            }
		}
	};

	// クッキー配列の書き出し
	ArrayRouteCookie.prototype.writeCookie = function() {

        // 有効期限: 12時間
        var dateExpire = new Date();
        dateExpire.setTime(dateExpire.getTime() + this.expireAfter);

        // クッキーデータを作成
        var strCookieData = this.arrayId.toString();

        // クッキーを設定
        // ArrayRouteCookie.setCookie(this.strCookieName, strCookieData, dateExpire);
        ArrayRouteCookie.setCookie(this.strCookieName, strCookieData, dateExpire, this.path, this.domain);
	};

	// IDの配列を取得
	ArrayRouteCookie.prototype.getArrayData = function() {
		return this.arrayId;
	};

	// クッキーを登録します
	//   クライアント機に保存されます
	//   これ以降にサーバにGET/POSTする時点でサーバに渡されます
	ArrayRouteCookie.setCookie = function(name, value, expire, path, domain) {
		var strSetting = name + "=" + encodeURIComponent(value) + ";expires=" + expire.toGMTString();
		if (path != null && trim(path) != "") {
			strSetting += '; path=' + path;
		}
		if (domain != null && trim(domain) != "") {
			strSetting += '; domain=' + domain;
		}
		document.cookie = strSetting;
	};

	// データの追加(末尾)
	//  strId: 追加するIDの値
	ArrayRouteCookie.prototype.appendData = function(strId) {
		// 最大格納数を越えたら先頭を除去
		if (this.maxCount > 0) {
			while (this.arrayId.length >= this.maxCount) {
				this.arrayId.shift();
			}
		}
		this.arrayId.push(strId)
	};

	// データの置換(全体)
	//  arrayId: 置換するIDの配列
	ArrayRouteCookie.prototype.replaceData = function(arrayId) {
		if (arrayId == null) {
			this.arrayId = new Array();
			return;
		}
		if (this.maxCount > 0) {
			if (arrayId.length > this.maxCount) {
				// 引数が最大格納数を越えていたら先頭から最大格納数を切り出す
				this.arrayId = arrayId.slice(0, this.maxCount);
			} else {
				this.arrayId = arrayId;
			}
		}
	};

	// データの削除
	//  strId: 削除するIDの値
	ArrayRouteCookie.prototype.deleteData = function(strId) {
		var ixMax = strId.length;
		for (var ix = ixMax - 1; ix >= 0; ix--) {
			if (this.arrayId[ix] == strId) {
				this.arrayId.splice(ix, 1);	// 対象の要素１個を置換対象を指定せずに置換(つまり削除)
			}
		}
	};

	// すべての目的地IDをダイアログに表示します
	ArrayRouteCookie.prototype.dump = function(strComment) {
		if (strComment == null) strComment = "";
		alert(strComment + ": " + this.arrayId.toString());
	};


	// --- 目的地をルートに追加するマップ画面に提供する関数 ---
	// 選択された目的地の登録No.をクッキーに追加する
	// クライアント側だけの処理である(サーバへの送信は行なわない)
	// パラメータ
	//   toroku:       目的地の登録No
	//   cookiePath:   URLのパスを指定する。
	//                 省略すると documentのパスになる。
	//   cookieDomain: ドメインを指定する。
	//                 省略するとサーバのホスト名になる。
	//                 セキュリティ上の理由で別ホストの指定は無視されるようである。
	//                 (ショッピングデータの上書等々の問題)
	function addToRoute(toroku, cookiePath, cookieDomain) {
		var cookieName = "RoutePoint";
		var arrayRoutePoint = null;
		// クッキー(目的地追加用)から目的地の登録No.の配列を取得
		arrayRoutePoint = new ArrayRouteCookie(cookieName);

		// ドメインを設定
		if (cookieDomain != null && trim(cookieDomain) != "") {
			arrayRoutePoint.setDomain(trim(cookieDomain));
		}

		// URLのパスを設定
		if (cookiePath != null && trim(cookiePath) != "") {
			arrayRoutePoint.setPath(trim(cookiePath));
		}

		// 既存のクッキーを読込み
		arrayRoutePoint.readCookie(cookieName);
		
		//最大登録数を超えていたら目的地を追加できないようにする
//		var text = "試験中：len=" + len + " : len2=" + len2;
//		alert(text);
		if (len2+len>=maxroute){
			alert("The number of maximum registration has been exceeded.");
			return;
		}

		// 同一目的地の登録No.のクッキーが既存であれば削除
		// (重複登録の回避)
		arrayRoutePoint.deleteData(toroku);

//		alert(toroku + " is added to the tour route.");

		// 目的地の登録No.をクッキーの末尾に追加
		arrayRoutePoint.appendData(toroku);

		// クッキー(目的地追加用)を更新
		arrayRoutePoint.writeCookie();
		alert('It added it to the route. ');
	};
