/**
 * Redirects to the current url but with the given locale.
 * In other words it changes the current's page language.
 * 
 * @param {String} locale a 2 characters string equal to a language 
 * (en,fr,pt,ar are the most probable).
 */
function gotoUrlOfLanguage(locale)
{
	var currentURL = location.href;
	currentURL = currentURL.replace(/\?locale=..&/, "?");
	currentURL = currentURL.replace(/\?locale=../, "");
	currentURL = currentURL.replace(/&locale=../, "");
	currentURL = currentURL.replace(/locale=../, "");
	
	if (endsWith(currentURL, ".jsp")){
		currentURL = currentURL.replace(".jsp", ".jsp?locale="+locale);
	} else {
		currentURL += '&locale='+locale;
	}
	location.href = currentURL;
}

/**
 * Checks if the the container string ends with the content string.
 * 
 * @param {Object} container the container string.
 * @param {Object} content the content string.
 */
function endsWith(container, content) 
{
   var offset = container.length - content.length;
   return offset >= 0 && container.lastIndexOf(content) === offset;
}