//=============================================
// isEmptyField() :
//=============================================
function isEmptyField(fld) {
	return (trim(fld).length == 0);
}

//=============================================
// isValidEmailAddress
//=============================================
function isValidEmailAddress(email) {
	// can't be empty
	if (isEmptyField(email)) return false;
	
	// can't contain any invalid character
	invalidChars = " /:,;";
	for (i=0;  i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) return false;
	}
	
	//  there must be one "@" character
	atPos = email.indexOf("@",1);
	if (atPos == -1) return false;
	
	// there must be at last one "." after the "@"
	dotPos = email.indexOf(".", atPos);
	if (dotPos == -1) return false;
	
	// there must be at least two characters after the "."
	if (dotPos + 3 > email.length) return false;
	
	return true;
}

//=============================================
// isValidDate(targetDate) : returns true if date is in YYYY-MM-DD format, otherwise returns false.
// IN: targetDate - the date to validate.
//=============================================
function isValidDate(targetDate) {
	// Date must be 10 characters.
	if (targetDate.length != 10) {
		return false;
	}
	
	// Check for a valid year.
	year = Number(targetDate.substr(0,4));
	today = new Date();
	currentYear = today.getFullYear();
	if ( (year  <  (currentYear - 50)) || (year > (currentYear  + 50)) ) {
		return false;
	}
	
	// Check for a valid month.
	month = Number(targetDate.substr(5,2));
	if (month < 1 || month > 12) {
		return false;
	}
	
	// Check for a valid day.
	day = Number(targetDate.substr(8,2));
	if (day < 1 || day > 31) {
		return false;
	}
	if (month == 2 && day > 29) {
		return false;
	}
	
	return true;
}

//=============================================
// requiredField
//=============================================
function requiredField(fld, label) {
	msg = '';
	fld = trim(fld);
	if (fld.length == 0) {
		msg = "\n'" + label + "' is required.";
	}
	return msg;
}	
