GALILEO.cylog.error.loginDlg = null;

GALILEO.cylog.error.Handler = function() {
	GALILEO.util.Event.globalException.subscribe(this.onGlobalException, this);
	GALILEO.util.Event.unknownError.subscribe(this.onUnknownError, this);
	GALILEO.util.Event.securityException.subscribe(this.onSecurityExcepcion, this);	
};

GALILEO.cylog.error.Handler.prototype.onGlobalException = function(type, args, obj) {
	var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgGeneralFailure', '25em', true, false, null, null, "Error", "Se ha producido un error inesperado al realizar la operacion solicitada.");
    dlg.setIcon(GALILEO.widget.SimpleDialog.config.ERROR_ICON);
	dlg.show();	
};

GALILEO.cylog.error.Handler.prototype.onUnknownError = function(type, args, obj) {
    var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgGeneralFailure', '25em', true, false, null, null, "Error", args[0].statusText);
    dlg.setIcon(GALILEO.widget.SimpleDialog.config.ERROR_ICON);
	dlg.show();
};

GALILEO.cylog.error.Handler.prototype.onSecurityExcepcion = function(type, args, obj) {
	// Comprobamos si existe el div que posee el formulario de login
	var capa = YAHOO.util.Dom.get('div-ajax-form');

	if (!capa) {
		GALILEO.cylog.error.loginDlg = null;
		capa = GALILEO.util.Dom.createElement('div', [{name: 'id', value: "div-ajax-form"}]);
		document.body.appendChild(capa);
	
		// Reiniciamos el div y construimos el formulario
		capa.innerHTML = "";
		
		var hd = "<div class=\"hd\">" + GALILEO.cylog.text.LOGIN_LEGEND + "</div>";
		var bd = "<div class=\"bd\">";
			bd += "<p>La sesi&oacute;n ha caducado</p>";
			bd += ("<form name=\"ajax-login\" id=\"ajax-login\" method=\"post\" action=\"" + GALILEO.config.contexto + "/j_dox_acegi_security_check\" class=\"formularioEstandar\">");
			bd += "<table width=\"99%\" class=\"tabla\" align=\"center\" border=\"0\">";
			bd += "<tr><td align=\"right\">"
			bd += "<label for=\"username\" >" + GALILEO.cylog.text.LOGIN_USUARIO + ": " + "</label>";
			bd += "</td><td align=\"left\">"
			bd += "<input name=\"j_username\" type=\"text\" onkeypress='javascript:comprobarIntro(event);' />";
			bd += "</td></tr><tr><td align=\"right\">"
			bd += "<label for=\"pwd\" >" + GALILEO.cylog.text.LOGIN_PASSWORD + ": " + "</label>";
			bd += "</td><td align=\"left\">"
			bd += "<input name=\"j_password\" type=\"password\" onkeypress='javascript:comprobarIntro(event);' />";
			bd += "</td></tr>"
			bd += "</table>";
			bd += "</form>";
			bd += "</div>";

		// Inyectamos el nuevo formulario de login		
		capa.innerHTML = (hd + bd);
		
		var buttonsName = {
		   submit: 'Aceptar',
		   cancel: 'Cancelar'
		};
			 
		var calls = {
		   callback: {
		   	  success: function(o) {
		   	  	var cabecera = GALILEO.cylog.text.LOGIN_LEGEND;
		   	  	var cuerpo = "<ul class='ulMod3'>";
		   	  	cuerpo += "<li>" + GALILEO.cylog.text.LOGIN_CORRECTO + "</li>";
  		   	  	cuerpo += "<li>" + GALILEO.cylog.text.LOGIN_REPETIR + "</li>";
		   	  	cuerpo += "</ul>";
	   	  	    var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgSuccessLogin', '500px', true, false, null, null, cabecera, cuerpo);
			    dlg.show();
		   	  },
		 	  failure: function(o){
		 	  	var cabecera = GALILEO.cylog.text.LOGIN_LEGEND;
		   	  	var cuerpo = "<ul class='ulMod3'><li>" + GALILEO.cylog.text.LOGIN_ERROR + "</li></ul>";
	   	  	    var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgSuccessLogin', '400px', true, false, null, null, cabecera, cuerpo);
			    dlg.show();
		 	  	GALILEO.util.Connect.handlerFailure
		 	  },
		 	  scope: this
		   }
		};
		
		// Creamos el dialogo que se debera mostrar
		if (GALILEO.cylog.error.loginDlg == null) {
			GALILEO.cylog.error.loginDlg = new GALILEO.widget.GCDialog(capa.id, '400px', buttonsName, calls, false, true);
		}
	}
	
	
	// Reiniciamos el formulario de login vaciando sus campos
	GALILEO.util.Form.getField('ajax-login', 'j_username').value = "";
	GALILEO.util.Form.getField('ajax-login', 'j_password').value = "";
	GALILEO.cylog.error.loginDlg.show();
};

//Enter en panel de login";
var comprobarIntro = function(event){
	if (event.keyCode == 13){
		var frm = YAHOO.util.Dom.get('ajax-login');
		var success = function(o) {
	   	  	var cabecera = GALILEO.cylog.text.LOGIN_LEGEND;
	   	  	var cuerpo = "<ul class='ulMod3'>";
	   	  	cuerpo += "<li>" + GALILEO.cylog.text.LOGIN_CORRECTO + "</li>";
 		   	  	cuerpo += "<li>" + GALILEO.cylog.text.LOGIN_REPETIR + "</li>";
	   	  	cuerpo += "</ul>";
   	  	    var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgSuccessLogin', '500px', true, false, null, null, cabecera, cuerpo);
		    dlg.show();
		    GALILEO.cylog.error.loginDlg.cancel();
		};
	  		var failure = function(o){
			var cabecera = GALILEO.cylog.text.LOGIN_LEGEND;
	   	  	var cuerpo = "<ul class='ulMod3'><li>" + GALILEO.cylog.text.LOGIN_ERROR + "</li></ul>";
 			  	    var dlg = new GALILEO.cylog.widget.GSimpleDialog('__dlgSuccessLogin', '400px', true, false, null, null, cabecera, cuerpo);
		    dlg.show();
	 	  	GALILEO.util.Connect.handlerFailure
		};
		GALILEO.cylog.utils.Connect.asyncSubmitFormEl(frm, success, failure);
	};
};

GALILEO.cylog.error.handler = new GALILEO.cylog.error.Handler();
