/**
*	Класс Ассинхронной загрузки документа
**/

//Конструктор
function ContentLoader(url, onload, onerror, method, secondParametr)
{
	try
	{
		this.url	 = url;
		this.req	 = null ;
		this.onload	 = (onload)  ? onload  : null;
		this.onerror = (onerror) ? onerror : this.defaultError;
		this.method  = (method)  ? method  : 'POST';
		this.secondParametr = (secondParametr) ? secondParametr : null;
		this.loadXMLDoc(url) ;
	}
	catch(err){}
}

ContentLoader.prototype = 
{
	//Константы готовности
	READY_STATE_UNINITIALIZED	: 0,
	READY_STATE_LOADING			: 1,
	READY_STATE_LOADED			: 2,	
	READY_STATE_INTERACTIVE		: 3,
	READY_STATE_COMPLETE		: 4,
	
	// Создание объекта HttpXmlRequest и загрузка документа
	loadXMLDoc:function(url)
	{
	    
	    try { this.req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
        
        try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
        
	    try { this.req = new XMLHttpRequest(); } catch(e) {}
	    
	   
	   if (this.req)
	   {
	   		try
	   		{
	   			
	   			var loader=this;
	   			this.req.onreadystatechange = function()
	   			{
	   				loader.onReadyState.call(loader);
				}
				// посылка запроса
				if(window.XMLHttpRequest)
				{
					this.req.open('GET', url, true);
					this.req.send(null);
				}
				else
				{
					this.req.open('GET', url, true);
					this.req.send();
				}
	  		}
			catch (err)
			{
				this.onerror.call(this) ;
			}
		}
		else
		{
			alert("XMLHttpRequest не поддерживается");
	   		return null;
		}
	},
	// Функция обратного вызова
	onReadyState:function()
	{
		try
		{
    		var req	 = this.req;
	       	if(!req) return false;		
		    var ready	= req.readyState;
			if (ready == this.READY_STATE_COMPLETE)
			{
				var httpStatus = req.status;
				if (httpStatus == 200 || httpStatus == 0)
				{
					if(this.onload != null)
						this.onload.call(this, this.secondParametr);
				}
				else
				{
					this.onerror.call(this);
				}
			}
		}
		catch(err){}
	},
	//Обработка ошибок (по умолчанию)
	defaultError:function()
	{
		str_alert = "Ошибка обработки данных! " + 
					"\n\nСтатус обработки: "    + this.getReadyState(this.req.readyState) + 
					"\nСтатус: "			   + this.req.status + 
					"\nHTTP заголовок: "	   + this .req.getAllResponseHeaders();
					
		alert(str_alert);
	},
	//Статус загрузки
	getReadyState:function(state)
	{
		switch(state)
		{
			case this.READY_STATE_UNINITIALIZED:
				return "Загрузка не инициализирована";			
			
			case this.READY_STATE_LOADING:
				return "Идет загрузка данных";			
				
			case this.READY_STATE_LOADED:
				return "Данные получены";			
				
			case this.READY_STATE_INTERACTIVE:
				return "???";			
				
			case this.READY_STATE_COMPLETE:
				return "Загрузка завершена";
		}
	},
	//статус загрузки
	getState:function()
	{
		return this.req.readyState;
	},
	//Получить XML Документ
	getXMLDoc:function()
	{
		if(this.getState() == this.READY_STATE_COMPLETE)
		{	
			if(window.XMLHttpRequest && this.req.responseXML.firstChild)
			{
				return this.req.responseXML;
			}
			else
			{
				IsLoad = this.req.responseXML.loadXML(this.req.responseText);
				if(IsLoad) return this.req.responseXML;	
			}
		}
		return false;
	}
}