//	SERVER-SIDE MODULE
//	(PLEASE DO NOT MODIFY THIS CODE)
//
//	The JS proxy to the Martha Stewart Living Omnimedia Web Service (MSLO_WS).
//
//	It exposes a set of verbs each which correspond to their server-side
//	equivalent. Some verbs include:
//
//	searchStore ().
//
//	USAGE:
//	Pass the constructor an application-level object, whose callback functions
//	will be invoked to accept any responses from the web service.
//	The name of these callback functions are identical to the name of the
//	verb.
//
//	For example, consider SEARCH_STORE.
//
//	Your application code would look as follows:
//	var	mslo_ws = new MSLO_WS (myAppObject);
//	mslo_ws.searchStore (...);		<<- sends the SEARCH_STORE request
//	myAppObject.searchStore (...)	<<- receives the SEARCH_STORE response
//
//	The interface is designed to be comfortable to a JS application developer
//	and is shielded from the low-level wire-protocol implementation mucky details.


//
//	PUBLIC
//

function MSLO_WS (callbackObject)
{
try {
	this.callbackObject = callbackObject;
	this.ahc = new AsyncHttpConn (this.WEB_SERVICE_URL, this);

} catch (e) { alert ("MSLO_WS (callbackObject) threw: " + e); }
}


MSLO_WS.prototype.ALL_RETAILERS=-1;
MSLO_WS.prototype.ALL_PRODUCTS=-1;
MSLO_WS.prototype.ROW_LIMIT=200;
MSLO_WS.prototype.MILE_TO_KM=1.609344;

MSLO_WS.prototype.searchStores = function (latitudeitude, longitudegitude, radiusKm, retailer, product)
{
	var				request =	"SEARCH_STORES\n" +
								this.ROW_LIMIT + "\n" +
								latitudeitude + '\n' +
								longitudegitude + '\n' +
								this.MILE_TO_KM*radiusKm + '\n' +
								retailer + '\n' +
								product + '\n';

try {
	this.ahc.send (request);
} catch (e) { alert ("searchStores () caught: " + e); }
}


//
//	CALLBACK (PROTECTED)
//

//	Dispatches all responses from web service to the appropriate handler.

MSLO_WS.prototype.ahcReply = function (message)
{
	try {

	var		lines = message.split ('\n');

	//	Remove the last line (which is empty).
	lines.pop ();

	//	Parse response
	var		message = lines[0];
	var		stores = [];

	if (message == "OK") {
		stores = readMultipleStores (lines, 1);
	}

	try {
	this.callbackObject.searchStores (message, stores);
	} catch (e) {
		alert ("searchStores () client-side handler threw an exception: " + e);
	}

	} catch (e) {
		//	Typically invoked if response format is corrupt.
		alert ("Caught exception calling dispatchResponse (): " + e);
	}
}


//
//	PRIVATE
//

//MSLO_WS.prototype.WEB_SERVICE_URL = "servlet/MSLO_WS";
//integration need full path because storelocator is deployed as seperated web app in weblogic:
MSLO_WS.prototype.WEB_SERVICE_URL = "/storelocator/servlet/MSLO_WS";



//
//	UTILITY FUNCTIONS
//

//	Reads multiple stores, and returns the stores as an
//	array. If no sstores were read, an array of length 0 is returned.

function readMultipleStores (lines, begin)
{
	var				stores = [];
	var				idx = begin;

	while (true) {
		var			store = readStore (lines, idx);

		if (store == null) {
			break;
		}

		stores.push (store);

		//	Advance to the next store
		idx += 14;
	}

	return stores;
}


//	Starting with 'lines[begin]' and continuing to 'lines[begin+13]' construct
//	and return a Stores object.
//	Return null if a fully formed store couldn't be read.

function readStore (lines, begin)
{
	//	ARE THERE ARE SUFFICIENT LINES TO READ ENTIRE CONTENTS OF STORE

	if (begin + 13 > lines.length) {
		return null;
	}

	var				store = {};

	//	READ STORE

	try {
	store.id			= Number (lines[begin]);
	store.latitude		= Number (lines[begin+1]);
	store.longitude     = Number (lines[begin+2]);
	store.retailer		= Number (lines[begin+3]);
	store.storeName		= lines[begin+4];
	store.locName		= lines[begin+5];
	store.address		= lines[begin+6];
	store.city			= lines[begin+7];
	store.state			= lines[begin+8];
	store.postCode		= lines[begin+9];
	store.country		= lines[begin+10];
	store.tel			= lines[begin+11];
	store.webURL		= lines[begin+12];
	store.hrsOperation	= lines[begin+13];

	} catch (e) {
		store = null;
	}

	return store;
}

