/*
*    Open Humanity 1.0
*    http://www.open-humanity.org
*    Copyright (C) 2009, Open Humanity
*
*    @author Xavier Boubert
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var loginElement ;
var passwdElement ;

Ext.onReady(function() {

	Ext.QuickTips.init();
	
	function sendForm() {
		if(formConnection.getForm().isValid()) {
			formConnection.getForm().submit({
				method: 'POST', 
				waitTitle: LANG.site_name + ' - ' + LANG.connection_title, 
				waitMsg: LANG.connection_loading,
				reset: true,
				success: function(form, action) {
					goToDesktop() ;
				},
				failure: function(form, action) { 
					if(action.failureType == 'server') { 
						var obj = Ext.util.JSON.decode(action.response.responseText) ;
						Ext.Msg.alert(LANG.site_name + ' - ' + LANG.all_server_problem, '<br /><span style="color:#970000;">'+obj.errors.reason+'</span><br />'); 
					}
					else { 
						Ext.Msg.alert(LANG.site_name + ' - ' + LANG.all_server_error_title, '<br /><span style="color:#970000;">'+ LANG.all_server_error_connect + action.response.responseText+'</span><br />'); 
					}
				}
			});
		}
	} ;
	
	var formConnection = new Ext.FormPanel({
		labelWidth: 100,
		url: 'index.php',
		frame: false,
		bodyStyle: 'padding: 20px 30px 0px 30px; background: none;',
		width: 298,
		border: false,
		monitorValid: true,
		defaults: {width: 240},
		defaultType: 'textfield',
		labelAlign: 'top',

		items: [loginElement = new Ext.form.TextField({
				name: 'login',
				id: 'login',
				fieldLabel: LANG.connection_login,
				labelStyle: 'font-size: 120%; color: #491e00;',
				allowBlank: false,
				value: lastLogin,
				listeners: {
					change: {
						fn: function () {
							if(lastLogin != '' && loginElement.getValue() != lastLogin && document.getElementById('avatar')) {
								document.getElementById('avatar').src = 'storage/avatars/none.jpg' ;
							}
							else if(lastLogin != '' && loginElement.getValue() == lastLogin && document.getElementById('avatar')) {
								document.getElementById('avatar').src = lastAvatar ;
							}
						}
					},
					specialkey: function(field, events){
						if (events.getKey() == Ext.EventObject.ENTER) {
							sendForm() ;
						}
					}
				}
			}), passwdElement = new Ext.form.TextField({
				name: 'passwd',
				id: 'passwd',
				inputType: 'password',
				fieldLabel: LANG.connection_password,
				labelStyle: 'font-size: 120%; color: #491e00;',
				allowBlank: false,
				listeners: {
					specialkey: function(field, events){
						if (events.getKey() == Ext.EventObject.ENTER) {
							sendForm() ;
						}
					}
				}
			}), new Ext.form.Checkbox({
				name: 'autocon',
				id: 'autocon',
				fieldLabel: 'M&eacute;moriser la connexion',
				labelStyle: 'font-size: 120%; color: #491e00; float: left; margin-right: 10px;',
				checked: true
			}), {
				inputType: 'hidden',
				name: 'action',
				value: 'connect'
			}
		],

		buttons: [{
			text: LANG.connection_title,
			formBind: true,
			handler: function () {
				sendForm() ;
			}
		}]
	});

	formConnection.render('form-connection');
	
	var connection = Ext.get('connection');
	connection.fadeIn({
		duration:.5,
		callback : function(){
			if(lastLogin == '') {
				loginElement.focus() ;
				loginElement.clearInvalid() ;
			}
			else {
				passwdElement.focus() ;
				passwdElement.clearInvalid() ;
			}
		}
	});
	
}) ;

function breakConnection () {
	Ext.Ajax.request({
		url: 'index.php',
		success: function(action, form) {
			var marginAvatar = Ext.get('margin-avatar') ;
			marginAvatar.fadeOut({ remove:true }) ;
			loginElement.setValue('') ;
			passwdElement.setValue('') ;
			passwdElement.clearInvalid() ;
			loginElement.focus() ;
			loginElement.clearInvalid() ;
		},
		params: { action: 'break-connection', _dc: (new Date().getTime()) }
	}) ;
}
	
function goToDesktop () {
	var connection = Ext.get('connection');
	connection.fadeOut({
		duration:.5,
		remove:true,
		callback : function(){
			var url = 'desktop.php' ;
			if(direct_app != '') {
				url += '?application=' + direct_app ;
			}
			window.location.href = url ;
		}
	});
} ;

function goToForgotten () {
	var connection = Ext.get('connection');
	connection.fadeOut({
		duration:.5,
		remove:true,
		callback : function(){
			window.location.href = 'forgotten.php' ;
		}
	});
} ;

function goToRegister () {
	var connection = Ext.get('connection');
	connection.fadeOut({
		duration:.5,
		remove:true,
		callback : function(){
			window.location.href = 'register.php' ;
		}
	});
} ;

