//
// Generic Data Access Object
// Execute an asynchronous request and
// execute method with result when success.
//
function DAO( url, callbackName, errorCallbackName )
{
	debugInfo( "DAO "+url+ " "+ callbackName );
	
	var httpRequest = GetHTTPRequestObject();
	httpRequest.onreadystatechange = function()
	{
		if (httpRequest.readyState == 4) 
		{
			debugInfo( "DAO "+url+ " httpRequest.readyState==4 "+ httpRequest );

			daostatus = 200;
			try { 
				daostatus = httpRequest.status;
			}catch(e)
			{
				debugFatal("DAO ERROR Exception "+e);
			}
	            if ( daostatus == 200) 
	            {
	           		debugInfo("DAO Received "+url+"\n"+callbackName );
	           		debugInfo( httpRequest.responseText );
	            	// Object received is JSON. Use eval to build object
	            	// in browser javascript engine based on returned stream
	            	if ( callbackName != "onUpdatedPosition" )
					{
						//alert(httpRequest.responseText);
		                eval( callbackName+"( "+httpRequest.responseText+" );" );
						}
		            else
		                eval( callbackName+"( \""+httpRequest.responseText+"\" );" );
		            
	                debugInfo("DAO callback "+callbackName+" done.");
	            } 
	            else 
	            {
	                debugInfo('There was a problem with the request.'+ httpRequest.status );
	                eval( errorCallbackName+"( "+httpRequest.responseText+" )" );                
	            }
				            
        }
	};
	httpRequest.open('GET', url, true);	// true async false sync
	httpRequest.setRequestHeader("Content-Type", "text/xml");	
	httpRequest.send('');
}
//
//
//xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//xmlhttp.send('rating=' + target.elements['rating'].value);
//
//
//
// Returns true if object given has an error property member
//
function HasErrorProperty(obj)
{
	return (typeof(obj.error) != "undefined");
}
//
// Get first object record or null if none
//
function GetFirstObject( jsonArray )
{
	if ( jsonArray == null )
		return null;
	//
	var oObjects = eval(jsonArray);
	//
	if ( HasErrorProperty( oObjects) )
		return null;
	//
	for ( var oId in oObjects)
	{
		var oObject = oObjects[oId];
		return oObject;
	}
	return null;
}
//
// DAO List (succession of synchronous callbacks)
//
function DAOCallbackEntry( url, callback, errorcallback )
{
	this.m_url 			= url;
	this.m_callback 	= callback;
	this.m_errorcallback= errorcallback;
	this.m_nextcallback = null;
}
var g_callbackIndex = 0;
var g_callbackList 	= new Array;

function DAOResetList( )
{
	g_callbackIndex = 0;
	g_callbackList 	= new Array;
}

function DAOAddToList( entry )
{
	g_callbackList[ g_callbackIndex ] = entry;
	if ( g_callbackIndex > 0 )
		g_callbackList[ g_callbackIndex-1 ] = entry;
	g_callbackIndex++;
}
function DAOExecuteList( )
{
	g_callbackIndex	= 0;
	DAOListExecuteNext( );
}
function DAOListExecuteNext( )
{
	//alert("DAO "+url+" "+callbackName);
	httpRequest = GetHTTPRequestObject();
	httpRequest.onreadystatechange = function()
	{
		if (httpRequest.readyState == 4) 
		{
            if (httpRequest.status == 200) 
            {
                eval( g_callbackList[g_callbackIndex].m_callback+"( "+httpRequest.responseText+" )" );
                g_callbackIndex++;
                if ( g_callbackIndex < g_callbackList.length )
                	DAOListExecuteNext( );
            } 
            else 
            {
                alert('There was a problem with the request.');
                eval( errorCallbackName+"( "+httpRequest.responseText+" )" );                
            }
        }
	};
	httpRequest.open('GET', g_callbackList[g_callbackIndex].m_url, true);
	httpRequest.send(null);
}
//
// DAO APIs
//
var DAOAPIROOT								=	"http://www.homelesscoach.org/socialinteraction/server/api/JSONApi.php?jsonAction=";
var DAOAPI_GETALLAREAS						= 	DAOAPIROOT + "GetAllAreas";

var DAOAPI_GETALLEVENTS						= 	DAOAPIROOT + "GetAllEvents";
var DAOAPI_CREATEEVENTS						= 	DAOAPIROOT + "CreateEvent";
var DAOAPI_DELETEEVENT						= 	DAOAPIROOT + "DeleteEventById&id=";
var DAOAPI_UPDATEEVENT						= 	DAOAPIROOT + "UpdateEvent";
var DAOAPI_GETALLSTREAMS					= 	DAOAPIROOT + "GetAllStreams";
var DAOAPI_GETSTREAMBYID					= 	DAOAPIROOT + "FindStreamById&id=";
var DAOAPI_GETALLSTREAMGROUPS				= 	DAOAPIROOT + "GetAllStreamGroups";
var DAOAPI_GETGPSARCHIVEDATEBYID			= 	DAOAPIROOT + "GetArchiveDateById&id=";
var DAOAPI_GETGPSLOCATIONSBYID				= 	DAOAPIROOT + "FindGPSLocationById&id=";
var DAOAPI_GETGPSLOCATIONSBYIDDATE			= 	DAOAPIROOT + "FindGPSLocationByIdAndDate&id=";
var DAOAPI_GETARCHIVEBYURL					= 	DAOAPIROOT + "GetArvhiveByURL&url=";
var DAOAPI_GETALLARCHIVES					= 	DAOAPIROOT + "GetAllArchives";
//var DAOAPI_GETALLARCHIVESBYDATE				= 	DAOAPIROOT + "GetAllArchivesByDate";
var DAOAPI_GETALLARCHIVESBYDATE				= 	DAOAPIROOT + "GetAllArchivesByDateDB";
var DAOAPI_GETCONTACTBYUSERNAME				= 	DAOAPIROOT + "FindContactsByUserName&username=";
var DAOAPI_ADDARCHIVETAG					=	DAOAPIROOT + "AddArchiveTag";
var DAOAPI_DELETEARCHIVETAG					= 	DAOAPIROOT + "DeleteArchiveTagById&id=";
//
//
//
