document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 13) {
        register();
    } 
}
/**
 * 输入验证
 */
$(function(){
	//用户名
	$("input[name='username']").bind("blur change",function(){
		var username=$("input[name='username']").val(); 
		if(!username){
			$("#username_hc").css("color","#ff0000");
			$("#username_hc").html('会员名不能为空');
		}else if(!username.match(/^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9])*$/)||username.length<6||username.length>18){
			$("#username_hc").css("color","#ff0000");
			$("#username_hc").html('请输入6-16位字符,可包含汉字、字母、数字');
		}else{
			$("#username_hc").css("color","#a6e506");
			$("#username_hc").html('通过验证');
		}
	});
	//手机号
	$("input[name='mobile']").bind("blur change",function(){
		var mobile=$("input[name='mobile']").val();
		if(mobile==''){
			$("#mobile_hc").css("color","#ff0000");
			$("#mobile_hc").html('手机号不能为空');
		}else if(!$.checkMobile(mobile)){
			$("#mobile_hc").css("color","#ff0000");
			$("#mobile_hc").html('手机号格式不正确');
		}else{
			$("#mobile_hc").css("color","#a6e506");
			$("#mobile_hc").html('通过验证');
		}
	});
	//短信验证码
	$("input[name='mobilecode']").bind("blur change",function(){
		var mobilecode=$("input[name='mobilecode']").val();
		if(mobilecode==''){
			$("#mobilecode_hc").css("color","#ff0000");
			$("#mobilecode_hc").html('短信验证码不能为空');
		}else{
			$("#mobilecode_hc").html('');
		}
	});
	//密码验证
	$("input[name='u_password']").bind("blur change",function(){
		var psw=$("input[name='u_password']").val();
		if(psw==''){
			$("#u_password_hc").css("color","#ff0000");
			$("#u_password_hc").html('密码不能为空');
		}else if(!psw.match(/^[\w]{6,12}$/)){
			$("#u_password_hc").css("color","#ff0000");
			$("#u_password_hc").html('请输入6-16位字符,可包含字母、数字、下划线');
		}else{
			$("#u_password_hc").css("color","#a6e506");
			$("#u_password_hc").html('通过验证');
		}
	});
	//确认密码验证
	$("input[name='u_repassword']").bind("blur change",function(){
		var resw=$("input[name='u_repassword']").val();
		var fistpsw=$("input[name='u_password']").val();
		if(!resw){
			$("#u_repassword_hc").css("color","#ff0000");
			$("#u_repassword_hc").html('确认密码不能为空');
		}else if(fistpsw != resw){
			$("#u_repassword_hc").css("color","#ff0000");
			$("#u_repassword_hc").html('两次密码不一致');
		}else{
			$("#u_repassword_hc").css("color","#a6e506");
			$("#u_repassword_hc").html('通过验证');
		}
	});
	//公司名称
	$("input[name='name']").bind("blur change",function(){
		var name=$("input[name='name']").val();
		if(name==''){
			$("#name_hc").css("color","#ff0000");
			$("#name_hc").html('公司名称不能为空');
		}else if(!name.match(/\S/)){
			$("#name_hc").css("color","#ff0000");
			$("#name_hc").html('请填写正确的公司名称');
		}else{
			$("#name_hc").css("color","#a6e506");
			$("#name_hc").html('通过验证');
		}
	});
	//公司详细地址
	$("input[name='address']").bind("blur change",function(){
		var address=$("input[name='address']").val();
		if(address==''){
			$("#address_hc").css("color","#ff0000");
			$("#address_hc").html('详细地址不能为空');
		}else if(!address.match(/\S/)){
			$("#address_hc").css("color","#ff0000");
			$("#address_hc").html('请填写正确的详细地址');
		}else{
			$("#address_hc").css("color","#a6e506");
			$("#address_hc").html('通过验证');
		}
	});
	//公司电话
	$("input[name='phone']").bind("blur change",function(){
		var phone=$("input[name='phone']").val();
		if(phone==''){
			$("#phone_hc").css("color","#ff0000");
			$("#phone_hc").html('电话不能为空');
		}else if(!phone.match(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)){
			$("#phone_hc").css("color","#ff0000");
			$("#phone_hc").html('格式不正确');
		}else{
			$("#phone_hc").css("color","#a6e506");
			$("#phone_hc").html('通过验证');
		}
	});
	//员工总数
	$("input[name='employee_count']").bind("blur change",function(){
		var employee_count=$("input[name='employee_count']").val();
		if(employee_count==''){
			$("#employee_count_hc").css("color","#ff0000");
			$("#employee_count_hc").html('员工总数不能为空');
		}else if(!employee_count.match(/\d/)){
			$("#employee_count_hc").css("color","#ff0000");
			$("#employee_count_hc").html('请填写正确数量');
		}else{
			$("#employee_count_hc").css("color","#a6e506");
			$("#employee_count_hc").html('通过验证');
		}
	});
	//注册资金
	$("input[name='registered_capital']").bind("blur change",function(){
		var registered_capital=$("input[name='registered_capital']").val();
		if(registered_capital==''){
			$("#registered_capital_hc").css("color","#ff0000");
			$("#registered_capital_hc").html('注册资金不能为空');
		}else if(!registered_capital.match(/\d/)){
			$("#registered_capital_hc").css("color","#ff0000");
			$("#registered_capital_hc").html('请填写正确的注册资金');
		}else{
			$("#registered_capital_hc").css("color","#a6e506");
			$("#registered_capital_hc").html('通过验证');
		}
	});
	//联系人姓名
	$("input[name='contacts']").bind("blur change",function(){
		var contacts=$("input[name='contacts']").val();
		if(contacts==''){
			$("#contacts_hc").css("color","#ff0000");
			$("#contacts_hc").html('联系人姓名不能为空');
		}else if(!contacts.match(/\S/)){
			$("#contacts_hc").css("color","#ff0000");
			$("#contacts_hc").html('请填写正确的联系人姓名');
		}else{
			$("#contacts_hc").css("color","#a6e506");
			$("#contacts_hc").html('通过验证');
		}
	});
	//联系人电话
	$("input[name='contacts_phone']").bind("blur change",function(){
		var contacts_phone=$("input[name='contacts_phone']").val();
		if(contacts_phone==''){
			$("#contacts_phone_hc").css("color","#ff0000");
			$("#contacts_phone_hc").html('联系人电话不能为空');
		}else if(!$.checkMobile(contacts_phone)){
			$("#contacts_phone_hc").css("color","#ff0000");
			$("#contacts_phone_hc").html('请填写正确的联系人电话');
		}else{
			$("#contacts_phone_hc").css("color","#a6e506");
			$("#contacts_phone_hc").html('通过验证');
		}
	});
	//邮箱
	$("input[name='email']").bind("blur change",function(){
		var email=$("input[name='email']").val();
		if(email==''){
			$("#email_hc").css("color","#ff0000");
			$("#email_hc").html('邮箱不能为空');
		}else if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)){
			$("#email_hc").css("color","#ff0000");
			$("#email_hc").html('邮箱格式不正确');
		}else{
			$("#email_hc").css("color","#a6e506");
			$("#email_hc").html('通过验证');
		}
	});
	//营业执照号
	$("input[name='business_licence_number']").bind("blur change",function(){
		var business_licence_number=$("input[name='business_licence_number']").val();
		if(business_licence_number==''){
			$("#business_licence_number_hc").css("color","#ff0000");
			$("#business_licence_number_hc").html('营业执照号不能为空');
		}else if(!business_licence_number.match(/\d{15}/)){
			$("#business_licence_number_hc").css("color","#ff0000");
			$("#business_licence_number_hc").html('营业执照号格式不正确');
		}else{
			$("#business_licence_number_hc").css("color","#a6e506");
			$("#business_licence_number_hc").html('通过验证');
		}
	});
	//营业执照所在地
	$("input[name='licence_address']").bind("blur change",function(){
		var licence_address=$("input[name='licence_address']").val();
		if(licence_address==''){
			$("#licence_address_hc").css("color","#ff0000");
			$("#licence_address_hc").html('营业执照所在地不能为空');
		}else if(!licence_address.match(/\S/)){
			$("#licence_address_hc").css("color","#ff0000");
			$("#licence_address_hc").html('请填写正确的营业执照所在地');
		}else{
			$("#licence_address_hc").css("color","#a6e506");
			$("#licence_address_hc").html('通过验证');
		}
	});
	//营业执照有效期
	$("input[name='licence_start']").bind("blur change",function(){
		var licence_start=$("input[name='licence_start']").val();
		if(licence_start==''){
			$("#licence_time_hc").css("color","#ff0000");
			$("#licence_time_hc").html('营业执照有效期不能为空');
		}else{
			$("#licence_time_hc").html('');
		}
	});/*
	$("input[name='licence_end']").bind("blur change",function(){
		var licence_end=$("input[name='licence_end']").val();
		if(licence_end==''){
			$("#licence_time_hc").css("color","#ff0000");
			$("#licence_time_hc").html('营业执照有效期不能为空');
		}else{
			$("#licence_time_hc").html('');
		}
	});*/
	//法定经营范围
	$("textarea[name='business_sphere']").bind("blur change",function(){
		var business_sphere=$("textarea[name='business_sphere']").val();
		if(licence_start==''){
			$("#business_sphere_hc").css("color","#ff0000");
			$("#business_sphere_hc").html('法定经营范围不能为空');
		}else if(!business_sphere.match(/\S/)){
			$("#business_sphere_hc").css("color","#ff0000");
			$("#business_sphere_hc").html('请填写正确的法定经营范围');
		}else{
			$("#business_sphere_hc").css("color","#a6e506");
			$("#business_sphere_hc").html('通过验证');
		}
	});
	//组织机构代码
	$("input[name='organization_code']").bind("blur change",function(){
		var organization_code=$("input[name='organization_code']").val();
		if(organization_code==''){
			return true;
		}else if(!organization_code.match(/[a-zA-Z0-9]{8}-[a-zA-Z0-9]/)){
			$("#organization_code_hc").css("color","#ff0000");
			$("#organization_code_hc").html('组织机构代码格式不正确');
		}else{
			$("#organization_code_hc").css("color","#a6e506");
			$("#organization_code_hc").html('通过验证');
		}
	});
});
/**
 * 注册
 */
function register(){
	var check=check_register();
	if(!check){return false;}
	
	var query = new Object();
	query = $("form[name='regist']").serialize();
	$.ajax({
		type:"POST",
		url:APP+"Shop/Register/add_shop",
		data:query,
		success:function(data){
			if(data.status==1){
				$.showSuccess(data.info,0,'hc');
				window.location.href=data.url;
			}else{
				$.showErr(data.info,0,'hc');
			}
		}
	});
}
/**
 * 注册项验证
 */
function check_register(){
	//验证会员信息
	var is_user = $("#userid").val();
	if(is_user == ''){
		var username=$("input[name='username']").val();
		if(username==''){$.showErr('请填写会员名称',0,'hc');return false;}
		if(!username.match(/^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9])*$/)||username.length<6||username.length>18){$.showErr('请填写正确的会员名称',0,'hc');return false;}
		
		
		var mobile=$("input[name='mobile']").val();
		if(mobile==''){$.showErr('请填写手机号',0,'hc');return false;}
		if(!$.checkMobile(mobile)){$.showErr('手机号格式不正确',0,'hc');return false;}
		
		var mobilecode=$("input[name='mobilecode']").val();
		if(mobilecode==''){$.showErr('请填写手机验证码',0,'hc');return false;}
		
		var u_password=$("input[name='u_password']").val();
		if(u_password==''){$.showErr('请填写密码',0,'hc');return false;}
		if(!u_password.match(/^[\w]{6,12}$/)){$.showErr('密码格式错误',0,'hc');return false;}
		
		var u_repassword=$("input[name='u_repassword']").val();
		if(u_repassword==''){$.showErr('请填写确认密码',0,'hc');return false;}
		
		if(u_password != u_repassword){$.showErr('两次密码不一致',0,'hc');return false;}
	}
	//验证供货商信息
	var name=$("input[name='name']").val();
	if(name==''){$.showErr('请填写公司名称',0,'hc');return false;}
	if(!name.match(/\S/)){$.showErr('请填写正确的公司名称',0,'hc');return false;}
	
	var province = $("#province").val();
	if(province==''){$.showErr('请选择省',0,'hc');return false;}
	
	var city_id = $("#city_id").val();
	if(city_id==''){$.showErr('请选择市',0,'hc');return false;}
	
	var district = $("#district").val();
	if(district==''){$.showErr('请选择区县',0,'hc');return false;}
	
	var address=$("input[name='address']").val();
	if(address==''){$.showErr('请填写详细地址',0,'hc');return false;}
	if(!address.match(/\S/)){$.showErr('请填写正确的详细地址',0,'hc');return false;}
	
	var phone=$("input[name='phone']").val();
	if(phone==''){$.showErr('请填写公司电话',0,'hc');return false;}
	if(!phone.match(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/)){$.showErr('请填写正确的公司电话',0,'hc');return false;}
	
	var employee_count=$("input[name='employee_count']").val();
	if(employee_count==''){$.showErr('请填写员工总数',0,'hc');return false;}
	if(!employee_count.match(/\d/)){$.showErr('请填写正确的员工总数',0,'hc');return false;}
	
	var registered_capital=$("input[name='registered_capital']").val();
	if(registered_capital==''){$.showErr('请填写注册资金',0,'hc');return false;}
	if(!registered_capital.match(/\d/)){$.showErr('请填写正确的注册资金',0,'hc');return false;}
	
	var contacts=$("input[name='contacts']").val();
	if(contacts==''){$.showErr('请填写联系人姓名',0,'hc');return false;}
	if(!contacts.match(/\S/)){$.showErr('请填写正确的联系人姓名',0,'hc');return false;}
	
	var contacts_phone=$("input[name='contacts_phone']").val();
	if(contacts_phone==''){$.showErr('请填写联系电话',0,'hc');return false;}
	if(!$.checkMobile(contacts_phone)){$.showErr('请填写正确的联系电话',0,'hc');return false;}
	
	var email=$("input[name='email']").val();
	if(email==''){$.showErr('请填写电子邮箱',0,'hc');return false;}
	if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)){$.showErr('请填写正确的电子邮箱',0,'hc');return false;}
	
	var business_licence_number=$("input[name='business_licence_number']").val();
	if(business_licence_number==''){$.showErr('请填写营业执照号',0,'hc');return false;}
	if(!business_licence_number.match(/\d{15}/)){$.showErr('请填写正确的营业执照号',0,'hc');return false;}
	
	var licence_address=$("input[name='licence_address']").val();
	if(licence_address==''){$.showErr('请填写营业执照所在地',0,'hc');return false;}
	if(!licence_address.match(/\S/)){$.showErr('请填写正确的营业执照所在地',0,'hc');return false;}
	
	var licence_start = $("#licence_start").val();
	if(licence_start==''){$.showErr('请填写营业执照有效期',0,'hc');return false;}
	/*
	var licence_end = $("#licence_end").val();
	if(licence_end==''){$.showErr('请填写营业执照有效期',0,'hc');return false;}
	*/
	var business_sphere=$("textarea[name='business_sphere']").val();
	if(business_sphere==''){$.showErr('请填写法定经营范围',0,'hc');return false;}
	if(!business_sphere.match(/\S/)){$.showErr('请填写正确的法定经营范围',0,'hc');return false;}
	
	var cate_ids=$("input[name='cate_ids[]']").is(':checked');
	if(!cate_ids){$.showErr('请选择经营类目',0,'hc');return false;}
	
	var business_licence_img=$("input[name='business_licence_img']").val();
	if(business_licence_img==''){$.showErr('请上传营业执照电子版',0,'hc');return false;}
	
	var organization_code=$("input[name='organization_code']").val();
	if(organization_code !=''){
		if(!organization_code.match(/[a-zA-Z0-9]{8}-[a-zA-Z0-9]/)){$.showErr('组织机构代码格式不正确',0,'hc');return false;}
	}
	
	var agreement=$("input[name='input_apply_agreement']").is(':checked');
	if(!agreement){$.showErr('请同意用户协议',0,'hc');return false;}
	return true;
}
/**
 * 发送短信
 */
var InterValObj; //timer变量，控制时间
var curCount;//当前剩余秒数
function sendMessage() {
	var time = $("#true_time").val();
	var count = parseInt(time); //间隔函数，1秒执行
    curCount = count;
    var mobile=$('#phone').val();
    if(verify_mobile(mobile)==false) return false;
    var param="mobile=" + mobile + "&unique=" + 1 + "&type=" + 1;
	
	//开启注册短信验证
    var sms_reg_verify=$('#sms_reg_verify').val();
    if(sms_reg_verify) {
        var regverify = $('#regverify').val();
        param += "&regverify=" + regverify;
    }
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: APP+"Home/Ajax/sendsms",
        data:param,
        success:function (data){
        if(data.status==0){
			$("#verify_img").click();
            $.showErr(data.msg,0,'hc');return false;
        }else{
			$("#btnSendCode").attr("disabled", "true");
			$("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        }
    }
});
}
/**
 * timer处理函数
 */
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
		$("#btnSendCode").removeAttr("disabled");//启用按钮
        $("#btnSendCode").val("重新发送验证码");
    }else {
        curCount--;
        $("#btnSendCode").val(curCount + "s后重新获取");
    }
}
/**
 * 验证手机号
 */
function verify_mobile(phone){
    if(phone == ''){$.showErr('手机号不能为空',0,'hc');return false;}
    if(!$.checkMobile(phone)){ $.showErr('手机号格式不正确',0,'hc');return false;}
	return true;
}



