// -------------------------------------- ฟังก์ชั่น Validate Number -------------------------------------- 
function isInteger(num)
{   var i;
    for (i = 0; i < num.length; i++)
    {   
        var c = num.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

// -------------------------------------- ฟังก์ชั่น Validate Alphabet -------------------------------------- 
function isAlpha(argvalue) {
  argvalue = argvalue.toString();
  var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    for (var n = 0; n < argvalue.length; n++) {
        if (validChars.indexOf(argvalue.substring(n, n+1)) == -1)
         return false;
    }
  return true;
}

// -------------------------------------- ฟังก์ชั่น Validate Student Form -------------------------------------- 
function validate_student ( )
{
	valid = true;
	var text = "";

	if ( document.student_reg.Nickname.value == "" )	
		{		
			text = "- ชื่อเล่น\n";		
			valid = false; 	
		} 
	if ( document.student_reg.School.value == "" )	
		{		
			text = text+"- สถานศึกษา\n";		
			valid = false; 	
		}
	if (	( document.student_reg.Level.selectedIndex == 0 ) 
			 || ( document.student_reg.Level.selectedIndex == 7 )
			 || ( document.student_reg.Level.selectedIndex == 12 )
			 || ( document.student_reg.Level.selectedIndex == 19 ) )
		{		
			text = text+"- ระดับชั้น\n";		
			valid = false; 	
		}
	if ( document.student_reg.Subject.selectedIndex == 0 ) 
		{		
			text = text+"- วิชาที่เรียน\n";		
			valid = false; 	
		}
	if (	( document.student_reg.Bilingual[0].checked == false ) 
			&& ( document.student_reg.Bilingual[1].checked == false ) )
		{
			text = text+"- หลักสูตรที่เรียน\n";		
			valid = false; 	
		}
	if ( document.student_reg.Place.value == "" )	
		{		
			text = text+"- สถานที่ที่ต้องการเรียน\n";		
			valid = false; 	
		}
	if ( document.student_reg.District.selectedIndex == 0 ) 
		{		
			text = text+"- เขตพื้นที่เรียน\n";		
			valid = false; 	
		}
	if (	( document.student_reg.D1.checked == false ) 
			 && ( document.student_reg.D2.checked == false ) 
			 && ( document.student_reg.D3.checked == false ) 
			 && ( document.student_reg.D4.checked == false ) 
			 && ( document.student_reg.D5.checked == false ) 
			 && ( document.student_reg.D6.checked == false ) 
			 && ( document.student_reg.D7.checked == false ) )
		{		
			text = text+"- วันที่ต้องการเรียน\n";		
			valid = false; 	
		}
	if ( document.student_reg.Hour.selectedIndex == 0 )	
		{		
			text = text+"- เวลาที่ต้องการเรียน และจำนวนชั่วโมงเรียน\n";		
			valid = false; 	
		}
	if (	( document.student_reg.TutorSex[0].checked == false ) 
			&& ( document.student_reg.TutorSex[1].checked == false )
			&& ( document.student_reg.TutorSex[2].checked == false ))
		{
			text = text+"- เพศติวเตอร์\n";		
			valid = false; 	
		}
	if ( document.student_reg.Contact.value == "" )	
		{		
			text = text+"- ชื่อผู้ติดต่อ\n";		
			valid = false; 	
		}
	if ( document.student_reg.Mobile.value == "" )	
		{		
			text = text+"- หมายเลขโทรศัพท์มือถือ (ตัวเลขเท่านั้น)\n";		
			valid = false; 	
		}
	if ( !isInteger(document.student_reg.Mobile.value) || !isInteger(document.student_reg.Phone.value) )
		{
			text = text+"- หมายเลขโทรศัพท์และมือถือ ต้องเป็นตัวเลขเท่านั้น\n";		
			valid = false; 	
		}

	if (!valid)
		{
			alert ("กรุณาตรวจสอบข้อมูลในช่องต่อไปนี้ : \n\n" + text)
		}

	return valid;
}
// -------------------------------------- ฟังก์ชั่น Validate Payment Form -------------------------------------- 
function validate_payment ()
{
	valid = true;
	var text = "";
	
	if (	( document.pay_confirm.MB_Register.checked == false ) 
			 && ( document.pay_confirm.SMS_25.checked == false ) 
			 && ( document.pay_confirm.SMS_50.checked == false ) 
			 && ( document.pay_confirm.SMS_75.checked == false ) 
			 && ( document.pay_confirm.SMS_100.checked == false )  )
		{		
			text = text+"- ประเภทการชำระเงิน\n";		
			valid = false; 	
		}
	if ( document.pay_confirm.Account.selectedIndex == 0 ) 
		{		
			text = text+"- บัญชีธนาคารที่ท่านโอนเงิน\n";		
			valid = false; 	
		}
	if ( 	( document.pay_confirm.TDay.selectedIndex == 0 ) &&
		 	 ( document.pay_confirm.TMonth.selectedIndex == 0 )&&
			  ( document.pay_confirm.TYear.selectedIndex == 0 ) 	)
		{		
			text = text+"- วัน/เดือน/ปี ที่โอนเงิน\n";		
			valid = false; 	
		}
	if ( 	( document.pay_confirm.Hour.selectedIndex == 0 ) &&
			  ( document.pay_confirm.Minute.selectedIndex == 0 ) 	)
		{		
			text = text+"- เวลาที่โอนเงิน\n";		
			valid = false; 	
		}
	if ( document.pay_confirm.How2pay.selectedIndex == 0 )
		{		
			text = text+"- วิธีการโอนเงิน\n";		
			valid = false; 	
		}
		
	if (!valid)
		{
			alert ("กรุณาตรวจสอบข้อมูลในช่องต่อไปนี้ : \n\n" + text)
		}

	return valid;
}
// -------------------------------------- ฟังก์ชั่น Validate Tutor Form -------------------------------------- 
function validate_tutor ( )
{
	// ตรวจสอบ Textbox
	if( ( document.tutor_reg.Name.value == "" )
			|| ( document.tutor_reg.Surname.value == "" )
			|| ( document.tutor_reg.Nickname.value == "" )
			|| ( document.tutor_reg.Address.value == "" )
			|| ( document.tutor_reg.Mobile.value == "" )
			|| ( document.tutor_reg.Email.value == "" )
			|| ( document.tutor_reg.Highschool.value == "" )
			|| ( document.tutor_reg.BFaculty.value == "" )
			|| ( document.tutor_reg.TeachStyle.value == "" )
			|| ( document.tutor_reg.Username.value == "" )
			)
		{		
			alert ("กรุณากรอกข้อมูลในช่องที่มีเครื่องหมายดอกจันทร์ (*) ให้ครบทุกช่อง");	
			return false;
		}
	//ตรวจสอบ Radio button
	if (	(( document.tutor_reg.Pre[0].checked == false ) 
			&& ( document.tutor_reg.Pre[1].checked == false )
			&& ( document.tutor_reg.Pre[2].checked == false ))
			|| (( document.tutor_reg.Bilingual[0].checked == false )
			&& ( document.tutor_reg.Bilingual[1].checked == false ))
			)
		{
			alert ("กรุณาเลือกข้อมูลในช่องที่มีเครื่องหมายดอกจันทร์ (*) ให้ครบทุกช่อง");	
			return false;
		}
	//ตรวจสอบ Select Box
	if ( ( document.tutor_reg.BirthDay.selectedIndex == 0 )
		  	|| ( document.tutor_reg.BirthMonth.selectedIndex == 0 )
			|| ( document.tutor_reg.BirthYear.selectedIndex == 0 )
			|| ( document.tutor_reg.BUniversity.selectedIndex == 0 )
			|| ( document.tutor_reg.BYear.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject1.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject1_Start.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject1_Start.selectedIndex == 7 )
			|| ( document.tutor_reg.Subject1_Start.selectedIndex == 12 )
			|| ( document.tutor_reg.Subject1_Start.selectedIndex == 19 )
			|| ( document.tutor_reg.Subject1_End.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject1_End.selectedIndex == 7 )
			|| ( document.tutor_reg.Subject1_End.selectedIndex == 12 )
			|| ( document.tutor_reg.Subject1_End.selectedIndex == 19 )
			|| ( document.tutor_reg.Subject2.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject2_Start.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject2_Start.selectedIndex == 7 )
			|| ( document.tutor_reg.Subject2_Start.selectedIndex == 12 )
			|| ( document.tutor_reg.Subject2_Start.selectedIndex == 19 )
			|| ( document.tutor_reg.Subject2_End.selectedIndex == 0 )
			|| ( document.tutor_reg.Subject2_End.selectedIndex == 7 )
			|| ( document.tutor_reg.Subject2_End.selectedIndex == 12 )
			|| ( document.tutor_reg.Subject2_End.selectedIndex == 19 )
			|| ( document.tutor_reg.Tutor_Pr.selectedIndex == 0 )
			  )
		{
			alert ("กรุณาเลือกข้อมูลในช่องที่มีเครื่องหมายดอกจันทร์ (*) ให้ครบทุกช่อง");	
			return false;
		}
	//ตรวจสอบเขตที่สามารถสอนได้
	if (	( document.tutor_reg.District1.checked == false ) 
			 && ( document.tutor_reg.District2.checked == false )  
			 && ( document.tutor_reg.District3.checked == false ) 
			 && ( document.tutor_reg.District4.checked == false ) 
			 && ( document.tutor_reg.District5.checked == false ) 
			 && ( document.tutor_reg.District6.checked == false ) 
			 && ( document.tutor_reg.District7.checked == false ) 
			 && ( document.tutor_reg.District8.checked == false ) 
			 && ( document.tutor_reg.District9.checked == false ) 
			 && ( document.tutor_reg.District10.checked == false ) 
			 && ( document.tutor_reg.District11.checked == false ) 
			 && ( document.tutor_reg.District12.checked == false ) 
			 && ( document.tutor_reg.District13.checked == false ) 
			 && ( document.tutor_reg.District14.checked == false ) 
			 && ( document.tutor_reg.District15.checked == false ) 
			 && ( document.tutor_reg.District16.checked == false ) 
			 && ( document.tutor_reg.District17.checked == false ) 
			 && ( document.tutor_reg.District18.checked == false ) 
			 && ( document.tutor_reg.District19.checked == false ) 
			 && ( document.tutor_reg.District20.checked == false ) 
			 && ( document.tutor_reg.District21.checked == false ) 
			 && ( document.tutor_reg.District22.checked == false ) 
			 && ( document.tutor_reg.District23.checked == false ) 
			 && ( document.tutor_reg.District24.checked == false ) 
			 && ( document.tutor_reg.District25.checked == false ) 
			 && ( document.tutor_reg.District26.checked == false ) 
			 && ( document.tutor_reg.District27.checked == false ) 
			 && ( document.tutor_reg.District28.checked == false ) 
			 && ( document.tutor_reg.District29.checked == false ) 
			 && ( document.tutor_reg.District30.checked == false ) 
			 && ( document.tutor_reg.District31.checked == false ) 
			 && ( document.tutor_reg.District32.checked == false ) 
			 && ( document.tutor_reg.District33.checked == false ) 
			 && ( document.tutor_reg.District34.checked == false ) 
			 && ( document.tutor_reg.District35.checked == false ) 
			 && ( document.tutor_reg.District36.checked == false ) 
			 && ( document.tutor_reg.District37.checked == false ) 
			 && ( document.tutor_reg.District38.checked == false ) 
			 && ( document.tutor_reg.District39.checked == false ) 
			 && ( document.tutor_reg.District40.checked == false ) 
			 && ( document.tutor_reg.District41.checked == false ) 
			 && ( document.tutor_reg.District42.checked == false ) 
			 && ( document.tutor_reg.District43.checked == false ) 
			 && ( document.tutor_reg.District44.checked == false ) 
			 && ( document.tutor_reg.District45.checked == false ) 
			 && ( document.tutor_reg.District46.checked == false ) 
			 && ( document.tutor_reg.District47.checked == false ) 
			 && ( document.tutor_reg.District48.checked == false ) 
			 && ( document.tutor_reg.District49.checked == false ) 
			 && ( document.tutor_reg.District50.checked == false ) 
			 && ( document.tutor_reg.District51.checked == false ) 
			 && ( document.tutor_reg.District52.checked == false )  )
		{		
			alert ("กรุณาเลือกเขตที่ท่านสามารถสอนได้");	
			return false;
		}
	// ตรวจสอบ Password & Re-Password
	if( ( document.tutor_reg.Password.value == "" )
			|| ( document.tutor_reg.RePassword.value == "" )
			)
		{		
			alert ("กรุณากรอก Password และ Re-type Password");	
			return false;
		}
	if ( document.tutor_reg.Password.value != document.tutor_reg.RePassword.value )
		{
			alert ("รหัสผ่านที่ท่านกำหนดในช่อง Password และ Re-type Password ไม่ตรงกัน");	
			return false;
		}
	if ( (document.tutor_reg.Username.length < 6) || (document.tutor_reg.Username.length > 24) )
		{
			alert ("Username จะต้องมีจำนวนตัวอักษร ระหว่าง 6 - 24 ตัวอักษร");
			return false;
		}
	if ( (document.tutor_reg.Password.length < 6) || (document.tutor_reg.Password.length > 24) )
		{
			alert ("Password จะต้องมีจำนวนตัวอักษร ระหว่าง 6 - 24 ตัวอักษร");
			return false;
		}
	// ตรวจสอบการกรอกตัวเลข
	if ( !isInteger(document.tutor_reg.Mobile.value) || !isInteger(document.tutor_reg.Phone.value) )
		{
			alert ("หมายเลขโทรศัพท์ และ หมายเลขโทรศัพท์มือถือ ต้องเป็นตัวเลขเท่านั้น");	
			return false;
		}
	//ตรวจสอบ Username และ Password
	if ( !isAlpha(document.tutor_reg.Username.value) || !isAlpha(document.tutor_reg.Password.value) || !isAlpha(document.tutor_reg.RePassword.value) )
		{
			alert ("Username และ Password ต้องประกอบด้วยตัวอักษร a-z ,A-Z และ 0-9 เท่านั้น");	
			return false;
		}
	
	 return true;
}