$(document).ready(function(){

  $("#txtLoginName, #txtPassLogin").keypress(function (e) {
    
    if (e.which == 13) {
        CheckLogin();
    }
  });

  $("#txtPassLogin").focus(function() {
      if ($(this).val() == "Passwort") {
          $(this).val("");
      }
  });
  
  $("#txtPassLogin").blur(function() {
      if ($(this).val() == "") {
          $(this).val("Passwort");
      }
  });
  
  $("#txtLoginName").focus(function() {
      if ($(this).val() == "E-Mail") {
          $(this).val("");
      }
  });

  $("#txtLoginName").blur(function() {
      if ($(this).val() == "") {
          $(this).val("E-Mail");
      }
  });

  $("#chBoxLoginExpire").next().click( function() {
      var ischecked = $(this).prev().attr("checked");
      if (ischecked) $(this).prev().attr("checked", "checked");
      else $(this).prev().attr("checked", "");
  });
  
  $("#chBoxLoginExpire").next().hover( function() {
      $(this).css("text-decoration", "underline");    
  }, function() {
      $(this).css("text-decoration", "none");    
  });

  $("#btnAnmeldeButton").click( function () {
      CheckLogin();
  });
});


/*
function login() {

  var x=0;
  var y=0;
  
  
  ShowBgTrans();
  
  
  var login = document.getElementById("login");
  //$("#login").css({ "position":"absolute" });
  
  //login.style.display = "block";
  //login.style.width = 400;
  //login.style.height = 300;
  
  
  $("#carouselDiv").hide();
  login.style.zIndex= 1;
  
  var bgtrans = $("#frame_top_left").offset();
  var loginOffset = $("#login").offset();
  var zeit = new Date();
  var ms = zeit.getMilliseconds();  
  
  //alert(( ($("#login").width() / 2 )));
  login.style.left = (bgtrans.left + ( ($("#frame_top_left").width() - $("#login").width()) / 2 ))+"px";
  login.style.top = (bgtrans.top + 10)+"px";
  
//  alert("drin");
  $("#LoginCodePic").html("<img src='http://" + document.domain + "/kbl/php/inc/create_img.php?ms="+ms+"' />");
  $("#login").fadeIn();

}
 */


function ReloadControlImg () {
  var zeit = new Date();
  var ms = zeit.getMilliseconds();  
  $("#LoginCodePic").attr("src","http://" + document.domain + "/kbl/php/inc/create_img.php?ms="+ms);
}



function CloseLogin() {

//var login_element = document.getElementById("login_element");
//login_element.style.display = "none";

var login = document.getElementById("login");
login.style.display = "none";
$("#carouselDiv").show();

var bgtrans = document.getElementById("bg_trans");
bgtrans.style.display = "none";
bgtrans.style.width = 0;
bgtrans.style.height = 0;

}


function SendAnmeldung() {

  var email = String(document.getElementById("txtEmailAnmeldung").value);
  var code = document.getElementById("txtCodeAnmeldung").value;
  var error = "";
  $(".Infobox").text("");
  $("#EMail, #KontrollCode").css("backgroundColor", "white");
  
  
  if (code == "") {
        error = "Sie haben keinen Code eingegeben";
        $("#KontrollCode").css("backgroundColor", "red");
        $("#KontrollCode .Infobox").text("keine Kontrollziffer eingegeben");
  } 
  
  if (email == "") {
        error = "Sie haben keine EMail Adresse eingegeben";
        $("#EMail").css("backgroundColor", "red");
        $("#EMail .Infobox").text("Bitte EMail-Adresse eingeben");
  }
  
  if (!error) {
        
      var content = $("#AnmeldeForm").html();
      $("#AnmeldeForm").html("<img src='./pics/icons/ajax-loader.gif' />");
      
      $.post("http://" + document.domain + "/kbl/php/inc/anmeldung.php", { txtEmailAnmeldung: email, txtCodeAnmeldung: code }, 
        function(msg, textStatus){
          // registrierte EMail-Addy war erfolgreich
          //alert("msg: "+msg);
          //alert(textStatus);
          if (msg != "0") {
            $("#AnmeldeForm").html(content);
            $("#txtEmailAnmeldung").val(email);
          } 
          
          switch (msg) {
            case "0":
                
              $("#AnmeldeForm").html("<span style='font-size:12px; font-weight:bold;'>Dir wurde ein Registrierungslink zugesendet!</span>");
                
            break;
            
            case "1":
              //alert("ungültige Email-Adresse");
              
              $("#EMail").css("backgroundColor", "red");
              $("#EMail .Infobox").text("Ungültige EMail-Adresse");
              $("#txtCodeAnmeldung").val("");
            break;
            
            case "2":
              //Check = confirm("EMail bereits vorhanden!\nHabst Du dein Passwort vergessen?");
              $("#EMail").css("backgroundColor", "red");
              $("#EMail .Infobox").html("EMail bereits vorhanden!<br />Falls Du Dein Passwort vergessen hast bitte <input onClick='javascript:ResetPassword()' type='button' id='btnResetPass' value='hier klicken' />");
              
            break;

            case "3":
              //alert("Kontrollcode ist ungültig");
              $("#KontrollCode").css("backgroundColor", "red");
              $("#KontrollCode .Infobox").text("falsche Kontrollziffer");
            break;        

            case "4":
              alert("Beim versenden des Registrierung-Links ist ein allg. Fehler aufgetreten");
            break;        

          }

      });
  
  }

}




function ResetPassword () {
  
      var email = $("#txtEmailAnmeldung").val();
      $.post("http://" + document.domain + "/kbl/php/inc/send.reset.password.link.php", { txtEmailAnmeldung: email }, function(msg){
          switch (msg) {
          
              case "0":
              $("#EMail").css("backgroundColor", "white");
              $("#EMail .Infobox").css({"border": "1px solid green",  "color":"#333333"}).fadeIn();
              $("#EMail .Infobox").text("Du hast einen Link erhalten mit dem Du Dein Passwort zurücksetzen kannst!");
              break;
              
              case "5":
              $("#EMail").css("backgroundColor", "red");
              $("#EMail .Infobox").css("border", "1px solid #ccc").fadeIn();
              $("#EMail .Infobox").text("E-Mail Adresse existiert nicht in unserem System!");
              break;              
              
              default:
              $("#EMail").css("backgroundColor", "red");
              $("#EMail .Infobox").css("border", "1px solid #ccc").fadeIn();
              $("#EMail .Infobox").text("E-Mail Adresse ist ungültig!");
              break;
            //alert("Beim versenden des Passwort-Resets ist ein Fehler unterlaufen!");
          }
      });
  

}





function CheckLogin() {


      var pass = document.getElementById("txtPassLogin").value;
      var name = document.getElementById("txtLoginName").value;
      var expire = $("#chBoxLoginExpire").attr("checked");
 
      if (expire == false) {
          expire = "";
      }
      if (pass && name) {
          
          $.get("http://" + document.domain + "/kbl/php/inc/login.php?txtPassLogin="+pass+"&txtLoginName="+name+"&boolExpire="+expire+"&btnSubmitLogin=True", function(text){

            if (text == "True") {
                window.location.href = "http://" + document.domain + "/kbl/index.php?Section=Konto";
            }
            else {
                $("#LoginAlert").html("Dein Passwort oder E-Mail ist falsch!");
                $("#LoginAlert").slideDown(300);
                setTimeout('$("#LoginAlert").slideUp(200);', 3500);
            }
            
          });
      }
      else {
                $("#LoginAlert").html("Dein Passwort oder Benutzername ist falsch!");
                $("#LoginAlert").slideDown(1000);
                setTimeout('$("#LoginAlert").slideUp(1000);', 3500);
                
            }
}


