/*
* Génération de tag HTML 
*/

function Tag(name)
{
   this.name = name;
   this.body = null;
   this.tagAttributes = new Array();
   
   return this;
}
// Ajoute un attribut, et renvoie l'objet
Tag.prototype.att = function (attName, attValue)
{
   // Il est obligatoire de mettre new String(...) car si on passe un
   // nombre, attValue.length ne fonctionnera pas
   if ( attValue == null )
   {
      attValue = "NO_VALUE";
   }
   this.tagAttributes[attName] = new String(attValue);
   return this;
}
// Supprime un attribut, et renvoie l'objet
Tag.prototype.removeAttribute = function (attName)
{
   this.tagAttributes[attName] = null;
   return this;
}
// Positionne le corps du tag tel quel
Tag.prototype.setBody = function (bodyValue)
{
   this.body = bodyValue;
   return this;
}
// Renvoie le texte
Tag.prototype.text = function ()
{
   var txt = "<" + this.name;
   var msg = "";
   for (var attName in this.tagAttributes) 
   {      
      var attValue = this.tagAttributes[attName];
      if ( (attValue != null) && (attValue != "NO_VALUE") )
      {
         txt += " " + attName + '="' + escapeDoubleQuote(attValue) + '"';
      }
      else
      {
         txt += " " + attName;
      }
   }
   if ( ! this.body )
      txt += " />";
   else
   {
      txt += " >";
      txt += this.body;
      txt += "</" + this.name + ">\n";
   }
   return txt;
}

//
function escapeDoubleQuote(s)
{
   var ret = "";
   for (var i=0;i<s.length; i++)
   {
      var c = s.toString().charAt(i);
      // tdn 12/10/2005 \" n'est pas correctement interprété par le navigateur
      // qui tronque donc la valeur en saisie. Solution : utiliser l'encodage XML
      //if (c == '"') ret += '\\';
      //ret += c;
      if (c == '"') ret += '&#x22;';
      else ret += c;
   }
   return ret;
}
//
function br()
{
   return "<br />\n";
}
