/**
 * [OElove Webapp] (C)2010-2099 oelove.com Inc.
 * Email: service@phpcoo.com，phpcoo@qq.com
 * This is not a freeware, use is subject to license terms
 * home LastTime 2019.08.09 Update by OE home
*/
$(function(){
    
    //发送短信前图片校验码...
    /*
    $(document).on("blur", "#block_range", function(){
        homeval = $(this).val(); //校验码输入框
        homeinput = $(this).attr("data-input"); //手机input名称...
        if (typeof(homeinput) == "undefined") {
            homeinput = "mobile";
        }
        if (homeval.length > 4) {
            _checkSlider(homeinput, homeval);
        }
    });
    */
    $(document).on("keyup", "#block_range", function(){
        var homeval = $(this).val();
        var homeinput = $(this).attr("data-input");
        if (typeof(homeinput) == "undefined") {
            homeinput = "mobile";
        }
        if (homeinput.length == 0) {
            homeinput = "mobile";
        }
        if (homeval.length > 4) {
            _checkSlider(homeinput, homeval);
        } 
    });
    
    //获取手机验证码0 新增 9.02.21
    $(document).on("click", "[f='but_send_mobile_code0']", function(){
        var home_input = $(this).attr("data-input");
        var home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        $("#send_type").val(home_sendtype);
        var home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            
            //9.08.09
            var homecheckimgcode = $(this).attr("data-checkimgcode");
            if (typeof(homecheckimgcode) == "undefined") {
                homecheckimgcode = "0";
            }
            if (home_sendtype == "reg") {
                if (homecheckimgcode == "1") {
                    //弹出图片校验码
                    $("#picker_imgcode_box").show();
                    _pickerGetImageCode();
                }
                else {
                    _pickerSendCode("mobile", home_sendtype, "but_send_mobile_code0");
                }
            }
            else {
                _pickerSendCode("mobile", home_sendtype, "but_send_mobile_code0");
            }
            
        }
        else {
            OEUI.message.msg({text: "请输入手机号码"})
        }
    });
    
    //获取手机验证码...
    $(document).on("click", "[f='but_send_mobile_code']", function(){
        var home_input = $(this).attr("data-input");
        var home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        var home_val = $("#"+home_input).val();
        // if (home_val.length > 0) {
        //     if ($("#block_range").val().length > 0) {
        //         $("#block_range").val("");               
        //     }
        //     $("#block_range").attr("data-input", home_input); //模块 mobile/email/qq
        //     $("#range_box").show();
        //     _update_validimg();
        // }
        // else {
            OEUI.message.msg({text: "请输入手机号码"})
        // }
        $("#send_type").val(home_sendtype);
    });
    
    //获取邮箱验证码... 
    $(document).on("click", "[f='but_send_email_code']", function(){
        
        //9.03.15 改成不需要图片校验码
        var home_input = $(this).attr("data-input");
        var home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        $("#send_type").val(home_sendtype);
        var home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            _pickerSendCode("email", home_sendtype, "but_send_email_code");
        }
        else {
            OEUI.message.msg({text: "请输入邮箱地址"})
        }
        
        /*
        home_input = $(this).attr("data-input");
        home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            if ($("#block_range").val().length > 0) {
                $("#block_range").val("");           
            }
            $("#block_range").attr("data-input", home_input); //模块 mobile/email/qq
            $("#range_box").show();
            _update_validimg();
        }
        else {
            OEUI.message.msg({text: "请输入邮箱地址"})
        }
        $("#send_type").val(home_sendtype);
        */
    });
    
    //获取QQ邮箱验证码...
    $(document).on("click", "[f='but_send_qq_code']", function(){
        
        //9.03.15 改成不需要图片校验码
        var home_input = $(this).attr("data-input");
        var home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        $("#send_type").val(home_sendtype);
        var home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            _pickerSendCode("qq", home_sendtype, "but_send_qq_code");
        }
        else {
            OEUI.message.msg({text: "请输入邮箱地址"})
        }
        
        /*
        home_input = $(this).attr("data-input");
        home_sendtype = $(this).attr("data-sendtype");
        if (typeof(home_sendtype) == "undefined") {
            home_sendtype = "reg";
        }
        home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            if ($("#block_range").val().length > 0) {
                $("#block_range").val("");            
            }
            $("#block_range").attr("data-input", home_input); //模块 mobile/email/qq
            $("#range_box").show();
            _update_validimg();
        }
        else {
            OEUI.message.msg({text: "请输入QQ号码"})
        }
        $("#send_type").val(home_sendtype);
        */
    });
    
    
    //获取手机验证码2方式  8.03.24
    $(document).on("click", "[f='but_send_mobile_code2']", function(){
        var home_input = $(this).attr("data-input");
        var home_val = $("#"+home_input).val();
        if (home_val.length > 0) {
            _pickerSendMobileCode2(home_input);
        }
        else {
            OEUI.message.msg({text: "手机号码不能为空"})
        }
    });
    
    //换一张图片校验码
    $(document).on("click", "#send_img_code", function(){
        _update_validimg();
    });
});

//更新图片校验码...
function _update_validimg() {
    $("#send_img_code").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=60&height=25&num=5&r="+get_rndnum(8));
}


//校验slide，成功后发送短信 8.08.14
function _checkSlider(homeinput, homeimgcode) {
    $.ajax({
        type: "POST",
        url: _ROOT_PATH+"index.php?c=picker",
        cache: false,
        data: {
            a:"slider", imgcode:homeimgcode, r:get_rndnum(8)
        },
        dataType: "json",
        success: function(homedata) {
            var homejson = eval(homedata);
            var homeresponse = homejson.response;
            var homeresult = homejson.result;
            if (homeresponse == "1") {
                $("#rangecode").val(homeimgcode);
                $("#range_box").hide();
                //发送验证码...
                _pickerSendCode(homeinput);
            }
            else {
                if (homeresult.length > 0) {
                    OEUI.message.msg({text: homeresult})
                }
                else {
                    OEUI.message.msg({text: "图片校验错误"})
                }
            }
        },
        error: function() {
            OEUI.message.msg({text: "系统繁忙，请稍后再试！"})
        }
    }); 
}

//发送验证码... 9.02.21 新增 homevalidcode 图片随机校验码 9.08.09
function _pickerSendCode(homemod, hometype, homef_name, homevalidcode) {
    if (typeof(homemod) == "undefined") {
        homemod = "mobile";
    }
    if (typeof(hometype) == "undefined") {
        hometype = $("#send_type").val(); //类型 reg/login/forget/rz...
        if (typeof(hometype) == "undefined") {
            hometype = "reg";
        }
    }
    if (typeof(homef_name) == "undefined") {
        homef_name = "";
    }
    //9.08.09
    if (typeof(homevalidcode) == "undefined") {
        homevalidcode = "";
    }

    var homeval = $("#"+homemod).val();
    if (homeval.length == 0) {
        if (hometype == "mobile") {
            OEUI.message.msg({text: "请填写手机号码"})
        }
        else if (hometype == "qq") {
            OEUI.message.msg({text: "请填写QQ号码"})
        }
        else {
            OEUI.message.msg({text: "请填写邮箱地址"})
        }
        return false;
    }
    var homeimgcode = $("#rangecode").val(); //图片验证码
    if (typeof(homeimgcode) == "undefined") {
        homeimgcode = "";
    }
    
    //$(".action-send-"+homemod+"-code").html("获取中...");
    if (homemod == "mobile") {
        var home_url = _ROOT_PATH+"index.php?c=picker&a=sendmobile";
    }
    else if (homemod == "qq") {
        var home_url = _ROOT_PATH+"index.php?c=picker&a=sendqq";
    }
    else {
        var home_url = _ROOT_PATH+"index.php?c=picker&a=sendemail";
    }
	$.ajax({
		type: "POST",
		url: home_url,
		cache: false,
		data: {
            type:hometype, imgcode:homeimgcode, mobile:homeval, email:homeval, qq:homeval,
            validcode:homevalidcode, 
            r:get_rndnum(8)
        },
		dataType: "json",
		success: function(homedata) {
            var homejson = eval(homedata);
            var homeresponse = homejson.response;
            var homeresult = homejson.result;
			if (homeresponse == "1") {
                if (homemod == "mobile") {
                    OEUI.message.msg({text: "发送成功，请查收手机短信！"})
                    if (homef_name == "") {
                        homef_name = "but_send_mobile_code";
                    }
                    _pickerCountDown("mobile", homef_name);
                }
                else if (homemod == "qq") {
                    OEUI.message.msg({text: "发送成功，请查收QQ邮箱！"})
                    //9.03.15
                    if (homef_name == "") {
                        homef_name = "but_send_qq_code";
                    }
                    _pickerCountDown("qq", homef_name);
                }
                else {
                    OEUI.message.msg({text: "发送成功，请查收邮件！"})
                    //9.03.15
                    if (homef_name == "") {
                        homef_name = "but_send_email_code";
                    }
                    _pickerCountDown("email", homef_name);
                }
			}
			else {
				if (homeresult.length > 0) {
					OEUI.message.msg({text: homeresult})
				}
				else {
                    if (homemod == "mobile") {
                        OEUI.message.msg({text: "发送失败，请检查手机号码是否可用！"})
                    }
                    else if (homemod == "qq") {
                        OEUI.message.msg({text: "发送失败，请检查QQ邮箱是否开通！"})
                    }
                    else {
                        OEUI.message.msg({text: "发送失败，请检查邮箱是否可用！"})
                    }
				}
				$(".action-send-"+homemod+"-code").html("<i>获取验证码</i>");
			}
			

		},
		error: function() {
			$(".action-send-"+homemod+"-code").html("<i>获取验证码</i>");
		}
	});
    
}


//发送手机验证码2 
function _pickerSendMobileCode2(homeinput) {
    if (typeof(homeinput) == "undefined") {
        homeinput = "mobile";
    }
    var homeval = $("#"+homeinput).val();
    if (homeval.length == 0) {
        OEUI.message.msg({text: "手机号码不能为空"})
        return false;
    }
    var home_url = _ROOT_PATH+"index.php?c=picker&a=sendmobile2";
	$.ajax({
		type: "POST",
		url: home_url,
		cache: false,
		data: {
            r:get_rndnum(8)
        },
		dataType: "json",
		success: function(homedata) {
            var homejson = eval(homedata);
            var homeresponse = homejson.response;
            var homeresult = homejson.result;
			if (homeresponse == "1") {
                OEUI.message.msg({text: "发送成功，请查收手机短信！"})
                _pickerCountDown(homeinput, "but_send_mobile_code2");
			}
			else {
				if (homeresult.length > 0) {
					OEUI.message.msg({text: homeresult})
				}
				else {
                    OEUI.message.msg({text: "发送失败，请检查手机号码是否可用！"})
				}
				$(".action-send-"+homeinput+"-code2").html("<i>获取验证码</i>");
			}
		},
		error: function() {
			$(".action-send-"+homeinput+"-code2").html("<i>获取验证码</i>");
		}
	});
    
}

//倒计时...
var cd_timer = 60; //1分钟
function _pickerCountDown(obj_id, attr_f) {
    var homeobj = $(".action-send-"+obj_id+"-code");
	if (cd_timer == 0) {
		homeobj.html("<i>获取验证码</i>");
		homeobj.addClass("current color_main"); 
		homeobj.attr("f", attr_f);
		cd_timer = 60; 
	}
	else {
    homeobj.removeClass("current color_main").css('color', '#999');
		homeobj.html("<i>"+cd_timer+"秒重新发送</i>");
		homeobj.attr("f", "");
		cd_timer--; 
		setTimeout(function() { 
			_pickerCountDown(obj_id, attr_f)
		},	1000);
	}
}


//图片校验码 9.08.09
$(function(){
    //关闭图片校验码
    $(document).on("click", "[f='but_picker_close_imgcode']", function(){
        $("#picker_imgcode_box").hide();
    });
    
    //刷新图片校验码
    $(document).on("click", "[f='but_picker_refresh_imgcode']", function(){
        $(this).find("img").show();
        _pickerGetImageCode();
    });
    
    //选择图片校验码
    $(document).on("click", "[f='but_picker_sel_imgcode']", function(){
        var homevalidcode = $(this).attr("data-code");
        $("[f='but_picker_sel_imgcode']").removeClass("current");
        $(this).addClass("current");
        var homesendtype = $("#send_type").val();
        var home_url = _ROOT_PATH+"index.php?c=picker&a=checkimgcode";
        
        $("#picker_imgcode_error").hide();
        $("#picker_imgcode_zhao").show();
        $.ajax({
            type: "POST",
            url: home_url,
            cache: false,
            data: {
                validcode:homevalidcode, r:get_rndnum(8)
            },
            dataType: "json",
            success: function(homedata) {
                var homejson = eval(homedata);
                var homeresponse = homejson.response;
                var homeresult = homejson.result;
                if (homeresponse == "1") {
                    //校验通过
                    $("#picker_imgcode_zhao").hide();
                    $("#picker_imgcode_error").hide();
                    $("#picker_imgcode_box").hide();
                    
                    //发送验证码
                    _pickerSendCode("mobile", homesendtype, "but_send_mobile_code0", homevalidcode);
                }
                else if (homeresponse == "2") {
                    //校验失败
                    $("#picker_imgcode_zhao").show();
                    $("#picker_imgcode_error").slideDown(100, function() {
                        setTimeout(function(){
                            $("#picker_imgcode_zhao").hide();
                            $("[f='but_picker_refresh_imgcode']").find("img").show();
                            _pickerGetImageCode();
                            $("#picker_imgcode_error").slideUp(100);
                        }, 1500);
                    });
                }
                else {
                    if (homeresult.length > 0) {
                        OEUI.message.msg({text: homeresult})
                    }
                    else {
                        OEUI.message.msg({text: "请求异常，请检查！"})
                    }
                    $("#picker_imgcode_zhao").hide();
                    $("#picker_imgcode_error").hide();
                }
            },
            error: function() {
                OEUI.message.msg({text: "系统繁忙，请稍后再试！"})
            }
        });
       
    });
    
});
//获取一组图片校验码 9.08.09
function _pickerGetImageCode() {
    var home_url = _ROOT_PATH+"index.php?c=picker&a=imgcode";
	$.ajax({
		type: "POST",
		url: home_url,
		cache: false,
		data: {
            r:get_rndnum(8)
        },
		dataType: "json",
		success: function(homedata) {
            var homejson = eval(homedata);
            var homeresponse = homejson.response;
            var homeresult = homejson.result;
            var homewzpic = homejson.wzpic;
            if (typeof(homewzpic) == "undefined") {
                homewzpic = "";
            }
            
			if (homeresponse == "1") {
                if (homewzpic == "") {
                    OEUI.message.msg({text: "点击/刷新过于频繁！"})
                }
                else {
                    $("#wzpic_title").find("img").attr("src", homewzpic);
                    $("#picker_imgcode_data").html(homeresult);
                }
			}
			else {
				if (homeresult.length > 0) {
					OEUI.message.msg({text: homeresult})
				}
				else {
                    OEUI.message.msg({text: "请求异常，请检查！"})
				}
			}
            $("[f='but_picker_refresh_imgcode']").find("img").hide();
		},
		error: function() {
            OEUI.message.msg({text: "系统繁忙，请稍后再试！"})
            $("[f='but_picker_refresh_imgcode']").find("img").hide();
		}
	});
    
}