// ================================================================
//  Cookie を使ったパラメータ引継ぎ cookie_param.js
//  2009/04/13 Var 1.1.0
// ================================================================

// ================================================================
// オファーページの URL

var cpStrUrl = 'http://www.benesse.co.jp/zemi/kocha/offer/index.shtml';


// ================================================================
// Window onLoad に関数を設定
if (window.attachEvent){
	window.attachEvent('onload', cpOnload);
} else {
	window.addEventListener('load', cpOnload, false);
}

function cpOnload() {
	// cpStrCourseID がグローバル変数として宣言されていれば Cookie の書き込みを実行
	if (window.cpStrCourseID == undefined) {
		// 処理しない
	} else {
		if (window.cpStrCourseID != "") {
			cpSetCookie('courseNewCk', cpStrCourseID);
		}
	}
}


// ================================================================
// Cookie の書き込み

function cpSetCookie(site_id_key, parent_site_id) {
	// 有効期限
	var intDay = 365;
	objExpDate = new Date();
	objExpDate.setTime(objExpDate.getTime()+(intDay*24*60*60*1000));
	strItem = "@" + site_id_key + "=" + escape(parent_site_id) + ";";
	strExpires = "expires="+objExpDate.toGMTString();

	// Cookie を書き込む
	//有効期限を指定する courseNewCkの場合は、有効期限無し（セッション単位）
	if(site_id_key == 'courseNewCk'){
		document.cookie = strItem + ';path=/';
	}else{
		document.cookie = strItem + strExpires + ';path=/';
	}
}


// ================================================================
// Cookie の読み取り

function cpGetCookie(strCookie){
	strCookie = "@" + strCookie + "=";
	strValue = '';
	strString = document.cookie + ";" ;
	intOffset = strString.indexOf(strCookie);
	if (intOffset != -1){
		intStart = intOffset + strCookie.length;
		intEnd   = strString.indexOf(";" , intStart);
		strValue = unescape(strString.substring(intStart,intEnd));
	}
	return strValue;
}


// ================================================================
// 生年月日入力フォームの値を Cookie に格納する

function cpSetCookieBd(strNameYear, strNameMonth, strNameDay) {
	var strYear, strMonth, strDay, strBd;

	// フォームから年月日を取得
	strYear  = document.getElementById(strNameYear).options[document.getElementById(strNameYear).selectedIndex].value;
	strMonth = document.getElementById(strNameMonth).options[document.getElementById(strNameMonth).selectedIndex].value;
	strDay   = document.getElementById(strNameDay).options[document.getElementById(strNameDay).selectedIndex].value;

	// 頭に 0 をつける
	if(strMonth < 10) { strMonth = "0" + strMonth; } 
	if(strDay   < 10) { strDay   = "0" + strDay; } 
	strBd = strYear + strMonth + strDay;

	// Cookie をセット
	cpSetCookie('bd', strBd);
}


// ================================================================
// Cookie を取得して URL を生成

function cpGetCookieParamUrl() {
	var strCourse;
	if (cpStrCourseID == '') {
		strCourse = encodeURI(cpGetCookie('courseNewCk'));
	} else {
		strCourse = encodeURI(cpStrCourseID);
	}
	var strFullUrl = cpStrUrl + '?course=' + strCourse + '&bd=' + cpGetCookie('bd');
	return(strFullUrl);
}


// ================================================================
// URL Query を引き継いで URL を生成

function cpGetParamUrl() {
	var strQuery
	strQuery = location.search;
	var strFullUrl = cpStrUrl + strQuery;
	return(strFullUrl);
}

