var topWnd = window;
if (window.AjaxRequests == null) {
	while ((topWnd.parent != topWnd) && (topWnd.parent != null)) {
		topWnd = topWnd.parent;
		if (topWnd.AjaxRequests != null) {
			window.AjaxRequests = topWnd.AjaxRequests;
		}
	}
}

if (window.AjaxRequests == null) {
	window.AjaxRequests = new Array();
}

/*
function traceCallStack(fn) {
	var callStack = new Array();
	var f = fn;
	while (f.caller) {
		callStack[callStack.length] = f.caller.toString();
		f = f.caller;
	}
	return callStack;
}
*/

// загрузка данных через AJAX
function get_data(url, callback, async)
{
	url = prepareUrl(url);
	if (async == null)
		async = true;
	if (!async) {
		//alert("Синхронный Ajax-запрос.\nОтладка.");
	}
	var pos = url.indexOf('?');
	var data = '';
	if (pos >= 0){
		data = url.substring(pos + 1);
		url = url.substring(0, pos);
	}
	//window.AjaxRequests.push({url: url, data: data/*, callStack: traceCallStack(get_data)*/});
	var method = 'POST';
	if (window.xml_http == null)
		window.xml_http = CreateXmlHttp();
	if (window.xml_http_busy != null){
		//debugger;
		window.xml_http.abort();
		if (window.xml_http == null)
			window.xml_http = CreateXmlHttp();
	}
	window.xml_http.open(method, url, async);
	window.xml_http_callback = callback;
	window.xml_http.onreadystatechange = function()
	{
		if (window.xml_http == null)
			return;
		if (window.xml_http.readyState != 4)
			return;
		var text = window.xml_http.responseText;
		window.xml_http = null;
		window.xml_http_busy = null;
		window.AjaxRequests.pop();
		if (window.xml_http_callback != null)
			window.xml_http_callback(text);
	}
	if (data == null){
		window.xml_http.send('');
		return;
	}	
	window.xml_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	window.xml_http_busy = true;
	window.xml_http.send(data);
}

function xmlHttpAbort()
{
	if (window.xml_http != null)
	{
		window.xml_http.abort();
		window.xml_http_busy = null;
	}
}

function can_get_data()
{
	return (window.xml_http_busy == null);
}

function load_scripts(arr, func)
{
	window.scripts = arr;
	window.scripts_done = func;
	load_script();
}

function load_script()
{
	if (window.scripts.length == 0){
		window.scripts_done();
		return;
	}
	var script = window.scripts.shift();
	get_data('js/' + script, function(answer)
	{
		try{
			if (answer != ''){
				if (navigator.userAgent.toLowerCase().match(/webkit/)){
				    answer = 'try{ ' + answer + '}catch (err){ alert(script + ": " + err.message) } load_script()';
					window.setTimeout(answer, 0);
					return;
				}else if (window.execScript){
					window.execScript(answer);
				}else{
					window.eval(answer);
				}
			}
		}catch (err){
			alert(script + ': ' + err.message);
		}
		load_script();
	});
}

// Функция получает строчку из переданного по ссылке элемента и устанавливает
// её в специальную переменную для главного окна приложения.
function load_history(el)
{
	if (el.value != ''){
		window.saved_state = el.value;
	}else{
		window.saved_state = el.getAttribute('state');
	}
}


