SCRIPTING:

Fernanda Tavares

COUNTRY CODES AND NATION NAMES MANAGEMENT CLASS
A comprehensive list of all the correct Country Codes and relative Nation Names (checked with the  CIA world factbook 2004 ). These lists are normally required for all those applications or web forms where a user has to select a nationality from a drop down menu.

A class that can perform a variety of searching and selection and nation name vs country code correspondences tasks, along with form populations. The class is modular, that is you can add or remove from it groups of country names listed alphabetically.
July 2004
{ @ }

The model above is Fernanda Tavares
LOADS OF FERNANDA TAVARES ON THE NET


PURPOSES OF THE FILE
What you can use this file for

You will do these things here:
  • Find a reliable and comprehensive list of all the countries known to date, with their relative country codes (often shown in a web url), and such that you can easily update it.
  • Enhance your web surfers' experience with drop down menus listing nationalities; I myself many a time, like yourself I guess, had to deal with such long menus and I invariably found a few limitations never addressed. For instance, why they never put separators to group the nation names alphabetically so to make their order more apparent, and I always have to peer at the list intensely to find where the leading letter of my own country's name starts appearing?

    Again, I often wondered about things like this: would Great Britain be listed precisely with such name, or perhaps has it been listed after the title of United Kingdom? Or would Svalbard be listed as Spitzbergen? If I can't find the name I have in mind, can I have a client side quick help, or must I really spend 3 minutes searching manually this menu?
    Can't I put in a filed a substring of the name I'm expecting, and see under what name it is actually listed?
    Also, isn't there a way to see only the nationalities that belong to a specific alphabetical family rather than having to scan the whole 250 or so items?

    These menu usability problems [external websites about usability: ELEGANTHACK, JACOB NIELSEN], and others, are all finally addressed here (like elsewhere the "how many chars I have already inserted in this max 200 chars textbox? Am I really supposed to count them by myself to guess I am about to submit an invalid form, only to start counting them again?" issue I addressed at the dynamic histogram bars file).

  • Print the list of these country names and/or country codes in a way that might be suitable to copy and paste all of them in a format already compatible with your applications - not just javascript ones! I found it the most annoying thing that I had to copy and paste every single item to build my own lists rather than having some nifty online tool that could do that boring job on my behalf. Here you will find such tool!

  • Find a class that will perform every type of search you want on this type of data, and so flexible that if you want to add something to an entry (say you want to add the term Zaire to the entry that says Congo), this would not jeopardize the functionalities of your applications!
    The only limit you must keep in mind, there can be no duplicates of the country codes (which is why, by the way, they are unique by convention); if you have to add a synonym, add it within the unique entry: the methods of the class will then enable you to find out in which entry the synonyms are located, example:
    "United Kingdom (Great Britain)", "UK"
    This tuple (=pair: Nation entry, followed by the Country Code) must have no duplicates, so whatever synonym for United Kingdom must go within that only entry, example:
    "United Kingdom (Great Britain; England)", "UK"

    If you don't do that and you produce multiple pairs such as:
    "United Kingdom", "UK"
    "Great Britain", "UK"
    "England", "UK"
    This would not seriously jeopardize this application though - which is another good thing! yet, you would notice that given the way it works you may end seeing its methods return always only one of those options (the higher one under an alphabetical point of view); for instance, a search for "Great Britain" would go on stubbornly coming up reporting "United Kingdom"; this is not a serious problem, arguably not even a problem: but your users might be obtuse, and think that then Great Britain is not listed because a search for it keeps reporting only United Kingdom...


THE CLASS CODEX
The codex and later the documentation

Here is your codex, a function whose name is countries.
It is meant to perform as a class, so to run it you have to initialize a variable (placeholder name foo) as a new instance of this class' name (it requires no arguments, so it is a very simple statement to initialize it):

var foo;
foo = new countries();


Keep in mind this class includes the names of over 230 nations (unique names, and every entry may include synonyms) so this is why it is lengthy. Yet as you'll see you could also use instances of this class where you have removed alphabetical listings that you think you won't use (example, the user says his/her nation name starts with letter s).

A Edouard Manet painting
A Edouard Manet painting, Dead Toreador, 1864

        lines:


DESCRIPTION OF THE METHODS AND VARIABLES
The documentation with a test form for every documented method

Here is a description of the available class variables and class methods within this class, and what each of them does. Every method, once described with what it does and what arguments it wants and what it returns, features immediately its own test form where you can get acquainted with it and see it in action.

All the following documentation uses as a placeholder name for a validly initialized class instance the term foo.

Bobbi Billard
CLASS VARIABLES
foo.a, foo.b, foo.c ... foo.z
These are all arrays, each array contains at odd indexes the name of a nation, at the subsequent even number its associated country code. Thus deleting or adding nations is pretty easy.
Obviously, foo.g for instance is the array of the nations whose names start with g - nations, not country codes: in this application it is the nation names what determines the grouping.
However, as you'll see you have also variables that host also the country codes in alphabetical order - which is necessary to stress because, whether you ever noticed it or not, country codes do not always follow the same alphabetical order of the nation names, for instance Samoa is a nation name starting with an S, but its country code is WS, which starts by W.

Note that the arrays names are all lowercase so foo.g is not the same as foo.G

You can additionally empty the class from its arrays holding the alphabetical lists: aside from doing that manually, you can unset a property by a statement like:
foo.g=null;
You can also add alphabetical arrays to it, on demand. If you have an alphabetical array defined somewhere in your script (out of the class, that is), you can add it by a simple statement like, say:
foo.g=your_G_array;
Yet you must remember that soon after you have applied such changes, once they are all assigned you must call on the class instance the method named shaper:
foo.shaper();
because only after a call to that method the class would fully reflect in its other internal variables the newly updated situation. So if you change things don't forget to call shaper.
foo.allStates
foo.allCodes
foo.allCodesOrdered
foo.allStatesAssociative
foo.allCodesAssociative
foo.allCodesOrderedAssociative
All these names are case sensitive.
  • foo.allStates: a numerically indexed array listing all the nation names.
  • foo.allCodes: a numerically indexed array listing all the country codes.
  • foo.allCodesOrdered: a numerically indexed array listing all the country codes in their own alphabetical order (remember? the alphabetical order is given by the nation names, so the country codes need to be listed twice: one in the order they have respective to their corresponding nation names, and another in their own alphabetical order - which you may need for some tasks).


  • foo.allStatesAssociative: a literally indexed array whose keys are the all lowercase or all uppercase (no mix!) names of the nations, and whose values are their respective numerical index in the array allStates.
  • foo.allCodesAssociative: a literally indexed array whose keys are the all lowercase or all uppercase (no mix!) country codes, and whose values are their respective numerical index in the array allCodes.
  • foo.allCodesOrderedAssociative: a literally indexed array whose keys are the all lowercase or all uppercase (no mix!) country codes, and whose values are their respective numerical index in the array allCodesOrdered.
These variables are used in the class methods. Anyway what they are for and what you can use them for too, is to spot instantaneously the country code from a nation name and viceversa.
For instance what is the nation name associated with the code "AZ"? Lowercase AZ to 'az' to be truly safe, and then
foo.allStates[ foo.allCodesAssociative['az'] ];
since allCodesAssociative reports the corresponding numerical index for allStates, foo.allCodesAssociative['az'] returns a number, and such number is the index entry you want to deduce the corresponding nation name from: Azerbaijan.

Analogously, from a nation name get its code:
foo.allCodes[ foo.allStatesAssociative['azerbaijan'] ]; //lowercase the nation's name!
An Edgar Degas painting
An Edgar Degas painting
CLASS METHODS
foo.print
This method accepts the following parameters, in the following order:
  1. targetObject: a reference to a form object you want to print. This reference must be to the object alone, without adding any of its properties, example:
    document.formName.fieldName
    If what you want to do is to print your nation names to a select form element (drop down menu, that is), this is all you need provided the target does is a select object: the function detects by itself if it is and if it is not, it won't attempt to populate it but will by default return a String with all the nation names in a column, separated namely by a newline char \n.


  2. array: defaults to the class array allStates if passed as zero or not passed; if you pass it, this should be a class array, instance: foo.allCodesOrdered - pass the ordered one if you pass an array of country codes.

  3. separateLists: if passed as number 1, it flags that you want to include in your menus also separating entries to make more apparent where a new leading alphabetical letter set starts. Such menu options would be by default in the text shape of:
    ===============A===============
    whereas A is whatever letter in the range A-Z, and these separating menu options' values (every html select menu option carries a text and a value, as I assume you already know) would carry as value a mere empty string.
    Since it is the value what is submitted to the server, your server side script may guess whether such a mere separator rather than an actual state/country code has been selected because the carried value would be an empty string.

    Conversely, as far options which are not mere separators are concerned, each select menu option would carry as its text an entry of the passed (or of the default allStates) array, and as its value that very same text but all lowercase. This value has been lowercase to avoid conflicts on your server side processing: if by mistake the text you wrote for one entry was in a wrong mixed case, your server side scripts may fail to understand that:
    $_POST['selectedValue']=='Germany';
    is true, simply because by mistake you printed in your menus say 'gErmany' rather than 'Germany'.
    So on your server just lowercase and compare against lowercase versions too:
    strtolower($_POST['selectedValue'])==strtolower('Germany');
  4. separatorBeforeAfter: a string which defaults to:
    ===============
    used in the separator options (if the argument separateLists has been passed) as a wrapper of the alphabetical letter:
    ===============A===============
  5. printNumber: if passed as number 1, it prints before each option text a progressive number from 0 included onward (and skips the separators!).
    A combination of separators and numbers may help a lot your users to localize an entry!
  6. joiner: a string, defaults to newline "\n".
    If you pass this argument, the function will assume you want to print the joined string of the array entries in a field (targetObject) which is not a select menu.
    If you bypass with a zero all the arguments passing at most only the array and you pass just the joiner, the function just returns the joined array:
    foo.print(0, optionalArray, 0, 0, "\n")
foo.print(
document.forms[0]. ,
array: ,
separateLists: ,
separatorBeforeAfter: ,
print numbers? ,
joiner:
)




foo.search
Ideal to search having like input partial matches only.
Accepts one argument, which can be either a String or a regular expression (obviously not in between quotes and with the leading and trailing forward slashes! plus the optional modifiers such as gi). Instance:
foo.search(/^al/i)
note that the regular expression searches for all those elements which start (^) with the two letters "al" and performs the search as case insensitive (/i)
That reports: Albania, Algeria and the country code AL

Whereupon it scans bot the allStates and the allCodes array and reports all the found instances in those arrays which included even just as a substring the passed argument - that is a mere search for "a" would report all the instances where the letter a recurs.

Returns ar array of two entries, both empty arrays if nothing was found; otherwise still an array of two entries both an array on their own: the first carrying all the values of the allStates array where an occurrence of the passed argument was found, the second carrying all the values of the allCodes array where an occurrence of the passed argument was found.

All searches are performed lowercasing the input argument and the checked array entries both, so they are case insensitive. Returned results are though in the case they appear in the allStates and allCodes arrays.


is input a regular expression?
or: insert a suitable regular expression example:
foo.getIndex
Pass to it a String, it returns an array of two entries, both numbers: the first is the numerical index of the first instance found (if any) within the class array allStates, the second is the numerical index of the first instance found (if any) within the class array allCodes.

Case insensitive, but requires a full match: Belgium or bElGiUm match, Belgiu (missing m) does not

An Jean Fouquet painting
A (rather powerful) Jean Fouquet painting, 17th century!
foo.getAssociated
Accepts two arguments:
  • input String
  • optional Number: if passed it instructs the method that the passed input string must be considered a country code; if not passed the string is considered a nation name.
If what you pass is thus a nation name, the method returns its corresponding country code. If what you pass is a country code, it returns the corresponding nation name. All searches are case insensitive.
It returns a String.

Yet if a failure to spot a match occurs, it automatically assumes you may have inserted a partial match, and then it searches for all the possible matches invoking the class method search, returning now an array whose each entry is a possible match.
So searching for, say, "bel" and without setting the second parameter, the method would return an array of these elements:
BY, BE, BZ
Those are the country codes associated with:
Belarus,
Belgium,
Belize

Cool...

      is country code?
foo.unfolder
You can pass to this method as first argument an array, arguably one of the alphabetical modules included in the class.
These modules by default go in pairs:
nation name 1 entry, its country code 1 entry
nation name 2 entry, its country code 2 entry
nation name 3 entry, its country code 3 entry...


The method gets this array and returns an array of two entries: the first entry is an array itself which holds only the nation names of the passed alphabetical module (say: foo.g), the second entry is an array itself which holds only the country codes of the passed alphabetical module.
foo.multipleUnfolder
Pass to it as its argument a list of alphabetical letters such as "abcdefg" separated by no space or anything, and it will execute upon each of them the previous method unfolder assuming each letter represents a valid alphabetical module present in the class.
The method will sort the passed letters to order them. Same type of return as unfolder, but run on more consecutive modules.
foo.shaper
It reshapes the internal variables of the class. Call it if you are dynamically rewriting in any fashion the alphabetical modules, as previously described in this file.
Monet, soleil grand
Monet, soleil grand
Bonus stuff for you!
Where else are you going to find a guy who does so much and for free for you, a stranger?

Get the list of
surround them with:
print items separated by:
If you choose to get BOTH, you can furtherly choose:
           

Gina Lynn

Otherwise get a set of drop down menu options already populated:

print numbers in texts too?

Or, last:
insert this before each:
insert this after each:       & new line after:



A Joseph Decamp painting
A Joseph Decamp painting