AJX Tools (AJX) : Ajax Library « Ajax Layer « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Ajax Layer » Ajax Library 
AJX Tools (AJX)


/*******************************************************************************
 *
 * AJX Tools (AJX)
 *
 * Developers: 
 *   - Tales Viglioni (talesviglioni@gmail.com)
 *   - Cristopher Sanches (cristopherms@gmail.com)
 *
 * Version: 1.0
 * Date: 06/26/2006
 
 * Copyright (c) 2006 T&C.
 * All rights reserved.
 *
 * AJX Tools is distributable under the terms of an GNU Library or 
 * Lesser General Public License (LGPL).
 *
 * http://ajxtools.sourceforge.net/
 
AJXtools
   |
   +- AJXload()
   |
   +- AJXgetHTTPObject()
   |
   +- AJXstatus()
   |
   +- AJXsetWaitMessage()
   |   
   +- AJXgetWaitMessage()
   |   
   +- AJXsetLoadingMessage()
   |   
   +- AJXgetLoadingMessage()

     
AJXapplication
   |
   +- AJXpopulateCombo()
   |
   +- AJXaddTableRow()
   |
   +- AJXupdateTableRow()
   |
   +- AJXremoveTableRow()
   |   
   +- AJXgetResponseText()   
   |
   +- AJXgetResponseXML()   
   |
   +- AJXautoComplete()      
   |
   +- AJXfillDiv()      
   |
   +- AJXcleanDiv()      
   |
   +- AJXautoComplete()
         
         
AJXutil
   |
   +- AJXfindPosX()
   |
   +- AJXfindPosY()
   |
   +- AJXvalidateRequired()
   |   
   +- AJXgetObjectName()
   |   
   +- AJXgetFormValues()
   |
   +- AJXshowMessage()
   |
   +- AJXexistsCssClass()
   |
   +- AJXtokenizer()
   |   
   +- AJXtrim()
   |   
   +- AJXRTrim()
   |   
   +- AJXLTrim()  
 ******************************************************************************/
  
/*******************************************************************************
 * AJXtools()
 * AJX Tools main class
 *
 ******************************************************************************/
function AJXtools()
{
   var waitMessage = "";
   var loadingMessage = "";
   var _this = this;
  var util = new AJXutil();
  
  this.AJXresponseXML=null;
  this.AJXresponseText=null;  
  this.AJXload = AJXload;
  this.AJXsetWaitMessage = AJXsetWaitMessage;
  this.AJXgetWaitMessage = AJXgetWaitMessage;
  this.AJXsetLoadingMessage = AJXsetLoadingMessage;
  this.AJXgetLoadingMessage = AJXgetLoadingMessage;

  /**
   * AJXload()
   * Method to load page thru GET or POST form methods.
   *
   * Parameters:
   *   - url: url to submition. (required)
   *   - objAJXapplication: instance of a object to execute the 
   *                        method "nextMethod". (required)
   *   - nextMethod: method of "objAJXapplication" called after 
   *                 send data. (required)
   *   - form: form submited. Case form exists, open method uses POST. 
   *           Otherwise, open method uses GET. (optional)
   *   - isAsync: configure if the call method will execute synchronous (false)
   *              or asynchronous (true). The default value is true. (optional)
   *   - returnXML: configure if the return will load AJXresponseXML (true) or 
   *                AJXresponseText (false). The default value is true. (optional)
   *
   */
  function AJXload(url, objAJXapplication, nextMethod, form, isAsync, returnXML)
  {
     var AJXhttp = AJXgetHTTPObject();  
    var data = null;
    var returnType = null;
    
    if (returnXML==false)
    {
      returnType = "text";
    }
    else
    {
      returnType = "xml";
    }

    if (isAsync != false)
    {
      isAsync = true;
    }
    
    if(form)
    {
      data = util.AJXgetFormValues(form);      
      AJXhttp.open("POST",url,isAsync);
    }
    else
    {
      AJXhttp.open("GET",url,isAsync);
    }  
    
    AJXhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    util.AJXshowMessage(AJXgetWaitMessage());    
    AJXhttp.send(data);
  
    if (isAsync)
    {
      AJXhttp.onreadystatechange = function() 
        {AJXstatus(AJXhttp, objAJXapplication, nextMethod, isAsync, returnType);};    
    }
    else
    {
      AJXstatus(AJXhttp, objAJXapplication, nextMethod, isAsync, returnType);
    }
  }   

  /**
   * AJXstatus()
   * Verifies the status of the request. 
   *
   * Parameters:
   *   - http: instance of XMLHttpRequest object. (required)
   *   - objExec: instance of a object to execute the 
   *              method "nextMethod". (required)
   *   - methodExec: method of "objExec" called after responseXML. (required)
   *   - isAsync: inform if the call was synchronous (false) or 
   *              asynchronous(true). (required)
   *   - returnType: configure the return to responseText or 
   *                 responseXML. (required)
   *
   */
  function AJXstatus(http, objExec, methodExec, isAsync, returnType)
  {  
    if(http.readyState <= 3)
    {
      util.AJXshowMessage(AJXgetLoadingMessage());
    }      
    
    if(http.readyState == 4)
    {
      if (http.status == 200)
      {
        util.AJXshowMessage("");

        if (returnType=="text")
        {
          _this.AJXresponseText = http.responseText;
        }
        else
        {
          _this.AJXresponseXML = http.responseXML;
        }

        if (isAsync)
        {
          if ((objExec!=null)&&(methodExec!=null))        
          {
            eval("objExec."+methodExec);
          }
          _this.AJXresponseXML = null;
          _this.AJXresponseText = null;
        }
      }
      else
      {
        util.AJXshowMessage("Error code: " + http.status);
      }
    }
  }  

  /**
   * AJXgetHTTPObject() 
   * Create the XMLHttpRequest object.
   
   */
  function AJXgetHTTPObject() 
  {
    var xmlhttp;

    // If the user is using Mozilla/Firefox/Safari
    if (window.XMLHttpRequest
    {
      xmlhttp = new XMLHttpRequest();    
      xmlhttp.overrideMimeType('text/xml');
    }  
    // If the user is using IE    
    else if (window.ActiveXObject
    {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
  }  

  /**
   * AJXgetWaitMessage() 
   * Gets the value of waitMessage attribute.
   
   */
    function AJXgetWaitMessage()
    {
        if (waitMessage == "")
        {
          AJXsetWaitMessage("Wait...");
        }
        return waitMessage;
    }

  /**
   * AJXsetWaitMessage() 
   * Sets the value of waitMessage attribute.
   
   */
    function AJXsetWaitMessage(waitMsg
    {
        waitMessage = waitMsg;
    }  

  /**
   * AJXgetLoadingMessage() 
   * Gets the value of loadingMessage attribute.
   
   */
    function AJXgetLoadingMessage()
    {
        if (loadingMessage == "")
        {
          AJXsetLoadingMessage("Loading...");
        }
        return loadingMessage;
    }

  /**
   * AJXsetLoadingMessage() 
   * Sets the value of loadingMessage attribute.
   
   */
    function AJXsetLoadingMessage(loadingMsg
    {
        loadingMessage = loadingMsg;
    }  
}

/*******************************************************************************
 * AJXapplication()
 * AJX Tools application class.
 *
 ******************************************************************************/
function AJXapplication()
{
  var AJXexecuting = false;
  var util = new AJXutil();
  var ajx = new AJXtools();

  this.AJXpopulateCombo = AJXpopulateCombo;
  this.AJXaddTableRow = AJXaddTableRow;
  this.AJXupdateTableRow = AJXupdateTableRow;
  this.AJXgetResponseText = AJXgetResponseText;
  this.AJXgetResponseXML = AJXgetResponseXML;
  this.AJXremoveTableRow = AJXremoveTableRow;
  this.AJXfillDiv = AJXfillDiv;
  this.AJXcleanDiv = AJXcleanDiv;
  this.AJXautoComplete = AJXautoComplete;

  /**
   * AJXpopulateCombo()
   * Populate a combo box with the values from a xml element.
   
   * Parameters:
   *  - url: url to submition. (required)
   *  - form: submited form if html form method is "post". (optional)
   *   - repeatedElement: xml repeated tag (required)
   *  - comboName: name of combo to populate (required)
   *   - valueTag: xml tag containing the "value" parameter of html
   *          "option" tag (required)
   *   - textTag: xml tag containing the text to combo (required)repeatedElement
   *  - functionAfterExecution: function called after the execution 
   *                of AJXpopulateCombo function (optional)
   *
   * Expected XML element in AJXresponseXML:
   *
   *  <root (repeatedElement*)>
   *    <repeatedElement(valueTag, textTag)>
   *    ...
   *  </root>
   *
   */
  function AJXpopulateCombo(url, form, repeatedElement, comboName, valueTag, textTag, functionAfterExecution)
  {
    if (!util.AJXvalidateRequired(url,repeatedElement, comboName, valueTag, textTag))
    {
      return false;
    }

    if (!AJXexecuting && ajx.AJXresponseXML==null)
    {
      AJXexecuting = true;
      executeMethod = "AJXpopulateCombo(\""+url+"\",\""+form+"\",\""+repeatedElement+"\",\""+comboName+"\",\""+valueTag+"\",\""+textTag+"\",\""+functionAfterExecution+"\");";
      ajx.AJXload(url,this,executeMethod,form);
      AJXexecuting = false;
      executeMethod = "";
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML!=null)
    {
      var opt;
      var XMLelement = ajx.AJXresponseXML.getElementsByTagName(repeatedElement)
      var cmb = document.getElementsByName(comboName)[0]

      for(j=cmb.length; j >= ; j--
      {
        cmb.remove(j);
      }    
      
      for(i=; i < XMLelement.length ; i++
      {
        opt = document.createElement("option");
        opt.text = XMLelement[i].getElementsByTagName(textTag)[0].firstChild.data; 
        opt.value = XMLelement[i].getElementsByTagName(valueTag)[0].firstChild.data; 
      
        try 
        {
          cmb.add(opt, null)// standards compliant; doesn't work in IE
        }
        catch(ex
        {
          cmb.add(opt)// IE only
        }    
      }  
      eval(functionAfterExecution);
    }
  }
  
  /**
   * AJXaddTableRow()
   * Add rows in a table with values from a xml element. Each repeated child 
   * represents a table row, and each child in repeated element, represents a 
   * table cell.
   
   * Parameters:
   *  - url: url to submition. (required)
   *  - form: submited form if html form method is "post". (optional)
   *   - repeatedElement: xml repeated tag. (required)
   *  - idTable: id of HTML table tag to add row. (required)
   *   - rowsPosition: first position after table headers. (required)
   *  - message: message showed if not found any "repeatedElement" tag. (optional)
   *  - clearTable: case "Y", remove all table rows begining in "rowPosition". (optional)
   *  - functionAfterExecution: function called after execution of AJXaddTableRow() method, if exists some "repeatedElement" tag. (optional)
   *  - functionAlwaysExecuted: function always called after execution of AJXaddTableRow() method. (optional)
   *
   * Expected XML element in AJXresponseXML:
   *
   *  <root (repeatedElement*)>
   *    <repeatedElement(child1, child2, ... ,childN)>
   *    ...
   *  </root>
   *
   */
  function AJXaddTableRow(url, form, repeatedElement, idTable, rowsPosition, message, clearTable, functionAfterExecution, functionAlwaysExecuted)
  {
    if (!util.AJXvalidateRequired(url, repeatedElement, idTable, rowsPosition))
    {
      return false;
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML==null)
    {
      AJXexecuting = true;
      executeMethod = "AJXaddTableRow(\""+url+"\",\""+form+"\",\""+repeatedElement+"\",\""+idTable+"\",\""+rowsPosition+"\",\""+message+"\",\""+clearTable+"\",\""+functionAfterExecution+"\",\""+functionAlwaysExecuted+"\")";
      ajx.AJXload(url,this,executeMethod,form);
      AJXexecuting = false;
      executeMethod = "";
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML!=null)
    {  
      var aTable;
      var aRow;
      var aCell;
      var childXMLelement;
      var XMLelement = ajx.AJXresponseXML.getElementsByTagName(repeatedElement);
      var aTable = document.getElementById(idTable);
      
      if (clearTable == "Y")
      {      
        if (aTable.rows.length > posrowsPosition)
        {
          var aLength = aTable.rows.length
          for(i=rowsPosition ; i < aLength; i++
          {          
            aTable.deleteRow(rowsPosition);
          }
        }
      }
  
      if (XMLelement.length == 0)
      {
        if (message != "")
        {
          alert(message);
        }
      }
      else
      {      
        for(i=; i < XMLelement.length ; i++
        {
          childXMLelement = XMLelement[i].childNodes;        
          aRow = aTable.insertRow(aTable.rows.length);
          var k = 0;
          for(j=; j < childXMLelement.length ; j++
          {
            if (childXMLelement[j].nodeType==1)
            {
              aCell = aRow.insertCell(k);            
              if (childXMLelement[j].firstChild!=null)
              {
                aCell.innerHTML = childXMLelement[j].firstChild.nodeValue;
              }
              else
              {
                aCell.innerHTML = "&nbsp;";
              }
              k++;
            }
          }
        }
        eval(functionAfterExecution);
      }
      eval(functionAlwaysExecuted);
    }
  }  

  /**
   * AJXupdateTableRow()
   * Update rows in a table with values from a xml element. Each repeated child 
   * represents a table row, and each child in repeated element, represents a 
   * table cell.
   
   * Parameters:
   *  - url: url to submition. (required)
   *  - form: submited form if html form method is "post". (optional)
   *   - repeatedElement: xml repeated tag. (required)
   *  - idTable: id of HTML table tag to update row. (required)
   *  - rowKey: table column(s) index(es) used as a unique identifier(s) of a table row.
   *        Case the "key" have more then one column, the values must be separated by commas.
   *        For example: rowKey="2" or rowKey="1,3,5".  
   *  - message: message showed if not found any "repeatedElement" tag. (optional)
   *  - functionAfterExecution: function called after execution of AJXaddTableRow() method, if exists some "repeatedElement" tag. (optional)
   *  - functionAlwaysExecuted: function always called after execution of AJXaddTableRow() method. (optional)
   *
   * Expected XML element in AJXresponseXML:
   *
   *  <root (repeatedElement*)>
   *    <repeatedElement(child1old, child2old, ... ,childNold)>
   *    <repeatedElement(child1new, child2new, ... ,childNnew)>   
   *    ...
   *  </root>
   *
   */   
  function AJXupdateTableRow(url, form, repeatedElement, idTable, rowKey, message, functionAfterExecution, functionAlwaysExecuted)
  {
    if (!util.AJXvalidateRequired(url, repeatedElement, idTable, rowKey))
    {
      return false;
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML==null)
    {
      AJXexecuting = true;
      executeMethod = "AJXupdateTableRow(\""+url+"\",\""+form+"\",\""+repeatedElement+"\",\""+idTable+"\",\""+rowKey+"\",\""+message+"\",\""+functionAfterExecution+"\",\""+functionAlwaysExecuted+"\")";
      ajx.AJXload(url,this,executeMethod,form);
      AJXexecuting = false;
      executeMethod = "";
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML!=null)
    {  
      var aTable;
      var childXMLelement;
      var XMLelement = ajx.AJXresponseXML.getElementsByTagName(repeatedElement);    
      var aTable = document.getElementById(idTable);
      var aIndex;
      var aExecute;
      var aHtml;
      
      if (XMLelement.length == 0)
      {
        if((message!= "undefined"&& (message != ""))
        {
          alert(message);
        }
      }
      else
      {    
        aKeys = util.AJXtokenizer(rowKey,',');
        aBiggerKey = 0;
        for(k=; k < aKeys.length ; k++
        {  
          if (aKeys[k> aBiggerKey
          {
            aBiggerKey = aKeys[k];
          }
        }
        
        for(i=; i < XMLelement.length ; i+=2
        {
          childXMLelement = XMLelement[i].childNodes;
          
          for(j=; j < aTable.rows.length ; j++
          {  
            aExecute = false;        
            var k = new Number(0);
            
            if (navigator.appName == "Netscape")
            {
              k=1;
            }
  
            for(y=; y < aKeys.length ; y++
            {    
              if (aTable.rows[j].cells.length - >= aBiggerKey)
              {
                if (childXMLelement[(parseInt(aKeys[y],10)+k)].nodeType==1)
                {
                    aHtml = aTable.rows[j].cells[aKeys[y]].innerHTML;
                  aHtml = aHtml.replace(/\n/gi, "");

                  if (util.AJXtrim(aHtml== childXMLelement[(parseInt(aKeys[y],10)+k)].firstChild.nodeValue)
                  {        
                    aExecute = true;
                    aIndex = j;
                  }
                  else
                  {
                    aExecute = false;
                    aIndex = null;
                  }                
                }
                else
                {
                  k++;
                  y--;
                }
              }
            }

            if (aExecute
            {
              childXMLelement = XMLelement[i+1].childNodes;
              var k = new Number(0);
              
              for (x=; x < aTable.rows[aIndex].cells.length; x++)
              {
                if (childXMLelement[x+k].nodeType==1)
                {  
                  
                  aTable.rows[aIndex].cells[x].innerHTML = childXMLelement[x+k].firstChild.nodeValue;
                }
                else
                {
                  k++;
                  x--;
                }
              }            
              aIndex=null;
            }
          }
        }
        eval(functionAfterExecution);
      }
      eval(functionAlwaysExecuted);
    }
  }
  
  /**
   * AJXremoveTableRow()
   * Remove rows from a table with values from a xml element. Each repeated child 
   * represents a table row, and each child in repeated element, represents a 
   * table cell.
   
   * Parameters:
   *  - url: url to submition. (required)
   *  - form: submited form if html form method is "post". (optional)
   *   - repeatedElement: xml repeated tag. (required)
   *  - idTable: id of HTML table tag to remove row. (required)
   *  - rowKey: table column(s) index(es) used as a unique identifier(s) of a table row.
   *        Case the "key" have more then one column, the values must be separated by commas.
   *        For example: rowKey="2" or rowKey="1,3,5".  
   *  - message: message showed if not found any "repeatedElement" tag. (optional)
   *  - functionAfterExecution: function called after execution of AJXaddTableRow() method, if exists some "repeatedElement" tag. (optional)
   *  - functionAlwaysExecuted: function always called after execution of AJXaddTableRow() method. (optional)
   *
   * Expected XML element in AJXresponseXML:
   *
   *  <root (repeatedElement*)>
   *    <repeatedElement(child1, child2, ... ,childN)>   
   *    ...
   *  </root>
   *
   */   
  function AJXremoveTableRow(url, form, repeatedElement, idTable, rowKey, message, functionAfterExecution, functionAlwaysExecuted)
  {
    if (!util.AJXvalidateRequired(url, repeatedElement, idTable, rowKey))
    {
      return false;
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML==null)
    {
      AJXexecuting = true;
      executeMethod = "AJXremoveTableRow(\""+url+"\",\""+form+"\",\""+repeatedElement+"\",\""+idTable+"\",\""+rowKey+"\",\""+message+"\",\""+functionAfterExecution+"\",\""+functionAlwaysExecuted+"\")";
      ajx.AJXload(url,this,executeMethod,form);
      AJXexecuting = false;
      executeMethod = "";
    }
    
    if (!AJXexecuting && ajx.AJXresponseXML!=null)
    {
      var aTable;
      var childXMLelement;
      var XMLelement = ajx.AJXresponseXML.getElementsByTagName(repeatedElement);
      var aTable = document.getElementById(idTable);
      var aIndex;
      var aExecute;
      var aHtml;
      
      if (XMLelement.length == 0)
      {
        if((message!= "undefined"&& (message != ""))
        {
          alert(message);
        }
      }
      else
      {  
        aKeys = util.AJXtokenizer(rowKey,',');
        aBiggerKey = 0;
        
        for(k=; k < aKeys.length ; k++
        {  
          if (aKeys[k> aBiggerKey
          {
            aBiggerKey = aKeys[k];
          }
        }
        
        for(i=; i < XMLelement.length ; i++
        {
          childXMLelement = XMLelement[i].childNodes;
  
          for(j=; j < aTable.rows.length ; j++
          {
            aExecute = false;
            var k = new Number(0);
            
            if (navigator.appName == "Netscape")<