package classes.matuto.utils.model
{
	

	public class MtMath
	{
		
		public static function random(min :Number = NaN, max :Number = NaN) :Number
		{
			if(isNaN(min)) min = 0;
			if(isNaN(max)) max = 0;
			
			var value :Number;
			
			value = Math.random() * (1+max-min) + min;
			
			return value;
			
		}
				
		public static function ceil(value :Number) :int
		{
			value = value==int(value) ? value : value >= 0 ? int(value+1) : int(value);
			
			return value;
		}
				
		public static function floor(value :Number) :int
		{
			return (value>>0);
		}
		
		public static function abs(value :Number) :Number
		{			
			return (value ^ (value >> 31)) - (value >> 31);
		}
		
		public static function mod(x :Number, y :Number) :Number
		{
			return (x%y);
		}
		
		public static function flip(value :Number) :Number
		{
			return ~value + 1;
		}
		
		public static function checkAge($ageToCheck:int, $month:int, $day:int, $year:int) :Boolean
		{
			
			var todayDate:Date = new Date();			
			var currentMonth:int = (todayDate.getMonth() + 1);			
			var currentDay:int = todayDate.getDate();			
			var currentYear:int = todayDate.getFullYear();
			
			var userMonth:int = $month;			
			var userDay:int = $day;			
			var userYear:int = $year;
			
			var yearDiff:int = (currentYear - userYear);			
			
			if (yearDiff == $ageToCheck)
			{
				
				// AGE IS EQUAL to VALID AGE ... need to check month and day
				var monthDiff:int = (currentMonth - userMonth);				
				
				if (monthDiff == 0)					
				{					
					// MONTH IS EQUAL ... need to check day					
					var dayDiff:int = currentDay - userDay;					
					
					if (dayDiff>= 0)						
					{						
						// DAY IS EQUAL OR GREATER .. PASS						
						return true;						
					}else{						
						// DAY INVALID ... too young						
						return false;
					}					
					
				}
					
				else if (monthDiff <0)					
				{
					// MONTH INVALID ... too young	
					return false;
				}else{
					// AGE PASS
					return true;
				}
				
			}else if (yearDiff <$ageToCheck){				
				// YEAR INVALID ... too young				
				return false;				
			}else{				
				// OVER AGE in YEARS				
				return true;				
			}
			
		}
		
	}
}