function getSessionFromURL ()
{	
	wSession = "";
	
	wURL = top.location.href;
	pINI = wURL.indexOf (";");
	pFIN = wURL.indexOf ("?");
	if (pFIN == -1)			
	{
		pFIN  = wURL.indexOf ("&");
	}
	if (pFIN == -1)			
	{
		pFIN  = wURL.length;
	}			
	if (pINI > -1 &&
	    pFIN > -1)			
	{
		wSession = wURL.substring (pINI , pFIN);  
	}
	else
	{
		wSession = "";
	}
	
	return wSession;
}

// añade a la url el parámetro 'jsessionid' con la sesión actual (si el parámetro ya está en la url lo añade, no lo substituye)
function encodeURL (pURL)
{	
	wINI = pURL.indexOf ("?");
	if (wINI == -1)			
	{
		wINI  = pURL.indexOf ("&");
	}

	var rURL = "";
	if (wINI > -1)
	{		
		rURL = pURL.substring (0, wINI) + 
			   getSessionFromURL ()     +
			   pURL.substring (wINI); 
	}
	else
	{
		rURL = pURL + 
		       getSessionFromURL ();
	}
	return prettyURL (rURL);			
}

function sendEncodedURL (pURL)
{	
	top.location = encodeURL (pURL);
	return true;
}

function prettyURL (pURL)
{
	pYES = pURL.indexOf ("?");
	if (pYES > -1)			
	{
		return pURL;
	}			
	
	pNOT = pURL.indexOf ("&");
	if (pNOT == -1)			
	{
		return pURL;
	}			

	var rURL = '';
	if (pNOT + 1 < pURL.length)
	{
		rURL = pURL.substring (0, pNOT) + 
				 "?filler=i&"             +
				 pURL.substring (pNOT + 1);
	}
	else
	{
		rURL = pURL.substring (0, pNOT) + 
				 "?filler=i&";
	}
	return rURL;
}

function replaceURL (pURL, pFrom, pTo)
{
	var i    = pURL.indexOf (pFrom);
	var rURL = '';
	if ((!i)      || 
	    (i == -1))
	{
		return pURL;
	}
	rURL += pURL.substring (0, i) + pTo;
	if (i + pFrom.length < pURL.length)
	{
		rURL += replace (pURL.substring (i + pFrom.length, pURL.length), pFrom, pTo);
	}
	return rURL;
}

// retorna el valor del parámetro 'jsessionid' de la url
function obtenerJSessionIdEnURL (pURL)
{
	var mySession = new Array ();
	mySession [0] = "";
	mySession [1] = -1;
	mySession [2] = -1;
		
	var i = pURL.indexOf (";jsessionid=");
	var j = pURL.indexOf ("?");
	if (j == -1)
	{
		j = pURL.indexOf ("&");
	}
	if (j == -1)
	{
		j = pURL.length;
	}	

	if (i > -1 &&
		j > -1)
	{
		mySession [0] = pURL.substring (i, j);
		mySession [1] = i;
		mySession [2] = j;
	}
	return mySession;
}

// dada una URL, substituye el valor del parámetro 'jsessionid' por la sesión actual
function substituirJsessionIdCorrectoEnURL (pURL)
{
	var myURL = "";
	var decodedURL      = obtenerJSessionIdEnURL (pURL);
	var decodedLocation = obtenerJSessionIdEnURL (document.location.toString ());
	
	if (decodedURL [1] > -1 &&
		 decodedURL [2] > -1)
	{
		myURL = pURL.substring (0, decodedURL [1]) +
			     decodedLocation [0]                +
	    		  pURL.substring (decodedURL [2]);
	}
	else
	{
		myURL = pURL;
	}
	return myURL;
}