// A very simple library of regular expression based
// form field validation functions

// Programmer: Darryl Ballard
// Date Created: 5/19/2007
// Last Update: 5/21/2007
// Version: 1.0

function ffv_isPhoneNumber(str)
{
	// 3 digits, a dash, and 4 digits
	var regExp = /^\d{3}-\d{4}$/;
	
	return str.match(regExp);
}

function ffv_isAreaCode(str)
{
	// 3 digits
	var regExp = /^\d{3}$/;
	
	return str.match(regExp);
}

function ffv_isZipCode5or9(str)
{
	var regExp = /^\d{5}$|^\d{5}-\d{4}$/;
	
	return str.match(regExp);
}

function ffv_isEmailAddress(str)
{
	var regExp = /^.+@[^\.].*\.[a-z]{2,}$/;
	
	return str.match(regExp);
}

function ffv_isPIN(str)
{
	// 4 digits
	var regExp = /^\d{4}$/;
	
	return str.match(regExp);
}

function ffv_isDigits(str)
{
	// From 1 to any number of only digits
	var regExp = /^\d+$/;
	
	return str.match(regExp);
}

function ffv_ccExpirationMonth(strMonth, strYear)
{
	var nMonth = parseInt(strMonth);
	var nYear = parseInt(strYear);
	var d = new Date();
	var currentYear = d.getYear() + 1900; // Year "1" is 1901
	var currentMonth = d.getMonth() + 1; // Months start at zero
	
	if( nYear > currentYear )
	{
		// For future years, check only that the month is 1 through 12
		if( nMonth >= 1 && nMonth <= 12 )
		{
			return true;
		}		
	}

	if( nYear == currentYear )
	{
		// For this year, allow only months from current up to December
		if( nMonth > currentMonth && nMonth <= 12 )
		{
			return true;
		}
	}
	
	return false;
}

function ffv_ccExpirationYear(strYear)
{
	var nYear = parseInt(strYear);
	var d = new Date();
	var currentYear = d.getYear() + 1900; // Year "1" is 1901
	
	if( nYear >= currentYear && nYear < 2050 )
	{
		return true;
	}

	return false;
}

function ffv_isCVV(strCVV, strCardType)
{
	var regExp;
	
	switch( strCardType.toLowerCase() )
	{
	case "visa":
	case "mc":
	case "mastercard":
	case "master card":
		regExp = /^\d{3}$/;
	break;
	
	case "amex":
	case "americanexpress":
	case "american express":
		regExp = /^\d{4}$/;
	break;
	}
	
	return strCVV.match(regExp);
}
