// Miva Merchant v5.x
//
// This file and the source codes contained herein are the property of
// MSB Acquisition, Inc. d/b/a Miva Merchant.  Use of this file is restricted
// to the specific terms and conditions in the License Agreement associated
// with this file.  Distribution of this file or portions of this file for
// uses not covered by the License Agreement is not allowed without a
// written agreement signed by an officer of MSB Acquisition, Inc. d/b/a Miva Merchant.
//
// Copyright 1998-2009 MSB Acquisition, Inc. d/b/a Miva Merchant  All rights reserved.
// http://www.mivamerchant.com
//
// $Id: ajax.js 23923 2010-04-28 22:54:59Z burch $
//
// Prefix         : MER-AJX-
// Next Error Code: 2    
//

/*!
 * \file
 * \brief	Functions that implement a browser-independent AJAX call mechanism
 */

// Order Processing Server-side AJAX calls
////////////////////////////////////////////////////

function AJAX_New()
{
	var http_request = null;

	if ( window.XMLHttpRequest )
	{
		http_request = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject )
	{
		http_request = new ActiveXObject( "Microsoft.XMLHTTP" );
	}

	return http_request;
}

function AJAX_Call( callback, session_type, func, parameters )
{
	var response;
	var http_request;
	
	if ( ( http_request = AJAX_New() ) == null )
	{
		return;
	}

	http_request.open( 'POST', json_url + 'Session_Type=' + encodeURIComponent( session_type ) + '&Store_Code=' + encodeURIComponent( Store_Code ) + '&Function=' + encodeURIComponent( func ), true );
	http_request.setRequestHeader( 'If-Modified-Since',	'Sat, 1 Jan 2000 00:00:00 GMT' );		// Avoid caching
	http_request.setRequestHeader( 'Content-type',		'application/x-www-form-urlencoded' );

	if ( MivaVM_API == 'Mia' && MivaVM_Version < 5.07 )
	{
		http_request.setRequestHeader( 'Connection',	'close' );
	}

	http_request.onreadystatechange = function()
	{
		if ( http_request.readyState == 4 )
		{
			if ( http_request.status == 200 )
			{
				try
				{
					response = eval( '(' + http_request.responseText + ')' );
				}
				catch ( e )
				{
					response				= new Object();
					response.success		= 0;
					response.error_code		= 'MER-AJX-00001';
					response.error_message	= 'Miva Merchant returned an invalid response.\n' +
											  'Function: ' + func + '\n' +
											  'Response: ' + http_request.responseText;
				}

				if ( response.error_code == 'session_timeout' )
				{
					location.reload();
					return;
				}

				callback( response );
			}

			http_request = null;
		}
	}

	if ( http_request.sendAsBinary )	http_request.sendAsBinary( parameters );
	else								http_request.send( parameters );

	return http_request;
}

function AJAX_Call_FieldList( callback, session_type, func, parameters, fields )
{
	for ( i = 0; i < fields.length; i++ )
	{
		parameters += '&' + encodeURIComponent( fields[ i ].name ) + '=' + encodeURIComponent( fields[ i ].value );
	}

	return AJAX_Call( callback, session_type, func, parameters );
}

function EncodeArray( unencoded_array )
{
	var i;
	var encoded = '';

	if ( unencoded_array.length == 0 )
	{
		return '';
	}

	for ( i = 0; i < unencoded_array.length - 1; i++ )
	{
		encoded += encodeURIComponent( unencoded_array[ i ] ) + '|';
	}

	encoded += encodeURIComponent( unencoded_array[ i ] );
	return encodeURIComponent( encoded );
}
