/*
 * common_validation.js
 *
 * support functions for all validation functions.
 */

/*
 * validateDate() error codes
 */
var ERR_INVALIDDATE = 1;
var ERR_EMPTYDATE = 2;

/*
 * validateEmailAddress() error codes
 */
var ERR_INVALIDEMAILADDRESS = 1;
var ERR_EMPTYEMAILADDRESS = 2;

/*
 * @brief Validates a date in UK format.
 *
 * @param v is the date to check. It must be
 * in the format DD/MM/YYYY.
 *
 * The following validation checks are performed:
 * - Year has 4 digits
 * - Month is in the range 1-12 inclusive
 * - Day is valid for the month
 *
 * Leap years are accommodated, with the exception
 * that pre-8AD leap year anomalies are not. Also,
 * the validation check will not reject any date
 * that falls within the dates that never occurred
 * due to the Gregorian adjustment in Septermber
 * 1752.
 */
function validateDate( v )
{
	if(v.replace(" ", "") == "")
		return ERR_EMPTYDATE;

	/*
	** temporarily accepting 00/00/0000 for those that have no
	** dob
	*/
	if(v == "00/00/0000")
		return 0;
	
	/*
	** parse the date
	*/
	var p1 = 0;
	var p2 = v.indexOf("/");

	if(p1 == -1)
		return ERR_INVALIDDATE;

	/* ignore leading zeros */
	while(v.charAt(p1) == "0" && p1 < p2)
		p1++;

	var d = parseInt(v.substr(p1, p2 - p1));
	p1 = p2 + 1;
	p2 = v.indexOf("/", p1);

	if(p2 == -1)
		return ERR_INVALIDDATE;

	/* ignore leading zeros */
	while(v.charAt(p1) == "0" && p1 < p2)
		p1++;

	var m = parseInt(v.substr(p1, p2 - p1));
	var y = parseInt(v.substr(p2 + 1));

	/*
	** validate the parsed date
	**
	** special cases like the pre-8AD lack of leap years and the
	** Gregorian calendar adjustment are not checked - they'll not
	** be needed.
	*/
	if(isNaN(y) || isNaN(m) || isNaN(d))
		return ERR_INVALIDDATE;

	/*
	** 1000 and 9999 force year to be 4 digits
	**
	** for now, we are accepting 00 as day and month
	** for those that don't have a valid date of birth
	*/
	if(y < 1000 || y > 9999 || d < 1 || m > 12 || m < 1)
		return ERR_INVALIDDATE;

	var maxDays = 31;

	switch(m)
	{
		case 2:
			/*
			** work out if it's a leap year:
			**
			** a year is a leap year if its year is divisible by 400
			** without remainder, or if its year is divisible by 4 without
			** remainder but not by 100 without remainder.
			*/
			if(	((y % 400) == 0) || (((y % 4) == 0) && ((y % 100) != 0)))
				maxDays = 29;
			else
				maxDays = 28;
		break;

		/* apr, jun, sep, nov have 30 days */
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		break;

		/* the rest have 31, which is the default above */
	}

	if(d > maxDays)
		return ERR_INVALIDDATE;

	return 0;
}


function validateEmailAddress( e )
{
	if(e == "")
		return ERR_EMPTYEMAILADDRESS;
	
	return 0;
}


function wordCount( s )
{
	/* trim leading space and append a single space char*/
	s = s.replace(/^[^A-Za-z0-9]+/gi, "") + " ";

	/* if all we have is the appended space, the string is empty */
	if(s.length < 2)
		return 0;
		
	return s.replace(/[^A-Za-z0-9]+/gi, " ").split(" ").length - 1;
}



/*
** this function is not internationally aware or punctuation aware
*/
function trimLeadingWhitespace( s )
{
	return s.replace(/^[^A-Za-z0-9]+/gi, "");//.replace(/[^A-Za-z0-9]+$/gi);
}

