function register(){

	var email1 = $F("email");
		var emailPat =  "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
		var matchArray =email1.match(emailPat);

	var check_num = true;
	var value = $F("phone");
	for(var i=0;i < $F("phone").length; ++i){
		var new_key = value.charAt(i); //cycle through characters
		if(((new_key < "0") || (new_key > "9")) && !(new_key == "")){
			check_num = false;
			break;
		 }
	  }
	
	if($F("username") == ""){
		alert("กรุณาระบุ username");
		Form.Element.focus('username');
		return false;
	}else if($F("username").length<6){
		alert("username ต้องมีความยาวอย่างน้อย 6 ตัวอักษร");
		Form.Element.focus('username');
		return false;
	}else if($F("pass")==""&&$F("main_mode")=="register"){
		alert("กรุณาระบุ password");
		Form.Element.focus('pass');
		return false;
	}else if($F("pass").length<6&&$F("main_mode")=="register"){
		alert("รหัสผ่านต้องมีความยาวอย่างน้อย  6 ตัวอักษร");
		Form.Element.focus('pass');
		return false;
	}else if($F("cpass")==""&&$F("main_mode")=="register"){
		alert("กรุณาระบุ ยืนยัน password");
		Form.Element.focus('cpass');
		return false;
	}else if($F("pass")!=$F("cpass")){
		alert("รหัสผ่านไม่ตรงกัน");
		Form.Element.focus('cpass');
		return false;
	}else if($F("namename")==""){
		alert("กรุณากรอกชื่อ");
		Form.Element.focus('namename');
		return false;
	}else if($F("lastname")==""){
		alert("กรุณากรอกนามสกุล");
		Form.Element.focus('lastname');
		return false;
	}else if($F("address")==""){
		alert("กรุณกรอกที่อยู่");
		Form.Element.focus('address');
		return false;
	}else if($F("phone")==""){
		alert("กรุณากรอกหมายเลขโทรศัพท์");
		Form.Element.focus('phone');
		return false;
	}else if(check_num==false){
		alert("กรุณากรอกหมายเลขโทรศัพท์เป็นตัวเลขเท่านั้น");
		Form.Element.focus('phone');
		return false;
	}else if (matchArray == null) {
		alert('รูปแบบ  E-mail ไม่ถูกต้อง : yourname@example.com');
		Form.Element.focus('email');
		return false;
	}else{
		$("loginloading").innerHTML = '<img src=system/images/ajax-loading.gif width="70px" />'; // โหลด
		setTimeout("saveregister()",500); //ส่งไปตรวจสอบ
	}
} 

function logincheck(){ //login
	if($F("username_log") == ""){
		alert("กรุณากรอก username");
		Form.Element.focus('username_log');
		return false;
	}else if($F("password_log")==""){
		alert("กรุณากรอก password");
		Form.Element.focus('password_log');
		return false;
	}else{
		$("loginloading_log").innerHTML = '<img src=system/images/ajax-loading.gif  width="46"  />'; // โหลด
		setTimeout("login()",500); //ส่งไปตรวจสอบ
	}
}
function login(){
	new Ajax.Request("action_main.php", // ไฟล์ที่ทำการตรวจสอบ
		{
			method:"POST",
			parameters:$("formajaxlogin").serialize(true),
			onComplete:function resultlogin(result){ // ไปรายงานการตรวจสอบ
					if(result.responseText=="login"){ //{ // ตรวจสอบไม่ถูกต้อง	
						$("loginloading_log").innerHTML = '';
						location.reload(true);
						return true;
					}else if(result.responseText=="login_f"){ //{ // ตรวจสอบไม่ถูกต้อง	
						$("loginloading_log").innerHTML = '<img src="images/btnsubmit.png" width="46" height="46" OnClick="javaScript:logincheck();">';
						alert("ไม่สามารถเข้าสู่ระบบได้");	
						return false;
					}else{
						alert(result.responseText);	
					}
			}
		}
	);
}

function logout(){ //logout
	new Ajax.Request("action_main.php",
		{
			method:"GET",
			onComplete: function logoutresult(result){
					location.reload(true);
					return true;
			}
		}
	);
} 

function forgot(){
	var email1 = $F("femail");
	var emailPat =  "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
	var matchArray =email1.match(emailPat);

	if (matchArray == null) {
		alert('รูปแบบ  E-mail ไม่ถูกต้อง : yourname@example.com');
		//Form.Element.focus('femail');
		return false;
	}else{
		$("loginloading2").innerHTML = '<img src=system/images/ajax-loading.gif  width="46"  />'; // โหลด
		setTimeout("forgotcheck()",500); //ส่งไปตรวจสอบ
	}
}

function forgotcheck(){
	new Ajax.Request("action_main.php", // ไฟล์ที่ทำการตรวจสอบ
		{
			method:"POST",
			parameters:$("formforgot").serialize(true),
			onComplete:regisresult // ไปรายงานการตรวจสอบ
		}
	);
}

	// ตรวจสอบ
function saveregister(){
	new Ajax.Request("action_main.php", // ไฟล์ที่ทำการตรวจสอบ
		{
			method:"POST",
			parameters:$("formajaxmain").serialize(true),
			onComplete:regisresult // ไปรายงานการตรวจสอบ
		}
	);
}

// รายงาน
function regisresult(result){	
	//alert(result.responseText);
	if(result.responseText==0){ // ตรวจสอบถูกต้อง
		$("loginloading").innerHTML = '';
		alert("มีผู้ใช้ username นี้แล้ว");
		Form.Element.focus('username');
		return false;
	}else if(result.responseText=="email_f"){ //{ // ตรวจสอบไม่ถูกต้อง
		$("loginloading").innerHTML = '';
		alert("อีเมล์นี้มีผู้ใช้ในระบบแล้ว");				
		Form.Element.focus('email');
		return true;
	}else if(result.responseText==1){ //{ // ตรวจสอบไม่ถูกต้อง
		$("loginloading").innerHTML = '';
		alert("ลงทะเบียนเสร็จสมบูรณ์");				
		//location.reload(true);
		window.location = 'index.php'; // ไปยังหน้านี้
		return true;
	}else if(result.responseText=="forgot"){ //{ // ตรวจสอบไม่ถูกต้อง	
		$("loginloading2").innerHTML = '';
		alert("ระบบได้ส่ง username และ password แจ้งไปที่อีเมล์เรียบร้อยแล้ว");
		Form.reset('formforgot');
		return true;
	}else if(result.responseText=="forgot_f"){ //{ // ตรวจสอบไม่ถูกต้อง
		$("loginloading2").innerHTML = '';
		alert("คุณระบุข้อมูลไม่ถูกต้อง");
		return false;
	}else if(result.responseText=="regisedit"){ //{ // ตรวจสอบไม่ถูกต้อง
		$("loginloading").innerHTML = '';
		alert("แก้ไขข้อมูลเสร็จสมบูรณ์");
		return false;
	}else{
		alert(result.responseText);
		return false;
	}
}  
