var rlsCalciumInputChanged = function(event){
   var thisElement = Event.element(event);
   updateLineTotal(thisElement);
   hideTotalShowButton();
};

var updateLineTotal = function(inputElement) {
   var thisValueField = $$('.formbox_calR .calR4.' + inputElement.name)[0];
   if(thisValueField) {
     var timesValue = thisValueField.previousSiblings()[0].innerHTML.replace('x','');
     if($F(inputElement)) {
       inputElement.value = parseFloat($F(inputElement));
     }
     var inputValue = $F(inputElement) || 0;
     thisValueField.update(inputValue * timesValue);
   }
}

var specialKeys = $A([ Event.KEY_BACKSPACE, Event.KEY_TAB, Event.KEY_RETURN,
  Event.KEY_ESC, Event.KEY_LEFT, Event.KEY_UP, Event.KEY_RIGHT, Event.KEY_DOWN,
  Event.KEY_DELETE, Event.KEY_HOME, Event.KEY_END, Event.KEY_PAGEUP,
  Event.KEY_PAGEDOWN ]);

var rlsDigitsOnly = function(event){
     var code = event.keyCode || event.which;
     var keyChar = String.fromCharCode(code);
     var numcheck = /\d/;
     if (!event.ctrlKey && !event.altKey && (specialKeys.indexOf(event.keyCode) < 0)
        && !numcheck.test(keyChar)) {
       // filter key stroke
       Event.stop(event);
     }
   };

var computeShowResult = function(event) {
  var newValue = 0;
  $$('.formbox_calR .calR4').each(function(lineTotal) {
    var fieldValueStr = lineTotal.innerHTML;
    var fieldValue = parseFloat(fieldValueStr);
    if (!isNaN(fieldValue)) {
      newValue = newValue + fieldValue;
    }
  });
  $$('.calR_total .totalCal')[0].update(newValue);

  var activeRadio = $(getRadio_calR());
  var genugId = 'rls_calciumGenug';
  var zuwenigZusatTextId = null;
  if(activeRadio.descendantOf($('schwangerRadio'))) {
    genugId = 'rls_calciumGenugSchwanger';
    zuwenigZusatTextId = 'rls_calciumZuWenigSchwanger';
  }
  if (newValue >= activeRadio.getValue()) {
    $(genugId).show();
    $('rls_calciumGenugText').show();
  } else if (newValue < activeRadio.getValue()) {
    $('rls_calciumZuWenig').show();
    $('rls_calciumZuWenigText').show();
    if (zuwenigZusatTextId) {
	    $(zuwenigZusatTextId).show();
    }
  }
  $('rls_calciumRechnerResultBox').show();
  showTotalHideButton();
  Event.stop(event);
}

var showTotalHideButton = function() {
    var totalCalButton = $('totalCal_button');
    if (totalCalButton.visible()) {
      totalCalButton.hide();
      $$('.calR_total .totalCal')[0].show();
      $$('.calR_total .totalCal_mg')[0].show();
    }
}

var hideTotalShowButton = function() {
    var totalCalButton = $('totalCal_button');
    if (!totalCalButton.visible()) {
	    $$('.calR_total .totalCal')[0].hide();
	    $$('.calR_total .totalCal_mg')[0].hide();
		  $('rls_calciumRechnerResultBox').hide();
		  $$('.resSnippet').each(function(elem) {
		    $(elem).hide();
		  });
	    totalCalButton.show();
	  }
}

var getRadio_calR = function() {
  var checkedElement = null;
  $A($('formCalR').radio_calR).each(function(radioCalR) {
    if (radioCalR.checked) {
      checkedElement = radioCalR;
    }
  });
  return checkedElement;
}

var activateCalculateButton = function() {
  hideTotalShowButton();
  $('totalCal_button').enable();
}

// onload hide "Needs JS" Notice, add all Listeners and show form
Event.observe(window, 'load', function() {
  if(!$('totalCal_button')) {
	  $$('.calR_total')[0].insert(new Element('input', {
	    id: 'totalCal_button',
	    className: 'totalCal',
	    value: 'berechnen',
	    type: 'submit'
	    }));
  }
  $('totalCal_button').hide().disable();

  Event.observe('formCalR', 'submit', computeShowResult);

  // hide "Needs JS" Notice
  $('needsJSNoticeMessage').hide();
  hideTotalShowButton();
  
  $$('.formbox_calR .checkbox_calR input').each(function(radioCalR) {
    $(radioCalR).observe('click', activateCalculateButton);
  });
  $$('.formbox_calR .calR1 input.input_data_calR').each(function(inputfield) {
      $(inputfield).observe('change', rlsCalciumInputChanged);
      $(inputfield).observe('keypress', rlsDigitsOnly);
      updateLineTotal(inputfield);
  });

  // show form
  $('rls_calciumRechnerFormBox').style.display = 'block';
});

