var isAjax = false;
var ajaxRequest = null;
var ajaxTimeOut = null;
var ajaxZIndex = 2000;

AjaxRequestDelay = function( aspId, tbId, reqDelay, reqUrl, reqParam, resId, mgTop, mgLeft )
{
	if ( window.XMLHttpRequest ) isAjax = true;
	else if ( window.ActiveXObject ) isAjax = true;
	if ( !isAjax ) return;
	if ( ajaxTimeOut ) {
		window.clearTimeout( ajaxTimeOut );
	}
	ajaxTimeOut = window.setTimeout( 'AjaxRequestEmit( \'' + aspId + '\', ' +
	                                                  '\'' + tbId + '\', ' +
	                                                  '\'' + reqUrl + '\', ' +
	                                                  '\'' + reqParam + '\', ' + 
	                                                  '\'' + resId + '\', ' + mgTop + ', ' + mgLeft + ' )', reqDelay );
}

AjaxRequestEmit = function( aspId, tbId, reqUrl, reqParam, resId, mgTop, mgLeft )
{
	if ( !document.getElementById( tbId ) ) return;
	var txt = document.getElementById( tbId ).value + '';
	if ( ajaxRequest && ajaxRequest.readyState < 4 ) ajaxRequest.abort();
	if ( txt != '' ) {
		if ( window.XMLHttpRequest ) {
			ajaxRequest = new window.XMLHttpRequest();
		} else if ( window.ActiveXObject ) {
			ajaxRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
		}
		ajaxRequest.onreadystatechange = function() { AjaxRequestCallBack( tbId, resId, mgTop, mgLeft ); };
		//self.alert(reqUrl + '?' + reqParam + '=' + txt);
		ajaxRequest.open( 'GET', reqUrl + '?' + reqParam + '=' + txt );
		if ( aspId != '' ) {
			ajaxRequest.setRequestHeader('Cookie', 'ASP.NET_SessionId=' + aspId);
		}
		ajaxRequest.send( null );
	} else {
		AjaxResultHide( resId );
	}
}

AjaxRequestCallBack = function( tbId, resId, mgTop, mgLeft )
{
	if ( ajaxRequest.readyState == 4 ) {
		if ( ajaxRequest.status == 200 ) {
			document.getElementById( resId ).innerHTML = ajaxRequest.responseText;
			if ( ajaxRequest.responseText + '' != '' ) {
				AjaxResultShow( tbId, resId, mgTop, mgLeft );
			} else {
				AjaxResultHide( resId );
			}
		}
	}
}

AjaxResultShow = function( tbId, resId, mgTop, mgLeft )
{
	var res = document.getElementById( resId );
	var tb = document.getElementById( tbId );
	var div = document.getElementById( tbId.replace( '_TB', '_DIV') ).style.zIndex = ajaxZIndex;
	ajaxZIndex = ajaxZIndex + 1;
	res.style.top = ( tb.offsetHeight + mgTop ) + 'px';
	res.style.left = mgLeft + 'px';
	res.style.display = 'block';
}

AjaxResultDelay = function( resId, hideDelay )
{
	window.setTimeout( 'AjaxResultHide( \'' + resId + '\' )', hideDelay );
}

AjaxResultHide = function( resId )
{
	document.getElementById( resId ).style.display = 'none';
}
