/*	LANGUAGE CHOOSER
 *  Javascript should not reload the current page if we are in a language specific page, because the purpose
 *	of this class is to redirect to a new language setting.  The LInternationalUpdater constructor below takes a domain root
 *	and a redirect URL which will be stripped of any language specification.
*/
function LInternationalUpdater(iDomainRoot, iURL)
{
	this.mDomainRoot = iDomainRoot;
	this.mURL = decodeURIComponent(iURL);
	// strip out language code from URL
	if (this.mURL.match('^/[a-z][a-z]/(.)*')) {
		this.mURL = this.mURL.substring(3);
	} else if(this.mURL.match('^/[a-z][a-z]$')) {
		this.mURL = '/';
	} else if (this.mURL.match('^/browse/search.php(.)*')) {
		// add random number to prevent caching on search page
		this.mURL += '?' + Math.random();
	}
}

LInternationalUpdater.prototype.storeIntl = function (iCOUID, iLang, iCurrency)
{
	var url = this.mDomainRoot + '/account/callbacks/update_intl_pref.php';
	var params = {'fCOUID' : iCOUID, 'fLang' : iLang, 'fCurrency' : iCurrency};

	try {
		var req = MochiKit.Async.getXMLHttpRequest();
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.setRequestHeader('Accept', 'application/json');
		var d = MochiKit.Async.sendXMLHttpRequest(req, queryString(params));
		d.addCallback(bind(this.updatePage, this));
	} catch (ex) {
		params.fRedirect = window.location;
		window.location = url + '?' + queryString(params);
	}
}

LInternationalUpdater.prototype.updatePage = function (transport) {
	response = evalJSON(transport.responseText);

	if (response.ok == 1) {
		if (this.mURL != null) {
			window.location = this.mURL;
		} else {
			window.location.reload(true);
		}
	} else {
		$('regionSelection').innerHTML = response.message;
	}
}
