
var selectId = "li05";

function changecur(){
	var cur=$("#currency").children('option:selected').val();
	if(cur=="JPY"){
		$("#li01").html("1000");
		$("#li02").html("3000");
		$("#li03").html("5000");
		$("#li04").html("10000");
		$("#li05").html("30000");
		for(i=1;i<=5;i++){
			$("#li0"+i).removeClass('selected');
		}
		$("#"+selectId).addClass('selected');
		$("#amount").val($("#"+selectId).html());
	}else{
		$("#li01").html("10");
		$("#li02").html("30");
		$("#li03").html("50");
		$("#li04").html("100");
		$("#li05").html("300");
		for(i=1;i<=5;i++){
			$("#li0"+i).removeClass('selected');
		}
		$("#"+selectId).addClass('selected');
		$("#amount").val($("#"+selectId).html());
	}
}

function setAmount(){
	$("#amount").val($("#depositmoney").val());
}

function clickothers(){
	$("#amount").val('');
	if($("#damount").css("display")=="none"){
		$("#damount").removeClass("hidediv");
	}
}

function clicknum(id){
	$("#amount").val($(id).html());
 selectId = $(id).attr('id');
	$("#damount").addClass("hidediv");
}

function checkTerms(lang){
	var check = $("#terms").is(':checked');
	if(check != true){
		if(lang == 'en'){
			alert('Please review the terms and conditions and check the box to proceed with your request.');
		}else if(lang == 'ja'){
			alert('Please review the terms and conditions and check the box to proceed with your request.');
		}else if(lang == 'sc'){
			alert('若要继续，请详阅合约条款并勾选此框');
		}else if(lang == 'tc'){
			alert('若要繼續，請詳閱合約條款並勾選此框');
		}
		return false;
	}else{
		$('#type').val('complete');
		submitForm();
	}
}

function randomStr(length) {
    var result           = [];
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
      result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
   }
   return result.join('');
}

$(function() {
	$("#depositmoney").inputFilter(function(value) {
		  return /^-?\d*$/.test(value); });
	
	// $.ajaxSetup({async: false});
	var h2pbank = [];
	$.ajax({
	    type: "GET",
	    url: "../logic/deposit/H2PBankList.txt",                 // or path to file
	    success: function(content) {
	        // parse the content (content is the file content)
			content = content.trim();
			var lines = content.split('\n');
			var splitlines = [];

				for ( var i = 0 ; i < lines.length ; i++ ) {

				    // take index1, index2, value and store them in a 2d array (parse lines[i])
					 splitlines [i] = lines[i].split(':');
				}
				for ( var i = 0 ; i < splitlines.length ; i++ ) {
						h2pbank [splitlines[i][0]] = splitlines[i][1].split(',');


				}
	    },
	    error: function() {
	        console.log('error');
	    }
	});
	// $.ajaxSetup({async: true});

	if($('#method').val() == '') {	// 金額を隠す
		$('#amount_box').hide();
	} else {						// 金額を表示
		$('#amount_box').show();
	}

	$('#btnComplete').click(function() {
		$('#type').val('complete');
		submitForm();
	});

	$('#btnCpsSendAPI').click(function() {
		$('#type').val('cps_send');
		submitForm();
	});

	$('#btnSend').click(function() {
		$('#btnSend').prop('disabled', true);
		$('#type').val('send');
		submitForm();
	});

	$('#btnLbtcSend').click(function() {
		$('#btnLbtcSend').prop('disabled', true);
        var amount = $("#amount").val().replace(/,/g, '');
        $("#amount").val(amount);
		$('#type').val('lbtc_confirm');
		submitForm();
	});

	$('#btnCupSend').click(function() {
        $('#btnCupSend').addClass('disabled');
        $('#btnCupSend').attr('disabled',true);
        var amount = $("#amount").val().replace(/,/g, '');
        $("#amount").val(amount);
		$('#type').val('cup_confirm');
		submitForm();
	});

	$('#btnCpsSend').click(function() {
        $('#btnCpsSend').addClass('disabled');
        $('#btnCpsSend').attr('disabled',true);
        var amount = $("#amount").val().replace(/,/g, '');
        $("#amount").val(amount);
		$('#type').val('cps_confirm');
		submitForm();
	});

	$('#btnBackLbtcInput').click(function() {
		$('#btnBackLbtcInput').prop('disabled', true);
		$('#type').val('lbtc_back');
		submitForm();
	});

	$('#btnBackCupInput').click(function() {
		$('#btnBackCupInput').prop('disabled', true);
		$('#type').val('cup_back');
		submitForm();
	});

	$('#btnBackCpsInput').click(function() {
		$('#btnBackCpsInput').prop('disabled', true);
		$('#type').val('cps_back');
		submitForm();
	});

    $('#btnClearsettleSend').click(function() {
		$('#btnClearsettleSend').prop('disabled', true);
		$('#type').val('clearsettle_confirm');
		submitForm();
	});

    $('#btnBackClearsettleInput').click(function() {
		$('#btnBackClearsettleInput').prop('disabled', true);
		$('#type').val('clearsettle_back');
		submitForm();
	});

	$('#btnFlat3Send').click(function() {
		$('#type').val('flatsend');
		submitForm();
	});

	$('#btnFlat3Print').click(function() {
		$('#type').val('flatprint');
		$('#acForm').attr('target','_blank');
		submitForm();
		$('#acForm').attr('target','_self');
	});

	$('#btnLBTF3Send').click(function() {
		$('#type').val('lbtf3_send');
		submitForm();
	});

	$('#btnLBTF3Print').click(function() {
		$('#type').val('lbtf3_print');
		$('#acForm').attr('target','_blank');
		submitForm();
		$('#acForm').attr('target','_self');
	});

	$('#btnJPVSend').click(function() {
		$('#type').val('jpv_send');
		submitForm();
	});

	$('#btnJPVPrint').click(function() {
		$('#type').val('jpv_print');
		$('#acForm').attr('target','_blank');
		submitForm();
		$('#acForm').attr('target','_self');
	});

//CCD functions 05312021 start
	$('#btnCCDSend').click(function() {
		$('#btnCCDSend').prop('disabled', true);
        var amount = $("#amount").val().replace(/,/g, '');
        $("#amount").val(amount);
        $("#token").val("CCD"+randomStr(12))
		$('#type').val('cc_deposit_confirm');
		submitForm();
	});
	$('#btnBackCCDInput').click(function() {
		$('#btnBackCCDInput').prop('disabled', true);
		$('#type').val('cc_deposit_input');
		submitForm();
	});
	
	$('#btnCCDComplete').click(function() {	
		$('#btnCCDComplete').prop('disabled', true);
		$('#type').val('cc_deposit_send');
		submitForm();	
	});
	
	$('#APNcurrency').change(function(){
		let cur = $(this).val();
		// alert(cur);
		if (cur.length != 0) {
			$('.depositAmt_cont').show()

			let amounts = [];

			amounts["USD"] = {
			    "li05" : 100,
			    "li02" : 500,
			    "li03" : 1000,
			    "li04" : 3000,
			    "li01" : 5000
			};
			amounts["EUR"] = {
			    "li05" : 100,
			    "li02" : 500,
			    "li03" : 1000,
			    "li04" : 3000,
			    "li01" : 5000
			};
			amounts["JPY"] = {
			    "li05" : 10000,
			    "li02" : 50000,
			    "li03" : 100000,
			    "li04" : 300000,
			    "li01" : 500000
			};
			amounts["PHP"] = {
			    "li05" : 1000,
			    "li02" : 5000,
			    "li03" : 10000,
			    "li04" : 25000,
			    "li01" : 50000
			};
			
			for(var ids in amounts[cur]){
			    $("#"+ids).html(amounts[cur][ids])
			}
		}
		else{
			$('.depositAmt_cont').hide()
		}
	});





//CCD functions 05312021 end

//Add by Robby 20170609 start
	$('.CUP').click(function() {
		$('#method').val('CUP');
		$('#type').val('complete');
		submitForm();
	});
	$('.IBT').click(function() {
			$('#method').val('IBT');
		$('#type').val('complete');
		submitForm();
	});

	$('.JRF').click(function() {
		$('#method').val('JRF');
		$('#type').val('complete');
		submitForm();
	});

	$('.FLAT3').click(function() {
		$('#method').val('FLAT3');
		$('#type').val('complete');
		submitForm();
	});

	$('.UNIONPAY').click(function() {
		$('#method').val('UNIONPAY');
		$('#type').val('zaneta_input');
		submitForm();
	});

	$('#btnUPCSend').click(function() {
		$('#btnUPCSend').prop('disabled', true);
		$('#type').val('zanetapay_confirm');
		submitForm();
	});

	$('#btnBackUPCInput').click(function() {
		$('#acForm').attr('action', '');
		$('#method').val('UNIONPAY');
		$('#type').val('zaneta_input');
		submitForm();
	});

	$('#btnUPCComplete').click(function() {
		$('#type').val('zaneta_send');
		submitForm();
	});

	$('.LBTF3').click(function() {
		$('#method').val('LBTF3');
		$('#type').val('complete');
		submitForm();
	});

	$('.JPV').click(function() {
		$('#method').val('JPV');
		$('#type').val('complete');
		submitForm();
	});

	$('.CCD').click(function(){
		$('#method').val('CCD');
		$('#type').val('cc_deposit_input');
		submitForm();
	});

	$('.CARDPAY').click(function() {
		$('#method').val('CARDPAY');
		$('#type').val('complete');
		submitForm();
	});

	$('.LBTC').click(function() {
		$('#method').val('LBTC');
		$('#type').val('complete');
		submitForm();
	});

    $('.SEPA').click(function() {
		$('#method').val('SEPA');
		$('#type').val('complete');
		submitForm();
	});

	$('.BIT').click(function() {
	$('#method').val('BIT');
	$('#type').val('complete');
	submitForm();
	});

	$('.CLEARSETTLE').click(function() {
	$('#method').val('CLEARSETTLE');
	$('#type').val('clearsettle_input');
	submitForm();
	});

	$('.CPS').click(function() {
		$('#method').val('CPS');
		$('#type').val('cps_input');
		submitForm();
	});


	$('.LBTJ').click(function() {
	$('#method').val('LBTJ');
	$('#type').val('complete');
	submitForm();
	});

	$('#btnBitComplete').click(function() {
		$('#acForm').attr('action', '');
		$('#method').val('BIT');
		$('#type').val('bit_send');
		submitForm();
	});

	$('#btnBitSendMail').click(function() {
			$('#acForm').attr('action', '');
			$('#type').val('bit_send_email');
			submitForm();
	});

	$('#btnBackCUPInput').click(function() {
		$('#acForm').attr('action', '');
		$('#method').val('CUP');
		$('#type').val('complete');
		submitForm();
	});

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

	});

	$('#btnBackCashFlowInput').click(function() {
		$('#acForm').attr('action', '');
		$('#method').val('CARDPAY');
		$('#type').val('complete');
		submitForm();
	});

	$('#btnBackClearsettleInput').click(function() {
		$('#acForm').attr('action', '');
		$('#method').val('CLEARSETTLE');
		submitForm();
	});
	$('#btnClearsettleComplete').click(function() {
		$('#type').val('clearsettle_send');
		submitForm();
	});


//Add by Robby 20170609 end

	$('#btnPrint').click(function() {
		$('#type').val('print');
		$('#acForm').attr('target','_blank');
		submitForm();
		$('#acForm').attr('target','_self');
	});

	$('#btnBackInput').click(function() {
		$('#acForm').attr('target','_self');
		$('#acForm').attr('action','');
		submitForm();
	});


	$('#btnGoGps').click(function() {
		$ . ajax( {
			url	: 			'deposit'
			, type :		'post'
			, dataType :	'json'
			, data: {
				type :				'apply'
				, user_account :	$('#user_account').val()
				, method : 			$('#method').val()
			},
			success: function(data) {
				if(data['result'] == 'INFO') {
					$('#acForm').attr('target','_self');
					submitForm();
				} else {
					alert(data['message']);
				}
				console.log(data);
			},
			error: function(data) {
				alert('通信エラー');
				console.log(data);
			}
		});
	});

	if($('#type').val() == 'print') {
		window.print();
	}

	$('#method').change(function() {
		if($('#method').val() == '') {								// 金額を隠す
			$('#amount_box').hide();
			$('#t_amount_box').hide();
		} else if($('#method').val() == 'IBT') {	// 金額を表示
			$('#t_amount_box').show();
			$('#amount_box').hide();
		} else {													// 金額を表示
			$('#amount_box').show();
			$('#t_amount_box').hide();
		}
	});

	//for cubits start 06-20-18// edited for R3 bitcoin 01282019
		$(document).ready(function(){
	    $('.error_popup').hide();
		});

		$('#btnBitCopy').click(function(){
				var copyText = document.getElementById("bit_address");
				copyText.select();
				document.execCommand("copy");
		});

		$('#rateSearch').click(function() {
		    var a=$("#ratenum").val();
				// if(parseFloat(a)>0.01){
				// 		$("#inputerror").addClass("hidediv");
				// 		document.getElementById("ratecheck").style.display="";
				// 		document.getElementById("ratetable").style.display="none"
				// }else{
				// 		$("#inputerror").removeClass("hidediv");
				// }
		    if(!isNaN(a))
		    {
					if(a >= 0.05)
			    {
									$("#inputerror").addClass("hidediv");
								//get exchange rate of cubits
							    var currency = document.getElementById('rate_currency').value;
							    var bitcoin_fee = document.getElementById('bitcoin_fee').value;
							    var fee_str = bitcoin_fee.substr(0, bitcoin_fee.indexOf('%'));
							    var fee_val = (100-fee_str)/100;
								var fee = 1 - fee_val;

							   $.ajax({
							      url:'../api/getBitRate.php',
							      type:'get',
							      data: {currency:currency},
							      dataType: "json",
							      success:function(data){
									  console.log(data);
							        // var currency = data.currency;
							        var rate = data.data[currency];
							        var str = a*rate;
									var str2 = str.toFixed(2);
									var str3 = str2*fee;
									var str4 = Math.round(str3 * 100) / 100;
									var str5 = str2-str4;
									var str6 = str5.toFixed(2);

							        document.getElementById("bit_rate").innerHTML='1:'+rate;
							        document.getElementById("bit_exchangeto").innerHTML=currency+' '+str2;
							        document.getElementById("bit_amount").innerHTML=currency+' '+str6;
							        document.getElementById("bit_fee").innerHTML=currency+' '+str4;
							        document.getElementById("ratetable").style.display="";
							      },

							      error:function(e){
											var popup = document.getElementById('popupmsg');
											var span = document.getElementsByClassName("error_popup_close")[0];

											popup.style.display = "block";
											span.onclick = function() {
													popup.style.display = "none";
											}
											window.onclick = function(event) {
													if (event.target == popup) {
															popup.style.display = "none";
													}
											}
							      }

							    });
							}else
					    {
									$("#inputerror").removeClass("hidediv");
					        document.getElementById("ratetable").style.display="none";
				    	}
		    }else
		    {
						$("#inputerror").removeClass("hidediv");
		        document.getElementById("ratetable").style.display="none";
			}

		});
	//for cubits end//
    //for lbtc saving of data
    $("#btnLbtcSendAPI").click(function(e){
        e.preventDefault();
        $.ajax({
            url: "../api/sdDepositConfig",
            dataType: "text",
            type: "POST",
            data: {action : "lbtc_save"},
            success: function (data){
                $("#lbtcForm").submit();
            }
        });
    });
    $("#btnCupSendAPI").click(function(e){
        e.preventDefault();
        $.ajax({
            url: "../api/sdDepositConfig",
            dataType: "text",
            type: "POST",
            data: {action : "cup_save"},
            success: function (data){
                $("#cupForm").submit();
            }
        });
    });

	//LBTJ
	$('#clear_input').click(function(e){
		e.preventDefault();
		$('#file_img').val('');
	});

	$('#lbtjUploadSubmit').click(function(){
		$('#type').val('lbtj_upload');
		submitForm();
	});

	$('.collapsible').click(function(){
		var url = window.location.href;
		var showMore = (url.includes('ja')) ? '表示する' : 'Show More';
		var showLess = (url.includes('ja')) ? '非表示にする' : 'Show Less';
		if($(this).hasClass('active')){
			$(this).html(showMore+' <span class="fa fa-caret-down"></span>');
		}else{
			$(this).html(showLess+' <span class="fa fa-caret-up"></span>');

		}
	});

	if(document.getElementsByClassName("collapsible").length > 0) {
		var coll = document.getElementsByClassName("collapsible");
		var i;
		var errorText = document.getElementById("errorText");
		var formDiv = document.getElementById("lbtjUploadForm");

		if(errorText.innerHTML.length < 1) {
			formDiv.style.maxHeight = null;
		} else {
			formDiv.style.maxHeight = formDiv.scrollHeight + "px";
		}

		for (i = 0; i < coll.length; i++) {
		  coll[i].addEventListener("click", function() {
		    this.classList.toggle("active");
		    var content = this.nextElementSibling;

		    if (content.style.maxHeight){
		      content.style.maxHeight = null;
		    } else {
		      content.style.maxHeight = content.scrollHeight + "px";
		    }
		  });
		}
	}
		// Help2Pay
		$('.H2P').click(function() {
		$('#method').val('H2P');
		$('#type').val('h2p_input');
		submitForm();
		});

		$('#btnH2PSend').click(function() {
		$('#btnH2PSend').prop('disabled', true);
		$('#type').val('h2p_confirm');
		submitForm();
		});

		$('#btnBackH2PInput').click(function() {
			$('#acForm').attr('action', '');
			$('#method').val('H2P');
			$('#type').val('h2p_input');
			submitForm();
		});

		$('#btnCompleteH2P').click(function() {
			$('#type').val('h2p_send');
			submitForm();
		});

		$('#country').change(function() {

			var country = $('#country option:selected').attr('value');
			var others = '<li onClick="clickothers()">others</li><div>';
			var others1 = '<li onClick="clickothers()">Lainnya</li><div>';

			if(country == 'Indonesia'){
				$('#currency').text("IDR");
				$('#currencyval').val("IDR");
				var num1 = '200,000';
				var num2 = '1,000,000';
				var num3 = '10,000,000';
				var num4 = '100,000,000';
				var num5 = '190,000,000';
			}else if(country == 'Malaysia'){
				$('#currency').text("MYR");
				$('#currencyval').val("MYR");
				var num1 = '50';
				var num2 = '1,000';
				var num3 = '10,000';
				var num4 = '30,000';
				var num5 = '48,000';
			}if(country == 'Thailand'){
				$('#currency').text("THB");
				$('#currencyval').val("THB");
				var num1 = '500';
				var num2 = '1,000';
				var num3 = '10,000';
				var num4 = '300,000';
				var num5 = '480,000';
			}if(country == 'Vietnam'){
				$('#currency').text("VND");
				$('#currencyval').val("VND");
				var num1 = '300,000';
				var num2 = '1,000,000';
				var num3 = '10,000,000';
				var num4 = '100,000,000';
				var num5 = '190,000,000';
			}

			var list1 = '<li onClick="clicknum(li01)" id="li01">'+num1+'</li><li onClick="clicknum(li02)" id="li02">'+num2+'</li><li onClick="clicknum(li03)" id="li03">'+num3+'</li>';
			var list2 = '<li onClick="clicknum(li04)" id="li04">'+num4+'</li><li onClick="clicknum(li05)" class="selected" id="li05">'+num5+'</li>';
			var list3 = '<li onClick="clicknum(li05)" class="selected" id="li05">'+num4+'</li>';

			$('#uc_01').empty().append(list1);
			if($('#is_executive').attr('value')==true){
				$('#uc_01').append(list2);
				$("#amount").val(num5);
			}else{
				$('#uc_01').append(list3);
				$("#amount").val(num4);
			}
			var url = location.href;

			if(url.indexOf('/id/') > 0){
				$('#uc_01').append(others1);
			}else{
				$('#uc_01').append(others);
			}

			var country = $('#country option:selected').attr('value');
			$('#bank_name').empty();
			$('#bank_name').append("<option disabled='' selected=''>Please select a bank</option>");
			if(country in h2pbank){
						for(var j in h2pbank[country]){
							if(h2pbank[country].hasOwnProperty(j)){
								 var bankNameIndex = country;

								if(h2pbank.hasOwnProperty(bankNameIndex)){
									var str = h2pbank[country][j].split('.');
									$('#bank_name').append("<option value='"+ str[1] +"'>" + str[0] + "</option>");
									//$('#receipt_bank_code').val(str[1]);
								}

							}
						}
			}
		});

});
