/**
 * [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 $
*/
var oeAddEvent = function(el,e,callback){
    if (el.attachEvent) {//for IE
        el.attachEvent("on"+e,callback);
    } else{
        el.addEventListener(e,callback,false);
    }
}

//错误
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 selectRegType(type) {
	if (type == "1") { //邮箱注册
		$("#puts_email").show();
		if ($("#puts_emailcode").length > 0) {
			$("#puts_emailcode").show();
		}

		$("#puts_mobile").hide();
		if ($("#puts_mobilecode").length > 0) {
			$("#puts_mobilecode").hide();
		}

	}
	else { //手机注册
		$("#puts_email").hide();
		if ($("#puts_emailcode").length > 0) {
			$("#puts_emailcode").hide();
		}

		$("#puts_mobile").show();
		if ($("#puts_mobilecode").length > 0) {
			$("#puts_mobilecode").show();
		}
	}
}


//地区onchange回调函数
function oeCheckArea() {
	/*
	if ($("#area1").find("option:selected").val() < 1) {
		errorBack("tips_area", "请选择一级地区");
		return;
	}
	*/
	if ($("#area2").length > 0) {
		if ($("#area2").find("option:selected").val() < 1 ) {
			errorBack("tips_area", "至少选择二级地区");
		}
		else {
			okBack("tips_area", "&nbsp;");
		}
	}
	else {
		errorBack("tips_area", "至少选择二级地区");
	}
}


//LOGO
function oeCheckShopLogo() {
	if ($("input[name='logo']").val().length == 0) {
		errorBack("tips_logo", "请上传店铺LOGO，LOGO要与店铺名称相关，否则不予审核");
	}
	else {
		okBack("tips_logo", "&nbsp;");
	}
}

//证件
function oeCheckShopPaper() {

	if ($("input[name='paper']").val().length == 0) {
		errorBack("tips_paper", "请上传个人/企业证件，证件图片必须清晰，否则不予审核！");
	}
	else {
		okBack("tips_paper", "&nbsp;");
	}
}


//所有function的检测
function oeCheckFun() {
	oeCheckArea();
	oeCheckShopLogo();
	oeCheckShopPaper();
}

$(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;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkmobile",
			cache: false,
			data: {mobile:$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_mobile", "通过");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_mobile", $result);
					}
					else {
						errorBack("tips_mobile", "手机已被注册");
					}
					
				}

			},
			error: function() {
				//ToastShow("检测失败，请检查网络！");
			}
		});
		
	});
	
	//获取手机验证码
	$("#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=sendmobilecode",
			cache: false,
			data: {mobile:$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;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkemail",
			cache: false,
			data: {email:$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_email", "通过");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_email", $result);
					}
					else {
						errorBack("tips_email", "邮箱已被注册");
					}
					
				}

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

	//获取邮箱验证码
	$("#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=sendemailcode",
			cache: false,
			data: {email:$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("检测失败，请检查网络！");
			}
		});

	});



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


	$("#username").bind("blur", function(){ //会员名称
		$val_username = $(this).val();
		if ($val_username.length == 0) {
			errorBack("tips_username", "请输入用户名");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkusername",
			cache: false,
			data: {username:$val_username, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_username", "通过");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_username", $result);
					}
					else {
						errorBack("tips_username", "用户名已被注册");
					}
				}

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

	
	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", "正确");
					}
					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());
		});

	}


	$("#shopname").bind("blur", function(){ //店铺名称
		$val_name = $(this).val();
		if ($val_name.length == 0) {
			errorBack("tips_shopname", "请输入店铺名称");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=checkshopname",
			cache: false,
			data: {shopname:$val_name, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					okBack("tips_shopname", "&nbsp;");
				}
				else {
					if ($result.length > 0) {
						errorBack("tips_shopname", $result);
					}
					else {
						errorBack("tips_shopname", "店铺名已被注册");
					}
				}

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


	$("#shoptype").bind("change", function(){ //商家类型
		if ($(this).find("option:selected").val() < 1) {
			errorBack("tips_shoptype", "请选择商家类型");
		}
		else {
			okBack("tips_shoptype", "&nbsp;");
		}
	});

	$("#catid").bind("change", function(){ //店铺分类
		if ($(this).find("option:selected").val() < 1) {
			errorBack("tips_catid", "请选择店铺分类");
		}
		else {
			okBack("tips_catid", "&nbsp;");
		}
	});

	$("#address").bind("blur", function(){
		if ($(this).val().length == 0) {
			errorBack("tips_address", "请填写详细地址");
		}
		else {
			okBack("tips_address", "&nbsp;");
		}
	});

	$("#phone").bind("blur", function(){
		if ($(this).val().length == 0) {
			errorBack("tips_phone", "请填写联系电话");
		}
		else {
			okBack("tips_phone", "&nbsp;");
		}
	});

	$("#idnumber").bind("blur", function(){
		if ($(this).val().length == 0) {
			errorBack("tips_idnumber", "请填写证件号码");
		}
		else {
			okBack("tips_idnumber", "&nbsp;");
		}
	});
	

	$("#intro").bind("blur", function(){
		if ($(this).val().length == 0) {
			errorBack("tips_intro", "请填写店铺简介");
		}
		else {
			okBack("tips_intro", "&nbsp;");
		}
	});

	$("#btn_post").bind("click", function(){ //提交
		if ($('input[type="text"]').blur() && $("#intro").blur() && $('input[type="password"]').blur() && $("#shoptype").change() && $("#catid").change() && oeCheckFun() || true) {
			$bool = $("#myform").find("._span_error").not($("._span_error:hidden"));
			if (typeof($bool.html()) == "undefined"){
				$("#myform").submit();
			}
			else{
				$top = $bool.scrollTop();
				$("html, body").animate({'scrollTop':$top}, 200);
			}
		}

	});

});