// Copyright (C) 2007 Americas-Mailbox.  All rights reserved.

function findElement( elId )
{
    var el = null;
    if ( document.getElementById )
        el = document.getElementById( elId );
    else if ( document.all )
        el = document.all[ elId ];
    return el;
}

// Because radio buttons are different element each, you cannot use "findElement" to 
// pass in a ID and retrieve which radio button is selected, because the ID would only
// reference one option. 
// If all radio buttons have the same group name you can loop to find the checked one.
function getCheckedRadio(radioGroupName) {
    radios = document.getElementsByName(radioGroupName);
    for (i = 0; i < radios.length; i++) 
    {
        if (radios[i].checked) 
            return radios[i];
    }
}
    
    
// Return the actual position of the element
// see http://www.webreference.com/dhtml/diner/realpos1/index.html
function DL_GetElementLeft(eElement)
{
    var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nLeftPos;                             // return the number calculated
}

// Return the actual position of the element
// see http://www.webreference.com/dhtml/diner/realpos1/index.html
function DL_GetElementTop(eElement)
{
    var nTopPos = eElement.offsetTop;            // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nTopPos += eParElement.offsetTop;        // appending top offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nTopPos;                              // return the number calculated
}

// Return the width of the element
function GetElementWidth( el )
{
    // TODO: this is IE-specific
    return el.clientWidth;
}

// Return the height of the element
function GetElementHeight( el )
{
    // TODO: this is IE-specific
    return el.clientHeight;
}

// Return an associative array of countries keyed by their 3-letter iso code
function GetCountries()
{
    var countries = new Object();
    
    countries["USA"] = "United States";
    countries["CAN"] = "Canada";
    countries["MEX"] = "Mexico";
    countries["AFG"] = "Afghanistan";
    countries["ALA"] = "Åland Islands";
    countries["ALB"] = "Albania";
    countries["DZA"] = "Algeria";
    countries["ASM"] = "American Samoa";
    countries["AND"] = "Andorra";
    countries["AGO"] = "Angola";
    countries["AIA"] = "Anguilla";
    countries["ATG"] = "Antigua and Barbuda";
    countries["ARG"] = "Argentina";
    countries["ARM"] = "Armenia";
    countries["ABW"] = "Aruba";
    countries["AUS"] = "Australia";
    countries["AUT"] = "Austria";
    countries["AZE"] = "Azerbaijan";
    countries["BHS"] = "Bahamas";
    countries["BHR"] = "Bahrain";
    countries["BGD"] = "Bangladesh";
    countries["BRB"] = "Barbados";
    countries["BLR"] = "Belarus";
    countries["BEL"] = "Belgium";
    countries["BLZ"] = "Belize";
    countries["BEN"] = "Benin";
    countries["BMU"] = "Bermuda";
    countries["BTN"] = "Bhutan";
    countries["BOL"] = "Bolivia";
    countries["BIH"] = "Bosnia and Herzegovina";
    countries["BWA"] = "Botswana";
    countries["BRA"] = "Brazil";
    countries["VGB"] = "British Virgin Islands";
    countries["BRN"] = "Brunei Darussalam";
    countries["BGR"] = "Bulgaria";
    countries["BFA"] = "Burkina Faso";
    countries["BDI"] = "Burundi";
    countries["KHM"] = "Cambodia";
    countries["CMR"] = "Cameroon";
    countries["CPV"] = "Cape Verde";
    countries["CYM"] = "Cayman Islands";
    countries["CAF"] = "Central African Republic";
    countries["TCD"] = "Chad";
    countries["CHL"] = "Chile";
    countries["CHN"] = "China";
    countries["COL"] = "Colombia";
    countries["COM"] = "Comoros";
    countries["COG"] = "Congo";
    countries["COD"] = "Congo (Democratic Republic)";
    countries["COK"] = "Cook Islands";
    countries["CRI"] = "Costa Rica";
    countries["CIV"] = "Côte d'Ivoire";
    countries["HRV"] = "Croatia";
    countries["CUB"] = "Cuba";
    countries["CYP"] = "Cyprus";
    countries["CZE"] = "Czech Republic";
    countries["DNK"] = "Denmark";
    countries["DJI"] = "Djibouti";
    countries["DMA"] = "Dominica";
    countries["DOM"] = "Dominican Republic";
    countries["ECU"] = "Ecuador";
    countries["EGY"] = "Egypt";
    countries["SLV"] = "El Salvador";
    countries["GNQ"] = "Equatorial Guinea";
    countries["ERI"] = "Eritrea";
    countries["EST"] = "Estonia";
    countries["ETH"] = "Ethiopia";
    countries["FRO"] = "Faeroe Islands";
    countries["FLK"] = "Falkland Islands";
    countries["FJI"] = "Fiji";
    countries["FIN"] = "Finland";
    countries["FRA"] = "France";
    countries["GUF"] = "French Guiana";
    countries["PYF"] = "French Polynesia";
    countries["GAB"] = "Gabon";
    countries["GMB"] = "Gambia";
    countries["GEO"] = "Georgia";
    countries["DEU"] = "Germany";
    countries["GHA"] = "Ghana";
    countries["GIB"] = "Gibraltar";
    countries["GRC"] = "Greece";
    countries["GRL"] = "Greenland";
    countries["GRD"] = "Grenada";
    countries["GLP"] = "Guadeloupe";
    countries["GUM"] = "Guam";
    countries["GTM"] = "Guatemala";
    countries["GGY"] = "Guernsey";
    countries["GIN"] = "Guinea";
    countries["GNB"] = "Guinea-Bissau";
    countries["GUY"] = "Guyana";
    countries["HTI"] = "Haiti";
    countries["VAT"] = "Holy See";
    countries["HND"] = "Honduras";
    countries["HKG"] = "Hong Kong";
    countries["HUN"] = "Hungary";
    countries["ISL"] = "Iceland";
    countries["IND"] = "India";
    countries["IDN"] = "Indonesia";
    countries["IRN"] = "Iran";
    countries["IRQ"] = "Iraq";
    countries["IRL"] = "Ireland";
    countries["IMN"] = "Isle of Man";
    countries["ISR"] = "Israel";
    countries["ITA"] = "Italy";
    countries["JAM"] = "Jamaica";
    countries["JPN"] = "Japan";
    countries["JEY"] = "Jersey";
    countries["JOR"] = "Jordan";
    countries["KAZ"] = "Kazakhstan";
    countries["KEN"] = "Kenya";
    countries["KIR"] = "Kiribati";
    countries["KWT"] = "Kuwait";
    countries["KGZ"] = "Kyrgyzstan";
    countries["LAO"] = "Lao";
    countries["LVA"] = "Latvia";
    countries["LBN"] = "Lebanon";
    countries["LSO"] = "Lesotho";
    countries["LBR"] = "Liberia";
    countries["LBY"] = "Libya";
    countries["LIE"] = "Liechtenstein";
    countries["LTU"] = "Lithuania";
    countries["LUX"] = "Luxembourg";
    countries["MAC"] = "Macao";
    countries["MKD"] = "Macedonia";
    countries["MDG"] = "Madagascar";
    countries["MWI"] = "Malawi";
    countries["MYS"] = "Malaysia";
    countries["MDV"] = "Maldives";
    countries["MLI"] = "Mali";
    countries["MLT"] = "Malta";
    countries["MHL"] = "Marshall Islands";
    countries["MTQ"] = "Martinique";
    countries["MRT"] = "Mauritania";
    countries["MUS"] = "Mauritius";
    countries["MYT"] = "Mayotte";
    countries["FSM"] = "Micronesia";
    countries["MDA"] = "Moldova";
    countries["MCO"] = "Monaco";
    countries["MNG"] = "Mongolia";
    countries["MNE"] = "Montenegro";
    countries["MSR"] = "Montserrat";
    countries["MAR"] = "Morocco";
    countries["MOZ"] = "Mozambique";
    countries["MMR"] = "Myanmar";
    countries["NAM"] = "Namibia";
    countries["NRU"] = "Nauru";
    countries["NPL"] = "Nepal";
    countries["NLD"] = "Netherlands";
    countries["ANT"] = "Netherlands Antilles";
    countries["NCL"] = "New Caledonia";
    countries["NZL"] = "New Zealand";
    countries["NIC"] = "Nicaragua";
    countries["NER"] = "Niger";
    countries["NGA"] = "Nigeria";
    countries["NIU"] = "Niue";
    countries["NFK"] = "Norfolk Island";
    countries["MNP"] = "Northern Mariana Islands";
    countries["PRK"] = "North Korea";
    countries["NOR"] = "Norway";
    countries["OMN"] = "Oman";
    countries["PAK"] = "Pakistan";
    countries["PLW"] = "Palau";
    countries["PAN"] = "Panama";
    countries["PNG"] = "Papua New Guinea";
    countries["PRY"] = "Paraguay";
    countries["PER"] = "Peru";
    countries["PHL"] = "Philippines";
    countries["PCN"] = "Pitcairn";
    countries["POL"] = "Poland";
    countries["PRT"] = "Portugal";
    countries["PRI"] = "Puerto Rico";
    countries["QAT"] = "Qatar";
    countries["REU"] = "Réunion";
    countries["ROU"] = "Romania";
    countries["RUS"] = "Russian Federation";
    countries["RWA"] = "Rwanda";
    countries["SHN"] = "Saint Helena";
    countries["KNA"] = "Saint Kitts and Nevis";
    countries["LCA"] = "Saint Lucia";
    countries["SPM"] = "Saint Pierre and Miquelon";
    countries["VCT"] = "Saint Vincent and the Grenadines";
    countries["WSM"] = "Samoa";
    countries["SMR"] = "San Marino";
    countries["STP"] = "Sao Tome and Principe";
    countries["SAU"] = "Saudi Arabia";
    countries["SEN"] = "Senegal";
    countries["SRB"] = "Serbia";
    countries["SYC"] = "Seychelles";
    countries["SLE"] = "Sierra Leone";
    countries["SGP"] = "Singapore";
    countries["SVK"] = "Slovakia";
    countries["SVN"] = "Slovenia";
    countries["SLB"] = "Solomon Islands";
    countries["SOM"] = "Somalia";
    countries["ZAF"] = "South Africa";
    countries["KOR"] = "South Korea";
    countries["ESP"] = "Spain";
    countries["LKA"] = "Sri Lanka";
    countries["SDN"] = "Sudan";
    countries["SUR"] = "Suriname";
    countries["SJM"] = "Svalbard and Jan Mayen Islands";
    countries["SWZ"] = "Swaziland";
    countries["SWE"] = "Sweden";
    countries["CHE"] = "Switzerland";
    countries["SYR"] = "Syria";
    countries["TJK"] = "Tajikistan";
    countries["TZA"] = "Tanzania";
    countries["THA"] = "Thailand";
    countries["TLS"] = "Timor-Leste";
    countries["TGO"] = "Togo";
    countries["TKL"] = "Tokelau";
    countries["TON"] = "Tonga";
    countries["TTO"] = "Trinidad and Tobago";
    countries["TUN"] = "Tunisia";
    countries["TUR"] = "Turkey";
    countries["TKM"] = "Turkmenistan";
    countries["TCA"] = "Turks and Caicos Islands";
    countries["TUV"] = "Tuvalu";
    countries["UGA"] = "Uganda";
    countries["UKR"] = "Ukraine";
    countries["ARE"] = "United Arab Emirates";
    countries["GBR"] = "United Kingdom";
    countries["URY"] = "Uruguay";
    countries["UZB"] = "Uzbekistan";
    countries["VUT"] = "Vanuatu";
    countries["VEN"] = "Venezuela";
    countries["VNM"] = "Viet Nam";
    countries["VIR"] = "Virgin Islands";
    countries["WLF"] = "Wallis and Futuna Islands";
    countries["ESH"] = "Western Sahara";
    countries["YEM"] = "Yemen";
    countries["ZMB"] = "Zambia";
    countries["ZWE"] = "Zimbabwe";
    
    return countries;
}

// Fill a <select> with countries
function FillWithCountries( el )
{
    var countries = GetCountries();
    
    for ( abbrev in countries ) {
        var oOption = document.createElement("option");
        oOption.text = countries[abbrev];
        oOption.value = abbrev;
        el.add( oOption );
    }
}

// return an associative array of states/provinces keyed by their abbreviations
function GetStatesAndProvinces()
{
    var states = new Object();
    states[""] = "";
    
    states["AA"] = "AA (APO/FPO)";
    states["AE"] = "AE (APO/FPO)";
    states["AP"] = "AP (APO/FPO)";
    states["AL"] = "Alabama";
    states["AK"] = "Alaska";
    states["AZ"] = "Arizona ";
    states["AR"] = "Arkansas";
    states["CA"] = "California";
    states["CO"] = "Colorado";
    states["CT"] = "Connecticut";
    states["DE"] = "Delaware";
    states["DC"] = "District of Columbia";
    states["FL"] = "Florida";
    states["GA"] = "Georgia";
    states["HI"] = "Hawaii";
    states["ID"] = "Idaho";
    states["IL"] = "Illinois";
    states["IN"] = "Indiana";
    states["IA"] = "Iowa";
    states["KS"] = "Kansas";
    states["KY"] = "Kentucky";
    states["LA"] = "Louisiana";
    states["ME"] = "Maine";
    states["MD"] = "Maryland";
    states["MA"] = "Massachusetts";
    states["MI"] = "Michigan";
    states["MN"] = "Minnesota";
    states["MS"] = "Mississippi";
    states["MO"] = "Missouri";
    states["MT"] = "Montana";
    states["NE"] = "Nebraska";
    states["NV"] = "Nevada";
    states["NH"] = "New Hampshire";
    states["NJ"] = "New Jersey";
    states["NM"] = "New Mexico";
    states["NY"] = "New York";
    states["NC"] = "North Carolina";
    states["ND"] = "North Dakota";
    states["OH"] = "Ohio";
    states["OK"] = "Oklahoma";
    states["OR"] = "Oregon";
    states["PA"] = "Pennsylvania";
    states["RI"] = "Rhode Island";
    states["SC"] = "South Carolina";
    states["SD"] = "South Dakota";
    states["TN"] = "Tennessee";
    states["TX"] = "Texas";
    states["UT"] = "Utah";
    states["VT"] = "Vermont";
    states["VA"] = "Virginia";
    states["WA"] = "Washington";
    states["WV"] = "West Virginia";
    states["WI"] = "Wisconsin";
    states["WY"] = "Wyoming";

    states["AB"] = "Alberta";
    states["BC"] = "British Columbia";
    states["MB"] = "Manitoba";
    states["NB"] = "New Brunswick";
    states["NL"] = "Newfoundland and Labrador";
    states["NT"] = "Northwest Territories";
    states["NS"] = "Nova Scotia";
    states["NU"] = "Nunavut";
    states["ON"] = "Ontario";
    states["PE"] = "Prince Edward Island";
    states["QC"] = "Quebec";
    states["SK"] = "Saskatchewan";
    states["YT"] = "Yukon";
    
    return states;
}

// Fill a <select> with states and provinces
function FillWithStatesAndProvinces( el )
{
    var states = GetStatesAndProvinces();
        
    for ( abbrev in states ) {
        var oOption = document.createElement("option");
        oOption.text = states[abbrev];
        oOption.value = abbrev;
        el.add( oOption );
    }
}
