//set up the RD object
if(!window.RD){
	window.RD = {};
}

if(!RD.XHTML){
	RD.XHTML = {};
}

RD.XHTML.ExporterManager = function(aArgs){
	if(aArgs){
		this.init(aArgs)
	}
	
	return this;
}

RD.XHTML.ExporterManager.Managers = {};
RD.XHTML.ExporterManager.Managers.length = 0;
RD.XHTML.ExporterManager.prototype.constructor = RD.XHTML.ExporterManager;

RD.XHTML.ExporterManager.MaxChunkSize = 1900;

RD.XHTML.ExporterManager.prototype.init = function(aArgs){
	this._Exporters = {};
	this._Exporters.length = 0;
	this._id = RD.XHTML.ExporterManager.Managers.length;
	RD.XHTML.ExporterManager.Managers.length++;

	
	this._chunkSize = aArgs.chunkSize;
	if(!this._chunkSize || this._chunkSize > RD.XHTML.ExporterManager.MaxChunkSize){
		this._chunkSize = RD.XHTML.ExporterManager.MaxChunkSize;
	}
	else if( this._chunkSize < 2 ){
		this._chunkSize = 2;
	}
	
	this._exportTextScript = aArgs.exportTextScript;
	this._callbackObject = aArgs.callbackObject;
	this._callbackFunction = aArgs.callbackFunction;
	
	RD.XHTML.ExporterManager.Managers[this._id] = this;
};

RD.XHTML.ExporterManager.prototype.createExporter = function(aArgs){
    aArgs.callbackObject = this;
	aArgs.chunkSize = this._chunkSize;
	aArgs.id = this._id + "_" + this._Exporters.length;
	aArgs.exportTextScript = aArgs.exportTextScript?aArgs.exportTextScript:this._exportTextScript;
	aArgs.manager = aArgs.manager?aArgs.manager:this;
	aArgs.callback = this;
	this._Exporters.length++;
	
	this._Exporters[aArgs.id] = new RD.XHTML.TextExporter(aArgs);
	this._Exporters[aArgs.id].manager = aArgs.manager;

	return this._Exporters[aArgs.id];
};

RD.XHTML.ExporterManager.prototype.update = function(aExportObj){
	if(aExportObj.callbackObject){
		if(aExportObj.percent != 100){
			if(this._callbackObject.update){
				this._callbackObject.update(aExportObj.status);
			}
		}
		else{
			if(this._callbackFunction && typeof(this._callbackFunction) == "string"){
				this._callbackObject[this._callbackFunction](aExportObj.status);
			}
			else if(this._callbackObject.operationComplete){
				this._callbackObject.operationComplete(aExportObj.status);
			}
			else{
				this._callbackObject(aExportObj.status);
			}
		}
	}
};

RD.XHTML.TextExporter = function(aArgs) {
    if(aArgs){
		this.init(aArgs);
	}
	
	return this;
}

RD.XHTML.TextExporter.MaxChunkSize = 1900;

RD.XHTML.TextExporter.prototype.init = function(aArgs){
	this._exportWhole = (aArgs.exportWhole!=null)?aArgs.exportWhole:false;
	this._callbackObject = aArgs.callbackObject;
	this._callbackFunction = aArgs.callbackFunction;
	this.text = aArgs.text;
	if(this._exportWhole){
		var vUrl = aArgs.exportTextScript;
		if(vUrl.indexOf("?") < 0){
			vUrl += "?";
		}
		else{
			vUrl += "&";
		}
		
		getURL(vUrl + "content=" + this.text, this, true);
	}
	else{
		this._chunkSize = aArgs.chunkSize;
		if(!this._chunkSize || this._chunkSize > RD.XHTML.TextExporter.MaxChunkSize){
			this._chunkSize = RD.XHTML.TextExporter.MaxChunkSize;
		}
		else if( this._chunkSize < 2 ){
			this._chunkSize = 2;
		}
		
		this._id = (aArgs.id!=null)?aArgs.id:RD.XHTML.getUniqueId();
		this._exportTextScript = aArgs.exportTextScript;
		this._curPosition = 0;
		this._totalBytes = this.text.length;
		this._buffer = String.empty;
		this._status  = {};
		this._state = String.empty;
		this._percent = 0;
		this._fieldName = aArgs.fieldName?aArgs.fieldName:"content";
		
		this.exportText();
	}
};

RD.XHTML.TextExporter.prototype.update = function(status){
	if(this._percent != 100){
		var vContent = status.responseText.split("&");
		var vReplaceVars;

		for(var n=0;n<vContent.length;n++){
			if(vContent[n].indexOf("=") > -1){
				vReplaceVars = vContent[n].split("=");
				var vVariable = vReplaceVars[0]+"=";
				var vValue = vReplaceVars[vReplaceVars.length-1];
				var vIndex = this._exportTextScript.indexOf(vVariable);
				if(vIndex > -1){
					var vExportTextArray = this._exportTextScript.split("?");
					var vNewArray = vExportTextArray[vExportTextArray.length-1].split("&");
					for(var z=0;z<vNewArray.length;z++){
						if(vNewArray[z].stripSpaces().indexOf(vVariable) > -1){
							vNewArray[z] = vVariable.stripSpaces() + vValue.stripSpaces();
						}
					}

					vExportTextArray[vExportTextArray.length-1] = vNewArray.join("&");
					this._exportTextScript = vExportTextArray.join("?");
				}
			}
		}

		this.send();
	}
};

RD.XHTML.TextExporter.prototype.exportText = function(){
	this._state = "getChunk";
    this.send();
};

RD.XHTML.TextExporter.prototype.send = function(){
	if(!this.text.length && this._curPosition && this._state != "done"){
		this._state = "done";
		this._percent = 100;
	}

	var vText = this.text.substr(0, this._chunkSize);
	
	//RD.XHTML.Debug.getManager().write([vText]);

	this._curPosition += this._chunkSize;
	this.text = this.text.substr(this._chunkSize);
	
	var params = [];
	params.push("content=" + vText);
	params.push("fieldName=" + this._fieldName);
	
	if(this._curPosition == this._chunkSize){
		params.push("done=-1");
	}
	else{
		if(!this.text.length && !vText){
			params.push("done=1");
		}
		else{
			params.push("done=0");
		}
	}

	//alert("|" + this.text + "|")
	// params should have special characters converted to hex encoding
	// would be a good idea to wrap this in a try/catch block to
	// catch security violations
	var vUrl = this._exportTextScript;
	if(vUrl.indexOf("?") < 0){
		vUrl += "?";
	}
	else{
		vUrl += "&";
	}

	getURL(vUrl + params.join("&"), this, true);
};

RD.XHTML.TextExporter.prototype.operationComplete = function(status){
    if( status.success ){
		this._status = status;
		if(this._exportWhole){
			this._state = "done";
			this._percent = 100;
		}

		switch(this._state){
			case "done":
				if(this._callbackObject){
					if(this._callbackObject.update){
						this._callbackObject.update(this);
					}
					else if(this._callbackFunction && typeof(this._callbackFunction) == "string"){
						this._callbackObject[this._callbackFunction](status);
					}
					else if(this._callbackObject.operationComplete){
						this._callbackObject.operationComplete(status);
					}
					else{
						if(typeof this._callbackObject == "string"){
							eval(this._callbackObject);
						}
						else{
							this._callbackObject(status);
						}
					}
				}
				break;
			case "getChunk":
				this.update(status);
				break;
			default:
				alert("Unrecognized state: " + this._state);
				break;
		}
    }
	else{
		if(this._exportWhole){
			alert("getURL() error\n\n Text could not be exported");
		}
		else{
			this._status.success = false;
			this._status.content = "getURL() error";
		}
    }
};
