//
// translation javascript // include this script if the application must support translation
//
function translateHandler()
{

	this.oTransFid = new objCollection(); 	// Hold collection of translateViews
	this.lUpdate = false;					// 0 read // 1 write
	this.oLabels = new objCollection();
	this.cExtraTags = "";
	this.nMasterFid = null;
}

translateHandler.prototype.backToList  = function(tnFid)
{
	var lcUrl, loHttp;
	lcUrl = '/vd.asp?folderid=' + tnFid + '&ADDVIEWDIV=YES&USERAW=YES';
	loHttp = new xmlHttpDom; 	
	if (qsHandler.oCurrentTabset)
		if (qsHandler.oCurrentTabset.oTab.oMenu)
		{
			loHttp.exec(qsHandler.oCurrentTabset.oTab.oMenu.cDivBody, lcUrl, tnFid);
		}
		else
		{
			lcTabDiv = 'div_' + qsHandler.oCurrentTabset.oTab.nTabId;
			loHttp.exec(lcTabDiv, lcUrl, tnFid);
		}
	else
	{
		loHttp.exec('qszone_appbody', lcUrl, tnFid)
	}
				
}

translateHandler.prototype.setLanguage = function(tnLangId)
{
	if (tnLangId > 0 )
	{
		qsHandler.setVarValue("qsi_langid", tnLangId );
		var lcHref = window.location.href
		window.location.href = lcHref;
	}
}
translateHandler.prototype.addItem = function(tnFid, tnDcId)
{
	var loRet = new translateView(tnFid, tnDcId);
	this.oTransFid.add(loRet)
	return loRet;
}

translateHandler.prototype.getItem = function(tnFid)
{
	var lni, loRet ;
	loRet  = null;
	for (lni = 0 ; lni < this.oTransFid.length; lni++)
	{
		if (this.oTransFid.item(lni).nFid  == tnFid)
		{
			loRet = this.oTransFid.item(lni);
			lni = this.oTransFid.length;
		}
	}
	return loRet;
}
	
translateHandler.prototype.editTranslation = function(tnFid)
{

	// edit translation for data items
 	this.nMasterFid = tnFid;
	this.lUpdate = true;
	
	this.collectItems();
	this.postNewItems(tnFid);
	
}

translateHandler.prototype.translateThePage = function()
{

	this.lUpdate = false;
	this.collectItems();	// reads the HTML and collects translation
	this.updateDom();		// updates items on dom
}



translateHandler.prototype.collectItems = function()
{
	var loTables, lni, loTable, lnFid, lnDcId, lnTranslate
	var loTransCollector;


	loTables = document.getElements('table[txTable="1"]');
	
	for (lni = 0 ; lni < loTables.length; lni++)
	{
		loTable 	= loTables[lni];
		lnFid  		= loTable.getAttribute("QSFID");
		lnDcId 		= loTable.getAttribute("QSDCID");
		lnTranslate = loTable.getAttribute("txDone");
// && loTable.id.indexOf('table_') > -1
		if (lnFid > 0  && (!(lnTranslate == 1) || (this.lUpdate && lnFid == this.nMasterFid)))
		{	
			loTransCollector = this.getItem(lnFid);
			if (isnull(loTransCollector))
				loTransCollector = this.addItem(lnFid, lnDcId)
	
			loTransCollector.nRunId = lni;
	
			loTransCollector.collectItems(this, loTable);
			loTable.setAttribute("txDone", 1)
		}
	}
}

translateHandler.prototype.updateDom = function()
{
	var lni, loTxView;
	// collect data and update DOM
	
	for (lni = 0 ; lni < this.oTransFid.length; lni++)
	{
		loTxView = this.oTransFid.item(lni);
		loTxView.translateIt();
	}
}

translateHandler.prototype.postNewItems = function(tnFid)
{
	var lni, loTxView;
	// collect data and update DOM
	
	
	loTxView = this.getItem(tnFid)
	if (!isnull(loTxView))
		loTxView.postNewItems();
}

translateItem = function(tnKey, tnCpId, toTd, ltTx)
{

	this.nKeyId = tnKey;
	this.nCpId = tnCpId;
	if (toTd.innerHTML=='')
		this.cText = '';
	else
		if (toTd.firstChild.nodeName=='#text')
			this.cText = toTd.innerHTML;
		else
			if (toTd.firstChild.tagName.toUpperCase()=='SPAN' && toTd.firstChild.className =='udf_fieldvalue')
				this.cText = toTd.firstChild.innerHTML 
			else
				this.cText = toTd.innerHTML 
	this.lTx = ltTx;
}

function translateView(tnFid, tnDcId, tnLangId)
{
	this.cKeyList = '';
	this.nFid  = tnFid;
	this.nDcId = tnDcId;
	this.aIds = new Array();
	this.oRecords = new objCollection();
	this.nLangId = tnLangId;
	this.nRunId = 0;
}

translateView.prototype.collectElements = function(tcElement, toTranslator, toElement, lnKeyId, lnj, lnk)
{
	// look for spans inside the TD and do not translate the TD

	var lnm, lnCpId, lnFid, loExtraCollector;
	var loSpans = $(toElement).getElements(tcElement);

	for (lnm = 0 ; lnm < loSpans.length; lnm++)
	{
		lnCpId = loSpans[lnm].getAttribute("txcpid");
		if (lnCpId)
		{
			if (loSpans[lnm].getAttribute("txcpidfid") > 0 )
				lnFid = loSpans[lnm].getAttribute("txcpidfid");
			else
				lnFid = this.nFid;

			if (loSpans[lnm].getAttribute("txkeyid") > 0 )
				lnKeyId = loSpans[lnm].getAttribute("txkeyid");

			// MV 2009.11.19 added is not id
			
			if (!$chk($(loSpans[lnm]).id))
				loSpans[lnm].id = toElement.tagName + lnFid + "_" + lnKeyId + "_" + lnCpId + "_" + lnj + "_" + lnk + "_" + lnm + this.nRunId
//				loSpans[lnm].id = toElement.tagName + lnFid + "_" + lnKeyId + "_" + lnCpId + "_" + lnj + "_" + lnk + "_" + lnm									
		
		
		
			if (lnFid > 0 && lnKeyId > 0 )
			{
				loExtraCollector = toTranslator.getItem(lnFid);
				if (isnull(loExtraCollector))
					loExtraCollector = toTranslator.addItem(lnFid, null)
				loExtraCollector.addKey(lnKeyId, lnCpId, loSpans[lnm].id)
			}
			
			if (toTranslator.lUpdate)
			{
				// Collect items to post new items to db and and go into edit mode
				// We are collecting all items and checking whether they are translated
				// need to collect items that are not translated so that we post them for update
				
				lnExtraFid = loSpans[lnm].getAttribute("txcpidfid");	

				if (!(lnExtraFid > 0) || lnExtraFid == this.nFid)				// only add if local
				{
					// only interested if part of primary table , exclude relations
					if (loSpans[lnm].getAttribute('txDone') == 2)
						this.addRecord(lnKeyId, lnCpId, loSpans[lnm], true);
					else
						this.addRecord(lnKeyId, lnCpId, loSpans[lnm], false);
				}
			}
							
			loSpans[lnm].setAttribute('txDone', 1); // collected
		}
	}
}

translateView.prototype.collectItems = function(toTranslator, toTable)
{
	var lcKeyList, loTrs, loTr, loTds, loTd, lni, lnj, lnk, lnm, lnKeyId, lnCpId, lnSpanTranslation, loSpans, loElements;
	var loExtraCollector, lnFid, lnExtraFid;

	loElements  = document.getElements("SELECT");
	for (lnk = 0 ; lnk < loElements.length; lnk++)
	{
		if (loElements[lnk].getAttribute('txspan') )
		{
			this.collectElements('OPTION', toTranslator, loElements[lnk], 1, 1, 1)
		}
	}
	

	lcKeyList = "";
	//loTrs  = toTable.getElements("tr");
	loTrs = toTable.firstChild.childNodes;
	lnFid = toTable.getAttribute('QSFID')
	for (lnj = 0 ; lnj < loTrs.length; lnj++)
	{

		lnKeyId = loTrs[lnj].getAttribute("KID");
		if (lnKeyId > 0 )
		{
			//loElements  = loTrs[lnj].getElements("td");
			loElements  = loTrs[lnj].childNodes;
			
			for (lnk = 0 ; lnk < loElements.length; lnk++)
			{
				if (isnull(loElements[lnk].getAttribute('txDone')) || toTranslator.lUpdate)
				{
					lnCpId = loElements[lnk].getAttribute("txcpid");
					lnSpanTranslation =  loElements[lnk].getAttribute("txspan");
					if (lnSpanTranslation=='1')
					{
						this.collectElements('SPAN', toTranslator, loElements[lnk], lnKeyId, lnj, lnk)
					}
					else
					{
						if (lnCpId > 0 )
						{
							if (toTranslator.lUpdate)
							{
								// Collect items to post new items to db and and go into edit mode
								// We are collecting all items and checking whether they are translated
								// need to collect items that are not translated so that we post them for update
								
								lnExtraFid = loElements[lnk].getAttribute("txcpidfid");	

								if (!(lnExtraFid > 0) || lnExtraFid == lnFid)				// only add if local
								{
									// only interested if part of primary table , exclude relations
									if (loElements[lnk].getAttribute('txDone') == 2)
										this.addRecord(lnKeyId, lnCpId, loElements[lnk], true);
									else
										this.addRecord(lnKeyId, lnCpId, loElements[lnk], false);
								}
							}
							else
							{
								lnFid = loElements[lnk].getAttribute("txcpidfid");
						
								if (lnFid > 0 )
								{
									//if (!isnull(loElements[lnk].id))
										loElements[lnk].id = "_tr" + lnFid + "_" + lnj + "_" + lnk	
									lnKeyId = loElements[lnk].getAttribute("txkeyid");
									if (lnKeyId > 0 )
									{
										loExtraCollector = toTranslator.getItem(lnFid)
										if (isnull(loExtraCollector))
											loExtraCollector = toTranslator.addItem(lnFid, null)
										loExtraCollector.addKey(lnKeyId, lnCpId, loElements[lnk].id)
									}
									
								}
								else
								{
									//if (!isnull(loElements[lnk].id))
										loElements[lnk].id = "_tr" + this.nFid + "_" + lnj + "_" + lnk	
									this.addKey(lnKeyId, lnCpId, loElements[lnk].id);
								}
							
							}
						}
					}
					loElements[lnk].setAttribute('txDone', 1)
				}
			}
		}
	}
}
translateView.prototype.addRecord = function(tnKey, tnCpId, toTd, tlTx)
{
	this.oRecords.add(new translateItem(tnKey, tnCpId, toTd, tlTx))
}


translateView.prototype.postNewItems = function()
{
	var lni, loHttp, loItem, lcKeyList, loDiv, lcVars;
	loHttp = new xmlHttpDom();
	
	
	lcKeyList = ',';
	
	for (lni=0; lni < this.oRecords.length; lni++)
	{
		loItem = this.oRecords.item(lni);
		
		if (!loItem.lTx)
		{
			// add item to the database if it was not translated
			lcVars = "txfid=" + gnFidTranslate + "&keyid=" + loItem.nKeyId + "&cpid=" + loItem.nCpId + "&dcid=" + this.nDcId + "&txt=" + encodeURIComponent(loItem.cText); 
			lcUrl = "/txbulkedit.asp"		// ?txfid=" + gnFidTranslate + "&keyid=" + loItem.nKeyId + "&cpid=" + loItem.nCpId + "&dcid=" + this.nDcId + "&txt=" + encodeURIComponent(loItem.cText); 
			// loHttp.callUrl("GET", lcUrl);
			
			
			
			loHttp.callUrl('POST', lcUrl, lcVars, false);
		}
		// but add it to the display key list so that all can be edited.
		if (lcKeyList.indexOf("," + loItem.nKeyId + "," ) == -1 )
			lcKeyList = lcKeyList +  loItem.nKeyId + ","
	}
	
	if (lcKeyList.length > 0)
		lcKeyList = lcKeyList.substr(1, lcKeyList.length-1)
	if (lcKeyList!='')
	{
		lcKeyList = lcKeyList + "0";
		loDiv = document.getElementById('qszone_txedit' + this.nFid);
		if (loDiv)
			loHttp.exec("qszone_txedit" + this.nFid, "/vd.asp?folderid=" + gnFidTranslateBE + "&SETVAR_qsi_txretfid=" + this.nFid + "&USERAW=YES&ADDVIEWDIV=YES&vp__extkeyid=" + lcKeyList + "&vp__extdcid=" + this.nDcId, 0);
		else	
			loHttp.exec("qszone_appbody", "/vd.asp?folderid=" + gnFidTranslateBE + "&SETVAR_qsi_txretfid=" + this.nFid + "&USERAW=YES&ADDVIEWDIV=YES&vp__extkeyid=" + lcKeyList + "&vp__extdcid=" + this.nDcId, 0);
	}
	
}

translateView.prototype.translateIt = function()
{
	var lcKeyList, lnFid, loHttp, loNodes, lnm;
	var lctranslation, lnCpId, lnKeyId, loItems, loItem;
	
	if (this.cKeyList !="")
	{
	
	
		lcKeyList = this.cKeyList + "0";
	
		lnFid = this.nFid;
		loHttp = new xmlDOC();
		
		loHttp.load(gnFidTranslate, "vp_inputfolderid=" + lnFid + "&vp__extkeyid=" + lcKeyList) 
					
		loNodes = loHttp.selectNodes("/xml/rs:data/z:row")

		for (lnm = 0 ; lnm < loNodes.length; lnm++)
		{
			lctranslation = loHttp.getAttribute(loNodes.item(lnm), "tsltranslation")
			lnCpId = loHttp.getAttribute(loNodes.item(lnm), "tslcpid")
			lnKeyId = loHttp.getAttribute(loNodes.item(lnm), "_extkeyid")
			
			loItems = this.aIds[lnCpId * 1000000 + lnKeyId];
	
			if (typeof(loItems) != 'undefined' && lctranslation)
			{
				for (lnj=0 ; lnj < loItems.length ; lnj++)
				{
					lcItem = loItems.item(lnj)
					loItem = document.getElementById(lcItem);
					if (loItem)
					{
						
						try 
						{
							if (loItem.firstChild.nodeName =='#text')
								loItem.innerHTML = lctranslation;
							else
								if (loItem.firstChild.tagName.toUpperCase()=='SPAN' && loItem.firstChild.className =='udf_fieldvalue')
									loItem.firstChild.innerHTML = lctranslation;			// this is the span of a display form field value 
								else
									loItem.innerHTML = lctranslation;			// display forms have an extra span
						} 
						catch(e) 
						{
							loItem.innerHTML = loItem.innerHTML + ' (tx Err)'
						}
						loItem.setAttribute('txDone', 2)
					}
				}
			}
		}
	}
}

translateView.prototype.addKey = function(tnKey, tnCpId, tcId)
{
	var lcStr;
	lcStr = "," + this.cKeyList ;
	if (lcStr.indexOf("," + tnKey +"," ) == -1 )
		this.cKeyList = this.cKeyList +  tnKey + ',';
		
	var lnIndex = tnCpId * 1000000 + tnKey;
	if (typeof(this.aIds[lnIndex])=='undefined')
		this.aIds[lnIndex] = new objCollection();
	this.aIds[lnIndex].add(tcId);
}

translateHandler.prototype.labelCollectElements = function(tcTagName, tlAll)
{
	var loItems, loItem, lni, lcTxId, lcKeyList, lcHtml, lcTag, lnLimit;
	var lcStr, lxDone;
	
	// Collect all items of a give tagname
	
	lcKeyList = '';
	
	loItems = document.getElements(tcTagName);
	// tlAll if true means collect all other wise only those not yet translated

	if (qsHandler.cTxCodeFamily=='')
		lnLimit=0
	else
		lnLimit = qsHandler.cTxCodeFamily.length
	
	
	for (lni = 0 ; lni < loItems.length ; lni++)
	{
		loItem = loItems[lni];
		if (tcTagName !='INPUT' || (typeof(loItem.type)=='string' && loItem.type!='hidden'))		// no tx of hidden inputs.
		{
			lcTxId = loItem.getAttribute('txId');
			if (typeof(lcTxId)!='string')
			{
				lcTxId = loItem.id;
				lcTag = lcTxId.substr(0,3);
				// if item does not contain a txId Attribute then if it is a qsi, qsa, qsz qs_ or fld then it is not to be translated
				// i also need to have a way to exclude any data items if I am collecting labels  -> done by excluding ids _tr
				if (lcTag=='qsi' || lcTag =='qsa'|| lcTag =='qsz' || lcTag =='qs_' || lcTag =='fld' || lcTag =='_tr' )
					lcTxId = null;
			}
			
			if (typeof(lcTxId)=='string' && (lnLimit==0 || lcTxId.substr(0,lnLimit)==qsHandler.cTxCodeFamily))
			{
				lxDone = loItem.getAttribute('txDone');
				if ((tcTagName =='INPUT' || tcTagName =='OPTION') && loItem.value !="")
				{
					
					if (lcTxId != "" && (tlAll || (lxDone!=1 && lxDone!=2)))
					{
						lcStr = "," + lcKeyList ;
						if (lcStr.indexOf("," + lcTxId +"," ) == -1 )
						{
							lcKeyList = lcKeyList  + lcTxId + ",";
							this.oLabels.add(new txLabel(lcTxId, loItem, tcTagName + lni, loItem.tagName, false))
						}
						else
							this.oLabels.add(new txLabel(lcTxId, loItem, tcTagName + lni, loItem.tagName, true))
						
						loItem.setAttribute('txDone', 1)
					}
				}
				else
					if (loItem.innerHTML != "" )
					{
						if (lcTxId != "" && !isnull(loItem.firstChild) && loItem.firstChild.nodeName=='#text' && (tlAll || (lxDone!=1 && lxDone!=2)))
						{
					
							lcStr = "," + lcKeyList ;
							if (lcStr.indexOf("," + lcTxId +"," ) == -1 )
							{
								lcKeyList = lcKeyList  + lcTxId + ",";
								this.oLabels.add(new txLabel(lcTxId, loItem, tcTagName + lni, loItem.tagName, false))
							}
							else
								this.oLabels.add(new txLabel(lcTxId, loItem, tcTagName + lni, loItem.tagName, true))
								
							
							loItem.setAttribute('txDone', 1)
						}
					}
			}
		}
	}
	return lcKeyList;
}

translateHandler.prototype.labelUpdateDom = function(toData)
{
	var lni, loLabel, lcTranslation, loNode, lnj;
	
	for (lni = 0 ; lni < this.oLabels.length ; lni++)
	{
		loLabel = this.oLabels.item(lni);
		loNode = toData.selectNodes("/xml/rs:data/z:row[normalize-space(@mtlcode)='" + loLabel.cTxId + "']")
		if (loNode.length > 0 )
		{
			lcTranslation = toData.getAttribute(loNode.item(0), "mtltext");
			if (loLabel.lMany)
			{
				loItem = document.getElements(loLabel.cTag + '[id="' + loLabel.cId + '"]');
				for (lnj=0; lnj< loItem.length; lnj++)
				{
					if (loLabel.lValue)
						loItem[lnj].value = lcTranslation;
					else
					{
						try {loItem[lnj].innerHTML = lcTranslation;} catch(e) {loItem[lnj].innerHTML = loItem[lnj].innerHTML + '(tx Err)'}
					}
					loItem[lnj].setAttribute('txDone', 2)
				}
			}
			else
			{
				loItem = document.getElementById(loLabel.cId);
				if (loLabel.lValue)
					loItem.value = lcTranslation;
				else
				{
					try {loItem.innerHTML = lcTranslation;} catch(e) {loItem.innerHTML = loItem.innerHTML + '(tx Err)'}
				}
				loItem.setAttribute('txDone', 2)
			}
		}
	}	
}



translateHandler.prototype.labelEditTranslation = function()
{
	var lni, loLabel, lcUrl, lcKeyList, loHttp;
	
	this.cExtraTags = qsHandler.cTxExtraTags;
	
	//
	// edit translation for label items (non data items)
	//

	lcKeyList = this.labelCollectElements('SPAN', true);
	lcKeyList = lcKeyList + this.labelCollectElements('TD', true);
	lcKeyList = lcKeyList + this.labelCollectElements('A', true);
	
	if (this.cExtraTags.indexOf('INPUT') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('INPUT', true);
	
	if (this.cExtraTags.indexOf('OPTION') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('OPTION', true);
	
	if (this.cExtraTags.indexOf('BUTTON') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('BUTTON', true);
	
	if (this.cExtraTags.indexOf('DIV') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('DIV', true);
	

	
	if (this.oLabels.length > 0 )
	{
		loHttp = new xmlHttpDom();
		for (lni = 0 ; lni < this.oLabels.length ; lni++)
		{
			loLabel = this.oLabels.item(lni);
			lcUrl = "/txbulkedit.asp?txfid=" + gnFidTranslateLbl + "&mtlcode=" + encodeURIComponent(loLabel.cTxId) + "&txt=" + encodeURIComponent(loLabel.cText)
			loHttp.callUrl("GET", lcUrl, false);
		}
		if (this.oLabels.length > 0)
		{
			lcKeyList = lcKeyList + "0";
			loDiv = document.getElementById('qszone_txedit' + this.nFid);
			loHttp.exec("qszone_appbody", "/vd.asp?folderid=" +  gnFidTranslateLbl + "&SETVAR_qsi_txretfid=" + this.nFid + "&USERAW=YES&ADDVIEWDIV=YES&vp_mtlcode=" + encodeURIComponent(lcKeyList), 0);
		}
	}
}



translateHandler.prototype.txLabels = function()
{
	//
	// translate label items
	//
	
	var lcKeyList, loHttp ;
	this.cExtraTags = qsHandler.cTxExtraTags;
	
	lcKeyList = this.labelCollectElements('SPAN', false);
	lcKeyList = lcKeyList + this.labelCollectElements('TD', false);
	lcKeyList = lcKeyList + this.labelCollectElements('A', false);
	if (this.cExtraTags.indexOf('INPUT') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('INPUT', false);
	
	if (this.cExtraTags.indexOf('OPTION') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('OPTION', false);
	
	if (this.cExtraTags.indexOf('BUTTON') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('BUTTON', false);
	
	if (this.cExtraTags.indexOf('DIV') > -1)
		lcKeyList = lcKeyList + this.labelCollectElements('DIV', false);
	
	if (lcKeyList !="")
	{
		lcKeyList = lcKeyList + "0";
		loHttp = new xmlDOC();
		loHttp.load(gnFidTranslateLbl, "vp_mtlcode=" + encodeURIComponent(lcKeyList) ) 
		this.labelUpdateDom(loHttp);
	}
	this.oLabels.length = 0;
}

txLabel = function(tcTxId, toElement, tcId, tcTag, tlMultiple)
{

	this.cTxId = tcTxId;
	this.cTag = tcTag;
	this.lMany = tlMultiple;
	if (toElement.id)
		this.cId = toElement.id;
	else
	{
		this.cId = tcId;
		toElement.id = tcId
	}
	if (tcTag =='INPUT')
	{
		this.cText = toElement.value;
		this.lValue = true;
	}
	else
	{
		this.cText = toElement.innerHTML;
		this.lValue = false;
	}
	
}
