function soundnessRequiredTypeChanged(value) {
  if(value == 0) {
    hideE('soundness_types');
  } else {
    showE('soundness_types');
  }
}

function hideE(id) {
  // on ie
  if(navigator.appName == 'Microsoft Internet Explorer') {
    getE(id).style.display = 'none';
  } else {
    getE(id).style.visibility = 'collapse';
  }
}

function showE(id) {
  // on ie
  if(navigator.appName == 'Microsoft Internet Explorer') {
    getE(id).style.display = 'block';   
  } else {
    getE(id).style.visibility = 'visible';   
  } 
}

function getE(id) {
  return document.getElementById(id);
}

function monthlyRateChanged() {
  var value = getE('submission_monthly_rate_currency').value;
  getE('submission_loan_amount_currency').value = '0,00';
}

function loanAmountChanged() {
  var value = getE('submission_loan_amount_currency').value;
  getE('submission_monthly_rate_currency').value = '0,00';
}

function stringToFloat(value) {
  return value.replace(/,/,'.') * 1;
}

function eToFloat(id) {
  return stringToFloat(getE(id).value);
}

function formatNumber(value) {
  var splitted = (value + '').split('.');
  if(splitted.length == 1) {
    return value + ',00'
  } else {
    if(splitted[1].length == 1) {
      return splitted[0] + ',' + splitted[1] + '0';
    } else if (splitted[1].length == 2) {
      return splitted[0] + ',' + splitted[1];
    } else {
      return splitted[0] + ',' + splitted[1].slice(0,2);
    }
  }
}

function showSoundnessTypes() {
  if(getE('submission_soundness_required_type_1').checked == 'checked') {
    showE('soundness_types');
    showE('soundness_types_blank');
  }
  else {
    hideE('soundness_types');
    hideE('soundness_types_blank');
  }
}

function disableLoanAmountCarLoan() {
  if(document.location.href.indexOf('car_loan') >= 0) {
    getE('submission_loan_amount_currency').disabled = true;
  } 
}

function reCalculate() {
  if(getE('submission_downpayment_type').value == '0') {
    //var loanAmount = getE('submission_loan_amount_currency').value;
    var price = eToFloat('submission_price_currency');
    var downpayment = eToFloat('submission_downpayment_currency');
    getE('submission_loan_amount_currency').value = formatNumber(price - downpayment);
  } else {
    var price = eToFloat('submission_price_currency');
    var percentage = eToFloat('submission_downpayment_currency');
    if(percentage > 100) {
      alert('Prozentsatz darf 100% nicht überschreiten');
      percentage = 0,
      getE('submission_downpayment_currency').value = formatNumber(percentage);
    }
    getE('submission_loan_amount_currency').value = formatNumber(price * (percentage / 100));    
  }  
}

function priceChanged() {
  reCalculate();
}

function downpaymentChanged() {
  reCalculate();
}

function downpaymentTypeChanged() {
  reCalculate();
}