
// This file contains all of the phone number-related translation
// and validation functions.  These functions are called by the various
// functions in gmsValidators.js.


var phoneValidationPattern = /^((((-|\.| )?\d(-|\.| )?){10,14})|((\s)?\+(\s)?\d{1,3}(-|\.| )?)?((\(?\d{2,3}\)?)|(\(?\d\)?(-|\.| )?\d))(-|\.| )?(\d{3,4})(-|\.| )?(\d{4}))$/;
var phoneIsPhoneNumPattern = /^\+?(((-|\.|\(|\)| )?\d(-|\.|\(|\)| )?){9,14})/;
var phoneStripPattern = /[\s\-\(\)\.]+/g;
var phoneSplitPattern = /^\+(\d{1,3}) ([\d\-\.\(\) ]*)$/;
var phoneMinLength = 9;
var countryCodeDefault = "1";


// countryCodes array stores phone specifications for specific countries.
// Each element stores country name, minimum length, match pattern and output pattern.
//
var countryCodes = new Array();
countryCodes["0"] = "Unknown;9;(\\d*);$1";
countryCodes["1"] = "United States;10;(\\d{3,})(\\d{3})(\\d{4});$1-$2-$3";
countryCodes["7"] = "Russia;10;(\\d{3,})(\\d{3})(\\d{4});$1-$2-$3";
countryCodes["20"] = "Egypt;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["27"] = "South Africa;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["30"] = "Greece;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["31"] = "Netherlands;9;(\\d{2,})(\\d{3})(\\d{4});$1-$2-$3";
countryCodes["32"] = "Belgium;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["33"] = "France;9;(\\d{3,})(\\d{3})(\\d{3});$1-$2-$3";
countryCodes["34"] = "Spain;9;(\\d{2,})(\\d{3})(\\d{4});$1-$2-$3";
countryCodes["36"] = "Hungary;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["39"] = "Italy;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["40"] = "Romania;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["41"] = "Switzerland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["43"] = "Austria;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["44"] = "United Kingdom;10;(\\d{2,})(\\d{4})(\\d{4});$1-$2-$3";
countryCodes["45"] = "Denmark;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["46"] = "Sweden;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["47"] = "Norway;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["48"] = "Poland;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["49"] = "Germany;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["51"] = "Peru;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["52"] = "Mexico;7;(\\d{2})(\\d{3})(\\d{2,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["53"] = "Cuba;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["54"] = "Argentina;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["55"] = "Brazil;8;(\\d{2})(\\d{3})(\\d{3,});$1-$2-$3";
countryCodes["56"] = "Chile;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["57"] = "Colombia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["58"] = "Venezuela;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["60"] = "Malaysia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["61"] = "Australia;9;(\\d{3})(\\d{3})(\\d{3,});$1-$2-$3";
countryCodes["62"] = "Indonesia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["63"] = "Philippines;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["64"] = "New Zealand;7;(\\d{2})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["65"] = "Singapore;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["66"] = "Thailand;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["81"] = "Japan;7;(\\d{2})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["82"] = "South Korea;7;(\\d{2})(\\d{2})(\\d{2,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["84"] = "Vietnam;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["86"] = "China;10;(\\d{3,})(\\d{4})(\\d{4});$1-$2-$3";
countryCodes["90"] = "Turkey;9;(\\d{3})(\\d{3})(\\d{3,});$1-$2-$3";
countryCodes["91"] = "India;10;(\\d{3,})(\\d{3})(\\d{4});$1-$2-$3";
countryCodes["92"] = "Pakistan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["93"] = "Afghanistan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["94"] = "Sri Lanka;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["95"] = "Burma (Myanmar);7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["98"] = "Iran;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["212"] = "Morocco;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["213"] = "Algeria;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["216"] = "Tunisia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["218"] = "Libya;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["220"] = "Gambia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["221"] = "Senegal;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["222"] = "Mauritania;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["223"] = "Mali;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["224"] = "Guinea;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["225"] = "Ivory Coast;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["226"] = "Burkina Faso;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["227"] = "Niger;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["228"] = "Togo;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["229"] = "Benin;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["230"] = "Mauritius;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["231"] = "Liberia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["232"] = "Sierra Leone;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["233"] = "Ghana;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["234"] = "Nigeria;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["235"] = "Chad;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["236"] = "Central African Republic;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["237"] = "Cameroon;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["238"] = "Cape Verde;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["239"] = "Sao Tome;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["240"] = "Equatorial Guinea;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["241"] = "Gabon;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["242"] = "Congo (Brazzaville);7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["243"] = "Congo (Kinshasa);7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["243"] = "Zaire;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["244"] = "Angola;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["245"] = "Guinea-Bissau;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["246"] = "Diego-Garcia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["247"] = "Ascension Island;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["248"] = "Seychelles;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["249"] = "Sudan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["250"] = "Rwanda;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["250"] = "Rwanda;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["251"] = "Ethiopia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["252"] = "Somalia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["253"] = "Djibouti;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["254"] = "Kenya;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["255"] = "Tanzania;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["256"] = "Uganda;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["257"] = "Burundi;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["258"] = "Mozambique;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["260"] = "Zambia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["261"] = "Madagascar;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["262"] = "Mayotte;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["263"] = "Zimbabwe;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["264"] = "Namibia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["265"] = "Malawi;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["266"] = "Lesotho;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["267"] = "Botswana;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["268"] = "Swaziland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["269"] = "Comoros;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["290"] = "Saint Helena;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["291"] = "Eritrea;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["297"] = "Aruba;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["298"] = "Faroe Islands;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["299"] = "Greenland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["350"] = "Gibraltar;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["351"] = "Portugal;8;(\\d{3})(\\d{3})(\\d{2,});$1-$2-$3";
countryCodes["352"] = "Luxembourg;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["353"] = "Ireland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["354"] = "Iceland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["355"] = "Albania;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["356"] = "Malta;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["357"] = "Cyprus;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["358"] = "Finland;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["359"] = "Bulgaria;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["370"] = "Lithuania;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["371"] = "Latvia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["372"] = "Estonia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["373"] = "Moldova;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["374"] = "Armenia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
//ITAR EMBARGOED countryCodes["375"] = "Belarus;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["376"] = "Andorra;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["377"] = "Monaco;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["378"] = "San Marino;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["380"] = "Ukraine;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["381"] = "Serbia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["382"] = "Montenegro;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["385"] = "Croatia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["386"] = "Slovenia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["387"] = "Bosnia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["389"] = "Macedonia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["420"] = "Czech Republic;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["421"] = "Slovakia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["423"] = "Liechtenstein;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["500"] = "Falkland Islands;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["501"] = "Belize;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["502"] = "Guatemala;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["503"] = "El Salvador;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["504"] = "Honduras;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["505"] = "Nicaragua;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["506"] = "Costa Rica;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["507"] = "Panama;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["508"] = "Saint-Pierre;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["509"] = "Haiti;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["590"] = "Guadeloupe;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["591"] = "Bolivia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["592"] = "Guyana;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["593"] = "Ecuador;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["594"] = "French Guiana;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["595"] = "Paraguay;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["596"] = "Martinique;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["597"] = "Suriname;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["598"] = "Uruguay;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["599"] = "Netherlands Antilles;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["670"] = "East Timor;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["672"] = "Australian External Territories;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["673"] = "Brunei Darussalm;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["674"] = "Nauru;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["675"] = "Papua New Guinea;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["676"] = "Tonga;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["677"] = "Solomon Islands;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["678"] = "Vanuatu;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["679"] = "Fiji;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["680"] = "Palau;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["681"] = "Wallis;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["682"] = "Cook Islands;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["683"] = "Niue Island;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["685"] = "Samoa;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["686"] = "Kiribati (Gilbert Islands);7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["687"] = "New Caledonia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["688"] = "Tuvalu (Ellice Islands);7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["689"] = "French Polynesia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["690"] = "Tokelau;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["691"] = "Micronesia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["692"] = "Marshall Islands;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["850"] = "North Korea;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["852"] = "Hong Kong;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["853"] = "Macao;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["855"] = "Cambodia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["856"] = "Laos;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["880"] = "Bangladesh;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["886"] = "Taiwan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["960"] = "Maldives;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["961"] = "Lebanon;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["962"] = "Jordan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["963"] = "Syria;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
// ITAR EMBARGOED countryCodes["964"] = "Iraq;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["965"] = "Kuwait;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["966"] = "Saudi Arabia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["967"] = "Yemen;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["968"] = "Oman;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["971"] = "United Arab Emirates;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["972"] = "Israel;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["973"] = "Bahrain;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["974"] = "Qatar;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["975"] = "Bhutan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["976"] = "Mongolia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["977"] = "Nepal;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["992"] = "Tajikistan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["993"] = "Turkmenistan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["994"] = "Azerbaijan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["995"] = "Georgia;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["996"] = "Kyrgyzstan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";
countryCodes["998"] = "Uzbekistan;7;(\\d{3})(\\d{3})(\\d{0,});$1-$2-$3";

// sharedCountryCodes array stores instances where country codes  are used by multiple countries.
//
var sharedCountryCodes = new Array();
sharedCountryCodes["1"] = "Canada;United States;Bahamas;Barbados;Anguilla;" +
  "Antigua;Barbuda;British Virgin Islands;Cayman Islands;Bermuda;Grenada;Carricou;" +
  "Turks;Caicos;Montserrat;Northern Marianas;Guam;American Samoa;Saint Lucia;Dominica;" +
  "Saint Vincent;Grenadines;Dominican Republic;Trinidad;Tobago;" +
  "Saint Kitts;Nevis;Jamaica;Puerto Rico;Carribean Nations";
sharedCountryCodes["7"] = "Russia;Kazakhstan";
sharedCountryCodes["239"] = "Sao Tome;Principe";
sharedCountryCodes["262"] = "Mayotte;Runion";
sharedCountryCodes["290"] = "Saint Helena;Tristan da Cunha";
sharedCountryCodes["387"] = "Bosnia;Herzegovina";
sharedCountryCodes["508"] = "Saint-Pierre;Miquelon";
sharedCountryCodes["596"] = "Martinique;French Antilles";
sharedCountryCodes["670"] = "East Timor;Saipan";
sharedCountryCodes["681"] = "Wallis;Futuna";
sharedCountryCodes["689"] = "French Polynesia;Tahiti";
sharedCountryCodes["996"] = "Kyrgyzstan;Kyrgyz Republic";

// Returns a formatted version of the given phone number based on the country code
//
function formattedPhone(phn) {
	var fmtPhone = "";
	var countryCode = "";
	var phoneSpecs = new Array();

	if (phn.substr(0,1) == "+") {
		if (countryCodes[phn.substr(1,1)] != null) {
		 	fmtPhone = phn.substr(2);
		 	countryCode = phn.substr(0,2);
		 	phoneSpecs = countryCodes[phn.substr(1,1)].split(";");
		} else if (countryCodes[phn.substr(1,2)] != null) {
		 	fmtPhone = phn.substr(3);
		 	countryCode = phn.substr(0,3);
		 	phoneSpecs = countryCodes[phn.substr(1,2)].split(";");
		} else if (countryCodes[phn.substr(1,3)] != null) {
		 	fmtPhone = phn.substr(4);
		 	countryCode = phn.substr(0,4);
		 	phoneSpecs = countryCodes[phn.substr(1,3)].split(";");
		} else {  // unknown country code so just return number as given
			return phn;
		}
	} else {  // no country code specified so use default
		fmtPhone = phn;
		countryCode = "+" + countryCodeDefault;
		phoneSpecs = countryCodes[countryCodeDefault].split(";");
	}
	var pattern = new RegExp(phoneSpecs[2]);
	fmtPhone = countryCode + " " + fmtPhone.replace(pattern,phoneSpecs[3]);
	var lastChar = fmtPhone.charAt(fmtPhone.length - 1);
	if (lastChar < "0" || lastChar > "9") fmtPhone = fmtPhone.substr(0,fmtPhone.length - 1);
	return fmtPhone;
}

// Returns a formatted version of the given phone number based on the country code
//
function formattedPhoneWithCC(countryCode, phn) {

	var phoneSpecs = countryCodes[countryCode].split(";");
	var pattern = new RegExp(phoneSpecs[2]);
	return phn.replace(pattern,phoneSpecs[3]);

}

// Returns a phone number stripped of all characters other than digits or "+"
//
function stripPhone(phn) {
	var newPhn = "";
	for (pIndex=0; pIndex < phn.length; pIndex++) {
		if (phn.substr(pIndex,1).match(/(\+|\d)/)) {
			newPhn += phn.substr(pIndex,1);
		}
	}
	return newPhn;
}

// Returns a phone number stripped of all characters other than digits
//
function stripPhoneWithoutCC(phn) {
	var newPhn = "";
	for (pIndex=0; pIndex < phn.length; pIndex++) {
		if (phn.substr(pIndex,1).match(/(\d)/)) {
			if ((newPhn.length == 0) && (phn.substr(pIndex,1) == "0")) continue;
			newPhn += phn.substr(pIndex,1);
		}
	}
	return newPhn;
}


// Validate the given phone number and either return a formatted version or an error string
//
function validatePhoneData(phn) {

	// allow only for digits, brackets, spaces and plus at the begining
	if(!phn.match(/^ *[+]?[0-9\s(]+([-]?[)0-9\s]+)+ *$/)) {
		return "!badFormat";
	}
	
	var phoneSpecs = new Array();
	var newPhone = stripPhone(phn);
	
	if (newPhone.substr(0,1) == "+") {
		if (countryCodes[newPhone.substr(1,1)] != null) {
			phoneSpecs = countryCodes[newPhone.substr(1,1)].split(";");
			newPhone = newPhone.substr(2);
		} else if (countryCodes[newPhone.substr(1,2)] != null) {
			phoneSpecs = countryCodes[newPhone.substr(1,2)].split(";");
			newPhone = newPhone.substr(3);
		} else if (countryCodes[newPhone.substr(1,3)] != null) {
			phoneSpecs = countryCodes[newPhone.substr(1,3)].split(";");
			newPhone = newPhone.substr(4);
		} else {
			return "!badCountryCode";
		}
		if (newPhone.length < phoneSpecs[1]) {
			return "!badLengthForCountryCode";
		} else {
			var pattern = new RegExp(phoneSpecs[2]);
			if (!newPhone.match(pattern)) {
				return "!badFormatForCountryCode";
			}
		}
	} else if (!newPhone.match(phoneValidationPattern)) {
		return "!badFormat";
	} else if (newPhone.length < phoneMinLength) {
		return "!tooShort";
	}
	
	phn = phn.replace(phoneStripPattern, "");
	return formattedPhone(phn);
}

// Validate the given phone number and either return a formatted version or an error string
function validatePhoneDataWithCC(countryCode, phn) {

	var invalidChars = /[^0-9\s\-\(\)\.]/;
	var invalidPattern = new RegExp(invalidChars);
	if (phn.match(invalidPattern)) {
		return "!badFormat";
	}

	var newPhone = stripPhoneWithoutCC(phn);
	var phoneSpecs = countryCodes[countryCode].split(";");
	
	if (newPhone.length < phoneSpecs[1]) {
		return "!badLengthForCountryCode";
	} else {
		var pattern = new RegExp(phoneSpecs[2]);
		if (!newPhone.match(pattern)) {
			return "!badFormatForCountryCode";
		}
	}
	
	return formattedPhoneWithCC(countryCode, newPhone);
}

// Validate the given field as a phone number and set any error messages accordingly
//
function phoneValidate(field){
	if(field.readOnly){
		return true;
	}
	var res = validatePhoneData(field.value);
	var message = "";
	if ((res == "!badFormat") || (res == "!tooShort")) {
		message = 'Invalid Telephone Number.';
	} else if (res == "!badCountryCode") {
		message = 'Unknown Country Code.';
	} else if (res == "!badLengthForCountryCode") {
		message = 'Telephone number too short for given country code.';
	} else if (res == "!badFormatForCountryCode") {
		message = 'Invalid Telephone Number for given country code.';
	} else {
		field.value = res;
	}
	notifyError(field.id, message);
	return message=="";
}

// Validate the given field as a phone number with country code and set any error messages accordingly
//
function phoneValidateWithCC(countryCode, telNumField) {
	
	if(telNumField.readOnly) return true;

	var res = validatePhoneDataWithCC(countryCode, telNumField.value);
	var message = "";
	if ((res == "!badFormat") || (res == "!tooShort")) {
		message = 'Invalid Telephone Number.';
	} else if (res == "!badLengthForCountryCode") {
		message = 'Telephone number too short for given country code.';
	} else if (res == "!badFormatForCountryCode") {
		message = 'Invalid Telephone Number for given country code.';
	} else {
		telNumField.value = res;
	}
	notifyError(telNumField.id, message);
	return message=="";
}

// Given a complete telephone number, return the country code portion without the "+" 
// Note: the telephone number is expected to begin with a "+" and contain an initial country code and space
function getCountryCode(formattedTelNum) {
	
	if (formattedTelNum.match(phoneSplitPattern)) {
		var pattern = new RegExp(phoneSplitPattern);
		return formattedTelNum.replace(pattern,"$1");
	} else {
		return "";
	}
}

// Given a complete telephone number, return everything except the country code
// Note: the telephone number is expected to begin with a "+" and contain an initial country code and space
function getPhoneNum(formattedTelNum) {
	
	if (formattedTelNum.match(phoneSplitPattern)) {
		var pattern = new RegExp(phoneSplitPattern);
		return formattedTelNum.replace(pattern,"$2");
	} else {
		return "";
	}
}
