// --------------------------------------------------------------------
// Author  : mashimonator
// Create  : 2009/01/05
// Update  : 2009/01/14
// Description : Loadイベント時にID・PASSWORDの表示を自動で行う
//             : Submitイベント時にID・PASSWORDの保存を自動で行う
// --------------------------------------------------------------------

var rwAuthInfo = {

	//-----------------------------------------
	// 設定値
	//-----------------------------------------
	conf : {

		// Cookie保存用のキーワード
		kwordId : 'auth_id',
		kwordPass : 'auth_pass',

		// Cookie保存期間
		savePeriod : 120,

		// ログイン情報を自動保存･表示させたいinput要素につけたidを指定
		idElemKey : 'dnn_ctr370_Login_Login_DNN_txtUsername',
		passElemKey : 'dnn_ctr370_Login_Login_DNN_txtPassword'

	},

	//-----------------------------------------
	// Cookie書込み
	//-----------------------------------------
	CookieWrite : function(kword, kdata, kday) {

		if (!navigator.cookieEnabled) {
			return;
		}

		var sday = new Date();
		sday.setTime(sday.getTime() + (kday * 1000 * 60 * 60 * 24));

		var s2day = sday.toGMTString();
		document.cookie = kword + "=" + escape(kdata) + ";expires=" + s2day + ";path=/";

	},

	//-----------------------------------------
	// Cookie読込み
	//-----------------------------------------
	CookieRead : function(kword) {

		if (typeof(kword) == "undefined") {
			return "";
		}

		kword = kword + "=";

		var kdata = "";
		var scookie = document.cookie + ";";
		var start = scookie.indexOf(kword);

		if (start != -1) {
			end = scookie.indexOf(";", start);
			kdata = unescape(scookie.substring(start + kword.length, end));
		}

		return kdata;

	},

	//-----------------------------------------
	// ログイン情報の表示
	//-----------------------------------------
	getAuthInfo : function() {

		var myPass = rwAuthInfo.CookieRead(rwAuthInfo.conf.kwordPass);
		var obj1 = document.getElementsByTagName('input');

		for(i = 0; i < obj1.length; i++){
			if ( obj1[i].id == rwAuthInfo.conf.passElemKey && myPass != "" ) {
				obj1[i].value = myPass;
			}
		}

		var myId = rwAuthInfo.CookieRead(rwAuthInfo.conf.kwordId);
		var obj2 = document.getElementsByTagName('input');

		for(i = 0; i < obj2.length; i++){
			if ( obj2[i].id == rwAuthInfo.conf.idElemKey && myId != "" ) {
				obj2[i].value = myId;
			}
		}

	},

	//-----------------------------------------
	// ログイン情報の保存
	//-----------------------------------------
	saveAuthInfo : function() {

		var obj1 = document.getElementById(rwAuthInfo.conf.passElemKey);

		if ( obj1.value != null && obj1.value != '' ) {
			var myPass = escape(obj1.value);
			rwAuthInfo.CookieWrite(rwAuthInfo.conf.kwordPass, myPass, rwAuthInfo.conf.savePeriod);
		}

		var obj2 = document.getElementById(rwAuthInfo.conf.idElemKey);

		if ( obj2.value != null && obj2.value != '' ) {
			var myId = escape(obj2.value);
			rwAuthInfo.CookieWrite(rwAuthInfo.conf.kwordId, myId, rwAuthInfo.conf.savePeriod);
		}

	},

	addSubmitEvent : function() {
		for ( i=0; i<document.forms.length; i++ ) {
			var frm = document.forms[i];
			if (typeof window.addEventListener == 'function') {
				frm.addEventListener('submit', rwAuthInfo.saveAuthInfo, false);
			} else if (typeof window.attachEvent == 'object') {
				frm.attachEvent('onsubmit', rwAuthInfo.saveAuthInfo);
			}
		}
	},

	addLoadEvent : function() {
		if (typeof window.addEventListener == 'function') {
			window.addEventListener('load', rwAuthInfo.getAuthInfo, false);
			window.addEventListener('load', rwAuthInfo.addSubmitEvent, false);
		} else if (typeof window.attachEvent == 'object') {
			window.attachEvent('onload', rwAuthInfo.getAuthInfo);
			window.attachEvent('onload', rwAuthInfo.addSubmitEvent);
		}
	}

}

rwAuthInfo.addLoadEvent();


