if(!dpss) var dpss = {};

dpss.TopPage = {
	kaiin_id: "",
	url: "login.php",
	topurl: "web/php/webtop.php",
	xotree: new XML.ObjTree(),
	cookie_name: "save_kaiin_id",
	Ctrl: {},
	Doc: {
		AID:			"999999",
		ap:				"",
		func:			"init",
		status:			"",
		course_list:	new Array()
	},
	Req: {
		AID: 			"999999",
		ap:				"webtop",
		func: 			"init"
	},
	main: function() {
		this.id1 = $("id1");
		this.id2 = $("id2");
		this.id3 = $("id3");
		this.msg = $("msg");
		this.frm = $("frm");
		//#02-->
		this.save_id = $("chk_save");
		this.btn_login = $("btn_login");
		Event.observe(this.id1, "keyup", this.nextFocus.bindAsEventListener(this, this.id1, this.id2), false);
		Event.observe(this.id2, "keyup", this.nextFocus.bindAsEventListener(this, this.id2, this.id3), false);
		Event.observe(this.id3, "keyup", this.nextFocus.bindAsEventListener(this, this.id3, this.btn_login), false);
		var save_kaiin_id = dpss.common.getCookie(this.cookie_name);
		var ary = save_kaiin_id.split("/");
		var chk_save = "";
		if(ary.length > 0) chk_save = ary[0];
		if(chk_save == "1" && ary.length > 1) this.kaiin_id = ary[1];
		//#04-->	2009/09/01
		var param = window.location.search.substring(1, window.location.search.length);
		var ary = param.split('&');
		for(var i = 0; i < ary.length; i++) {
			var a = ary[i].split('=');
			if(a.length == 2 && a[0] == "no") {
				this.kaiin_id = a[1];
				break;
			}
		}
		if(this.kaiin_id != "") {
			this.id1.value = this.kaiin_id.substr(0, 4);
			this.id2.value = this.kaiin_id.substr(4, 4);
			this.id3.value = this.kaiin_id.substr(8, 4);
		} else {
			this.id1.value = "";
			this.id2.value = "";
			this.id3.value = "";
		}
		this.save_id.checked = (chk_save == "1") ? true : false;
		//#04<--
		//#02<--
		Event.observe($("btn_login"), 		"click", this.login.bindAsEventListener(this), false);
		Event.observe($("search_course"), 	"click", this.searchCourse.bindAsEventListener(this), false);
		var param 	= this.getReqParam();
		dpss.common.ajaxPostData(this.topurl, param, this.initForm.bindAsEventListener(this));		
	},
	nextFocus: function(e, prev, next) {	//#02
		if(prev.value.length == 4) next.focus();
	},
	initForm: function(request) {
		var responseData = this.getResData(request);
		if(this.Doc.status != "0") {
			wcommon.showRequestError();
			return;
		}
		this.Ctrl.course1 = $("course1");
		this.Ctrl.course2 = $("course2");
		this.Ctrl.course3 = $("course3");
		this.Ctrl.course4 = $("course4");
		this.Ctrl.course5 = $("course5");
		this.Ctrl.course6 = $("course6");
		this.Ctrl.course7 = $("course7");
		this.Ctrl.course8 = $("course8");
		//新着ドライブコースピックアップ
		this.Doc.course_list = new Array();
		if(responseData.dpss.ccnt == 1) this.Doc.course_list[0] = responseData.dpss.course_list;
		if(responseData.dpss.ccnt > 1) this.Doc.course_list = responseData.dpss.course_list;
		dpss.common.ajaxPostData('./web/php/top_course.php', '', this.showTopCourse.bindAsEventListener(this));	//#03	2009/08/05
		this.showForm();
	},
	showForm: function() {
		var html;
		for(var i = 0; i < this.Doc.course_list.length; i++) {
			//#03-->	2009/08/05
			if(this.Doc.course_list[i].course_id) {
				html = "<dl>";
				html += "<dt>";
				html += "<a href=\"javascript:dpss.TopPage.reqDetail('" + this.Doc.course_list[i].course_id + "');\">";
				if(parseInt(this.Doc.course_list[i].photo_id1) > 0) {
					html += "<img src=\"" + this.Doc.course_list[i].photo_url1 + "\" alt=\"" + this.Doc.course_list[i].photo_title_1 + "\"";
					html += " width=\"" + this.Doc.course_list[i].photo_width1 + "\" height=\"" + this.Doc.course_list[i].photo_height1 + "\" />";
				} else if(parseInt(this.Doc.course_list[i].photo_id2) > 0) {
					html += "<img src=\"" + this.Doc.course_list[i].photo_url2 + "\" alt=\"" + this.Doc.course_list[i].photo_title_2 + "\"";
					html += " width=\"" + this.Doc.course_list[i].photo_width2 + "\" height=\"" + this.Doc.course_list[i].photo_height2 + "\" />";
				}
				html += "</a>";
				html += "</dt>";
				html += "<dd>";
				html += "<a href=\"javascript:dpss.TopPage.reqDetail('" + this.Doc.course_list[i].course_id + "');\">";
				html += "<div class=\"coursearea\">【"+ this.Doc.course_list[i].region_name + "】" + this.Doc.course_list[i].update_date + "更新</div>";
				html += "</a>";
				html += "<div class=\"coursename\">";
				if(parseInt(this.Doc.course_list[i].theme_id) > 0) {
					html += "<a href=\"javascript:dpss.TopPage.reqDetail('" + this.Doc.course_list[i].course_id + "');\">";
					html += "<img src=\"" + this.Doc.course_list[i].theme_icon_file + "\" alt=\"" + this.Doc.course_list[i].theme_name + "\" />";
					html += "</a>";
				}
				html += "<a href=\"javascript:dpss.TopPage.reqDetail('" + this.Doc.course_list[i].course_id + "');\">";
				html += "<span>" + this.Doc.course_list[i].course_name + "</span>";
				html += "</a>";
				html += "</div>";
				html += "</dd>";
				html += "</dl>";
			}
			//#03<--	2009/08/05
			switch(i + 1) {		//#3	2009/08/05
				case 1:
					this.Ctrl.course1.innerHTML = html;
					break;
				case 2:
					this.Ctrl.course2.innerHTML = html;
					break;
				case 3:
					this.Ctrl.course3.innerHTML = html;
					break;
				case 4:
					this.Ctrl.course4.innerHTML = html;
					break;
				case 5:
					this.Ctrl.course5.innerHTML = html;
					break;
				case 6:
					this.Ctrl.course6.innerHTML = html;
					break;
				case 7:
					this.Ctrl.course7.innerHTML = html;
					break;
				case 8:
					this.Ctrl.course8.innerHTML = html;
					break;
				default:
			}
		}
			
	},
	showTopCourse: function(request) {
		var html = request.responseText;
		if(html == "") return;
		$('top_course').innerHTML = html;
		$('top_course_box').show();
	},
	reqDetail: function(course_id) {
		$("func").value = "init";
		$("course_id").value = course_id;
		$("ap").value = "course_detail";
		this.frm.submit();
		return;
	},
	login: function() {
		if(!this.check()) {
			return false;
		}
		var url = "login.php";
		//#02-->
		this.kaiin_id = this.id1.value + this.id2.value + this.id3.value;
		var pars = "uid=" + this.kaiin_id;
		//#02<--
		pars += "&utype=9";
		dpss.common.ajaxPostData(url, pars, this.logon.bindAsEventListener(this));
	},

	logon: function(request) {
		var responseData = this.xotree.parseXML(request.responseText);
		if(responseData.dpss.status != "0") {
			//#04-->	2009/09/01
			if(responseData.dpss.status == "1") {
				this.msg.innerHTML = "<div style=\"color:red;\">有効期限が切れています。</div>";
			} else if(responseData.dpss.status == "2") {
				this.msg.innerHTML = "<div style=\"color:red;\">ログイン情報に誤りがあります。</div>";
			} else {
				this.msg.innerHTML = "<div style=\"color:red;\">システム不安定です。<br />しばらくお待ちください。</div>";
			}
			//#04<--
			return false;
		}
		//#02-->
		if(this.save_id.checked) {
			var save_kaiin_id = "1/" + this.kaiin_id;
			dpss.common.setCookie(this.cookie_name, save_kaiin_id, 180);
		} else {
			dpss.common.clearCookie(this.cookie_name);
		}
		//#02<--
		$("AID").value = responseData.dpss.AID;
		$("ap").value = responseData.dpss.ap;
		this.frm.submit();
	},

	check: function() {
		if(this.id1.value.length != 4 || !this.id1.value.match(/\d{4}/)) {
			return this.showMsg(this.id1);
		}
		if(this.id2.value.length != 4 || !this.id2.value.match(/\d{4}/)) {
			return this.showMsg(this.id2);
		}
		if(this.id3.value.length != 4 || !this.id3.value.match(/\d{4}/)) {
			return this.showMsg(this.id3);
		}
		return true;
	},
	searchCourse: function() {
		$("AID").value = this.Doc.AID;
		$("ap").value = "search_course";
		this.frm.submit();
	},
	showMsg: function(input) {
		this.msg.innerHTML = "<div style=\"color:red;\">会員番号は１２桁の数字です。</div>";
		input.focus();
		return false;
	},
	getReqParam: function() {
		this.Req.AID = this.Doc.AID;
		var reqDoc = {dpss: {request: this.Req}};
		var param = this.xotree.writeXML(reqDoc);
		return param;
	},
	getResData: function(request) {
		var responseData = this.xotree.parseXML(request.responseText);
		this.Doc.status 	= responseData.dpss.status;
		return responseData;
	}
	
};

Event.observe(window, "load", function() {
	dpss.TopPage.main();
}, false);


