

// 
// Firefox does not have event.keyCode. It only handles "document.onkeypress = function(e)"
// Save global lastEventCode for later use.
//
var lastEventCode = null;
if (typeof(window.event) != 'undefined') 
{
	lastEventCode = null;
}
else 
{
	document.onkeypress = function(e)
	{
		lastEventCode = e.keyCode;
		// if (e.target.nodeName.toUpperCase() == 'INPUT')
		// 			return isValidChar(e.keyCode);   // Keycode is always 0 exept for special chars.
	}
}

function isValidChar(ch)
{
	//
	// This function has serious problems with unicode...
	//
	return true;
	/*	
	return 		ch >= "A".charCodeAt(0) && ch <= "Z".charCodeAt(0) ||
				ch >= "a".charCodeAt(0) && ch <= "z".charCodeAt(0) ||
				ch == "Å".charCodeAt(0) || ch == "å".charCodeAt(0) ||
				ch == "Ä".charCodeAt(0) || ch == "ä".charCodeAt(0) ||
				ch == "Ö".charCodeAt(0) || ch == "ö".charCodeAt(0) ||
				ch == "É".charCodeAt(0) || ch == "é".charCodeAt(0) ||
				ch == "Ü".charCodeAt(0) || ch == "ü".charCodeAt(0) ||
				ch == "Á".charCodeAt(0) || ch == "á".charCodeAt(0) ||
				ch == "À".charCodeAt(0) || ch == "à".charCodeAt(0) ||
				ch == "Í".charCodeAt(0) || ch == "í".charCodeAt(0) ||
				ch == "Ì".charCodeAt(0) || ch == "Ì".charCodeAt(0) ||
				ch == "Ï".charCodeAt(0) || ch == "ï".charCodeAt(0) ||
				ch == "*".charCodeAt(0) || 
				ch == "?".charCodeAt(0) || 
				ch == "'".charCodeAt(0) ||
				ch == "-".charCodeAt(0) ||
				ch == " ".charCodeAt(0) ||
				ch == "#".charCodeAt(0) ||
				ch == "@".charCodeAt(0) ||
				ch == "[".charCodeAt(0) ||
				ch == "]".charCodeAt(0) ||
				ch == 13;		// RETURN
	*/
}

function eventToUpper()
{
	// Check if is character...
	event.returnValue = isValidChar(event.keyCode);

	event.keyCode = String.fromCharCode(event.keyCode).toUpperCase().charCodeAt(0);
	
	return event.returnValue;
}

function handleKeys(form, field)
{
	if (typeof(window.event) != 'undefined')
		lastEventCode = event.keyCode;
		
	if (lastEventCode == 8 && field > 1)		// BACKSPACE
	{
		if (document.forms[form].elements["L" + field].value.length > 0)
		{
			document.forms[form].elements["L" + field].value = "";
		}
		else
		{
			document.forms[form].elements["L" + (field - 1)].value = "";
			document.forms[form].elements["L" + (field - 1)].focus();
		}
		return;
	}

}

function fieldToUpper(form, field)
{
	document.forms[form].elements["L" + field].value = document.forms[form].elements["L" + field].value.toUpperCase();
}

function checkField(form, field)
{
	if (!isValidChar(document.forms[form].elements["L" + field].value.charCodeAt(0)))
		document.forms[form].elements["L" + field].value = "";
}

function checkFieldLong(form, field)
{
	sOld = document.forms[form].elements[field].value;
	sNew = "";
	for (i = 0; i < sOld.length; i++)
	{
//alert(sOld.charAt(i));
		if (isValidChar(sOld.charCodeAt(i)))
			sNew = sNew + sOld.charAt(i);
	}
	document.forms[form].elements[field].value = sNew;
}

function nextField(form, field)
{
	var len = document.forms[form].elements["L" + field].value.length;
	if (len >= 1)
			document.forms[form].elements["L" + (field + 1)].focus();
}

function antiEmailRobotImg(imgsrc, name, domain){
	document.write("<a href=" + "mail" + "t" + "o:" + name + "@" + domain + ">" + name + "<img valign=absmiddle src=" + imgsrc + " border=0>" + domain + "</a>");
}

function antiEmailRobot(name, domain){
	document.write("<a href=" + "mail" + "t" + "o:" + name + "@" + domain + ">" + name + "@" + domain + "</a>");
}

function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}

function HideElements(name, start, end)
{
    for (var x = start; x <= end; x++)
    {
        var element = document.getElementById(name + x);
        if (element != null)
            element.style.display = "none";
    }
}

//
// AJAX code
//

var xmlHttp;
var xmlHttp2;

function GetSearchResult(stype, sword)
{ 
	document.getElementById("resultWordCount").innerHTML = "Söker...";
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		document.getElementById("resultWordCount").innerHTML = "Your browser does not allow direct searches";
		return;
	}
	var url = "apSearchHandler.php";
	url = url + "?stype=" + stype;
	url = url + "&sword=" + sword;
	// url = url + "&restype=" + restype;
	xmlHttp.onreadystatechange = stateChangedWordCount;
	xmlHttp.open("GET", url + "&restype=wordcount", true);
	xmlHttp.send(null);

	xmlHttp2=GetXmlHttpObject();
	// url = url + "&restype=" + restype;
	xmlHttp2.onreadystatechange = stateChangedWordList;
	xmlHttp2.open("GET", url + "&restype=wordlist", true);
	xmlHttp2.send(null);
}

function stateChangedWordCount() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("resultWordCount").innerHTML = "Funna ord: " + xmlHttp.responseText;
	} 
}

function stateChangedWordList() 
{ 
	if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == "complete")
	{ 
		document.getElementById("resultWordList").innerHTML = xmlHttp2.responseText;
	} 
}

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;
}
