function Database() {
   this.products              = new Array();
   this.productSheets         = new Array();
   this.categories            = new Array();
   this.categoryProducts      = new Array();
   this.mainCategories        = new Array();
   this.mainCategorySurfaces  = new Array();
   this.mainCategoryAppliances = new Array();
   this.corrosivityCategories = new Array();
   this.environments          = new Array();
   this.pretreatments         = new Array();
   this.componentTypes        = new Array();
   this.companies             = new Array();
   this.surfaces              = new Array();
   this.appliances            = new Array();
   this.systems               = new Array();
   this.systemLayers          = new Array();
   this.systemUsages          = new Array();
   this.systemUsageCategories = new Array();
   this.infoPages             = new Array();
   this.systemAppliances      = new Array();
   this.systemCorrosivityCategories = new Array();
}



Database.prototype.addProduct = function(product) {
   product.database = this;
   this.products[product.id] = product;
}

Database.prototype.addProductSheet = function(productSheet) {
   productSheet.database = this;
   this.productSheets[productSheet.id] = productSheet;
}

Database.prototype.addCategory = function(category) {
   category.database = this;
   this.categories[category.id] = category;
}

Database.prototype.addMainCategory = function(mainCategory) {
   mainCategory.database = this;
   this.mainCategories[mainCategory.id] = mainCategory;
}

Database.prototype.addCompany = function(company) {
   company.database = this;
   this.companies[company.id] = company;
}

Database.prototype.addSurface = function(surface) {
   surface.database = this;
   this.surfaces[surface.id] = surface;
}

Database.prototype.addAppliance = function(appliance) {
   appliance.database = this;
   this.appliances[appliance.id] = appliance;
}

Database.prototype.addCorrosivityCategory = function(corrosivityCategory) {
   corrosivityCategory.database = this;
   this.corrosivityCategories[corrosivityCategory.id] = corrosivityCategory;
}

Database.prototype.addEnvironment = function(environment) {
   environment.database = this;
   this.environments[environment.id] = environment;
}

Database.prototype.addPretreatment = function(pretreatment) {
   pretreatment.database = this;
   this.pretreatments[pretreatment.id] = pretreatment;
}

Database.prototype.addSystem = function(system) {
   system.database = this;
   this.systems[system.id] = system;
}

Database.prototype.addSystemLayer = function(systemLayer) {
   systemLayer.database = this;
   this.systemLayers[systemLayer.id] = systemLayer;
}

Database.prototype.addSystemUsage = function(systemUsage) {
   systemUsage.database = this;
   this.systemUsages[systemUsage.id] = systemUsage;
}

Database.prototype.addSystemUsageCategory = function(systemUsageCategory) {
   systemUsageCategory.database = this;
   this.systemUsageCategories[systemUsageCategory.id] = systemUsageCategory;
}

Database.prototype.addComponentType = function(componentType) {
   componentType.database = this;
   this.componentTypes[componentType.id] = componentType;
}

Database.prototype.addInfoPage = function(infoPage) {
   infoPage.database = this;
   this.infoPages[infoPage.id] = infoPage;
}



/* ---------------------------------------- */



function Product(id, code, name, description, version, density, solids, repaint, hasTech, sheet) {
   this.id          = id;
   this.code        = code;
   this.name        = name;
   this.description = description;
   this.version     = version;
   this.density     = density;
   this.solids      = solids;
   this.repaint     = repaint;
   this.hasTech     = hasTech;
   this.sheet       = sheet;
   this.database    = null;
}



Product.prototype.getProductSheets = function(msds) {
   var results = new Array();
   for (var i in this.database.productSheets) {
      if (this.database.productSheets[i].product == this.id && this.database.productSheets[i].msds == msds) {
         results.push(this.database.productSheets[i]);
      }
   }
   return results;
}



function productSort(a, b) {
   if (a.name > b.name) {
      return 1;
   } else if (a.name < b.name) {
      return -1;
   } else {
      return 0;
   }
}



/* ---------------------------------------- */



function ProductSheet(id, product, name, file, msds) {
   this.id       = id;
   this.product  = product;
   this.name     = name;
   this.file     = file;
   this.msds     = msds;
   this.database = null;
}



/* ---------------------------------------- */



function Category(id, name, mainCategory) {
   this.id           = id;
	this.name         = name;
   this.mainCategory = mainCategory;
   this.database     = null;
}



Category.prototype.getProducts = function() {
   var results = new Array();
   for (var i = 0; i < this.database.categoryProducts.length; i++) {
      if (this.database.categoryProducts[i][1] == this.id) {
         results[this.database.categoryProducts[i][0]] = this.database.products[this.database.categoryProducts[i][0]];
      }
   }
   return results;
}



/* ---------------------------------------- */



function MainCategory(id, company, name) {
   this.id           = id;
   this.company      = company;
   this.name         = name;
   this.database     = null;
}



MainCategory.prototype.getCategories = function() {
   var results = new Array();
   for (var i in this.database.categories) {
      if (this.database.categories[i].mainCategory == this.id) {
         results.push(this.database.categories[i]);
      }
   }
   return results;
}



MainCategory.prototype.getProducts = function() {
   var results = new Array();
   var categories = this.getCategories();
   
   for (var i in categories) {
		if ( typeof categories[i].getProducts == 'function' )
	      for (var j in categories[i].getProducts()) {
	         if (indexOf(categories[i].getProducts()[j], results) == -1) {
	            results.push(categories[i].getProducts()[j]);
	         }
	      }
   }

   results.sort(productSort);
   
   return results;
}



MainCategory.prototype.getCompany = function() {
   return this.database.companies[this.company];
}



MainCategory.prototype.getSurfaces = function() {
   var results = new Array();
   for (var i in this.database.mainCategorySurfaces) {
      if (this.database.mainCategorySurfaces[i][0] == this.id) {
         results.push(this.database.surfaces[this.database.mainCategorySurfaces[i][1]]);
      }
   }
   return results;
}



MainCategory.prototype.getCorrosivityCategories = function() {
   var results = new Array();
   for (var i in this.database.mainCategoryAppliances) {
      if (this.database.mainCategoryAppliances[i][0] == this.id) {
         var appliance = this.database.appliances[this.database.mainCategoryAppliances[i][1]];
         var corrosivityCategory = this.database.corrosivityCategories[appliance.corrosivityCategory].name;
         if (indexOf(this.database.corrosivityCategories[appliance.corrosivityCategory], results) == -1) {
            results.push(this.database.corrosivityCategories[appliance.corrosivityCategory]);
         }
      }
   }
   return results;
}



MainCategory.prototype.getInfoPages = function() {
   var results = new Array();
   for (var i in this.database.infoPages) {
      if (this.database.infoPages[i].mainCategory == this.id) {
         results[i] = this.database.infoPages[i];
      }
   }
   return results;
}



MainCategory.prototype.getSystems = function(surface, corrosivityCategory) {
   var results = new Array();
   for (var i in this.database.systems) {
      if (typeof this.database.systems[i].getAppliances != 'undefined') {
	      var appliances = this.database.systems[i].getAppliances();
	      for (var j = 0; j < appliances.length; j++) {
	         if (appliances[j].surface == surface.id && appliances[j].corrosivityCategory == corrosivityCategory.id ) {
	            results.push(this.database.systems[i]);
	         }
	      }
	   }
   }
   return results;
}



/* ---------------------------------------- */



function Company(id, name, data, info) {
   this.id           = id;
	this.name         = name;
	this.data         = data;
	this.info         = info;
   this.database     = null;
}



/* ---------------------------------------- */



function Property(id, name) {
   this.id           = id;
	this.name         = name;
   this.database     = null;
}



/* ---------------------------------------- */



function CorrosivityCategory(id, name) {
   this.id           = id;
	this.name         = name;
   this.database     = null;
}



CorrosivityCategory.prototype.getAppliance = function(surface, pretreatment) {
   for (var i in this.database.appliances) {
      if (this.database.appliances[i].corrosivityCategory == this.id) {
         if (this.database.appliances[i].surface == surface && ( pretreatment == null || this.database.appliances[i].pretreatment == pretreatment )) {
            return this.database.appliances[i];
         }
      }
   }
   return null;
}



CorrosivityCategory.prototype.getSurfaces = function(mainCategory, pretreatment) {
   var results = new Array();
   for (var i in this.database.appliances) {
      if (this.database.appliances[i].corrosivityCategory == this.id) {
         for (var j = 0; j < this.database.mainCategoryAppliances.length; j++) {
            if (this.database.mainCategoryAppliances[j][0] == mainCategory.id && this.database.mainCategoryAppliances[j][1] == i) {
               if (pretreatment == null || this.database.appliances[i].pretreatment == pretreatment) {
                  if (indexOf(this.database.appliances[i].getSurface(), results) == -1) {
                     results.push(this.database.appliances[i].getSurface());
                  }
               }
            }
         }
      }
   }
   return results;
}



/* ---------------------------------------- */



function Surface(id, name) {
   this.id           = id;
	this.name         = name;
   this.database     = null;
}



Surface.prototype.getAppliances = function(mainCategory) {
   var results = new Array();
   for (var i in this.database.appliances) {
      if (this.database.appliances[i].surface == this.id) {
         for (var j = 0; j < this.database.mainCategoryAppliances.length; j++) {
            if (this.database.mainCategoryAppliances[j][0] == mainCategory.id && this.database.mainCategoryAppliances[j][1] == i) {
               if (indexOf(this.database.appliances[i], results) == -1) {
                  results.push(this.database.appliances[i]);
               }
            }
         }
      }
   }
   return results;
}



Surface.prototype.getPretreatments = function(mainCategory) {
   var results = new Array();
   for (var i in this.database.appliances) {
      if (this.database.appliances[i].surface == this.id) {
         for (var j = 0; j < this.database.mainCategoryAppliances.length; j++) {
            if (this.database.mainCategoryAppliances[j][0] == mainCategory.id && this.database.mainCategoryAppliances[j][1] == i) {
               if (indexOf(this.database.appliances[i].getPretreatment(), results) == -1) {
                  results.push(this.database.appliances[i].getPretreatment());
               }
            }
         }
      }
   }
   return results;
}



Surface.prototype.getCorrosivityCategories = function(mainCategory, pretreatment) {
   var results = new Array();
   for (var i in this.database.appliances) {
      if (this.database.appliances[i].surface == this.id) {
         for (var j = 0; j < this.database.mainCategoryAppliances.length; j++) {
            if (this.database.mainCategoryAppliances[j][0] == mainCategory.id && this.database.mainCategoryAppliances[j][1] == i) {
               if (pretreatment == null || this.database.appliances[i].pretreatment == pretreatment) {
                  if (indexOf(this.database.appliances[i].getCorrosivityCategory(), results) == -1) {
                     results.push(this.database.appliances[i].getCorrosivityCategory());
                  }
               }
            }
         }
      }
   }
   return results;
}



/* ---------------------------------------- */



function Appliance(id, name, surface, corrosivityCategory, pretreatment, preparation) {
   this.id                  = id;
	this.name                = name;
	this.surface             = surface;
   this.corrosivityCategory = corrosivityCategory;
   this.pretreatment        = pretreatment;
	this.preparation         = preparation;
   this.database            = null;
}



Appliance.prototype.getSurface = function() {
   return this.database.surfaces[this.surface];
}



Appliance.prototype.getCorrosivityCategory = function() {
   return this.database.corrosivityCategories[this.corrosivityCategory];
}



Appliance.prototype.getPretreatment = function() {
   return this.database.pretreatments[this.pretreatment];
}



Appliance.prototype.getSystems = function() {
   var results = new Array();
   for (var i = 0; i < this.database.systemAppliances.length; i++) {
      if (this.database.systemAppliances[i][1] == this.id) {
         results.push(this.database.systems[this.database.systemAppliances[i][0]]);
      }
   }
   return results;
}



/* ---------------------------------------- */



function Pretreatment(id, name, description) {
   this.id                  = id;
   this.name                = name;
   this.description         = description;
   this.database            = null;
}



/* ---------------------------------------- */



function System(id, code, quantity, appliance, componentType, usage, mainCategory, lifetime) {
   this.id                  = id;
   this.code                = code;
   this.quantity            = quantity;
   this.appliance           = appliance;
   this.componentType       = componentType;
   this.usage               = usage;
   this.mainCategory        = mainCategory;
   this.lifetime            = lifetime;
   this.database            = null;
}



System.prototype.getComponentType = function() {
   return this.database.componentTypes[this.componentType];
}



System.prototype.getUsage = function() {
   return this.database.systemUsages[this.usage];
}



System.prototype.getAppliances = function() {
   var results = new Array();
   for (var i = 0; i < this.database.systemAppliances.length; i++) {
      if (this.database.systemAppliances[i][0] == this.id) {
         results.push(this.database.appliances[this.database.systemAppliances[i][1]]);
      }
   }
   return results;
}



System.prototype.getCorrosivityCategories = function() {
   var results = new Array();
   for (var i = 0; i < this.database.systemCorrosivityCategories.length; i++) {
      if (this.database.systemCorrosivityCategories[i][0] == this.id) {
         results.push([this.database.corrosivityCategories[this.database.systemCorrosivityCategories[i][1]],this.database.systemCorrosivityCategories[i][2]]);
      }
   }
   return results;
}



System.prototype.getSystemLayers = function() {
   var results = new Array();
   for (var i in this.database.systemLayers) {
      if (this.database.systemLayers[i].system == this.id) {
         results.push(this.database.systemLayers[i]);
      }
   }
   return results;
}



/* ---------------------------------------- */



function SystemLayer(id, system, product, index, thickness, repaint) {
   this.id                  = id;
	this.system              = system;
	this.product             = product;
	this.index               = index;
	this.thickness           = thickness;
	this.repaint             = repaint;
   this.database            = null;
}



SystemLayer.prototype.getProduct = function() {
   return this.database.products[this.product];
}



SystemLayer.prototype.getRepaint = function() {
   if (this.repaint) {
      return this.repaint;
   } else {
      return this.database.products[this.product];
   }
}



/* ---------------------------------------- */



function SystemUsage(id, category, name, description) {
   this.id                  = id;
	this.category            = category;
	this.name                = name;
	this.description         = description;
   this.database            = null;
}



SystemUsage.prototype.getSystems = function() {
   var results = new Array();
   for (var i in this.database.systems) {
      if (this.database.systems[i].usage == this.id) {
         results.push(this.database.systems[i]);
      }
   }
   return results;
}



/* ---------------------------------------- */



function SystemUsageCategory(id, name) {
   this.id           = id;
	this.name         = name;
   this.database     = null;
}



SystemUsageCategory.prototype.getSystemUsages = function() {
   var results = new Array();
   for (var i in this.database.systemUsages) {
      if (this.database.systemUsages[i].category == this.id) {
         results.push(this.database.systemUsages[i]);
      }
   }
   return results;
}



/* ---------------------------------------- */



function InfoPage(id, mainCategory, title, file, content) {
   this.id                  = id;
	this.mainCategory        = mainCategory;
	this.title               = title;
	this.file                = file;
	this.content             = content;
   this.database            = null;
}
