/*/////////////////////////////////////
//				     //
//  This is the engien for the chat  //
//  ChatEngine.addMessage();	     //
//  ChatEngine.drawAll();	     //
//  ChatEngine.deleteMess(objNr);    //
//  ChatEngine.readKey(event);	     //
//				     //
/////////////////////////////////////*/

var HtmlGen = {
  // This function adda a element
  addElement:function(parentElement, newElement, attributeList, textNode)
  {
    var theElement = document.createElement(newElement);
    for(var i_attr = 0; i_attr < attributeList.length; i_attr++)
    {
      if(attributeList[i_attr][0] == "class")
      {
        theElement.className=attributeList[i_attr][1];
      }
      else
      {
        theElement.setAttribute(attributeList[i_attr][0], attributeList[i_attr][1]);
      }
    }
    if(textNode != null)
    {
      var thetext=document.createTextNode(textNode);
      theElement.appendChild(thetext);
    }
    parentElement.appendChild(theElement);
    return theElement;
  },
  
  innerHtml:function(theParrent, theHtml)
  {
    //  This first code is from w3schhols.com
    try //Internet Explorer
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML("<start>"+theHtml+"</start>");
    }
    catch(e)
    {
      try //Firefox, Mozilla, Opera, etc.
      {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString("<start>"+theHtml+"</start>","text/xml");
      }
      catch(e) {alert(e.message)}
    }
    
    // Now its all me
    this.writeNodes(theParrent, xmlDoc.documentElement.childNodes);


  },
  writeNodes:function(parentNode, theNodes)
  {
    for(var i_node=0; i_node < theNodes.length; i_node++)
    {
      if(theNodes[i_node].nodeType == 1)
      {
        var theElement = document.createElement(theNodes[i_node].nodeName);
        var attr = theNodes[i_node].attributes;
        for(var i_attr = 0; i_attr < attr.length; i_attr++)
        {
          if(attr[i_attr].name == "onload" || attr[i_attr].name == "onunload" || attr[i_attr].name == "onchange" || attr[i_attr].name == "onsubmit" || attr[i_attr].name == "onreset" || attr[i_attr].name == "onselect" || attr[i_attr].name == "onblur" || attr[i_attr].name == "onfocus" || attr[i_attr].name == "onkeydown" || attr[i_attr].name == "onkeypress" || attr[i_attr].name == "onkeyup" || attr[i_attr].name == "onclick" || attr[i_attr].name == "ondblclick" || attr[i_attr].name == "onmousedown" || attr[i_attr].name == "onmousemove" || attr[i_attr].name == "onmouseover" || attr[i_attr].name == "onmouseout" || attr[i_attr].name == "onmouseup")
          {
            theElement[attr[i_attr].name] = eval(attr[i_attr].value);
          }
          else
          {
            // This is an ie fix
            if (navigator.appName == "Microsoft Internet Explorer")
            {
              if(attr[i_attr].name == "class")
              {
                theElement.className=attr[i_attr].value;
              }
              if(attr[i_attr].name == "style")
              {
                var styleList = attr[i_attr].value.split(/;/);
                for(var i_style = 0; i_style < styleList.length; i_style++)
                {
                  if(styleList[i_style] != "")
                  {
                    styleAttr = styleList[i_style].split(/:/);
                    styleSplit = styleAttr[0].split(/-/);
                    if(styleSplit.length > 1)
                    {
                      styleAttr[0] = styleSplit[0]+styleSplit[1].substr(0,1).toUpperCase()+styleSplit[1].substr(1);
                    }
                    theElement.style[styleAttr[0].replace(/ /g, "")]=styleAttr[1].replace(/ /g, "");
                  }
                }
              }
              else
              {
                theElement.setAttribute(attr[i_attr].name, attr[i_attr].value);
              }
            }
            // This is for all the other browsers
            else
            {
              theElement.setAttribute(attr[i_attr].name, attr[i_attr].value);
            }
          }
        }
      }
      if(theNodes[i_node].nodeType == 3)
      {
        var textNode = theNodes[i_node].textContent;
        if(textNode == undefined)
        {
          textNode = theNodes[i_node].text;
        }
        var theElement=document.createTextNode(textNode);
      }
      var newParent = parentNode.appendChild(theElement);
      
      this.writeNodes(newParent,theNodes[i_node].childNodes);
    }
  }
}

