﻿var WardLib = window.WardLib || {};

WardLib.ValidationTestManager = function()
{
	var _IsEmpty = function(sValue)
	{
		return (sValue == "");
	}
	
	var _IsDate = function(iYear,iMonth,iDay)
	{
	/*
		try
		{
			var tempDate = new Date(iYear,iMonth,iDay);
			return true;
		}
		catch
		{
			return false;
		}
		*/
	}
	
	var _MatchesRegex = function(sValue, sRegex)
	{
		return sRegex.test(sValue);
	}

	var _FieldsMatch = function(sValue1, sValue2)
	{
		return (sValue1 == sValue2);
	}	
	
	var _ItemChosenFromList = function(sValue)
	{
		return (sValue != "");
	}
	
	var _IsBetweenDates = function()
	{
	
	}
	
	var _IsOldEnough = function(sDateOfBirth, iAgeLimit)
	{
		var today = new Date();
		var targetBirthday = new Date(sDateOfBirth);

		var differenceInSeconds = today.getTime()- targetBirthday.getTime(); 

		var age = differenceInSeconds / (1000 * 60 * 60 *24 * 365)

		return (age > iAgeLimit);
	}
	
	return {
		IsEmailAddress : function(sValue)
		{
			var sRegexPattern = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
			return sRegexPattern.test(sValue);
			
			return _MatchesRegex(sValue,sRegexPattern);
			
		},
		IsUrl : function(sValue)
		{
			//TODO : Implement IsUrl
			return _MatchesRegex(sValue,''); //need regex for urls - not for this project, but would be a nice-to-have
		},
		IsInteger : function(iValue)
		{
			//TODO : Implement IsInteger
		},
		IsDate : function(iDay,iMonth,iYear,sLocale)
		{
			//TODO : Implement localisation for IsDate
			return _IsDate(iYear,iMonth,iDay);
		},
		IsOldEnough : function(sDateOfBirth, iAgeLimit)
		{
			//TODO : Would rather implement _IsBetweenDates than use this method
			return _IsOldEnough(sDateOfBirth, iAgeLimit);
		},
		FieldsMatch : function(sValue1, sValue2)
		{
			return _FieldsMatch(sValue1, sValue2);
		},
		IsEmpty : function(sValue)
		{
			return _IsEmpty(sValue);
		},
		ItemChosenFromList : function(sValue)
		{
			return _ItemChosenFromList(sValue);
		}
	}
	
}();