var organisations = new Array();

function Organisation(orgID, orgName) {
	this.orgID = orgID;
	this.orgName = orgName;
	this.catalogues = new Array();
}

function addOrganisation(orgID, orgName) {
	return organisations[organisations.length++] = new Organisation(orgID, orgName);
}

Organisation.prototype.addCatalogue = function(catalogueID, catalogueName) {
	return this.catalogues[this.catalogues.length++] = new Catalogue(catalogueID, catalogueName);
}

function Catalogue(catalogueID, catalogueName) {
	this.catalogueID = catalogueID;
	this.catalogueName = catalogueName;
	this.sections = new Array();
}

Catalogue.prototype.addSection = function(sectionID, sectionName) {
	return this.sections[this.sections.length++] = new Section(sectionID, sectionName);
}

function Section(sectionID, sectionName) {
	this.sectionID = sectionID;
	this.sectionName = sectionName;
}

//with default first option
function populateOrganisations(form) {
	_populateOrganisations(form, false, true, "All", "");
}

function populateOrganisations2(form, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue) {
		_populateOrganisations(form, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue);
}

function _populateOrganisations(form, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue) {
	
	form.orgID.options.length = 0;
	if (isAddEmptyOption == true) {
		form.orgID.options[form.orgID.options.length] = new Option("", "");
	}
	if (isAddCustomOption == true) {
		form.orgID.options[form.orgID.options.length] = new Option(customOptionLabel, customOptionValue);
	}
	
	for (var i = 0; i < organisations.length; i++) {
		form.orgID.options[form.orgID.options.length] = new Option(organisations[i].orgName, organisations[i].orgID);
	}
}

function populateCatalogues(form, orgID) {
	_populateCatalogues(form, orgID, false, true, "All", "");
}

function populateCatalogues2(form, orgID, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue) {
	_populateCatalogues(form, orgID, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue);
}

function _populateCatalogues(form, orgID, isAddEmptyOption, isAddCustomOption, customOptionLabel, customOptionValue) {
	
	form.catalogueID.options.length = 0;
	if (isAddEmptyOption == true) {
		form.catalogueID.options[form.catalogueID.options.length] = new Option("", "");	
	}
	if (isAddCustomOption == true) {
		form.catalogueID.options[form.catalogueID.options.length] = new Option(customOptionLabel, customOptionValue);
	}
	
	for (var i = 0; i < organisations.length; i++) {
		if (organisations[i].orgID == orgID) {
			var organisation = organisations[i];
			for (var j = 0; j < organisation.catalogues.length; j++) {
				form.catalogueID.options[form.catalogueID.options.length] = new Option(organisation.catalogues[j].catalogueName, organisation.catalogues[j].catalogueID);
			}
		}
	}
}

function mergeSections(orgID, catalogueID) {
	var sections1 = new Array();
	for (var i = 0; i < organisations.length; i++) {
		if (orgID == "" || organisations[i].orgID == orgID) {
			var organisation = organisations[i];
			for (var j = 0; j < organisation.catalogues.length; j++) {
				if (catalogueID == "" || organisation.catalogues[j].catalogueID == catalogueID) {
					var catalogue = organisation.catalogues[j];
					for (var k = 0; k < catalogue.sections.length; k++) {
						var section = catalogue.sections[k];
						sections1[sections1.length] = new Section(section.sectionID, ltrim(section.sectionName));
					}
				}
			}
		}
	}
	sections1.sort(compareSections);
	sections2 = new Array();
	for (var i = 0; i < sections1.length; i++) {
		if (i > 0 && sections1[i].sectionName == sections1[i-1].sectionName) {
			sections2[sections2.length-1].sectionID += "," + sections1[i].sectionID;
		}
		else {
			sections2[sections2.length] = sections1[i];
		}
	}
	return sections2;
}

function compareSections(a, b) {
	if ( a.sectionName < b.sectionName ) return -1;
	if ( a.sectionName > b.sectionName ) return 1;
	return 0;
}

function populateSections(form, orgID, catalogueID) {
	var field = form.attributeTopSectionID;
	field.options.length = 0;
	field.options[field.options.length] = new Option("All", "");
	if (catalogueID != "") {
		for (var i = 0; i < organisations.length; i++) {
			if (orgID == "" || organisations[i].orgID == orgID) {
				var organisation = organisations[i];
				for (var j = 0; j < organisation.catalogues.length; j++) {
					if (catalogueID == "" || organisation.catalogues[j].catalogueID == catalogueID) {
						var catalogue = organisation.catalogues[j];
						for (var k = 0; k < catalogue.sections.length; k++) {
							field.options[field.options.length] = new Option(catalogue.sections[k].sectionName, catalogue.sections[k].sectionID);
						}
					}
				}
			}
		}
	}
	else {
		var sections = mergeSections(orgID, catalogueID);
		for (var i = 0; i < sections.length; i++) {
			field.options[field.options.length] = new Option(sections[i].sectionName, sections[i].sectionID);
		}
	}
}
