﻿

/**
 * This script is part of the element4solution framework - http://www.element4solution.org
 * (C)door2solution software gmbh
 *
 * Rev 20070321 RS Mozilla Compatibility
 * Rev 20070719 RS getFormParams() - undefined values
 * 
 * $LastChangedDate: 2010-01-22 19:32:21 +0100 (Fr, 22 Jän 2010) $
 * $LastChangedRevision: 5590 $
 * $LastChangedBy: norbert $
 */

var console = null;

function TRACE(data)
{
   if (console != null)
   {
      var newline = document.createElement("div");
      console.appendChild(newline);
      var txt = document.createTextNode(data);
      newline.appendChild(txt);
   }
}




/**
 * Gets the Ajax object.
 */
function e4s_initXmlHttpReq()
{
   var xRequest = null;
 
   if (window.XMLHttpRequest)
   {
      xRequest = new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      xRequest = new ActiveXObject("Microsoft.XMLHTTP");
   }

   return xRequest;
}



/**
 * Removes all element from the specified node
 */
function removeAllElements(element)
{

   if (element) 
   {
      if (element.hasChildNodes())
      { 
         for( var i = element.childNodes.length - 1; i >= 0; i-- )
         {
            // removeAllElements(element.childNodes[i]);
            element.removeChild(element.childNodes[i]);
         }
      }
   }
}



/**
 * Get the form parameters in Cgi-notation
 */
function getFormParams( frm )
{
   var res = '';
   var i = frm.elements.length;
		
   while( i > 0 )
   {
      i = i - 1;

      if (frm.elements[i].name)
      {
         var varname = frm.elements[i].name;

         if ((varname != null) && (varname != ""))
         {		
            var vartype = frm.elements[i].type;
	    var varvalue = "";

            if (vartype == "select-one")
            {
               var Index = frm.elements[i].options.selectedIndex;
	       if (Index >= 0)
                  varvalue = frm.elements[i].options[Index].value;
            } 
            else if (vartype == "checkbox")
            {
               if (frm.elements[i].checked == true)
	          varvalue = 1; 
               else
                  varvalue = 0;
            } 
	    else
            {
               varvalue = frm.elements[i].value; 
            }	
  
            res = res + '&' + varname + '=' + escape(varvalue);
         }
      }
   } 

   return res;
}

/**
 * Takes an url param string and removes '&' from the beginning if present.  
 * (The getFormParams() function produces strings starting with "&". This is not according
 * to the http specification for POST requests. 
 */
function checkParams(params)
{
   /*
   if (params == null || typeof(params)=="undefined" || params == '') 
   {
      //it is better to have an empty param string than to have a param string like 'undefined' or 'null'
      return '';	
   }
	
   if (params.charAt(0) == '&') 
   {
      params = params.substr(1);
   }
   */
 
   return params;
}




/**
 * Holds information about an attribute, e.g. <TABLE BORDER=1> then the attribute name = BORDER and the
 * attribute value = 1
 */
function E4SAttribute(attr_name,attr_val)
{
   this.m_name = attr_name;
   this.m_val = attr_val;

   this.toString = attributeToString;
}




/**
 * Holds information about a html tag, e.g. "<P ALIGN=LEFT>This Text</P>" then the tag-name = P, the
 * attribute array contains one element (ALIGN=LEFT) and the m_tags element contains one array entry
 * containing a new tag having m_data set to "This Text". The m_tags array includes all nested tags.
 */
function E4STag(tag_name,attributes)
{
   this.m_tags = new Array();

   if (tag_name)
      this.m_name = tag_name.toLowerCase();
   else
      this.m_name = '';

   this.m_data = '';
   
   this.toString = tagToString;
   this.tagInit = tagInit;
   this.renderTag = renderTag;
   this.debug = TagDebug;
   this.getClassName = getClassName;
   this.tagToSelectOptions = tagToSelectOptions;
	   
   if (attributes)
      this.m_attributes = attributes;   
   else
      this.m_attributes = new Array();

   // TRACE(this.toString());
}


function getClassName()
{
   return 'TEST';
}


function attributeToString()
{
   return this.m_name + "=\"" + this.m_val + "\"";
}


function tagToSelectOptions( selectElem )
{
   var n = selectElem.options.length;
   var i = 0;
   var k = 0;


   var selected_idx = selectElem.options.selectedIndex;
   var selected_value = "";

   if (selected_idx >= 0)
      var selected_value = selectElem.options[selected_idx].text;

   for( i = n - 1; i >= 0; i-- )
      selectElem.options[i] = null;

   if (this.m_tags.length > 0)
   {
      for(i = 0; i < this.m_tags.length; i++)
      {
          var opt = this.m_tags[i];

          if ((opt.m_name.toUpperCase() == 'OPTION') && (opt.m_tags.length == 1))
          {
             var val = "";

             for( iA = 0; iA <  opt.m_attributes.length; iA++ )
             {
                var a = opt.m_attributes[iA];
                if (a.m_name.toUpperCase() == "VALUE")
                   val = a.m_val;
             }

            selectElem.options[k] = new Option(opt.m_tags[0].m_data,val);
            k++;
         }
      }

      for(i = 0; i < selectElem.options.length; i++)
      {
         if (selected_value == selectElem.options[i].text)
         {
            selectElem.options.selectedIndex = i;
            break;
         }
      }
   }
}         


function renderTag( baseelement )
{
   var resDebug = '';

   var node;

   if (this.m_name && (this.m_name != ''))
   {
      node = document.createElement(this.m_name);
      // node.className = this.getClassName();

      resDebug = resDebug + "<" + this.m_name;

      var isNetscape = (navigator.appName == "Netscape");

      if (this.m_attributes.length > 0)
      {
         for( j = 0; j < this.m_attributes.length; j++ )
         {
            var a = this.m_attributes[j];

            if ((! isNetscape) && (node.attributes.length > 0))
            {
               var checkname = node.attributes[a.m_name];

               if (! checkname)
	       {
                  name_lc = a.m_name.toLowerCase();

                  for( k = 0; k < node.attributes.length; k++ )
                  {
                     if (node.attributes[k].nodeName.toLowerCase() == name_lc)
                     {
                        checkname = node.attributes[k].nodeName;
                        break;
                     }
                  }
	       }
               
               if (! checkname)
	       {
                  var errMsg = "Invalid attribute " + a.m_name + "=" + a.m_val + " for <" + this.m_name + ">\n\n";
                  var errMsg2 = '';

                  errMsg = errMsg + "Possible attributes are:\n";
                    
                  
                  for( k = 0; k < node.attributes.length; k++ )
                  {
                     errMsg2 = errMsg2 + node.attributes[k].nodeName + " ";

                     if ((k == node.attributes.length - 1) || (errMsg2.length > 80))
                     {
                        errMsg = errMsg + "  " + errMsg2 + "\n";
                        errMsg2 = '';
                     }
                  }

                  alert(errMsg);
               }
               else
               {
                  node.setAttribute(a.m_name,a.m_val);
                  resDebug = resDebug + " " + a.m_name + "=" + a.m_val;
               }
	    }
            else
            {
               // Mozilla
               node.setAttribute(a.m_name,a.m_val);
               resDebug = resDebug + " " + a.m_name + "=" + a.m_val;
            }
         }
      }

      resDebug = resDebug + ">";
   }
   else if (this.m_data && (this.m_data != ''))
   {
      node = document.createElement("DIV");
      var txt = document.createTextNode(this.m_data);
      node.appendChild(txt);
      resDebug = resDebug + this.m_data;
   }
   else 
   {
      node = document.createElement("DIV");
      // node=basenode - try this!!
   } 

   if (node)
   {
      baseelement.appendChild(node);
   
      if (this.m_tags.length > 0)
      {
         var i;
 
         for(i = 0; i < this.m_tags.length; i++)
         {
            resDebug = resDebug + this.m_tags[i].renderTag(node);
         }
      }

   }

   return resDebug;
}


function tagToString()
{
   var s = '';

   if (this.m_name)
   {
      s = "<" + this.m_name;

      if (this.m_attributes.length > 0)
      {
         for (j = 0; j < this.m_attributes.length; j++)
         {
            s = s + " " + this.m_attributes[j].toString();
         }
      }

      s = s + ">";
   }
 
   return s;
}

function TagDebug()
{
   var sTag = this.toString();

   if (sTag != '')
      TRACE(sTag); 

   if (this.m_data != '')
      TRACE(this.m_data);

   if (this.m_tags.length > 0)
   {
      var i;
 
      for(i = 0; i < this.m_tags.length; i++)
      {
         this.m_tags[i].debug();
      }
   }

   if (this.m_name)
      TRACE("</" + this.m_name + ">");
}


function tagInit(data,level)
{
   this.m_tags = new Array();

   if (! level)
      level = 1;

   if (data && (data != '')) 
   {
      var len = data.length;

      var intag = false;
      var intag_name = false;
      var inquote = '';
      var indata = false;
      var inattr = '';
      var tag_name = '';
      var attribute_name = '';
      var attribute_value = '';
      var attributes = new Array(0);
      var i = 0;
      var ch = '';
      var n = 1;
      var iEnd = 0;
      var subData = '';
      var data2 = '';
      var tag_data = '';

      for( i = 0; i < len; i++ )
      {
         var ch = data.charAt(i);

   	 // TRACE(level + ' ['+i+'] \'' + ch + '\' A:' + inattr + ' Q:' + inquote + ' T:' + intag + ' TN:' + intag_name + ' AN:' + attribute_name + ' AV:' + attribute_value);

         if ((ch == 13) || (ch == 10) || (ch == '\n'))
         {
            // ignore
         }
         else if ((ch == '<') && (intag == false))
         {
            intag = true;
            intag_name = true;

            if (tag_data != '')
            {
               var tData = new E4STag();
               tData.m_data = tag_data;

               this.m_tags[this.m_tags.length] = tData;
               tag_data = '';
            }
         }
         else if ((intag == true) && (inquote == '') && (ch == '>'))
         {
            if ((inattr != '') && (attribute_name != ''))
            {
               // TRACE("*2A: " + attribute_name + "=" + attribute_value);

               var a = new E4SAttribute(attribute_name,attribute_value);
               attributes[attributes.length] = a;
            }

            if (tag_data != '')
            {
               var tData = new E4STag();
               tData.m_data = tag_data;

               this.m_tags[this.m_tags.length] = tData;
               tag_data = '';
            }

            var tTag = new E4STag(tag_name,attributes);
            attributes = new Array();

            this.m_tags[this.m_tags.length] = tTag;

            data2 = data.substring(i);
            iEnd = i + data2.indexOf("</" + tag_name + ">");

            if ((iEnd != -1) && ((iEnd - i - 1) > 0))
            {
               subData = data.substr(i + 1,iEnd - i - 1);

  	       tTag.tagInit(subData,level + 1);
  
               i = iEnd + 3 + tag_name.length - 1;
               // TRACE(level + "." + n + ": i="+i + " len=" + len + " " + data);
            }

            n = n + 1;

            intag = false;
            intag_name = false;
            inattr = '';
            tag_name = '';
            attribute_name = '';
            attribute_value = '';
         }
         else if ((intag_name == true) && (ch == ' '))
         {
            intag_name = false;
            inattr = 'N';
         }
         else if ((intag_name == true) && (ch != ' '))
         {
            tag_name = tag_name + ch;
         }
         else if ((intag_name == true) && (ch == ' '))
	 {
            inattr = 'N';
         }
         else if (inattr == 'N')
         {
            if (ch == '=')
               inattr = 'V';
            else if (ch != ' ')
               attribute_name = attribute_name + ch;
         }
         else if (inattr == 'V')
         {
            if ((inquote == '') && ((ch == '\"') || (ch == '\'')))
               inquote = ch;
            else if (inquote == ch)
               inquote = '';
            else if ((inquote == false) && (ch == ' '))
            {
               // TRACE("*2B: " + attribute_name + "=" + attribute_value);

               var a = new E4SAttribute(attribute_name,attribute_value);
               attributes[attributes.length] = a;

               inattr = 'N';
               attribute_name = '';
               attribute_value = '';
            }
            else
               attribute_value = attribute_value + ch;
         }
         else
         {
	    tag_data = tag_data + ch;
         }
      }
   }
   
   if (tag_data != '')
   {
      var tData = new E4STag();
      tData.m_data = tag_data;

      this.m_tags[this.m_tags.length] = tData;
   }

   return true;
}

