/**
 * [OEmarry] (C)2010-2099 OEMARRY.COM Inc.
 * Email:service@phpcoo.com,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2015-07-01 by OEdev $
*/
//错误
function errorBack(id, text) {
	if (typeof(text) == "undefined") {
		text = "";
	}
	$obj_id = $("#"+id);
	$obj_id.removeClass("_span_ok");
	$obj_id.removeClass("_span_error");
	$obj_id.attr("class", "_span_error");
	if (text.length > 0) {
		$obj_id.html(text);
	}
}	
//正确
function okBack(id, text) {
	if (typeof(text) == "undefined") {
		text = "";
	}
	$obj_id = $("#"+id);
	$obj_id.removeClass("_span_ok");
	$obj_id.removeClass("_span_error");
	$obj_id.attr("class", "_span_ok");
	//if (text.length > 0) {
		$obj_id.html(text);
	//}
}

//取回方式
function selectForgetType(type) {
	if (type == "1") { //邮箱取回
		$("#puts_email").show();
		$("#puts_emailcode").show();
		$("#puts_mobile").hide();
		$("#puts_mobilecode").hide();

	}
	else { //手机取回
		$("#puts_email").hide();
		$("#puts_emailcode").hide();
		$("#puts_mobile").show();
		$("#puts_mobilecode").show();
	}
}



$(function(){
	
	$("#mobile").bind("blur", function(){ //手机号码
		$val_mobile = $(this).val();
		if ($val_mobile.length != 11) {
			errorBack("tips_mobile", "请输入11位手机号码");
			return false;
		}
		if (!isMobile($val_mobile)) {
			errorBack("tips_mobile", "手机号码格式不正确");
			return false;
		}	
		okBack("tips_mobile", "&nbsp;");
		
	});
	
	//获取手机验证码
	$("#btn_send_mobilecode").bind("click", function(){
		$val_mobile = $("#mobile").val();
		if ($val_mobile.length != 11) {
			errorBack("tips_mobile", "请输入11位手机号码");
			return false;
		}
		if (!isMobile($val_mobile)) {
			errorBack("tips_mobile", "手机号码格式不正确");
			return false;
		}
		$(this).val("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=sendforget",
			cache: false,
			data: {type:"2", name:$val_mobile, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_mobilecode", "发送成功，请查收短信");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_mobilecode", $result);
					}
					else {
						errorBack("tips_mobilecode", "发送失败，请检查手机号码是否可用");
					}
					
				}
				$("#btn_send_mobilecode").val("获取验证码");

			},
			error: function() {
				ToastShow("发送失败，请检查网络！");
				$("#btn_send_mobilecode").val("获取验证码");
			}
		});

	});

	//检查手机验证码
	$("#mobilecode").bind("blur", function(){
		$val_mobile = $("#mobile").val();
		$val_code = $(this).val();
		if (!isMobile($val_mobile)) {
			errorBack("tips_mobile", "手机号码格式不正确");
			return false;
		}
		if ($val_code.length == 0) {
			errorBack("tips_mobilecode", "请输入手机验证码");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkmobilecode",
			cache: false,
			data: {mobile:$val_mobile, mobilecode:$val_code, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_mobilecode", "手机验证码正确");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_mobilecode", $result);
					}
					else {
						errorBack("tips_mobilecode", "手机验证码错误，请重试");
					}
					
				}

			},
			error: function() {
				ToastShow("检测失败，请检查网络！");
			}
		});

	});



	$("#email").bind("blur", function(){ //邮箱
		$val_email = $(this).val();
		if ($val_email.length == 0) {
			errorBack("tips_email", "请输入邮箱地址");
			return false;
		}
		okBack("tips_email", "&nbsp;");
	});

	//获取邮箱验证码
	$("#btn_send_emailcode").bind("click", function(){
		$val_email = $("#email").val();
		if ($val_email.length  == 0) {
			errorBack("tips_email", "请输入邮箱地址");
			return false;
		}
		$(this).val("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=sendforget",
			cache: false,
			data: {type:"1", name:$val_email, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_emailcode", "发送成功，请查收邮件");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_emailcode", $result);
					}
					else {
						errorBack("tips_emailcode", "发送失败，请检查邮箱是否可用");
					}
					
				}
				$("#btn_send_emailcode").val("获取验证码");

			},
			error: function() {
				ToastShow("发送失败，请检查网络！");
				$("#btn_send_emailcode").val("获取验证码");
			}
		});

	});

	//检查邮箱验证码
	$("#emailcode").bind("blur", function(){

		$val_email = $("#email").val();
		$val_code = $(this).val();
		if ($val_email.length  == 0) {
			errorBack("tips_email", "请输入邮箱地址");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkemailcode",
			cache: false,
			data: {email:$val_email, emailcode:$val_code, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_emailcode", "邮箱验证码正确");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_emailcode", $result);
					}
					else {
						errorBack("tips_emailcode", "邮箱验证码错误，请重试");
					}
					
				}

			},
			error: function() {
				ToastShow("检测失败，请检查网络！");
			}
		});

	});


	$("#newpassword").bind("blur", function(){ //密码
		if ($(this).val().length <6 || $(this).val().length > 16) {
			errorBack("tips_newpassword", "请设置6~16位的新密码");
		}
		else {
			okBack("tips_newpassword", "&nbsp;");
		}
	});

	$("#confirmpassword").bind("blur", function(){ //确认密码
		if ($(this).val().length <6 || $(this).val().length > 16) {
			errorBack("tips_confirmpassword", "请再次输入新密码");
			return false;
		}
		if ($(this).val() != $("#newpassword").val()) {
			errorBack("tips_confirmpassword", "确认密码不正确");
			return false;
		}
		okBack("tips_confirmpassword", "&nbsp;");
	
	});

	
	$("#btn_post").bind("click", function(){ //提交
		$type = $('input[name="logintype"]:checked').val();
		if ($type == "" || typeof($type) == 'undefined') {
			$type = 1;
		}

		if ($('input[type="text"]').blur() && $('input[type="password"]').blur() || true) {
			$bool = $("html").find("._span_error").not($("._span_error:hidden"));
			if (typeof($bool.html()) == "undefined"){
				if ($type == 1) {
					$name = $("#email").val();
					$validcode = $("#emailcode").val();
				}
				else {
					$name = $("#mobile").val();
					$validcode = $("#mobilecode").val();
				}
				$password = $("#newpassword").val();
				$.ajax({
					type: "POST",
					url: _ROOT_PATH + "index.php?c=passport&a=setpassword",
					cache: false,
					data: {type:$type, name:$name, validcode:$validcode, password:$password, r:get_rndnum(8)},
					dataType: "json",
					success: function($data) {
						$json = eval($data);
						$response = $json.response;
						$result = $json.result;
						if ($response == "1") {
							ToastShow("取回密码成功，请记住新密码！");
							setTimeout(function(){
								window.location.href = _ROOT_PATH+"index.php";
							}, 800);
						}
						else {
							if ($result.length > 0) {
								ToastShow($result);
							}
							else {
								ToastShow("取回密码失败，请检查输入是否正确。");
							}
							
						}

					},
					error: function() {
						ToastShow("取回失败，请检查网络！");
					}
				});

			}
			else{

			}
		}

	});

});