/*
mls.js

Multi-language support variables and functions.
*/

/*
Array of codes for defined languages.
*/
var arrMLSLanguages = new Array();

/*
Array of defined text codes.
arrMLSTextIDs[i][j] = j-th text ID for i-th language.
Languages are defined in arrMLSLanguages.
*/
var arrMLSTextIDs = new Array();

/*
Array of defined text translations.
arrMLSTranslations[i][j] = translation for the j-th text ID in the i-th language.
Languages are defined in arrMLSLanguages.
Text IDs are defined in arrMLSTextIDs.
*/
var arrMLSTranslations = new Array();

/*
Code of the default language.
This language is used in the MLSGetTextD function.
*/
var strDefaultLanguage = "";

/*
Sets the code of the default language
*/
function MLSSetDefaultLanguage(strLanguage) {
	strDefaultLanguage = strLanguage;
}

/*
Returns the index of the language with the specified code.
Specified language code is searched into arrMLSLanguages.
If the language is not defined, returns -1.
*/
function MLSFindLanguage(strLanguage) {
	
	var res = -1;
	var i;
	for(i = 0; i < arrMLSLanguages.length; ++i) {
		if(arrMLSLanguages[i] == strLanguage) {
			res = i;
			break;
		}
	}
	
	return(res);
} // MLSFindLanguage

/*
Returns the index of the specified text ID within the specified language.
Specified language code is searched into arrMLSLanguages.
If the language is found (for example, under the index i in arrMLSLanguages),
the specified text ID is searched in the arrMLSTextIDs[i] sub-array of arrMLSTextIDs.
If the text ID is found in the sub-array, its index is returned as a result.
If the language is not defined, or the text ID is not defined, returns -1.
*/
function MLSFindText(strLanguage, strTextID) {
	
	var res = -1;
	var intLanguageIndex = MLSFindLanguage(strLanguage);
	if(intLanguageIndex != -1) {
		var arrTexts = arrMLSTextIDs[intLanguageIndex];
		var i;
		for(i = 0; i < arrTexts.length; ++i) {
			if(arrTexts[i] == strTextID) {
				res = i;
				break;
			}
		}
	}
	
	return(res);
} // MLSFindText

/*
Defines a translation for the specified text ID within the specified language.
If the language code is not defined in arrMLSLanguages, it will be defined.
If the text ID is not defined in arrMLSTextIDs, it will be defined.
In either case, a translation will be added or updated in arrMLSTranslations.
The function does not return anything.
*/
function MLSDefineText(strLanguage, strTextID, strText) {
	
	var intLanguageIndex = MLSFindLanguage(strLanguage);
	if(intLanguageIndex != -1) { // The language exists
		var intTextIndex = MLSFindText(strLanguage, strTextID);
		if(intTextIndex != -1) { // The text ID exists
			arrMLSTranslations[intLanguageIndex][intTextIndex] = strText;
		}
		else { // The text ID does not exist
			var intNewLength = arrMLSTextIDs[intLanguageIndex].length;
			arrMLSTextIDs[intLanguageIndex][intNewLength] = strTextID;
			arrMLSTranslations[intLanguageIndex][intNewLength] = strText;
		}
	}
	else { // The language does not exist
		var intNewLength = arrMLSLanguages.length;
		arrMLSLanguages[intNewLength] = strLanguage;
		arrMLSTextIDs[intNewLength] = new Array(strTextID);
		arrMLSTranslations[intNewLength] = new Array(strText);
	}
} // MLSDefineText

/*
Returns the translation of the specufued text ID within the specified language.
If the language or the text ID is not defined, the specified default text is returned.
*/
function MLSGetText(strLanguage, strTextID, strDefault) {
	
	var res = strDefault;
	var intLanguageIndex = MLSFindLanguage(strLanguage);
	if(intLanguageIndex != -1) { // The language exists
		var intTextIndex = MLSFindText(strLanguage, strTextID);
		if(intTextIndex != -1) {
			res = arrMLSTranslations[intLanguageIndex][intTextIndex];
		}
	}

	return(res);
} // MLSGetText

/*
Same as MLSGetText, but the default language is used.
*/
function MLSGetTextD(strTextID, strDefault) {
	
	return(MLSGetText(strDefaultLanguage, strTextID, strDefault));
}
