﻿
// --------------------------------------------------------------------------------------------
// © Intelogie.com 2007
// 
// WARNING : This computer program is protected by copyright law and international treaties.
// Unauthorized reproduction or distribution of this program, or any porttion of it, may
// result in severe civil and criminal penalties, and will be prosecuted to the maximum extent
// possible under the law.
// --------------------------------------------------------------------------------------------

var frmLogin = {
    dialog : null,
    clear : function() {
        txtUserName.setValue('');
        txtPassword.setValue('');
        txtUserName.focus(true, true);
    },
    doLogin : function() {
        cp.set('Login_UserName',txtUserName.getValue());        
        cp.set('Login_Language',cboLng.getValue());
        ExecuteLogin();               
    },
    centerForm : function() {
        if (this.dialog != null) {
            this.dialog.center();
        }
    },
    showDialog : function() {
        if (!this.dialog) {
            this.dialog = new Ext.Window({
                    el:'divLogin',
                    layout:'fit',
                    width:350,
                    height:175,
                    title : getKeyWord(getLangueSelected(), '_SYSTEMAUTHENTIFICATION'),
                    closable : false,
                    resizable : false,
                    draggable: false,
                    plain: true,                    
                    buttons: [{
                        id : 'btnLogin',
                        text: getKeyWord(getLangueSelected(), '_LOGIN'),
                        disabled:false,
                        handler: function() {
                            frmLogin.doLogin();
                        }
                    },{
                        id : 'btnCancel',
                        text: getKeyWord(getLangueSelected(), '_CANCEL'),
                        handler: function(){
                            frmLogin.clear();
                        }
                    }]                    
            });
            this.dialog.show();
        }
    }
}
    

// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
//Ext.EventManager.onDocumentReady(loginDialog.init, loginDialog, true);

function ExecuteLogin() {
    
    if (txtUserName.getValue() == '') {
        Beezilla.MsgBox(usernameRequiredText, getKeyWord(getLangueSelected(), '_LOGIN'));
        txtUserName.focus(true, true);
        return false;
    }
    
    if (txtPassword.getValue == '') {
        Beezilla.MsgBox(passwordRequiredText, getKeyWord(getLangueSelected(), '_LOGIN'));
        txtPassword.focus(true, true);
        return false;
    }
    
    var sParameters = 'UserName=' + txtUserName.getValue();
    sParameters += '&Password=' + txtPassword.getValue();
    sParameters += '&Language=' + cboLng.getValue();
    
    Ext.lib.Ajax.request(
        'POST',
        '/modules/security/login/login.validate.php',
        {success:LoginSuccess,failure:LoginFailure, scope:this},
        sParameters);
    
    function LoginSuccess(r) {
	
		var ardata = Ext.data.Record.create([
   			{name: 'status', mapping: 'status'},    
			{name: 'error', mapping: 'error'}       
		]);
		
		var myReader = new Ext.data.XmlReader({
		   totalRecords: "results", 
		   record: "row",           
		   id: "id"                 // The element within the row that provides an ID for the record (optional)
		}, ardata);

		var data = myReader.readRecords(r.responseXML);
 					
        if (data.records[0].data.status == 'OK') {
            document.location.href = '/modules/explorer/index.php';
        } else if (data.records[0].data.status == 'LOCKED') {
            Beezilla.MsgBox(getKeyWord(getLangueSelected(), '_ACCOUNTLOCKED'), getKeyWord(getLangueSelected(), '_LOGIN'));
        } else if (data.records[0].data.status == 'NOLICENSE') {
            Beezilla.MsgBox(noLicenceText,getKeyWord(getLangueSelected(), '_LOGIN'));            
        } else if (data.records[0].data.status == 'MUSTCHANGE') {
            Beezilla.MsgBox(mustChangePasswordText,getKeyWord(getLangueSelected(), '_LOGIN')); 
            showChangePassword();
        } else {
			Beezilla.MsgBox(getKeyWord(getLangueSelected(), '_INVALIDLOGIN'),getKeyWord(getLangueSelected(), '_LOGIN'));
        }
    }
    
    function LoginFailure(r) {
        Beezilla.MsgBox(getKeyWord(getLangueSelected(), '_INVALIDLOGIN'),getKeyWord(getLangueSelected(), '_LOGIN'));
    }
    
}

function showChangePassword() {
    document.getElementById('txtUser').value = '';
    document.getElementById('txtOldPassword').value = '';
    document.getElementById('txtNewPassword').value = '';
    document.getElementById('txtConfirmPassword').value = '';
    document.getElementById('txtUser').value = document.getElementById('txtUserName').value;    
    changePasswordDlg.showDialog(); 
    if (document.getElementById('txtUser').value == '') {
        document.getElementById('txtUser').focus();
    } else {
        document.getElementById('txtOldPassword').focus();
    }
}


var changePasswordDlg = function(){
    // everything in this space is private and only accessible in the HelloWorld block
    
    // define some private variables
    var dialog;
    
    // return a public interface
    return {       
        showDialog : function(){
            if(!dialog){ // lazy initialize the dialog and only create it once
                dialog = new Ext.LayoutDialog("changePasswordDlg", { 
                        modal:true,
                        width:400,
                        height:230,
                        shadow:true,
                        minWidth:300,
                        minHeight:300,
                        proxyDrag: true,                        
	                    center: {
	                        autoScroll:true,
	                        tabPosition: 'top',
	                        closeOnTab: true,
	                        alwaysShowTabs: false
	                    }
                });
                dialog.addKeyListener(27, cancelChangePassword, dialog);
                dialog.addKeyListener(13, changePassword, dialog);
                dialog.addButton('Modifier le mot de passe', changePassword, dialog);
                dialog.addButton('Annuler', cancelChangePassword, dialog);
                
                var layout = dialog.getLayout();
                layout.beginUpdate();                
	            layout.add('center', new Ext.ContentPanel('changePassword', {title: 'Modifier mot de passe'}));            	            
	            layout.endUpdate();
	            
            }
            dialog.show();
        },
        hideDialog : function() {
            dialog.hide();
        }
    };
        
}();

function changePassword() {

    if (document.getElementById('txtUser').value == '') {
        Beezilla.MsgBox(usernameRequiredText);
        document.getElementById('txtUser').focus();
        return;
    }
    if (document.getElementById('txtOldPassword').value == '') {
        Beezilla.MsgBox(passwordRequiredText);
        document.getElementById('txtOldPassword').focus();
        return;
    }
    if (document.getElementById('txtNewPassword').value == '') {
        Beezilla.MsgBox(newPasswordRequiredText);
        document.getElementById('txtNewPassword').focus();
        return;
    }
    if (document.getElementById('txtConfirmPassword').value == '') {
        Beezilla.MsgBox(confirmationPasswordRequiredText);
        document.getElementById('txtConfirmPassword').focus();
        return;
    }
    if (document.getElementById('txtNewPassword').value !=  document.getElementById('txtConfirmPassword').value) {
        Beezilla.MsgBox(passwordsDoNotMatchText);
        document.getElementById('txtNewPassword').focus();
        return;
    }
    
    var sParameters = 'txtUser=' + document.getElementById('txtUser').value;
    sParameters += '&txtOldPassword=' + document.getElementById('txtOldPassword').value;
    sParameters += '&txtNewPassword=' + document.getElementById('txtNewPassword').value;
    sParameters += '&Language=' + document.getElementById('Language').value;
    
    Ext.lib.Ajax.request(
        'POST',
        '/modules/security/login/changepassword.aspx',
        {success:PasswordSuccess,failure:PasswordFailure, scope:this},
        sParameters);
    
    function PasswordSuccess(r) {
        if (r.responseText == 'OK') {
            Beezilla.MsgBox(changePasswordOKText);
            document.getElementById('txtPassword').value = '';
            changePasswordDlg.hideDialog();    
        } else if (r.responseText == 'CANTCHANGE') {
            Beezilla.MsgBox(cantChangePasswordText);     
        } else {
            Beezilla.MsgBox(changePasswordErrorText);            
        }
    }
    
    function PasswordFailure(r) {
        Beezilla.MsgBox(changePasswordErrorText);
    }
        
}

function cancelChangePassword() {
    changePasswordDlg.hideDialog();
}
