$(document).ready(function () {
    $(".btn_submit_form").click(function () {
        var btn = $(this);
        var form = $(this).parents('form:first');
        if (form.length == 0) {
            return;
        }
        var url = window.location.pathname;
        var url_search = window.location.search; //保留原有的URL参数

        var bLogin = true;
        if (url.indexOf("/login") > -1 || url.indexOf("/Login") > -1) {
            url = url + "?op=LoginOn&r=" + Math.random();
            bLogin = true;
        }
        else {
            url = url + "?op=Reg&r=" + Math.random();
            bLogin = false;
        }
        if (url_search.indexOf('?') > -1) {
            //加载原有的URL参数
            url_search = url_search.substring(url_search.indexOf('?') + 1);
            if (url_search.length > 0)
                url += "&" + url_search;
        }

        form.attr("method", "post");
        btn.attr("disabled", "true");
        form.ajaxSubmit({
            url: url,
            success: function (result) {
                if (result.indexOf("ok:") > -1) {
                    if (!bLogin) {
                        alert("注册成功");
                    }
                    window.location.href = result.replace("ok:", "");
                } else {
                    if (result.indexOf("验证码") > -1)
                        ShowValidImage();
                    alert(result);
                }
                btn.removeAttr("disabled");
            }
        });
        return false;
    })
    $("#ddlProvince").change(function () {
        ProvinceCtiyCounty(this.value, 'LoadCityJson', 'ddlCity');
    })
    ShowValidImage();
})

//省市联动
function ProvinceCtiyCounty(id, op, bindControlId) {
    AjaxProvinceCity('/Ajax.aspx?op=' + op + '&id=' + id, bindControlId);
}
function AjaxProvinceCity(url, ElementId) {
    $.get(url, function (result) {
        if (result == null || result == "") {
            alert("数据加载失败");
        }
        var item = document.getElementById(ElementId);
        item.options.length = 1;
        var dataArray = eval(result);
        for (var i = 0; i < dataArray.length; i++) {
            if (dataArray[i] == "undefined" || dataArray[i] == null) continue;
            item.options.add(new Option(dataArray[i].Text, dataArray[i].Value));
        }
    })
}

function ShowValidImage() {
    var numkey = Math.random();
    var img = document.getElementById("imgVCode");
    if(img!=null)
        img.src = "/ValidateCode.aspx?t=" + numkey;
}
