//	SERVER-SIDE MODULE
//	(PLEASE DO NOT MODIFY THIS CODE)
//
//	The JS proxy to the King County Web Service (Locator_WS).
//
//	It exposes a set of verbs each which correspond to their server-side
//	equivalent. Some verbs include:
//
//	searchLocation ().
//
//	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_LOCATIONS.
//
//	Your application code would look as follows:
//	var	kc_ws = new Locator_WS (myAppObject);
//	kc_ws.searchLocations (...);		<<- sends the SEARCH_STORE request
//	myAppObject.searchLocations (...)	<<- 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 Locator_WS (callbackObject)
{
try {
	this.callbackObject = callbackObject;
	this.ahc = new AsyncHttpConn (this.WEB_SERVICE_URL, this);

} catch (e) { alert ("Locator_WS (callbackObject) threw: " + e); }
}

Locator_WS.prototype.findMeeting = function (meetingId)
{
	this.currentRequest 	= 	"FIND_THIS_MEETING";

	var				request =	"FIND_THIS_MEETING\n" +
								
								meetingId + '\n';
try {
	this.ahc.send (request, this.FIND_MEETING_URL);
} catch (e) { alert ("searchLocations () caught: " + e); }
}
Locator_WS.prototype.searchLocations = function (startDate, endDate, latitude, longitude, radiusKm, admin, onlySched)
{
	this.currentRequest 	= 	"SEARCH_LOCATIONS";

	var				request =	"SEARCH_LOCATIONS\n" +
								latitude + '\n' +
								longitude + '\n' +
								radiusKm + '\n' +
								startDate + '\n' + 
								endDate + '\n' + 
								(admin ? "1" : "0") + '\n' +
								(onlySched ? "1" : "0") + '\n';
try {
	this.ahc.send (request, this.WEB_SERVICE_URL);
} catch (e) { alert ("searchLocations () caught: " + e); }
}
Locator_WS.prototype.joinMeeting = function(userId,meetingId,obj)
{
 if(confirm("Are you sure you want to join this meeting?")){
 obj.display ='none';
 this.currentRequest 	= 	"JOIN_MEETING";
 var request =	"JOIN_MEETING\n" +							
								userId + '\n' +
								meetingId + '\n';
								
try 
{
	this.ahc.send (request, this.JOIN_SERVICE_URL);
} catch (e) {
	alert ("searchLocations () caught: " + e); }
 }
}


Locator_WS.prototype.changeProfile = function(userId,meetingId)
{
 

 this.currentRequest 	= 	"CHANGE_PROFILE";
 var request =	"CHANGE_PROFILE\n" +							
								userId + '\n' +
								meetingId + '\n';
								
try 
{
	this.ahc.send (request, this.CHANGE_PROFILE_SERVICE_URL);
} catch (e) {
	alert ("searchLocations () caught: " + e); }
 
}
Locator_WS.prototype.switchM = function(userId,meetingId)
{
 

 this.currentRequest 	= 	"SWITCH_MEETING";
 var request =	"SWITCH_MEETING\n" +							
								userId + '\n' +
								meetingId + '\n';
								
try 
{
	this.ahc.send (request, this.CHANGE_PROFILE_SERVICE_URL);
} catch (e) {
	alert ("searchLocations () caught: " + e); }
 
}

Locator_WS.prototype.update = function (host_ccfid, guest_ccfids)
{
	this.currentRequest	=	"UPDATE";

	var			request =	"UPDATE\n" +
							host_ccfid + '\n';
   	 
	for (var i=0; i<guest_ccfids.length; i++) {
		        guest = guest_ccfids[i];
                request +=	guest.id+'~'+guest.email+'~'+guest.first+'~'+guest.last+'~'+guest.ccfid+'~'+guest.snailm+'~'+guest.phone+'~'+guest.alPhone+'~'+
					        guest.gAdd+'~'+guest.gUnit+'~'+guest.gCity+'~'+guest.gState+'~'+guest.gZip+'\n';
				
				//request +=	guest_ccfids[i] + '\n';
	}
	try {
		this.ahc.send (request, this.ADMIN_WEB_SERVICE_URL);
	} catch (e) { alert ("update () caught: " + e); }
}

Locator_WS.prototype.email = function (host_ccfid, guest_ids)
{
	this.currentRequest	=	"EMAIL";

	var			request =	"EMAIL\n" +
							host_ccfid + '\n';
   	 
	for (var i=0; i<guest_ids.length; i++) {
		        guest = guest_ids[i];
                request +=	guest.id+'\n';
				
				//request +=	guest_ccfids[i] + '\n';
	}
	try {
		this.ahc.send (request, this.JOIN_SERVICE_URL);
	} catch (e) { alert ("update () caught: " + e); }
}

//
//	CALLBACK (PROTECTED)
//

//	Dispatches all responses from web service to the appropriate handler.

Locator_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];

	if (this.currentRequest == "SEARCH_LOCATIONS") {
		if (message == "OK") {
			locations = readMultipleLocations (lines, 1);
		}

		try {
		this.callbackObject.searchLocations (message, locations);
		} catch (e) {
			alert ("searchLocations () client-side handler threw an exception: " + e);
		}
	}
	else if (this.currentRequest == "UPDATE") {
		if (message == "OK") {
			lines.shift();
		}

		try {
		this.callbackObject.update (message, lines);
		} catch (e) {
			alert ("update () client-side handler threw an exception: " + e);
		}
	}
    else if (this.currentRequest == "JOIN_MEETING") {
		if (message == "OK") {
			lines.shift();
			
		}

		try {
		this.callbackObject.joinMeeting (message, lines);
		} catch (e) {
			alert ("Join Meeting () client-side handler threw an exception: " + e);
		}
	}
	else if (this.currentRequest == "CHANGE_PROFILE") {
		if (message == "OK") {
			lines.shift();
			
		}

		try {
		this.callbackObject.changeProfile (message, lines);
		} catch (e) {
			alert ("Change Profile () client-side handler threw an exception: " + e);
		}
	}
	else if (this.currentRequest == "SWITCH_MEETING") {
		if (message == "OK") {
			lines.shift();
			
		}

		try {
		this.callbackObject.changeProfile (message, lines);
		} catch (e) {
			alert ("Change Profile () client-side handler threw an exception: " + e);
		}
	}
	else if (this.currentRequest == "EMAIL") {
		if (message == "OK") {
			lines.shift();
			
		}

		try {
		this.callbackObject.emailResponse (message, lines);
		} catch (e) {
			alert ("EMAIL () client-side handler threw an exception: " + e);
		}
	}
	else
	if (this.currentRequest == "FIND_THIS_MEETING") {
		if (message == "OK") {
			meeting = readMultipleLocations (lines, 1);
		}

		try {
		this.callbackObject.searchLocations (message, meeting);
		} catch (e) {
			alert ("findMeeting () client-side handler threw an exception: " + e);
		}
	}

	} catch (e) {
		//	Typically invoked if response format is corrupt.
		alert ("Caught exception calling dispatchResponse (): " + e);
	}
}


//
//	PRIVATE
//

Locator_WS.prototype.WEB_SERVICE_URL = "/Kcmap-portlet/Location_WS";
Locator_WS.prototype.FIND_MEETING_URL = "/Kcmap-portlet/Location_WS";
Locator_WS.prototype.ADMIN_WEB_SERVICE_URL = "/Kcmap-portlet/Admin_WS";
Locator_WS.prototype.JOIN_SERVICE_URL = "/KCUser-portlet/Join_WS";
Locator_WS.prototype.CHANGE_PROFILE_SERVICE_URL = "/KCUser-portlet/Join_WS";


//
//	UTILITY FUNCTIONS
//

//	Reads multiple locations, and returns the locations as an
//	array. If no locations were read, an array of length 0 is returned.

function readMultipleLocations (lines, begin)
{
	var				locations = [];
	var				idx = begin;

	while (true) {
		var			loc = readLocation (lines, idx);

		if (loc == null) {
			break;
		}

		locations.push (loc);

		//	Advance to the next location
		idx += 37;
		
	}

	return locations;
}


//	Starting with 'lines[begin]' and continuing to 'lines[begin+13]' construct
//	and return a Locations object.
//	Return null if a fully formed location couldn't be read.

function readLocation (lines, begin)
{
	//	ARE THERE ARE SUFFICIENT LINES TO READ ENTIRE CONTENTS OF LOCATION

	if (begin + 36 > lines.length) {
		return null;
	}

	var				loc = {};

	//	READ LOCATION

	try {
	loc.email		= lines[begin];
	loc.ccf_id		= lines[begin+1];
	loc.hgc			= lines[begin+2];
	loc.ooc			= Number (lines[begin+3]);
	loc.first		= lines[begin+4];
	loc.last		= lines[begin+5];
	loc.hAddr		= lines[begin+6];
	loc.hUnit		= lines[begin+7];
	loc.hCity		= lines[begin+8];
	loc.hState		= lines[begin+9];
	loc.hPostCode	= lines[begin+10];
	loc.phone		= lines[begin+11];
	loc.host		= Number (lines[begin+12]);
	loc.sched		= lines[begin+13];
	loc.accomm		= Number (lines[begin+14]);
	loc.numGuests	= Number (lines[begin+15]);
	loc.gAddr		= lines[begin+16];
	loc.gUnit		= lines[begin+17];
	loc.gCity		= lines[begin+18];
	loc.gState		= lines[begin+19];
	loc.gPostCode	= lines[begin+20];
	loc.assist		= Number (lines[begin+21]);
	loc.assignedPhone	= lines[begin+22];
	loc.assignedName	= lines[begin+23];
	loc.lat			= Number (lines[begin+24]);
	loc.lng			= Number (lines[begin+25]);
	loc.memberSince	= lines[begin+26];
	loc.meetingstatus =Number (lines[begin+27]);
	loc.sText =lines[begin+28];
	loc.meetingId = Number (lines[begin+29]);
	loc.hId = Number (lines[begin+30]);
	loc.gId = Number (lines[begin+31]);
	loc.liferayId = Number (lines[begin+32]);
	loc.snailmail = lines[begin+33];
    loc.alPhone = lines[begin+34];
    loc.subject = lines[begin+35];
    loc.note = lines[begin+36];
	
	} catch (e) {
		loc = null;
	}
  
	return loc;
}