var Country = Class.create();
Country.prototype = {
	initialize: function(name, cCode, nddCode) {
		this.name = name;    // no need for use yet, so null to save resources
		this.cCode = cCode.toString();
		this.nddCode = (nddCode ? nddCode.split(",") : null);
	},

	getName: function() { return this.name; },
	getCCode: function() { return this.cCode; },
	getNDDCode: function() { return this.nddCode; }
}

var PhoneNumber = Class.create();
PhoneNumber.prototype = {
	initialize: function(phoneNumber) { this.phoneNumber = phoneNumber; },

	clean: function() {
		this.phoneNumber = this.phoneNumber.gsub(/[^0-9]/, "");
	},

	removeNDD: function(country) {
		nddCode = country.getNDDCode();
		if (!nddCode) { return; }

		var reString = "^(" + nddCode.join("|") + ")";
		var reNDD = new RegExp(reString);
		this.phoneNumber = this.phoneNumber.sub(reNDD, "");
	},

	addCCode: function(country) {
		this.phoneNumber = country.getCCode() + this.phoneNumber;
	},

	getParsedNumber: function(country) {
		this.removeNDD(country);
		this.addCCode(country);
		return this.phoneNumber;
	},

	isValid: function() {
		return (this.phoneNumber.length >= 10 ? true : false);
	},
	isValidUS: function() {
		return (this.phoneNumber.match(/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/) ? true : false);
	},
	getNumber: function() { return this.phoneNumber; }
}

// These MUST match values in select box options.
var arCountry = {
	afghanista: new Country('AFG', 93, "0"),
	albania: new Country('ALB', 355, "0"),
	algeria: new Country('DZA', 213, "7"),
	americansa: new Country('ASM', 1, "1"),
	andorra: new Country('AND', 376, null),
	angola: new Country('AGO', 244, "0"),
	anguilla: new Country('AIA', 1, "1"),
	antarctica: new Country('ATA', 672, null),
	antigua: new Country('ATG', 1, "1"),
	argentina: new Country('ARG', 54, "0"),
	armenia: new Country('ARM', 374, "8"),
	aruba: new Country('ABW', 297, null),
	ascension: new Country(null, 247, null),
	australia: new Country('AUS', 61, "0"),
	australian: new Country('AUS', 672, "0"),
	austria: new Country('AUT', 43, "0"),
	azerbaijan: new Country('AZE', 994, "8"),
	bahamas: new Country('BHS', 1, "1"),
	bahrain: new Country('BHR', 973, null),
	bangladesh: new Country('BGD', 880, "0"),
	barbados: new Country('BRB', 1, "1"),
	barbuda: new Country(null, 1, "1"),
	belarus: new Country('BLR', 375, "8"),
	belgium: new Country('BEL', 32, "0"),
	belize: new Country('BLZ', 501, "0"),
	benin: new Country('BEN', 229, null),
	bermuda: new Country('BMU', 1, "1"),
	bhutan: new Country('BTN', 975, null),
	bolivia: new Country('BOL', 591, "010,011,012,013"),
	bosniaherz: new Country('BIH', 387, "0"),
	botswana: new Country('BWA', 267, null),
	brazil: new Country('BRA', 55, "014,015,021,023,031,0"),
	britishvir: new Country('VGB', 1, "1"),
	bruneidaru: new Country('BRN', 673, "0"),
	bulgaria: new Country('BGR', 359, "0"),
	burkinafas: new Country('BFA', 226, null),
	burundi: new Country('BDI', 257, null),
	cambodia: new Country('KHM', 855, "0"),
	cameroon: new Country('CMR', 237, null),
	canada: new Country('CAN', 1, "1"),
	capeverdei: new Country('CPV', 238, null),
	caymanisla: new Country('CYM', 1, "1"),
	centralafr: new Country('CAF', 236, null),
	chad: new Country('TCD', 235, null),
	chathamisl: new Country(null, 64, null),
	chile: new Country('CHL', 56, "0"),
	chinaprc: new Country('CHN', 86, "0"),
	christmasi: new Country('CXR', 618, "0"),
	cocoskeeli: new Country('CCK', 61, "0"),
	colombia: new Country('COL', 57, "03,05,07,09"),
	comoros: new Country('COM', 269, null),
	congo: new Country('COG', 242, null),
	congodemre: new Country('COD', 243, null),
	cookisland: new Country('COK', 682, "0"),
	costarica: new Country('CRI', 506, null),
	croatia: new Country('HRV', 385, "0"),
	cuba: new Country('CUB', 53, "0"),
	cubaguanta: new Country(null, 5399, "0"),
	curacao: new Country(null, 599, null),
	cyprus: new Country('CYP', 357, null),
	czechrepub: new Country('CZE', 420, null),
	denmark: new Country('DNK', 45, null),
	diegogarci: new Country(null, 246, null),
	djibouti: new Country('DJI', 253, null),
	dominica: new Country('DMA', 1, "1"),
	dominicanr: new Country('DOM', 1, "1"),
	easttimor: new Country(null, 670, null),
	easterisla: new Country(null, 56, null),
	ecuador: new Country('ECU', 593, "0"),
	egypt: new Country('EGY', 20, "0"),
	elsalvador: new Country('SLV', 503, null),
	emsatmobil: new Country(null, 88213, null),
	equatorial: new Country('GNQ', 240, null),
	eritrea: new Country('ERI', 291, "0"),
	estonia: new Country('EST', 372, null),
	ethiopia: new Country('ETH', 251, "0"),
	falklandis: new Country('FLK', 500, null),
	faroeislan: new Country('FRO', 298, null),
	fijiisland: new Country('FJI', 679, null),
	finland: new Country('FIN', 358, "0"),
	france: new Country('FRA', 33, "0"),
	frenchanti: new Country(null, 596, null),
	frenchguia: new Country('GUF', 594, null),
	frenchpoly: new Country('PYF', 689, null),
	gabonesere: new Country('GAB', 241, null),
	gambia: new Country('GMB', 220, null),
	georgia: new Country('GEO', 995, "8"),
	germany: new Country('DEU', 49, "0"),
	ghana: new Country('GHA', 233, null),
	gibraltar: new Country('GIB', 350, null),
	greece: new Country('GRC', 30, null),
	greenland: new Country('GRL', 299, null),
	grenada: new Country('GRD', 1, "1"),
	guadeloupe: new Country('GLP', 590, null),
	guam: new Country('GUM', 1, "1"),
	guantanamo: new Country(null, 5399, "0"),
	guatemala: new Country('GTM', 502, null),
	guineabiss: new Country('GNB', 245, null),
	guinea: new Country('GIN', 224, "0"),
	guyana: new Country('GUY', 592, "0"),
	haiti: new Country('HTI', 509, "0"),
	honduras: new Country('HND', 504, "0"),
	hongkong: new Country('HKG', 852, null),
	hungary: new Country('HUN', 36, "6"),
	iceland: new Country('ISL', 354, "0"),
	india: new Country('IND', 91, "0"),
	indonesia: new Country('IDN', 62, "0"),
	inmarsatat: new Country(null, 871, null),
	inmarsatat: new Country(null, 874, null),
	inmarsatin: new Country(null, 873, null),
	inmarsatpa: new Country(null, 872, null),
	inmarsatsn: new Country(null, 870, null),
	internatio: new Country(null, 800, null),
	internatio: new Country(null, 808, null),
	iran: new Country('IRN', 98, "0"),
	iraq: new Country('IRQ', 964, "0"),
	ireland: new Country('IRL', 353, "0"),
	israel: new Country('ISR', 972, "0"),
	italy: new Country('ITA', 39, null),
	ivorycoast: new Country(null, 225, "0"),
	jamaica: new Country('JAM', 1, "1"),
	japan: new Country('JPN', 81, "0"),
	jordan: new Country('JOR', 962, "0"),
	kazakhstan: new Country('KAZ', 7, "8"),
	kenya: new Country('KEN', 254, "0"),
	kiribati: new Country('KIR', 686, "0"),
	koreanorth: new Country('PRK', 850, "0"),
	koreasouth: new Country('KOR', 82, "082,0"),
	kuwait: new Country('KWT', 965, "0"),
	kyrgyzrepu: new Country('KGZ', 996, "0"),
	laos: new Country('LAO', 856, "0"),
	latvia: new Country('LVA', 371, null),
	lebanon: new Country('LBN', 961, "0"),
	lesotho: new Country('LSO', 266, "0"),
	liberia: new Country('LBR', 231, "22"),
	libya: new Country('LBY', 218, "0"),
	liechtenst: new Country('LIE', 423, null),
	lithuania: new Country('LTU', 370, "8"),
	luxembourg: new Country('LUX', 352, null),
	macao: new Country('MAC', 853, "0"),
	macedonia: new Country('MKD', 389, "0"),
	madagascar: new Country('MDG', 261, "0"),
	malawi: new Country('MWI', 265, null),
	malaysia: new Country('MYS', 60, "0"),
	maldives: new Country('MDV', 960, "0"),
	malirepubl: new Country('MLI', 223, "0"),
	malta: new Country('MLT', 356, "21"),
	marshallis: new Country('MHL', 692, "1"),
	martinique: new Country('MTQ', 596, "0"),
	mauritania: new Country('MRT', 222, "0"),
	mauritius: new Country('MUS', 230, "0"),
	mayotteisl: new Country('MYT', 269, null),
	mexico: new Country('MEX', 52, "1"),
	micronesia: new Country('FSM', 691, "1"),
	midwayisla: new Country(null, 1, "1"),
	moldova: new Country('MDA', 373, "0"),
	monaco: new Country('MCO', 377, "0"),
	mongolia: new Country('MNG', 976, "0"),
	montenegro: new Country('MNE', 382, "0"),
	montserrat: new Country('MSR', 1, "1"),
	morocco: new Country('MAR', 212, null),
	mozambique: new Country('MOZ', 258, "0"),
	myanmar: new Country('MMR', 95, null),
	namibia: new Country('NAM', 264, "0"),
	nauru: new Country('NAM', 674, "0"),
	nepal: new Country('NPL', 977, "0"),
	netherland: new Country('NLD', 31, "0"),
	netherland: new Country('NLD', 599, "0"),
	nevis: new Country(null, 1, "1"),
	newcaledon: new Country('NCL', 687, "0"),
	newzealand: new Country('NZL', 64, "0"),
	nicaragua: new Country('NIC', 505, "0"),
	niger: new Country('NER', 227, "0"),
	nigeria: new Country('NGA', 234, "0"),
	niue: new Country('NIU', 683, "0"),
	norfolkisl: new Country('NFK', 672, null),
	northernma: new Country('MNP', 1, "1"),
	norway: new Country('NOR', 47, null),
	oman: new Country('OMN', 968, "0"),
	pakistan: new Country('PAK', 92, "0"),
	palau: new Country('PLW', 680, null),
	palestinia: new Country('PSE', 970, "0"),
	panama: new Country('PAN', 507, "0"),
	papuanewgu: new Country('PNG', 675, null),
	paraguay: new Country('PRY', 595, "0"),
	peru: new Country('PER', 51, "0"),
	philippine: new Country('PHL', 63, "0"),
	poland: new Country('POL', 48, "0"),
	portugal: new Country('PRT', 351, null),
	puertorico: new Country('PRI', 1, "1"),
	qatar: new Country('QAT', 974, "0"),
	reunionisl: new Country('REU', 262, "0"),
	romania: new Country('ROU', 40, "0"),
	russia: new Country('RUS', 7, "8"),
	rwandesere: new Country('RWA', 250, "0"),
	sthelena: new Country('SHN', 290, null),
	stkittsnev: new Country('KNA', 1, "1"),
	stlucia: new Country('LCA', 1, "1"),
	stpierremi: new Country('SPM', 508, "0"),
	stvincentg: new Country('VCT', 1, "1"),
	samoa: new Country('VCT', 685, "0"),
	sanmarino: new Country('SMR', 378, "0"),
	saotomeand: new Country('STP', 239, "0"),
	saudiarabi: new Country('SAU', 966, "0"),
	senegal: new Country('SEN', 221, "0"),
	serbia: new Country('SRB', 381, "0"),
	seychelles: new Country('SYC', 248, "0"),
	sierraleon: new Country('SLE', 232, "0"),
	singapore: new Country('SGP', 65, null),
	slovakrepu: new Country('SVK', 421, "0"),
	slovenia: new Country('SVN', 386, "0"),
	solomonisl: new Country('SLB', 677, null),
	somalidemo: new Country('SOM', 252, null),
	southafric: new Country('ZAF', 27, "0"),
	spain: new Country('ESP', 34, null),
	srilanka: new Country('LKA', 94, "0"),
	sudan: new Country('SDN', 249, "0"),
	suriname: new Country('SUR', 597, null),
	swaziland: new Country('SWZ', 268, null),
	sweden: new Country('SWE', 46, "0"),
	switzerlan: new Country('CHE', 41, "0"),
	syria: new Country('SYR', 963, "0"),
	taiwan: new Country('TWN', 886, null),
	tajikistan: new Country('TJK', 992, "8"),
	tanzania: new Country('TZA', 255, "0"),
	thailand: new Country('THA', 66, "0"),
	thurayamob: new Country(null, 88216, null),
	timorleste: new Country('TLS', 670, null),
	togolesere: new Country('TGO', 228, null),
	tokelau: new Country('TKL', 690, null),
	tongaislan: new Country('TON', 676, null),
	trinidadto: new Country('TTO', 1, "1"),
	tunisia: new Country('TUN', 216, "0"),
	turkey: new Country('TUR', 90, "0"),
	turkmenist: new Country('TKM', 993, "8"),
	turksandca: new Country('TCA', 1, "1"),
	tuvalu: new Country('TUV', 688, null),
	uganda: new Country('UGA', 256, "0"),
	ukraine: new Country('UKR', 380, "8"),
	unitedarab: new Country('ARE', 971, "0"),
	unitedking: new Country('GBR', 44, "0"),
	unitedstat: new Country('USA', 1, "1"),
	usvirginis: new Country('VIR', 1, "1"),
	universalp: new Country(null, 878, null),
	uruguay: new Country('URY', 598, "0"),
	uzbekistan: new Country('UZB', 998, "8"),
	vanuatu: new Country('VUT', 678, null),
	vaticancit: new Country(null, 39, null),
	venezuela: new Country('VEN', 58, "0102,0107,0110,0111,0114,0119,0123,0150,0133,0199,0"),
	vietnam: new Country('VNM', 84, "0"),
	wakeisland: new Country(null, 808, null),
	wallisandf: new Country(null, 681, null),
	yemen: new Country('YEM', 967, "0"),
	zambia: new Country('ZMB', 260, "0"),
	zanzibar: new Country(null, 255, "0"),
	zimbabwe: new Country('ZWE', 263, "0")
};
