function get_http() {
	var xmlhttp;
/*@cc_on
	@if (@_jscript_version >= 5) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new
				ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	} @else {
		xmlhttp = false;
	}
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function get_ajax_url(app_alias,id) {
	return (app_alias ? ('ajax.php?app='+app_alias+(id ? ('&id='+id) : '')+'&rnd='+random()) : '');
}

function random() {
	return Math.floor(Math.random()*(10000000-10000+1))+10000;
}

ajaxLookupWorking = false;
function lookup(text,div_id,url) {

	var container = document.getElementById(div_id);
	if(!container) return;

	if(!is_lookup_key()) return;
	if(!text.length) {
		container.innerHTML = '';
		return;
	}
	if(text.length < 4) return;

	if(!this.http) this.http = get_http();
	if(!this.http) {
		alert('Ошибка при создании XMLHTTP объекта.');
		return;
	}
	if(!ajaxLookupWorking) {
		var http = this.http;
		url += "&search="+encodeURIComponent(text)+"&"+random();
		this.http.open("GET",url,true);
		this.http.onreadystatechange = function() {
			if(4 == http.readyState) {
				if(200 == http.status) container.innerHTML = http.responseText;
				ajaxLookupWorking = false;
			} else {
			}
		}
		ajaxLookupWorking = true;
		this.http.send(null);
	}
}

function is_lookup_key() {
    var e = window.event;
    if(!e) return true;
    var key = e.keyCode;
	return !((20==key) || (45==key) || ((key>=16) && (key<=18)) || ((key>=33) && (key<=40)));
}
