// JavaScript Document

function checkID(id) { 

if(id.length != 13) return false; 

for(i=0, sum=0; i < 12; i++) 

sum += parseFloat(id.charAt(i))*(13-i); 

if((11-sum%11)%10!=parseFloat(id.charAt(12))) return false; 

return true; 

} 

function check_number(ch){
	var len, digit;
	
	if(ch == " ")
		{ 
			return false;
			len=0;
		}
	else
		{
			len = ch.length;
		}
	
	for(var i=0 ; i<len ; i++)
		{
			digit = ch.charAt(i)
			if(digit >="0" && digit <="9")
				{
					; 
				}
			else
				{
					return false; 
				} 
		} 
return true;
		}
		
function validatefield(field) 

{

		var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

		var ok = true;

		var temp;

		

		for (var j=0; j<field.length; j++) 

			{

				temp = "" + field.substring(j, j+1);

				if (valid.indexOf(temp) == "-1") 

				ok = false;

			}

		return ok

}

function validateEmail(field) 
{

		var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@."

		var ok = true;

		var temp;

		

		for (var j=0; j<field.length; j++) 

			{

				temp = "" + field.substring(j, j+1);

				if (valid.indexOf(temp) == "-1") 

				ok = false;

			}

		return ok
}




/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,dtCh,minYear,maxYear){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("วันเกิดของคุณไม่ถูกต้อง");
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }


function chk_forget()
		{
			with(document.forget_pass)
				{
						if(email.value=="")
						{
							alert("กรุณากรอก E-mail");
							email.focus();
							return false;
						}		
					else if ((email.value.indexOf('@') == -1)||(email.value.indexOf('.') == -1))
						{
							alert("E-mail ผิดรูปแบบ");
							email.value="";
							email.focus();
							return false;
						}
					else if(!validateEmail(email.value))
						{
							alert("E-mail ผิดรูปแบบ");
							email.value="";
							email.focus();
							return false;
						}
					else
						{
							return true;		
						}
				}
		}

function chk_form()
		{
				
			var dtCh= "/";
			var minYear=1900;
			var maxYear=2100;
				
			
			with(document.register)
				{
					//---------------- Member Account ---------------//	
					 if(email.value=="")
						{
							alert("กรุณากรอก E-mail");
							email.focus();
							return false;
						}		
					else if ((email.value.indexOf('@') == -1)||(email.value.indexOf('.') == -1))
						{
							alert("E-mail ผิดรูปแบบ");
							email.value="";
							email.focus();
							return false;
						}
					else if(!validateEmail(email.value))
						{
							alert("E-mail ผิดรูปแบบ");
							email.value="";
							email.focus();
							return false;
						}
					else  if(email2.value=="")
						{
							alert("กรุณากรอกยืนยัน E-mail");
							email2.focus();
							return false;
						}		
					else if ((email2.value.indexOf('@') == -1)||(email2.value.indexOf('.') == -1))
						{
							alert("ยืนยันE-mail ผิดรูปแบบ");
							email2.value="";
							email2.focus();
							return false;
						}
					else if(!validateEmail(email2.value))
						{
							alert("E-mail ผิดรูปแบบ");
							email2.value="";
							email2.focus();
							return false;
						}
					else if (email2.value!=email.value)
						{
							alert("E-mail และ ยืนยันE-mail ไม่ตรงกัน");
							email2.value="";
							email2.focus();
							return false;
						}
					else if(password.value=="")
						{
							alert("กรุณากรอก รหัสผ่าน");
							password.focus();
							return false;
						}
					else if(!validatefield(password.value) || password.value.length<4) 
						{
							alert ("รูปแบบ รหัสผ่าน ไม่ถูกต้อง");
							password.value="";
							password.focus();			
							return false;		
						}
					else if(password2.value=="")
						{
							alert("กรุณากรอก ยืนยัน รหัสผ่าน");
							password2.focus();
							return false;
						}
					else if(!validatefield(password2.value) || password.value.length<4) 
						{
							alert("รูปแบบ ยืนยัน รหัสผ่าน ไม่ถูกต้อง");
							password2.value="";
							password2.focus();			
							return false;		
						}
					else if(password.value!=password2.value)
						{
							alert("รหัสผ่าน กับ ยืนยัน รหัสผ่าน ไม่ตรงกัน");
							password2.value="";
							password2.focus();
							return false;
						}
					
				
				//---------------- โปรไฟล์สมาชิก ---------------//
					
					else if(fname.value=="")
						{
							alert("กรุณากรอกชื่อ");
							fname.focus();
							return false;
						}
					else if(lname.value=="")
						{
							alert("กรุณากรอกนามสกุล");
							lname.focus();
							return false;
						}
					else if(txt_birthday.value=="")
						{
							alert("กรุณาเลือกวันเกิด");
							txt_birthday.focus();
							return false;
						}
					else if(txt_birthmonth.value=="")
						{
							alert("กรุณาเลือกเดือนเกิด");
							txt_birthmonth.focus();
							return false;
						}
					else if(txt_birthyear.value=="")
						{
							alert("กรุณาเลือกปีเกิด");
							txt_birthyear.focus();
							return false;
						}
					else if(isDate(txt_birthmonth.value+"/"+txt_birthday.value+"/"+(txt_birthyear.value-543),dtCh,minYear,maxYear)==false)
						{
							return false;
						}
					else if(id_number.value=="" || id_number.value.length<13)
						{
							alert("กรุณากรอก หมายเลขบัตรประชาชน 13 หลัก ");
							id_number.focus();
							return false;	
						}
					else if(!check_number(id_number.value)) 
						{
							alert("รูปแบบ หมายเลขบัตรประชาชน 13 หลัก กรอกได้เฉพาะตัวเลขเท่านั้น");
							id_number.value="";
							id_number.focus();			
							return false;		
						}
					else if(!checkID(id_number.value))
						{
							alert("รูปแบบ หมายเลขบัตรประชาชน 13 หลัก ไม่ถูกต้อง");
							id_number.value="";
							id_number.focus();			
							return false;
						}
					else if(address.value=="")
						{
							alert("กรุณากรอกที่อยู่");
							address.focus();
							return false;
						}
					else if(province.value=="")
						{
							alert("กรุณาเลือกจังหวัด");
							province.focus();
							return false;
						}
					else if(zip.value=="" || zip.value.length<5)
						{
							alert("กรุณากรอกรหัสไปรษณีย์ให้ครบถ้วน");
							zip.focus();
							return false;
						}
					else if(!check_number(zip.value)) 
						{
							alert("รูปแบบ รหัสไปรษณีย์กรอกได้เฉพาะตัวเลขเท่านั้น");
							zip.value="";
							zip.focus();			
							return false;		
						}
					else if(mobile.value=="" || mobile.value.length<10)
						{
							alert("กรุณากรอกเบอร์โทรศัพท์มือถือให้ครบถ้วน");
							mobile.focus();
							return false;
						}
					else if(!check_number(mobile.value)) 
						{
							alert("รูปแบบ เบอร์โทรศัพท์มือถือกรอกได้เฉพาะตัวเลขเท่านั้น");
							mobile.value="08";
							mobile.focus();			
							return false;		
						}
					else if(accept_condition.checked==false) 
						{
							alert("กรุณากดเลือกยอมรับนโยบายและเงื่อนไขด้วยค่ะ");
							accept_condition.focus();
							return false;		
						}
					else if(secure_code.value =="")
						{
							alert("กรุณากรอก Secure Code");
							secure_code.focus();		
							return false;			
						}
					else if(secure_code.value != capcha.value) 
						{
							alert("Secure Code ไม่ตรงกับตัวอักษรที่ระบบกำหนดให้");
							secure_code.focus();		
							return false;			
						}
					else	
							return true;			
				}
		}
	
	function chk_edit()
		{
				
			var dtCh= "/";
			var minYear=1900;
			var maxYear=2100;
				
			
			with(document.edit_profile)
				{
					//---------------- Member Account ---------------//	
					if(password.value!="")
						{
							if(!validatefield(password.value) || password.value.length<4) 
								{
									alert ("รูปแบบ รหัสผ่าน ไม่ถูกต้อง");
									password.value="";
									password.focus();			
									return false;		
								}
							
							if(!validatefield(password2.value) || password.value.length<4) 
								{
									alert("รูปแบบ ยืนยัน รหัสผ่าน ไม่ถูกต้อง");
									password2.value="";
									password2.focus();			
									return false;		
								}
							if(password.value!=password2.value)
								{
									alert("รหัสผ่าน กับ ยืนยัน รหัสผ่าน ไม่ตรงกัน");
									password2.value="";
									password2.focus();
									return false;
								}
						}
					else if(password2.value!="")
						{
							if(!validatefield(password2.value) || password.value.length<4) 
								{
									alert("รูปแบบ ยืนยัน รหัสผ่าน ไม่ถูกต้อง");
									password2.value="";
									password2.focus();			
									return false;		
								}
								
							if(!validatefield(password.value) || password.value.length<4) 
								{
									alert ("รูปแบบ รหัสผ่าน ไม่ถูกต้อง");
									password.value="";
									password.focus();			
									return false;		
								}
							if(password.value!=password2.value)
								{
									alert("รหัสผ่าน กับ ยืนยัน รหัสผ่าน ไม่ตรงกัน");
									password2.value="";
									password2.focus();
									return false;
								}
						}
					else if(password.value!=password2.value)
						{
							alert("รหัสผ่าน กับ ยืนยัน รหัสผ่าน ไม่ตรงกัน");
							password2.value="";
							password2.focus();
							return false;
						}
					
				
				//---------------- โปรไฟล์สมาชิก ---------------//
					
					else if(fname.value=="")
						{
							alert("กรุณากรอกชื่อ");
							fname.focus();
							return false;
						}
					else if(lname.value=="")
						{
							alert("กรุณากรอกนามสกุล");
							lname.focus();
							return false;
						}
					else if(txt_birthday.value=="")
						{
							alert("กรุณาเลือกวันเกิด");
							txt_birthday.focus();
							return false;
						}
					else if(txt_birthmonth.value=="")
						{
							alert("กรุณาเลือกเดือนเกิด");
							txt_birthmonth.focus();
							return false;
						}
					else if(txt_birthyear.value=="")
						{
							alert("กรุณาเลือกปีเกิด");
							txt_birthyear.focus();
							return false;
						}
					else if(isDate(txt_birthmonth.value+"/"+txt_birthday.value+"/"+(txt_birthyear.value-543),dtCh,minYear,maxYear)==false)
						{
							return false;
						}
					else if(address.value=="")
						{
							alert("กรุณากรอกที่อยู่");
							address.focus();
							return false;
						}
					else if(province.value=="")
						{
							alert("กรุณาเลือกจังหวัด");
							province.focus();
							return false;
						}
					else if(zip.value=="" || zip.value.length<5)
						{
							alert("กรุณากรอกรหัสไปรษณีย์ให้ครบถ้วน");
							zip.focus();
							return false;
						}
					else if(!check_number(zip.value)) 
						{
							alert("รูปแบบ รหัสไปรษณีย์กรอกได้เฉพาะตัวเลขเท่านั้น");
							zip.value="";
							zip.focus();			
							return false;		
						}
					else if(mobile.value=="" || mobile.value.length<10)
						{
							alert("กรุณากรอกเบอร์โทรศัพท์มือถือให้ครบถ้วน");
							mobile.focus();
							return false;
						}
					else if(!check_number(mobile.value)) 
						{
							alert("รูปแบบ เบอร์โทรศัพท์มือถือกรอกได้เฉพาะตัวเลขเท่านั้น");
							mobile.value="08";
							mobile.focus();			
							return false;		
						}
					else	
							return true;			
				}
		}
	


///////////////////////////////////////////////////////////////
/////////// ฟังก์ชั่นเรียกจำนวนของฟอร์มที่ต้องการทราบ ///////////////////
///////////////////////////////////////////////////////////////
	function chk_checked_form(form_name)
		{
			var arr = document.getElementsByName(form_name);
			var all_qty =  arr.length;
			var count = 0;
			for(i=0;i<all_qty;i++)
				{
					if(document.getElementById(form_name+"["+i+"]").checked==true)
						{
							count++;
						}
				}
			
			if(count>0)
				return 1;
			else
				return 0;
		}
	
	function chk_other_choice(form_name)
		{
			var arr = document.getElementsByName(form_name);
			var all_qty =  arr.length;
			var count = 0;
			for(i=0;i<all_qty;i++)
				{
					if(document.getElementById(form_name+"["+i+"]").checked==true)
						{
							if(document.getElementById(form_name+"["+i+"]").value=="other")
							{
								count++;
							}
						}
				}

			if(count>0)
				return 1;
			else
				return 0;
		}

function chk_username()
	{
		var username = document.getElementById("username");
		if(username.value=="")
			{
				alert("กรุณากรอก Username ด้วยค่ะ");
				username.focus();
			}
		else if(!validatefield(username.value) || username.value.length<4) 
			{
				alert("รูปแบบ Username ผิด");
				username.value="";
				username.focus();			
				return false;		
			}
		else
			{
				document.getElementById("iframe_username").src = "chk_username.php?username="+username.value+"";	
			}
	}