// variabili globali
var debug = false;
var url_xml_rpc;
var struct_param = new Object();
var iso_encoding;

// funzione per preparare le opzioni una chiamata ajax che codifica dati in formato json (richiede jquery.js)
function setup(data_object,callback,data_type) {
	if(typeof data_type === 'undefined') {
		data_type = 'json';
	}
	
	var options = {
		url: url_xml_rpc,
		type: 'POST',
		contentType: 'application/json',
		data: JSON.stringify(data_object),
		dataType: data_type,
		processData: false,
		success: function(data,textStatus) {
			if(data_type != 'text/html') {
				callback(data.response);
			}
			else {
				callback(data);
			}
		},
		complete: function(XMLHttpRequest,textStatus) {
			if(textStatus != "success" && (typeof(debug) != 'undefined' && debug)) {
				alert("ATTENTION: "+textStatus+"\nXMLHttpRequest.responseText: "+XMLHttpRequest.responseText);
			}
		}
	};
	
	if(typeof(debug) != 'undefined' && debug) {
	    //var_dump(options);
    }
	
	return options;
}


//
// converte i caratteri speciali presenti in tstr in entita' html
//
function convertToEntities(tstr) {
    var bstr = '';
    for(i = 0; i < tstr.length; ++i) {
        if(tstr.charCodeAt(i) > 127) {
            bstr += '&#' + tstr.charCodeAt(i) + ';';
        }
        else {
            bstr += tstr.charAt(i);
        }
    }
    return bstr;
}


// VAR_DUMP PER JAVA SCRIPT
var box;
function var_dump(obj) {
	box = window.open('','Var_dump','resizable=yes,toolbar=no,scrollbars=yes,width=200,height=200');
	explore(obj,0,0,0,0,0);
	box.document.close();
}

// funzione ricorsiva che esamina l'elemento passato
function explore(obj,eln,inside,level) {
	var index = '';
	var spaces = '';
	for (var i = 0; i < level;i++)
		spaces += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	index += spaces;
	if (inside == 0)
		index += '';
	else
		index += '[' + eln + ']&nbsp;=>&nbsp;';
    if (obj == null) {
        box.document.write(index + 'NULL<br>');
		return;
    }
	else if (typeof(obj) == 'number') {
		box.document.write(index + 'number(' + obj + ')<br>');
		return;
	}
	else if (typeof(obj) == 'string') {
		box.document.write(index + 'string(' + obj.length + ')&nbsp;"' + obj + '"<br>');
		return;
	}
	else if (typeof(obj) == 'boolean') {
		box.document.write(index + 'bool(' + obj + ')<br>');
		return;
	}
	else if (typeof(obj) == 'object') {
		var count = 0;
		for (var i in obj)
			count++;
		if (count == 0)
			box.document.write(index + 'array(' + count + ')&nbsp;{&nbsp;}<br>');
		else {
			box.document.write(index + 'array(' + count + ')&nbsp;{<br>');
                for (var i in obj) 
                    explore(obj[i],i,1,level+1);
			box.document.write(spaces + spaces + '}<br>');
		}
		return;
	}
    else {
        box.document.write(index + 'NULL<br>');
		return;
    }
	return;
}
// FINE VAR_DUMP
