//{{{ Ajax Core

// debug switcher
	var _ajaxDebug = false;
//---------------------------------------------------------------------------}}}

//{{{ ajaxEncode
/**
 * create urlencoded POST-data for ajax
 * @param object params object containing params to POST
 * @return string
 */
function ajaxEncode(params)
{
	var result = '';
	var i = 0;
	var j = 0;
	if ( _ajaxDebug ) {
		alert(params.toString());
	}
	for ( i in params )
	{
		if ( i != 'ajaxTransmit' )
		{
			if ( 'object' == typeof params[i] )
			{
				for ( j = 0; j < params[i].length; j++)
				{
					if ( 'object' == typeof params[i][j] ) {
						result += '&' + params[i][j][0] + '=' + encodeURIComponent( params[i][j][1] );
					}
					else {
						result += '&' + i +'[' +j+ ']' + '=' + encodeURIComponent( params[i][j] );
					}
				}
			}
			else {
				result += '&' + i + '=' + encodeURIComponent( params[i] );
			}
		}
	}
	if ( _ajaxDebug ) {
		alert(result.toString());
	}
	return result;
}
//---------------------------------------------------------------------------}}}
//{{{ ajaxSend
/**
 * make request to the remote ajax handler
 * @param int action action code
 * @param object params request params
 */
function ajaxSend( action, params )
{
	function ajaxBindCallback()
	{
		if ( 4 == ajaxRequest.readyState )
		{
			if ( 200 == ajaxRequest.status )
			{
				if ( _ajaxDebug ) {
					alert(ajaxRequest.responseText);
				}
				eval( "var result = ("+ajaxRequest.responseText+")" );
				if ( result.AJAX_ERROR ) {
					createMessage( result.AJAX_ERROR, 'red', 3000 );
				}
				else
				{
					if ( ajaxTransmit ) {
						result.ajaxTransmit = ajaxTransmit;
					}
					handleAjax( result );
				}
			}
			else {
				alert("Error:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
		}
	}

	var ajaxRequest = null;
	if ( params.ajaxTransmit ) {
		var ajaxTransmit = params.ajaxTransmit;
	}

	if (window.XMLHttpRequest) {		// mozilla
		ajaxRequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {	// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (ajaxRequest)
	{
//		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open('POST', action, true );
		ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajaxRequest.send( ajaxEncode(params) );
	}
	else {
		createMessage( 'Use modern browser!', 'red', 3000 );
	}
}
//---------------------------------------------------------------------------}}}

/*============================================================================*
 *  END OF Ajax Core                                                          *
 *=========================================================================}}}*/

function hideMessage()
{
   with (document.getElementById('ajax_message'))
   {
		innerHTML = "";
		style.visibility = "hidden";
		style.color = "black";
   }
}

function createMessage(text, color, time_to_show)
{
	with(document.getElementById('ajax_message'))
	{
		style.visibility = "visible";
		innerHTML        = text;
		style.color      = color;
	}
	if ( time_to_show > 0 ) {
		setTimeout("hideMessage()", time_to_show);
	}
}

window.onscroll = setX;

function setX()
{
	if(document.getElementById('ajax_message')){
		document.getElementById('ajax_message').style.pixelTop = document.body.scrollTop + 200;
	}
}

/*============================================================================*
 * vim: set expandtab tabstop=3 shiftwidth=3 foldmethod=marker:               *
 *   END OF FILE                                                              *
 *============================================================================*/