
	function XmlHttp()
	{
	
		this.requestObj = null;
		this.url = "";
		this.method = "";
		this.callBackFunc = null;
		this.errorCallbackFunc = null;
		this.errors = [];
		this.timeoutObj = null;
		
		this.Init = function()
		{
			var xh = null;
			try {
				xh = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					xh = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					xh = null;
				}
			}
			if(!xh && typeof XMLHttpRequest != "undefined") {
				xh = new XMLHttpRequest();
			}
			if (!xh) return false;
			this.requestObj = xh;
		}
		
		this.PrepareString = function(str)
		{
			return str.replace("&", "%26").replace("+", "%2B");
		}
		
		this.time = function()
		{
			return new Date().getTime();
		}
		
		ParseParams = function(params)
		{
			var str = "";
			for (var index in params)
			{
				str += index + "=" + params[index] + "&";
			}
			return str;
		}
		
		OnReadyStateChange = function(obj, req)
		{
			
		}
		
		this.SendRequest = function(vars, timeout, canTimedOutOn)
		{
			var obj = this;
			if (!canTimedOutOn) canTimedOutOn = 2;
			var req = this.requestObj;
			//vars.hash = "c15c139d2e5a1be2d562657b3701103db676b3a8";
			vars.hash = global_hash; 
			if (this.method == "post") {
				this.requestObj.open("POST", this.url, true);
				this.requestObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				//xmlHttp.setRequestHeader("Content-Type", "application/octet_stream");
			} else {
				this.requestObj.open("GET", this.url + "?" + ParseParams(vars), true);
			}
			var sendTime = this.time();
			this.requestObj.onreadystatechange = function() {
				//try { 
					if (req.readyState == 4) 
					{
						if (req.status == 200) {
							obj.callBackFunc ? obj.callBackFunc(req) : obj.standartCallbackFunc();
						} else {						
							obj.errorCallbackFunc ? obj.errorCallbackFunc() : obj.standartErrorCallbackFunc();
						}
					}
				//} catch (e) {
				//	obj.errors[obj.errors.length] = "Ошибка при обращении к серверу: " + e;
				//	obj.errorCallbackFunc ? obj.errorCallbackFunc(obj.GetErrors() + "xh error 1") : obj.standartErrorCallbackFunc();
				//}
			};
			if (timeout)
				this.timeoutObj = setTimeout(function(){obj.CheckRequestTimeout(canTimedOutOn);}, timeout);
			if (this.method == "post") this.requestObj.send( ParseParams(vars) );
				else this.requestObj.send(null);
		}
		
		this.CheckRequestTimeout = function(canTimedOutOn)
		{
			if (this.requestObj.readyState <= canTimedOutOn)
			{
				this.requestObj.onreadystatechange = null;
				this.requestObj.abort();
				this.errors[this.errors.length] = "Время ожидания ответа вышло.";
				this.errorCallbackFunc ? this.errorCallbackFunc(this.GetErrors(), this.requestObj.readyState) : this.standartErrorCallbackFunc();
			}
		}
		
		this.standartCallbackFunc = function()
		{
			
		}
		
		this.standartErrorCallbackFunc = function()
		{
		}
		
		this.GetErrors = function()
		{
			var str = "";
			for (var i=0;i<this.errors.length;i++) {
				str = this.errors[i] + "<br>";
			}
			return str;
		}
		
		
	}

	show_load_title = true;
  
	function CreateXMLHTTP() 
	{ 
		var xh = null;
		try {
			xh = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xh = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xh = null;
			}
		}
		if(!xh && typeof XMLHttpRequest != "undefined") {
			xh = new XMLHttpRequest();
		}
		if (!xh) return false;
		return xh;
	}
	
	//var timeout;
	
	function ConvertAmps(text) 
	{
		return text.replace("&", "%26");
	}
	
	function xh_prepare_str(str)
	{
		return str.replace("&", "%26").replace("+", "%2B");
	}
	
	function xhParseParams(params)
	{
		var str = "";
		for (var index in params)
		{
			str += index + "=" + params[index] + "&";
		}
		return str;
	}
 
	function sendXmlHttpRequest(method, url, vars, callBackFunc, errorCallbackFunc)
	{
		var xmlHttp = CreateXMLHTTP();
		if (xmlHttp) 
		{
			xmlHttp.onreadystatechange = function() 
			{
				//document.getElementById("load_title").innerHTML = xmlHttp.readyState;
				try { 
					if (xmlHttp.readyState == 4) {
						if (xmlHttp.status == 200) {
							callBackFunc ? callBackFunc(xmlHttp) : xhStandartBackFunc(xmlHttp);
							xhShowLoad(false);
						} else {
							//alert("Ошибка загрузки данных:\n" + xmlHttp.statusText); 	
							errorCallbackFunc ? errorCallbackFunc() : xhStandartErrorBackFunc();
						}
					}
				} catch (e) {
					//alert("Ошибка при обращении к серверу");
					xhShowLoad(false);
					errorCallbackFunc ? errorCallbackFunc() : xhStandartErrorBackFunc();
				}
			}
			xhShowLoad(true);
			//timeout = setTimeout();
			//vars = vars + "&hash=c15c139d2e5a1be2d562657b3701103db676b3a8";
			vars = vars + "&hash=" + global_hash;
			if (method == "post") {
				xmlHttp.open("POST", url, true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				//xmlHttp.setRequestHeader("Content-Type", "application/octet_stream");
				xmlHttp.send(vars);
			} else {
				xmlHttp.open("GET", url + "?" + vars, true);
				xmlHttp.send(null);
			}
		} else {
			alert("Ошибка! Объект не создан.");
		}		
	}
	
	function xhShowLoad(show)
	{
		if (show_load_title)
		if (show) {
			document.getElementById("load_title").style.display = "block";
			document.body.style.cursor = "wait";
		} else {
			document.getElementById("load_title").style.display = "none";
			document.body.style.cursor = "auto";
		}
		
	}

	function xhStandartBackFunc(xmlHttp)
	{
	}
	
	function xhStandartErrorBackFunc()
	{
	}	
	
	function ShowMsg(text, type, blockName)
	{
		if (text != "") {
			if (!blockName) blockName = "msg_block";
			var msgBlock = document.getElementById(blockName);
			msgBlock.className = type + "_block";
			msgBlock.style.display = "block";
			text += "&nbsp;&nbsp;&nbsp;<a href=\"javascript:///\" class=\"small_link\" onClick=\"CloseMsg('"+blockName+"');\">Спрятать</a>";
			msgBlock.innerHTML = text;
			
			var f = new Fader(blockName, "");
			f.fadeIn();
		}
	}
	
	//!
	function CloseMsg(blockName)
	{
		if (!blockName) blockName = "msg_block";
		var msgBlock = document.getElementById(blockName);
		if (msgBlock.style.display != "none") {		
			//msgBlock.style.display = "block";
			var f = new Fader(blockName, "hide");
			f.fadeOut();
		}
	}
	
	//---------
	var pm_auto_checker = new PmAutoChecker();
	pm_auto_checker.Init();
	//setInterval(function() {pm_auto_checker.checkPms()}, 1000);
	function PmAutoChecker()
	{		
		var lastsuccess = true;
		var req = null;
		
		this.Init = function()
		{
			req = new XmlHttp();
			req.Init();
			req.url = "/actions/pm.php";
			req.method = "post";
		}
		
		this.checkPms = function()
		{
			if (!lastsuccess) return false;
			var vars = {
				action: "get_new_count"
			};	
			lastsuccess = false;
			try {
				req.callBackFunc = function(jsHttp) 
				{
					lastsuccess = true;					
					if (jsHttp.responseText == "") return;
					eval("var json = " + jsHttp.responseText);
					
					if (json.Message.Type == "info") 
					{
						document.getElementById("pmUnreadCount").innerHTML = json.UnreadCount;
						document.getElementById("pm_unread_image").style.display = document.getElementById("pm_unread_container").style.display = (json.UnreadCount == 0) ? "none" : "inline";
						
					} else {
						//alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				};
				req.SendRequest(vars);
			} catch (e) {
				
			}
		}
	}

