/**
 * SMS Register AJAX Library
 * This library contains functions for calling the SMS Register
 * Web Service provided by the sms-register.php program.
 * 
 * This library requires jQuery.
 * 
 * @author Cameron McKay
 */

var SmsRegister = 
{
	// The possible responses from the web service.
	INTERNAL_ERROR: "INTERNAL_ERROR",
	NUMBER_NOT_SET: "NUMBER_NOT_SET",
	NUMBER_FORMAT_INVALID: "NUMBER_FORMAT_INVALID",
	NUMBER_OPT_IN_FAILED: "NUMBER_OPT_IN_FAILED",
	NUMBER_OPT_IN_SUCCESSFUL: "NUMBER_OPT_IN_FAILED",
	NUMBER_ALREADY_SUBSCRIBED: "NUMBER_ALREADY_SUBSCRIBED",
	
	/**
	 * Pretty versions of the responses.
	 */
	message:
	{
		INTERNAL_ERROR: "An internal error has occurred. Plesae notify the webmaster with this message",
		NUMBER_NOT_SET: "Number not received, please notify the webmaster with this message",
		NUMBER_FORMAT_INVALID: "Number format invalid, please notify the webmaster with this message",
		NUMBER_OPT_IN_FAILED: "Number is outside the service area or invalid",
		NUMBER_OPT_IN_SUCCESSFUL: "Number registered successfully",
		NUMBER_ALREADY_SUBSCRIBED: "That number is already subscribed"		
	},
	
	/**
	 * Returns a pretty version of the response given by the
	 * web service.
	 * 
	 * @param {String} response The response from the web service.
	 * @return The pretty version of the response.
	 * @type String
	 */
	getMessage: function(response)
	{
		if (this.message[response] === undefined)
		{
			return this.message[INTERNAL_ERROR];
		}

		return this.message[response];
	},
	
	/**
	 * Attempts to register the passed mobile number with the SMS
	 * Register Web Service.
	 * 
	 * @param {String} url The web service url.
	 * @param {String} mobileNumber The mobile number to register.
	 * @param {Function} callback The callback function.
	 * @return The response indicating success.
	 * @type String
	 */
	registerNumber: function(url, mobileNumber, callback)
	{
		jQuery.post(url, { "number": mobileNumber }, callback, "text");
	}
};

