/*global Ajax, Services */

if (typeof this.Services === "undefined") { this.Services = {}; }

//////////
//	general purpose functions
//////////
function $(id) { return document.getElementById(id); }

function debug( msg ) {
	// log to Firebug
	try { 
		console.log(msg); 
	} catch (e) { 
	}
}

function debugObj( obj ) {
	// log to Firebug
	try { 
		console.dir(obj); 
	} catch (e) { 
	} 
}

////////////////////////////////////////////////////////////
//  Services
////////////////////////////////////////////////////////////
Services.parseJSON = function( strObject ) {
	// this requires json2.js from www.json.org

	// may encounter date issue:
	// NewtonSoft JSON.Net library returns dates like this 
	//   => "StatusWhen": new Date(1107388800000)
	// if so, revert to eval or 'reviver' function to JSON.parse

	//return eval( '(' + strObject + ')' );		// evil but necessary
	return JSON.parse(strObject);
};

Services.isRequestOK = function() {
	var status, HTTP_OK;
	HTTP_OK = 200;

	if ( Ajax.isReady() )
	{
		status = Ajax.getRequestStatus();
		if (status === HTTP_OK) {
			return true;
		} else {
			// otherwise, server returned an HTTP error
			// this would generally be 500 Internal Server Error, 
			// when an aspx/ashx page throws an uncaught exception
			alert("An error occurred.");
			debug(status);
			debug(Ajax.getRequestStatusText());
			debug(Ajax.getResponseHeaders());
			debug(Ajax.getResponse());
		}
	}
	return false;
};


Services.GetNews = function( handler, types )
{
	Ajax.makeGetRequest("services/News.ashx?types=" + types, handler);
};

