/*
* функции для работы с AJAX
*
*
*/


/* в 1
* desc		sends AJAX request and passes the answer to response handling function
* param		arr request string
* param		str action url to send request to
* param		str name of the response handling function
* params	mix params to be passed to response handling function (first always is an AJAX response object)
*/
function ajaxRequest()
{
	if (arguments.length < 3)
	{
		alert('Expecting at least three arguments: request string, action URL and response function name');
		return;
	}

	var req_str		= arguments[0];
	var act_url		= arguments[1];
	var resp_func	= arguments[2];

	// the rest arguments are for response handling function
	var resp_args	= '';

	for (var i = 3; i < arguments.length; i++)
	{
		resp_args += ', ' + arguments[i];
	}

	var ajax_req	= false;

	if (window.XMLHttpRequest)
	{
		try 
		{
			ajax_req = new XMLHttpRequest();
		} catch(e) {
			ajax_req = false;
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			ajax_req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try 
			{
				ajax_req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				ajax_req = false;
			}
		}
	}

	if (ajax_req)
	{
		ajax_req.onreadystatechange = function()	{
														if (ajax_req.readyState == 4)	// only if req shows "loaded"
														{
															if (ajax_req.status == 200)	// only if "OK"
															{
																eval(resp_func + '(ajax_req' + resp_args + ')');

															} else {

																alert("There was a problem retrieving the XML data:\n" + ajax_req.statusText);
															}	// else
														}	// if
													}

		ajax_req.open('POST', act_url + '?ms=' + new Date().getTime());

		ajax_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		ajax_req.send(req_str);
	}	// if (ajax_req)

}	// function


/* в 1	TODO: доделать и протестировать на сложных скриптах
* desc		находит в строке и выполняет скрипты
* param		str
*
* return	str строка с вырезанными скриптами
*/
function evalScript(str)
{
	var script = new RegExp(/<script[\s\w="]*>([\w\(\)\{\}'"\!\;\s\t\n\,\.\\\=]+)<\/script>/gim);

	while (fnd = script.exec(str))
	{
		eval(fnd[1]);
		str = RegExp.leftContext + RegExp.rightContext;
	}


	return str;
}