/**
 *	This script check form validity
 *	Example is at the bottom.
 */

/**
 *	Check text is not '' and not = nottext
 *	Also show alert message
 */
function checkText(obj, nottext, text) {
	var regExp = /^\s*$/;
	if (regExp.test(obj.value) || (obj.value == nottext)) {
		alert(text);
		obj.focus();
		return false;
	} else {
		return true;
	}
}

/**
 *	Check text is not in formatreg format, where formatreg is RegExp object
 *	Also show alert message
 */
function checkFormatText(obj, formatreg, text) {
	var regExp = formatreg;
	if (!regExp.test(obj.value)) {
		alert(text);
		obj.focus();
		return false;
	} else {
		return true;
	}
}

/**
 *	Check whether email is valid or not
 *	Also show alert message
 */
function checkEmail(obj, text)
{
	var str = obj.value;
	var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check = /@[\w\-]+\./;
	var checkend = /\.[a-zA-Z]{2,3}$/;

	// Check email format
	if (!(!exclude.test(str) && check.test(str) && checkend.test(str))) {
		alert(text);
		obj.focus();
		return false;
	} 	else {
		return true;
	}
}

/**
 *	Check password in pw and cpw are valid?
 *	Also show alert message
 */
function checkPassword(pw, cpw, text) {
	if (pw.value.length < 6) {
		alert('Password must be at least 6 characters');
		pw.focus();
		return false;
	}
	var regExp = /\s/;
	if (regExp.test(pw.value)) {
		alert('Password cannot contain space characters');
		pw.focus();
		return false;
	}
	if (checkText(pw, '', text)) {
		if (pw.value == cpw.value) {
			return true;
		} else {
			alert('Your confirm password is not match your password, please retype');
			cpw.focus();
			return false;
		}
	} else {
		pw.focus();
		return false;
	}
}

function checkDate(dd, mm, yy, text) {
	if (!(	checkText(dd, 'Date', 'Please select date') && 
			checkText(mm, 'Month', 'Please select month') && 
			checkText(yy, 'Year', 'Please select year'))) {
		return false;
	}
	return true;
}

/**
 *	Check if two textbox is not empty and match each other
 */
function check2Text(obj, obj2, nottext, text, notmatchtext) {
	var regExp = /^\s*$/;
	if (regExp.test(obj.value) || (obj.value == nottext)) {
		alert(text);
		obj.focus();
		return false;
	} else if (regExp.test(obj2.value) || (obj2.value == nottext)) {
		alert(text);
		obj2.focus();
		return false;
	} else if (obj.value != obj2.value) {
		alert(notmatchtext);
		obj2.focus();
		return false;
	} else {
		return true;
	}
}

/**
 *	Check if check boxes are checked some or not
 */
function checkBox(obj, text) {
	if ((obj.length == null) && (obj.checked)) {
		return true;
	}
	for (i = 0; i < obj.length; i++) {
		if (obj[i].checked == true) {
			return true;
		}
	}
	alert(text);
	if (obj.length == null) {
		obj.focus();
	} else {
		obj[0].focus();
	}
	return false;
}

/**
 *	Check number of character user enter in text field
 *	return false if #character greater than maxlimit then alert "text"
 */
function checkMaxTextLength(field, maxlimit, text) {
	if (field.value.length > maxlimit) {
		if (text != undefined) {
			alert(text);
		}
		return false;
	}
	return true;
}

/**
 *	Check number of character user enter in text field
 *	return false if #character less than minlimit then alert "text"
 */
function checkMinTextLength(field, minlimit, text) {
	if (field.value.length < minlimit) {
		if (text != undefined) {
			alert(text);
		}
		return false;
	}
	return true;
}

/**
 *	Check min/max number of character enter in text field
 *	return false if #character < minlimit or > maxlimit then alert "text"
 */
function checkTextLength(field, minlimit, maxlimit, text) {
	return checkMinTextLength(field, minlimit, text) && checkMaxTextLength(field, maxlimit, text);
}

/**
 *	Check text length and set new value to text if length exceed limit
 *	Also show alert message
 */
function limitTextLength(field, minlimit, maxlimit, text) {
	if ((minlimit > 0) && !(checkMinTextLength(field, minlimit, text))) {
		return false;
	}
	if ((maxlimit > 0) && !(checkMaxTextLength(field, maxlimit, text))) {
		field.value = field.value.substring(0, maxlimit - 1);
		return false;
	}
	return true;
}

/*	Sample source code for  validate from
function validateForm(form) {
	if (!(	checkText(form.name, '', 'กรุณากรอกชื่อภาษาไทยด้วยค่ะ') &&
			checkText(form.lastname, '', 'กรุณากรอกนามสกุลภาษาไทยด้วยค่ะ') &&
			checkText(form.ename, '', 'กรุณากรอกชื่อภาษาอังกฤษด้วยค่ะ') &&
			checkText(form.elastname, '', 'กรุณากรอกนามสกุลภาษาอังกฤษด้วยค่ะ') &&
			checkEmail(form.email, 'กรุณากรอก email ที่ถูกต้องด้วยค่ะ') &&
			checkPassword(form.password, form.cpassword, 'กรุณากรอกรหัสผ่านให้ถูกต้องด้วยค่ะ') &&
			checkText(form.address, '', 'กรุณากรอกที่อยู่ด้วยค่ะ') &&
			checkDate(form.date, form.month, form.year, 'กรุณากรอกวันเกิดด้วยค่ะ') &&
			checkText(form.job, 'กรุณาเลือกอาชีพด้วยค่ะ', 'กรุณาเลือกอาชีพด้วยค่ะ'))) {
		return false;
	} else {
		return true;
	}
}
*/
