// portocalc.js
// berechnet Grundporto fuer Portoberechnung
//
var autohinweis = false;

function reCalc() {
  klasse = 1;
  format = document.porto.format.options[document.porto.format.selectedIndex].value;
  dicke =  document.porto.dicke.options[document.porto.dicke.selectedIndex].value;
  gewicht = document.porto.gewicht.value;

  // Klassifizierung
  if (format > 1 || dicke > 1 || gewicht > 20) { klasse = 2; }
  if (format > 1 || dicke > 2 || gewicht > 50) { klasse = 3; }
  if (dicke > 3) { klasse = 4 }
  
  for (i=1; i<5; i++) {
    action = "document.porto.p"+i+".checked = ";
    if (i == klasse) { 
      action += "true;";
    } else {
      action += "false;";
    }
    eval(action);
  }
  // Einzelporto berechnen
  switch (klasse) {
    case 1: { // Standard
      document.porto.ip.value = "0.25";
      document.porto.ib.value = "0.35";
      document.porto.br.value = "0.55";
      break;
    }
    case 2: { // Kompakt
      if (gewicht <= 20) {
        document.porto.ip.value = "0.28";
      } else {
        document.porto.ip.value = (Math.round((gewicht - 20) * 0.352 + 28))/100;
      }
      document.porto.ib.value = "0.75";
      document.porto.br.value = "0.90";
      break;
    }
    case 3: { // Gross
      if (gewicht <= 20) {
        document.porto.ip.value = "0.36";
      } else if (gewicht <= 100) {
        document.porto.ip.value = (Math.round((gewicht - 20) * 0.352 + 36))/100;
      } else {
        document.porto.ip.value = (Math.round((gewicht - 100) * 0.046 + 64 ))/100;
      }
      document.porto.ib.value = "1.35";
      document.porto.br.value = "1.45";
      break;
    }
    case 4: { // Maxi
      if (gewicht <= 20) {
        document.porto.ip.value = "0.73";
      } else if (gewicht <= 100) {
        document.porto.ip.value = (Math.round((gewicht - 20) * 0.352 + 73))/100;
      } else {
        document.porto.ip.value = (Math.round((gewicht - 100) * 0.046 + 101 ))/100;
      }
      document.porto.ib.value = "1.80";
      document.porto.br.value = "2.20";
      break;
    }
  }
}

function startup() {
  if (gewicht > 0) {
    document.forms['porto'].gewicht.value = gewicht;
  }
  if (format == 'C6') { document.forms['porto'].format.selectedIndex = 0;}
  if (format == 'DINlang') { document.forms['porto'].format.selectedIndex = 1;}
  if (format == 'C5') { document.forms['porto'].format.selectedIndex = 2;}
  if (format == 'C4') { document.forms['porto'].format.selectedIndex = 3;}
  reCalc();
}

function automatisch() {
  if (!autohinweis) {
    alert('Dieses Feld wird automatisch berechnet');
    autohinweis = true;
  }         
  document.forms['porto'].start.focus();
}
//  End -->

