/*--------------------------------------------------|
| Site.Head Ajax Object                             |
|---------------------------------------------------|
| Copyright (c) 2006 Nikolai Liogenki               |
|                                                   |
| Updated: 13.03.2006                               |
|--------------------------------------------------*/


function SHAjaxRequest() {
	this.rq = false;
		try {
		  this.rq = new XMLHttpRequest();
		} catch (trymicrosoft) {
		  try {
		    this.rq = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (othermicrosoft) {
		    try {
		      this.rq = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch (failed) {
		      this.rq = false;
		    }
		  }
		}
	
	if (!this.rq) {
	  alert("Невозможно создать объект XMLHttpRequest!");
	}
	return this;
}


SHAjaxRequest.prototype.getResponse = function () {
  if (this.rq.readyState == 4)
    if (this.rq.status == 200) {
    	return this.rq.responseText;
    } else {
    	alert('Ошибка сервера: ' + this.rq.status);
    }
}

SHAjaxRequest.prototype.process = function (url, func) {
	this.rq.open("GET", url, true);
	this.rq.onreadystatechange = func;
	this.rq.send(null);
}

SHAjaxRequest.prototype.processPost = function (url, data, func) {
	this.rq.open("POST", url, true);
	this.rq.onreadystatechange = func;
	this.rq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded, charset=utf-8');
	this.rq.send(data);
}



function SHAjax() {
	this.request = new SHAjaxRequest();
	this.url = null;
	this.defaultReload = null;
	this.progressTag = '<img class="progress" src="/img/progress.gif" width="100" height="16" alt="">';
	return this;
}

SHAjax.prototype.setDiv = function(div, content) {
	var div = document.getElementById(div);
	if (div) {
		div.innerHTML = content;
	}
}

SHAjax.prototype.callUserFunction = function (_url, func) {
	this.request.process(_url, func);
}

SHAjax.prototype.progressBar = function(div) {
	this.setDiv(div, this.progressTag);	
}

SHAjax.prototype.response = function() {
	return this.request.getResponse();
}

SHAjax.prototype.processPost = function(_url, data, func) {
	this.request.processPost(_url, data, func);
}