var oldrequest = "", reload = 0, position = "top", sync = "on";
// HTTPí êMópÅAã§í ä÷êî
function createXMLHttpRequest(cbFunc)
{
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}

// document.getElementById
function $(tagId)
{
	return document.getElementById(tagId);
}
function dataSave(reload)
{
	textData = document.ajaxForm.request.value;
    if ((reload !== 0)||((textData !== "")&&(oldrequest !== textData)&&(sync !== "off"))) {
		httpObj = createXMLHttpRequest(displayData);
		if (httpObj)
		{
			httpObj.open("GET","parser.php?lang="+language+"&request="+encodeURIComponent(textData),true);
			httpObj.send(null);
	    	oldrequest = textData;
		}
/*		httpObj.open("POST","convert.php",true);
		httpObj.send("request="+encodeURI(textData));*/
	}
}
function displayData(){
	if ((httpObj.readyState == 4) && (httpObj.status == 200))
	{
		$("result_"+position).innerHTML = decodeURIComponent(httpObj.responseText);
        $("result_"+position).style.background = "#fff";
		$("html").innerHTML = decodeURIComponent(httpObj.responseText);
	}else{
		/*$("result").innerHTML = "<b>Wait...</b>";*/
        /*$("wait").innerHTML = "<img src='./image/loadinfo-small.gif'/>";*/
		if(position == 'top'){
	        $("result_"+position).style.background = "#fff url(./image/ajax-loader.gif) no-repeat center bottom";
		}else{
	        $("result_"+position).style.background = "#fff url(./image/ajax-loader.gif) no-repeat center top";
		}
	}
}
function clearQuery(){
    oldrequest = "";
	$("result_top").innerHTML = '';
	$("result_bottom").innerHTML = '';
	$("html").innerHTML = '';
	document.ajaxForm.request.value = '';
}
function setLang(){
	language = document.ajaxForm.lang.value;
	dataSave(1);
}
function setSync(){
	sync = document.ajaxForm.sync.value;
}
function setPosition(){
	$("result_"+position).innerHTML = "";
    $("result_"+position).style.background = "#fff";
	$("result_"+position).style.padding = '';
	position = document.ajaxForm.position.value;
	if (position == 'top'){
		$("result_"+position).style.padding = '0 0 24px 0';
	}else{
		$("result_"+position).style.padding = '24px 0 0 0';
	}
	dataSave(1);
}
onload = function (){
    setInterval( "dataSave(0)", 2000);
}