﻿/*
* For the response data from ajaxed actions
*/

function AjaxActionResponse()
{
	// FUNCTIONS
	this.parseResponse = function(responseText)
	{
		var start = 0;
		var stop = 0;
		
		// Action string
		start = responseText.indexOf('<action>') + 8;
		stop = responseText.indexOf('</action>');
		
		this.actionString = responseText.substring(start, stop);
		
		// Cart Data - NOT REQUIRED
		if( responseText.indexOf('<cartid>') > -1 )
		{
			start = responseText.indexOf('<cartid>') + 8;
			stop = responseText.indexOf('</cartid>');
			this.cartId = responseText.substring(start, stop);
			
			if( this.cartId > 0 )
			{
				start	= responseText.indexOf('<cartuid>') + 9;
				stop	= responseText.indexOf('</cartuid>');
				
				this.cartGuid = responseText.substring(start, stop);
				
				start	= responseText.indexOf('<carttype>') + 10;
				stop	= responseText.indexOf('</carttype>');
				
				this.cartType = responseText.substring(start, stop);
			}
		}
		
		// Errors
		start	= responseText.indexOf('<errorcount>') + 12;
		stop	= responseText.indexOf('</errorcount>');
		this.errorCount = responseText.substring(start, stop);
		
		if( this.errorCount > 0 )
		{
			start = responseText.indexOf('<error>', start) + 7;
			stop	= responseText.indexOf('</error>', start);
			
			// returns - 1 when not found.. +7 == 6
			while( start > 6 )
			{
				this.errors.push(responseText.substring(start, stop));
				
				start = responseText.indexOf('<error>', start) + 7;
				stop= responseText.indexOf('</error>', start);
			}
		}
		
		// Messages
		// Errors
		start	= responseText.indexOf('<messagecount>') + 14;
		stop	= responseText.indexOf('</messagecount>');
		this.messageCount = responseText.substring(start, stop);
		
		if( this.messageCount > 0 )
		{
			start	= responseText.indexOf('<message>', start) + 9;
			stop	= responseText.indexOf('</message>', start);
				
			// returns - 1 when not found.. +7 == 8				
			while( start > 8 )
			{
				this.messages.push(responseText.substring(start, stop));
				
				start	= responseText.indexOf('<message>', start) + 9;
				stop	= responseText.indexOf('</message>', start);
			}
		}

	}

	this.getFormErrorResponse = function(errorString) {
		return errorString.split("|");
	}
	

	// MEMBERS
	this.actionString	= "";
	this.cartId			= 0;
	this.cartGuid		= "";
	this.cartType		= 0;

	this.errorCount		= 0;
	this.messageCount	= 0;
	
	this.errors			= new Array();
	this.messages	=	 new Array();
	
}
