/*
 *	TITLE:	locale.js
 *	VER:	1.0
 *  AUTHOR:	David Pattison
 *	DATE:	6/04/2004
 */

/* 
 * Global variables
 */
var xmlLocale = new ActiveXObject("Microsoft.XMLDOM");
var strLang;

/*
 * Choose the current locale based upon Query String, Browser Preference and user selection
 */
function GetLocale()
{
	try
	{
		// Startup use query string or browser locale
		strLang = QueryString("lang");
		
		if ((strLang == null ) || (strLang == ""))
		{
			strLang = navigator.userLanguage;
		}
		return strLang;
	}
	catch(err)
	{
		alert("Error in GetLocale():" + err.description);
		return null;
	}
}

/*
 * replace tags of the specified type with entries in the local dataset
 */
function ReplaceTagType(object, strTagType)
{
	// Do the replacing business
	try
	{
		var strTagList = object.getElementsByTagName(strTagType);
	
		for( var ii=0; ii<strTagList.length; ii++ )
		{	
			if( strTagList[ii].id != null )
			{
				if( strTagList[ii].id != "" )
				{
					var strNewText=GetLocaleString(strTagList[ii].id, strTagType)
					if( strNewText != null )
					{
						if( strTagType != "img" )
						{
							strTagList[ii].innerText=strNewText;
						}
						else
						{
							strTagList[ii].alt=strNewText;
						}
					}
				}
			}
			else
			{
				var xmlMsgNode=strTagList[ii];
				var strMsgId=xmlMsgNode.selectSingleNode("from");
				var strMsgTag=xmlMsgNode.selectSingleNode("tag");
				var strMsgTxt=xmlMsgNode.selectSingleNode("message");

				strMsgTxt.text=GetLocaleMessage(strMsgId.text, strMsgTag.text);
			}
		}
	}
	catch(err)
	{
		alert("Error in ReplaceTagType():" + err.description);
	}
}

/*
 *	load specified locale resource into global object xmlLocale
 */
function loadLocale(strLocale)
{
	try
	{
		xmlLocale.async = false;
		xmlLocale.load("locales/" + strLocale.toLowerCase() + "msg.xml");
		if( xmlLocale.parseError != 0 )
		{
			// msg.xml not found error
			if (xmlLocale.parseError.errorCode == -2146697210)
			{
				return false;
			}
			throw new Error(xmlLocale.parseError.errorCode, xmlLocale.parseError.reason);
		}
	}
	catch (err)
	{
		alert("Error in loadLocale(): number - " + err.number + ", description is " + err.description);
		return false;
	}
	return true;
}

/*
 *	load specified locale resource into global object xmlLocale
 *	if the full length locale (eg zh-cn) fails
 *	then try the short length (eg zh)
 *	otherwise, use the defautl (eg en)
 */
function loadLocaleEx(strLocale)
{
	try
	{
		var bSuccess = loadLocale(strLocale);
		
		if (false == bSuccess)
		{
			if (strLocale.length > 2)
			{
				bSuccess =loadLocale(strLocale.substring(0, 2));
			}
		}
		if (false == bSuccess)
		{
			loadLocale("en");
		}
	}
	catch (err)
	{
		alert("Error in loadLocaleEx(): number - " + err.number + ", description is " + err.description);
		return false;
	}
}

/*
 * Main Entry point, called on initial load or mouse click event
 */
function SetLocale()
{
	try
	{
		var strOldLang=strLang;
		
		if( arguments.length == 0 )
		{
			strLang = GetLocale();
		}
		else
		{
			strLang = arguments[0];
		}
		
		if (strOldLang != strLang)
		{
			loadLocaleEx(strLang);
		}

		ReplaceTagType(document, "span");
		ReplaceTagType(document, "img");
		ReplaceTagType(xmlStatus, "status");
	}
	catch (err)
	{
		alert("Error in SetLocale(): number - " + err.number + ", description is " + err.description);
	}
}

/*
 * Set locale for message display, called on initial load event
 */
function SetLocaleInit()
{
	try
	{
		var strOldLang = strLang;
		
		strLang = GetLocale();
		
		loadLocaleEx(strLang);	

		// the 1st argument could be false to indicate the xmlStatus object
		//  is not on this page, the scada.js makes a document w/o xmlStatus
		if( arguments.length == 0 )
		{
			ReplaceTagType(xmlStatus, "status");
		}		
	}
	catch(err)
	{
		alert("Error in SetLocale(): number - " + err.number + ", description is " + err.description);
	}
}

/*
 * Set locale for image tool tips, called on loaded event
 */
function SetLocaleImg()
{
	var element = window.event.srcElement;
	if( element.tagName.toLowerCase() == "img" )
	{
		if( element.id != "" )
		{
			var strNewText=GetLocaleString(element.id, "img")
			if( strNewText != null )
			{
				element.alt=strNewText;
			}
		}
	}
}

/*
 * Extract locale string given an id from the locale dataset
 */
function GetLocaleString(strTagId, strTagType)
{
	try
	{
		var xmlRoot=xmlLocale.documentElement;
		if( xmlRoot != null)
		{
			var xmlLangText=xmlRoot.selectSingleNode(strTagType+"[@id='"+strTagId+"']");
			
			if( xmlLangText != null )
			{
				return xmlLangText.text;
			}
		}
	}
	catch(err)
	{
		alert("Error in GetLocaleString():" + err.description);
	}
	return null;
}

/*
 * Extract locale system message string given an id and insert message at % symbol
 */
function GetLocaleMessage(strMsgId, strExtraText)
{
	var strMsg=new String();
	
	try
	{
		strMsg=GetLocaleString(strMsgId, "sysmsg");
	
		if( strMsg != null )
		{
			var strSplit=strMsg.split("%");
	
			if( strSplit.length > 1 )
			{
				strMsg=strSplit[0]+strExtraText+strSplit[1];
			}
		}
		else
		{
			strMsg="";
		}
		return strMsg;
	}
	catch(err)
	{
		alert("Error in GetLocaleMessage():" + err.description);
	}
	return null;
}

