
var V4;
var Literal;


//////////////////////////////////////////////////////////////////
//
// Literal
// ==============================================================
// 
// Permet accedir de forma asincrona a un literal de struts
//
//////////////////////////////////////////////////////////////////

if (Literal == undefined) {
	Literal = function (key, param1, param2, param3, param4) 
	{
		this.v4 = __v4; // aquesta variable sempre esta al tophead.jsp
		this.key = key;
		this.message = "not loaded";
		this.load(param1, param2, param3, param4);
	};
}


Literal.prototype.getKey = function () {
	return this.key;
};

Literal.prototype.getMessage = function () {
	return this.message;
};


Literal.prototype.load = function (param1, param2, param3, param4) 
{
	var where = '/'+this.v4.appid+'V4/ajax.do?actiontodo=getliteral&sessionId='+this.v4.getSessionId()+'&domainId='+this.v4.getDomainId()+'&key='+this.key;
	if (param1) where += "&param1="+param1;
	if (param2) where += "&param2="+param2;
	if (param3) where += "&param3="+param3;
	if (param4) where += "&param4="+param4;
	var me = this;
	
	new Ajax.Request(where,
	{
	    method:'get',
	    onSuccess: function(transport)
	    {
	    	me.message = transport.responseText;
	    },
	    onFailure: function() 
	    {
	    	me.message = "????";
	    }
	});
};


//////////////////////////////////////////////////////////////////
//
// V4
// ==============================================================
//
// Conte informacio de l'entorn
//
//////////////////////////////////////////////////////////////////


if (V4 == undefined) {
	V4 = function (sessionId, domainId) 
	{
		this.sessionId = sessionId;
		this.domainId = domainId;
		this.appid = null;
	};
}



V4.prototype.getSessionId = function () {
	return this.sessionId;
};

V4.prototype.getDomainId = function () {
	return this.domainId;
};


///////////////////////////////////
// fa un ping de la sessio
///////////////////////////////////

V4.prototype.ping = function () 
{
	var where = '/'+this.appid+'V4/ajax.do?actiontodo=ping&sessionId='+this.sessionId+'&domainId='+this.domainId;
	
	new Ajax.Request(where,
	{
	    method:'get',
	    onSuccess: function(transport)
	    {
	    },
	    onFailure: function() 
	    {
	    }
	});
};



V4.prototype.can = function (serv,priv,callback) 
{
	var where = '/'+this.appid+'V4/ajax.do?actiontodo=can&sessionId='+this.sessionId+'&domainId='+this.domainId+'&canServ='+serv+'&canPriv='+priv+'&nocacheid='+new Date().getTime();
	
	new Ajax.Request(where,
	{
	    method:'get',
	    onSuccess: function(transport)
	    {
	    	callback.call(this, transport.responseText);
	    },
	    onFailure: function() 
	    {
	    	callback.call(this, "false");
	    }
	});
};


V4.prototype.ajax = function (where,callbackSuccess,callbackFailure,args)
{
	where += '&nocacheid='+new Date().getTime()
	new Ajax.Request(where,
	{
	    method:'get',
	    onSuccess: function(transport)
	    {
	    	callbackSuccess.call(this, transport.responseText, args);
	    },
	    onFailure: function() 
	    {
	    	callbackFailure.call(this, transport.responseText, args);
	    }
	});
};

//solve IE memory leak (requires Prototype.js) 
Event.observe(window, 'unload', function (){
	if (typeof(Ajax.transport) != 'undefined'){
		Ajax.transport.open("GET", "dummy",true);
		delete Ajax.transport;
	}}, false);
