// JavaScript Document
function map() {
    var mapOptions = new MMapOptions();// 构建地图辅助类
    // mapOptions.center = new MLngLat(position.lng, position.lat); //设置地图中心点
    mapOptions.center = new MLngLat(116.397428, 39.90923);
    mapOptions.toolbar = MConstants.ROUND; // 设置地图初始化工具条，ROUND:新版圆工具条
    mapOptions.toolbarPos = new MPoint(20, 20); // 设置工具条在地图上的显示位置
    mapOptions.overviewMap = MConstants.HIDE; // 设置鹰眼地图的状态，SHOW:显示，HIDE:隐藏（默认）
    mapOptions.scale = MConstants.SHOW; // 设置地图初始化比例尺状态，SHOW:显示（默认），HIDE:隐藏。
    mapOptions.zoom = 13;// 要加载的地图的缩放级别
    mapOptions.language = MConstants.MAP_CN;// 设置地图类型，MAP_CN:中文地图（默认），MAP_EN:英文地图
    mapOptions.fullScreenButton = MConstants.HIDE;// 设置是否显示全屏按钮，SHOW:显示（默认），HIDE:隐藏
    mapOptions.centerCross = MConstants.HIDE;// 设置是否在地图上显示中心十字,SHOW:显示（默认），HIDE:隐藏
    mapOptions.requestNum = 100;// 设置地图切片请求并发数。默认100。
    mapOptions.isQuickInit = true;// 设置是否快速显示地图，true显示，false不显示。
    mapOptions.hasDefaultMenu = false;
    mapOptions.mapComButton = MConstants.SHOW_NO;
    mapOptions.groundLogo = MConstants.HIDE;
    mapObj = new MMap("map", mapOptions); // 地图初始
    mapObj.setKeyboardEnabled(false);
    mapObj.addEventListener(mapObj, MConstants.MOUSE_CLICK, addMarker);
}
function addMarker(param) {
    var lng = param.eventX;
    var lat = param.eventY;
    var location1 = new MLngLat(lng, lat);
    var marker = new MMarker(location1);
    mapObj.addOverlay(marker, true);
    if (i == 0) {
        $("#end").val(lng + "," + lat);
    }
    if (i == 1) {
        $("#start").val(lng + "," + lat);
        mapObj.removeEventListener(mapObj, MConstants.MOUSE_CLICK, addMarker);
    }
    i++;

}

function showLogin() {
    $("#popup-login").show();
    $("#email").val("用户名 / 电子邮箱");
    $("#password").val("");
    $("#password").hide();
    $("#password_text1").show();
    $("#passworderror").html("");
    $("#validate_pop").css("display", "none");
    $("#captcha").val("请输入验证码");
}

function popLogin() {
    var email = null;
    var password = null;
    if ($("#validate_pop").css("display") != "none") {
        var captcha = $("#captcha").val();
        if (captcha == null || captcha == "" || captcha == "请输入验证码") {
            $('#captcha_codeerror').html("<font>验证码不能为空</font>");
            return;
        } else {
            if (!index.checkCaptcha(captcha)) {
                $('#captcha_codeerror').html("<font>验证码错误</font>");
                return;
            }
        }
    }
    email = $("#email").val();
    password = (hex_md5($("#password").val()));
    $.ajax({
        url : '/uc/ws/0.1/login',
        dataType : 'JSON',
        type : 'POST',
        data : {
            username : email,
            password : password,
            provider : 'CARSMART'
        },
        success : function(data) {
            successLogin(email, password);
        },
        error : function(xhr) {
            common401Handler(xhr);

            $("#passworderror").html("用户名或密码错误,请重新输入").show();
            $("#validate_pop").show();
        }
    });
}

function LoginPagelogin() {
    var email = null;
    var password = null;
    if ($("#validate").css("display") != "none") {
        var captcha = $("#captcha1").val();
        if (captcha == null || captcha == "" || captcha == "请输入验证码") {
            $('#captcha1_codeerror').html("<font>验证码不能为空</font>");
            $('#captcha1_codeerror').attr("class", "onError");
            return;
        } else {
            if (!index.checkCaptcha(captcha)) {
                $('#captcha1_codeerror').html("<font>验证码错误</font>");
                $('#captcha1_codeerror').attr("class", "onError");
                return;
            }
        }
    }
    email = $("#email1").val();
    password = (hex_md5($("#password1").val()));

    $.ajax({
        url : '/uc/ws/0.1/login',
        dataType : 'JSON',
        type : 'POST',
        data : {
            username : email,
            password : password,
            provider : 'CARSMART'
        },
        success : function(data) {
            var backurl = getQueryString("backurl");
            successLogin(email, password);
            if (backurl == "" || backurl == null || backurl == "null") {
                window.location.href = "index.jsp";
            } else {
                window.location.href = decodeURIComponent(backurl) + "";
            }
        },
        error : function(xhr) {
            loginFailCall(xhr, email);
        }
    });
}
var mailUrl;
function loginFailCall(xhr, loginUser) {
    var message = eval("(" + xhr.responseText + ")");
    if (message[0].code.indexOf("inactive") != -1) {
        var email = message[0].code.split(":")[1];
        if (email.indexOf("gmail") != -1 || email.indexOf("hotmail") != -1) {
            mailUrl = "http://" + email;
        } else {
            mailUrl = "http://mail." + email;
        }
        $.cookie("loginUser", loginUser);
        window.location.href = "/nova/registry-ack.jsp?email=" + email + "&mailUrl="
                + encodeURIComponent(mailUrl);
    } else {
        $("#password1error").html("用户名或密码错误,请重新输入").show();
        $('#password1error').attr("class", "onError");
        $("#validate").show();
    }

}
function gotoActivite() {
    window.open(mailUrl);
}
function login() {
    var email = null;
    var password = null;
    if ($("#validate").css("display") != "none") {
        var captcha = $("#captcha1").val();
        if (captcha == null || captcha == "" || captcha == "请输入验证码") {
            $('#captcha1_codeerror').html("<font>验证码不能为空</font>");
            $('#captcha1_codeerror').attr("class", "onError");
            return;
        } else {
            if (!index.checkCaptcha(captcha)) {
                $('#captcha1_codeerror').html("<font>验证码错误</font>");
                $('#captcha1_codeerror').attr("class", "onError");
                return;
            }
        }
    }

    email = $("#email1").val();
    password = (hex_md5($("#password1").val()));

    $.ajax({
        url : '/uc/ws/0.1/login',
        dataType : 'JSON',
        type : 'POST',
        data : {
            username : email,
            password : password,
            provider : 'CARSMART'
        },
        success : function(data) {
            var backurl = getQueryString("backurl");
            successLogin(email, password);

            if (backurl == "" || backurl == null || backurl == "null") {
                window.location.href = "index.jsp";
            } else {
                window.location.href = decodeURIComponent(backurl) + "";
            }
        },
        error : function(xhr) {
            loginFailCall(xhr, email);
        }
    });
}

function homeReady() {
    $("#password_text1").focus(function() {
        $("#password").show();
        $("#password").focus();
        $("#password_text1").hide();
    });
    $("#password_text").focus(function() {
        $("#password1").show();
        $("#password1").focus();
        $("#password_text").hide();
    });
    $("#email1").focus(function() {
        empty_if(this, "用户名 / 电子邮箱");
    });
    $("#password1").keydown(function(e) {
        do_if_return(e, login);
    });
    $("#password1").blur(function(e) {
        run_if_empty(this, function() {
            $("#password1").hide();
            $("#password_text").show();
        });
    });
    $("#password").blur(function(e) {
        run_if_empty(this, function() {
            $("#password").hide();
            $("#password_text1").show();
        });
    });
    $("#email1").blur(function() {
        set_if_empty(this, "用户名 / 电子邮箱");
    });
    $("#email").focus(function() {
        empty_if(this, "用户名 / 电子邮箱");
    });
    $("#email").blur(function() {
        set_if_empty(this, "用户名 / 电子邮箱");
    });
    $("#Left_Tab").tabs();

    $("#go_svc").mouseover(function() {
        $("#go_svc").css("cursor", "pointer");
    });
    $("#go_svc").click(function() {
        window.location.href = "home.jsp";
        $.cookie("urlHash", "");
    });
    $("#search").focus(function() {
        empty_if(this, "昵称,用户名,邮箱");
    });
    $("#search").blur(function() {
        set_if_empty(this, "昵称,用户名,邮箱");
    });

    $("#captcha").focus(function() {
        empty_if(this, "请输入验证码");
        $('#captcha_codeerror').html("");
    });
    $("#captcha").blur(function() {
        set_if_empty(this, "请输入验证码");
    });
    $("#captcha1").focus(function() {
        empty_if(this, "请输入验证码");
        $('#captcha1_codeerror').html("");
    });
    $("#captcha1").blur(function() {
        set_if_empty(this, "请输入验证码");
    });
    // $(".title a").click(function(event) {
    // showLogin();
    // });
    $("#captcha").keydown(function(e) {
        do_if_return(e, login);
    });
    $("#findPasswd").mouseover(function() {
        $("#findPasswd").css("cursor", "pointer");
    });
    $("#findPasswd").click(
            function() {
                var code = $("#email").val();
                if (code == null || code == "" || code == "用户名 / 电子邮箱") {
                    $('#emailerror').html("<font>邮箱或帐号不能为空</font>");
                    $('#emailerror').attr("class", "onError");
                    return;
                }
                var captcha = $("#captcha").val();
                if (captcha == null || captcha == "") {
                    $('#captcha_codeerror').html("<font>验证码不能为空</font>");
                    $('#captcha_codeerror').attr("class", "onError");
                    return;
                } else {
                    if (!index.checkCaptcha(captcha)) {
                        $('#captcha_codeerror').html("<font>验证码错误</font>");
                        $('#captcha_codeerror').attr("class", "onError");
                        return;
                    }
                }
                $("#findPasswd").hide();
                $('#wait').show();
                if ($("#recoverPwdByCall").css("display") == "block") {// 判断手机号码
                    var reg = /^1[3|4|5|8][0-9]\d{8}$/;
                    var mobile = $("#mobile").val();
                    if (mobile == "" || mobile == null) {
                        $('#mobileerror').html("<font>请输入手机号码</font>");
                        $('#mobileerror').attr("class", "onError");
                        $("#findPasswd").show();
                        $('#wait').hide();
                        return;
                    } else {
                        if (!reg.test(mobile)) {
                            $('#mobileerror').html("<font>手机号码格式错误。</font>");
                            $('#mobileerror').attr("class", "onError");
                            $("#findPasswd").show();
                            $('#wait').hide();
                            return;
                        }
                    }

                    $.ajax({
                        url : '/uc/ws/0.1/password/getcode?user=' + encodeURIComponent(code) + '&mobile='
                                + mobile + '',
                        type : 'GET',
                        datatype : 'JSON',
                        async : false,
                        success : function(data) {
                            /*
                             * if (data.success) { alert("重置已经发送到您手机请注意查收"); wi
                             * ndow.location.href="/nova/uc-recover-password-mobile-ack.jsp"; }
                             * else { $('#emailerror').html("<font>" +
                             * data.message + "</font>"); }
                             */
                            alert("密码重置的验证码已经发送到您手机请注意查收");
                            window.location.href = "uc-password-reset.jsp";
                        },
                        error : function(xhr) {
                            common401Handler(xhr);
                            if (xhr.responseText.indexOf("message") != -1) {
                                var message = eval("(" + xhr.responseText + ")");
                                $('#emailerror').html(message[0].message);
                                $("#findPasswd").show();
                                $('#wait').hide();
                            }

                        }
                    });
                    return;
                }
                $.ajax({
                    url : '/uc/passwd/checkEmail',
                    data : {
                        email : code
                    },
                    type : 'GET',
                    datatype : 'JSON',
                    success : function(data) {
                        var alias;
                        if (data.success) {
                            if (code.indexOf("@") != -1) {
                                alias = code.substring(0, code.indexOf("@"));
                                url = '/uc/ws/0.1/user/search/or?email=' + code;
                            } else {
                                alias = code;
                                url = '/uc/ws/0.1/user/search/or?alias=' + code;
                            }

                            $.ajax({
                                url : url,
                                type : 'GET',
                                dataType : 'JSON',
                                success : function(data) {
                                    if (data.length > 0) {
                                        var email = data[0].email;
                                        window.location.href = "uc-recover-password-ack.jsp?email=" + email;
                                    }

                                },
                                error : function(xhr) {
                                    common401Handler(xhr);
                                }
                            });

                        } else {
                            $('#emailerror').html("<font>" + data.message + "</font>");
                        }
                    },
                    error : function(xhr) {
                        common401Handler(xhr);
                    }
                });
            });
}
function closeComent() {
    $("#popup-login").hide();
}

function navi() {
    var code = $("#code").val();
    if (code == "" || code == null) {
        alert("请您输入验证码");
    } else {
        window.location.href = "/nova/jsp/business/codetonova.jsp?code=" + code + "";
    }
}
