/**
 * [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
 * $ LastTime 2019.09.07 Update by CL $
*/
$(function(){
    //焦点处理...
    $(document).on("keyup", "#block_range", function(){
        $val = $(this).val();
        $input = $(this).attr("data-input");
        if (typeof($input) == "undefined") {
            $input = "mobile";
        }
        if ($input.length == 0) {
            $input = "mobile";
        }
        if ($val.length > 4) {
            _checkSlider($input, $val);
        } 
    });
    
    
    //获取手机验证码...
    $(document).on("click", "[f='but_send_mobile_code']", function(){
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            if ($("#block_range").val().length > 0) {
                $("#block_range").val("");               
            }
            $("#block_range").attr("data-input", $_input); //模块 mobile/email/qq
            $("#range_box").show();
            _update_validimg();
        }
        else {
            mui.toast("请输入手机号码");
        }
        $("#send_type").val($_sendtype);
    });
    
    //获取邮箱验证码...
    $(document).on("click", "[f='but_send_email_code']", function(){
        
        //9.03.15 无需要图片校验码
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $("#send_type").val($_sendtype);
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            _pickerSendCode("email", $_sendtype, "but_send_email_code");
        }
        else {
            mui.toast("请输入邮箱地址");
        }
        
        
        /* 9.03.15 去除
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            if ($("#block_range").val().length > 0) {
                $("#block_range").val("");           
            }
            $("#block_range").attr("data-input", $_input); //模块 mobile/email/qq
            $("#range_box").show();
            _update_validimg();
        }
        else {
            mui.toast("请输入邮箱地址");
        }
        $("#send_type").val($_sendtype);
        */
    });
    
    //获取QQ邮箱验证码...
    $(document).on("click", "[f='but_send_qq_code']", function(){
        
        //9.03.15 无需要图片校验码
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $("#send_type").val($_sendtype);
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            _pickerSendCode("qq", $_sendtype, "but_send_qq_code");
        }
        else {
            mui.toast("请输入QQ号码");
        }
        
        /* 9.03.15 去除
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            if ($("#block_range").val().length > 0) {
                $("#block_range").val("");            
            }
            $("#block_range").attr("data-input", $_input); //模块 mobile/email/qq
            $("#range_box").show();
            _update_validimg();
        }
        else {
            mui.toast("请输入QQ号码");
        }
        $("#send_type").val($_sendtype);
        */
    });
    
    
    //获取手机验证码2方式  8.03.24
    $(document).on("click", "[f='but_send_mobile_code2']", function(){
        $_input = $(this).attr("data-input");
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            _pickerSendMobileCode2($_input);
        }
        else {
            mui.toast("手机号码不能为空");
        }
    });
    
    //换一张图片校验码
    $(document).on("click", "#send_img_code", function(){
        _update_validimg();
    });
    
    
    //获取手机验证码.注册用...8.11.06
    $(document).on("click", "[f='but_send_mobile_code0']", function(){
        $_input = $(this).attr("data-input");
        $_sendtype = $(this).attr("data-sendtype");
        if (typeof($_sendtype) == "undefined") {
            $_sendtype = "reg";
        }
        $("#send_type").val($_sendtype);
        $_val = $("#"+$_input).val();
        if ($_val.length > 0) {
            
            //9.09.02
            $checkimgcode = $(this).attr("data-checkimgcode");
            if (typeof($checkimgcode) == "undefined") {
                $checkimgcode = "0";
            }
            if ($_sendtype == "reg") {
                if ($checkimgcode == "1") {
                    //弹出图片校验码
                    $("#picker_imgcode_box").show();
                    _pickerGetImageCode();
                }
                else {
                    _pickerSendCode("mobile", $_sendtype, "but_send_mobile_code0");
                }
            }
            else {
                _pickerSendCode("mobile", $_sendtype, "but_send_mobile_code0");
            }
        }
        else {
            mui.toast("请输入手机号码");
        }
    });
    
});


//更新图片校验码...
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($input, $imgcode) {
    $.ajax({
        type: "POST",
        url: WAP_APPFILE+"&c=picker",
        cache: false,
        data: {
            a:"slider", imgcode:$imgcode, r:get_rndnum(8)
        },
        dataType: "json",
        success: function($data) {
            $json = eval($data);
            $response = $json.response;
            $result = $json.result;
            if ($response == "1") {
                $("#rangecode").val($imgcode);
                $("#range_box").hide();
                //发送验证码...
                _pickerSendCode($input);
            }
            else {
                if ($result.length > 0) {
                    mui.toast($result);
                }
                else {
                    mui.toast("图片校验错误");
                }
            }
        },
        error: function() {
            mui.toast("系统繁忙，请稍后再试！");
        }
    }); 
}

//发送验证码... 图片随机校验码 9.09.02
function _pickerSendCode($mod, $type, $f_name, $validcode) {
    if (typeof($mod) == "undefined") {
        $mod = "mobile";
    }
    if (typeof($type) == "undefined") {
        $type = $("#send_type").val(); //类型 reg/login/forget/rz...
    }
    if ($type.length == 0) {
        $type = "reg";
    }
    if (typeof($f_name) == "undefined") {
        $f_name = "";
    }
    
    //9.09.02
    if (typeof($validcode) == "undefined") {
        $validcode = "";
    }
    
    if (!$("[f='"+$f_name+"']").hasClass("forbid_send")) {    
    
        $val = $("#"+$mod).val();
        if ($val.length == 0) {
            
            //9.09.02
            if ($mod == "mobile") {
                mui.toast("请填写手机号码");
            }
            else if ($mod == "qq") {
                mui.toast("请填写QQ号码");
            }
            else {
                mui.toast("请填写邮箱地址");
            }
            
            return false;
        }
        
        $imgcode = $("#rangecode").val(); //滑块随机码
        if (typeof($imgcode) == "undefined") {
            $imgcode = "";
        }
        
        //$(".action-send-"+$mod+"-code").html("获取中...");
        if ($mod == "mobile") {
            $_url = WAP_APPFILE+"&c=picker&a=sendmobile";
            if ($f_name.length == 0) {
                $f_name = "but_send_mobile_code";
            }
        }
        else if ($mod == "qq") {
            $_url = WAP_APPFILE+"&c=picker&a=sendqq";
            if ($f_name.length == 0) {
                $f_name = "but_send_qq_code";
            }
        }
        else {
            $_url = WAP_APPFILE+"&c=picker&a=sendemail";
            if ($f_name.length == 0) {
                $f_name = "but_send_email_code";
            }
        }
        
        $("[f='"+$f_name+"']").addClass("forbid_send"); //防止多次点击 8.11.11
        
        _maskShow(); //9.03.15
        
        $.ajax({
            type: "POST",
            url: $_url,
            cache: false,
            data: {
                type:$type, imgcode:$imgcode, mobile:$val, email:$val, qq:$val, 
                validcode:$validcode, 
                r:get_rndnum(8)
            },
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                
                 _maskClose(); //9.03.15
                $("[f='"+$f_name+"']").removeClass("forbid_send");
                
                if ($response == "1") {
                    if ($mod == "mobile") {
                        mui.toast("发送成功，请查收手机短信！");
                        _pickerCountDown("mobile", $f_name);
                    }
                    else if ($mod == "qq") {
                        mui.toast("发送成功，请查收QQ邮箱！");
                        _pickerCountDown("qq", $f_name);
                    }
                    else {
                        mui.toast("发送成功，请查收邮件！");
                        _pickerCountDown("email", $f_name);
                    }
                }
                else {
                    if ($result.length > 0) {
                        mui.toast($result);
                    }
                    else {
                        if ($mod == "mobile") {
                            mui.toast("发送失败，请检查手机号码是否可用！");
                        }
                        else if ($mod == "qq") {
                            mui.toast("发送失败，请检查QQ邮箱是否开通！");
                        }
                        else {
                            mui.toast("发送失败，请检查邮箱是否可用！");
                        }
                    }
                    $(".action-send-"+$mod+"-code").html("<i>获取验证码</i>");
                }
                

            },
            error: function() {
                $("[f='"+$f_name+"']").removeClass("forbid_send");
                $(".action-send-"+$mod+"-code").html("<i>获取验证码</i>");
                
                _maskClose(); //9.03.15
            }
        });
    
    }
    
}


//发送手机验证码2 8.03.24
function _pickerSendMobileCode2($input) {
    if (typeof($input) == "undefined") {
        $input = "mobile";
    }
    $val = $("#"+$input).val();
    if ($val.length == 0) {
        mui.toast("手机号码不能为空");
        return false;
    }
    _maskShow();
    $_url = WAP_APPFILE+"&c=picker&a=sendmobile2";
	$.ajax({
		type: "POST",
		url: $_url,
		cache: false,
		data: {
            r:get_rndnum(8)
        },
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
            _maskClose();
			if ($response == "1") {
                mui.toast("发送成功，请查收手机短信！");
                _pickerCountDown($input, "but_send_mobile_code2");
			}
			else {
				if ($result.length > 0) {
					mui.toast($result);
				}
				else {
                    mui.toast("发送失败，请检查手机号码是否可用！");
				}
				$(".action-send-"+$input+"-code2").html("<i>获取验证码</i>");
			}
		},
		error: function() {
			$(".action-send-"+$input+"-code2").html("<i>获取验证码</i>");
            _maskClose();
		}
	});
    
}

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


//图片校验码 9.09.02
$(function(){
    //关闭图片校验码
    $(document).on("tap", "[f='but_picker_close_imgcode']", function(){
        $("#picker_imgcode_box").hide();
    });
    
    //刷新图片校验码
    $(document).on("tap", "[f='but_picker_refresh_imgcode']", function(){
        $(this).find("img").show();
        _pickerGetImageCode();
    });
    
    //选择图片校验码
    $(document).on("tap", "[f='but_picker_sel_imgcode']", function(){
        $validcode = $(this).attr("data-code");
        $("[f='but_picker_sel_imgcode']").removeClass("current");
        $(this).addClass("current");
        $sendtype = $("#send_type").val();
        $_url = _ROOT_PATH+"index.php?m=wap&c=picker&a=checkimgcode";
        
        $("#picker_imgcode_error").hide();
        $("#picker_imgcode_zhao").show();
        $.ajax({
            type: "POST",
            url: $_url,
            cache: false,
            data: {
                validcode:$validcode, r:get_rndnum(8)
            },
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                if ($response == "1") {
                    //校验通过
                    $("#picker_imgcode_zhao").hide();
                    $("#picker_imgcode_error").hide();
                    $("#picker_imgcode_box").hide();
                    
                    //发送验证码
                    _pickerSendCode("mobile", $sendtype, "but_send_mobile_code0", $validcode);
                }
                else if ($response == "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 ($result.length > 0) {
                        mui.toast($result);
                    }
                    else {
                        mui.toast("请求异常，请检查！");
                    }
                    $("#picker_imgcode_zhao").hide();
                    $("#picker_imgcode_error").hide();
                }
            },
            error: function() {
                layer.msg("系统繁忙，请稍后再试！");
            }
        });
       
    });
    
});
//获取一组图片校验码 9.09.02
function _pickerGetImageCode() {
    $_url = _ROOT_PATH+"index.php?m=wap&c=picker&a=imgcode";
	$.ajax({
		type: "POST",
		url: $_url,
		cache: false,
		data: {
            r:get_rndnum(8)
        },
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
            $wzpic = $json.wzpic;
            if (typeof($wzpic) == "undefined") {
                $wzpic = "";
            }
            
			if ($response == "1") {
                if ($wzpic == "") {
                    mui.toast("点击/刷新过于频繁！");
                }
                else {
                    $("#wzpic_title").find("img").attr("src", $wzpic);
                    $("#picker_imgcode_data").html($result);
                }
			}
			else {
				if ($result.length > 0) {
					mui.toast($result);
				}
				else {
                    mui.toast("请求异常，请检查！");
				}
			}
            $("[f='but_picker_refresh_imgcode']").find("img").hide();
		},
		error: function() {
            mui.toast("系统繁忙，请稍后再试！");
            $("[f='but_picker_refresh_imgcode']").find("img").hide();
		}
	});
    
}
