// 初期処理呼び出し
document.observe("dom:loaded", function(){initialize()});

var objWin = [];

// AJAX
function ajax_request(url, pane_name, async)
{
	if(arguments.length == 2) async = true;

	new Ajax.Request
	(
		url,
		{
			asynchronous: async,
			method: "get",
			onLoading: loading($(pane_name)),
			onFailure: function(httpObj){failure(httpObj, $(pane_name));},
			onSuccess: function(httpObj){complete(httpObj, $(pane_name));}
		}
	);
}
function loading(objElement)
{
	objElement.innerHTML = '<div class="area"><center>Now Loading ...<br><img src="/img/indicator_loading.gif"></center></div>';
}
function failure(httpObj, objElement)
{
	objElement.innerHTML = '<div class="area"><center>通信エラーで読み込みに失敗しました。再度お試しください。</center></div>';
}
function complete(httpObj, objElement)
{
	res = httpObj.responseText.split('----- k-c-b -----');

	switch(res[0])
	{
		case "error":
			switch(res[1])
			{
				case "timeout":
					new Ajax.Request
					(
						"ajax.php?m=logout",
						{
							method: "get",
							onSuccess: function(httpObj){location.href = "/";}
						}
					);
	
					alert("セッションが途切れました。再ログインしてください");
					break;
			}
			break;
		case "rewrite":
			ajax_request("/pane/" + res[1], objElement.id);
			//jump_menu(res[1]);
			break;
		default:
			objElement.innerHTML = res[1];
	}
}

// メニュー移動
function jump_menu()
{
	m = "";
	a = "";
	c1 = "";
	c2 = "";

	switch(arguments.length)
	{
		case 4:
			c2 = arguments[3];
		case 3:
			c1 = arguments[2];
		case 2:
			a = arguments[1];
		case 1:
			m = arguments[0];
	}

	scroll(0, 0);
	ajax_request("/pane/" + m + ".php?m=" + m + "&a=" + a + "&c1=" + c1 + "&c2=" + c2, "pane_right");

	void(0);
}

// ページ移動
function move_page(mode, page, cond_id, pane)
{
	scroll(0, 0);
	if(cond_id)
		ajax_request("/pane/" + mode + ".php?p=" + page + "&" + Form.serialize(cond_id), pane);
	else
		ajax_request("/pane/" + mode + ".php?p=" + page, pane);
}

// ウィンドウを開く
function window_open(params, name, width, height)
{
	if(objWin[name] && objWin[name].closed == false)
	{
		objWin[name].location.href = "window.php?p=" + params;
	}
	else
	{
		objWin[name] = window.open("window.php?p=" + params, name, 
				"width=" + width + ",height=" + height + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
	}
	objWin[name].focus();
}

// 入力フォームの送信
function form_submit(form_id, pane_name)
{
	if(arguments.length == 1) pane_name = "pane_right";

	url = $(form_id).action;

	if(entry_check(form_id))
	{
		param = Form.serialize(form_id);
		new Ajax.Request
		(
			url,
			{
				method: "post",
				parameters: param,
				onLoading: loading($(pane_name)),
				onFailure: function(httpObj){failure(httpObj, $(pane_name));},
				onSuccess: function(httpObj){complete(httpObj, $(pane_name));}
			}
		);
	}

	return false;
}

// 入力フォームの送信（逆ナンパ）
function form_submit2(form_id, pane_name)
{
	if(arguments.length == 1) pane_name = "pane_right";

	url = $(form_id).action;

	if(entry_check2(form_id))
	{
		param = Form.serialize(form_id);
		new Ajax.Request
		(
			url,
			{
				method: "post",
				parameters: param,
				onLoading: loading($(pane_name)),
				onFailure: function(httpObj){failure(httpObj, $(pane_name));},
				onSuccess: function(httpObj){complete(httpObj, $(pane_name));}
			}
		);
	}

	return false;
}

// プロフィールの送信
function prof_submit(form_id)
{
	url = $(form_id).action;

	if(entry_check(form_id))
	{
		param = Form.serialize(form_id);
		new Ajax.Request
		(
			url,
			{
				method: "post",
				parameters: param,
				onLoading: loading($('pane_right')),
				onFailure: function(httpObj){failure(httpObj, $('pane_right'));},
				onSuccess: function(httpObj)
				{
					complete(httpObj, $('pane_right'));
					menu_refresh(document);
				}
			}
		);
	}

	return false;
}

// 必須入力項目確認
function entry_check(form_id)
{
	var errObj;

	$$("#" + form_id + " .necessary").each
	(
		function(obj)
		{
			if(!obj.value && !errObj) errObj = obj;
		}
	);
	if(errObj)
	{
		alert("必須項目が入力されていません");
		errObj.focus();
		return false;
	}

	$$(".hankaku").each
	(
		function(obj)
		{
			if(obj.value.match(/[^0-9A-Za-z]/) && !errObj) errObj = obj;
		}
	);
	if(errObj)
	{
		alert("半角英数以外が使用されています");
		errObj.focus();
		return false;
	}

	$$(".numeric").each
	(
		function(obj)
		{
			if(obj.value.match(/[^0-9]/) && !errObj) errObj = obj;
		}
	);
	if(errObj)
	{
		alert("半角数字以外が使用されています");
		errObj.focus();
		return false;
	}

	$$(".mailaddr").each
	(
		function(obj)
		{
			if(obj.value.match(/[^0-9A-Za-z\-\.@]/) && !errObj) errObj = obj;
		}
	);
	if(errObj)
	{
		alert("メールアドレスに使用できない文字（例えば全角文字など）が使用されています");
		errObj.focus();
		return false;
	}

	return true;
}

// ログイン処理
function login()
{
	new Ajax.Request
	(
		"ajax.php?m=login&i=" + $('login_id').value + "&p=" + $('login_pw').value,
		{
			method: "get",
			onLoading: function()
			{
				$("login_id").disabled = true;
				$("login_pw").disabled = true;
			},
			onFailure: function(httpObj)
			{
				alert("error");
			},
			onSuccess: function(httpObj)
			{
				if(httpObj.responseText == "success")
				{
					location.href = "/";
				}
				else if(httpObj.responseText == "leave")
				{
					if(confirm("あなたは退会済みです。\n復帰処理を行いますか？"))
					{
						if(confirm("本当に復帰しますか？"))
						{
							new Ajax.Request
							(
								"ajax.php?m=comeback&member_id=" + $('login_id').value,
								{
									method: "get",
									onSuccess: function(httpObj)
									{
										alert('復帰処理が完了しました。\nログインします');
										login();
									}
								}
							);
						}
					}
				}
				else if(httpObj.responseText == "black")
				{
					alert("お客様は何らかの理由により利用停止にされております");
					$("login_id").disabled = false;
					$("login_pw").disabled = false;
					$("login_id").focus();
				}
				else
				{
					alert("ログインＩＤまたはパスワードが違います");
					$("login_id").disabled = false;
					$("login_pw").disabled = false;
					$("login_id").focus();
				}
			}
		}
	);

	return false;
}

// ログアウト処理
function logout()
{
	if(confirm("ログアウトしますか？"))
	{
		new Ajax.Request
		(
			"ajax.php?m=logout",
			{
				method: "get",
				onSuccess: function(httpObj){location.href = "/";}
			}
		);
	}
}

// DIVの開閉（トグル）
function toggle(div_id)
{
	$(div_id).toggle();
}

// 地区を選択すると地域を再登録する
function change_district(district_id, area)
{
	objElement = $(area);

	while((objElement.options.length > 0) || (objElement.options[0] != null))
		objElement.options[0] = null;

	objElement.options[0] = new Option("問わない", "");

	new Ajax.Request
	(
		"ajax.php?m=area&district_id=" + district_id,
		{
			method: "get",
			onFailure: function(httpObj)
			{
				return;
			},
			onSuccess: function(httpObj)
			{
				list = httpObj.responseText.split("\n");
				len = list.length;
				for(i = 0; i < len; i++)
				{
					sub = list[i].split(":");
					if(sub.length == 2) objElement.options[i + 1] = new Option(sub[1], sub[0]);
				} 
			}
		}
	);
}

// チェックボックスの排他処理
function ex_check(check)
{
	if(check.checked)
	{
		len = arguments.length;
		for(i = 1; i < len; i++)
		{
			if(check.id != arguments[i]) $(arguments[i]).checked = false;
		}
	}
}

// ポイントチェック（今から支払うポイントの残高があるか？）
function point_check(member_id, point)
{
	var ret = true;

	new Ajax.Request
	(
		"ajax.php?m=point_check&member_id=" + member_id,
		{
			method: "get",
			asynchronous: false,
			onSuccess: function(httpObj)
			{
				if(parseInt(httpObj.responseText) < point)
				{
					alert("あなたのポイントは現在「" + httpObj.responseText + "pt」です。\n" +
							"この操作には「" + point + "pt」が必要になりますので、ポイントをご購入の上、\n" +
							"再度操作をお願いします。");
					ret = false;
				}
			}
		}
	);

	return ret;
}

// 文字数表示
function disp_msg_count(objComment, element_id, max_count)
{
	if(objComment.value.length > max_count)
	{
		color = "#FF0000";
	}
	else
	{
		color = "#0000FF";
	}
	$(element_id).innerHTML = '<font color="' + color + '">' + objComment.value.length + ' / ' + max_count + '</font>';
}

// メールの削除
function mail_delete(mail_id, status, target_id, page)
{
	if(!confirm("このメールを削除しますか？")) return false;

	var objElement = $('mail' + mail_id);

	new Ajax.Request
	(
		"ajax.php?m=delete_mail&mail_id=" + mail_id + "&status=" + status + "&page=" + page,
		{
			method: "get",
			onFailure: function(httpObj){failure(httpObj, objElement);},
			onSuccess: function(httpObj)
			{
				ajax_request("/pane/detail.php?a=list&target_id=" + target_id + "&p=" + page, "history_list");
			}
		}
	);
}

// メール送信前チェック
function mail_check(form_id, member_id, point, sex_id, target_svip, pickup)
{
	if(entry_check(form_id))
	{
		if($('comment').value.length > 60 && !pickup)
		{
			alert('文字数が60文字を超えています\n改行は２文字になります ');
		}
		else if(target_svip && !$('am').checked && !$('at').checked && !pickup)
		{
			alert('スーパーVIPへのメール送信には、電話番号またはメールアドレスの添付が必須です');
		}
		else
		{
			attach = 0;
			if($('am').type == "checkbox")
				if($('am').checked) attach++;
			if($('at').type == "checkbox")
				if($('at').checked) attach++;

			if(attach > 0 && sex_id == 1 && !target_svip) point += 120;
			if(pickup) point = 0;

			if(point_check(member_id, point))
			{
				new Ajax.Request
				(
					"/pane/detail.php",
					{
						method: "post",
						parameters: Form.serialize(form_id),
						onLoading: loading($("mailarea")),
						onSuccess: function(httpObj)
						{
							$("mailarea").innerHTML = httpObj.responseText;
						}
					}
				);
			}
		}
	}

	return false;
}

// メール送信前チェック
function mail_check2(form_id, member_id, point, sex_id, target_svip)
{
	if(entry_check(form_id))
	{
		if(point_check(member_id, point))
		{
			new Ajax.Request
			(
				"/pane/detail.php",
				{
					method: "post",
					parameters: Form.serialize(form_id),
					onLoading: loading($("pmailarea")),
					onSuccess: function(httpObj)
					{
						$("pmailarea").innerHTML = httpObj.responseText;
					}
				}
			);
		}
	}

	return false;
}

// メール関連用イベント
function mail_submit(form_id)
{
	new Ajax.Request
	(
		"/pane/detail.php",
		{
			method: "post",
			parameters: Form.serialize(form_id),
			onLoading: loading($("mailarea")),
			onFailure: function(httpObj){failure($("mailarea"));},
			onSuccess: function(httpObj)
			{
				$("mailarea").innerHTML = httpObj.responseText;
			}
		}
	);

	return false;
}

// メール関連用イベント
function mail_submit2(form_id)
{
	new Ajax.Request
	(
		"/pane/detail.php",
		{
			method: "post",
			parameters: Form.serialize(form_id),
			onLoading: loading($("pmailarea")),
			onFailure: function(httpObj){failure($("pmailarea"));},
			onSuccess: function(httpObj)
			{
				$("pmailarea").innerHTML = httpObj.responseText;
			}
		}
	);

	return false;
}

// メール送信処理
function mail_send(form_id, member_id, point, target_id)
{

	if(point_check(member_id, point))
	{
		new Ajax.Request
		(
			"/pane/detail.php",
			{
				asynchronous: false,
				method: "post",
				parameters: Form.serialize(form_id),
				onFailure: function(httpObj){failure(httpObj, $("mailarea"));},
				onSuccess: function(httpObj)
				{
					$("mailarea").innerHTML = httpObj.responseText;
				}
			}
		);

		ajax_request("/pane/detail.php?a=list&target_id=" + target_id, "history_list");		

		new Ajax.Request
		(
			"ajax.php?m=point_sub&member_id=" + member_id + "&point=" + point,
			{
				method: "get",
				onSuccess: function(httpObj)
				{
					menu_refresh();
				}
			}
		);
	}

	return false;
}

// メール送信処理
function mail_send2(form_id, member_id, point, target_id)
{

	if(point_check(member_id, point))
	{
		new Ajax.Request
		(
			"/pane/detail.php",
			{
				asynchronous: false,
				method: "post",
				parameters: Form.serialize(form_id),
				onFailure: function(httpObj){failure(httpObj, $("pmailarea"));},
				onSuccess: function(httpObj)
				{
					$("pmailarea").innerHTML = httpObj.responseText;
				}
			}
		);

		ajax_request("/pane/detail.php?a=list&target_id=" + target_id, "history_list");		

		new Ajax.Request
		(
			"ajax.php?m=point_sub&member_id=" + member_id + "&point=" + point,
			{
				method: "get",
				onSuccess: function(httpObj)
				{
					menu_refresh();
				}
			}
		);
	}

	return false;
}

// メール開封処理
function mail_open(mail_id, target_id, element_id, page)
{
	ajax_request("/pane/detail.php?a=mail_open&mail_id=" + mail_id + "&target_id=" + target_id + "&page=" + page, element_id, false);

	objElement = opener.document.getElementById('pane_left');
	new Ajax.Request
	(
		"/pane/menu.php",
		{
			method: "get",
			onLoading: loading(objElement),
			onFailure: function(httpObj){failure(httpObj, objElement);},
			onSuccess: function(httpObj){complete(httpObj, objElement);}
		}
	);
}

// ポイント加算処理
function point_send(form_id)
{
	url = $(form_id).action;
	param = Form.serialize(form_id);

	new Ajax.Request
	(
		url,
		{
			asynchronous: false,
			method: "post",
			parameters: param,
			onFailure: function(httpObj){failure(httpObj, $("pane_window"));},
			onSuccess: function(httpObj)
			{
				if(httpObj.responseText == "success")
				{
					ajax_request(url + "?m=complete", "pane_window");		
				}
				else
				{
					ajax_request(url + "?m=error", "pane_window");
				}
			}
		}
	);

	menu_refresh();

	return false;
}

function menu_refresh(obj)
{
	//if(obj == 'undefined') obj = opener.document;
	if(arguments.length == 0) obj = opener.document;

	objElement = obj.getElementById('pane_left');
	new Ajax.Request
	(
		"/pane/menu.php",
		{
			method: "get",
			asynchronous: false,
			onLoading: loading(objElement),
			onFailure: function(httpObj){failure(httpObj, objElement);},
			onSuccess: function(httpObj){complete(httpObj, objElement);}
		}
	);
}

function leave()
{
	ajax_request("/pane/leave.php?m=complete", "pane_right", false);
	ajax_request("/pane/menu.php", "pane_left");
}

function send_url(element_id)
{
	if($(element_id).value.match(/[^0-9A-Za-z\-\.@]/) || !$(element_id).value)
	{
		alert('送り先メールアドレスを正しく入力してください');
		$(element_id).focus();
	}
	else
	{
		new Ajax.Request
		(
			"ajax.php?m=send_url&mail=" + $(element_id).value,
			{
				method: "get",
				onSuccess: function(httpObj)
				{
					alert('送信しました');
				}
			}
		);
	}
	
}