function submitGetProfile(hojinBango) {
	if ( document.getElementById("return_screen") != null ) {
		$('<form/>', {id: 'getprofile', action: './ichiran'})
		.append($('<input/>', {type: 'hidden', name: 'hojinBango', value: hojinBango}))
		.appendTo(document.body)
		.submit();
	}
}

function submitProfile(hojinBango) {
	returnScreenSubmit("./ichiran?hojinBango=" + hojinBango, "_self", "post", hojinBango);
}

function submitCsvDownload(hojinBango) {
	if (hojinBango != null && hojinBango != "") {
		$("#prev").val("profile");
	} else {
		$("#prev").val("serch");
	}
	returnScreenSubmit("./CsvDownload", "_self", "post", hojinBango);
}

function submitXmlDownload(hojinBango) {
	$("#prev").val("profile");
	returnScreenSubmit("./XmlDownload", "_self", "post", hojinBango);
}

function returnScreenSubmit(action, target, method, hojinBango) {
	$("#return_screen").attr('action', action);
	$("#return_screen").attr('target', target);
	$("#return_screen").attr('method', method);
	
	if (hojinBango != null && hojinBango != "") {
		$("#profileNumber").val(hojinBango);
	}

	$("#return_screen").submit();
}

$(function(){
	$("#current_window").text($('#h1_text').val());
	$("#content-top").text($('#h1_text').val());
	$('#return_screen').submit(function(event) {
		if ($('#return_screen').attr('action')
				.indexOf('./ichiran?hojinBango=') < 0) {
			// 法人プロフィールへ遷移する場合以外に以下を実行

			// HTMLでの送信をキャンセル
			event.preventDefault();
			ajaxSubmit();
		}
		
	});
});

function ajaxSubmit() {
	// 操作対象のフォーム要素を取得
	var $form = $('#return_screen');
	
	// 送信
	$.ajax({
		url: $form.attr('action'),
		type: $form.attr('method'),
		data: $form.serialize(),
		datatype: 'html',
		timeout: 180000,  // 単位はミリ秒
		
		// 送信前
		beforeSend: function(xhr, settings) {
			history.pushState(null, null, "TopPage#");
			$.blockUI({ message: '<img src="./common/images/gif-load.gif" style="vertical-align:middle;"> 検索中です...' });
		},
		// 応答後
		complete: function(xhr, textStatus) {
			$.unblockUI();
		},
		
		// 通信成功時の処理
		success: function(result, textStatus, xhr) {
			document.open();
			document.write(result);
			document.close();
		},
		
		// 通信失敗時の処理
		error: function(xhr, textStatus, error) {
			alert("検索時間が一定時間を超過しました。\n検索条件を絞り込んで再度検索ください。");
		}
	});
}
