Function.prototype.bind = function(object) {
    var method = this
    return function() {
        return method.apply(object, arguments) 
    }
}


var ag_server=Class.create();
var ag_server_static_connections = 0;
ag_server.prototype = {
initialize: function(){
    this._url		= "http://www.ag.ru/ajax/";
    this._code		= "";
    this._form		= "";
	this._method	= "get";
	this._param		= {}; 
	this._server_obj= {}; 
	this._client_obj= {}; 
	this._client_obj['handler'] = {}; 
	this._client_obj['data'] = {}; 
	this._error_message	= "";
	this._warning_message	= "";
	this._is_connected	= 0;
},

set_method: function(m){
	this._method = m;
},


form_serialize: function(form_id){
	this._form = Form.serialize(form_id);
},

set_url: function(p){
    this._url = p;
},

get: function(p){
	return this._server_obj[p];
},

data: function(p){
	return this._server_obj;
},

set_code: function(p){
    this._code = p;
},

add_handler: function(key,value){
	this._client_obj['handler'][key] = value;
},

on_complete: function(value){
	this._client_obj['handler']['on_complete'] = value;
},

on_load: function(value){
	this.on_complete(value);
},

set_param: function(key,value){
	this._param[key] = encodeURIComponent(value);
},

set_data: function(key,value){
	this._client_obj['data'][key] = value;
},

get_data: function(key){
	return this._client_obj['data'][key];
},

load: function(){
	this._client_obj['request'] = '';
	this._client_obj['exception'] = '';
	this._client_obj['json'] = '';
	ag_server_static_connections++;
	this._is_connected = 1;
	this.check_browser_connections();
	this.check_connections();
	var myAjax = new Ajax.Request(
		this._url, 
		{
			method: this._method, 
			parameters: this._param_string(), 
			onComplete: this._on_complete.bind(this),
			onFailure:  this._on_failure.bind(this),
			onException: this._on_exception.bind(this)
		}
	);
},

is_error: function(){
	return this._server_obj['is_error'];
},

error_text: function(){
	return this._server_obj['error_message'];
},

is_warning: function(){
	return this._server_obj['is_warning'];
},

warning_text: function(){
	return this._server_obj['warning_message'];
},


_on_complete: function(request,json){
	ag_server_static_connections--;
	this._is_connected = 0;
	this.check_browser_connections();
	this.check_connections();
	if (ag_server_static_connections < 0) ag_server_static_connections = 0;
	eval(request.responseText);
	this._client_obj['request'] = request;
	this._client_obj['json'] = json;
	if(this._server_obj['is_error']){
		this._error_message = this._server_obj['error_message'];
	}
	if(this._server_obj['is_warning']){
		this._warning_message = this._server_obj['warning_message'];
	}
	if(this._client_obj['handler']['on_complete']){
		this._client_obj['handler']['on_complete'](this,request,json);
	}
},

_on_failure: function(request){
	ag_server_static_connections--;
	this._is_connected = 0;
	this.check_browser_connections();
	this.check_connections();
	if (ag_server_static_connections < 0) ag_server_static_connections = 0;
	this._client_obj['request'] = request;
	alert('Ошибка соединения с сервером');
},

_on_exception: function(request, exc){
	ag_server_static_connections--;
	this._is_connected = 0;
	this.check_browser_connections();
	this.check_connections();
	if (ag_server_static_connections < 0) ag_server_static_connections = 0;
	this._client_obj['request'] = request;
	this._client_obj['exception'] = exc;
	alert("exception:\n"+exc.message+'\n'+request.responseText);
},

is_browser_connected: function(){
	return ag_server_static_connections;
},

browser_connections_count: function(){
	return ag_server_static_connections;
},

check_browser_connections: function(){
	if(this._client_obj['handler']['check_browser_connections']){
		this._client_obj['handler']['check_browser_connections'](this,this.browser_connections_count());
	}

},

check_connections: function(){
	if(this._client_obj['handler']['check_connections']){
		this._client_obj['handler']['check_connections'](this,this.is_connected());
	}

},

on_change_connection_count: function(value){
	this._client_obj['handler']['check_browser_connections'] = value;
},

on_change_connection: function(value){
	this._client_obj['handler']['check_connections'] = value;
},

is_connected: function(){
	return this._is_connected;
},

_param_string: function(){
	this._param_str = this._form;
	this._form = '';

	if (this._code != "") {
		this._param_str = this._param_str + "&code=" + this._code;
	}
	else{
		return this._fatal_error('Не задан необходимый параметр');
	}

	for (var key in this._param){
		this._param_str = this._param_str + "&" + key + "=" + this._param[key];
	}
	this._param_str = this._param_str + "&" + this._rand_tail();
	return this._param_str;
},

_fatal_error : function (msg)
{
	alert('[ag_server] '+msg);
},

_rand_tail: function ()
{
	return 'rand='+Math.random()+'12'+Math.random();
}

}


