SeqConnector = function( processorUrl ){
	this.processorUrl=processorUrl;
	this.pending=false;
	
	this.sequence = new Array();
	this.curr_request_object=new Object();
	
	this.loadedCss=new Array();
	this.uploadButtons=new Array();
}
SeqConnector.prototype.sendRequest=function(){
	if( this.pending==true || this.sequence.length==0 )return;
	this.pending=true;
	this.curr_request_object=this.sequence.shift();	
	
	var xhr_connector=new Object();
	xhr_connector.content=this.curr_request_object.post_vars;
	xhr_connector.url=this.processorUrl;
	xhr_connector.handleAs="json";
	
	_this=this;
	xhr_connector.load=function( response_object ){
		_this.onResponse(response_object);
	}
	xhr_connector.error=function( response_object ){
		this.sendRequest();
	}
	dojo.xhrPost( xhr_connector );
}

SeqConnector.prototype.onResponse=function( response_object ){
	var commit_handler=true;
	this.pending=false;

	switch( response_object.type ){
		case 'wrn':
			alert( response_object.content );
		break;
		case 'strict':
			alert('Внимание:\n\n'+response_object.content);
			commit_handler=false;
		break;
		case 'error':
			alert('Програмная ошибка сервера:\n\n'+response_object.content);
			commit_handler=false;
		break;
		case 'kick':
			this.loadDialog(response_object.content,response_object.msg);
			//this.addToSequence(this.curr_request_object)
			this.freeze();
		break;
		case 'confirm':
			if( confirm(response_object.content) ){
				this.curr_request_object.post_vars._confirmed=1;
				this.addToSequence(this.curr_request_object)
				commit_handler=false;
			}
		break;
	}
	if( this.freezed==true )
		return;
	if( response_object.msg )
		alert(response_object.msg);
	if( commit_handler )
		this.commitResponseHandler( this.curr_request_object, response_object.content );
		
	this.sendRequest();
}
SeqConnector.prototype.commitResponseHandler=function( request_object, response_content ){	
	if( typeof request_object.listener == 'object' ){
		try{
			eval('request_object.listener.'+request_object.handler_name+'(response_content)');
		}
		catch(e){
			alert('SeqConnector Eval error:\n\n'+e);
		}
	}
	else if( typeof request_object.listener == 'function' ){
		request_object.listener(response_content);
	}
}

SeqConnector.prototype.addToSequence = function ( request_object ){
	this.sequence.push( request_object );
	this.sendRequest();
}
SeqConnector.prototype.addRequest = function ( post_vars, listener, handler_name ){
	var request_object=new Object();
	request_object.post_vars=post_vars;
	request_object.listener=listener;
	request_object.handler_name=handler_name;
	
	this.addToSequence( request_object );
}
SeqConnector.prototype.addRedirection=function( request ){
	var vars=this.serialize( request );
	location.href=this.processorUrl+'?'+vars;
}





SeqConnector.prototype.loadDialog=function( url, msg ){
	var xhr_connector={url:url};
	xhr_connector.load=function( html ){
		var dialog=document.createElement('div');
		dialog.id='SeqDialog';
		dialog.innerHTML=html;
		document.body.appendChild(dialog);
		dojo.byId('SeqDialogMsg').innerHTML=msg;
	}
	dojo.xhrGet( xhr_connector );
}
SeqConnector.prototype.freeze=function(){
	this.freezed=true;
}
SeqConnector.prototype.unfreeze=function(){
	document.body.removeChild(dojo.byId('SeqDialog'));
	this.freezed=false;
}
SeqConnector.prototype.goon=function(){
	this.unfreeze();
	this.addToSequence(this.curr_request_object)
	this.sendRequest();
}





SeqConnector.prototype.addCss=function( path ){
	if( this.loadedCss[path] )return;
	this.loadedCss[path]=true;
	
	var fileref=document.createElement("link")
	fileref.setAttribute("rel", "stylesheet")
	fileref.setAttribute("type", "text/css")
	fileref.setAttribute("href", path);	
	if (typeof fileref!="undefined")document.getElementsByTagName("head")[0].appendChild(fileref)
}



SeqConnector.prototype.serialize=function( object ){
	var str='';
	for( param in object ){
		if( typeof param == 'function' )continue;
		str+="&"+param+"="+object[param];
	}
	return str.substr(1);
}
