// SCI Project Selection Form
// Copyright Clifford E Ford 2008
// Released under the Gnu Public Licence (GPL)

/*global Cmp, LastUpdate, Option, alert, document, window */

// some initial constants
var nContinents = 7;
var nCountries = 300; // but not all used
var nTopics = 12;

// Country names and codes - incomplete so sparse array
var Countries = {};
var cic = [nCountries]; // Countries in Continents
Countries['Afghanistan'] = 193;
cic[193] = 1;
Countries['Albania'] = 1;
cic[1] = 3;
Countries['Algeria'] = 134;
cic[134] = 6;
Countries['Andorra'] = 184;
cic[184] = 3;
Countries['Andorra'] = 194;
cic[194] = 3;
Countries['Angola'] = 195;
cic[195] = 0;
Countries['Antigua and Barbuda'] = 196;
cic[196] = 4;
Countries['Argentina'] = 135;
cic[135] = 5;
Countries['Armenia'] = 2;
cic[2] = 3;
Countries['Australia'] = 3;
cic[3] = 2;
Countries['Austria'] = 4;
cic[4] = 3;
Countries['Azerbaijan'] = 5;
cic[5] = 3;
Countries['Bahamas'] = 197;
cic[197] = 4;
Countries['Bahrain'] = 198;
cic[198] = 6;
Countries['Bangladesh'] = 136;
cic[136] = 1;
Countries['Barbados'] = 199;
cic[199] = 4;
Countries['Belarus'] = 137;
cic[137] = 3;
Countries['Belgium - Flemish'] = 59;
cic[59] = 3;
Countries['Belgium - Walloon'] = 63;
cic[63] = 3;
Countries['Belize'] = 200;
cic[200] = 5;
Countries['Bhutan'] = 201;
cic[201] = 1;
Countries['Bolivia'] = 138;
cic[138] = 5;
Countries['Bosnia and Herzegovina'] = 53;
cic[53] = 3;
Countries['Botswana'] = 139;
cic[139] = 0;
Countries['Brasil'] = 140;
cic[140] = 5;
Countries['Brunei'] = 202;
cic[202] = 1;
Countries['Bulgaria'] = 9;
cic[9] = 3;
Countries['Burkina Faso'] = 141;
cic[141] = 0;
Countries['Burma'] = 203;
cic[203] = 1;
Countries['Burundi'] = 204;
cic[204] = 0;
Countries['Cambodia'] = 205;
cic[205] = 1;
Countries['Canada'] = 142;
cic[142] = 4;
Countries['Cape Verde'] = 206;
cic[206] = 0;
Countries['Central African Republic'] = 207;
cic[207] = 0;
Countries['Chad'] = 208;
cic[208] = 0;
Countries['China'] = 209;
cic[209] = 1;
Countries['Colombia'] = 143;
cic[143] = 5;
Countries['Comoros'] = 210;
cic[210] = 0;
Countries['Congo'] = 211;
cic[211] = 0;
Countries['Costa Rica'] = 212;
cic[212] = 5;
Countries['Croatia'] = 12;
cic[12] = 3;
Countries['Cuba'] = 213;
cic[213] = 5;
Countries['Cyprus'] = 185;
cic[185] = 3;
Countries['Cyprus'] = 214;
cic[214] = 3;
Countries['Czech Republic'] = 40;
cic[40] = 3;
Countries['Denmark'] = 13;
cic[13] = 3;
Countries['Djibouti'] = 215;
cic[215] = 0;
Countries['Dominica'] = 216;
cic[216] = 5;
Countries['Dominican Republic'] = 217;
cic[217] = 5;
Countries['East Timor'] = 218;
cic[218] = 1;
Countries['Ecuador'] = 145;
cic[145] = 5;
Countries['Egypt'] = 144;
cic[144] = 6;
Countries['El Salvador'] = 219;
cic[219] = 5;
Countries['Equatorial Guinea'] = 220;
cic[220] = 0;
Countries['Eritrea'] = 221;
cic[221] = 0;
Countries['Estonia'] = 14;
cic[14] = 3;
Countries['Ethiopia'] = 222;
cic[222] = 0;
Countries['Fiji'] = 224;
cic[224] = 2;
Countries['Finland'] = 62;
cic[62] = 3;
Countries['France'] = 16;
cic[16] = 3;
Countries['Gabon'] = 225;
cic[225] = 0;
Countries['Gambia'] = 226;
cic[226] = 0;
Countries['Georgia'] = 17;
cic[17] = 3;
Countries['Germany'] = 18;
cic[18] = 3;
Countries['Ghana'] = 146;
cic[146] = 0;
Countries['Great Britain'] = 179;
cic[179] = 3;
Countries['Greece'] = 21;
cic[21] = 3;
Countries['Groeenland'] = 22;
cic[22] = 3;
Countries['Guinea'] = 227;
cic[227] = 0;
Countries['Guinea-Bissau'] = 228;
cic[228] = 0;
Countries['Guyana'] = 229;
cic[229] = 5;
Countries['Haiti'] = 230;
cic[230] = 5;
Countries['Honduras'] = 231;
cic[231] = 5;
Countries['Hungary'] = 51;
cic[51] = 3;
Countries['Iceland'] = 25;
cic[25] = 3;
Countries['India'] = 147;
cic[147] = 1;
Countries['Indonesia'] = 148;
cic[148] = 1;
Countries['Iran'] = 232;
cic[232] = 6;
Countries['Iraq'] = 233;
cic[233] = 6;
Countries['Ireland'] = 23;
cic[23] = 3;
Countries['Israel'] = 149;
cic[149] = 6;
Countries['Italy'] = 26;
cic[26] = 3;
Countries['Ivory Coast'] = 150;
cic[150] = 0;
Countries['Jamaica'] = 234;
cic[234] = 4;
Countries['Japan'] = 19;
cic[19] = 1;
Countries['Jordan'] = 151;
cic[151] = 6;
Countries['Kazakhstan'] = 192;
cic[192] = 1;
Countries['Kenya'] = 152;
cic[152] = 0;
Countries['Kiribati'] = 235;
cic[235] = 2;
Countries['Korea, North'] = 236;
cic[236] = 1;
Countries['Korea, South'] = 11;
cic[11] = 1;
Countries['Kuwait'] = 237;
cic[237] = 6;
Countries['Kyrgyzstan'] = 27;
cic[27] = 1;
Countries['Laos'] = 238;
cic[238] = 1;
Countries['Latvia'] = 154;
cic[154] = 3;
Countries['Latvia'] = 239;
cic[239] = 3;
Countries['Lesotho'] = 153;
cic[153] = 0;
Countries['Liberia'] = 240;
cic[240] = 0;
Countries['Libya'] = 241;
cic[241] = 6;
Countries['Liechtenstein'] = 186;
cic[186] = 3;
Countries['Liechtenstein'] = 242;
cic[242] = 3;
Countries['Lithuania'] = 155;
cic[155] = 3;
Countries['Lithuania'] = 243;
cic[243] = 3;
Countries['Luxembourg'] = 31;
cic[31] = 3;
Countries['Macedonia'] = 32;
cic[32] = 3;
Countries['Madagascar'] = 244;
cic[244] = 0;
Countries['Malawi'] = 245;
cic[245] = 0;
Countries['Malaysia'] = 156;
cic[156] = 1;
Countries['Maldives'] = 246;
cic[246] = 0;
Countries['Mali'] = 247;
cic[247] = 0;
Countries['Malta'] = 248;
cic[248] = 3;
Countries['Malta'] = 187;
cic[187] = 3;
Countries['Marshall Islands'] = 249;
cic[249] = 2;
Countries['Mauritania'] = 250;
cic[250] = 0;
Countries['Mauritius'] = 157;
cic[157] = 0;
Countries['Mexico'] = 158;
cic[158] = 5;
Countries['Micronesia'] = 251;
cic[251] = 2;
Countries['Moldova'] = 33;
cic[33] = 3;
Countries['Monaco'] = 188;
cic[188] = 3;
Countries['Monaco'] = 252;
cic[252] = 3;
Countries['Mongolia'] = 34;
cic[34] = 1;
Countries['Montenegro'] = 35;
cic[35] = 3;
Countries['Morocco'] = 159;
cic[159] = 6;
Countries['Mozambique'] = 160;
cic[160] = 0;
Countries['Namibia'] = 253;
cic[253] = 0;
Countries['Nauru'] = 254;
cic[254] = 2;
Countries['Nepal'] = 161;
cic[161] = 1;
Countries['Netherlands'] = 37;
cic[37] = 3;
Countries['New Zealand'] = 189;
cic[189] = 2;
Countries['New Zealand'] = 255;
cic[255] = 2;
Countries['Nicaragua'] = 162;
cic[162] = 5;
Countries['Nigeria'] = 163;
cic[163] = 0;
Countries['Northern Ireland'] = 180;
cic[180] = 3;
Countries['Norway'] = 36;
cic[36] = 3;
Countries['Oman'] = 256;
cic[256] = 6;
Countries['Pakistan'] = 164;
cic[164] = 1;
Countries['Palau'] = 257;
cic[257] = 2;
Countries['Palestine'] = 165;
cic[165] = 6;
Countries['Panama'] = 258;
cic[258] = 5;
Countries['Papua New Guinea'] = 259;
cic[259] = 2;
Countries['Peru'] = 166;
cic[166] = 5;
Countries['Philippines'] = 260;
cic[260] = 2;
Countries['Poland'] = 38;
cic[38] = 3;
Countries['Portugal'] = 39;
cic[39] = 3;
Countries['Qatar'] = 261;
cic[261] = 6;
Countries['Romania'] = 167;
cic[167] = 3;
Countries['Russia'] = 43;
cic[43] = 3;
Countries['Rwanda'] = 262;
cic[262] = 0;
Countries['Saint Kitts and Nevis'] = 263;
cic[263] = 4;
Countries['Saint Lucia'] = 264;
cic[264] = 4;
Countries['Saint Vincent and the Grenadines'] = 265;
cic[265] = 4;
Countries['Samoa'] = 266;
cic[266] = 2;
Countries['San Marino'] = 190;
cic[190] = 3;
Countries['São Tomé and Príncipe'] = 267;
cic[267] = 0;
Countries['Saudi Arabia'] = 268;
cic[268] = 6;
Countries['Senegal'] = 168;
cic[168] = 0;
Countries['Serbia'] = 44;
cic[44] = 3;
Countries['Seychelles'] = 269;
cic[269] = 0;
Countries['Sierra Leone'] = 169;
cic[169] = 0;
Countries['Singapore'] = 270;
cic[270] = 1;
Countries['Slovakia'] = 41;
cic[41] = 3;
Countries['Slovenia'] = 45;
cic[45] = 3;
Countries['Solomon Islands'] = 271;
cic[271] = 2;
Countries['Somalia'] = 272;
cic[272] = 0;
Countries['South Africa'] = 273;
cic[273] = 0;
Countries['Spain'] = 46;
cic[46] = 3;
Countries['Spain - Catalunya'] = 54;
cic[54] = 3;
Countries['Sri Lanka'] = 170;
cic[170] = 1;
Countries['Sudan'] = 274;
cic[274] = 0;
Countries['Suriname'] = 275;
cic[275] = 5;
Countries['Swaziland'] = 276;
cic[276] = 0;
Countries['Sweden'] = 48;
cic[48] = 3;
Countries['Switzerland'] = 49;
cic[49] = 3;
Countries['Syria'] = 171;
cic[171] = 6;
Countries['Tajikistan'] = 172;
cic[172] = 1;
Countries['Tanzania'] = 173;
cic[173] = 0;
Countries['Thailand'] = 174;
cic[174] = 1;
Countries['Togo'] = 175;
cic[175] = 0;
Countries['Tonga'] = 277;
cic[277] = 2;
Countries['Trinidad and Tobago'] = 278;
cic[278] = 4;
Countries['Tunisia'] = 176;
cic[176] = 6;
Countries['Turkey'] = 177;
cic[177] = 6;
Countries['Turkmenistan'] = 279;
cic[279] = 1;
Countries['Tuvalu'] = 280;
cic[280] = 2;
Countries['Uganda'] = 178;
cic[178] = 0;
Countries['Ukraine'] = 50;
cic[50] = 3;
Countries['United Arab Emirates'] = 281;
cic[281] = 6;
Countries['United States of America'] = 55;
cic[55] = 4;
Countries['Uzbekistan'] = 52;
cic[52] = 1;
Countries['Vanuatu'] = 65;
cic[65] = 2;
Countries['Vatican City'] = 191;
cic[191] = 3;
Countries['Vietnam'] = 181;
cic[181] = 1;
Countries['Yemen'] = 282;
cic[282] = 6;
Countries['Zambia'] = 182;
cic[182] = 0;
Countries['Zimbawe'] = 183;
cic[183] = 0;

// countries in regions
var Continents = {};

Continents.Africa = {};
Continents.Africa.Countries = ["Angola", "Botswana", "Burkina Faso", "Burundi", "Cape Verde", "Central African Republic", "Chad", "Comoros", "Congo", "Djibouti", "Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia", "Ghana", "Guinea", "Guinea-Bissau", "Ivory Coast", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Maldives", "Mali", "Mauritania", "Mauritius", "Mozambique", "Namibia", "Nigeria", "Rwanda", "São Tomé and Príncipe", "Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa", "Sudan", "Swaziland", "Tanzania", "Togo", "Uganda", "Zambia", "Zimbawe"];
Continents.Africa.Index = 0;

Continents.Asia = {};
Continents.Asia.Countries = ["Afghanistan", "Bangladesh", "Bhutan", "Brunei", "Burma", "Cambodia", "China", "East Timor", "India", "Indonesia", "Japan", "Kazakhstan", "Korea, North", "Korea, South", "Kyrgyzstan", "Laos", "Malaysia", "Mongolia", "Nepal", "Pakistan", "Singapore", "Sri Lanka", "Tajikistan", "Thailand", "Turkmenistan", "Uzbekistan", "Vietnam"];
Continents.Asia.Index = 1;

Continents.Australasia = {};
Continents.Australasia.Countries = ["Australia", "Fiji", "Kiribati", "Marshall Islands", "Micronesia", "Nauru", "New Zealand", "New Zealand", "Palau", "Papua New Guinea", "Philippines", "Samoa", "Solomon Islands", "Tonga", "Tuvalu", "Vanuatu"];
Continents.Australasia.Index = 2;

Continents.Europe = {};
Continents.Europe.Countries = ["Albania", "Andorra", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium - Flemish", "Belgium - Walloon", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Cyprus", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Great Britain", "Greece", "Groeenland", "Hungary", "Iceland", "Ireland", "Italy", "Latvia", "Latvia", "Liechtenstein", "Liechtenstein", "Lithuania", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Malta", "Moldova", "Monaco", "Monaco", "Montenegro", "Netherlands", "Northern Ireland", "Norway", "Poland", "Portugal", "Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Spain - Catalunya", "Sweden", "Switzerland", "Ukraine", "Vatican City"];
Continents.Europe.Index = 3;

Continents['North America'] = {};
Continents['North America'].Countries = ["Antigua and Barbuda", "Bahamas", "Barbados", "Canada", "Jamaica", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Trinidad and Tobago", "United States of America"];
Continents['North America'].Index = 4;

Continents['Latin America'] = {};
Continents['Latin America'].Countries = ["Argentina", "Belize", "Bolivia", "Brasil", "Colombia", "Costa Rica", "Cuba", "Dominica", "Dominican Republic", "Ecuador", "El Salvador", "Guyana", "Haiti", "Honduras", "Mexico", "Nicaragua", "Panama", "Peru", "Suriname"];
Continents['Latin America'].Index = 5;

Continents.Mediterranean = {};
Continents.Mediterranean.Countries = ["Algeria", "Bahrain", "Egypt", "Iran", "Iraq", "Israel", "Jordan", "Kuwait", "Libya", "Morocco", "Oman", "Palestine", "Qatar", "Saudi Arabia", "Syria", "Tunisia", "Turkey", "United Arab Emirates", "Yemen"];
Continents.Mediterranean.Index = 6;

var Topics = {};
Topics['Antiracism, antifascism, ...'] = 1;
Topics['North South solidarity'] = 2;
Topics['Peace and disarmament'] = 3;
Topics.Disabilities = 4;
Topics['Children, teenagers, elderly'] = 5;
Topics.Environment = 6;
Topics['Sexuality and Gender'] = 7;
Topics['Socially disadvantaged'] = 8;
Topics['Art, culture, history'] = 9;
Topics['Ideology and spirituality'] = 10;
Topics.Others = 11;
Topics.Unspecified = 0;

var ContinentCounts = [nContinents];
var CountryCounts = [nCountries];
var TopicCounts = [nTopics];
var TotalCounts = [3];

var CampList = [];

var CampData = [];

var j, k;

for (j = 0; j < nCountries; j = j + 1) {
    CampData[j] = [nTopics];
    for (k = 0; k < nTopics; k = k + 1) {
        CampData[j][k] = [];
    }
}

function set_dates() {
    var begin = document.projects.Begin.value,
    camp,
    end = document.projects.End.value,
    i, j, k,
    next,
	country,
	continent;
    // zero the camp arrays at startup and after a date change
    for (i = 0; i < nContinents; i = i + 1) {
        ContinentCounts[i] = 0;
    }
    for (j = 0; j < nCountries; j = j + 1) {
        CountryCounts[j] = 0;
        for (k = 0; k < nTopics; k = k + 1) {
            CampData[j][k].length = 0;
        }
    }
    // put date-limited camps into the arrays
    TotalCounts[0] = 0;
    for (camp in Cmp) {
		if (Cmp.hasOwnProperty(camp)) {
	        if (Cmp[camp][0] < begin || Cmp[camp][1] > end) {
				continue; 
			}
	        next = CampData[Cmp[camp][2]] [Cmp[camp][3]].length;
	        CampData[Cmp[camp][2]][Cmp[camp][3]][next] = camp;
	        // count camps in continents here
			country = Cmp[camp][2];
			continent = cic[country];
	        ContinentCounts[continent] += 1;
	        TotalCounts[0] += 1;
	        // Count camps in countries here
	        CountryCounts[Cmp[camp][2]] += 1;
	    }
	}
    TotalCounts[1] = TotalCounts[0];
}

function fill_continents_box() {
    var anoption,
    continent,
    continents_box = document.projects.continents,
    index,
    nnn;

    continents_box.options.length = 0;
    anoption = new Option('All (' + TotalCounts[0] + ')', '');
    continents_box.options[continents_box.options.length] = anoption;
    for (continent in Continents) {
		if (Continents.hasOwnProperty(continent)) {
	        index = Continents[continent].Index;
	        nnn = ContinentCounts[index];
	        anoption = new Option(continent + ' (' + nnn + ')', continent);
	        continents_box.options[continents_box.options.length] = anoption;
	    }
	}
}

function fill_countries_box() {
    var all,
    anoption,
    continent,
    continents,
    continentname,
    country,
    countryid,
    countries = document.projects.countries,
    countryname,
    j,
    nnn;

    // clear the countries box
    countries.options.length = 0;
    // find if one or all contents are selected
    continents = document.projects.continents;
    continent = continents.selectedIndex;
    if (continent < 1) {
        nnn = TotalCounts[0];
		all = true;
    } else {
        nnn = ContinentCounts[continent - 1];
		all = false;
        TotalCounts[1] = nnn;
    }
    anoption = new Option('All (' + nnn + ')', '');
    countries.options[countries.options.length] = anoption;
    for (j = 0; j < continents.options.length; j = j + 1) {
        // put countries in the country box
        if (all) {
            // all or none selected - so go through all the countries
            for (countryname in Countries) {
				if (Countries.hasOwnProperty(countryname)) {
	                nnn = CountryCounts[Countries[countryname]];
	                anoption = new Option(countryname + ' (' + nnn + ')', Countries[countryname]);
	                countries.options[countries.options.length] = anoption;
				}
            }
            break;
        } else {
            if (continents.options[j].selected === true) {
                continentname = continents.options[j].value;
                for (country in Continents[continentname].Countries) {
					if (Continents[continentname].Countries.hasOwnProperty(country)) {
	                    countryname = Continents[continentname].Countries[country];
	                    countryid = Countries[countryname];
	                    nnn = CountryCounts[countryid];
	                    anoption = new Option(countryname + ' (' + nnn + ')', Countries[countryname]);
	                    countries.options[countries.options.length] = anoption;
					}
                }
            }
        }
    }
}

function fill_topics_box() {
    var anoption,
    countries = document.projects.countries,
    country,
    countryIndex,
    i,
    nnn,
    test,
    topic,
    topics = document.projects.topics;
    // zero the topic counts
    for (i = 0; i < nTopics; i = i + 1) {
        TopicCounts[i] = 0;
    }
    // selectedIndex < 1 means all countries in the countries box selected
    countryIndex = countries.selectedIndex;
    test = 0;
    nnn = 0;
    CampList.length = 0;
    for (country = 1; country < countries.options.length; country += 1) {
        if (countryIndex < 1 || countries.options[country].selected === true) {
            for (i = 0; i < nTopics; i = i + 1) {
                // if there is a country with no number Korea (North)
                if (CampData[countries.options[country].value] === null) {
                    if (test === 0) {
                        alert("Test: " + countries.options[country].value);
                        test = 1;
                    }
                } else {
                    TopicCounts[i] += CampData[countries.options[country].value][i].length;
                    nnn += CampData[countries.options[country].value][i].length;
                }
            }
        }
    }
    topics.options.length = 0;
    anoption = new Option('All (' + nnn + ')', '');
    topics.options[topics.options.length] = anoption;

    for (topic in Topics) {
		if (Topics.hasOwnProperty(topic)) {
	        nnn = TopicCounts[Topics[topic]];
	        anoption = new Option(topic + ' (' + nnn + ')', Topics[topic]);
	        topics.options[topics.options.length] = anoption;
	    }
	}
}

function fill_camps_box() {
    // fill the camps box
    var anOption,
    camp,
    camps = document.projects.camps,
    countries = document.projects.countries,
    country,
    countryIndex = countries.selectedIndex,
    i,
    topics = document.projects.topics,
    topicIndex = topics.selectedIndex;

    camps.options.length = 0;
    CampList.length = 0;
    for (country = 1; country < countries.options.length; country += 1) {
        if (countryIndex < 1 || countries.options[country].selected === true) {
            for (i = 0; i < nTopics; i = i + 1) {
                if (topicIndex < 1 || topics.options[i].selected === true) {
                    for (camp in CampData[countries.options[country].value][i]) {
						if (CampData[countries.options[country].value][i].hasOwnProperty(camp)) {
	                        CampList[CampList.length] =
	                            CampData[countries.options[country].value][i][camp];
	                    }
					}
                }
            }
        }
    }

    for (camp in CampList) {
		if (CampList.hasOwnProperty(camp)) {
	        anOption = new Option(Cmp[CampList[camp]][4] + ": " + Cmp[CampList[camp]][5], CampList[camp]);
	        camps.options[camps.options.length] = anOption;
	    }
	}
}

function ready_message(text) {
    var x;
    if (document.getElementById) {
        x = document.getElementById('messages');
		x.innerHTML = '';
		x.innerHTML = text + ' ' + CampList.length + ' Camps';
	} else if (document.all) {
		x = document.all.messages;
		x.innerHTML = text + ' ' + CampList.length + ' Camps';
	}
}

function init() {
    // set today's date in the form Start box
    var today = new Date(),
    begin = document.projects.Begin.value,
    end = document.projects.End.value,
    year = today.getYear(),
    month = 1 + today.getMonth(),
    day = today.getDate();

    // on linux the year needs 1900 added
    if (year < 1000) {
        year += + 1900;
    }
    // make the month two characters
    if (month < 10) { 
		month = "0" + month;
    } else { 
		month = month.toString(); 
	}
    // make the day two characters
    if (day < 10) { 
		day = "0" + day;
    } else { 
		day = day.toString(); 
	}
    document.projects.Begin.value = year + "/" + month + "/" + day;
    document.projects.End.value = (year + 1) + "/" + month + "/" + day;
    set_dates();
    // prime the continents array
    fill_continents_box();
    fill_countries_box();
    fill_topics_box();
    fill_camps_box();
    // Set the message line LastUpdate="2008/03/02"; //LastUpdate in campsini.js
    // nAdded=70;
    // nDeleted=0;
    ready_message('Filtering may take several seconds! Last update: ' + LastUpdate);
}

function wait_message() {
    var text = "Please wait, filtering...",
    x;
    if (document.getElementById) {
        x = document.getElementById('messages');
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all.messages;
		x.innerHTML = text;
	}
}

function debug_message(text) {
    var x;
    if (document.getElementById) {
        x = document.getElementById('debug');
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all.debug;
		x.innerHTML = text;
	}
}

function select_dates() {
    // need to clear selections in continent, country topic boxes
    document.projects.continents.selectedIndex = -1;
    document.projects.countries.selectedIndex = -1;
    document.projects.topics.selectedIndex = -1;
    set_dates();
    fill_continents_box();
    fill_countries_box();
    fill_topics_box();
    fill_camps_box();
    ready_message('Dates set: ');
}

function select_continent() {
    document.projects.countries.selectedIndex = -1;
    document.projects.topics.selectedIndex = -1;
    fill_countries_box();
    fill_topics_box();
    fill_camps_box();
    ready_message('Region set: ');
}

function select_country() {
    document.projects.topics.selectedIndex = -1;
    fill_topics_box();
    fill_camps_box();
    ready_message('Country set: ');
}

function select_topic() {
    fill_camps_box();
    ready_message('Topic set: ');
}

function show_camp() {
    var camps = document.projects.camps,
    campid = CampList[camps.selectedIndex],
    url = 'camp_details.php?id=' + campid,
    newwin;

    if (camps.selectedIndex < 0) {
        alert("Please select a Project from the Projects list!"); 
		return;
    }
    newwin = window.open(url, campid, 'scrollbars=yes,menubar=yes,resizeable=yes,width=750,height=500');
    newwin.focus();
    //debug_message(url+campid);
}

function show_all() {
    // make a list of selected countries
    var campid = 'AllCamps',
    countries = document.projects.countries,
    country,
    countryIndex = countries.selectedIndex,
    country_list = '',
    newwin,
    topic,
    topics = document.projects.topics,
    topicIndex = topics.selectedIndex,
    topic_list = '',
    // parse the start and end dates
    ymd = document.projects.Begin.value.split('/'),
    sy = ymd[0],
    sm = ymd[1],
    sd = ymd[2],
    ymd2 = document.projects.End.value.split('/'),
    ey = ymd2[0],
    em = ymd2[1],
    ed = ymd2[2],
    url;

    for (country = 1; country < countries.options.length; country += 1) {
        if (countryIndex < 1 || countries.options[country].selected === true) {
            country_list += countries.options[country].value + '.';
        }
    }
    for (topic = 1; topic < topics.options.length; topic += 1) {
        if (topicIndex < 1 || topics.options[topic].selected === true) {
            topic_list += topics.options[topic].value + '.';
        }
    }
    url = 'http://camps.sciint.org/result.php?branch=28&page=0&order=';
    url += '&sd=' + sd + '&sm=' + sm + '&sy=' + sy;
    url += '&ed=' + ed + '&em=' + em + '&ey=' + ey + '&dateMatters=checked';
    url += '&tmp_topic=' + topic_list;
    url += '&tmp_country=' + country_list;
    newwin = window.open(url, campid, 'scrollbars=yes,menubar=yes,resizeable=yes,width=750,height=500');
    newwin.focus();
}

// validate the form
function verify(f) {
    var adate,
    msg = "",
    ymd = f.Begin.value.split('/');

    adate = new Date(ymd[0], ymd[1], ymd[2]);
    if (adate === "Invalid Date") {
        msg += "The Begin Date value is invalid!\n";
    }
    ymd = f.End.value.split('/');
    adate = new Date(ymd[0], ymd[1], ymd[2]);
    if (adate === "Invalid Date") {
        msg += "The End Date value is invalid!\n";
    }
    if (msg !== "") {
        alert(msg);
        return false;
    }
    return true;
}
