// -------------
// parsing
// -------------

var my_fvb = getElemById ("my-fvb");

function show_hide_myfvb_nav(show) {
    // blende alle Menü-Punkte ein
    for (var i in my_fvb.childNodes) {
      var list = my_fvb.childNodes[i];
      if (list && list.nodeName && list.nodeName.toLowerCase() == "ul") {
        for (var j in list.childNodes) {
          var cur_li = list.childNodes[j];
          if (!cur_li || !cur_li.nodeName || cur_li.nodeName.toLowerCase() != "li") continue;
          if (!cur_li.getAttribute("id")) {
            cur_li.style.display = show ? "block" : "none";
          }
        }
        break;
      }
    }
}

function show_myfvb_nav() {
    show_hide_myfvb_nav(true);
}
function hide_myfvb_nav() {
    show_hide_myfvb_nav(false);
}

function add_logout_button() {
  var newLI = document.createElement("li");
  newLI.setAttribute("class", "logout");
  newLI.innerHTML = "<a href='#' onclick='logout(); return false;'>Abmelden</a>";
  var uls = document.getElementsByTagName("ul");
  for (var i in uls) {
    console.log(uls[i]);
    if (!uls[i] || !uls[i].getAttribute) continue;
    var ul_cl = uls[i].getAttribute("class");
    if (ul_cl && ul_cl.toLowerCase() == "second-level") {
      uls[i].appendChild(newLI);
    }
  }
}

function analyseName (name) {
    console.log ("got name: " + name);

    var logoutField = getElemById ("myfvb_logout");
    var greeting = getElemById ("myfvb_greeting");
    var loginbox = getElemById ("loginbox");

    // on member page: don't display the whole my-fvb div
    if (window.location.pathname.indexOf("/meine_fvb/mitgliederbereich") == 0) {
        console.log (getElemById("my-fvb"));
        my_fvb.style.display = "none";

        if (name) {
            // add logout button to navigation
            add_logout_button();
        }
    } else {
        if (name) {
            greeting.innerHTML = "Guten Tag, " + name + "!";
            greeting.style.display = "block";
            logoutField.style.display = "block";

            loginbox.style.display = "none";

            show_myfvb_nav();
        }
        else {
            greeting.style.innerHTML = "";
            greeting.style.display = "none";
            logoutField.style.display = "none";

            loginbox.style.display = "block";

            hide_myfvb_nav();
        }
    }
}


function parseGetNameResponse () {
    analyseName (this.response);
}

function parseGetLastSearchResponse () {
    // do something ...
    console.log ("got last search:" + this.response);
    if (this.response) {
        var qsearch = getElemById("lastsearch");
        qsearch.href = this.response;
    }
}

function parseLoginResponse () {
    var resp = eval( '(' + this.response + ')' );
    switch (resp.status) {
        case "error" : 
            alert (resp.response); 
            break;
        case "success": 
            //analyseName(resp.response); 
            //getElemById ("loginbox").style.display = "none";
            location.reload(true);
            break;
        case "servererror": 
            alert ("Ein interner Server-Fehler ist aufgetreten:" + resp.response); 
            break;
        default: 
            break;
    }
}

function parseLogoutResponse () {
    location.reload(false);
}


// ------------------------------
// ajax function definitions
// ------------------------------

function getname () {
    var ajax = new sack ();
    ajax.onCompletion = parseGetNameResponse;
    ajax.requestFile = "/_SYSTEM/ajax/getname.html";
    ajax.method = "GET";
    ajax.runAJAX();
}

function login (formElem) {
    var ajax = new sack ();
    ajax.onCompletion = parseLoginResponse;
    ajax.requestFile = "/_SYSTEM/ajax/mitglieder_login.html";
    ajax.method = "POST";
    ajax.setVar ("mitnum", formElem.elements[0].value);
    ajax.setVar ("password", formElem.elements[1].value);
    ajax.runAJAX();
}

function logout () {
    var ajax = new sack ();
    ajax.onCompletion = parseLogoutResponse;
    ajax.requestFile = "/_SYSTEM/ajax/mitglieder_logout.html";
    ajax.method = "GET";
    ajax.runAJAX();
}

function getLastSearch () {
    var ajax = new sack ();
    ajax.onCompletion = parseGetLastSearchResponse;
    ajax.requestFile = "/_SYSTEM/ajax/getlastsearch.html";
    ajax.method = "GET";
    ajax.runAJAX();
}


// --------------
// ajax calls
// --------------

// on member page: don't display the whole my-fvb div
if (window.location.pathname.indexOf("/meine_fvb/mitgliederbereich") == 0) {
    my_fvb.style.display = "none";
} else {
    // blende andere Menü-Punkte als Login aus
    hide_myfvb_nav();
}

getname();
getLastSearch();