//===============================================================================================================================================
// Date : 2009.07.02
// File : Login.js
// Doc Author : Malak Khalil
// Use  : Login form , it's called from index.html , this file contain the username , password and enabled password  , after submitting the form
// 		  2 responses are possible : 
// OnLoginSuccess  : the page redirects to main.html
// OnLoginFailure  : bad authentication back to index.html
//================================================================================================================================================

var frmLogin;
var frmLoginFrame;

function CreateFrmLogin()
{
    var username = new Ext.form.TextField({ 
        fieldLabel:'Username', 
        name:'elf_username',
        enableKeyEvents: true,
        allowBlank:false 
    });
    
    var password = new Ext.form.TextField({ 
        fieldLabel:'Password', 
        name:'elf_password', 
        inputType:'password', 
        enableKeyEvents: true,
        allowBlank:false
    });
    
    var enable_password = new Ext.form.TextField({ 
        fieldLabel:'Administrator Password (enable)', 
        name:'elf_enable_password', 
        inputType:'password', 
        enableKeyEvents: true,
        allowBlank:true 
    });
    
    username.on('keyup', OnKeyUp);
    password.on('keyup', OnKeyUp);
    enable_password.on('keyup', OnKeyUp);

    username.value = 'mgmt';
    if( debug == 1 )
    {
        // Automatic logon, for debug only
        password.value = 'mgmt';
        enable_password.value = 'mgmt';
    }

    var loginBtn = new Ext.Button({ 
        text:'Login',
        formBind: true,	 
        handler:OnSubmitLogin
    });
    
    frmLogin = new Ext.FormPanel({
        labelWidth: 200, // label settings here cascade unless overridden
        url:ELF_CGI,
        //frame:true,
        border: false,
        bodyStyle:'padding:5px 5px 0 10px',
        width: 470,
        defaults: {width: 230},
        defaultType: 'textfield',
        items: [
            username,
            password,
            enable_password,
            { 
                fieldLabel:'Action', 
                name:'elf_action', 
                inputType:'hidden',
                value:'login',
                allowBlank:false 
            }
        ],

        buttons: [loginBtn]
    },
	DisableLogin = function()
	{
		loginBtn.setDisabled(true)
	},
	EnableLogin = function()
	{
		loginBtn.setDisabled(false)
	},
	Getpassword = function()
	{
		return password.getValue();
	}
);
    
    frmLogin.render('frmLogin');

    password.focus('', 10);
    DisableLogin();
    if( debug == 1 )
    {
        // Automatic logon, for debug only
        OnSubmitLogin();
    }
}

function OnKeyUp(scope, ev)
{
	if ( Getpassword() != '')
	{
		EnableLogin();
		if( ev.browserEvent.keyCode == ev.RETURN )
	  {		
		  OnSubmitLogin();
	  }
	}
	else
	{
		DisableLogin();
	}
	  
}

function OnSubmitLogin()
{
	frmLogin.el.mask('Connecting, please wait...', 'x-mask-loading-login');
    frmLogin.getForm().submit({ 
        method:'GET', 
        //waitTitle:'Connecting', 
        //waitMsg:'Sending data...',
        // Functions that fire (success or failure) when the server responds. 
        // The one that executes is determined by the response that comes from
        // the login web service. The web service would actually respond with valid JSON, 
        // something like: 
        // - "{ success: true }" or 
        // - "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
        // depending on the logic contained within your server script.
        success:OnLoginSuccess,
        // Failure function, see comment above re: success and failure. 
        failure:OnLoginFailure 
    });
	  
}

function OnLoginSuccess(form, action)
{
	frmLogin.el.unmask();
    obj = Ext.util.JSON.decode(action.response.responseText); 
    window.location = "main.html?sid=" + obj.sid.sid;
}

function OnLoginFailure(form, action)
{
	frmLogin.el.unmask();
    Ext.Msg.alert('Login Failed!', "Login Failed, please verify your username and password."); 
//    if(action.failureType == 'server')
//    { 
//        obj = Ext.util.JSON.decode(action.response.responseText); 
//        Ext.Msg.alert('Login Failed!', obj.errors.reason); 
//    }
//    else
//    { 
//        Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText + "abcd"); 
//    } 
    frmLogin.getForm().reset(); 
}


