/**
*
* 2019/04/15
* anton
*	
*/
var lang = $("html[lang]").attr("lang")


/* Alert Messages */
var invalidPageNumber = (lang == "en" ? "Please Enter numeric characters" : "数値を入力してください。");
var totalPageExceeded = (lang == "en" ? "You've exceeded length of pages." : "合計ページを超えています。");
var undefinedSearch = (lang == "en" ? "Search cannot be performed. Please contact the developer" : "検索できません。\n開発者にお問い合わせ下さい。");
var curExchange = (lang == "en" ? "Obtaining..." : "取得中...");

var nowDate	= new Date();
var month	= nowDate.getMonth();
var year	= nowDate.getFullYear();

if(month < 9) {
	month = "0" + (month + 1);
} else {
	month++;
}

var nowStr = year + '/' + month + '/01';
//var nowStr = '2015/01/01';


$(function() {

    $(document).on('click', '#btnExportCommonHigh', function(){
        $('#type_search').val('export_common');
        $('#searchForm').submit();
    });
	// $('#btnExportCommonHigh').click(function() {
	// 	$('#type_search').val('export_common');
	// 	$('#searchForm').submit();
	// });

	$('#btnExportCommonLow').click(function() {
		$('#type_search').val('export_common');
		$('#searchForm').submit();
	});

	$('#btnHeaderExchangeCurrency').click(function() {

		var from	= $('#header_from_currency').val();
		var to		= $('#header_to_currency').val();

		// データの入れ替え
		$('#header_from_currency').val(to);
		$('#header_to_currency').val(from);
	});

	$('#btnHeaderExchange').click(function() {

		var params = {'from'					: $('#header_from_currency').val()
						, 'to'					: $('#header_to_currency').val()
						, 'amount'				: $('#header_amount').val()
						, 'datetime'			: $('#header_datetime').val()
						, 'exchange_fee_id'		: $('#exchange_fee_id').val()
					 };
		$('#original_amount').text(curExchange);
		$('#publish_amount').text(curExchange);

		postAction('../../api/Exchange.php', params)
		.done(function(data) {
			$('#original_amount').text(data.original_amount.toString());
			$('#publish_amount').text(data.publish_amount.toString());
		})
		.fail(function(data) {
			console.log(data);
		});
	});

	/**
	 * 
	 * 2019/04/12
	 * anton
	 * 
	 */
	$(".lang-unit").click(function(e){
		e.preventDefault()
		
		var mobj = $("html[lang]")
		
		var slang = mobj.attr("lang")
		var alang = $(this).attr("lang");
		
		if(slang == alang && !mobj.hasClass("translated-ltr"))
			return;
		
		if($(".lang-trans-data").length > 0){
			if($("#lang_data_id").length > 0 && $("#lang_data_value").length > 0){
				var idn = $("#lang_data_id").val();
				var idv = $("#lang_data_value").val();
				$(idn).val(idv);
			}
			
			if($("#lang_data_type").length > 0){
				var idt = $("#lang_data_type").val();
				$("#type").val(idt);
			}
			
			var ida = $("#lang_data_action").val();
			var idf = $("#lang_data_form").val();
			
			$(idf).attr("action", ida);
			$(idf).submit()
		}else{
			var strArray = window.location.href.split("/");
			var lastRoute = strArray[strArray.length - 1];
			window.location = "/"+(alang == "en" ? alang+"/" : "")+lastRoute;
		}
	})
	
	/**
	 * 
	 * 2019/04/12
	 * anton
	 *
	 */
	$(".lang-unit").each(function(i, e){
		var slang = $("html[lang]").attr("lang")
		var alang = $(this).attr("lang")
		
		if(slang == alang)
			$(this).addClass("blink")
	})
	
	//
	$('#header_datetime').datetimepicker({

		format	: 'Y-m-d H'
//		, inline	: true
		, lang		: 'ja'
	});

	$('#btnPageBackNav').click(function() {
		$('#navForm').attr('action', $('#nav_action').val());
		$('#navForm').submit();

	});

});

///**
// * サブミットを開始する(Form用)
// */
function submitForm(aId, act) {
	if(aId == undefined
		|| aId == '') {
		$('#acForm').submit();
	} else {
		if(act == undefined)
			act = '';
		
		$('#' + aId).attr('action',act);
		$('#' + aId).submit();
	}
}

///**
//* formを取得する
//*/
function getForm(aId) {
	if(aId == undefined
		|| aId == '') {
		return $('#acForm');
	} else {
		return $('#' + aId);
	}movePage
}

///**
//* ページ移動
//*/
function movePage(page) {
	if($('#searchForm') == undefined) {
		alert(undefinedSearch);
	} else {
		$('#type_search').val('search');
		if($('#s_type').val() != null){
            $('#s_type').val('');
        } else{
            $('#type').val('');
        }
		
		if(!window.location.href.includes("?")){
			adjustTargetPage('?', page);
		}else{
			adjustTargetPage('&', page);
		}
		
		$('#searchForm').submit();
		/*$('#acForm').attr('action','?target_page=' + page);
		$('#acForm').submit();*/
	}
}

function adjustTargetPage(prefix, page){
	var mainurl = window.location.href;
	
	if(!mainurl.includes('target_page')){
		$('#searchForm').attr('action', mainurl + prefix + 'target_page=' + page);
	}else{
		var act = mainurl.replace(/target_page=[0-9]+/g, 'target_page=' + page)
		$('#searchForm').attr('action', act);
	}
}

$(function() {
    $(document).on('click', '#btnMovePageHigh', function(){
        movePageHigh();
    });
	// $('#btnMovePageHigh').click(function() {
	// 	movePageHigh();
	// });

	$('#btnMovePageLow').click(function() {
		movePageLow();
	});
});

function movePageHigh() {

	var curPage		= $('#move_page_high').val();
	var totalPage	= $('#total_page').val();

	if(!curPage.match(/[^0-9]/g)) {

		curPage		= parseInt(curPage);
		totalPage	= parseInt(totalPage);

		if(curPage > totalPage) {
			alert(totalPageExceeded);
			$('#move_page').val(totalPage);
		} else {
			movePage(curPage);
		}

	} else {
		alert(invalidPageNumber);
	}
}

function movePageLow() {

	var curPage		= $('#move_page_low').val();
	var totalPage	= $('#total_page').val();

	if(!curPage.match(/[^0-9]/g)) {

		curPage		= parseInt(curPage);
		totalPage	= parseInt(totalPage);

		if(curPage > totalPage) {
			alert(totalPageExceeded);
			$('#move_page').val(totalPage);
		} else {
			movePage(curPage);
		}

	} else {
		alert(invalidPageNumber);
	}
}

/**
 * ユーザ詳細
 */
function userDetail(uAccount, target) {
	$('#acForm').attr('action','account_edit');
	$('#detail_account').val(uAccount);
	$('#action').val('account_list');
	$('#type').val('action');

	// ターゲットがない場合
	if(target != undefined) {
		getForm().attr('target', target);
	} else {
		getForm().attr('target', '');
	}

	submitForm();

	getForm().attr("target","_blank");
}

$(function() {
	$("#btnLogout").click(function() {
		location.href = 'logout';
	});
});

function postAction(target, params) {

	// 変数宣言部
	var cnt		= 0;
	var result	= null;

	return $.ajax({
		url:		target
		, type:		'post'
		, dataType:	'json'
		, data:		params
	});
}

$.fn.inputFilter = function(inputFilter) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        this.value = "";
      }
    });
};