function checkField(name){
	var valid = true;
	switch(name){
	case "name":
		if(($("#name").val().split(' ').join('')).length == 0){
			$("#nameStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">此欄必須輸入</span>');
			valid = false;
		}
		else{
			$("#nameStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
	break;
	case "gender":
		/*
		if($("input[@name=gender][@checked]").val() == undefined){
			$("#genderStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">此欄必須輸入</span>');
			valid = false;
		}
		else{
			$("#genderStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
		*/
	break;
	case "hkid":
		if(!checkHKID($("#hkid").val())){
			$("#hkidStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">身份証號碼不正確</span>');
			valid = false;
		}
		else{
			$("#hkidStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
	break;
	case "birthday":
		if($("#birthday_y").val() != "" || $("#birthday_m").val() != "" || $("#birthday_d").val() != ""){
			if(!isValidDate($("#birthday_y").val(), $("#birthday_m").val(), $("#birthday_d").val())){
				$("#birthdayStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">日期不正確</span>');
				valid = false;
			}
			else{
				$("#birthdayStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
	break;
	case "phone":
		if($("#phone").val().length != 0){
		       if(!checkPhone($("#phone").val())){
				$("#phoneStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">電話格式不正確</span>');
				valid = false;
			}
			else{
				$("#phoneStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
		else{
			$("#phoneStatus").html('');
		}
	break;
	case "weight":
		if($("#weight").val().length != 0){
			if(isNaN($("#weight").val())){
				$("#weightStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">必須為數字</span>');
			}
			else{
				$("#weightStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
		else{
			$("#weightStatus").html('');
		}
	break;
	case "weightDate":
		if($("#weight").val().length != 0){
			if(!checkDate($("#weightDate").val())){
				$("#weightStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">日期不正確</span>');
			}
			else{
				checkField('weight');
//				$("#weightStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
		else{
			$("#weightStatus").html('');
		}
	break;
	case "height":
		if($("#height").val().length != 0){
			if(isNaN($("#height").val())){
				$("#heightStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">必須為數字</span>');
			}
			else{
				$("#heightStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
		else{
			$("#heightStatus").html('');
		}
	break;
	case "heightDate":
		if($("#height").val().length != 0){
			if(!checkDate($("#heightDate").val())){
				checkField('weight');
//				$("#heightStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">日期不正確</span>');
			}
			else{
				$("#heightStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
			}
		}
		else{
			$("#weightStatus").html('');
		}
	break;
	case "login":
		if($("#login").val().length < 8 || !checkInput($("#login").val())){
			$("#loginStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">必須為至少8個位數字或英文字母</span>');
			valid = false;
		}
		else{
			$.ajax({
				type: "POST",
				url: "members/checkLogin",
				data: "&id="+$("#login").val(),
				dataType: "xml",
				success: function(r){
					if($('status', r).text() == "success"){
						$("#loginStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
					}
					else {
						$("#loginStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">登入名稱已被使用</span>');
						valid = false;
					}
				},
				async: false
			});
		}
	break;
	case "email":
		if(!checkEmail($("#email").val())){
			$("#emailStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">電郵地址格式不正確</span>');
			valid = false;
		}
		else{
			$.ajax({
				type: "POST",
				url: "members/checkEmail",
				data: "&email="+$("#email").val(),
				dataType: "xml",
				success: function(r){
					if($('status', r).text() == "success"){
						$("#emailStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
					}
					else {
						$("#emailStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">電郵已被登記</span>');
						valid = false;
					}
				},
				async: false
			});
		}
	break;
	case "password":
		if($("#password").val().length < 8 || !checkInput($("#password").val())){
			$("#passwordStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">密碼格式不正確</span>');
			valid = false;
		}
		else{
			$("#passwordStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
	case "passwordConfirm":
		if($("#password").val().length < 8 || !checkInput($("#password").val()) || $("#password").val() != $("#passwordConfirm").val()){
			$("#passwordConfirmStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">密碼確認不正確</span>');
			valid = false;
		}
		else{
			$("#passwordConfirmStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
	break;
	case "accept":
		if(!$("#accept").attr("checked")){
			$("#acceptStatus").html('<img src="'+base+'img/reg_images/reg_cross.gif" width="16" height="16"> <span class="red_small">請剔選我同意用戶協議資料</span>');
			valid = false;
		}
		else{
			$("#acceptStatus").html('<img src="'+base+'img/reg_images/reg_tick.gif" width="16" height="16">');
		}
	break;
	}
	return valid;
}
function clearStatus(name){
	switch(name){
	case "name":
		$("#nameStatus").html('');
	break;
	case "gender":
		$("#genderStatus").html('');
	break;
	case "hkid":
		$("#hkidStatus").html('');
	break;
	case "birthday":
		$("#birthdayStatus").html('');
	break;
	case "phone":
		$("#phoneStatus").html('');
	break;
	case "weight":
	case "weightDate":
		$("#weightStatus").html('');
	break;
	case "height":
	case "heightDate":
		$("#heightStatus").html('');
	break;
	case "login":
		$("#loginStatus").html('');
	break;
	case "email":
		$("#emailStatus").html('');
	break;
	case "password":
		$("#passwordStatus").html('');
		$("#passwordConfirmStatus").html('');
	break;
	case "passwordConfirm":
		$("#passwordConfirmStatus").html('');
	break;
	case "accept":
		$("#acceptStatus").html('');
	break;
	}
}
function resetForm(){
	form = document.getElementById("MemberAddForm");
	form.reset();
}
function submitForm(){
	if(checkForm()){
		form = document.getElementById("MemberAddForm");
		form.submit();
		/*
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "&a=doReg&account="+$("#account").val()+"&password="+$("#password").val()+"&nickname="+$("#nickname").val()+"&fullName="+$("#fullName").val()+"&title="+$("#title").val()+"&gender="+$("input[@name=gender][@checked]").val()+"&birthday="+$("#birthday_y").val()+"/"+$("#birthday_m").val()+"/"+$("#birthday_d").val()+"&salary="+$("#salary").val()+"&state="+$("#state").val()+"&phone1="+$("#phone1").val()+"&phone2="+$("#phone2").val()+"&sendNewsletter="+(($("#sendNewsletter").attr('checked')==true)?"Yes":"No")+"&referral1="+$("input[@name=referral1][@checked]").val()+"&referral2="+$("#referral2").val()+"&email="+$("#email").val()+"&join="+$("input[@name=join][@checked]").val()+"&booking="+$("input[@name=booking][@checked]").val()+"&option1="+$("#option1").val()+"&option2="+$("#option2").val()+"&weight="+$("#weight").val()+"&height="+$("#height").val()+"&weightDate="+$("#weightDate").val()+"&heightDate="+$("#heightDate").val()+"&weightUnit="+$("input[@name=weightUnit][@checked]").val()+"&heightUnit="+$("input[@name=heightUnit][@checked]").val(),
			dataType: "xml",
			success: function(r){
				if($('status', r).text() == "success"){
					alert("恭喜你已成功登記及登入。\n你可以立即進入飲食記錄表，輸入你的進食記錄。\n又或者進入健康記錄表檢視你的卡路里記錄、BMI記錄或重量記錄。\n你亦可進入訊息記錄表查閱營養師給你的飲食建議或提出問題。");
					window.location = "./?a=entry";
				}
				else {
					alert("請正確選擇/輸入:\n" + $('msg', r).text());
				}
			}
		});
		*/
	}
}
function checkForm(){
	var valid = true;
	checkFieldsName = Array("name", "gender", "hkid", "birthday", "phone", "weight", "weightDate", "height", "heightDate", "login", "email", "password", "passwordConfirm", "accept");
	for(i=0;i<checkFieldsName.length;i++){
		if(!checkField(checkFieldsName[i])){
			valid = false;
		}
	}
	return valid;

	/*
	msg = "";

	obj1_1 = document.getElementById("join_1");
	obj1_2 = document.getElementById("join_2");
	obj2_1 = document.getElementById("booking_1");
	obj2_2 = document.getElementById("booking_2");
	obj2_3 = document.getElementById("booking_3");
	obj3 = document.getElementById("option1");
	obj4 = document.getElementById("option2");
	obj5 = document.getElementById("nickname");
	obj6 = document.getElementById("fullName");
	obj7_1 = document.getElementById("birthday_y");
	obj7_2 = document.getElementById("birthday_m");
	obj7_3 = document.getElementById("birthday_d");
	obj17 = document.getElementById("phone1");
	obj18 = document.getElementById("phone2");
	obj8 = document.getElementById("weight");
	obj9 = document.getElementById("weightDate");
	obj10 = document.getElementById("height");
	obj11 = document.getElementById("heightDate");
	obj16 = document.getElementById("account");
	obj12 = document.getElementById("email");
	obj13 = document.getElementById("password");
	obj14 = document.getElementById("passwordConfirm");
	obj15 = document.getElementById("accept");

	if(!obj1_1.checked && !obj1_2.checked){
		msg += "請選擇是否參加『桂格至fit有「營」挑戰』。\n";
		valid = false;
	}
	if(obj1_1.checked && (!obj2_1.checked && !obj2_2.checked && !obj2_3.checked)){
		msg += "請選擇參加講座或約見營養師\n";
		valid = false;
	}
	if($("select[@name=option1]>option").length > 1 && (obj1_1.checked && obj2_1.checked && obj3.selectedIndex == obj4.selectedIndex)){
		msg += "參加講座 - 第一及第二選擇必須不同\n";
		valid = false;
	}
	if(obj5.value.length == 0){
		msg += "稱呼(討論區用)\n";
		valid = false;
	}
	if(obj6.value.length == 0 || !checkFullName(obj6.value)){
		msg += "全名\n";
		valid = false;
	}
	if(!isValidDate(obj7_1.value, obj7_2.value, obj7_3.value)){
		msg += "生日格式不正確\n";
		valid = false;
	}
	else if(!aboveEighteen(obj7_1.value, obj7_2.value, obj7_3.value)){
		msg += "生日 - 參加者必須年滿18歲或以上\n";
		valid = false;
	}
	if(($("#_ctl1_PhoneLabel").attr('class') == 'red_small' && (obj17.value.length == 0 && obj18.value.length == 0)) || (obj17.value.length != 0 && (obj17.value.length != 8 || isNaN(obj17.value))) || obj18.value.length != 0 && (obj18.value.length != 8 || isNaN(obj18.value))){
		msg += "電話(香港地區適用) \n";
		valid = false;
	}
	if(obj1_2.checked && (obj8.value.length == 0 || isNaN(obj8.value) || obj8.value <= 0)){
		msg += "體重\n";
		valid = false;
	}
	if(obj1_2.checked && !checkDate(obj9.value)){
		msg += "量度日期(體重)\n";
		valid = false;
	}
	if(obj1_2.checked && (obj10.value.length == 0 || isNaN(obj10.value) || obj10.value <= 0)){
		msg += "身高\n";
		valid = false;
	}
	if(obj1_2.checked && !checkDate(obj11.value)){
		msg += "量度日期(身高)\n";
		valid = false;
	}
	if(obj16.value.length < 8 || !checkInput(obj16.value)){
		msg += "登入名稱(必須為至少8個位數字或英文字母)\n";
		valid = false;
	}

	if(!checkEmail(obj12.value)){
		msg += "電郵地址格式不正確\n";
		valid = false;
	}
	if(obj13.value.length < 8){
		msg += "密碼\n";
		valid = false;
	}
	else if(!checkInput(obj13.value) || obj13.value != obj14.value){
		msg += "再輸入密碼不正確\n";
		valid = false;
	}
	if(!obj15.checked){
		msg += "請剔選我同意用戶協議資料\n";
		valid = false;
	}
	if(!valid){
		msg = "請正確選擇/輸入:\n" + msg;
		alert(msg);
	}

	return valid;
	*/
}
function checkFullName(text){
	var filter = /^([a-zA-Z ]|[\u4E00-\u9FA5])+$/;
	return filter.test(text);
}
function checkInput(text){
	var filter = /^([a-zA-Z0-9_\-])+$/;
	return filter.test(text);
}
function checkHKID(text) {
	var filter = /^([a-zA-Z]{1,2})([0-9]{4})$/;
	return filter.test(text);
}
function checkDate(text) {
	var filter = /^([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})$/;
	if(filter.test(text)){
		var t = text.split("/");
		var today = new Date();
		if(t[0] <= today.getFullYear()){
			return isValidDate(t[0], t[1], t[2]);
		}
	}
	return false;
}
function checkPhone(text) {
	var filter = /^([0-9]{8})$/;
	return filter.test(text);
}
function checkEmail(text) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(text);
}
function isValidDate(year, month, day){
	var dt = new Date(year, month-1, day);
	if (dt.getMonth() != month-1) {
		return false;
	}
	return true;
}
function aboveEighteen(year, month, day){
	var today = new Date();
	var target = new Date(today.getFullYear() - 18, today.getMonth(), today.getDate());
	var userDate = new Date(year, month - 1, day);
	return (userDate < target);
}
function checkWeightUnit(obj){
	obj1 = document.getElementById('weight_unit_label')
	if(obj.value == "kg"){
		obj1.innerHTML = "公斤";
	}
	else{
		obj1.innerHTML = "磅";
	}
}
function checkHeightUnit(obj){
	obj1 = document.getElementById('height_unit_label1')
	if(obj.value == "cm"){
		obj1.innerHTML = "厘米";
	}
	else{
		obj1.innerHTML = "寸";
	}
}
function updateTitle(){
	if($("input[@name=gender][@checked]").val() == "Female"){
		$("#title>option").eq(0).attr("selected", true);
	}
	else{
		$("#title>option").eq(2).attr("selected", true);
	}
}
function updateGender(){
	if($("select[@name=title]").val() == "Miss" || $("select[@name=title]").val() == "Mrs"){
		$("#gender_1").attr("checked", "checked");
	}
	else{
		$("#gender_2").attr("checked", "checked");
	}
}
function submitOldForm(){
	form = document.getElementById("MemberOldForm");
	form.submit();
		/*
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "&a=doReg&account="+$("#account").val()+"&password="+$("#password").val()+"&nickname="+$("#nickname").val()+"&fullName="+$("#fullName").val()+"&title="+$("#title").val()+"&gender="+$("input[@name=gender][@checked]").val()+"&birthday="+$("#birthday_y").val()+"/"+$("#birthday_m").val()+"/"+$("#birthday_d").val()+"&salary="+$("#salary").val()+"&state="+$("#state").val()+"&phone1="+$("#phone1").val()+"&phone2="+$("#phone2").val()+"&sendNewsletter="+(($("#sendNewsletter").attr('checked')==true)?"Yes":"No")+"&referral1="+$("input[@name=referral1][@checked]").val()+"&referral2="+$("#referral2").val()+"&email="+$("#email").val()+"&join="+$("input[@name=join][@checked]").val()+"&booking="+$("input[@name=booking][@checked]").val()+"&option1="+$("#option1").val()+"&option2="+$("#option2").val()+"&weight="+$("#weight").val()+"&height="+$("#height").val()+"&weightDate="+$("#weightDate").val()+"&heightDate="+$("#heightDate").val()+"&weightUnit="+$("input[@name=weightUnit][@checked]").val()+"&heightUnit="+$("input[@name=heightUnit][@checked]").val(),
			dataType: "xml",
			success: function(r){
				if($('status', r).text() == "success"){
					alert("恭喜你已成功登記及登入。\n你可以立即進入飲食記錄表，輸入你的進食記錄。\n又或者進入健康記錄表檢視你的卡路里記錄、BMI記錄或重量記錄。\n你亦可進入訊息記錄表查閱營養師給你的飲食建議或提出問題。");
					window.location = "./?a=entry";
				}
				else {
					alert("請正確選擇/輸入:\n" + $('msg', r).text());
				}
			}
		});
		*/
}

$(document).ready(function(){
//	updateGender();
});
