/**
 * [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-12-22 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 selectLoginType(type) {
	if (type == "1") { //邮箱/用户名登录
		$("#puts_loginname").show();
		$("#puts_mobile").hide();

	}
	else { //手机登录
		$("#puts_loginname").hide();
		$("#puts_mobile").show();
	}
}



$(function(){
	
	$("#mobile").bind("blur", function(){ //手机号码
		$val_mobile = $(this).val();
		if ($val_mobile.length != 11) {
			errorBack("tips_mobile", "请输入手机号码");
			return false;
		}
		if (!isMobile($val_mobile)) {
			errorBack("tips_mobile", "手机号码格式不正确");
			return false;
		}
		okBack("tips_mobile", "&nbsp;");
	});
	

	$("#loginname").bind("blur", function(){ //邮箱、用户名
		$val_name = $(this).val();
		if ($val_name.length == 0) {
			errorBack("tips_loginname", "请输入邮箱或者用户名");
			return false;
		}
		okBack("tips_loginname", "&nbsp;");
		
	});


	$("#password").bind("blur", function(){ //密码
		if ($(this).val().length <6 || $(this).val().length > 16) {
			errorBack("tips_password", "请输入6~16位的密码");
		}
		else {
			okBack("tips_password", "&nbsp;");
		}
	
	});

	
	if ($("#checkcode").length > 0) { //验证码
		$("#checkcode").bind("blur", function(){ 
			$val_checkcode = $(this).val();
			if ($val_checkcode.length == 0) {
				errorBack("tips_checkcode", "请填写验证码");
				return false;
			}
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=passport&a=checkcheckcode",
				cache: false,
				data: {checkcode:$val_checkcode, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						okBack("tips_checkcode", "&nbsp;");
					}
					else {
						if ($result.length > 0) {
							errorBack("tips_checkcode", $result);
						}
						else {
							errorBack("tips_checkcode", "验证码错误");
						}
					}

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

		$("#img_checkcode").bind("click", function(){ //更换验证码
			$(this).attr("src", _ROOT_PATH+"source/include/imagecode.php?act=verifycode&"+Math.random());
		});

	}


	$("#btn_post").bind("click", function(){ //提交
		$type = $('input[name="logintype"]:checked').val();
		if ($type == "" || typeof($type) == 'undefined') {
			$type = 1;
		}
		
		$(this).html("登录中...");
		if ($('input[type="text"]').blur() && $('input[type="password"]').blur() || true) {
			$bool = $("html").find("._span_error").not($("._span_error:hidden"));
			if (typeof($bool.html()) == "undefined"){
				
				$loginname = $("#loginname").val(); //邮箱、用户名
				$mobile = $("#mobile").val(); //手机号码
				$password = $("#password").val(); //密码
				if ($("#checkcode").length > 0) {
					$logincode = $("#checkcode").val(); //验证码
				}
				else {
					$logincode = "";
				}

				$.ajax({
					type: "POST",
					url: _ROOT_PATH + "index.php?c=passport",
					cache: false,
					data: {a:"loginpost", logintype:$type, loginname:$loginname, mobile:$mobile, password:$password, checkcode:$logincode, r:get_rndnum(8)},
					dataType: "json",
					success: function(data) {
						$json = eval(data);
						$response = $json.response;
						$result = $json.result;
						if ($response == "1") { //成功，返回页面
							$forward = "";
							if ($forward.length == 0) {
								if ($result == "1") { //个人
									$forward = _ROOT_PATH+"index.php?m=user";
								}
								else { //商家
									$forward = _ROOT_PATH+"index.php?m=shop";
								}
							}
							window.location.href = $forward;					
						}
						else if ($response == "2") { //整合OElove 2015.12.22
							window.top.location.href = $result;
						}
						else {
							if ($result.length > 0) {
								ToastShow($result);
							}
							else {
								ToastShow("登录失败，请检查帐号、密码是否正确");
							}
						}
						$("#btn_post").html("登录");
					},
					error: function() {
						$("#btn_post").html("登录");
						ToastShow("登录失败，请检查网络！");
					}
				});

			}
			else{
				$("#btn_post").html("登录");
				//$top = $bool.scrollTop();
				//$("html, body").animate({'scrollTop':$top}, 200);
			}
		}

	});

});