// rsr.js - Submit Ajax Request with Script Response.
//
//	FST Application Framework, Version 3.4
//	Copyright (c) 2009-10, Norman Lippincott Jr, Saylorsburg PA USA
//	All Rights Reserved
//
//	The FST Application Framework, and its associated libraries, may
//	be used only with the expressed permission of the copyright holder.
//	Usage without permission is strictly prohibited.

// rsr:
//
// Submits a request via Ajax.Request, with the expected response being script
// code (within <script>..</script> tags), an error message (which appears as
// plain text), or both. Any error message returned is displayed via an alert
// box, and any script code returned is executed.
//
// Options may be provided, which are passed along to Ajax.Request. However,
// if onSuccess is supplied via an option, it is ignored.

function rsr (url, options)
{
	if (options === undefined) options = { };
	options.onSuccess = rsrcb;
	new Ajax.Request(url, options);
}

// rsrcb:
//
// Callback function for rsr, above. This is provided as a separate function
// so that it may be used as a callback function independently.

function rsrcb (res)
{
	err = res.responseText.stripScripts();
	if (!err.blank())
		alert(err.strip());
	res.responseText.evalScripts();
}

// rsrsub:
//
// Submits a form via Ajax.Request, with the expected response being script
// code (within <script>..</script> tags), an error message (which appears as
// plain text), or both. This function behaves identically to rsr, except that
// a form is submitted rather than a simple Ajax request being issued. Note
// that the form's action property must be set to the URL to which the Ajax
// request is to be sent.
//
// For convenience, this function returns false (which may be ignored). This
// is so that it may be used within the onsubmit attribute of a form, such as
// 'onsubmit="return rsrsub('myform')"', to defeat the browser's default submit
// behavior. This could be accomplished without using the return value, as in
// 'onsubmit="rsrsub('myform');return false"'. The convenience return value
// makes the code a bit shorter.
function rsrsub (form, options)
{
	if (options === undefined) options = { };

	options.onSuccess = rsrcb;

	// It seems that with the release of Microsoft's latest wonderful
	// technological revelation, IE8 (yes, I am being sarcastic - I really
	// mean Microsoft's latest piece of fucking crap), we need to explicitly
	// pass the form method. This means that, if using this function, you
	// now *must* use the POST method, and if you specify GET in the form,
	// it is ignored. If you want to use GET, you're fucked.
	// Thanks Microsoft - bunch of fucking assholes!
	options.method = 'post';

	$(form).request(options);

	return false;
}

