var ddlSchoolsEmpty = createSchoolsDropDownList();
ddlSchoolsEmpty.appendChild(createOption('', 'Select a school board on previous field...'));

var ddlSchoolsMargueriteBourgeoys = createSchoolsDropDownList();
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('', 'Select a school...'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Cavalier-de-LaSalle', 'Cavalier-de-LaSalle'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Des Sources', 'Des Sources'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Jean XXIII', 'Jean XXIII'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Jean XXIII', 'Jean XXIII'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Mont-Royal', 'Mont-Royal'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Paul-Gérin-Lajoie', 'Paul-Gérin-Lajoie'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Pierre-Laporte', 'Pierre-Laporte'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Saint-Laurent', 'Saint-Laurent'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('Saint-Louis', 'Saint-Louis'));
ddlSchoolsMargueriteBourgeoys.appendChild(createOption('other', 'Other school...'));

var ddlSchoolsMontreal = createSchoolsDropDownList();
ddlSchoolsMontreal.appendChild(createOption('', 'Select a school...'));
ddlSchoolsMontreal.appendChild(createOption('Académie Duntun', 'Académie Dunton'));
ddlSchoolsMontreal.appendChild(createOption('Académie de Roberval', 'Académie de Roberval'));
ddlSchoolsMontreal.appendChild(createOption('Chomedey-De-Maisonneuve', 'Chomedey-De-Maisonneuve'));
ddlSchoolsMontreal.appendChild(createOption('De la Lancée', 'De la Lancée'));
ddlSchoolsMontreal.appendChild(createOption('De la Source', 'De la Source'));
ddlSchoolsMontreal.appendChild(createOption('Édouard-Montpetit', 'Édouard-Montpetit'));
ddlSchoolsMontreal.appendChild(createOption('Espace-Jeunesse', 'Espace-Jeunesse'));
ddlSchoolsMontreal.appendChild(createOption('Eulalie-Durocher', 'Eulalie-Durocher'));
ddlSchoolsMontreal.appendChild(createOption('Eurêka', 'Eurêka'));
ddlSchoolsMontreal.appendChild(createOption('Évangéline', 'Évangéline'));
ddlSchoolsMontreal.appendChild(createOption('FACE', 'FACE'));
ddlSchoolsMontreal.appendChild(createOption('Georges-Vanier', 'Georges-Vanier'));
ddlSchoolsMontreal.appendChild(createOption('Honoré-Mercier', 'Honoré-Mercier'));
ddlSchoolsMontreal.appendChild(createOption('École internationale de Montréal', 'École internationale de Montréal'));
ddlSchoolsMontreal.appendChild(createOption('Irénée-Lussier', 'Irénée-Lussier'));
ddlSchoolsMontreal.appendChild(createOption('Jeanne-Mance', 'Jeanne-Mance'));
ddlSchoolsMontreal.appendChild(createOption('Joseph-Charbonneau', 'Joseph-Charbonneau'));
ddlSchoolsMontreal.appendChild(createOption('Joseph-François-Perreault', 'Joseph-François-Perreault'));
ddlSchoolsMontreal.appendChild(createOption('La Dauversière', 'La Dauversière'));
ddlSchoolsMontreal.appendChild(createOption('Le Tremplin', 'Le Tremplin'));
ddlSchoolsMontreal.appendChild(createOption('Le Vitrail', 'Le Vitrail'));
ddlSchoolsMontreal.appendChild(createOption('La Voie', 'La Voie'));
ddlSchoolsMontreal.appendChild(createOption('Louis-Joseph-Papineau', 'Louis-Joseph-Papineau'));
ddlSchoolsMontreal.appendChild(createOption('Louis-Riel', 'Louis-Riel'));
ddlSchoolsMontreal.appendChild(createOption('Louise-Trichet', 'Louise-Trichet'));
ddlSchoolsMontreal.appendChild(createOption('Lucien-Pagé', 'Lucien-Pagé'));
ddlSchoolsMontreal.appendChild(createOption('Marguerite-De-Lajemmerais', 'Marguerite-De-Lajemmerais'));
ddlSchoolsMontreal.appendChild(createOption('Marie-Anne', 'Marie-Anne'));
ddlSchoolsMontreal.appendChild(createOption('Père-Marquette', 'Père-Marquette'));
ddlSchoolsMontreal.appendChild(createOption('Rosalie-Jetté', 'Rosalie-Jetté'));
ddlSchoolsMontreal.appendChild(createOption('Saint-Henri', 'Saint-Henri'));
ddlSchoolsMontreal.appendChild(createOption('Saint-Louis', 'Saint-Louis'));
ddlSchoolsMontreal.appendChild(createOption('Saint-Luc', 'Saint-Luc'));
ddlSchoolsMontreal.appendChild(createOption('Sophie-Barat', 'Sophie-Barat'));
ddlSchoolsMontreal.appendChild(createOption('other', 'Other school...'));

var ddlSchoolsPointeDeIle = createSchoolsDropDownList();
ddlSchoolsPointeDeIle.appendChild(createOption('', 'Select a school...'));
ddlSchoolsPointeDeIle.appendChild(createOption('Antoine-de-St-Exupéry', 'Antoine-de-St-Exupéry'));
ddlSchoolsPointeDeIle.appendChild(createOption('Calixa-Lavallée', 'Calixa-Lavallée'));
ddlSchoolsPointeDeIle.appendChild(createOption('D\'Anjou', 'D\'Anjou'));
ddlSchoolsPointeDeIle.appendChild(createOption('Daniel-Johnson', 'Daniel-Johnson'));
ddlSchoolsPointeDeIle.appendChild(createOption('Guy-Vanier', 'Guy-Vanier'));
ddlSchoolsPointeDeIle.appendChild(createOption('Henri-Bourassa', 'Henri-Bourassa'));
ddlSchoolsPointeDeIle.appendChild(createOption('Jean-Grou', 'Jean-Grou'));
ddlSchoolsPointeDeIle.appendChild(createOption('Jean-Grou Annexe', 'Jean-Grou Annexe'));
ddlSchoolsPointeDeIle.appendChild(createOption('La Passerelle', 'La Passerelle'));
ddlSchoolsPointeDeIle.appendChild(createOption('Le Prélude', 'Le Prélude'));
ddlSchoolsPointeDeIle.appendChild(createOption('Le Tournesol', 'Le Tournesol'));
ddlSchoolsPointeDeIle.appendChild(createOption('Pointe-aux-Trembles', 'Pointe-aux-Trembles'));
ddlSchoolsPointeDeIle.appendChild(createOption('other', 'Other school...'));

var ddlSchoolsEnglishMontreal = createSchoolsDropDownList();
ddlSchoolsEnglishMontreal.appendChild(createOption('', 'Select a school...'));
ddlSchoolsEnglishMontreal.appendChild(createOption('FACE', 'FACE'));
ddlSchoolsEnglishMontreal.appendChild(createOption('James Lyng', 'James Lyng'));
ddlSchoolsEnglishMontreal.appendChild(createOption('John F. Kennedy', 'John F. Kennedy'));
ddlSchoolsEnglishMontreal.appendChild(createOption('John Grant', 'John Grant'));
ddlSchoolsEnglishMontreal.appendChild(createOption('LaurenHill Academy', 'LaurenHill Academy'));
ddlSchoolsEnglishMontreal.appendChild(createOption('LaurenHill Junior campus', 'LaurenHill Junior campus'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Lester B. Pearson', 'Lester B. Pearson'));
ddlSchoolsEnglishMontreal.appendChild(createOption('MIND', 'MIND'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Marymount Academy', 'Marymount Academy'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Paul VI', 'Paul VI'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Rosemount', 'Rosemount'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Royal Vale', 'Royal Vale'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Royal West Academy', 'Royal West Academy'));
ddlSchoolsEnglishMontreal.appendChild(createOption('St.Raphael', 'St.Raphael'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Vincent Massey College', 'Vincent Massey College'));
ddlSchoolsEnglishMontreal.appendChild(createOption('Westmount', 'Westmount'));
ddlSchoolsEnglishMontreal.appendChild(createOption('other', 'Other school...'));

var ddlSchoolsLesterBPearson = createSchoolsDropDownList();
ddlSchoolsLesterBPearson.appendChild(createOption('', 'Select a school...'));
ddlSchoolsLesterBPearson.appendChild(createOption('Beaconsfield', 'Beaconsfield'));
ddlSchoolsLesterBPearson.appendChild(createOption('Beurling Academy', 'Beurling Academy'));
ddlSchoolsLesterBPearson.appendChild(createOption('John Rennie', 'John Rennie'));
ddlSchoolsLesterBPearson.appendChild(createOption('Lakeside Academy', 'Lakeside Academy'));
ddlSchoolsLesterBPearson.appendChild(createOption('LaSalle Community Comprehensive', 'LaSalle Community Comprehensive'));
ddlSchoolsLesterBPearson.appendChild(createOption('Lindsay Place', 'Lindsay Place'));
ddlSchoolsLesterBPearson.appendChild(createOption('MacDonald', 'MacDonald'));
ddlSchoolsLesterBPearson.appendChild(createOption('Pierrefonds Comprehensive', 'Pierrefonds Comprehensive'));
ddlSchoolsLesterBPearson.appendChild(createOption('Riverdale', 'Riverdale'));
ddlSchoolsLesterBPearson.appendChild(createOption('St.Thomas', 'St.Thomas'));
ddlSchoolsLesterBPearson.appendChild(createOption('Westwood Junior', 'Westwood Junior'));
ddlSchoolsLesterBPearson.appendChild(createOption('Westwood Senior', 'Westwood Senior'));
ddlSchoolsLesterBPearson.appendChild(createOption('other', 'Other school...'));

var ddlSchoolsOther = createSchoolsDropDownList();
ddlSchoolsOther.appendChild(createOption('other', 'Other school...'));

function createSchoolsDropDownList()
{
	var ddlSchools = document.createElement('select');
	ddlSchools.setAttribute('id', 'ddlSchools');
	
	return ddlSchools;
}

function createOption(value, label)
{
	var option = document.createElement('option');
	option.setAttribute('value', value);
	option.appendChild(document.createTextNode(label));
	
	return option;
}

function populateSchoolsDropDownList(selectedSchoolBoard, selectedSchool)
{
	var currentSchoolsDropDownList = document.getElementById('ddlSchools');
	
	switch(selectedSchoolBoard)
	{
		case '':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsEmpty, selectedSchool);
			break;
		case 'Marguerite-Bourgeoys':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsMargueriteBourgeoys, selectedSchool);
			break;
		case 'Montréal':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsMontreal, selectedSchool);
			break;
		case 'Pointe-de-lÎle':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsPointeDeIle, selectedSchool);
			break;
		case 'English Montreal':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsEnglishMontreal, selectedSchool);
			break;
		case 'Lester B.- Pearson':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsLesterBPearson, selectedSchool);
			break;
		case 'other':
			switchSchoolsDropDownList(currentSchoolsDropDownList, ddlSchoolsOther, selectedSchool);
			break;
	}
	
	toggleOtherSelection(currentSchoolsDropDownList.options[currentSchoolsDropDownList.selectedIndex].value, 'schoolOtherInputContainer');
}

function switchSchoolsDropDownList(currentDropDownList, newDropDownList, selectedSchool)
{
	// remove every options from currentDropDownList
	while (currentDropDownList.hasChildNodes())
	{
		currentDropDownList.removeChild(currentDropDownList.firstChild);
	}
	
	// add options found into the new drop down list to the current drop down list
	for (var i = 0; i < newDropDownList.childNodes.length; i++)
	{
		var clonedOption = newDropDownList[i].cloneNode(true);
		
		if (clonedOption.value == selectedSchool)
		{
			clonedOption.selected = true;
		}
		
		currentDropDownList.appendChild(clonedOption);
	}
}

function toggleOtherSelection(selectedValue, otherSelectionContainerID)
{
	var otherSelectionContainer = document.getElementById(otherSelectionContainerID);
	if (selectedValue == 'other')
	{
		otherSelectionContainer.style.display = 'inline';
	}
	else
	{
		otherSelectionContainer.style.display = 'none';
	}
}
