var flag = false;
var flagEm = false;
function noticePwd() {
	var pat = /^(\w){6,16}$/;
	var pwd = $("#password").val();
	if (pwd == "" || !pat.test(pwd)) {
		$("#pwdSpan").html("* 输入秘密错误!").css({"color" : "red"});
		return false;
	}
	$("#pwdSpan").html("*").css( { "color" : "blue" });
	return true;
}

function noticeRepwd() {
	var pwd = $("#password").val();
	var repwd = $("#repassword").val();
	if (repwd == "" || pwd != repwd) {
		$("#repwdSpan").html("* 输入重复密码错误!").css( {"color" : "red"});
		return false;
	}
	$("#repwdSpan").html("*").css( { "color" : "blue" });
	return true;
}

function checkName(chtype) {
	var profile_username_toolong = '用户名超过 15 个字符';
	var profile_username_tooshort = '用户名小于3个字符';
	var username = $.trim($("#username").val());
	if(username == '') {
		$("#result").html("用户名不能为空！").show("slow").css( {"color" : "red"});
		return;
	}
	var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
	if(unlen < 3 || unlen > 15) {
		var profile_msg = unlen < 3 ? profile_username_tooshort : profile_username_toolong;
		$("#result").html(profile_msg).show("slow").css( { "color" : "red" });
		return;
	}	
	$.ajax( {
		url : URL + '/checkName',
		type : 'POST',
		data : "username=" + username,
		dataType : 'json',
		//timeout : 1000,
		error : function() {
			//alert('Error loading XML document');
		},
		success : function(data) {
			var status = data["status"];
			var info = "* " + data["info"];
			if (status == 0) {
				$("#result").html(info).show("slow").css({ "color" : "red" });
			} else if (status == 1 && chtype == 'send') {
				$("#userReg").submit();
			} else {
				$("#result").html(info).show("slow").css({ "color" : "blue" });
			}
		}
	});
}
function noticeEmail() {
	var pat = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var em = $.trim($("#email").val());
	if (!pat.test(em)) {
		$("#emSpan").html("* 请输入正确的Email地址！").css({"color" : "red"});
		flagEm = false;
	} else {
		$.ajax( {
			url : URL + '/checkEmail',
			type : 'POST',
			data : "email=" + em,
			dataType : 'json',
			//timeout : 1000,
			error : function() {
				//alert('Error loading XML document');
			},
			success : function(data) {
				var status = data["status"];
				var info = "* " + data["info"];
				if (status == 0) {
					$("#emSpan").html(info).show("slow").css({"color" : "red"});
					flagEm = false;
				} else {
					$("#emSpan").html(info).show("slow").css({"color" : "blue"});
					flagEm = true;
				}
			}
		});
	}
}

function checkVerify() {
	var verify = $("#verify").val();
	if(verify!=""){
		$.ajax( {
			url : URL + '/checkVerify',
			type : 'POST',
			data : "verify=" + verify,
			dataType : 'json',
			//timeout : 1000,
			error : function() {
				//alert('Error loading XML document');
			},
			success : function(data) {
				var status = data["status"];
				var info = "* " + data["info"];
				if (status == 0) {
					$("#vspan").html(info).show("slow").css({"color" : "red"});
					flag = false;
				} else if (status == 1) {
					$("#vspan").html(info).show("slow").css({"color" : "blue"});
					flag = true;
				}
			}
		});
	}else{
		flag= false;
	}
}

function checkCbscope(){
	var cbs = $("#cbscope").val();
	if(cbs==""){
		$("#cbscopespan").html("* 请填写经营范围！").css({ "color" : "red" });
		return false;
	}else {
		$("#cbscopespan").html("*").css({"color" : "blue"});
		return true;
	}
}

function checkCname(){
	var cname = $("#cname").val();
	if(cname==""){
		$("#cnamespan").html("* 请填写公司名！").css({"color" : "red"});
		return false;
	}else{
		$("#cnamespan").html("*").css({"color" : "blue"});
		return true;
	}
	
}

function sendForm() {
	var flagpwd = noticePwd();
	var flagrep = noticeRepwd();
	noticeEmail();
	if (flagpwd && flagrep && flagEm) {
		checkName('send');
		return true;
	}
	return false;
}

function sendComForm() {
	var flagpwd = noticePwd();
	var flagrep = noticeRepwd();
	var flagCbs = checkCbscope();
	var flagCname = checkCname();
	noticeEmail();
	if (flagpwd && flagrep && flagEm && flagCbs && flagCname) {
		checkName('send');
		return true;
	}
	return false;
}
