/*
function handleTable(data, kwargs)

Handles a generic table (list of lists) returned by the server

handleTable is auxiliated of SiteTable* dictionaries (hashes) of pairs
'objectname.methodname' : relevantInfo
that uses the call object and method name to identify relevant information
to results

The auxiliar tables are:

SiteTableCellCast:
    It is used when kwargs['cell_cast'] is not present and must contains
    the function cast to every cell.

SiteTableHeaders:
    It is used to retieve the headers of tables

Special arguments (via kwargs)
skipBackgroundRowColors. If it is set DOM3K_handleTable will not colorize your table
BackgroundColors. If its set must be an array of CSS colors to be set (alternating) in rows
*/
var SiteTableHeaders = new Array();
var SiteTableCellCast = new Array();

function DOM3K_handleTable(data, kwargs)
{
    var name = kwargs['call_object'] + '.' + kwargs['call_method'];
    headers = SiteTableHeaders[name];
    cast = kwargs['cell_cast'];
    if (cast == null) cast = SiteTableCellCast[name];
    var d = DOM3K_listToTableHTML(data,null,cast,kwargs,headers);
    if (!kwargs['skipBackgroundRowColors']) {
	if (kwargs['BackgroundColors']) {
	    DOM3K_BackgroundRowColors(d, kwargs['BackgroundColors']);
	} else {
	    DOM3K_BackgroundRowColors(d, ['rgb(230,230,240)','inherit']);
	} 
    }
    kwargs['showresults'](d);
}

/*
function DOM3K_handleSmart(data, kwargs)

A smart handler, it handles data different as it "looks"
*/
function DOM3K_handleSmart(data, kwargs)
{
    var type = typeof(data);
    if (type == 'object') {
	DOM3K_handleTable(data,kwargs);
	return;
    }
    kwargs['showresults'](data);
}
/* 
function DOM3K_createCallTheCaller(kwargs)

Creates and returns a lexical closure to call to the caller.
*/
function DOM3K_createCallCaller(kwargs)
{
    return function (d) {
	call2(kwargs['handlerargs'], kwargs['call_object'], kwargs['call_method'],
	      kwargs['call_args'], kwargs['call_kwargs'], kwargs['call_auth'])
    }
}

/*
function DOM3K_createCallCallerText(kwargs)

Creates and returns a text call to the caller, it has many
limitations because of var scopes but, might work for many
applications.
Note: this function ignores call_kwargs and call_auth

Use DOM3K_createCallTheCaller instead if you need a complex operations

*/
function DOM3K_createCallCallerText(kwargs)
{
    var x = [ kwargs['call_object'], kwargs['call_method']].join("','");
    var n = "['args']";
    return "{'handler' : function (d) { call2(null, '" + x + "'," + n + ")}}";
}

function DOM3K_HandlerDispatcher(data, kwargs)
{
    var headers = null;
    var cast = null;
    if (kwargs['data_filter']) data = kwargs['data_filter'](data, kwargs);
    if (kwargs['rawhandler']) return kwargs['rawhandler'](data, kwargs['handlerargs']);
    return kwargs['handler'](data, kwargs);
}

/*
function call2(handlerargs,objname,method,args,kwargs,auth)

Call a remote method, is a high level interface to callMethod (pronounced |call to|)
|handlerargs| Arguments to the handlers
|objname| Object name
|method| methodname
|args| arguments to the method call
|kwargs| Keyword arguments to the method call
|auth| Authentication

The handlerargs is a sophisticated way to comunicate and manage the returned data
The default arguments are:

    var defaultargs = {
	'handlerargs' :  handlerargs, \/* The arguments to the handler *\/
	'cell_cast': null, \/* Cast to every cell (If it is handled as a table) *\/
	'call_object' : objname, \/* Object name  *\/
	'call_method' : method, \/* Method called *\/
	'call_args' : args, \/*Arguments of the call*\/
	'call_kwargs' : kwargs, \/*Keyword arguments of the call(er)*\/
	'call_auth' : auth, \/*Authentication of the call(er)*\/
	'showresults' : DOM3K_showResults, \/* Some default handlers support showresults keyword
			       The showresults is a function that handles the final 
			       write to the document, by default DOM3K_showResults.
			       prototype:
			       showresults(data)
			       *\/
	'domcontainer' : DOM3K_DivResults, \/* Destiny's DOM element, used to perform style changes *\/
	'rawhandler' : null, \/* If it is present it will be used instead |handler|, 
	                     the prototype is
			     function rawhandler(data, handlerargs)
			     Where data is a the returned data  and 
			     handlerargs is the value of kwargs['handlerargs']
			     This handlers doesn't know about kwargs
	                      *\/
	'handler' : handleSmart, \/* Handler to be executed as last statement
			     The prototype of |handler| is:
			     function handler(data,kwargs)
			     Where |handlerargs| is the first argument of call2
			     |data| Result of the remote execution
			     It returns void.
			     By default it uses handleSmart
			  *\/
	'data_filter' : null, \/*|data_filter| has the same prototype that |handler|
			   but returns a (possible) modified version of data, that
			   will be set as the data. By default it returns untouched data.
			   It could possible change kwargs
			*\/
	'errorhandler' :  function(errmsg, kwargs, request, response) {
	    SPyRO_errorHandler(errmsg, null, null, request, response); 
	    } /\* The method to call when an error arises *\/
    };

*/
function call2(handlerargs,objname,method,args,kwargs,auth)
{
    if (handlerargs == null) handlerargs = new Array();
    var defaultargs = {
	'handlerargs' :  handlerargs, /* The arguments to the handler */
	'cell_cast': null, /* Cast to every cell (If it is handled as a table) */
	'call_object' : objname, /* Object name  */
	'call_method' : method, /* Method called */
	'call_args' : args, /*Arguments of the call*/
	'call_kwargs' : kwargs, /*Keyword arguments of the call(er)*/
	'call_auth' : auth, /*Authentication of the call(er)*/
	'showresults' : DOM3K_showResults, /* Some default handlers support showresults keyword
					      The showresults is a function that handles the final 
					      write to the document, by default DOM3K_showResults.
					      prototype:
					      showresults(data)
					   */
	'rawhandler' : null, /* If it is present it will be used instead |handler|, 
				the prototype is
				function rawhandler(data, handlerargs)
				Where data is a the returned data  and 
				handlerargs is the value of kwargs['handlerargs']
				This handlers doesn't know about kwargs*/
	'handler' : DOM3K_handleSmart, /* Handler to be executed as last statement
			     The prototype of |handler| is:
			     function handler(data,kwargs)
			     Where |handlerargs| is the first argument of call2
			     |data| Result of the remote execution
			     It returns void.
			     By default it uses DOM3K_handleSmart
			  */
	'data_filter' : null, /*|data_filter| has the same prototype that |handler|
			   but returns a (possible) modified version of data, that
			   will be set as the data. By default it returns untouched data.
			   It could possible change kwargs
			*/
	'errorhandler' :  function(errmsg, kwargs, request, response) {
	    SPyRO_errorHandler(errmsg, null, null, request, response);
	}/* The method to call when an error arises */
    };
   
    for (var k in defaultargs) {
	if (handlerargs[k] == null) handlerargs[k] = defaultargs[k];
    }

    var errorhandler = function (errmsg, handler, args, request, response) {
	handlerargs['errorhandler'](errmsg, handlerargs, request, response);
    }
    callMethod(DOM3K_HandlerDispatcher,handlerargs,objname,method,args,kwargs,auth,errorhandler);
}
