
//------------------------------------------------------------------------------

function getParam(paramName)
{   	  
 	var get = new String(window.location);
  var x1 = get.indexOf(paramName);
	var x2 = get.indexOf(x1, "&");
	if (x2 == -1) x2 = get.length;
	return get.slice(x1,x2);
}


var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1 ;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;
net.TTM = 60*60*1000;

//Create Timer for TTM
var logout = function()
{
	location.href="/auth/logout";
}
var refreshLogoutTimer = function()
{
	if (window.logoutTimer && net.TTM)
	{
		clearTimeout(window.logoutTimer);
		window.logoutTimer = setTimeout("logout();", net.TTM);
	}
}

// 2 Конструктор

net.ContentLoader = function(url, onload, onerror, obj, method, params)
{	
	this.url = url;
	this.req = null ;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;	
	this.obj = obj;
	this.method = method ? method : "GET"
	this.baseParams = {};
	this.isAbort = false;	
	this.params = ounite({async:true}, params);
}

net.ContentLoader.prototype.loadXMLDoc = function(url)
{
	if (window.XMLHttpRequest){
		this.req = new XMLHttpRequest() ;
	} else if (window.ActiveXObject){
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	}
    window.req = this.req;
	if (this.req)
	    try {
		    var loader = this;
		    
		    if (this.params.async){
    		    this.req.onreadystatechange = function(){				
    			    loader.onReadyState.call(loader);					
    		    }
            }
            
		    url = url + "?" + this.addParam();
		    if (this.method == "POST"){
                this.req.open('POST', this.url, this.params.async);            
                this.req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                this.req.send(this.addParam());
            }
            else{
      		    
			    this.req.open('GET', url, this.params.async);
                this.req.setRequestHeader("X-Requested-With", "XMLHttpRequest");		        
                this.req.send(null);
            }
	    }
	    catch (err){
		    this.onerror.call(this);
	    }
        if(!this.params.async){
            loader.onReadyState.call(loader);
        }
}



net.ContentLoader.prototype.onReadyState = function()
{

	var req = this.req;
	
	var ready = req.readyState;
	if (!this.isAbort && ready == net.READY_STATE_COMPLETE)
	{		
		var httpStatus = req.status;		
		if (httpStatus == 403) 
		{
			if (req.responseText == 1)
			{
				alert("Внимание! Потеряна аутентификация.");
				location.href = "/";
				return;
			}
			if (req.responseText == 2)
			{
				alert("Нет доступа");				
				return;
			}
			alert("Ошибка! " + req.responseText);
		}
		if (httpStatus == 401)
		{
			location.href = "/";
		}
		
		if (httpStatus == 200 || httpStatus == 0)
		{			
			if (this.obj)
				this.onload.call(this.obj, this.req);
			else
				this.onload(this.req);
		}
		else
		{
			this.onerror(this.req, this.url, this.obj);
		}
		refreshLogoutTimer();
	}
}

net.ContentLoader.prototype.defaultError = function()
{
	if (this.req.status == 403) return;
    if (this.req.status == 401){
       alert('Потеряна аутентификация!');
       location.href = '/';
       return null;
    }
	alert('Произошла ошибка, обратитесь в службу технической поддержки!(код ошибки 12)'+
	'\n\nstatus: ' + this.req.status +
	'\nurl: ' + this.url);
}

net.ContentLoader.prototype.abort = function()
{
	if (this.req && this.req.readyState != net.READY_STATE_COMPLETE)
	{	
		this.isAbort = true;
		this.onerror.call(this.obj);
		this.isAbort = false;
	}		
}

function Request(url, callback, baseParams, onerror, method, obj, params)
{	
	var request = new net.ContentLoader(url, callback, onerror, obj, method, params);
	request.baseParams = baseParams;
	request.loadXMLDoc(url);			
}



net.ContentLoader.prototype.addParam = function()
{
	
	if (this.baseParams.length == 0) return url;
	var param = "";		
	for (var p in this.baseParams)
	{		
		//param += p + "=" + encodeURIComponent(unescape(this.baseParams[p])) + "&";				
		param += p + "=" + encodeURIComponent(this.baseParams[p]) + "&";
		//param += p + "=" + unescape(this.baseParams[p]) + "&";
		//param += p + "=" + (this.baseParams[p]) + "&";
	}		
	return param.slice(0,-1);
}


