
var char_limit = 1000;

function do_set(){

  if (document.forms[''+form_name].comment.value.length > char_limit){
    if (confirm("Ihre Anregungen oder Anmerkungen enthalten mehr als 1000 Zeichen.\nSoll Ihr Text automatisch gekürzt werden?")){
      document.forms[''+form_name].comment.value = document.forms[''+form_name].comment.value.substr(0,char_limit);
    }
  }
  else {
    if (check_form_values()){
      document.forms[''+form_name].submit();
    }
  }
}



function check_question_radio(f_question){
  var checked = false;
  for (i=0;i<document.forms[''+form_name].elements[''+f_question].length;i++){
    if (document.forms[''+form_name].elements[''+f_question][i].checked){
      checked = true;
    }
  }
  // if (!checked){ alert(f_question); }
  return checked;
}




function check_form_values(){
  
  var l_ok = true;
  var msg = "";
  var checked = false;

  // Select-Fragen prüfen
  if (document.forms[''+form_name].f_1_1.value == "" ||
      document.forms[''+form_name].f_2.value == "" ||
      document.forms[''+form_name].f_8.value == "" ||
      document.forms[''+form_name].p_edu.value == ""
    ){
    l_ok = false;
  }

  // Fragen mit Radiobuttons
  if (l_ok) { l_ok = check_question_radio("f_1_2"); }
  if (l_ok) { l_ok = check_question_radio("f_1_3"); }
  if (l_ok) { l_ok = check_question_radio("f_3_1"); }
  if (l_ok) { l_ok = check_question_radio("f_3_2"); }
  if (l_ok) { l_ok = check_question_radio("f_3_3"); }
  if (l_ok) { l_ok = check_question_radio("f_3_4"); }
  if (l_ok) { l_ok = check_question_radio("f_3_5"); }
  if (l_ok) { l_ok = check_question_radio("f_3_6"); }
  if (l_ok) { l_ok = check_question_radio("f_4_1"); }
  if (l_ok) { l_ok = check_question_radio("f_4_2"); }
  if (l_ok) { l_ok = check_question_radio("f_4_3"); }
  if (l_ok) { l_ok = check_question_radio("f_4_4"); }
  if (l_ok) { l_ok = check_question_radio("f_4_5"); }
  if (l_ok) { l_ok = check_question_radio("f_4_6"); }
  if (l_ok) { l_ok = check_question_radio("f_4_7"); }
  if (l_ok) { l_ok = check_question_radio("f_4_8"); }
  if (l_ok) { l_ok = check_question_radio("f_5"); }
  if (l_ok) { l_ok = check_question_radio("f_6"); }
  if (l_ok) { l_ok = check_question_radio("f_7"); }
  if (l_ok) { l_ok = check_question_radio("p_gender"); }

  if (!l_ok){
    msg = msg + "Bitte beantworten Sie alle Fragen.\n\n";
  }


  // Geburtsjahr prüfen
  if (l_ok){
    if (isNaN(document.forms[''+form_name].p_ybirth.value) || document.forms[''+form_name].p_ybirth.value == ""){
      l_ok = false;
      msg = msg + "Bitte geben Sie eine gültige Jahreszahl für Ihr Geburtsjahr an.\n";
    }
  }

  // PLZ prüfen
  if (l_ok){
    if (isNaN(document.forms[''+form_name].p_plz.value) || document.forms[''+form_name].p_plz.value == ""){
      l_ok = false;
      msg = msg + "Bitte geben Sie die ersten beiden Ziffern Ihrer Postleitzahl an.\n";
    }
  }

  // E-Mail prüfen bei Teilname am Gewinnspiel
  if (l_ok){
    if (document.forms[''+form_name].lottery_confirm.checked){
      var l_email = document.forms[''+form_name].lottery_email.value;
      var has_at = false;
      var has_dot = false;
      for (i=0;i<l_email.length;i++){
        if (i > 0 && l_email.charAt(i) == "@"){
          has_at = true;
        }
        if (i > 2 && has_at && l_email.charAt(i) == "."){
          has_dot = true;
        }
      }
      if (!(has_at && has_dot) || l_email.length < 6){
        l_ok = false;
        msg = msg + "Bitte überprüfen Sie Ihre angegebene E-Mail-Adresse.";
      }
    }
  }

  if (!l_ok){
    alert(msg);
  }


  return l_ok;

}

