<!--//

// XmlParser
function XmlParser()
{
	this.parserFunctionArray = [];	
}
XmlParser.prototype.addParserFunction = function(parserFunction)
{
	this.parserFunctionArray.push(parserFunction);
}
XmlParser.prototype.getParserFunction = function(index)
{
	return this.parserFunctionArray[index];
}

function FormValidator()
{
	this.validateFunctionArray = [];	
}
FormValidator.prototype.addValidateFunction = function(validateFunction)
{
	this.validateFunctionArray.push(validateFunction);
}
FormValidator.prototype.getValidateFunction = function(index)
{
	return this.validateFunctionArray[index];
}

function PageValidator()
{
	//this.elementArray = []; 
	this.validatorObject = new FormValidator();
}
PageValidator.prototype.attachValidator/*ToElement*/=function(/*elementID,*/ handler)
{
	//this.elementArray.push(elementID);
	this.validatorObject.addValidateFunction(handler);
}
PageValidator.prototype.validate = function()
{	
	var retVal = true;
	for(var i=0;i < this.validatorObject.validateFunctionArray.length; i++)
	{
		if (!(this.validatorObject.getValidateFunction(i))())
		{
			retVal = false;
		}
	}
	return retVal;
}

// PageUpdater
function PageUpdater()
{
	this.elementArray = []; 
	this.parserObject = new XmlParser();
}
PageUpdater.prototype.attachUpdaterToElement=function(elementID, handler)
{
	this.elementArray.push(elementID);
	this.parserObject.addParserFunction(handler);
}
PageUpdater.prototype.update = function(respXML)
{	
	var htmlOutput = "";

	for (var i = 0; i < this.elementArray.length; i++)
	{
		htmlOutput = "";
		
		// Execute the parser function and set it's output to the
		// HTML content area
		htmlOutput = (this.parserObject.getParserFunction(i))(respXML);
		if (this.elementArray[i] != "main_content")
		{
			var element = document.getElementById(this.elementArray[i]);
			if (!element) break;
			
			element.innerHTML = htmlOutput;
		}
		else
		{
			// Set the main content area to the initial value
			changeContentArea(DIRECTION_START, TIMEOUT_VALUE);
		}
	}
}

// Command
function Command(cmdName,cmdSubject,cmdQuantifier,cmdID,cmdLimit)
{
	this.name = cmdName;
	this.subject = cmdSubject;
	this.quantifier = cmdQuantifier;
	this.id = cmdID;
	this.limit = cmdLimit;
}
Command.prototype.serialize = function()
{
	var cmdOutput = "<cmd>";
	cmdOutput += "<name>" + this.name + "</name>";
	cmdOutput += "<subject>" + this.subject + "</subject>";
	cmdOutput += "<quantifier>" + this.quantifier + "</quantifier>";
	cmdOutput += "<id>" + this.id + "</id>";
	cmdOutput += "<limit>" + this.limit + "</limit>";
	cmdOutput += "</cmd>";
	return cmdOutput;
}

// Command Request Object
function CommandRequest()
{
	this.cmdArray = [];
	this.validatorObj = null;
	this.updaterObj = null;
}
CommandRequest.prototype.addCommand = function(cmdName,cmdSubject,cmdQuantifier,cmdID,cmdLimit)
{
	var cmd = new Command(cmdName,cmdSubject,cmdQuantifier,cmdID,cmdLimit);
	this.cmdArray.push(cmd);
}
CommandRequest.prototype.serialize = function()
{
	var output = "<CmdList>";
	for (var i = 0; i < this.cmdArray.length; i++)
	{
		output += this.cmdArray[i].serialize();
	}
	output += "</CmdList>";
	
	return output;
}

// Command Response Object
function CommandResponse(updater)
{
	// responseData used for processing
	this.responseData = 0;

	// States for readyState
	this.STATE_UNINITIALIZED = 0;
	this.STATE_OPENED = 1;
	this.STATE_SENT = 2;
	this.STATE_RECEIVING = 3;
	this.STATE_FINISHED = 4;
	
	// updater object to handle updates
	this.updaterObj = updater;
}
CommandResponse.prototype.handler=function(request,response)
{
	if (!request)
	{
		return false;
	}
	if (request.readyState == response.STATE_FINISHED)
	{
		if (request.status == 200)
		{
			// Pull back the XML from the request			
			response.responseData = request.responseXML;
			// Now call update for the updater object, passing the XML data
			response.updaterObj.update(response.responseData);
			
			return true;
		}
		else
		{
			// Handle invalid response status
			return false;
		}
	}
}
function FormRequest(updater,validator,formName)
{
	this.updaterObj = updater;
	this.validatorObj = validator;
	this.formItem = formName;
}
FormRequest.prototype.serialize = function()
{
	queryString = "";
	var formObject = document.getElementById(this.formItem);
	if (formObject == null)
	{
		return "";
	}
	var numberElements = formObject.elements.length;
	for (var i = 0; i < numberElements; i++)
	{
		if (i < numberElements - 1)
		{
			queryString += formObject.elements[i].name + "=" + encodeURIComponent(formObject.elements[i].value) + "&";
		}
		else
		{
			queryString += formObject.elements[i].name + "=" + encodeURIComponent(formObject.elements[i].value);
		}
	}
	return queryString;
}

// Ajax Transaction Object
function AjaxTransaction(url,req,resp)
{
	this.ASYNC_REQUEST = true;
	this.ajaxURL = url;
	this.ajaxRequest = req;
	this.ajaxResponse = resp;
	this.requestObj = false;
	
	if (window.XMLHttpRequest)
	{
		this.requestObj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		this.requestObj = new ActiveXObject("Msxml2.XMLHTTP");
		if (!this.requestObj)
		{
			this.requestObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else
	{
		this.requestObj = false;
	}
}

AjaxTransaction.prototype.send=function()
{
	validated = true;
	
	if ( this.ajaxRequest.validatorObj != null)
	{
		// If we weren't able to validate, adjust
		if (!this.ajaxRequest.validatorObj.validate())
		{
			validated = false;
		}
	}
	if ( validated )
	{
		// Clear the response data, make sure we receive new data
		this.ajaxResponse.responseData=0;
		
		// Send the request
		if (this.requestObj)
		{
			var response = this.ajaxResponse;
			var request = this.requestObj;
			// Use forwarding function to pass both the request and the response to the callback
			this.requestObj.onreadystatechange=function () { response.handler(request,response); }
			this.requestObj.open("POST",this.ajaxURL,this.ASYNC_REQUEST);
			this.requestObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			this.requestObj.send(this.ajaxRequest.serialize());
			
			// Update the user
			if ( this.ajaxRequest.updaterObj != null)
			{
				this.ajaxRequest.updaterObj.update();
			}
			
			return true;
		}
		else
		{
			// Request not created properly
			return false;
		}
	}
}

//-->