/*
 * ajax_manager.js
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This 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.
 *
 * This software 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.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*
	Requires: array_enh.js

*/

function ajax_int_show_indicator ( visible )
{
	var d = document.getElementById ( 'ajax_indicator' );
	if ( ! d ) return;

	if ( visible )
		d.style.visibility = "inherit";
	else
		d.style.visibility = "hidden";
	
}

// PUBLIC: ajax_response
function ajax_int_base_reqchange ( req, callback, easy, err_handler )
{
	if ( ! easy ) 
	{
		if ( callback ) callback ( req );
	} else {
		// Remove the req from the Request Pool
		if ( req.readyState == 4 ) 
		{
			if ( req.responseText.LStrip ().startsWith ( "var ajax" ) )
			{
				try {
					eval ( req.responseText );
					if ( ajax_response [ 'err_code' ] )
					{
						if ( ajax_response [ 'err_descr' ] )
						{
							if ( ! err_handler )
							{
								console.error ( ajax_response [ 'err_descr' ] );
								alert ( ajax_response [ 'err_descr' ] );
							} else err_handler ( ajax_response );
							
						} else {
							console.error ( "Generic Request error. Error code: " + ajax_response [ 'err_code' ] );
						}

						return;
					}
				} catch ( e ) {
					console.error ( "ERROR IN EVAL: " + req.responseText );
					return;
				}

				if ( callback ) callback ( ajax_response );
			} else
				console.error ( req.responseText );
		}
	}
}

function ajax_meth_request ( url, vars, callback, easy, sync ) 
{
	var req = null;
	var id = '';
	var t;
	var res = '';
	var self = this;
	var s;

	if ( ! url ) url = this.url;

	if ( vars ) 
	{
		vars = Array.fromObject ( vars );

		for ( t in vars ) 
		{
			if ( typeof ( vars [ t ] ) == 'undefined' ) continue;
			if ( typeof ( vars [ t ] ) == 'function' ) continue;

			/*
				This is a hack for IE, since it considers functions as objects (!!!)
			*/
			if ( typeof ( vars [ t ] ) == 'object' )
			{
				s = vars [ t ].toString ();
				if ( s.match ( /^function/ ) ) continue;
			}

			if ( vars [ t ] == '__arr' ) continue;

			// alert ( "t: " + t + " - " + typeof ( vars [ t ] ) + " - " + vars [ t ]  );
			// res += t + "=" + escape ( vars [ t ] ) + "&";
			if ( ( typeof ( vars [ t ] ) == 'string' ) || ( typeof ( vars [ t ] ) == 'number' ) )
				res += t + "=" + escape ( vars [ t ] ) + "&";
			else
			{
				try 
				{
					res += t + "=" + escape ( vars [ t ].toJSONString() ) + "&";
				} catch ( e ) {
					res += t + "=" + escape ( vars [ t ] ) + "&";
				}
			}
		}

		res = res.substr ( 0, res.length - 1 );
	}

	req = this._build_req_obj ();


	// The request callback
	req.onreadystatechange = function () { ajax_int_base_reqchange ( req, callback, easy ); }

	var async = true;

	if ( sync ) async = false;

	req.open ( "POST", url, async );
	req.setRequestHeader ( 'Content-Type', 'application/x-www-form-urlencoded' );
	req.send ( res );
}

function ajax_meth_easy_req ( arr, cback ) { this.request ( null, arr, cback, true ); }

function ajax_meth_int_build_req_obj ()
{
	var req;

	if ( window.XMLHttpRequest )		// Mozilla, Safari, Konqueror, Netscape...
	{
		req = new XMLHttpRequest ();
	} else {
		try {
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      		} catch(e) {
        		try {
          			req = new ActiveXObject("Microsoft.XMLHTTP");
        		} catch ( e ) {
          			req = null;
				console.error ( "XMLHTTP Request object not available." );
        		}
		}
	}

	return req;
}

// PUBLIC:
//		AJAXManager,
//		request
function AJAXManager ()
{
	this._req_counter = 0;

	this.url = "/ajax.php";

	this.request = ajax_meth_request;
	this.easy    = ajax_meth_easy_req;
	this._build_req_obj = ajax_meth_int_build_req_obj;
	this.error_handler = null;
}

