function updateInfo(us_id) {
    $.ajax({
        url: 'update_user/' + us_id,
        type: 'POST',
        data: $("#frm1").serialize(),
        success: function (data, textStatus, jqXHR)
        {
            result = jQuery.parseJSON(data);
            if (result.success == "1")
            {
				$("#updateInfoSuccess").modal('show');
				$("#close").click(function(){
					window.location.reload(true);
				});
                
            }
            else
            {
                for (var key in result.errors)
                {
                    $('#' + key).html(result.errors[key]);
                }
            }
        }
    });
}
$("#package_register").submit(function (event)
{ 
    $.ajax({
        url: $(this).attr("action"),
        type: "POST",
        data: $("#package_register").serialize(),
        success: function (data, textStatus, jqXHR)
        {

            var results = jQuery.parseJSON(data);
            $('#package_register_modal').replaceWith(results.modal);
            $('#package_register_modal').modal('show');
            $("#package_register_back").click(function () {
                $("#account-info-tab").click();
            });
            $("#finish_package_register").click(function () {
                finish_package_register();
            });

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            $(".ajaxloading").hide();
        }
    });
    event.preventDefault();
});

function finish_package_register()
{
    $("#package_register_errors").html("");
    $.ajax({
        url: url + "frontend/user/finishRegisterPackage",
        type: "POST",
        data: $("#do_package_register").serialize(),
        success: function (data, textStatus, jqXHR)
        {
            var results = jQuery.parseJSON(data);
            if (results.success == "1")
            {
                $("#package_register_modal").modal("hide");
                $("#success_package_register_content").html(results.message);
                $("#success_package_register_modal").modal("show");
                
                $("#package_register_finish_button").click(function () {
                window.location.href = url+"frontend/user/profile/services-register"; 
                
            });
            }
            else
            {
                var er = "<div class=\"alert alert-block alert-success\">";
                for (var i = 0; i < results.errors.length; i++)
                {
                    var item = results.errors[i];
                    er=er+""+item+"<br />";
                }
                $("#package_register_errors").prepend(er+"</div>");
            }
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            $(".ajaxloading").hide();
        }
    });
}
$("#add_money").click(function () {
    $("#account-info-tab").click(); 
});
$("#"+current_tab+"-tab").click();
  

function changePassword(us_id)
{
	$('#passwd_old_err').html("");
	$('#passwd_new_err').html("");
	$('#confirm_passwd_new_err').html("");
	$.ajax({
		url: url+'frontend/user/change_password/' + us_id,
		type: 'POST',
		data: $("#frm2").serialize(),
		success: function (data, textStatus, jqXHR)
		{	
		console.log(data);
			result = jQuery.parseJSON(data);
			if (result.success == "1")
			{   
				$("#changePassSuccess").modal('show');
			}
			else
			{
				for (var key in result.errors)
				{
					$('#'+key).html(result.errors[key]);
				}
			}
		}
	});
}

function clearFormFields(id){
        $(id).find('input:text, input:hidden, input:password, input:file, select, textarea').val('');
        $(id).find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
		$(id).find('.help-block').html("");
}

$('body').on('hidden.bs.modal', '.modal', function () {
	clearFormFields('#frm2');
});

function chargeCard(id)
{
	$("#charging").css('display', 'block');
	$('#btn-charge').attr("disabled", true);
	$.ajax({
		url: '/services/recharge/recharge_card/'+id,
		type: 'POST',
		data: $("#frm3").serialize(),
		success: function (data, textStatus, jqXHR)
		{	
			$("#pin_card").val('');
			$("#card_serial").val('');
			$("#charging").css('display', 'none');
			$('#btn-charge').attr("disabled", false);
			result = jQuery.parseJSON(data);
			if(result.success == "1")
			{
				//$("#success_charge").find('#success_charge_info > center').text("Quý vị đã nạp thành công " + result.amount + " VNĐ vào tài khoản " + result.username + ", tài khoản ảo của Quý vị là " + result.acc_balanced + " VNĐ!");
				$("#success_charge").find('#success_charge_info > center').text(result.message);
				$("#success_charge").modal('show');
				$("#vip-regis-money").text(addCommas(result.acc_balanced));
				$("#topup-money").text(addCommas(result.acc_balanced) + ' VNĐ');			
			}else
			{
				$("#not_success_charge").find('div#no_success_charge_info > center').html (result.errMsg);
				$("#not_success_charge").modal('show');
			}
			
		}
	});
}

function tradeHistory(id, page)
{
	$.ajax({
		url: '/frontend/user/trade_history/'+id +'/page/'+page,
		type: 'POST',
		success: function(data)
		{
			results = jQuery.parseJSON(data);
			paging = results.paging;
			result = results.paidlogs;
			content = "";
			if(result.length>0){
				for (var i in result)
				{
					content += '<tr><td class="center">'+result[i].source+'</td><td class="center">'+result[i].amount+'</td><td>'+result[i].paid_time+'</td></tr>';
					
				}
			}else
			{
				content += '<tr><td class="center" colspan=3>Chưa có giao dịch nào!</td></tr>';
			}
			$("#trade-history").find("#content-trade-histoty").html(content);
			$("#trade-history").find(".pagination").html(paging);
			 $(".ajax_paging").click(function ()
			 {
				$.ajax({
					url: $(this).attr("href"),
					type: "POST",
					success: function (data, textStatus, jqXHR)
					{
						results = jQuery.parseJSON(data);
						tradeHistory(id, results.current_page);
					},
					error: function ()
					{
						$(".ajaxloading").hide();
					}
				});
				return false;
			 });
			$("#trade-history").modal('show');
		}
	});
}

function regisHistory(id, page)
{
	$.ajax({
		url: '/frontend/user/regis_history/'+id +'/page/'+page,
		type: 'POST',
		success: function(data)
		{
			results = jQuery.parseJSON(data);
			paging = results.paging;
			result = results.packagelogs;
			content = "";
			if(result.length>0){
				for (var i in result)
				{
					source = result[i].source;
					if (result[i].source == null || result[i].source == '') source = 'Thẻ cào';
					content += '<tr><td class="center">'+source+'</td><td class="center">'+result[i].p_name+'</td><td>'+result[i].created_time+'</td></tr>';
					
				}
			}else
			{
				content += '<tr><td class="center" colspan=3>Chưa đăng kí gói dịch vụ nào!</td></tr>';
			}
			$("#regis-history").find("#content-regis-histoty").html(content);
			$("#regis-history").find(".pagination").html(paging);
			 $(".ajax_paging").click(function ()
			 {
				$.ajax({
					url: $(this).attr("href"),
					type: "POST",
					success: function (data, textStatus, jqXHR)
					{
						results = jQuery.parseJSON(data);
						regisHistory(id, results.current_page);
					},
					error: function ()
					{
						$(".ajaxloading").hide();
					}
				});
				return false;
			 });
			$("#regis-history").modal('show');
		}
	});
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}