/**
 * Permission key (and other data) retriever
 * @title		Permission Retriever
 * @author		Colin Nolan
 * @version		v2.0.0
 * @created		24/12/10
 * @updated		27/09/11 (v2.0.0)
 * @requires	ScriptLoader
 */
var PermissionRetriever = function(emailInput, inputBtn, errorOutput, statusOutput, nameOutput, createdOutput, keyOutput)
{
	/*
	 * Settings
	 */
	// UI messages
	var CONNECTION_ERROR_MSG = "<u>Request Server Error:</u><br/>Could not establish a connection to the permission checker at this time - please try again later.<br/>If this problem persists, please contact the site owner via the contact page.";
	var PERMISSION_ERROR_MSG = "<u>No Permission Data Found:</u><br/>No data was found, associated with the entered email address you submitted - please check you have entered the address correctly!<br/>Please note that to get a permission key, you must have donated to the site. For more information about this, please look at the section above labelled: 'Donate'<br/><br/>If you believe your permission key should have been found, please contact the site owner";
	var INVALID_EMAIL_MSG = "<u>An error occurred relating to the email address you submitted:</u><br/>The email address you entered was not valid - please enter a valid email address.";

	var RETRIEVAL_URL = "http://permissions.survey-remover.com/data/getEntry/";		// Location as to where the permissions can be retrieved from



	/*
	 * Instance variables
	 */
	var scriptLoader = new ScriptLoader();							// Loads request scripts



	// Mutators
	/**
	 * Retrieve and then show permission response
	 */
	this.retrieve = function()
	{
		// Hide any previously shown output elements
		errorOutput.style.display = "none";
		statusOutput.parentNode.parentNode.parentNode.style.display = "none";

		// Stop simultaneous requests
		disableInput();

		// Internally validate email address
		if(!isValidEmail(emailInput.value)) {
			// Email address is not valid - inform user
			showError(INVALID_EMAIL_MSG);

			// Allow input again
			enableInput();
		}
		else {
			// Setup the callback
			var callbackID = "cb_"+ Math.floor(Math.random()*9999);			// ID of callback method, fired when the data has loaded
			PermissionRetriever[callbackID] = onDataLoad;

			// Load permissions
			scriptLoader.loadJS(
				RETRIEVAL_URL +"?email="+ emailInput.value +"&callback=PermissionRetriever."+ callbackID,
				null,
				function() {
					showError(CONNECTION_ERROR_MSG);
					enableInput();
				}
			);
		}
	};



	// Private methods
	/*
	 * Disable the input form
	 */
	var disableInput = function()
	{
		emailInput.readonly = true;
		inputBtn.disabled = true;
	};


	/*
	 * Disable the input form
	 */
	var enableInput = function()
	{
		emailInput.readonly = false;
		inputBtn.disabled = false;
	};
	enableInput();							// Ensure inputs are enabled onload


	/*
	 * Validate email address
	 * @param	{string} address	Email address submitted by the user
	 * @return	{boolean}	Whether the email address is valid
	 */
	var isValidEmail = function(address)
	{
		var regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!regexp.test(address)) {
			return false;
		}
		else {
			return true;
		}
	};


	/*
	 * Show error
	 * @param	{string} err	Error message to show
	 */
	var showError = function(err)
	{
		errorOutput.style.display = "";
		errorOutput.innerHTML = err;
	};


	/*
	 * To be called when the permission data has loaded from the server
	 * @param	{JSON} data	Data loaded from the server
	 */
	var onDataLoad = function(data)
	{
		if(!data.found) {
			showError(PERMISSION_ERROR_MSG);
		}
		else {
			// Show data
			statusOutput.innerHTML = (function(str) {
				return str.charAt(0).toUpperCase() + str.slice(1);			// Capatilizes first letter
			})(data.type);
			nameOutput.innerHTML = data.name;
			createdOutput.innerHTML = data.created;
			keyOutput.value = data.key;
			statusOutput.parentNode.parentNode.parentNode.style.display = "";
		}

		// Re-enable input
		enableInput();
	};
};
