// Generate a list of distributors for a given country

var dist = [];
var swtb = [ "Canada", "United States", "United States Virgin Islands", "Mexico" ];

dist["Canada"] = [ "USA - Software Toolbox" ];
dist["United States" ] = [ "USA - Software Toolbox" ];
dist["South Africa" ] = [ "USA - Software Toolbox" ];
dist["US Virgin Islands" ] = [ "USA - Software Toolbox" ];
dist["Mexico"] = [ "USA - Software Toolbox" ];
// dist["Belgium"] = [ "Belgium - ATS Applied Tech Systems" ];
dist["Brazil"] = [ "Brazil - Exata Sistemas de Automação" ];
// dist["Denmark"] = [ "Denmark - PC Instruments" ];
// dist["Sweden"] = [ "Denmark - PC Instruments" ];
// dist["Norway"] = [ "Denmark - PC Instruments" ];
dist["India"] = [ "India - Protocol Automation Technologies" ];
// dist["Indonesia"] = [ "Indonesia - AIC Consulting" ];
dist["Japan"] = [ "Japan - Nissin Systems", "Japan - Takebishi Corp.", "Japan - Hashimoto Shokai" ];
dist["Korea"] = [ "Korea - BridgeWare", "Korea - RT Solutions" ];
dist["Netherlands"] = [ "Netherlands - ATS Applied Tech Systems" ];
dist["Romania"] = [ "Romania - Vianet Serv SRL" ];
dist["Russia"] = [ "Russia - SWD Software Ltd." ];
dist["Spain"] = [ "Spain - Sumelco Technologies" ];
dist["Switzerland"] = [ "Switzerland - Logicpark GmbH" ];
dist["Germany"] = [ "Switzerland - Logicpark GmbH" ];
dist["Austria"] = [ "Switzerland - Logicpark GmbH" ];
dist["Taiwan"] = [ "Taiwan - Youngtec Systems", "Taiwan - Sunware Information Systems" ];
dist["United Kingdom"] = [ "UK - M.A.C. Solutions" ];
dist["all"] = [];

function FindElement(myarray, element)
{
    for (var i=0; i<myarray.length; i++) {
	if (myarray[i] == element)
	    return i;
    }
    return -1;
}

function FindDropValue(dropdown, element)
{
    for (var i=0; i<dropdown.options.length; i++) {
	if (dropdown.options[i].value == element)
	    return i;
    }
    return -1;
}

var country;
for (country in dist)
{
    var distribs = dist[country];
    for (var i=0; i<distribs.length; i++) {
	if (FindElement(dist["all"], distribs[i]) == -1)
	    dist["all"][dist["all"].length] = distribs[i];
    }
}
dist["all"].sort();

var short_separator = [ "-------------------------" ];

function GetDistributorsByCountry(country)
{
    var shortlist = dist[country];
    var longlist = null;
    var fulllist = null;
    var cogent = [ "Canada - Cogent" ];
    var ontario = [ "Southern Ontario - Cogent" ];

    if (country == "Canada")
	longlist = ontario;
    else if (FindElement(swtb, country) == -1)
	longlist = dist["all"];

    if (shortlist == undefined)
	shortlist = null;
    
    if (shortlist != null && longlist != null) {
	if (country == "Japan")
	    fulllist = shortlist.concat(short_separator, cogent);
	else if (longlist != ontario)
	    fulllist = shortlist.concat(short_separator, cogent, longlist);
	else
	    fulllist = shortlist.concat(short_separator, longlist);
    } else if (shortlist != null) {
	fulllist = shortlist;
    } else {
	fulllist = cogent.concat(longlist);
    }
    
    return fulllist;
}

function DropAdd(dropdown, name, value)
{
    var option = new Option(name, value);
    try {
	dropdown.add(option, null);
    } catch (e) {
	dropdown.add(option);
    }
}

function SetDropDown(dropdown, country)
{
    var fulllist = GetDistributorsByCountry(country);
    var options = [];
    var option;
    var sepindex = FindElement(fulllist, short_separator);
    var prior = null;

    if (dropdown.selectedIndex != null &&
	dropdown.selectedIndex >= 0)
	prior = dropdown.options[dropdown.selectedIndex].value;

    while (dropdown.options.length > 0)
	dropdown.remove(0);

    if (fulllist.length > 1 && sepindex != 1)
	DropAdd (dropdown, "---- Please select your Cogent Distributor", "Not selected");
    
    for (var i=0; i<fulllist.length; i++) {
	DropAdd (dropdown, fulllist[i], fulllist[i] == short_separator ? "Other company" : fulllist[i]);
    }

    DropAdd (dropdown, "Other reseller ...", "Other company");

    //window.alert(prior);
    // var idx = FindDropValue (dropdown, prior);
    // if (idx != -1) {
	// dropdown.selectedIndex = idx;
    // }
}

