/////////////////////////////////////////////////////////////////////////
// GLOBALS
/////////////////////////////////////////////////////////////////////////
distros    = new Array();

region_map = new Array();
region_map['all']     = 'all';
region_map['region1'] = 'North America';
region_map['region2'] = 'Australia';
region_map['region3'] = 'Middle East';
region_map['region4'] = 'Europe';
region_map['region5'] = 'India';
region_map['region6'] = 'Asia';
region_map['region7'] = 'Africa';


/////////////////////////////////////////////////////////////////////////
// CLASSES
/////////////////////////////////////////////////////////////////////////
function Distributor (tag, name, addr, country, contact, phone, mobile,
                      fax, email, web, note, region, countries)
{
    // Variables
    this.tag       = tag;
    this.name      = name;
    this.addr      = addr;
    this.country   = country;
    this.contact   = contact;
    this.phone     = phone;
    this.mobile    = mobile;
    this.fax       = fax;
    this.email     = email;
    this.web       = web;
    this.note      = note;
    this.region    = region;
    this.countries = countries;

    // Methods
    this.stringRow = stringRow;
}


/////////////////////////////////////////////////////////////////////////
// FUNCTIONS
/////////////////////////////////////////////////////////////////////////
// Remove duplicates in a sorted array
function removeDuplicates (a) {
    var i = 0;
    while (i < a.length) {
        if (a[i] == a[i + 1]) {
            a.splice(i,1);
            continue;
        }
        i++;
    }
}

// Create a string that has the distributor information in an HTML row
function stringRow (row) {
    // Start row
    string = '<tr id="d' + row + '">';

    webref_open  = this.web != "" ? '<a href="http://' + this.web + '" target="_blank">' : '';
    webref_close = this.web != "" ? '</a>' : '';

    // Countries
    string += '<td class="d_countries">';
    for (c in this.countries)
        string += this.countries[c] + '<br />';
    string += '</td>';

    // Name
    string += '<td class="d_name">' + webref_open + '<b>' + this.name +
        '</b>' + webref_close + '<br />';

    // Address
    string += '<td class="d_addr">';
    for (line = 0; line != this.addr.length; line++) {
        string += this.addr[line] + '<br />';
    }
    string += this.country + '<br />';
    string += '</td>';

    // Contact info
    string += '<td class="d_contact">';
    string += this.contact != "" ? 'Contact: ' + this.contact + '<br />' : '';
    string += this.phone   != "" ? 'Phone: '   + this.phone   + '<br />' : '';
    string += this.fax     != "" ? 'Fax: '     + this.fax     + '<br />' : '';
    string += this.mobile  != "" ? 'Mobile: '  + this.mobile  + '<br />' : '';
    string += this.email   != "" ? 'Email: <a href=\"mailto:' + this.email +
        '">' + this.email + '</a><br />' : '';
    string += this.web     != "" ? 'Web: ' + webref_open + this.web +
        webref_close + '<br />' : '';
    string += this.note    != "" ? this.note + '<br />' : '';
    string += '</td></tr>';

    return string;
}

// Check for a matching region
function regionMatch (tomatch, regions) {
    if (tomatch == "all") return true;
    for (r in regions) {
        if (tomatch == regions[r]) return true;
    }
    return false;
}

// Check for a matching country
function countryMatch (tomatch, countries) {
    if (tomatch == false) return true;
    for (c in countries) {
        if (tomatch == countries[c]) return true;
    }
    return false;
}

function showDistributors (region, country) {
    // We won't put the names of the regions in the html so we need
    // to use the region_map here to get the names
    var region_name = region_map[region]

    // Set the country to false for easier processing below
    if (country == "all") country = false;

    // Keep track of which countries are displayed for this region
    var countries = new Array();

    var row_text = '';

    // For setting row color
    var row = 0;

    // Get all the distributors that match this region and generate the
    // table data to put inserted in the HTML later
    for (d in distros) {
        dist = distros[d];

        // Only include this distributor if it matches the
        // region or we're showing all
        if (regionMatch(region_name, dist.region)) {
            if (countryMatch(country, dist.countries)) {
                row_text += dist.stringRow(row % 2);
                row += 1;
            }

            // Remember which countries are covered in this region
            for (c in dist.countries)
                countries.push(dist.countries[c])
        }
    }

    string = '';

    // Sort and remove duplicate countries
    countries.sort();
    removeDuplicates(countries);

    selected = country ? '' : 'selected="selected" ';

    // Add the select box
    string += '<p>Filter by country in this region: ' +
        '<select onChange="showDistributors(\'' + region + '\', ' +
        'this.options[this.selectedIndex].value)">' +
        '<option ' + selected + 'value="all">-- All --</option>';

    for (c in countries) {
        selected = (country && country == countries[c]) ?
            'selected="selected" ' : '';
        string += '<option ' + selected + ' value="' + countries[c] + '">' +
            countries[c] + '</option>'
    }
    string += '</select></p>';

    // Generate table text
    string += '<table class="distros">' +
        '<tr><th>Countries</th><th>Name</th>' +
        '<th>Address</th><th>Contact</th></tr>' +
        row_text + '</table>';

    document.getElementById("list").innerHTML = string;
}

