//////////////////////////////////////////////////////////////
// login

var sPath = "system/";

function successFn(result, request )
{
	var o = Ext.decode(result.responseText);
	
	if (o.success)
	{
    	window.location = sPath + "desktop.php";
	}
	else if (o.error == "newuser")
	{
        var winDlg = new Ext.Window({
            title: "Welcome To OneDesk",
            width: document.body.clientWidth * 0.8,
            height: document.body.clientHeight * 0.8,
            layout:'fit',
            closable: false,
            resizable: false,
            padding:10,
            border: true,
            modal:true,
            html : '<iframe src="system/termsandconditions.html" style="width:100%;height:100%;"></iframe>',
            buttons: [
                {
                    text:'Decline',
                    width:60,
                    handler: function(btn)
                    {
                        winDlg.close();
                    }
                },
                {
                    text:'Accept',
                    width:60,
                    handler: function(btn)
                    {
                     	var sUserid = document.getElementById("userid").value;
                     	var sPassword = document.getElementById("password").value;
                    	var sWidth = document.body.clientWidth;
                    	var sHeight = document.body.clientHeight;
                    	
                    	Ext.Ajax.request({
                       		url: sPath + 'ajax.php',
                       		method: 'POST',
                       		success: successFn,
                       		failure: failedFn,
                       		params: { command: 'user.login', userid: sUserid, password: sPassword, width: sWidth, height: sHeight, accept:'Y' }
                    	});
                        winDlg.close();
                    }
                }
            ]
        });
        winDlg .show(); 
    }
    else
	{
	    Ext.MessageBox.alert('Warning', o.error, null);
	}
}

function failedFn(result, request )
{
	alert("AJAX Command Failed");
}

function DoLogin()
{
 	var sUserid = document.getElementById("userid").value;
 	var sPassword = document.getElementById("password").value;
	var sWidth = document.body.clientWidth;
	var sHeight = document.body.clientHeight;
	
	Ext.Ajax.request({
   		url: sPath + 'ajax.php',
   		method: 'POST',
   		success: successFn,
   		failure: failedFn,
   		params: { command: 'user.login', userid: sUserid, password: sPassword, width: sWidth, height: sHeight }
	});
}

function entsub(evt)
{
    try
    {
    	if (evt != null)
    	{
    		if (evt.which == 13)
    		{
    			DoLogin();
    			return false;
    		}
    		else if (window.event.keyCode == 13)
    		{
    			DoLogin();
    			return false;
    		}
    	}
    	else
    	{
    		 if (window.event.keyCode == 13)
    		 {
    			DoLogin();
    			return false;
    		 }
    	}
    }
    catch(e)
    {
    }
}

function doResize()
{
 	var panel = document.getElementById("login-panel");
 	
 	if (panel)
 	{
		panel.style.marginTop = (document.body.clientHeight - 480)/2;
		panel.style.marginLeft = (document.body.clientWidth - 665)/2;
		document.getElementById("userid").focus();
 	}
    document.body.style.backgroundImage = "url(system/image.php?src=images/bg.jpg&width=" + document.body.clientWidth + "&height=" + document.body.clientHeight + ")";
}

function showMessage(title,msg,icon) 
{
    if (icon == null) icon = Ext.Msg.INFO;
    Ext.Msg.show({
         title:title
        ,msg:Ext.util.Format.ellipsis(msg, 2000)
        ,icon:icon
        ,buttons:Ext.Msg.OK
        ,minWidth:1200 > String(msg).length ? 360 : 600
    });
} // eo function showMessage

function doForgotPassword()
{
 	var sUserid = document.getElementById("userid").value;
 	
 	if (sUserid == "")
 	{
 	      showMessage('Warning', 'Need User ID',Ext.Msg.WARNING);
 	      return;
    }
	Ext.Ajax.request({
   		url: sPath + 'ajax.php',
   		method: 'POST',
   		success: function(result, request)
           {
                try
                {
                    var o = Ext.decode(result.responseText);
                    
                    if (o.success)
                    {
                        showMessage('Success', 'Password Sent To Your Email Account',Ext.Msg.INFO);    
                    }
                    else
                    {
                        showMessage('Warning', o.error,Ext.Msg.WARNING);
                    }
                }
                catch(e)
                {
                    alert(result.responseText);
                }
           }
           ,
   		failure: failedFn,
   		params: { command: 'user.forgotpassword', user: sUserid }
	});
}
Ext.onReady(function()
{	
    doResize();
    //addIntro();
});

