// JSONRequest.js
//
// A helper object for creating JSON requests against thePlatform Portal Server
//
// Copyright (c) 2006 thePlatform for Media, Inc.

// create a JSONRequest object
function JSONRequest(URL, Callback, Context)
{
	// get the head tag
	var head = document.getElementsByTagName("head");
	if (!head)
	{
		alert("You need to define a <head> element to use the JSONRequest object.");
		return false;
	}
	this.headTag = head.item(0);

	// check the URL
	if (URL == null || URL.length == 0)
	{
		alert("You need to provide the URL.");
		return false;
	}
	this.url = URL;

	// add callback and/or context
	if (Callback != null && Callback.length > 0)
	{
		this.url += "&callback=" + escape(Callback);
	}
	if (Context != null && Context.length > 0)
	{
		this.url += "&context=" + escape(Context);
	}

	// determine the ID for this script
	this.scriptID = "JSONRequest" + JSONRequest.index++;
}

// a counter for ID references
JSONRequest.index = 1;

// send the request
JSONRequest.prototype.send = function ()
{
	// Create a script element
	var scriptObj = document.createElement("script");

	// Set the attributes
	scriptObj.setAttribute("type", "text/javascript");
	scriptObj.setAttribute("src", this.url);
	scriptObj.setAttribute("id", this.scriptID);

	// Add it to the <head>; this will immediately run it
	this.headTag.appendChild(scriptObj);
}

