/* NEWSLETTER */
//ä Ä ü Ü ö Ö
//("%E4 %C4 %FC %DC %F6 %D6"));
var uml_a = "%E4";
var uml_A = "%C4";
var uml_u = "%FC";
var uml_U = "%DC";
var uml_o = "%F6";
var uml_O = "%D6";
var forgotProc = false;
var DEBUG = false;
var isLive = true;
var maxLengthPWD = 8;
if((""+location.href).indexOf("stelling")!=-1)isLive=false;

//  (""+location.href).indexOf("//www.fischerverlage.de")!=-1
//  ||
//  (""+location.href).indexOf("//www.fischergeneration.de")!=-1
//  ||
//  (""+location.href).indexOf("//www.fischerschatzinsel.de")!=-1
//  ||
//  (""+location.href).indexOf("//www.hochschule.fischerverlage.de")!=-1
//  )

function checkAnmelde(form, isLogin)
{
  var email = checkEmail(form);
  var isReminder = forgotProc;
  if(email==false){
    alert(unescape("Bitte geben Sie eine g%FCltige Email-Adresse an!"));
    return false;
  }
  return isReminder || checkPasswordLength(form, isLogin);
}

function checkPassword(form)
{
  var ret = false;
  var msg = "";
  /*alert(form.form_get_password);
  alert(form.form_get_password.value);
  alert(form.form_get_password2);
  alert(form.form_get_password2.value);*/
  //alert(form.form_get_password.value.length);
  //return false;
  var val1 = form.form_get_password;
  var val2 = form.form_get_password2;
  var PWD1Empty = val1.value == "";
  var PWD2Empty = val2.value == "";

  if( PWD1Empty  || PWD2Empty)
  {
    msg += unescape("-->Das "+(PWD1Empty ? "1.":"2.")+" Passwortfeld ist leer. Bitte machen Sie eine Eingabe\n");
    if(PWD1Empty) val1.focus();
    else if(PWD2Empty) val2.focus();
    ret = false;
  }else
  {
    ret = true;
  }
  //if(!ret){alert(msg); return ret;}
  //################################################

  var PWD1Short = val1.value.length  < maxLengthPWD;
  var PWD2Short = val2.value.length  < maxLengthPWD;

  if( PWD1Short  || PWD2Short)
  {
    msg += unescape("-->Das "+(PWD1Short ? "1.":"2.")+" Passwortfeld ist zu kurz (mind. "+maxLengthPWD+" Zeichen)\n");
    if(PWD1Short) val1.focus();
    else if(PWD2Short) val2.focus();
    ret = false;
  }else
  {
    ret = true;
  }
  if(!ret){alert(msg); return ret;}
  //################################################

  if( val1.value  != val2.value)
  {
    msg += unescape(unescape("Die Passw"+uml_o+"rter stimmen nicht "+uml_u+"berein!\n"));
    val1.focus();
    ret = false;
  }else
  {
    ret = true;
  }


  if(!ret){alert(msg);}
  return ret;
}

function checkPasswordLength(form, isLogin)
{
  var ret = false;
  var msg = "";
  var val1 = form.form_get_password;
  var PWD1Empty = val1.value  == "";

  if( PWD1Empty)
  {
    msg += unescape("-->Das Passwortfeld ist leer. Bitte machen Sie eine Eingabe\n");
    val1.focus();
    alert(msg);
    return false;
  }else
  {
    ret = true;
  }
  //if(!ret){alert(msg); return ret;}
  //################################################

  var PWD1Short = val1 && val1.value.length  < maxLengthPWD;
  if(PWD1Short && !isLogin)
  {
    msg += unescape("-->Das Passwortfeld ist zu kurz (mind. "+maxLengthPWD+" Zeichen)\n");
    val1.focus();
    alert(msg);
    return false;
  }else
  {
    ret = true;
  }
  if(!ret){alert(msg); return ret;}
  //################################################
  if(!ret){alert(msg);}
  return ret;
}

function checkEmail(form)
{
  if(form.form_get_email && form.form_get_email.value.indexOf("@")!= -1)
  {
    return true;
  }
  else{
    form.form_get_email.focus();
    return false;
  }
}

function confirmUnsub()
{
    Check = confirm("Wollen Sie sich wirklich vom fischerverlag.de Newsletter trennen ?");
    if(Check == false) return false;
    else return true;
}

function setForgot()
{
  forgotProc = true;
}

function submitForm(theForm, command)
{
  if(command=='login')
  {
    if(!checkAnmelde(theForm, true)) return false;
    theForm.form_but_login.value = 'Login';
    theForm.submit();
  }
  else if(command=='forgot')
  {
    setForgot();
    if(!checkAnmelde(theForm, false)) return false;
    theForm.form_but_remind.value = 'Passwort vergessen';
  }
  else if(command=='subscribe' || command=='change')
  {
    //submitForm(document.persdatenform, 'subscribe')
    //Spezielfall PLZ:
    //alert(theForm.subscriber_groups(0));
    //var indexer = ""+theForm.form_get_ex_fischer_PLZ_5.value
    //indexer = indexer.substring(0,1);
    //alert(indexer);
    //return false;
    if(!checkAnmelde(theForm, false)) return false;
    if(!checkPassword(theForm)) return false;

  }
  else if(command=='logout')
  {
    //nix
    if(DEBUG)alert("Logout");
  }
  else if(command=='unsubscribe')
  {
    if(DEBUG)alert("Unsub");
    var sure = confirmUnsub();
    if(!sure) return false;
    if(DEBUG)alert("Ich will!");
  }else
    alert("Unknown action!");
  theForm.submit();
}
var currentPLZ = -1;
var selectPLZNothingIndex = 0;
var selectPLZAllIndex     = 1;
/**
* Ist Main gechecked ?
*/
function checkSelectPLZ(selecter, form)
{
  DEBUG = false;
  nodelist = form.elements['subscriber_groups[]']
  currentPLZ = selecter.value;
  isOff = selecter.selectedIndex==0;
  if(DEBUG)alert("selecter.selectedIndex: "+selecter.selectedIndex);
  plzIndex = isLive? 7:5;
  for( i=0;i<nodelist.length;i++)
  {
    if(nodelist[i].value==plzIndex){ nodelist[i].checked=isOff ? false:true; return;}
  }
}
//Default bei EIN nicht -1 sondern 19 (Alle Autoren)
function checkSelectPLZByCheckbox(checker, form)
{
  DEBUG = false;
  nodelist = form.elements['subscriber_groups[]']
  isOff = checker.checked==false;
  isOn  = checker.checked==true;
  //DEBUG = isOff;
  plzIndex = isLive? 7:5;
  //if(DEBUG)alert(plzIndex);
  if(DEBUG)alert("isOn: "+isOn);
  isNotAuthorsGroup = checker.value!=plzIndex;
  //alert("return ?: "+isNotAuthorsGroup);
  if(isNotAuthorsGroup) return;//ist nicht das Autoren feld
  //alert("nodelist.length: "+nodelist.length);
  if(DEBUG)alert("currentPLZ: "+currentPLZ);
  for( i=0;i<nodelist.length;i++)
  {
    if(DEBUG)alert("Select-Option-Wert: "+nodelist[i].value+" currentPLZ: "+currentPLZ);
    /*Stimmt letztes PLZ-Gebiet */
    if(nodelist[i].value==currentPLZ)
    {//hier biegt nur das SELECT AB!
      if(DEBUG)alert("Index "+nodelist[i].selectedIndex);
      nextSet = isOn ? selectPLZAllIndex:selectPLZNothingIndex;
      if(DEBUG)alert("Setting index: "+nextSet);
      nodelist[i].selectedIndex = nextSet;
      //alert("nodelist[i] :"+nodelist[i].value);
      currentPLZ = nodelist[i].value;
      //if(DEBUG)      alert("currentPLZ :"+currentPLZ);
      return;
    }
  }
}

/* / NEWSLETTER */

