/**
 * Change to target locale, including current page and server environment.
 */
function changeToLocale(pageRoot, currLocaLe, targetLocale, relativePosPrefix) {
	try {
		changeServerToLocale(targetLocale, relativePosPrefix);
		changeCurrentPageLocale(pageRoot, currLocaLe, targetLocale);
	} catch(e) {
	    window.alert("Error: " + e.name + e.message);
	}	
}

function changeServerToLocale(locale, relativePosPrefix) {
	var servletUrl= relativePosPrefix + "common/locale/Locale.faces?locale=" + locale;
	var responseText = doSyncHttpRequest(servletUrl, null);
}

/**
 * Change current page to target locale.
 */
function changeCurrentPageLocale(pageRoot, currLocaLe, targetLocale) {     

	var location = pageRoot.window.location.href;
	var localeStringStartPos = location.indexOf(currLocaLe);
	if (localeStringStartPos > -1 ) {
		//locale string found, replace with target locale string.
		var localeRExp = currLocaLe;
		//var localeRExp = new RegExp(currLocaLe,"ig");
		location = location.replace(localeRExp, targetLocale);
		pageRoot.window.location.href = location;
	} else {
		//locale string not found, just refresh current page
		//pageRoot.window.location.reload(); 
		pageRoot.window.location.href = location;
	}
}
