$(document).ready(function(){
	var emailok = false;
	var userok = false;		
	var myForm = $("#myform"), username_t = $("#user_name_t"), email_t = $("#email_t"), emailInfo = $("#emailInfo") ;
	var pass_t = $("#pass_t"), month_t = $("#month_t"), day_t = $("#day_t"), year_t = $("#year_t"), userInfo = $("#userInfo");
	var fullname = $("#fullname"), lastname = $("#lastname"), city = $("#city");
	var str = '!@#$%^&*()+=.<->?/|\{}[],฿ๆฯ_';
	var strThai = 'กุขูคิฅีฆึงืจเไฉำแชะซัฌํญ๊ฎาฏ๋ฐ่ฑ้ฒ็ณดโตถฺท์ธนฦบปผฝพฟภมยรลวศษสหอฬฮ';		
		
		
	myForm.submit(function(){
		var keyOK = 0;
		var keyThaiOK = 0;
		for (i=0;i<str.length;i++){
			if(username_t.attr("value").indexOf(str.charAt(i))!= -1) keyOK = 1;
		}
		for (i=0;i<strThai.length;i++){			
			if(username_t.attr("value").indexOf(strThai.charAt(i))!= -1) keyThaiOK = 1;				
		}
		if (fullname.attr("value") == ""){
			jAlert('error', 'กรุณากรอกชื่อด้วยค่ะ', 'Error Dialog');
			fullname.focus();
			return false;
		}
		if (lastname.attr("value") == ""){
			jAlert('error', 'กรุณากรอกนามสกุลด้วยค่ะ', 'Error Dialog');
			lastname.focus();
			return false;
		}
		if(username_t.attr("value") == ""){
			jAlert('error', 'กรุณากรอกชื่อผู้ใช้งานด้วยค่ะ', 'Error Dialog');
			userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			username_t.focus();
			return false;
		}else if((username_t.attr("value").length < 6 ) || (username_t.attr("value").length > 16)){
			jAlert('error', 'ชื่อผู้ใช้งานต้องมีตั้งแต่ 6-16 ตัวอักษรนะคะ', 'Error Dialog');
			userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			username_t.focus();
			return false;
		}else if(keyOK == 1 || username_t.attr("value").indexOf("\"")!= -1|| username_t.attr("value").indexOf("'")!= -1 || username_t.attr("value").indexOf("::")!= -1){
			jAlert('error', 'ชื่อผู้ใช้งานต้องเป็นตัว a-z, A-Z, 0-9 เท่านั้นค่ะ', 'Error Dialog');
			userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			username_t.focus();           
			return false;
		}else if (keyThaiOK == 1){
			jAlert('error', 'ชื่อผู้ใช้งานต้องเป็นตัว a-z, A-Z, 0-9 เท่านั้นค่ะ', 'Error Dialog');	
			userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');			
			username_t.focus();           
			return false;
		}else if (username_t.attr("value").indexOf(" ")!= -1){
			jAlert('error', 'ชื่อผู้ใช้งานไม่ถูกต้องค่ะ', 'Error Dialog');
			userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			username_t.focus();           
			return false;
		}
		if(!userok){
			username_t.attr("value","");
			username_t.focus();
			return false;
		}
		if(email_t.attr("value") == ""){
			jAlert('error', 'กรุณากรอกอีเมล์ด้วยค่ะ', 'Error Dialog');
			emailInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			email_t.focus();
			return false;
		}
		if(!emailok){
			email_t.attr("value","");
			email_t.focus();
			return false;
		}
		if (pass_t.attr("value") == ""){
			jAlert('error', 'กรุณากรอกพาสเวิร์ดด้วยค่ะ', 'Error Dialog');
			pass_t.focus();
			return false;
		}else if (pass_t.attr("value").length < 4 ){
			jAlert('error', 'พาสเวิร์ดต้องมากว่า 4 ตัวนะคะ', 'Error Dialog');
			pass_t.focus();
			return false;
		}	
		if(!$("#sex_m").attr("checked") && !$("#sex_f").attr("checked")){
			jAlert('error', 'กรุณาระบุเพศด้วยค่ะ', 'Error Dialog');	
			return false;
		}
				
		if (month_t.attr("value") == 0 || day_t.attr("value") == 0 || year_t.attr("value") == 0){
			jAlert('error', 'กรุณาระบุวันเกิดด้วยค่ะ', 'Error Dialog');	
			return false;
		}		
		
		if (city.attr("value") == ""){
			jAlert('error', 'กรุณากรอกจังหวัดด้วยค่ะ', 'Error Dialog');
			city.focus();
			return false;
		}
		if ($("#school").val() == ''){
			jAlert('error', 'กรุณากรอกชื่อโรงเรียนด้วยค่ะ', 'Error Dialog');
			$("#school").focus();
			return false;
		}
		if ($("#mobile").val() == ''){
			jAlert('error', 'กรุณากรอกเบอร์โทรศัพท์ด้วยค่ะ', 'Error Dialog');
			$("#mobile").focus();
			return false;
		}
			
	});
		
	username_t.blur(function(){
	$.ajax({
		type: "POST",
		data: "username_t="+$(this).attr("value"),
		url: "check_user.php",
		beforeSend: function(){
			userInfo.html('<img src="images/loading.gif" width="16" height="16" />');				
		},
		success: function(data){
					
			if(data == "0"){
				userok = false;
				userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');		
			}else if(data == "1"){
				userok = false;					
				userInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
				jAlert('warning', 'มีคนใช้ชื่อนี้ไปแล้วค่ะ กรุณาตรวจสอบใหม่อีกครั้งนะคะ', 'Warning Dialog');		
			}else{
				userok = true;
				userInfo.html('<img src="images/checked.gif" width="14" height="14" />');
				
			}
		}
		});
	});
		
	email_t.blur(function(){
	$.ajax({
		type: "POST",
		data: "email_t="+$(this).attr("value"),
		url: "check_email.php",
		beforeSend: function(){
			emailInfo.html('<img src="images/loading.gif" width="16" height="16" />');
			
		},
		success: function(data){
			
			if(data == "0"){
				emailok = false;
				emailInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
			}else if(data == "invalid"){
				emailok = false;
				emailInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
				jAlert('warning', 'อีเมล์ไม่ถูกต้องค่ะ', 'Warning Dialog');
			}else if(data == 1){
				emailok = false;
				emailInfo.html('<img src="images/unchecked.gif" width="14" height="14" />');
				jAlert('warning', 'มีคนใช้อีเมล์นี้ไปแล้วค่ะ กรุณาตรวจสอบใหม่อีกครั้งนะคะ', 'Warning Dialog');
			}else{
				emailok = true;
				emailInfo.html('<img src="images/checked.gif" width="14" height="14" />');
			}
		}
	});
	});
});
