$(function() {
		$('#datepicker').datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'dd/mm/yy'
		});
		
		$("#cemail").click(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#memail").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("ajax_check-email.php",{email:$('#emailaa').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='no') //if correct login detail
		  {
		  	$("#memail").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('อีเมล์นี้ใช้ได้').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 //document.location='secure.php';
				 //alert('email OKKKKK');
				 
				 $("#mcheck").val('y')
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#memail").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('อีเมล์นี้มีสมาชิกท่านอื่นใช้แล้ว หรืออีเมล์ผิดรูปแบบ กรุณาตรวจสอบอีเมล์อีกครั้ง').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	
	
	
	$("#cnickname").click(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#mnickname").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("ajax_check-nickname.php",{nickname:$('#nickname').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='no') //if correct login detail
		  {
		  	$("#mnickname").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('ชื่อนี้ใช้ได้').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 //document.location='secure.php';
				 //alert('Nickname OKKKKK');
				 $("#mn").val('y')
				 
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#mnickname").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('ชื่อนี้มีคนใช้แล้ว กรุณาเลือกชื่ออื่น').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	
		
		
	});
	
	function check_register()
            {	
		var emailFilter=/^.+@.+\..{2,3}$/;
		var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~"; 
	
		var mcheck  = document.cr.mcheck.value;
		var mn  = document.cr.mn.value;
		
		var email = document.cr.emailaa.value;
		var pass = document.cr.pass.value;
		var repass = document.cr.repass.value;
		
		var nickname = document.cr.nickname.value;
		
		var name = document.cr.name.value;
		var lastname = document.cr.lastname.value;
		
		var phone = document.cr.phone.value;
		
		var brithday = document.cr.brithday.value;
		//var sex = document.cr.sex.value;
		/*
		var address = document.cr.address.value;
		var city = document.cr.city.value;
		var state = document.cr.state.value;
		var zip = document.cr.zip.value;
		var tel = document.cr.tel.value;
		*/
		
		if(document.cr.ccuntomer1.checked == false && document.cr.ccuntomer2.checked == false){
			alert("กรุณาเลือกช่อง 'คุณเคยใช้บริการของเมโกะคลินิกหรือไม่' ค่ะ");
				return false;
			}
		///////////////
		if(email == ""){
			alert("กรุณากรอกช่อง 'อีเมล์' ค่ะ");
				return false;
			}else if(!(emailFilter.test(email)))
			{
			alert("กรุณากรอกช่อง 'อีเมล์ให้ถูกรูปแบบ' ค่ะ");
					return false;
			}else if(mcheck == "n"){
			alert("กรุณากดปุ่ม Check อีเมล์ เพื่อตรวจสอบอีเมล์\nที่สามารถสมัครสมาชิกได้ค่ะ");
					return false;
			}
		///////////////	
		if(pass == ""){
			alert("กรุณากรอกช่อง 'รหัสผ่าน' ค่ะ");
				return false;
		
		}else if(pass.length < 5){
		
		alert("กรุณากรอกช่อง 'รหัสผ่านมากกว่า 5 ตัว' ค่ะ");
				return false;
		}
		
		for (var i = 0; i < pass.length; i++) {
		if (iChars.indexOf(pass.charAt(i)) != -1) {
		  alert ("กรุณาห้ามใส่ '!@#$%^&*()+=-[]\\\';,./{}|\":<>?~' ลงในช่อง กำหนดรหัสผ่านค่ะ");
		return false;
		}
	  }
		
		 if(pass != repass){
			alert("กรุณากรอกช่อง 'รหัสผ่านอีกครั้งให้ตรงกับรหัสผ่านที่คุณกำหนด' ค่ะ");
				return false;
		}
		///////////////
		if(nickname == ""){
			alert("กรุณากรอกช่อง 'ชื่อนามแฝง' ค่ะ");
				return false;
			}else if(nickname.length < 5){
			alert("กรุณากรอกช่อง 'ชื่อนามแฝงมากกว่า 5 ตัว' ค่ะ");
					return false;
			}else if(mn == "n"){
			alert("กรุณากดปุ่ม Check ชื่อนามแฝง เพื่อตรวจสอบชื่อ\nที่สามารถสมัครสมาชิกได้");
					return false;
			}
			for (var i = 0; i < nickname.length; i++) {
		if (iChars.indexOf(nickname.charAt(i)) != -1) {
		  alert ("กรุณาห้ามใส่ '!@#$%^&*()+=-[]\\\';,./{}|\":<>?~' ลงในช่อง ชื่อนามแฝงค่ะ");
		return false;
		}
	  }
		///////////////
		if(name == ""){
			alert("กรุณากรอกช่อง 'ชื่อ' ค่ะ");
				return false;
			}else if(name.length < 2){
			alert("กรุณากรอกช่อง 'ชื่อมากกว่า 2 ตัว' ค่ะ");
					return false;
			}
		///////////////
		if(lastname == ""){
			alert("กรุณากรอกช่อง 'นามสกุล' ค่ะ");
				return false;
			}else if(lastname.length < 2){
			alert("กรุณากรอกช่อง 'นามสกุล' ค่ะ");
				return false;
			}
		
		///////////////
		if(phone == ""){
			alert("กรุณากรอกช่อง 'มือถือ' ค่ะ");
				return false;
			}else if(!check_number(phone) || phone == "" )
			{
			alert("กรุณากรอกช่อง 'มือถือ'เป็นตัวเลขค่ะ");
			return false;
			}else if(phone.length < 8){
			alert("กรุณากรอกช่อง 'มือถือ' ให้ถูกต้องค่ะ");
					return false;
			}
		if(brithday == ""){
			alert("กรุณากรอกช่อง 'วันเดือนปีเกิด' ค่ะ");
				return false;
			}
		if(document.cr.csex1.checked == false && document.cr.csex2.checked == false){
			alert("กรุณากรอกช่อง 'เพศ' ค่ะ");
				return false;
			}
			
		
			
			
		
		
		
		
	   
			
			
			/*
				if (mn == "y")
				{
					alert("Pass1");
					return false;
				}
				*/
			
		}