//=======================================================================================
// MULTI-LANGUAGE CLIENT-SIDE CODE...
//=======================================================================================

function SetLang(lang)
{
	//	Redirect to the current page but with the necessary URL parameter set (or revised)
	//	to set the current language to the language specified in 'lang'

	//	(Don't reload the page if the requested language is already the current language)
	var curLang = document.getElementById("sCurLang").value;
	if (curLang == lang) return(false);
	
	var sURL = top.location.href;		// use top.location (instead of self.location or document.location) for IE!
	if (sURL.charAt(sURL.length-1) == "#") sURL = sURL.substr(0,sURL.length-1);		// strip off trailing # -- messes up redirect!
	
	// if there are no querystring parameters, add one for the language...
	if (top.location.search == "")
	{
		top.location.href = sURL + "?lang=" + lang;
	}
	else
	{
		// If "lang=xx" already is in the URL, replace it with the new language specifier...
		var pos = sURL.lastIndexOf("lang=");
		if (pos > 0)
			top.location.href = sURL.substr(0,pos) + "lang=" + lang + sURL.substr(pos+7);
			
		// or add the new language specifier to the existing querystring...
		else
			top.location.href = sURL + "&lang=" + lang;
	}
}


//----------------------------------------------------------------------------------------
function ml_Edit(ctl)
{
	var lang = document.getElementById("sCurLang").value;
	var page = document.getElementById("sCurPage").value;
	var term = ctl.id;

	//Ajax, Moodal window for editor...
	//	Note: we tried using cookies on the TermEditor page for the language rather than passing it as a URL parameter
	//			but it was unreliable depending on user's browser settings.  It's safer to explicitly pass the language.
	MOOdalBox.open( "/Common/MultiLang/TermEditor.asp?term=" + term + "&lang=" + lang + "&page=" + page, // the link URL
		"Birkey Term Translator", 	// the caption (link's title) - can be blank
		"500 300" 					// width and height of the box - can be left blank
		);

}


//----------------------------------------------------------------------------------------
function doTranslation(lang)
{
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}

	var sNew = document.getElementById("NewText").value;
	sNew = sNew.replace(/^\s*|\s*$/g, ""); 		//Trim leading and trailing spaces

	if (sNew.length > 0)
	{
		var sField = document.getElementById("sID").value;
		var url = "/Common/MultiLang/TermPutback.asp";
		var params = "term=" + sField + "&lang=" + lang + "&val=" + escape(sNew);
		xmlHttp.open("POST", url, true);
		
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.onreadystatechange = stateChangeFunc;

		xmlHttp.send(params);
	}
	else
		alert("You have to enter something, please!");

}


//----------------------------------------------------------------------------------------
function stateChangeFunc()
{	// Function to be called when the state changes

	var sField = document.getElementById("sID").value;
	var sType = document.getElementById("sType").value;
	var ctl = sType + sField;
	var sNew = document.getElementById("NewText").value;

//	if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 	//This should be necessary but don't know why it doesn't work!!
//	{
//		if (xmlHttp.responseText != "")
//		{
			// Handle putting text back into different types of controls...
			if (sType == "bn_")		//buttons
				document.getElementById(ctl).value = sNew;
			else					// basic text SPANs
				document.getElementById(ctl).innerHTML = sNew;

			// Close the Ajax term editor box...
			MOOdalBox.close();
//		}
//	}
}


//----------------------------------------------------------------------------------------
function GetXmlHttpObject()
{  
	var xmlHttp = null;
	
	try
	{    // Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	} 
	catch (e)
	{    // Internet Explorer    
		try
		{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
		catch (e)
		{      
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
		}
	}
	return xmlHttp;
}
