function ververs()
{
	var f = document.frmVertaal
	location.href = 'http://' + location.hostname + location.pathname + '?gebrTaal=' + f.gebrTaal.value + '&vk=' + f.vk.value + '&ps=' + f.ps.value + '&bronTaal=' + f.bronTaal.value + '&doelTaal=' + f.doelTaal.value + '&teVertalen=' + f.teVertalen.value
}

var virtualLatinKeyboard
var virtualCyrillicKeyboard
var virtualGreekKeyboard
var virtualThaiKeyboard

function closeVirtKeyboards()
{
	if (virtualLatinKeyboard != null)
	{
		virtualLatinKeyboard.close()
	}
	if (virtualCyrillicKeyboard != null)
	{
		virtualCyrillicKeyboard.close()
	}
	if (virtualGreekKeyboard != null)
	{
		virtualGreekKeyboard.close()
	}
	if (virtualThaiKeyboard != null)
	{
		virtualThaiKeyboard.close()
	}
}

function showKeyboards(gebrTaal, show, formName, formElement)
{
	var title
	var latin
	var cyrillic
	var greek
	var thai
	switch (gebrTaal)
	{
		case 'afr':
			title = 'Virtuele toetsbord';
			latin = 'Latyns';
			cyrillic = 'Cyrillies';
			greek = 'Grieks';
			thai = 'Thai';
			hide = 'Verberg';
			break
		case 'deu':
			title = 'Virtuelle Tastatur ';
			latin = 'Lateinisch';
			cyrillic = 'Kyrillisch';
			greek = 'Griechisch';
			tai = 'Thai';
			hide = 'Verstecken';
			break
		case 'dut':
			title = 'Virtueel toetsenbord';
			latin = 'Latijns';
			cyrillic = 'Cyrillisch';
			greek = 'Grieks';
			thai = 'Thais';
			hide = 'Verbergen';
			break
		case 'epo':
			title = 'Virtuala klavaro';
			latin = 'Latina';
			cyrillic = 'Cirila';
			greek = 'Greka';
			thai = 'Taja';
			hide = 'Kaŝi';
			break
		case 'fra':
			title = 'Clavier virtuel';
			latin = 'Latin';
			cyrillic = 'Cyrillique';
			greek = 'Grec';
			thai = 'Taï';
			hide = 'Minimaliser';
			break
		default:
			title = 'Virtual keyboard';
			latin = 'Latin';
			cyrillic = 'Cyrillic';
			greek = 'Greek';
			thai = 'Thai';
			hide = 'Hide';
			break
	}
	document.write('<div id="vkeyb_disp" style="display:none">')
	document.write('<input type="button" value="' + title + '" onclick="unhideVKeyboardPanel()">')
	document.write('</div>')
	document.write('<div id="vkeyb" style="display:none">')
	document.write('<table bgcolor="#bdb76b" cellpadding="5">')
	document.write('<tr><th colspan="3">' + title + '</th><th><input type="button" value="' + hide + '" onclick="hideVKeyboardPanel()"></th></tr>')
	document.write('<tr><td>')
	var windowParams = "'/Web/Majstro/lat_kb.php?gebrTaal=" + gebrTaal + "&f=" + formName + "&e=" + formElement + "', 'VKeyboardL', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=310'"
	document.write('<input type="button" value="' + latin + '" onclick="virtualLatinKeyboard=window.open(' + windowParams + ')">')
	document.write('</td><td>')
	windowParams = "'/Web/Majstro/cyr_kb.php?gebrTaal=" + gebrTaal + "&f=" + formName + "&e=" + formElement + "', 'VKeyboardC', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=330'"
	document.write('<input type="button" value="' + cyrillic + '" onclick="virtualCyrillicKeyboard=window.open(' + windowParams + ')">')
	document.write('</td><td>')
	windowParams = "'/Web/Majstro/grk_kb.php?gebrTaal=" + gebrTaal + "&f=" + formName + "&e=" + formElement + "', 'VKeyboardG', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=310'"
	document.write('<input type="button" value="' + greek + '" onclick="virtualGreekKeyboard=window.open(' + windowParams + ')">')
	document.write('</td><td>')
	windowParams = "'/Web/Majstro/tai_kb.php?gebrTaal=" + gebrTaal + "&f=" + formName + "&e=" + formElement + "', 'VKeyboardT', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=330'"
	document.write('<input type="button" value="' + thai + '" onclick="virtualThaiKeyboard=window.open(' + windowParams + ')">')
	document.write('</td></tr></table></div>')
	if (show == 1)
	{
		unhideVKeyboardPanel()
	}
	else
	{
		hideVKeyboardPanel()
	}
}

function hideVKeyboardPanel()
{
	var b = document.getElementById('vkeyb')
	b.style.display = 'none'
	b = document.getElementById('vkeyb_disp')
	b.style.display = 'block'
	b = document.getElementById('vk')
	if (b != null)
	{
		b.value = '0'
	}
}

function unhideVKeyboardPanel()
{
	var b = document.getElementById('vkeyb')
	b.style.display = 'block'
	b = document.getElementById('vkeyb_disp')
	b.style.display = 'none'
	b = document.getElementById('vk')
	if (b != null)
	{
		b.value = '1'
	}
}

function setLangs(comb)
{
	document.frmVertaal.bronTaal.value = comb.value.substring(0, 3).toLowerCase()
	document.frmVertaal.doelTaal.value = comb.value.substring(4, 7).toLowerCase()
	ververs()
}

function assignLangs(frmVertaal, selCombNr)
{
	var allCookies = document.cookie
	if (allCookies == '')
	{
		return
	}
	var nextDate = new Date()
	nextDate.setDate(nextDate.getDate() + 7)
	bronTaal = frmVertaal.bronTaal.value.toUpperCase()
	doelTaal = frmVertaal.doelTaal.value.toUpperCase()
	frmVertaal.elements['s' + selCombNr].value = bronTaal + '-' + doelTaal
	var pos = allCookies.indexOf('snlK=')
	var start = pos + 5
	var end = allCookies.indexOf(';', start)
	if (end == -1)
	{
		end = allCookies.length
	}
	var value = allCookies.substring(start, end)
	start = 0
	var newCookieVal = ''
	for (var i = 1; i < 7; i++)
	{
		end = value.indexOf('0', start)
		if (end == -1)
			end = value.length
		var taalComb = value.substring(start, end)
		start = end + 1
		if (i != selCombNr)
		{
			newCookieVal = newCookieVal + taalComb + '0'
		}
		else
		{
			newCookieVal = newCookieVal + bronTaal + '-' + doelTaal + '0'
		}
	}
	document.cookie = 'snlK=' + newCookieVal.substring(0, newCookieVal.length - 1) + '; expires=' + nextDate.toGMTString(); 
}

function showPreSelect(s1, s2, s3, s4, s5, s6, tekst, tekst2, show, txtHide)
{
	document.write('<div id="psel_disp" style="none">')
	document.write('<input type="button" value="' + tekst + '" onclick="unhidePreSelectPanel()">')
	document.write('</div>')
	document.write('<div id="psel" style="display:none">')
	document.write('<fieldset>')
	document.write('<legend>' + tekst + '</legend>');
	document.write('<table cellpadding="5">')
	document.write('<tr align="center">')
	for (var i = 1; i < 7; i++)
	{
		document.write('<td><button type="button" style="width: 6em" value="' + i + '" onclick="assignLangs(frmVertaal, ' + i + ')">' + tekst2 + '</button></td>')
	}
	document.write('</td><td rowspan="2"><input type="button" value="' + txtHide + '" onclick="hidePreSelectPanel()">')
	document.write('</tr><tr align="center">')
	document.write('<td><input type="button" value="' + s1 +'" name="s1" onclick="setLangs(s1)"></td>')
	document.write('<td><input type="button" value="' + s2 +'" name="s2" onclick="setLangs(s2)"></td>')
	document.write('<td><input type="button" value="' + s3 +'" name="s3" onclick="setLangs(s3)"></td>')
	document.write('<td><input type="button" value="' + s4 +'" name="s4" onclick="setLangs(s4)"></td>')
	document.write('<td><input type="button" value="' + s5 +'" name="s5" onclick="setLangs(s5)"></td>')
	document.write('<td><input type="button" value="' + s6 +'" name="s6" onclick="setLangs(s6)"></td>')
	document.write('</tr></table></fieldset></div><br>')
	if (show == 1)
	{
		unhidePreSelectPanel()
	}
	else
	{
		hidePreSelectPanel()
	}
}

function hidePreSelectPanel()
{
	b = document.getElementById('psel')
	b.style.display = 'none'
	b = document.getElementById('psel_disp')
	b.style.display = 'block'
	b = document.getElementById('ps')
	b.value = '0'
}

function unhidePreSelectPanel()
{
	b = document.getElementById('psel')
	b.style.display = 'block'
	b = document.getElementById('psel_disp')
	b.style.display = 'none'
	b = document.getElementById('ps')
	b.value = '1'
}

function showPopup(id)
{
	b = document.getElementById(id)
	b.style.visibility = 'visible'
}

function hidePopup(id)
{
	b = document.getElementById(id)
	b.style.visibility = 'hidden'
}

function reverseSel(bron, doel, errMsg)
{
	var idxBron = bron.selectedIndex
	if (idxBron == 0)
	{
		alert(errMsg)
		return
	}
	var idxDoel = doel.selectedIndex
	bron.selectedIndex = idxDoel + 1
	doel.selectedIndex = idxBron - 1
}

function reverseSel2(bron, doel, errMsg)
{
	var idxBron = bron.selectedIndex
	bron.selectedIndex = doel.selectedIndex
	doel.selectedIndex = idxBron
}

function showMore(id)
{
	var elem = document.getElementById(id)
	elem.style.display = "table-row-group"
}

function AutoSuggestControl(oTextbox, oProvider)
{
	this.cur = -1
	this.layer = null
	this.provider = oProvider
	this.textbox = oTextbox
	this.timeoutId = null
	this.userText = oTextbox.value
	this.init()
}

AutoSuggestControl.prototype.hideSuggestions = function()
{
	this.layer.style.visibility = "hidden"
}

AutoSuggestControl.prototype.selectRange = function(iStart, iEnd)
{
	if (this.textbox.createTextRange)
	{
		var oRange = this.textbox.createTextRange()
		oRange.moveStart("character", iStart)
		oRange.moveEnd("character", iEnd - this.textbox.value.length)
		oRange.select()
	}
	else if (this.textbox.setSelectionRange)
	{
		this.textbox.setSelectionRange(iStart, iEnd)
	}
	this.textbox.focus()
}

AutoSuggestControl.prototype.typeAhead = function(sSuggestion)
{
	if (SuggestionProvider.lastSearch == this.textbox.value)
	{
		if (this.textbox.createTextRange || this.textbox.setSelectionRange)
		{
			var iLen = this.textbox.value.length
			this.textbox.value = sSuggestion
			this.selectRange(iLen, sSuggestion.length)
		}
	}
};

AutoSuggestControl.prototype.autosuggest = function(aSuggestions, bTypeAhead)
{
	this.cur = -1
	if (aSuggestions[0] != "")
	{
		if (bTypeAhead)
		{
			this.typeAhead(aSuggestions[0])
		}
		this.showSuggestions(aSuggestions)
	}
	else
	{
		this.hideSuggestions()
	}
}

AutoSuggestControl.prototype.handleKeyUp = function(oEvent)
{
	if (!containsWildCards(this.textbox.value))
	{
		var iKeyCode = oEvent.keyCode
		var oThis = this
		this.textbox.value = normalize(this.textbox.value)
		this.userText = this.textbox.value
		clearTimeout(this.timeoutId)
		if (iKeyCode == 8 || iKeyCode == 46)
		{
			this.timeoutId = setTimeout(function()
				{
					oThis.provider.requestSuggestions(oThis, false)
				}, 250)
		}
		else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123))
		{
			//ignore
		}
		else
		{
			this.timeoutId = setTimeout(function()
				{
					oThis.provider.requestSuggestions(oThis, true)
				}, 250)
		}
	}
	else
	{
		this.hideSuggestions()
	}
}

AutoSuggestControl.prototype.handleKeyDown = function(oEvent)
{
	switch(oEvent.keyCode)
	{
		case 38: //up arrow
			this.goToSuggestion(-1)
			break
		case 40: //down arrow
			this.goToSuggestion(1)
			break
		case 27: //esc
			this.textbox.value = this.userText
			this.selectRange(this.userText.length, 0)
			this.hideSuggestions()
			oEvent.returnValue = false
			if (oEvent.preventDefault)
			{
				oEvent.preventDefault()
			}
			break
		case 13: //enter
			clearTimeout(this.timeoutId)
			break
	}
}

AutoSuggestControl.prototype.init = function()
{
	var oThis = this;
	this.textbox.onkeyup = function(oEvent)
	{
		if (!oEvent)
		{
			oEvent = window.event
		}
		oThis.handleKeyUp(oEvent)
	}
	this.textbox.onkeydown = function(oEvent)
	{
		if (!oEvent)
		{
			oEvent = window.event
		}
		oThis.handleKeyDown(oEvent)
	}
	this.textbox.onblur = function()
	{
		oThis.hideSuggestions()
	}
	this.createDropDown()
}

AutoSuggestControl.prototype.highlightSuggestion = function(oSuggestionNode)
{
	for (var i=0; i < this.layer.childNodes.length; i++)
	{
		var oNode = this.layer.childNodes[i]
		if (oNode == oSuggestionNode)
		{
			oNode.className = "current"
		}
		else if (oNode.className == "current")
		{
			oNode.className = ""
		}
	}
}

AutoSuggestControl.prototype.createDropDown = function()
{
	this.layer = document.createElement("div")
	this.layer.className = "suggestions"
	this.layer.style.visibility = "hidden"
	this.layer.style.border = "1px solid black"
	this.layer.style.width = this.textbox.offsetWidth
	document.body.appendChild(this.layer)
	var oThis = this
	this.layer.onmousedown = this.layer.onmouseup = this.layer.onmouseover = function(oEvent)
	{
		oEvent = oEvent || window.event
		oTarget = oEvent.target || oEvent.srcElement
		if (oEvent.type == "mousedown")
		{
			oThis.textbox.value = oTarget.firstChild.nodeValue
			oThis.hideSuggestions()
		}
		else if (oEvent.type == "mouseover")
		{
			oThis.highlightSuggestion(oTarget)
		}
		else
		{
			oThis.textbox.focus()
		}
	}
}

AutoSuggestControl.prototype.getLeft = function()
{
	var oNode = this.textbox
	var iLeft = 0
	while(oNode.tagName != "BODY")
	{
		iLeft += oNode.offsetLeft
		oNode = oNode.offsetParent
	}
    return iLeft
}

AutoSuggestControl.prototype.getTop = function()
{
	var oNode = this.textbox
	var iTop = 0
	while(oNode.tagName != "BODY")
	{
		iTop += oNode.offsetTop
		oNode = oNode.offsetParent
	}
	return iTop
}

AutoSuggestControl.prototype.showSuggestions = function(aSuggestions)
{
	var oDiv = null
	this.layer.innerHTML = ""
	for (var i=0; i < aSuggestions.length; i++)
	{
		oDiv = document.createElement("div")
		oDiv.appendChild(document.createTextNode(aSuggestions[i]))
		this.layer.appendChild(oDiv)
	}
	this.layer.style.left = this.getLeft() + "px"
	this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px"
	this.layer.style.visibility = "visible"
}

AutoSuggestControl.prototype.goToSuggestion = function(iDiff)
{
	var cSuggestionNodes = this.layer.childNodes
	if (cSuggestionNodes.length > 0)
	{
		var oNode = null
		if (iDiff > 0)
		{
			if (this.cur < cSuggestionNodes.length-1)
			{
				oNode = cSuggestionNodes[++this.cur]
			}
		}
		else
		{
			if (this.cur > 0)
			{
                oNode = cSuggestionNodes[--this.cur]
            }
        }
		if (oNode)
		{
			this.highlightSuggestion(oNode)
			this.textbox.value = oNode.firstChild.nodeValue
		}
	}
}

function SuggestionProvider() {}

SuggestionProvider.req = null
SuggestionProvider.lastSearch = ""
	
SuggestionProvider.prototype.requestSuggestions = function(oAutoSuggestControl, bTypeAhead)
{
	if (SuggestionProvider.req == null)
	{
		if (window.XMLHttpRequest)
		{
			SuggestionProvider.req = new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
			try
			{
				SuggestionProvider.req = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e)
			{
				try
				{
					SuggestionProvider.req = new ActiveXObject("Microsoft.XMLHTTP")
				} 
				catch (e) {}
			}
		}
	}
	SuggestionProvider.lastSearch = oAutoSuggestControl.userText
	var url = "/Web/Majstro/suggest.php?input=" + encodeURI(oAutoSuggestControl.userText) + "&bronTaal=" + document.frmVertaal.bronTaal.value + "&doelTaal=" + document.frmVertaal.doelTaal.value
	SuggestionProvider.req.open("GET", url, true)
	SuggestionProvider.req.send(null)
	SuggestionProvider.req.onreadystatechange = function()
	{
		if (SuggestionProvider.req.readyState == 4)
		{
			if (SuggestionProvider.req.status == 200)
			{
				aSuggestions = SuggestionProvider.req.responseText.split("\t")
				oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead)
			}
		}
	}
}

function normalize(t)
{
	var n = ''
	var ch = ''
	for (var i = 0; i < t.length; i++)
	{
		ch = t.substring(i, i + 1)
		switch (ch)
		{
			case "'":
				ch = '’'
				break
			case "-":
				ch = '‐'
		}
		n += ch
	}
	return n
}

function containsWildCards(w)
{
	return w.indexOf('_') != -1 || w.indexOf('%') != -1
}
