/* BEGIN CODE FOR LANG */
function isThai(s) {
	return determineLang(s, false);
}

/* bool == false -> thai
   bool == true -> eng*/
function determineLang(s, bool) {
	if (s != null) {
		for (var i = 0; i < s.length; i++) {
			var c = s.substring(i, i+ 1);
			if (isThaiCharacter(c) == bool && isCommonChar(c) == false) {
				return false;
			}
		}
	}
	return true;
}

function isThaiCharacter(c) {
	var valid = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤฤลฦฦวศษสหฬอฮอะอัอาอำอิอีอึอือุอูเอแอโอใอไออ่อ้อ๊อ๋อ็อ์ฯๆ";
	return valid.indexOf(c) != -1;
}

function isEnglish(s) {
	return determineLang(s, true);
}


function validateUsername(s) {
	if (s != null) {
		if(s.length < 4 || s.length > 10){
			return false;
		}
		for (var i = 0; i < s.length; i++) {
			var c = s.substring(i, i+ 1);
			if (!isUsernameCharacter(c)) {
				return false;
			}
		}
	} else {
		return false;
	}
	return true;
}

function validatePassword(s) {
	if (s != null) {
		if(s.length < 6 || s.length > 10){
			return false;
		}
	} else {
		return false;
	}
	return true;
}

function isUsernameCharacter(c) {
		var valid = "abcdefghijklmnopqrstuvwxyz0123456789-_.";
		return valid.indexOf(c) != -1;
}

function isCommonChar(c) {
	var valid = "&()-_',. ";
	return valid.indexOf(c) != -1;
}

function isEngChar(c) {
		var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		return valid.indexOf(c) != -1;
}

function isNumberChar(c) {
	var valid = "0123456789";
	return valid.indexOf(c) != -1;
}

function isNumber(inputVal, allownegative, allowdecimal) {
    oneDecimal = false
    inputStr = "" + inputVal

    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i)
        if (i == 0 && oneChar == "-") {
            if (allownegative==true) 
                continue
            else
                return false
        }

        if (oneChar == "." && !oneDecimal) {
            if (allowdecimal==true) {
                oneDecimal = true
                continue
            }
            else
                return false
        }

        if (oneChar < "0" || oneChar > "9") {
            return false
        }
   }
   return true
}

function validatePassport(s) {
	if (s != null) {
		/*
		for (var i = 0; i < s.length; i++) {
			var c = s.substring(i, i+ 1);
			if (!isEngChar(c) && !isNumberChar(c)) {
				return false;
			}
		}
		*/
		return true;
	} else {
		return false;
	}
	//return true;
}

/* END CODE FOR LANG */

//function getAge(d) {
//	var mil = d.getTime();
//	var todayMil = (new Date()).getTime();
//	return (todayMil - mil) / (1000 * 60 * 60 * 24 * 365);
//}

// year - 4 digits
// month - values from 1 to 12.
function isEligible(year, month, day, compareYear, compareMonth, compareDay) {		
	var date1 = new Date(year, month - 1, day);
	var date2 = new Date(compareYear, compareMonth, compareDay);
	return date1.valueOf() <= date2.valueOf();
}

function getSelectedValue(select) {
	var index = select.selectedIndex;
	return select.options[index].value;
}

function validateNamesFull(txtNameTh, txtSurnameTh){
	var firstNameTh = txtNameTh.value;
	var lastNameTh = txtSurnameTh.value;
	if(firstNameTh == ""){
		alert("กรุณากรอกชื่อ");
		txtNameTh.focus();
		return false;
	}
	if(lastNameTh == ""){
		alert("กรุณากรอกนามสกุล");
		txtSurnameTh.focus();
		return false;
	}
	return true;
}

function validateNames(userIndex) {
	with (document.frmInput) {
		if(userIndex == 1){
			return validateNamesFull(txtNameThY1, txtSurnameThY1);
		} else if(userIndex == 2){
			return validateNamesFull(txtNameThY2, txtSurnameThY2);
		} else if(userIndex == 3){
			return validateNamesFull(txtNameThY3, txtSurnameThY3);
		} else {
			return validateNamesFull(txtNameTh, txtSurnameTh);
		}
	}
	return true;
}

function validateIdCardAndPassportFull(txtIdNo) {	
		if(txtIdNo == ""){
			alert("กรุณากรอกเลขที่บัตรประชาชน 13 หลัก.");
			return false;
		} else {
			if(isNumber(txtIdNo, false, false) && checkIDCard(txtIdNo)){
				return true;
			} else {
				alert("เลขที่บัตรประชาชนไม่ถูกต้อง.");
				return false;
			}
		}
}

function validateIdCardAndPassport(userIndex) {
	var txtIdNo = "";
	with (document.frmInput) {
		if(userIndex == 1){
			if(document.frmInput.txtIdCardY1){
				txtIdNo = txtIdCardY1.value;
			} else {
				txtIdNo += txtIdCard0Y1.value;
				txtIdNo += txtIdCard1Y1.value;
				txtIdNo += txtIdCard2Y1.value;
				txtIdNo += txtIdCard3Y1.value;
				txtIdNo += txtIdCard4Y1.value;
				txtIdNo += txtIdCard5Y1.value;
				txtIdNo += txtIdCard6Y1.value;
				txtIdNo += txtIdCard7Y1.value;
				txtIdNo += txtIdCard8Y1.value;
				txtIdNo += txtIdCard9Y1.value;
				txtIdNo += txtIdCard10Y1.value;
				txtIdNo += txtIdCard11Y1.value;
				txtIdNo += txtIdCard12Y1.value;
			}
		} else if(userIndex == 2){
			if(document.frmInput.txtIdCardY2){
				txtIdNo = txtIdCardY2.value;
			} else {
				txtIdNo += txtIdCard0Y2.value;
				txtIdNo += txtIdCard1Y2.value;
				txtIdNo += txtIdCard2Y2.value;
				txtIdNo += txtIdCard3Y2.value;
				txtIdNo += txtIdCard4Y2.value;
				txtIdNo += txtIdCard5Y2.value;
				txtIdNo += txtIdCard6Y2.value;
				txtIdNo += txtIdCard7Y2.value;
				txtIdNo += txtIdCard8Y2.value;
				txtIdNo += txtIdCard9Y2.value;
				txtIdNo += txtIdCard10Y2.value;
				txtIdNo += txtIdCard11Y2.value;
				txtIdNo += txtIdCard12Y2.value;
			}
		} else if(userIndex == 3){
			if(document.frmInput.txtIdCardY3){
				txtIdNo = txtIdCardY3.value;
			} else {
				txtIdNo += txtIdCard0Y3.value;
				txtIdNo += txtIdCard1Y3.value;
				txtIdNo += txtIdCard2Y3.value;
				txtIdNo += txtIdCard3Y3.value;
				txtIdNo += txtIdCard4Y3.value;
				txtIdNo += txtIdCard5Y3.value;
				txtIdNo += txtIdCard6Y3.value;
				txtIdNo += txtIdCard7Y3.value;
				txtIdNo += txtIdCard8Y3.value;
				txtIdNo += txtIdCard9Y3.value;
				txtIdNo += txtIdCard10Y3.value;
				txtIdNo += txtIdCard11Y3.value;
				txtIdNo += txtIdCard12Y3.value;
			}
		} else {
			if(document.frmInput.txtIdCard){
				txtIdNo = txtIdCard.value;
			} else {
				txtIdNo += txtIdCard0.value;
				txtIdNo += txtIdCard1.value;
				txtIdNo += txtIdCard2.value;
				txtIdNo += txtIdCard3.value;
				txtIdNo += txtIdCard4.value;
				txtIdNo += txtIdCard5.value;
				txtIdNo += txtIdCard6.value;
				txtIdNo += txtIdCard7.value;
				txtIdNo += txtIdCard8.value;
				txtIdNo += txtIdCard9.value;
				txtIdNo += txtIdCard10.value;
				txtIdNo += txtIdCard11.value;
				txtIdNo += txtIdCard12.value;
			}
		}
		return validateIdCardAndPassportFull(txtIdNo);
	}
	return true;
}

function checkIDCard(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; 
	} else {
		return true;
	}
}

function clickMrRadioFull(txtGender) {
	txtGender[0].disabled = false;
	txtGender[1].disabled = true;
	txtGender[0].checked = true;
}

function clickMrsMissRadioFull(txtGender) {
	txtGender[0].disabled = true;
	txtGender[1].disabled = false;
	txtGender[1].checked = true;
}

function clickOtherRadioFull(txtGender) {
	txtGender[0].disabled = false;
	txtGender[1].disabled = false;
}


function clickMrRadio(userIndex) {
	with (document.frmInput) {
		if(userIndex == 1){
			return clickMrRadioFull(txtGenderY1);
		} else if(userIndex == 2){
			return clickMrRadioFull(txtGenderY2);
		} else if(userIndex == 3){
			return clickMrRadioFull(txtGenderY3);
		} else {
			return clickMrRadioFull(txtGender);
		}
	}
}

function clickMrsMissRadio(userIndex) {
	with (document.frmInput) {
		if(userIndex == 1){
			return clickMrsMissRadioFull(txtGenderY1);
		} else if(userIndex == 2){
			return clickMrsMissRadioFull(txtGenderY2);
		} else if(userIndex == 3){
			return clickMrsMissRadioFull(txtGenderY3);
		} else {
			return clickMrsMissRadioFull(txtGender);
		}
	}
}

function clickOtherRadio(userIndex) {
	with (document.frmInput) {
		if(userIndex == 1){
			return clickOtherRadioFull(txtGenderY1);
		} else if(userIndex == 2){
			return clickOtherRadioFull(txtGenderY2);
		} else if(userIndex == 3){
			return clickOtherRadioFull(txtGenderY3);
		} else {
			return clickOtherRadioFull(txtGender);
		}
	}
}

function isValidEmail(email) {
	return (email != null && email.indexOf('@') != -1);
}

function validateDate(strDate, strMonth, strYear){
	if(strMonth == 0 || strDate == 0 || strYear == 0){
		return false;
	}
	if(strMonth == 1 || strMonth == 3 ||strMonth == 5 ||strMonth == 7 ||strMonth == 8 ||strMonth == 10 ||strMonth == 12){
		return true;
	} else if(strMonth == 2) {
		if((strYear % 4 == 0) && (strYear % 100 != 0)){
			if(strDate > 29){
				return false;
			}
		} else {
			if(strDate > 28){
				return false;
			}
		}
	} else if(strMonth == 4 || strMonth == 6 ||strMonth == 9 ||strMonth == 11){
		if(strDate > 30){
			return false;
		}
	}
	return true;
}

function checkpassword(field1, type1, chknull, fname) {
    var   isvalid = true
    if (chknull) {
         if (field1.value == null || field1.value == "") {
             alert("กรุณาระบุค่า" + fname)
             isvalid=false
         }
    }
    strVal = field1.value
    if (isvalid==true)   {
    	var offset = 0
			offset = strVal.indexOf(' ')
			if (offset != -1)   {
				alert("ห้ามมีเว้นวรรคใน" + fname)	     		
		    	isvalid = false
			}
	}
	
    strVal = field1.value
    if (isvalid==true) {          
         if( (strVal.length < 6) || (strVal.length > 10))
         {
             alert(fname + "ต้องเป็นตัวอักษรภาษาอังกฤษ 6-10 ตัวอักษร")
             isvalid=false
         }
    }
    if (isvalid==false) {
        if (field1.type == "text") {
            field1.focus()
        }
        return false
    } else { 
        return true
    }
}

function checkfield(field1, type1, nlen, chknull, fname) {
    var  isvalid =true;
    
    if (chknull) {
         if (field1.value == null || field1.value == "") {
             alert("กรุณาระบุค่า" + fname)
             isvalid=false
         }
    }

    strVal=field1.value;
    if (nlen != 0 && isvalid==true) {          
         if (strVal.length < nlen) {
             alert(fname + " minimum " + nlen + " of character.");
             isvalid=false;
         }
    }


    if (type1 == "n-" && isvalid==true) {
         if (!isNumber(field1.value, true, true)) {
             alert(fname + "ต้องเป็นตัวเลขเท่านั้น");
             isvalid=false;
         }
    }

    if (type1 == "n" && isvalid==true) {
         if (!isNumber(field1.value, false, true)) {
             alert(fname + "ต้องเป็นตัวเลขเท่านั้น");
             isvalid=false;
         }
    }

    if (type1 == "i-" && isvalid==true) {
         if (!isNumber(field1.value, true, false)) {
             alert(fname + "ต้องเป็นตัวเลขเท่านั้น");
             isvalid=false;
         }
    }

    if (type1 == "i" && isvalid==true) {
         if (!isNumber(field1.value, false, false)) {
             alert(fname + "ต้องเป็นตัวเลขเท่านั้น");
             isvalid=false;
         }
    }

  			   
//------------------tested------------------------------------------//			   
			   
   
   if (isvalid==true)
   {
      if (field1.type == "text") 
      {
            field1.focus();
        }
        return true;
    } else { 
        return false;
    }
}
