goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.proto2.ObjectSerializer');
goog.require('goog.string.StringBuffer');
goog.require('google_i18n.phonenumbers.AsYouTypeFormatter');
goog.require('google_i18n.phonenumbers.PhoneNumberFormat');
goog.require('google_i18n.phonenumbers.PhoneNumberType');
goog.require('google_i18n.phonenumbers.PhoneNumberUtil');
goog.require('google_i18n.phonenumbers.PhoneNumberUtil.ValidationResult');

function phone_get_countries(callback)
{
	util = google_i18n.phonenumbers.PhoneNumberUtil.getInstance();

	function callback_with_icc(country)
	{
		country["icc"] = util.getCountryCodeForRegion(country["code"]);
		callback(country);
	}

	country_get_countries(callback_with_icc);
}

function phone_normalise(input)
{
	input = input.split(' ').join('');
	input = input.split('-').join('');
	input = input.split('(').join('');
	input = input.split(')').join('');
	input = input.split('+').join('');
	return input;
}

function phone_get_icc(country_code, phone_raw)
{
	util = google_i18n.phonenumbers.PhoneNumberUtil.getInstance();

	try
	{
		phone_pb = util.parseAndKeepRawInput(phone_raw, country_code);
	}
	catch (e)
	{
		return "";
	}

	if (util.isValidNumberForRegion(phone_pb, country_code) || phone_raw[0]=='0') // test mode
	{
		return util.formatOutOfCountryCallingNumber(phone_pb);
	}
	else
	{
		return "";
	}
}

function phone_get_canon(country_code, phone_raw)
{
	return phone_normalise(phone_get_icc(country_code, phone_raw));
}
