//#
//#----------------------------------------------------------------------------
//#	validate_phone_number
//#
//#	  Parameters:
//#		  phone: the phone number
//#		  countryCode: the A2 country code whose rules apply;
//#
//#	  Purpose:
//#	  	Validates the format of a phone number, per country if
//#			rules are available.
//#
//#  	Return:
//#  		0   if valid
//#     -10 if invalid: not present
//#     -20 if invalid: invalid chars present
//#     -30 if invalid: invalid format
//#     
//#----------------------------------------------------------------------------
function validate_phone_number( phone, countryCode )
{

	//# not present or ""?
	if (!phone || (phone.length == 0) )
	{
		return( -10 );
	}

	
	// ####################################################################
	// For each country, be sure to assign both format and chars patterns.
	// ####################################################################

	// postal code format
	var format = new Object();
	// permitted characters
	var chars = new Object();

	chars["US"] = "^[-() 0-9.]+$";
	format["US"] = "^[( ]*[0-9]{3}[-) .]*[0-9]{3}[- .]*[0-9]{4} *$";

	chars["CA"] = "^[-() 0-9.]+$";
	format["CA"] = "^[( ]*[0-9]{3}[-) .]*[0-9]{3}[- .]*[0-9]{4} *$";

	// For non-US or Canada numbers, no extra format rules for now.
	chars["other"] = "^[-()+ 0-9.]+$";
	format["other"] = "^[-()+ 0-9.]+$";

	var country = countryCode;
	if ( countryCode != "US" && countryCode != "CA" ) country = "other";

	// If either test is missing, return ok
	if ( ! format[country] || ! chars[country] )
	{
	    return 0 ;
	}
	else
	{
	    if (! phone.match(chars[country]))
	    {
			return( -20 );  //# invalid characters
	    }
	    else if (! phone.match(format[country]))
	    {
	        return( -30 ); //# invalid format
	    }
		else
		{
			return( 0 ); //# appears to be valid
		}
	}

}

