var checkConsent = '请阅读并同意协议';
function addVal(obj)
{
    var num = $(obj).attr("data-num");
    $("#js_data_num").val(num);
}


$(function() {


    $("#js_input_pay").each(function(index, element) {
        if ($("#js_input_pay").val() == "")
        {
            $("#js_for_label").show();
        }
        else
        {
            $("#js_for_label").hide();
        }
        ;
        $(this).focus(function() {
            $("#js_for_label").hide();
        }).blur(function() {
            if ($("#js_input_pay").val() == "")
            {
                $("#js_for_label").show();
            }
            else
            {
                $("#js_for_label").hide();
            }
            ;
        })
    });


    $("#js_select_pay .sct").live("click", function() {
        addVal(this);
        $("#js_select_pay .sct").removeClass("select");
        $(this).addClass("select");
        $("#js_input_pay").val("");
        $("#js_for_label").show();
        $("#js_input_pay_pay .error").remove();

        var rmb = $(this).attr("data-RMB");
        var sale = $("#js_data_sale").val();
        var fd = rmb * sale;
        $("#js_RMB_01,#js_RMB_02").text(rmb);
        $("#js_FD").text(fd);
    });

    $("#js_input_pay").live("focus", function() {
        addVal(this);
        $("#js_select_pay .sct").removeClass("select");
        if ($(this).val() == "")
        {
            $("#js_RMB_01,#js_RMB_02,#js_FD").text(0);
        }
    }).live("keyup", function() {
        var rmb = parseInt($(this).val());
        var sale = $("#js_data_sale").val();
        var fd = rmb * sale;
        if (!isNaN(rmb) && rmb > 0)
        {
            $("#js_RMB_01,#js_RMB_02").text(rmb);
            $("#js_FD").text(fd);
        }
        else
        {
            $("#js_RMB_01,#js_RMB_02,#js_FD").text(0);
        }
    });

    $("#js_invoice").click(function() {
        if (this.checked)
        {
            $("#js_invoice_input_box").show();
        }
        else
        {
            $("#js_invoice_input_box").hide();
            $("#js_invoice_pay").empty();
        }
    });


    $.validator.addMethod("mTmlNum", function(value, element, params) {
        var reg1 = /^([a-zA-Z0-9\u4E00-\u9FA5]|\s)*$/g;
        if (reg1.test(value))
            return true;
        else
            return false;
    }, "只能输入中文、数字、英文和空格");

	$(".js_btn_pay_close_zz").click(function(){
		$("#new_login_btn_none").hide();
	});
    $("#js_pay_form").validate({
        submitHandler: function(form) {
			$("#new_login_btn_none").show();
			var bank_name = $("input[name=back_name]:checked").val();
			if(bank_name != 'wxpay'){
                $("#js_pay_form").attr('target','_blank');
                openWin('js_pay_open');
            }
            if(bank_name == 'wxpay') {
                $("#js_pay_form").attr('target','_self');
            }
            form.submit();
		 },
        errorPlacement: function(error, element) {
            var id = element.attr("id");
            error.appendTo($("#" + id + "_pay"));
        },
        rules: {
            else_num: {
                required: function() {
                    if ($("#js_data_num").val() == 0) {
                        return true;
                    }
                    else {
                        return false;
                    }
                },
                digits: true,
                min: 5,
                max: 999999999
            },
            invoice: {
                required: "#js_invoice",
                maxlength: 40,
                mTmlNum: true
            },
            checkConsent: {
                required: true
            }
        },
        messages: {
            else_num: {
                required: "请填写需要充值的金额",
                digits: "只能输入正整数",
                min: "最少充值5",
                max: "最大充值999999999"
            },
            invoice: {
                required: "请填写发票抬头",
                maxlength: "最多只能填写40个字"
            },
            checkConsent: {
                required: checkConsent
            }
        }

    });

});