var xmlreq = null;
var rivreq = null;
var old_text = '';

function siirraSrc() {
	var ke = document.getElementById('kuvaimg');
	ke.src = newsrc;
	this.src = newsrc = null;
}

var img = new Image();
img.onload = siirraSrc;
var newsrc = "";

function paivitaKuva(src) {
	var t = src.value;
	if (old_text != t) {
		teeKuvanPaivitys(t);
		old_text = t;
	}
}

function teeKuvanPaivitys(teksti) {
	img.src = "/ilmoitus/kuva.php?teksti=" + encodeURIComponent(teksti);
	newsrc = img.src;
	window.setTimeout("laskeRivit()", 100);
/*	if (null == xmlreq) {
		xmlreq = luoPyynto();
		if (null != xmlreq) {
			xmlreq.onreadystatechange = kasitteleKuvanLataus;
			xmlreq.open("GET", "/ilmoitus/uusilinkki.php?teksti=" + encodeURIComponent(teksti), true);
			xmlreq.send('');
		}
	}*/
}

function luoPyynto() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	return null;
}

function kasitteleKuvanLataus() {
	if (null != xmlreq && xmlreq.readyState == 4) {
		if (xmlreq.status == 200) {
			img.src = xmlreq.responseText;
			newsrc = img.src;
			xmlreq = null;
			window.setTimeout("laskeRivit()", 100);
		}
	}
}

function laskeRivit() {
	rivreq = luoPyynto();
	rivreq.onreadystatechange = kasitteleRivienMaara;
	rivreq.open("GET", "/ilmoitus/riveja.php", true);
	rivreq.send('');
}

function kasitteleRivienMaara() {
	if (null != rivreq && rivreq.readyState == 4) {
		if (rivreq.status == 200) {
			var riveja = rivreq.responseText;
			document.getElementById('riveja_display').innerHTML = riveja;
			document.getElementById('f_riveja_d').value = riveja;
			rivreq = null;
		}
	}
}

