/* ---------------------------------------------------------------------- */
/* ---------------------------- Store ----------------------------------- */
/* ---------------------------------------------------------------------- */
/* Класс Аякс запроса */
/* url( String ) Урл */
/* params( Object ) Параметры запроса */
/* method( String ) Метод */

Store = function( url, params, target, method ) {
	Store.superClass.apply( this, arguments );
	
	this.Id = 'Store';
	this.target = target || false;
	this.url = url || false;
	this.method = ( ( method && method.toLowerCase() == 'get' ) ? 'GET' : 'POST' );
	this.params = params || new Array();
	this.Hfactory = null;
	this.avToProcess = true;
}
Store.inheritsFrom( IL );

/* ----------------------- Encode From Data ----------------------------- */

Store.prototype.encodeFromData = function( data ) {
	if( !data ) return false;
	var pairs = [];
	var regexp = /%20/g;
	for( var name in data ) {
		if( data[name] == undefined ) continue;
		var value = data[name].toString();
		var pair = encodeURIComponent( name ).replace( regexp, "+" ) + '=' + encodeURIComponent( value ).replace( regexp, "+" );
		pairs.push(pair);
	}
	return pairs.join('&');
}

/* ------------------------ Get Response -------------------------------- */

Store.prototype.getResponse = function( request ) {
	if( !request || !request.responseText ) return false;
	switch( request.getResponseHeader( "Content-Type" ) ) {
		case "text/xml" : return request.responseXML;
		case "text/json":
		case "text/json; charset=utf-8":
		case "text/javascript":
		case "application/json":
		case "application/json; charset=utf-8":
		case "application/javascript":
		case "application/x-javascript": return eval( '(' + request.responseText + ')' );
		default: return request.responseText;
	}
}

/* --------------------------- Request ---------------------------------- */

Store.prototype.request = function() {
	var factories = [
		function() { return new XMLHttpRequest(); },
		function() { return new ActiveXObject('Msxm12.XMLHTTP'); },
		function() { return new ActiveXObject('Microsoft.XMLHTTP'); }
	];
	if( this.Hfactory != null ) return this.Hfactory();
	for( var i = 0; i < factories.length; i++ ) {
		try{
			var factory = factories[i];
			var request = factory();
			if( request != null ) {
				this.Hfactory = factory;
				return request;
			}
		} catch(e) {
			continue;
		}
	}
	this.Hfactory = function() { throw new Error( 'XMLHttpRequest() not available' ); } 
	this.Hfactory();
}

/* ------------------------- Get Params --------------------------------- */

Store.prototype.getParams = function() {
	return this.params;
}

/* ------------------------- Set Target --------------------------------- */

Store.prototype.setTarget = function( target ) {
	this.target = target;
}

/* ------------------------- Set Params --------------------------------- */

Store.prototype.setParams = function( params ) {
	this.params = params;
}

/* ------------------------- Set Params --------------------------------- */

Store.prototype.addParams = function( params ) {
	for( var c in params ) {
		this.params[c] = params[c];
	}
}

/* --------------------------- Set URL ---------------------------------- */

Store.prototype.setUrl = function( url ) {
	url = url || false;
	if( typeof url != 'string' ) return false;
	this.url = url;
}

/* ---------------------------- Load ------------------------------------ */

Store.prototype.load = function() {
	if( !this.url ) return false;
	var request = this.request();
	this.avToProcess = true;
	var self = this;

	request.onreadystatechange = function() {
		if( request.readyState == 4 ) {
			if( request.status == 200 && self.avToProcess == true ) {
				self.notify( 'Loaded', self.getResponse( request ) );
				return true;
			} else {
				self.notify( 'Loaded', false );
				return false;
			}
		}
	}

	if( this.method == 'POST' ) {
		request.open( this.method, this.url );
		request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=utf-8' );
		request.send( this.encodeFromData( this.params ) || null );
	} else {
		var target = this.url;
		target += '?' + this.encodeFromData( this.params );
		request.open( this.method, target );
		request.send( null );
	}
}

/* ---------------------------- Stop ------------------------------------ */

Store.prototype.stop = function( flag ) {
	this.avToProcess = flag;
}

/* ---------------------------------------------------------------------- */
/* ---------------------------- Store ----------------------------------- */
/* ---------------------------------------------------------------------- */

Checker = function( url, params, target, method ) {
	this.Id = 'Checker';
	this.timer = false;
	Checker.superClass.apply( this, arguments );
}
Checker.inheritsFrom( Store );

Checker.prototype.load = function() {
	if( !this.url ) return false;
	var request = this.request();
	this.timer = false;
	this.avToProcess = true;
	var self = this;

	request.onreadystatechange = function() {
		if( request.readyState == 4 ) {
			if( request.status == 200 ) {
				var result = self.getResponse( request );
				if( result['Activity'] == 'DONE' && self.avToProcess == true ) {
					self.notify( 'Loaded', result );
					return true;
				} else if( ( result['Activity'] == 'ERROR' || !Boolean( result['Activity'] ) ) && self.avToProcess == true ) {
					self.notify( 'Error', result );
					return false;
				} else if( result['Activity'] == 'PROCESSING' && self.avToProcess == true ) {
					this.timer = window.setTimeout( function() { self.load.call( self ) }, 3000 );
					return;
				}
			} else {
				self.notify( 'Loaded', false );
				return false;
			}
		}
	}

	if( this.method == 'POST' ) {
		request.open( this.method, this.url );
		request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=utf-8' );
		request.send( this.encodeFromData( this.params ) || null );
	} else {
		var target = this.url;
		target += '?' + this.encodeFromData( this.params );
		request.open( this.method, target );
		request.send( null );
	}
}
