/*
 * $Id: faba5.login-1.0.js 1910 2011-07-08 14:41:23Z markus.tripp@fabasoft.com $
 * 
 * Copyright © Fabasoft R&D GmbH, A-4020 Linz, 1995-2011.
 * 
 * All rights reserved. All hardware and software names used are registered trade names and/or registered trademarks of  
 * the respective manufacturers.
 * 
 * The user of this computer program acknowledges that the above copyright notice, which constitutes the Universal 
 * Copyright Convention, will be attached at the position in the function of the computer program which the author has 
 * deemed to sufficiently express the reservation of copyright. It is prohibited for customers, users and/or third  
 * parties to remove, modify or move this copyright notice.
 * 
 * This computer program is protected by copyright law and international treaties. Unauthorized reproduction or 
 * redistribution of this program, or any portion of it, may result in severe civil and criminal penalties.
 * 
 * The use of this software requires a valid license agreement for this software. This license agreement specifies 
 * especially permitted use through the licensing model. A license agreement must already exist prior to installing the 
 * software as the installation process of this software does not include the conclusion of a license agreement.
 */

var faba5 = faba5 || {};

faba5.login = function(selector) {
    var loginDialog = $(selector);
    var buttons = {};
    
    buttons[loginDialog.data('button-login')] = function() {
        loginDialog.find('.fmessage').hide();
        var form = loginDialog.find('form');
        
        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: form.serialize(),
            success: function(data) {
                if (data.status == 'success') {
                    window.location.reload(true);
                } else {
                    loginDialog.message(loginDialog.data('error-authentication'), {type: 'error', autoHide: false});
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                loginDialog.message(loginDialog.data('error-connection'), {type: 'error'});
            }
        });
    };
    
    buttons[loginDialog.data('button-cancel')] = function() {
        $(this).dialog('close');
    };

    loginDialog.dialog({
        autoOpen: false,
        modal: true,
        buttons: buttons
    });
    
    loginDialog.keyup(function(event) {
        if (event.keyCode == 13) {
            buttons[loginDialog.data('button-login')]();
        }
    });

    loginDialog.find('.message').hide();
    loginDialog.find('form :input').val('');
    
    loginDialog.dialog('open');
};

