

var undefined;

function Tarife (id) {
  this.id = undefined;
  this.setID(id);
  this._property;
  this._kWh = 0;
  this._result = 0;
  this._tax = 0;
}

Tarife.prototype._getResultStandard = function() {
  var result = 0;
  result = parseFloat(this._kWh * Tarife.properties['standard'].preis);
  result = parseFloat(result + Tarife.properties['standard'].grundgebuehr);
  return result;
}

Tarife.prototype._getResultOeko = function() {
  var result = 0;
  if (this._kWh <= 1500){
    result = parseFloat(this._kWh * Tarife.properties['öko'].preisBasic);
    result = parseFloat(result + Tarife.properties['öko'].grundgebuehrBasic);
  } else {
    result = parseFloat(this._kWh * Tarife.properties['öko'].preisFamily);
    result = parseFloat(result + Tarife.properties['öko'].grundgebuehrFamily);
  }
  return result;
}

Tarife.prototype.setID = function(str) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Tarife->setID:Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Tarife->setID:Argument ist nicht vom Typ String!");
  }
  this.id = str;
}

Tarife.prototype.setProperty = function(str) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Tarife->setProperty:Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Tarife->setProperty:Argument ist nicht vom Typ String!");
  }
  this._property = str;
}

Tarife.prototype.setResult = function(n) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Tarife->setResult:Falsche Anzahl von Argumenten!");
  }
  if (!Tarife.checkNumber(n)) {
    focus();
    throw new Error("Tarife->setResult:Argument ist nicht vom Typ Number!");
  }
  this._result = n;
}

Tarife.prototype.setkWh = function(n) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Tarife->setkWh:Falsche Anzahl von Argumenten!");
  }
  if (!Tarife.checkNumber(n)) {
    focus();
    throw new Error("Tarife->setkWh:Argument ist nicht vom Typ Number!");
  }
  this._kWh = n;
}

Tarife.prototype.getResult = function() {
  if (this._property == 'öko'){
    this.setResult(this._getResultOeko());
  } else {
    this.setResult(this._getResultStandard());
  }
  return this._result;
}

Tarife.prototype.getHTML = function() {
  var html = '';
  html += '<table cellspacing="0" cellpadding="0" class="table-formular-eintrag">\n';
  html += '<tr>\n';
  html += '<th>\n';
  html += 'betragen die Kosten';
  var tarif = ' mit <i><b>S</b>Kompakt</i>';
  if (this._property == 'öko'){
    tarif = ' mit <i><b>S</b>Komfort-Öko</i>';
  }
  html += tarif + '<br />\n';
  html += '</th>\n';
  html += '<td class="table-formular-eintrag-endergebnis">\n';
  html += '<span class="feld-endergebnis">'+ Tarife.formatCashFloat(this.getResult() + Tarife.getTax(this.getResult())) +'</span> EUR<br />\n';
  html += '</td>\n';
  html += '</tr>\n';
  html += '</table>\n';
  return html;
}

Tarife.getTax = function (n) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Tarife.getTax:Falsche Anzahl von Argumenten!");
  }
  if (!Tarife.checkNumber(n)) {
    focus();
    throw new Error("Tarife.getTax:Argument ist nicht vom Typ Number!");
  }
  var tax = n/100;
  tax = tax * Tarife.taxRate;
  return tax;
}

Tarife.getInstance = function (id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (Tarife._registerInstance[id])){
    focus();
    throw new Error("Es ist keine Tarife.Instance mit id=" + id + " registriert!");
  } 
  return Tarife._registerInstance[id];
}

// -------
// createInstance
// -------
Tarife.createInstance = function(id) {
  if (!arguments.length) {
    id = 'navigation' + Tarife._defaultID.length;
    Tarife._defaultID.push(1);
  }
  if (! (Tarife._registerInstance[id])){
    Tarife._registerInstance[id] = new Tarife(id);
  } 
  return Tarife.getInstance(id);
}

Tarife._defaultID = [];

Tarife._registerInstance = {};

Tarife.taxRate = 19;

Tarife.properties = {};
Tarife.properties['standard'] = {};
Tarife.properties['standard'].grundgebuehr = 87;
//Tarife.properties['standard'].preis = 0.1776;
Tarife.properties['standard'].preis = 0.1835;

Tarife.properties['öko'] = {};
Tarife.properties['öko'].grundgebuehrBasic = 60;
Tarife.properties['öko'].grundgebuehrFamily = 84;
Tarife.properties['öko'].preisBasic = 0.1826;
Tarife.properties['öko'].preisFamily = 0.1666;

Tarife.defaultKWh = {};
Tarife.defaultKWh['1'] = 1500;
Tarife.defaultKWh['2'] = 2500;
Tarife.defaultKWh['3'] = 3360;
Tarife.defaultKWh['4'] = 4000;
Tarife.defaultKWh['5'] = 6000;

Tarife.checkNumber = function(n){
  if (!arguments.length){
    return false;
  }
  if (n == undefined){
    return false;
  }
  n = n.toString();
  if (n == '0'){
    return n;
  }
  if (!n.length){
    return false;
  }
  n = n.replace(/,/g,'.');
  if (isNaN(n)){
    return false;
  }
  if (!isFinite(n)) {
    return false;
  }
  n = parseFloat(n);
  return n;
}

Tarife.formatCashFloat = function(n){
  cash_float = parseFloat(new String(n).replace(/\,/,'.'));
  cash_float = parseFloat(cash_float * 100);
  cash_float = parseFloat(Math.round(cash_float) / 100);
  cash_float = new String(cash_float);
  if (!cash_float.match(/\./)){
    cash_float = cash_float + '.00';
  }
  if (cash_float.split(/\./)[1].length == 1){
    cash_float = cash_float + '0';
  }
  cash_float = cash_float.replace(/\./,',');
  return cash_float;
}

Tarife.getHTMLkWH = function(n) {
  var html = '';
  html += '<table cellspacing="0" cellpadding="0" class="table-formular-eintrag">\n';
  html += '<tr>\n';
  html += '<th>\n';
  html += 'Bei einem Jahresverbrauch von<br />\n';
  html += '</th>\n';
  html += '<td>\n';
  html += '<span class="feld-jahresverbrauch">' + n + '</span> kWh<br />\n';
  html += '</td>\n';
  html += '</tr>\n';
  html += '</table>\n';
  return html;
}

Tarife.getHTMLDifference = function(n) {
  var html = '';
  html += '<table cellspacing="0" cellpadding="0" class="table-formular-eintrag">\n';
  html += '<tr>\n';
  html += '<th style="color: #2B5FC2">\n';
  html += 'Sie sparen mit <i><b>S</b>Komfort-Öko</i><br />\n';
  html += '</th>\n';
  html += '<td style="color: #2B5FC2">\n';
  html += '<span class="feld-jahresverbrauch">' + n + '</span> EUR<br />\n';
  html += '</td>\n';
  html += '</tr>\n';
  html += '</table>\n';
  return html;
}

Tarife.createTarife = function () {
  var kWh_index = document.getElementById('defaultkWh').options[document.getElementById('defaultkWh').selectedIndex].value + '';
  var kWh = Tarife.defaultKWh[kWh_index];
  if (document.getElementById('kWh').value && document.getElementById('kWh').value > 0){
    kWh = document.getElementById('kWh').value;
  }
  
  if (! Tarife.checkNumber(kWh)){
    alert('Sie müssen eine gültige Zahl eingeben');
    return false;
  }
  
  if (kWh <= 200){
    alert('Ihr Jahresverbrauch ist zu niedrig, bitten geben Sie einen höheren Verbrauch an.');
    return false;
  }
  
  var resultStandard = Tarife.createInstance('standard');
  resultStandard.setProperty('standard');
  resultStandard.setkWh(kWh);
  document.getElementById('_result_standard').innerHTML = resultStandard.getHTML();
  
  var resultOeko = Tarife.createInstance('öko');
  resultOeko.setProperty('öko');
  resultOeko.setkWh(kWh);
  document.getElementById('_result_oeko').innerHTML = resultOeko.getHTML();
  
  var _diff = Tarife.formatCashFloat((resultStandard.getResult() + Tarife.getTax(resultStandard.getResult())) - (resultOeko.getResult() + Tarife.getTax(resultOeko.getResult())));
  document.getElementById('_result_difference').innerHTML = Tarife.getHTMLDifference(_diff);
  
  
  document.getElementById('_result_kWh').innerHTML = Tarife.getHTMLkWH(kWh);
  
  document.getElementById('ergebnis-form').style.display = 'block';
  document.getElementById('eingabe-form').style.display = 'none';
}

Tarife.createTarifeBack = function () {
  document.getElementById('ergebnis-form').style.display = 'none';
  document.getElementById('eingabe-form').style.display = 'block';
}
