//JQUERY页面加载执行部分
$(document).ready(function() {
	
	getNations();	//提取国家信息
	
	/**
	 * 从后台提取国家信息
	 */
	function getNations() {
		var url = 'GetAllFndNations.action';
		var params = {};
		jQuery.post(url, params, fillNationsCb, 'json');
	}
	
	/**
	 * 向下拉框填充国家信息
	 * @param {Object} data
	 */
	function fillNationsCb(data) {
		for (var i = 0; i < data.nationList.length; i++) {
			$("#nation").append("<option value=\"" + data.nationList[i].nation_code + "\">" + data.nationList[i].name_cn + "</option>");
		}
	}
	
    //提交注册的表单
    $('#userForm').ajaxForm({
        dataType:		'json',
        beforeSubmit:	showWaiting,
        success:		processRegistReturn,
        timeout:		120000
    });
});

function changeValidateCode(obj) {
	//获取当前的时间作为参数，无具体意义   
	var timenow = new Date().getTime();
	//每次请求需要一个不同的参数，否则可能会返回同样的验证码   
	//这和浏览器的缓存机制有关系，也可以把页面设置为不缓存，这样就不用这个参数了。   
	obj.src = "rand.action?d=" + timenow;
}

/**
 * 检查注册信息是否符合要求
 * @return {BOOLEAN} 
 */
function checkRegisterInfo() {
	var pass = true;
	var focused = false;
	if (document.getElementById("userName").value.trim() == '') {
		$('#userName').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#userName").focus();
			focused = true;
		}
	} 
	if (document.getElementById("userName").value.length < 6) {
		$('#userName').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#userName").focus();
			focused = true;
		}
	} 
	if (document.getElementById("pwd").value.trim() == '') {
		$('#pwd').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#pwd").focus();
			focused = true;
		}
	} 
	if (document.getElementById("pwd").value.length < 6) {
		$('#pwd').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#pwd").focus();
			focused = true;
		}
	} 
	if (document.getElementById("mobile").value.trim() == '') {
		$('#mobile').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#mobile").focus();
			focused = true;
		}
	}
	if (document.getElementById("email").value == '') {
		$('#email').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#email").focus();
			focused = true;
		}
	} 
	//电子邮件格式
	if (!checkIsEmail(document.getElementById("email").value)) {
		$('#email').css("border", "1px solid #FF0000");
		pass = false;
		if (focused == false) {
			$("#email").focus();
			focused = true;
		}
	} 
	
	if (pass) {
		if (document.getElementById("pwd").value != document.getElementById("rePwd").value) {
			alert('确认密码同密码不符合！');
			pass = false;
		}
		
		//输入验证码
		if (document.getElementById("addOnCode").value == '') {
			$('#addOnCode').css("border", "1px solid #FF0000");
			pass = false;
			if (focused == false) {
				$("#addOnCode").focus();
				focused = true;
			}
		}
	}

	return pass;
}

function regist() {
	if ($('#agree').attr("checked") == true) {
		if (checkRegisterInfo()) {
			//$('#userForm').attr('action', 'AddCustomer.action');
			$('#userForm').submit();
		}
	}
}

/**
 * 显示等待画面
 * @return
 */
function showWaiting() {
	$('#registerContent').ajaxloader('show', 'tata-ajax-loader-img');
}

/**
 * 处理注册返回
 * @param data	返回的JSON数据
 * @return		无
 */
function processRegistReturn(data) {
	$('#registerContent').ajaxloader('hide');
	
	if (data.success == 'success') {
		alert('用户注册成功！' + '\r\n您的会员卡号：' + data.memberCard);
		window.location.href = "../main/InitHomepage.action";
	} else if (data.addOnCode == 'error') {
		alert('验证码不正确！');
		document.getElementById("pwd").value = '';
		document.getElementById("rePwd").value = '';
	} else if (data.success != 'success') {
		alert(data.success);
		document.getElementById("pwd").value = '';
		document.getElementById("rePwd").value = '';
	} else {
		alert('系统错误，请稍后再试！');
		document.getElementById("pwd").value = '';
		document.getElementById("rePwd").value = '';
	}
}

