//
// Code JavaScript pour le support de l'Ajax. Pour le projet Asloca.
//
// Auteur:  OrdinaSoft
//          Patrick Lanz
//          Lausanne
//          info@ordinasoft.ch
//
// Première version: 10 mai 2007
//
// Dépend de:
//   - OrdinaSoft.Ajax.js
//   - DialogBox.js
//   - Master.js
//
// Note: quand Ajax_Initialized est défini, la bibliothèque OrdinaSoft.Ajax est aussi
//       initialisée.

//-----------------------------------------------------------------------------------------------
// Initialisation du namespace.

if (typeof Ajax == 'undefined')
  Ajax = new Object ();

//-----------------------------------------------------------------------------------------------
// Initialisation.

Ajax.Init = function () {

  // Attend que les dépendances soient initialisées
  if ((typeof OrdinaSoft_Ajax_Initialized == 'undefined') ||
      (typeof DialogBox_Initialized       == 'undefined') ||
      (typeof Master_Initialized          == 'undefined')) {
    setTimeout (Ajax.Init, 42);
    return false;
  }  // non initialisé 

  Ajax.Request.prototype = new OrdinaSoft.Ajax.Request ();


  // Contrôle si on a reçu une erreur en XML. Si une erreur a été reçue, le message reçu sera
  // affiché. Doit être appelée lorsque la requête est terminée.
  //  - Le résultat sera True si la requête est en ordre et False si la requête a une erreur.
  Ajax.Request.prototype.IsXmlOK = function () {

    var Error = this.Req.responseXML;
    if (Error == null)
      return true;
    Error = Error.getElementsByTagName ('Error');
    if (Error.length == 0)
      return true;
    OrdinaSoft.DialogBox.MessageBox.Error (OrdinaSoft.Ajax.GetXmlText (Error [0]),
                                           Master.AppName);
    return false;
  } // Ajax.Request.prototype.IsXmlOK


  // Premier appel d'un groupe.
  OrdinaSoft.Ajax.FirstRequest = function () {
    if (Ajax.Mask == null) {
      Ajax.Mask = document.createElement ('div');
      var Style = Ajax.Mask.style;
      Style.position = 'fixed';
      Style.left = 0;
      Style.top = 0;
      Style.width = '100%';
      Style.height = '100%';

      var s = [];
      s [s.length] = '<table border="0" cellspacing="0" cellpadding="0" ' +
                       'style="height: 100%; width: 100%">';
      s [s.length] = '  <tr style="height: 100%; width: 100%">';
      s [s.length] = '    <td style="height: 100%; text-align: center; width: 100%">' +
                            '<img src="' + Master.VirtAppPath + 'Images/Wait.gif" /></td>';
      s [s.length] = '  </tr>';
      s [s.length] = '</table>';
      Ajax.Mask.innerHTML = s.join ('\r\n');

      document.forms [0].appendChild (Ajax.Mask);
    } // Ajax.Mask == null
    return true;
  } // OrdinaSoft.Ajax.FirstRequest


  // Dernier appel d'un groupe.
  OrdinaSoft.Ajax.LastRequest = function () {
    if (Ajax.Mask != null) {
      Ajax.Mask.parentNode.removeChild (Ajax.Mask);
      Ajax.Mask = null;
    } // (Ajax.Mask != null)
    return true;
  } // OrdinaSoft.Ajax.LastRequest


  Ajax_Initialized = true;
  return true;
} // Ajax.Init


Ajax.Init ();

//----------------------------------------------------------------------------------------------
// Création d'une requête.

// Constructeur.
//  - URL est l'URL de la requête.

Ajax.Request = function (URL) {

  this.Init (URL);

  this.NoAjaxSupportFunc = function () {
      OrdinaSoft.DialogBox.MessageBox.Error
        ('Votre browser ne peut pas utiliser ce site,<br />' +
           'car il n\'est pas compatible avec la technologie Ajax.',
         Master.AppName);
      return true;
    }

  this.ErrorFunc = function (StatusCode) {
      OrdinaSoft.DialogBox.MessageBox.Error
        ('Erreur ' + StatusCode.toString () + ' sur le serveur !<br />' +
           'Veuillez r&eacute;essayer plus tard.<br /><br />' +
           'Si le probl&egrave;me persiste, veuillez contacter le ' + 
           '<a href="mailto:gwlesite@ordinasoft.ch">webmaster</a>.',
         Master.AppName);
      return true;
    }

  return true;
} // Ajax.Request
