
 /*
  *   CopyRight (C) 2007 unsignal.com dev@unsignal.com 
  *   http://unsignal.com/ - All Rights Reserved.
  *
  *   PAJAX - Practical Asynchronous Javascript And XML
  *   $ Last Revision: 05/30/2007 $ -andrei
  *   
  *   This script is part of the pAjax project. The pAjax project is
  *   free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation; either version 2 of the License, or
  *   (at your option) any later version.
  *
  *   The GNU General Public License can be found at
  *   http://www.gnu.org/copyleft/gpl.html.
  *   A copy is found in the textfile GPL.txt and important notices to the license
  *   from the author is found in LICENSE.txt distributed with these scripts.
  *
  *   This script is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *   GNU General Public License for more details.
  *
  *   This copyright notice MUST APPEAR in all copies of the script! 
  * 
  *   @ Author: Andrei Rosseti
  *   @ File: ajax.js
  *   @ Desc: Pajax Javascript Library 
  * 
  */
 
var list  = Array();
var req   = false;
    pajax = new Object();

/* 
 * in_array - Find in arrayMixed the value "findStr" 
 * (Boolean) 
 */
function in_array(arrayMixed, findStr)
{
  var i = 0;
  
  for (i in arrayMixed)
  {
    if (arrayMixed[i] == findStr)
    return true;
    
    i++;
  }
  
  return false;
}

pajax = 
{
  
  /* **************************************************************** 
   * LOAD CONFIGURATION
   * **************************************************************** */     
  load: 
  {
  
    /*
     * message: set the message to show in the "box-container"
     * (String)       
     */       
    message: "Loading...",
    
    /*
     * target: it defines the id of "box-container" to insert into (this.message)
     * (String)       
     */       
    target: "loadingPajax0x6d0x640x78",
    
    /*
     * time: it defines the time that the message will stay on display
     * (Integer, in miliseconds - 1s = 1000, 0.5s = 500)        
     */       
    time: 500,
    
    /*
     * show: define true to show the "box-container" or false to hide.
     * (Boolean)        
     */       
    show: false,

    /* 
     * destroy - Destroy the DIV::default or hide your setted id container. 
     */
    destroy: function()
    {
      targetId = this.target;
      showMessage = this.message;
        
      var theDiv = document.getElementById(targetId);
              
      if (targetId == "loadingPajax0x6d0x640x78")
        setTimeout(function () {
        
          if (document.getElementById(targetId)) 
            document.getElementsByTagName('body')[0].removeChild(document.getElementById(targetId));
           
        }, this.time);
      else
        setTimeout(function () {
        
          if (document.getElementById(targetId)) 
            theDiv.style.display = "none";
             
        }, this.time);
    },
    
    /* 
     * init - Callback to show the loading "box-container"             
     */
    init: function()
    {
      targetId = this.target;
      showMessage = this.message;
        
      if (!document.getElementById(targetId))
      {
        var newDiv = document.createElement('div');
            newDiv.style.backgroundColor = "#FFFC88";
            newDiv.style.fontFamily = "Tahoma";
            newDiv.setAttribute('id', targetId);
            newDiv.id = targetId;
            newDiv.style.fontSize = "12px";
            newDiv.style.fontWeight = "bold";
            newDiv.style.padding = "5px";
            newDiv.style.width = "165px";
            newDiv.style.position = "absolute";
            newDiv.style.right = "0px"; 
            newDiv.style.top = document.getElementsByTagName('html')[0].scrollTop + "px";
            newDiv.style.marginTop = "0px";
            newDiv.style.border = "1px solid #444444";
            newDiv.style.borderRight = "0px"; newDiv.style.borderTop = "0px";
            newDiv.style.zIndex = "700"; 
            newDiv.innerHTML = showMessage;
        document.getElementsByTagName('body')[0].appendChild(newDiv);
      } else
      {
        var theDiv = document.getElementById(targetId);
            theDiv.style.display = "block";
            theDiv.innerHTML = showMessage;
      }
    }
    
  },
  /* **************************************************************** */
    
  /*
   * getForm - Proccess FORM information (serialize)
   * 
   * @formElement: ID of the FORM or FORM object      
   */   
  getForm: function(formElement)
  {
    var name, total, value = null;
    var i      = 0;
    var checktypes = [ "checkbox", "radio" ];
    
    if (typeof(formElement) != "object")
      formElement = document.getElementById(formElement);
      
    var frmlen = formElement.length;      
  
    for (i = 0; i < frmlen; i++)
    {
      if (in_array(checktypes, formElement[i].type) && !formElement[i].checked)
        continue;
                 
      if (formElement[i].name && formElement[i].value)
      {
        name   = formElement[i].name;
        value  = escape(formElement[i].value);
        total += "$" + escape(name) + "=" + escape(value);
      }
    }
    
    total += "$";
  	 
    return total;
  },
  /* **************************************************************** */
  
  /* 
   * send - Add calls to array.
   */
  send: function(url, post_data, wndproc)
  {
    list.push(Array(url, post_data, wndproc));
  	if (list.length == 1) 
        this.__send(url, post_data, wndproc);    
  },
  /* **************************************************************** */
  
  /* 
   * __send - Send the Ajax POST-data to the server.                
   */
  __send: function (url, post_data, wndproc){
  	if(window.XMLHttpRequest) {
  		req = new XMLHttpRequest();
  		req.open("POST",url,true);
  		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  		req.send(post_data);
  	}
  	else {
  		req = new ActiveXObject('Microsoft.XMLHTTP');
  		req.open("POST",url,true);
  		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  		req.send(post_data);
  	}
  	
      req.onreadystatechange = this.processReqChangeWndProc;
  },
  /* **************************************************************** */
  
  /* 
   * processReqChangeWndProc - Processing the received data
   */
  processReqChangeWndProc: function()
  {
    var request = req;
    
    if (request) 
    {
      if (request.readyState == 2) 
      {
        if (pajax.load.show == true)
          pajax.load.init();
        
        } else if (request.readyState == 4) 
        {
          if (pajax.load.show == true)
            pajax.load.destroy();
 			
          
          if ((request.status == 200)||(request.status == 0)) 
          {
              callwndproc = list[0][2];
              callwndproc(request);
              list.shift();
          	    				
          	if (list.length)
                pajax.__send(list[0][0], list[0][1], list[0][2]);
          }
          else 
            alert("Problem when getting the data:\n" + req.statusText);
          
       }
     }
  }
  
};

