// JS

function _(e) { return document.getElementById(e)?document.getElementById(e):false; }

// Comments functions

function msgLoading(id)
{
	_('rate'+id).innerHTML = '<font color=yellow>Загрузка...</font>';
}
function msgError(id)
{	_('rate'+id).innerHTML = '<font color=red>Ошибка при получении...</font>';
}
function RateClick(id,value)
{
	new AJAX('/rate.php',{dataSend: 'id='+id+'&value='+value, onLoading: 'msgLoading('+id+')', onError: 'msgError('+id+')'});
}

// Ajax functions

Function.prototype.bind = function(object) { var __method = this; return function() { return __method.apply(object,arguments); } }
var Class = { create: function() { return function() { this.initialize.apply(this,arguments); } } }
AJAX = Class.create();
AJAX.prototype = {
	initialize: function(url,options)
	{
		this.transport = this.getTransport();
		this.method = options.method || 'post'; this.method = this.method.toLowerCase();
		this.divOut = _(options.divOut) || null;
		this.dataSend = options.dataSend || '';
		this.jsRun = options.jsRun || null;
		this.onLoading = options.onLoading || 'msgLoading();';
		this.onError = options.onError || 'msgError();';
		this.onComplete = options.onComplete || null;
		this.execute(url);
	},
	execute: function(url)
	{
		if (this.onLoading) { eval(this.onLoading); }
		if (this.method == "get")
		{
			this.transport.open(this.method,url+"?"+this.dataSend,true);
			this.transport.onreadystatechange = this.onStateChange.bind(this);
			this.transport.send(null);
		}
		if (this.method == "post")
		{
			this.transport.open(this.method,url,true);
			this.transport.onreadystatechange = this.onStateChange.bind(this);
			this.transport.setRequestHeader("X-Requested-With","XMLHttpRequest");
			this.transport.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.transport.setRequestHeader("Content-Length",this.dataSend.length);
			if (this.transport.overrideMimeType) { this.transport.setRequestHeader("Connection","Close"); }
			this.transport.send(this.dataSend);
		}
	},
	onStateChange: function()
	{
		if (this.transport.readyState == 4)
		{
			if (this.transport.status >= 200 && this.transport.status < 300)
			{
				if (this.divOut) { setTimeout(function(){this.divOut.innerHTML = this.transport.responseText;}.bind(this),10); } else { if (this.onComplete) { setTimeout(function(){this.onComplete(this.transport);}.bind(this),10); } else { eval(this.transport.responseText); } }
				if (this.jsRun) { eval(this.jsRun); }
				this.transport.onreadystatechange = function(){};
			} else { if (this.onError) { eval(this.onError); } }
		}
	},
    getTransport: function() { if (window.ActiveXObject) { return new ActiveXObject('Microsoft.XMLHTTP'); } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return false; } }
};