$(function () {
   var simu = new Simulation();
   if (simu.erreur === true) {
      return;
   }
});

function Simulation() {
   this.erreur = false;

   this.surface = document.getElementById('surface');
   this.gamme = document.getElementById('gamme');
   this.ville = document.getElementById('ville');
   this.puissance = document.getElementById('puissance');
   this.production = document.getElementById('production');
   this.gain = document.getElementById('gain');
   if (!this.surface || !this.gamme || !this.ville || !this.puissance || !this.production || !this.gain) {
      this.erreur = true;
      return;
   }
   this.puissances = {sanyo: 166.66, suntech: 133, solar: 101.26, amorphe: 73.11};
   this.villes = {
      montpellier: {sanyo: 1.31, suntech: 1.23, solar: 1.26, amorphe: 1.24},
      bordeaux: {sanyo: 1.18, suntech: 1.12, solar: 1.14, amorphe: 1.13},
      toulouse: {sanyo: 1.21, suntech: 1.14, solar: 1.18, amorphe: 1.16},
      rodez: {sanyo: 1.21, suntech: 1.14, solar: 1.18, amorphe: 1.16},
      perpignan: {sanyo: 1.3, suntech: 1.21, solar: 1.25, amorphe: 1.22},
      nimes: {sanyo: 1.39, suntech: 1.3, solar: 1.32, amorphe: 1.28},
      marsan: {sanyo: 1.18, suntech: 1.12, solar: 1.14, amorphe: 1.13}
   };
   this.rachatEDF = 0.58;
   this.nbDecimal = 2;
   this.maxSurface = 9999;

   var objet = this;
   this.surface.onkeyup = function () { objet.calculs(); };
   this.gamme.onchange = function () { objet.calculs(); };
   this.ville.onchange = function () { objet.calculs(); };
}
Simulation.prototype.calculs = function () {
   this.puissance.innerHTML = this.production.innerHTML = this.gain.innerHTML = 0;
   if (this.verifs() === false) {
      return;
   }
   var puissance = this.puissances[this.gamme.options[this.gamme.selectedIndex].value];
   var rendement = this.villes[this.ville.options[this.ville.selectedIndex].value][this.gamme.options[this.gamme.selectedIndex].value];

   puissance = this.surface.value * puissance;
   this.puissance.innerHTML = this.numberFormat(puissance);

   var production = puissance * rendement;
   this.production.innerHTML = this.numberFormat(production);

   var gain = production * this.rachatEDF;
   this.gain.innerHTML = this.numberFormat(gain);
};
Simulation.prototype.verifs = function () {
   if (this.gamme.options[this.gamme.selectedIndex].value == '' || this.ville.options[this.ville.selectedIndex].value == '') {
      return false;
   }
   document.getElementById('err_surface').innerHTML = '';
   if (!this.isInteger(this.surface.value)) {
      document.getElementById('err_surface').innerHTML = "Ceci n'est pas un entier";
      return false;
   }
   if (this.surface.value > this.maxSurface) {
      document.getElementById('err_surface').innerHTML = "Doit être inférieur à " + this.maxSurface;
      return false;
   }
   return true;
};
Simulation.prototype.isInteger = function (valeur) {
   for (var i = 0; i < valeur.length; i++) {
      var c = valeur.charCodeAt(i);
      if (c < 48 || c > 57) {
         return false;
      }
   }
   return true;
};
Simulation.prototype.isFloat = function (valeur) {
   var pointPresent = false;
   for (var i = 0; i < valeur.length; i++) {
      var c = valeur.charCodeAt(i);
      if ((c < 48 || c > 57) && c != 46) {
         return false;
      }
      if (c == 46) {
         if (pointPresent == true) return false;
         pointPresent = true;
      }
   }
   return true;
};
Simulation.prototype.numberFormat = function (valeur) {
   var d = Math.pow(10, this.nbDecimal);
   valeur = (Math.round(valeur * d) / d).toString();
   var pos = (valeur.indexOf(".") == -1) ? valeur.length - 1 : valeur.indexOf(".") - 1;
   for (var i = pos; i >= 0; i--) {
      if (pos != i && ((pos - i) % 3) == 0) {
         valeur = valeur.substring(0, i + 1) + " " + valeur.substring(i + 1);
      }
   }
   var pos = (valeur.indexOf(".") == -1) ? false : valeur.indexOf(".");
   if (pos !== false) {
      while (valeur.length - 1 - pos < this.nbDecimal) {
         valeur += "0";
      }
   }
   valeur = valeur.replace('.', ',');
   return valeur;
};
