/*
 * Module : ROP Web Application
 * Version : V 1.0
 * Modified date : Nov, 15 2001
 * By : Net-Innova Co., Ltd.  
 * Description : JavaScript
 */

// **************************************************************
//           Check correctness of the user input
// **************************************************************
function checkInput(theForm)
{	
  if (checkAppName(theForm)&& checkAppAddr(theForm) && checkAppInfo(theForm)&& checkHint(theForm)&& checkMemberSpnosor(theForm))	  
       return true;
	else
		return false;
}

function checkUpdate(theForm)
{	
    if(checkAppAddr(theForm) && checkAppInfo(theForm)&& checkHint(theForm))	
       return true;
	else
		return false;
}
// ----------------------------------------------
// check applicant' s name information
// ----------------------------------------------
function checkAppName(theForm)
{	
	sIndex = theForm.Salutation.selectedIndex;
    if ( ((theForm.Salutation[sIndex].value == "Mr." || theForm.Salutation[sIndex].value == "CHD Mr.") && theForm.Gender[1].checked) || (((theForm.Salutation[sIndex].value == "Mrs." || theForm.Salutation[sIndex].value == "Ms." || theForm.Salutation[sIndex].value == "Miss" || theForm.Salutation[sIndex].value == "CHD Miss") && theForm.Gender[0].checked)) )
    {
        alert('คำนำหน้าชื่อของท่านไม่สอดคล้องกับเพศ');
		theForm.Salutation.focus();
        return false;
    }	
	if (theForm.Salutation[sIndex].value == "Other" && theForm.SalutationOther.value == "")
    {
        alert('กรุณาใส่ข้อมูลลงในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n หากท่านเลื่อกอื่นๆ กรุณาใส่ข้อมูลในคำนำหน้าชื่อ ');
		theForm.SalutationOther.focus();
        return false;
    }
	if (theForm.Salutation[sIndex].value == "Other")
		if(!IsString2(theForm.SalutationOther, "กรุณาใส่ข้อมูลในคำนำหน้าชื่อ")) return false;
	if (theForm.FName.value == "")
    {		
        alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ใส่ชื่อของท่าน ');
		theForm.FName.focus();
        return false;
    }
	if (theForm.FName.value.length < 2)
    {
        alert('ชื่อของท่านต้องมีความยาวอย่างน้อย 2 ตัวอักษร ');
		theForm.FName.focus();
        return false;
    }
	if(!IsString1(theForm.FName, "กรุณาใส่ ชื่อ ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if(!IsString2(theForm.MName, "กรุณาใส่ ชื่อกลาง ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if (theForm.LName.value == "")
    {
        alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ใส่นามสกุลของท่าน ');
		theForm.LName.focus();
        return false;
    }	
    if (theForm.LName.value.length < 2)
    {
        alert('ชื่อของท่านต้องมีความยาวอย่างน้อย 2 ตัวอักษร');
		theForm.LName.focus();
        return false;
    }
	if(!IsString1(theForm.LName, "กรุณาใส่ นามสกุล ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if (theForm.Suffix[theForm.Suffix.selectedIndex].value == "Other" && theForm.SuffixOther.value == "")
   	{
        alert ('หากท่านเลือกอื่นๆ กรุณาใส่ข้อมูลของคำต่อท้าย');
		theForm.SuffixOther.focus();
   	    return false;
    }  
	if (theForm.Suffix[theForm.Suffix.selectedIndex].value == "Other")
		if(!IsString2(theForm.SuffixOther, "กรุณาใส่ คำต่อท้ายของท่านให้ถูกต้อง")) return false;
    return true;
}

// ----------------------------------------------
// check applicant' s address information
// ----------------------------------------------
function checkAppAddr(theForm)
{
//-----------------
   
if (!(theForm.Specify[0].checked))
	{
		if (!(theForm.Title.value == ""))
		  {		
			 alert('* สำหรับที่อยู่ที่ทำงานเท่านั้น');
			 theForm.Title.focus();
			 return false;
		   }
		  if (!(theForm.Company1.value == ""))
		  {		
			alert('* สำหรับที่อยู่ที่ทำงานเท่านั้น');
			theForm.Company1.focus();
			return false;
		   }
		   if (!(theForm.Company2.value == ""))
		  {		
			alert('* สำหรับที่อยู่ที่ทำงานเท่านั้น');
			theForm.Company2.focus();
			return false;
		  }
	  }
	 if ((theForm.Specify[0].checked))
	 {
		if (!(theForm.Title.value == ""))
		{		
		  if(!IsString3(theForm.Title, "กรุณาใส่ ตำแหน่ง ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
		 }
		if (!(theForm.Company1.value == ""))
		{		
		if(!IsString3(theForm.Company1, "กรุณาใส่ ชื่อบริษัท ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
		}
		if (!(theForm.Company2.value == ""))
		{		
		if(!IsString3(theForm.Company2, "กรุณาใส่ ชื่อบริษัท ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
		}
	 }
//------------------------

    if (theForm.Addr1.value == "")
    {
		alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านต้องใส่ ที่อยู่ในบรรทัดแรก ');
		theForm.Addr1.focus();
		return false;
    }

	if(!IsString3(theForm.Addr1, "กรุณา ใส่ที่อยู่ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if(!IsString3(theForm.Addr2, "กรุณา ใส่ที่อยู่ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if(!IsString3(theForm.Addr3, "กรุณา ใส่ที่อยู่ของท่านให้ถูกต้องเป็นภาษาอังกฤษ")) return false;

	if (theForm.CityList[theForm.CityList.selectedIndex].value == "*")
	{
		alert("กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n เมื่องที่ท่านเลือกไม่ถูกต้อง กรุณาเลือกใหม่อีกครั้ง ");
		theForm.CityList.focus();
		return false;
	}
	if (theForm.CityList[theForm.CityList.selectedIndex].value == "Other" && theForm.OtherCity.value == "")
	{
		alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n หากท่านเลือกเมืองอื่นๆ กรุณาใส่ชื่อเมือง ');
		theForm.OtherCity.focus();
		return false;
	}
	if (theForm.CityList[theForm.CityList.selectedIndex].value == "Other")
		if(!IsString2(theForm.OtherCity, "กรุณาใส่เข้อมูลในมืองอื่นๆ ให้ถูกต้องเป็นภาษาอังกฤษ")) return false;
	if (theForm.CountryList[theForm.CountryList.selectedIndex].value == "*")
	{
	    alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้เลือกประเทศ');
		theForm.CountryList.focus();
	    return false;
	}
	if(theForm.CountryList[theForm.CountryList.selectedIndex].value == "US" || theForm.CountryList[theForm.CountryList.selectedIndex].value == "CA" || theForm.CountryList[theForm.CountryList.selectedIndex].value == "AU" || theForm.CountryList[theForm.CountryList.selectedIndex].value == "BR" || theForm.CountryList[theForm.CountryList.selectedIndex].value == "AR")
	{
	    if (theForm.StateList[theForm.StateList.selectedIndex].value == "*")
	    {
			alert ('ท่านต้องเลือก รัฐ หากท่านอาศัยอยู่ในประเทศ อาร์เจนตินา ออสเตรเลีย บราซิล แคนาดา หรือ สหรัฐฯ ');
			theForm.StateList.focus();
			return false;
	    }
	} else {
	    if (theForm.StateList[theForm.StateList.selectedIndex].value != "*")
	    {
			alert ('ท่านต้องเลือก รัฐ หากท่านอาศัยอยู่ในประเทศ Argentina, Australia, Brazil, Canada or the USA ');
			theForm.StateList.focus();
			return false;
	    }
	}
	if ((theForm.CountryList[theForm.CountryList.selectedIndex].value == "US" && theForm.StateList[theForm.StateList.selectedIndex].value.substring(0,2) != "US"))
	{
	    alert('กรุณาเลือกรัฐให้ถูกต้อง รัฐที่ท่านเลือกไม่ได้อยู่ในประเทศ USA ');
		theForm.StateList.focus();
	    return false;
	}
	if ((theForm.CountryList[theForm.CountryList.selectedIndex].value == "CA" && theForm.StateList[theForm.StateList.selectedIndex].value.substring(0,2) != "CA"))
	{
	    alert('กรุณาเลือกรัฐให้ถูกต้อง รัฐที่ท่านเลือกไม่ได้อยู่ในประเทศ Canada ');
		theForm.StateList.focus();
	    return false;
	}
	if ((theForm.CountryList[theForm.CountryList.selectedIndex].value == "AU" && theForm.StateList[theForm.StateList.selectedIndex].value.substring(0,2) != "AU"))
	{
	    alert('กรุณาเลือกรัฐให้ถูกต้อง รัฐที่ท่านเลือกไม่ได้อยู่ในประเทศ Australia ');
		theForm.StateList.focus();
	    return false;
	}
	if ((theForm.CountryList[theForm.CountryList.selectedIndex].value == "AR" && theForm.StateList[theForm.StateList.selectedIndex].value.substring(0,2) != "AR"))
	{
	    alert('กรุณาเลือกรัฐให้ถูกต้อง รัฐที่ท่านเลือกไม่ได้อยู่ในประเทศ   Argentina ');
		theForm.StateList.focus();
	    return false;
	}
	if ((theForm.CountryList[theForm.CountryList.selectedIndex].value == "BR" && theForm.StateList[theForm.StateList.selectedIndex].value.substring(0,2) != "BR"))
	{
	    alert('กรุณาเลือกรัฐให้ถูกต้อง รัฐที่ท่านเลือกไม่ได้อยู่ในประเทศ   Brazil');
		theForm.StateList.focus();
	    return false;
	}
	if(theForm.PostalCode.value == "" && theForm.CountryList[theForm.CountryList.selectedIndex].value != "HK")
	{
	    alert('กรุณากรอกข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ใส่รหัสไปรษณีย์ ');
		theForm.PostalCode.focus();
	    return false;
	}
	if((theForm.emailAddr1.value == ""))
	{
	    alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ใส่อีเมล ');
		theForm.emailAddr1.focus();
	    return false;
	}
	if((theForm.emailAddr1.value.length > 0)&&(!isEmail(theForm.emailAddr1.value)))
	{
		alert('กรุณาใส่อีเมลของท่านให้ถูกต้อง และใส่ได้เพียง 1 อีเมลเท่านั้น');
		if(theForm.checkEmail.value == 'N') 
			theForm.TB110B.value="";
		theForm.emailAddr1.focus();
	    return false;
	}
	if (theForm.emailAddr1.value != theForm.emailAddr2.value)
	{
		alert('อีเมลที่ท่านใส่ไม่เหมือนกัน กรุณาลองใหม่อีกครั้ง ');
		theForm.emailAddr2.focus();
		return false;
	}
	if( (theForm.CountryCode1.value == "" || theForm.PhoneNbr1.value == ""))
	{
	    alert('ท่านต้องใส่เบอร์โทรศัพท์ที่ติดต่อได้สะดวก\nเบอร์ติดต่อจำเป็นจะต้องมี รหัสประเทศ และ รหัสพื้นที่ที่ถูกต้อง ');
		theForm.CountryCode1.focus();
	    return false;
	}	
	if(!checkCompletePhones(theForm)){		
		return false;
	}
	return true;	
}

function checkCompletePhones(theForm)
{	
	for(var i=1;i<=4;i++) {
		var ptval = i==1?theForm.elements["PhoneType"+i].value:theForm.elements["PhoneType"+i].options[theForm.elements["PhoneType"+i].selectedIndex].value;
	
		if(ptval != "*")
		{
			if (theForm.elements["CountryCode"+i].value == "") {
				alert("กรุณาใส่ รหัสประเทศ ให้ถูกต้อง"+i);
				theForm.elements["CountryCode"+i].focus();
				return false;
			}
			if (theForm.elements["PhoneNbr"+i].value == "") {
				alert("กรุณาใส่หมายเลขโทรศัพท์ให้ถูกต้อง"+i);
				theForm.elements["PhoneNbr"+i].focus();
				return false;
			}		
    		if (isNaN(theForm.elements["CountryCode"+i].value) || isNaN(theForm.elements["PhoneNbr"+i].value) || isNaN(theForm.elements["AreaCode"+i].value) || isNaN(theForm.elements["ExtNbr"+i].value)) {
				alert("กรุณาใส่หมายเลขโทรศัพท์ให้ถูกต้อง"+i);
				theForm.elements["CountryCode"+i].focus();
				return false;
			}
			
    		if (theForm.elements["CountryCode"+i].value.search("-") != -1 || theForm.elements["PhoneNbr"+i].value.search("-") != -1 || theForm.elements["AreaCode"+i].value.search("-") != -1 || theForm.elements["ExtNbr"+i].value.search("-") != -1) {
				alert("กรุณาใส่หมายเลขโทรศัพท์ให้ถูกต้อง"+i);
				theForm.elements["CountryCode"+i].focus();
				return false;
			}
		   	if (theForm.elements["CountryCode"+i].value.indexOf(".") != -1 || theForm.elements["PhoneNbr"+i].value.indexOf(".") != -1 || theForm.elements["AreaCode"+i].value.indexOf(".") != -1 || theForm.elements["ExtNbr"+i].value.indexOf(".") != -1) {
				alert("กรุณาใส่หมายเลขโทรศัพท์ให้ถูกต้อง"+i);
				theForm.elements["CountryCode"+i].focus();
				return false;
			}
    		if (theForm.elements["CountryCode"+i].value.indexOf("+") != -1 || theForm.elements["PhoneNbr"+i].value.indexOf("+") != -1 || theForm.elements["AreaCode"+i].value.indexOf("+") != -1 || theForm.elements["ExtNbr"+i].value.indexOf("+") != -1) {
				alert("กรุณาใส่หมายเลขโทรศัพท์ให้ถูกต้อง"+i);
				theForm.elements["CountryCode"+i].focus();
				return false;
			}
		}
	}// end for
	return true;
}

function isEmail(str) {
		if(str == null) return false;
			str = str.toUpperCase();
		len=str.length;
		
		if(((str.charAt(0)<'A')||(str.charAt(0)>'Z'))&&((str.charAt(0)<'0')||(str.charAt(0)>'9'))) return false;
		if((str.charAt(len-1)<'A')||(str.charAt(len-1)>'Z')) return false;

		for( i=0; i<len; i++) {
			if(((str.charAt(i)<'A')||(str.charAt(i)>'Z'))&&((str.charAt(i)<'0')||(str.charAt(i)>'9'))&&(str.charAt(i)!='_')&&(str.charAt(i)!='-')&&(str.charAt(i)!='.')&&(str.charAt(i)!='@')) return false;
			if((str.charAt(i) != '.') && (str.charAt(i) != '-')&& (str.charAt(i) != '_')) continue;
			if(((str.charAt(i-1)<'A')||(str.charAt(i-1)>'Z'))&&((str.charAt(i-1)<'0')||(str.charAt(i-1)>'9'))) return false;
			if(((str.charAt(i+1)<'A')||(str.charAt(i+1)>'Z'))&&((str.charAt(i+1)<'0')||(str.charAt(i+1)>'9'))) return false;
			//if((str.charAt(i+1)<'A')||(str.charAt(i+1)>'Z')) return false;
		}

		i1 = str.indexOf('@');
		if(i1 < 0) return false;	// no @
		if(i1 != str.lastIndexOf('@')) return false;	// more @
		i2 = str.indexOf('.');
		i3 = str.lastIndexOf('.');
		i4  = str.indexOf("-");
		if(i2 < 0) return false;	// no .
		if(i1 > i3) return false;	// no . after @
		if(i4 < 0) return true; // can no -
		//if((i4>=0)&&(i1>i4)) return false; // have - but - must after @

		return true;
	}

// ----------------------------------------------
// check applicant' s contact information
// ----------------------------------------------
function checkAppInfo(theForm)
{  

	if((theForm.birthdate.value == "")||(theForm.birthdate.value == "0-0-0"))	
    {
         alert ('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้เลือก วันเดือนปีเกิด');
		 theForm.dd.focus();
         return false;
    }
	if(!checkBirthDate(theForm.birthdate.value)) 
	{
		alert('ท่านจะต้องมีอายุ 2 ปีขึ้นไปในการสมัครเป็นสมาชิก รอยัล ออร์คิด พลัส');
		theForm.yy.focus();
         return false;
	}
    if (!(theForm.Gender[0].checked) && !(theForm.Gender[1].checked))
    {
        alert ('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้เลือกเพศ');
		theForm.Gender[0].focus();
        return false;
    }   
    return true;
}

function checkBirthDate(bd)
{
	bDate = bd.split("-");
	//bDate = new Date(tmp[0],tmp[1]-1,tmp[2]);
	cDate = new Date();
	if((cDate.getFullYear() - bDate[0]) < 2)
		return false;
	if((cDate.getFullYear() - bDate[0]) == 2)
	{
		if((cDate.getMonth() - (bDate[1]-1)) < 0)
			return false;
		if((cDate.getMonth() - (bDate[1]-1)) == 0)
		{
			if((cDate.getDate() - bDate[2]) < 0)
				return false;
			else
				return true;
		}
		return true;
	}
	return true;	
}

// ----------------------------------------------
// check applicant' s contact information
// ----------------------------------------------
function checkHint(theForm)
{
	if(theForm.answer1.value == "") {
		alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ตอบคำถามข้อที่ 1');
		theForm.answer1.focus();
		return false;
	}
	if(theForm.answer2.value == "") {
		alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ตอบคำถามข้อที่ 2 ');
		theForm.answer2.focus();
		return false;
	}
	if(theForm.answer3.value == "") {
		alert('กรุณาใส่ข้อมูลในช่องที่มีเครื่องหมาย* ให้ครบถ้วน \n\n ท่านยังไม่ได้ตอบคำถามข้อที่ 3 ');
		theForm.answer3.focus();
		return false;
	}
	return true;
}

function checkMemberSpnosor(theForm) {
	if (theForm.MemberSponsor.value != "")
    {
        if (theForm.MemberSponsor.value.length != 7)
        {
            alert('All member IDs contain 7 characters. \n\nPlease correct the sponsorship field');
			theForm.MemberSponsor.focus();
            return false;
        }        
        if (isNaN(theForm.MemberSponsor.value.substring(2,7)))
        {
            alert ('All member ID have the Format: AA00000.\n\nPlease correct the sponsorship field');
			theForm.MemberSponsor.focus();
            return false;
        }
    }
	return true;
}

function checkCity(theForm) {
	theForm.TB016B.value="Y";
	if(theForm.CityList[theForm.CityList.selectedIndex].value != "Other")
		theForm.OtherCity.value = "";
}

function IsString1(data,msg) {
	tmp = data.value;
	for(i = 0;i < tmp.length;i++) {
		if(isEmpty(data)) 
			return true;
		if(!isLetter1(tmp.charAt(i))) {
			alert(msg);
			data.select();
			return false;
		}	
	}
	return true;
}

function IsString2(data,msg) {
	tmp = data.value;
	for(i = 0;i < tmp.length;i++) {
		if(isEmpty(data)) 
			return true;
		if(!isLetter2(tmp.charAt(i))) {
			alert(msg);
			data.select();
			return false;
		}	
	}
	return true;
}

function IsString3(data,msg) {
	tmp = data.value;
	for(i = 0;i < tmp.length;i++) {
		if(isEmpty(data)) 
			return true;
		if(!isLetter3(tmp.charAt(i))) {
			alert(msg);
			data.select();
			return false;
		}	
	}
	return true;
}


function isLetter1 (c){   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c==" "))
}

function isLetter2 (c){   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c==" ") || (c==".") || (c==","))
}

function isLetter3 (c){   
	return ( ((c >= "0") && (c <= "9")) || ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c==" ") || (c==".") || (c==",") || (c=="/") || (c=="#") || (c=="*") || (c=="-"))
}


function isDigit (c){   
	return ((c >= "0") && (c <= "9"))
}

function isEmpty(s){   
	return ((s == null) || (s.length == 0))
}


// ***************************************************************************
//                               END OF SCRIPT
// ***************************************************************************