/* 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 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, txtNameEn, txtSurnameEn){
	var firstNameTh = txtNameTh.value;
	var lastNameTh = txtSurnameTh.value;
	var firstNameEng = txtNameEn.value;
	var lastNameEng = txtSurnameEn.value;
	
	if (isThai(firstNameTh) == false) {
		alert("Name (in Thai) - enter only Thai characters");
		txtNameTh.focus();
		return false;
	}
	if (isThai(lastNameTh) == false) {
		alert("Last Name (in Thai) - enter only Thai characters");
		txtSurnameTh.focus();
		return false;
	}
	if (isEnglish(firstNameEng) == false) {
		alert("Name (in English) - enter only English characters");
		txtNameEn.focus();
		return false;
	}
	if (isEnglish(lastNameEng) == false) {
		alert("Last Name (in English) - enter only English characters");
		txtSurnameEn.focus();
		return false;
	}
	return true;
}

function validateNames(userIndex) {
	with (document.frmInput) {
		if(userIndex == 1){
			return validateNamesFull(txtNameThY1, txtSurnameThY1, txtNameEnY1, txtSurnameEnY1);
		} else if(userIndex == 2){
			return validateNamesFull(txtNameThY2, txtSurnameThY2, txtNameEnY2, txtSurnameEnY2);
		} else if(userIndex == 3){
			return validateNamesFull(txtNameThY3, txtSurnameThY3, txtNameEnY3, txtSurnameEnY3);
		} else {
			return validateNamesFull(txtNameTh, txtSurnameTh, txtNameEn, txtSurnameEn);
		}
	}
	return true;
}

function validateIdCardAndPassportFull(txtIdCard, txtPassport) {	
		if(txtIdCard.value != "" && txtPassport.value != ""){
			alert("Please enter only Identification No or Passport No.");
			return false;
		} else if(txtIdCard.value == "" && txtPassport.value == ""){
			alert("Pleaes enter EITHER Identification No or Passport No. Identification No must be number having 13 characters long.");
			return false;
		} else if(txtIdCard.value != ""){
			if(isNumber(txtIdCard.value, false, false) && checkIDCard(txtIdCard.value)){
				return true;
			} else {
				alert("Incorrect identification no");
				return false;
			}
		} else if(txtPassport.value != ""){
			if(validatePassport(txtPassport.value) == true){
				return true;
			} else {
				alert("Incorrect passport no");
				return false;
			}
		} else {
			return false;
		}	
}

function validateIdCardAndPassport(userIndex) {	
	with (document.frmInput) {
		if(userIndex == 1){
			return validateIdCardAndPassportFull(txtIdCardY1, txtPassportY1);
		} else if(userIndex == 2){
			return validateIdCardAndPassportFull(txtIdCardY2, txtPassportY2);
		} else if(userIndex == 3){
			return validateIdCardAndPassportFull(txtIdCardY3, txtPassportY3);
		} else {
			return validateIdCardAndPassportFull(txtIdCard, txtPassport);
		}
	}
	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 == 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;
}