var httpReq = createXMLHttpRequest();

function createXMLHttpRequest() 
{
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}	
	alert("XMLHttpRequest not supported");
	return null;
}

function haePienilista()
{	
	p = '_command_=Muistilista.PieniLista&Kieli=' + escape(document.getElementById('Kieli').value);
	httpReq.onreadystatechange = naytaPienilista;
	httpReq.open('POST', '/servlet/Muistilista', true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", p.length);
	httpReq.setRequestHeader("Connection", "close");
	httpReq.send(p);
}

function naytaPienilista()
{	
	if (httpReq.readyState == 4)
	{
		if (httpReq.status == 200)
		{
			if (httpReq.responseText.length > 5)
			{
				if (document.getElementById('pienimuistilista') && document.getElementById('muistilistabox'))
				{
					document.getElementById('pienimuistilista').innerHTML = httpReq.responseText;
					document.getElementById('muistilistabox').style.display = '';
				}
			}
			else
			{
				if (document.getElementById('muistilistabox'))
				{
					document.getElementById('muistilistabox').style.display = 'none';
				}
			}				
		}
	}
}

function haeMuistilistamaara()
{	
	url = "/servlet/Muistilista";
	pars = '_command_=Muistilista.Indikaatio';
	httpReq.open("POST", url, true);
	httpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpReq.onreadystatechange = naytaIndikaatio;
	httpReq.send(pars);		
}

function naytaIndikaatio()
{
	if (httpReq.readyState == 4)
	{
		if (httpReq.status == 200)
		{
			if (httpReq.responseText > 0)
			{
				if (document.getElementById('muistilistamaara'))
				{
					document.getElementById('muistilistamaara').innerHTML = httpReq.responseText;
					document.getElementById('muistilistabox').style.display = '';
				}
			}
			else
			{
				if (document.getElementById('muistilistabox'))
				{
					document.getElementById('muistilistabox').style.display = 'none';
				}
			}				
		}
	}
}

function lisaaMuistilistalle()
{
	url = "/servlet/Muistilista";
	pars = '_command_=Muistilista.Lisaa&';
	f = document.forms['muistilista'];
	arr = f.elements;
	cnt = 0;
	
	for (i=0; i<arr.length ; i++)
	{
		if (f.elements[i].name != "")
		{
			if (f.elements[i].type=='checkbox')
			{
				cnt++;
				if (f.elements[i].checked)
				{
					//pars += document.forms['muistilista'].elements[i].name + "=" + document.forms['muistilista'].elements[i].value + "&";		
					pars += f.elements[i].name + "=" + cnt + "&";
					f.elements[i].checked = false;			
				}
			}
			else
			{
				/*var uus = replaceAll(f.elements[i].value, 'ž', 'z');
				uus = replaceAll(uus, '–', '-');
				uus = replaceAll(uus, '€', 'E');
				uus = replaceAll(uus, 'š', 's');		
				uus = replaceAll(uus, 'Š', 'S');*/
				uus = muunnaMerkit(f.elements[i].value);
				pars += f.elements[i].name + "=" + escape(uus) + "&";				
			}
		}
	}		
	httpReq.open("POST", url, true);
	httpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpReq.onreadystatechange = naytaPienilista;
	httpReq.send(pars);			
}

function replaceAll(str, from, to)
{	
	while (str.indexOf(from) != -1)
	{
		str = str.replace(from,to);
	}
	return str;
}

function muunnaMerkit(mr)
{	
	ln = mr.length;		
	index = 0
	res = mr;
	for (index ; index<ln ; index++)
	{
		m = mr.charCodeAt(index);
		switch (m) 
		{
			case 352: 
				res = res.substring(0,index) + "S" + res.substring(index+1);
				break;
			case 382: 
				res = res.substring(0,index) + "z" + res.substring(index+1);
				break;
			case 8364: 
				res = res.substring(0,index) + "E" + res.substring(index+1);
				break;
			case 353: 
				res = res.substring(0,index) + "s" + res.substring(index+1);
				break;
			case 8211: 
				res = res.substring(0,index) + "-" + res.substring(index+1);
				break;		
			case 8217: 
				res = res.substring(0,index) + "'" + res.substring(index+1);
				break;			
		}
	}
	return res;
}
