//purpose: to compare if date1 occurs before date 2
function dateDiff(dateValue1, dateValue2) {
	date1 = new Date();
	date2 = new Date();

	date1temp = new Date(dateValue1);
	date1.setTime(date1temp.getTime());

	date2temp = new Date(dateValue2);
	date2.setTime(date2temp.getTime());

	if (!(date1temp <= date2temp))
		return false;

	return true;
}

// purpose: to check a textfield / textarea if it is filled.
function isNumFilled(obj){
	if (obj.value.length<1){
		return false;
	}
	if (!isFloat(obj,true)) return false;
	return true;
}

// purpose: to check a group of check boxes for at least one box ticked
function isOneTicked(obj){
	if (obj[0].type!="checkbox" && obj[0].type!="radio")
		return false;
		
	for (var i=0;i<obj.length;i++){
		if (obj[i].checked==true)
			return true;
	}
	obj[0].focus();
	return false;
}
function VNAisOneTicked(obj, inmsg){
	return VNAisOneTicked(obj, inmsg, false);
}
function VNCAisOneTicked(obj, inmsg, cstmsg){
	if (!isOneTicked(obj)){
		var msg = '';
		if (cstmsg) msg = inmsg;
		else msg = 'Please specify ' + inmsg;
		fieldError(obj, msg);
		return false;
	}
	return true;
}
// purpose: to check a group of textfield / textarea for at least N box(es) filled
function isNFilled(obj, minfilled){
	if (obj[0].type!="text")
		return false;
	var str;
	var count = 0;
	for (var i=0;i<obj.length;i++){
		if (obj[i]!=null){
			str = new String(obj[i].value);
			if (str.length>0){
				count++;
			}
			if (count >= minfilled)
				return true;
		}
	}
	return false;
}
// purpose: to check a checkbox if it is checked
function isTicked(obj){
	if (!obj.checked){
		return false;
	}
	return true;
}
// purpose: to check a textfield / textarea if it is filled.
function isFilled(obj){
	if (obj.value.length<1){
		return false;
	}
	return true;
}

function VNAisFilled(obj, inmsg){
	return VNCAisFilled(obj, inmsg, false);
}
function VNCAisFilled(obj, inmsg, cstmsg){
	if (!isFilled(obj)){
		var msg = '';
		if (cstmsg) msg = inmsg;
		else msg = 'Please fill in ' + inmsg;
		fieldError(obj, msg);
		return false;
	}
	return true;
}
// purpose: to check a option menu if it has been selected
function isSelected(obj){
	if (obj[0].selected){
		return false;
	}
	return true;
}
function VNAisSelected(obj, inmsg){
	return VNCAisSelected(obj, inmsg, false);
}
function VNCAisSelected(obj, inmsg, cstmsg){
	if (!isSelected(obj)){
		var msg = '';
		if (cstmsg) msg = inmsg;
		else msg = 'Please select ' + inmsg;
		selectError(obj, msg);
		return false;
	}
	return true;
}
// purpose: to check if an entry is a valid email
function isEmail(obj){
	var str = new String(obj.value); 
	if (str.match(/^[a-zA-Z0-9_\.\-\+\#\%]+@[a-zA-Z0-9_\.\-]+\.[a-zA-Z]{2,3}$/) == null){
		return false;
	}
	return true;
}
// purpose: to check if an antry is a valid Malaysian postcode
function isPostCode(obj){
	var str = new String(obj.value); 
	if (str.match(/^[0-9]{5}$/) == null){
		return false;
	}
	return true;
}
// purpose: to check if an entry is a valid Malaysian mobile phone number
function isMobilePhone(obj){
	var str = new String(obj.value); 
	if (str.match(/^[0-9]{3}-[0-9]{7}$/) == null){
		return false;
	}
	return true;
}
// purpose: to check if an entry is a valid fixed line phone number
function isFixedPhone(obj){
	var str = new String(obj.value); 
	if (str.match(/^[0-9]{2,3}-[0-9]{6,8}$/) == null){
		return false;
	}
	return true;
}

// purpose: to check NRIC1
function isNRIC1(obj){
	var str = new String(obj.value);
	if (str.match(/^[0-9]{6}$/) == null) return false;
	return true;
}
// purpose: to check NRIC2
function isNRIC2(obj){
	var str = new String(obj.value);
	if (str.match(/^[0-9]{2}$/) == null) return false;
	return true;
}
// purpose: to check NRIC3
function isNRIC3(obj){
	var str = new String(obj.value);
	if (str.match(/^[0-9]{4}$/) == null) return false;
	return true;
}

function isMOBILE1(obj) {
	var str = new String(obj.value);
	if (str.match(/^01[0,2,3,6,7,8,9]{1}$/) == null) {
		return false; 
	}
	return true;
}
function isMOBILE2(obj) {
	var str = new String(obj.value);
	if (str.match(/^[0-9]{7}$/) == null) {
		return false; 
	}
	return true;
}

// purpose: to check a 3 field IC numbers for a valid entry length
function is3FNRIC(obj1, obj2, obj3){
	var str1 = new String(obj1.value);
	var str2 = new String(obj2.value);
	var str3 = new String(obj3.value);
	if (str1.match(/^[0-9]{6}$/) == null) return false;
	if (str2.match(/^[0-9]{2}$/) == null) return false;
	if (str3.match(/^[0-9]{4}$/) == null) return false;
	return true;
}
// purpose: to check the first field of an IC number if valid
function isValidNRIC(obj){
	var str = new String(obj.value);
	var vYear  = parseInt(str.substr(0,2));
	var vMonth = parseInt(str.substr(2,2));
	if (vMonth > 12){
		return false;
	}
	var vDate  = parseInt(str.substr(4,2));
	if (vDate > 31){
		return false;
	}
	return true;
}
// purpose: to check the entry if it is alphanumeric, with ignore white space flag
function isAlphanumeric(obj, ignoreWhiteSpace) {
	var string = new String(obj.value);
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)){
			return false;
		}
	}
	return true;
}

// purpose:  Check that a string contains only letters
function isAlphabetic(obj, ignoreWhiteSpace) {
	var string = new String(obj.value);
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)){
			return false;
		}
	}
	return true;
}

// purpose: Check for a valid name
function isName(obj, ignoreWhiteSpace) {
	var string = new String(obj.value);
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\@\'\-\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z\@\'\-]/) != -1)){
			return false;
		}
	}
	return true;
}

// purpose:  Check that a string contains only numbers
function isFloat(obj, ignoreWhiteSpace) {
	var string = new String(obj.value);
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\.\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D\./) != -1)){
			return false;
		}
	}
	return true;
}

// purpose:  Check that a string contains only numbers
function isNumeric(obj, ignoreWhiteSpace) {
	var string = new String(obj.value);
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)){
			return false;
		}
	}
	return true;
}
// purpose: 
function fieldError(elem, errMsg)
{
	if (elem.type == "textfield" || elem.type == "textarea" || elem.type=="select")
		elem.select();
	if (elem.length > 1) elem[0].focus();
	else elem.focus();
	alert(errMsg);
}

function selectError(elem, errMsg)
{
	if (elem.type == "textfield" || elem.type == "textarea" || elem.type=="select")
		elem.select();
	if (elem.length > 1 && elem.type == "text")  elem[0].focus();
	else elem.focus();
	alert(errMsg);
}