var _errors = [];

function getHttpRequest(url, params, _onload)
{
	var httpRequest = false;
	if (window.XMLHttpRequest) 
	{
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) 
		{
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) 
	{
		try 
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) 
			{
			}
		}
	}

	if (!httpRequest)
	{
		alert('Unfortunatelly you browser doesn\'t support this feature.');
		return false;
	}

	httpRequest.onreadystatechange = function()
	{
		if (httpRequest.readyState == 4)
		{
			if (httpRequest.status == 200)
			{
				if(elem_main = document.getElementById("xml_http_tmp"))
				{						
					//alert(httpRequest.responseText);						
					elem_main.innerHTML = httpRequest.responseText;
					process_responce(elem_main);
					if(_onload)
					{
						eval(_onload);
					}
					try{initAjaxLinks()}catch(e){}
					elem_main.innerHTML = '';
				}
			}
			else
			{
				alert('There was a problem with the request.(Code: ' + httpRequest.status + ')');
			}
		}
	}

	if(params == undefined || params == null)
	{
		httpRequest.open('GET', url, true);
		httpRequest.send(null);
	}
	else
	{
		httpRequest.open('POST', url, true);
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", params.length);		
		httpRequest.setRequestHeader("Connection", "close");
		httpRequest.send(params);
	}
}

function process_responce(tmp)
{
	var nodes = tmp.getElementsByTagName("div");
	for (var i=0; i < nodes.length; i++)
	{
		if(nodes[i].id == "js2run")
		{
			eval(nodes[i].innerHTML);
		}
		else
		{			
			var new_id = nodes[i].id.replace(new RegExp("-tmp"), "");
			if(new_id.length > 0)
			{
				var somediv = document.getElementById(new_id);
				if(somediv)
				{
					somediv.innerHTML = nodes[i].innerHTML;
				}
			}
		}
	}
	var nodes = tmp.getElementsByTagName("textarea");
	for (var i=0; i < nodes.length; i++)
	{
		var new_id = nodes[i].id.replace(new RegExp("-tmp"), "");
		if(new_id.length > 0)
		{
			var somediv = document.getElementById(new_id);
			if(somediv)
			{
				somediv.innerHTML = nodes[i].innerHTML;
			}
		}
	}
	tmp.innerHTML = '';
}

function initAjaxLinks()
{
	if(!document.getElementById('xml_http_tmp'))
	{
		var _div = document.createElement('div');
		_div.id = 'xml_http_tmp';
		_div.style.display = 'none';
		_body = document.getElementById('main');
		if(_body)
		{
			_body.appendChild(_div);
		}
	}
	var _as = document.getElementsByTagName('a');
	for(var i = 0; i < _as.length; i++)
	{
			var _href = _as[i].href;
			if(_href.length > 0 && _as[i].rel == 'ajax' && _as[i].href.indexOf("javascript") == -1)
			{
				_as[i].ajaxhref = _href + "?ajax";
				_as[i].href = 'javascript:;';
				_as[i].onclick = function(){
					getHttpRequest(this.ajaxhref, null, 'init_sifr()');
					return false;
				};
			}

		if(_as[i].rel == "blank")
		{
			_as[i].onclick = function()
			{
				window.open(this.href);
				return false;
			}
		}
	}
	return false;
}
/*
function initTargetBlank()
{
	var _as = document.getElementsByTagName('a');
	for(var i = 0; i < _as.length; i++)
	{
		if(_as[i].rel == "blank")
		{
			_as[i].onclick = function()
			{
				window.open(this.href);
				return false;
			}
		}
	}	
}
*/
window.onload = initAjaxLinks;

