/** Function:	xl_SetLangs
 *  Purpose: 	This function is used to initialize the esdi multi-language
 *				engine in order to tell it what alternate languages are valid.
 *				The order of the languages provided to this function equates
 *				to the order of the alternate strings provided to the 
 *				xl_AddLangEntry function, and they also equate to the value
 *				passed to the xl_SwitchLang function
 */
function xl_SetLangs()
{
	var argv = xl_SetLangs.arguments;
	var argc = xl_SetLangs.arguments.length;
	var x = 0;
	lang_array = new Array();
	while(x < argc)
	{
		// Convert the parms into entries in the array
		lang_array[x] = argv[x];
		x++;
	}
	
	// Store the array in a new property of the document so that
	// we can retrieve it later
	document.xl_oSupportedLangs = lang_array;
}


/** Function:	xl_AddLangEntry
 *  Purpose: 	This function is used to create the 'translatation'
 *				table between the native language (the first parm)
 *				and each alternate language (as defined in the
 *				xl_SetLangs call)
 */
function xl_AddLangEntry(native_lang)
{
	var argv = xl_AddLangEntry.arguments;
	var argc = xl_AddLangEntry.arguments.length;
	var x = 1;

	// If the table (array) of language entries has not been
	// attached to the document yet, do it now.
	if(!document.xl_oMultiLangArr)
		document.xl_oMultiLangArr = new Array();
	
	// Construct a new language object for this 'record' in the table
	// and assign it the various translational equivalents (this assumes
	// the parms are in the same order as specified in xl_SetLangs
	langstr = new Object();
	while(x < argc)
	{
		langstr[document.xl_oSupportedLangs[x - 1]] = argv[x];
		x++;
	}

	// Add the entry to the array
	document.xl_oMultiLangArr[native_lang] = langstr;

}


/** Function:	xl_SwitchLang
 *  Purpose: 	This function is used to switch all string within
 *				spans identified as language specific (with lang="Y")
 *				to the equivalent string in the language specified
 *				as a parm.  If no match is found, the text is left as-is.
 *				If the same string needs to be translated differently
 *				then use an 'id' tag in the span, and also use the same
 *				id in the AddLangEntry call.
 */
function xl_SwitchLang(target_lang)
{
	var i=0; 
	var tokens = document.getElementsByTagName("SPAN"); 
	// Cycle through all spans
	for (i=0; i < tokens.length; i++) 
	{
		// Trap for ones with a lang attribute set to "Y"
		lang = tokens[i].getAttribute("lang");
		if (lang == "Y") 
		{ 
			// If there is an ID attribute, we want to use that
			// to look it up in our replacement table, otherwise
			// use the string itself
			id = tokens[i].getAttribute("id");
			if((id == "") || (id ==null))
				newstr = document.xl_oMultiLangArr[tokens[i].innerHTML];
			else
				newstr = document.xl_oMultiLangArr[id];
				
			// If we found a replacement, replace it.
			if((newstr != null) && (newstr[target_lang] != null))
				tokens[i].innerHTML = newstr[target_lang];
		}
	} 
} 

function xl_SwitchLangBox(target_lang)
{
	var i=0; 
	var tokens = document.getElementsByTagName("OPTION"); 
	// Cycle through all spans
	for (i=0; i < tokens.length; i++) 
	{
		// Trap for ones with a lang attribute set to "Y"
		lang = tokens[i].getAttribute("lang");
		if (lang == "Y") 
		{ 
			// If there is an ID attribute, we want to use that
			// to look it up in our replacement table, otherwise
			// use the string itself
			id = tokens[i].getAttribute("id");
			if((id == "") || (id ==null))
				newstr = document.xl_oMultiLangArr[tokens[i].innerHTML];
			else
				newstr = document.xl_oMultiLangArr[id];
				
			// If we found a replacement, replace it.
			if((newstr != null) && (newstr[target_lang] != null))
				tokens[i].innerHTML = newstr[target_lang];
		}
	} 
}

function xl_SwitchLangButton(target_lang)
{
	var i=0; 
	var tokens = document.getElementsByTagName("INPUT"); 
	// Cycle through all spans
	for (i=0; i < tokens.length; i++) 
	{
		// Trap for ones with a lang attribute set to "Y"
		lang = tokens[i].getAttribute("lang");
		if (lang == "Y") 
		{ 
			// If there is an ID attribute, we want to use that
			// to look it up in our replacement table, otherwise
			// use the string itself
			id = tokens[i].getAttribute("id");
			if((id == "") || (id ==null))
				newstr = document.xl_oMultiLangArr[tokens[i].value];
			else
				newstr = document.xl_oMultiLangArr[id];
				
			// If we found a replacement, replace it.
			if((newstr != null) && (newstr[target_lang] != null))
			{
				document.getElementById("buttontext").innerHTML = newstr[target_lang];
				tokens[i].value = document.getElementById("buttontext").innerHTML;
				document.getElementById("buttontext").innerHTML = "";
			}
				
		}
	} 
}

/** Function:	changeLang
 *  Purpose: 	This function is used to reload the program with the user-selected 
				language ID parm. It appends the langid=xx parameter to the URL 
				and reloads the page.
				The function expects the SELECT element as a parameter.
	Created By:	Katie Bateman, ExcelSystems - April 21, 2009

	Modifications:
	TEH 20090707 - Need to make this work for POST forms
 */
function changeLang (obj)
{
	// set the new language ID
	var langID = obj.options[obj.selectedIndex].value;

	//We set a JavaScript variable of 'jstask' with the task so we can treat the ones with POST
	// differently than those with GET methods.
	//If POST methods...
	if((jstask == "DEFAULT") && (jsprogram == "EC165W"))
	{
		document.srchCat.langid.value = langID;
		document.srchCat.submit();
	}
	//now for GET methods...
	else
	{
		// append to the URL
		var url = new String(document.location);

		// parse off existing langid attribute if it exists in url
		url = url.replace(/\?langid=\w\w/gi, '');
		url = url.replace(/&langid=\w\w/gi, '');

		if (url.indexOf('?') == -1)
		{
			url += '?langid=' + langID;
		}
		else
		{
			url += '&langid=' + langID;
		}

		// load the page
		document.location = url;
	}
}


/** Function:	addLangSelector
 *  Purpose: 	This function is called in the onload event of the page body, to 
				reposition the language dropdown to the appropriate position in 
				the page header.
	Created By:	Katie Bateman, ExcelSystems - April 21, 2009
 */
function addLangSelector()
{
	// get dropdown object
	var dropobj = document.getElementById('droplang');
	
	// find language dropdown container div
	var containerdiv = document.getElementById('selectlang');

	// append new dropdown inside container
	containerdiv.appendChild(dropobj);

	// show the dropdown (it is initially hidden)
	dropobj.style.display = '';
}