var langs = ['ru', 'lv'];

function __ (text, params) {
	if (phrases[text])
		phrase = phrases[text];
	else
		phrase = '*' + text + '*';
			
	if (typeof(phrase) == "object" || typeof(phrase) == "array") {
		var param;
		for (key in params) {
			param = params[key];
			break;
		}
		
		phrase = phrase[plural(param)];
	}
			
	for (key in params)
		phrase = phrase.replace('%{' + key + '}', params[key]);
	
	return phrase;
}

function Request(module, method, params, callback) {
	if (li = $('loading')) {
		li.style.display = 'block';
	}
	
	new Ajax.Request('/!/'+module+'/'+method, {
		method:'post',
		postBody:'json='+encodeURIComponent(Object.toJSON(params)),
		onSuccess: function(transport) {
		
			if (li = $('loading')) {
				li.style.display = 'none';
			}
			
			try {
				var json = transport.responseText.evalJSON();
			}
			catch (e) {
				return alert(transport.responseText);
			}
			
			json.json = transport.responseText;
			if (json.alert)
				alert(json.alert);
			if (json.error)
				alert(json.error);
			else
				callback(json);
				
		}
	});
}

function Language(lang) {
	var ed = new Date();
	ed.setDate(ed.getDate() + 365);
	document.cookie = 'lang='+lang+'; expires='+ed.toGMTString()+'; path=/';
	document.location.reload();
}


function Money (count) {
	return 'Ls ' + (count / 100).toFixed(2).toString().replace('.', ',');
}

function ToggleDisplay(id) {
	el = $(id);
	if (el.style.display == 'none') {
		set = 0;
		el.style.display = '';
	}
	else {
		set = 1;
		el.style.display = 'none';
	}
	Request('Settings', 'SetOpt', { option: 'hide '+id, value: set }, function () {});
}

function CheckSubmit(e, id) {
	var cc;

	if (e && e.which) {
		cc = e.which;
	}
	else {
		e = event;
		cc = e.keyCode;
	}

	if (cc == 13) {
		$(id+'submit').click();
		return false;
	}

	return true;
}