﻿function ShowModalDialogue()
{
    var height = document.documentElement.clientHeight;
    var dialogue = Builder.node('div', { id : 'ModalDialogue', style : 'display:none;height:'+ height + 'px' + ';width:100%;' });
    document.body.appendChild(dialogue);
    new Effect.Appear(dialogue, { duration: 0.4, to: 0.7 });
}
    
function HideModalDialogue()
{
    var dialogue = $('ModalDialogue');
    if((dialogue != null) && (dialogue.parentNode != null))
    {
        new Effect.Fade($('ModalDialogue'), { duration: 0.4 });
        window.setTimeout(function(){ dialogue.parentNode.removeChild(dialogue); }, 400);
    }
}

function ShowLogin()
{
    this.ShowModalDialogue();    
    var loginWin = Builder.node('div', {id: 'loginWin', style: 'display:none'});
    
    var loginTitle = Builder.node('h1', { style: 'font-size:14px' });
    loginTitle.appendChild(document.createTextNode('Login'));
    loginWin.appendChild(loginTitle);
    
    var loginNameLabel = Builder.node('label');
    loginNameLabel.appendChild(document.createTextNode('Benutzer:'));
    loginWin.appendChild(loginNameLabel);
    
    var loginNameInput = Builder.node('input', { id: 'username', type: 'text', style: 'top:43px;left:100px;width:200px;position:absolute;border:1px solid #A4C34C;background-color:#CCCCCC' });
    loginWin.appendChild(loginNameInput);
    Event.observe(loginNameInput, 'mouseover', OverInput);
    Event.observe(loginNameInput, 'mouseout', OutInput);
    
    var loginPassLabel = Builder.node('label', { style: 'top:75px;left:10px;position:absolute' });
    loginPassLabel.appendChild(document.createTextNode('Passwort:'));
    loginWin.appendChild(loginPassLabel);
    
    var loginPassInput = Builder.node('input', { id: 'userpass', type: 'password', style: 'top:73px;left:100px;width:200px;position:absolute;border:1px solid #A4C34C;background-color:#CCCCCC' });
    loginWin.appendChild(loginPassInput);
    Event.observe(loginPassInput, 'mouseover', OverInput);
    Event.observe(loginPassInput, 'mouseout', OutInput);
    
    var message = Builder.node('p', { id: 'loginMessage', style: 'top:100px;left:10px;width:290px;height:11px;position:absolute;color:red;font-size:11px;font-family:Verdana;text-align:center;' });
    loginWin.appendChild(message);
    
    var loginSubmitBtn = Builder.node('input', { type: 'button', value: 'Anmelden', style: 'width:90px;top:140px;left:60px;position:absolute;'});
    loginWin.appendChild(loginSubmitBtn);
    Event.observe(loginSubmitBtn, 'click', this.SendLoginData.bindAsEventListener(this));
    
    var loginCancelBtn = Builder.node('input', { type: 'button', value: 'Abbrechen', style: 'width:90px;top:140px;left:160px;position:absolute;'});
    loginWin.appendChild(loginCancelBtn);
    Event.observe(loginCancelBtn, 'click', this.CloseLogin.bindAsEventListener(this));
    
    document.body.appendChild(loginWin);
    new Effect.Appear(loginWin, { duration: 0.6 });
    
    try
    {
        window.setTimeout(function(){ $('username').focus(); }, 600);
    }
    catch(e)
    {
    }
}

function ShowLogout()
{
    this.ShowModalDialogue();    
    var loginWin = Builder.node('div', {id: 'loginWin', style: 'display:none;height:100px;'});
    
    var loginTitle = Builder.node('h1', { style: 'font-size:14px' });
    loginTitle.appendChild(document.createTextNode('Logout'));
    loginWin.appendChild(loginTitle);
    
    var loginNameLabel = Builder.node('label', { style: 'width:100%;text-align:center' });
    loginNameLabel.appendChild(document.createTextNode('Möchten wirklich abgemeldet werden?'));
    loginWin.appendChild(loginNameLabel);
    
    var loginSubmitBtn = Builder.node('input', { type: 'button', value: 'Abmelden', style: 'width:90px;top:80px;left:60px;position:absolute;'});
    loginWin.appendChild(loginSubmitBtn);
    Event.observe(loginSubmitBtn, 'click', this.SendLogoutData.bindAsEventListener(this));
    
    var loginCancelBtn = Builder.node('input', { type: 'button', value: 'Abbrechen', style: 'width:90px;top:80px;left:160px;position:absolute;'});
    loginWin.appendChild(loginCancelBtn);
    Event.observe(loginCancelBtn, 'click', this.CloseLogin.bindAsEventListener(this));
    
    document.body.appendChild(loginWin);
    new Effect.Appear(loginWin, { duration: 0.6 });
}

function CloseLogin()
{
    var loginWin = $('loginWin');
    
    if(loginWin != null)
    {
        new Effect.Fade(loginWin, { duration: 6.0 });
        window.setTimeout(function(){ $('loginWin').parentNode.removeChild($('loginWin')); HideModalDialogue(); }, 600);
    }
}

function SendLoginData()
{
    var userNameInput = $('username');
    var userPassInput = $('userpass');
    
    if((userNameInput == null) || (userPassInput == null))
    {
        throw 'Error: Cannot find login form input fields within dom tree';
    }
    
    var username = $F(userNameInput);
    var userpass = $F(userPassInput);
    
    // Create input parameters for the webservice method
    var params=$H( {UserName : username, UserPass: userpass} ).toQueryString();
    // Create new Ajax request
    new Ajax.Request("AdminService.asmx/LogIn", { method : "post", parameters : params, requestHeaders : ['Pragma', 'no-cache', 'Cache-Control', 'no-store, no-cache, max-age=0, must-revalidate'], onSuccess : this.OnSendLoginDataResponse.bind(this), onFailure : this.OnSendLoginFail.bind(this) });
}

function SendLogoutData()
{   
    // Create new Ajax request
    new Ajax.Request("AdminService.asmx/LogOut", { method : "post", parameters : null, requestHeaders : ['Pragma', 'no-cache', 'Cache-Control', 'no-store, no-cache, max-age=0, must-revalidate'], onSuccess : this.OnSendLogoutResponse.bind(this) });
}

function OnSendLoginDataResponse(transport)
{    
    // Set local variable holding the xml data
    var responseXml = new XmlDocument(transport.responseXML);
    // Check if update was successful
    var response = responseXml.SelectSingleNode("XML_RESPONSE/SUCCESS").text
        
        
    if(response == 'true')
    {
        var message = $('loginMessage');
        message.style.color = '#A4C34C';
        message.innerHTML = 'Anmeldung erfolgreich!';
        
        var status = Builder.node('input', { type: 'hidden', id: 'status', value: 'true' });
        document.body.appendChild(status);
        
        CloseLogin();
        var createPageFunc = function(){ page = new Page(); };
        window.setTimeout(createPageFunc.bind(this), 600);
    }
    else if(response == 'false')
    {
        var message = $('loginMessage');
        message.innerHTML = 'Benutzername oder Passwort falsch!';
    }
    else if(response == null)
    {
        var message = $('loginMessage');
        message.innerHTML = 'Bei der Anmeldung ist eine Fehler aufgetreten. Bitte evrsuchen Sie es später erneut.!';
    }
}

function OnSendLoginFail(transport)
{
    var a = "b";
}

function OnSendLogoutResponse(transport)
{
    // Set local variable holding the xml data
    var responseXml = new XmlDocument(transport.responseXML);
    // Check if update was successful
    var response = responseXml.SelectSingleNode("XML_RESPONSE/SUCCESS").text
        
        
    if(response == 'true')
    {       
        var status = $('status');
        if(status != null)
        {
            status.parentNode.removeChild(status);
        }
        
        CloseLogin();
        
        if(page != null)
        {
            page.Reset();
        }
    }
}

function OverInput(Evt)
{
    var inputElem = Event.findElement(Evt, 'input');
    if(inputElem != null)
    {
        inputElem.style.border = '1px solid #1D3683';
    }
}

function OutInput(Evt)
{
    var inputElem = Event.findElement(Evt, 'input');
    if(inputElem != null)
    {
        inputElem.style.border = '1px solid #A4C34C';
    }
}


var status = false;
var activateLoginKey = false;
var page = null;
Event.observe(window, 'load', Init);

function Init()
{
    Event.observe(document, 'keydown', KeyDown);
    Event.observe(document, 'keyup', KeyUp);
    
    
    var statusField = $('status');
    if((statusField != null) && ($F(statusField) == 'true'))
    {
        status = true;
        new Page();
    }
}

function KeyDown(piEvent)
{
    var code = piEvent.keyCode;
    if(piEvent.keyCode == 17)
    {
        activateLoginKey = true;
    }
    else if((piEvent.keyCode == 88) && (activateLoginKey == true))
    {
        var statusField = $('status');
        if((statusField != null) && ($F(statusField) == 'true'))
        {
            ShowLogout();
            activateLoginKey = false;
        }
        else
        {
            ShowLogin();
            activateLoginKey = false;
        }
    }
}

function KeyUp(piEvent)
{
    if(piEvent.keyCode == 17)
    {
        activateLoginKey = false;
    }
}
