// JavaScript Document
/**
 * @var String Token form token
 */
var Token;


/**
 * @var Object refForm Reference to the current form being used
 */
var refForm;


/*
 * Main Ajax call.
 */
var submitNewsletterForm = function(elmt)
{
	refForm	= elmt.form;
	
	if (undefined != Token) {
		elmt.form.token.value	= Token;
	}
	
	new Ajax.Request(elmt.form.action, {
			method:		elmt.form.method,   
			parameters: Form.serialize(elmt.form),
			onComplete:	newsletterReturn,
			onFailure:	function(req) {alert("Sorry but there has been an error. Please try again.")}
		});
	
	disableForm(true);
}


/*
 * resultsAction for the main Ajax call.
 */
var newsletterReturn = function(req, json)
{
	try {
		updateToken(json.token);
		displayMessage(json.message);
	} catch (e) {
		message		= new Array();
		message[0]	= "Sorry, there was an error, please try again";
		displayMessage(message);
	}
	
	disableForm(false);
}

/*
 * loadingIcon for the Ajax call.
 */
var disableForm = function(state)
{
	if (refForm) {
		// Get all the images within the form
		images	= refForm.getElementsByTagName("img");
		
		// Look for the loading icon
		for (a = 0; a < images.length; a++) {
			if ("loading" == images[a].className) {
				loadingIcon	= images[a];
				break;
			}
		}
		
		// Ditch the rest of the image references
		images	= null;
		
		// If we have found a loading icon, lets display/hide it
		if (loadingIcon) {
			if (state) {
				loadingIcon.style.display	= "";
			} else {
				loadingIcon.style.display	= "none";
			}
		}
		
		// Now lets disable/enable the form inputs to prevent resubmission
		// Check if we also have an input image as a submit button, and hide/display it
		inputs	= refForm.getElementsByTagName("input");
		for (a = 0; a < inputs.length; a++) {
			if ("image" == inputs[a].type) {
				submitButton	= inputs[a];
				submitButton.style.display	= state ? "none" : "";
			} else {
				inputs[a].disabled	= state;
			}
		}
	}
}

/*
 * each Ajax call returns a new token. Here is where we update it.
 */
var updateToken = function(newToken)
{
    Token = newToken;
}


/*
 * Each payload comes back with a message array. If there's anything
 * in it, display it.
 */
var displayMessage = function(messages)
{
	var msg	= "";
	
	if (refForm) {
		messageDiv	= refForm.getElementsByClassName('message');
		messageDiv	= messageDiv[0];
		//messageDiv.innerHTML	= "";
	}

	if (messages.length > 0) {
		for (a = 0;a < messages.length; a++) {
			msg	+= messages[a] + (messageDiv ? "<br/>" : "\n");
		}
	}

	if (msg) {
		try {
			messageDiv.style.display	= "";
			messageDiv.innerHTML		= "<div class='messages'>" + msg + "</div>";
		} catch(e) {
			alert(msg);
		}
	} else {
			messageDiv.style.display	= "block";
	}
	
	return;
}

var resetCaptchaForm = function(showId)
{
	var show = showId;
	
	document.getElementById('fieldSet_' + show).style.display = 'block';
	document.getElementById('crossRed_' + show).style.display = 'none';
	document.getElementById('closeBtn_' + show).style.display = 'none';
	document.getElementById('thanksMsg_' + show).style.display = 'none';
	
	document.getElementById('submitAlert_' + show).style.display = 'none';
}
 
var startCaptcha = function(captchaId, showId, codeResultImg)
{
	var show = showId;
	
	if (codeResultImg != '1') {
		document.getElementById('crossRed_' + show).style.display = 'block';
		document.getElementById('crossRed_' + show).src = '/images/cross_red.png';
	}
	
	if (codeResultImg == '1') {
		document.getElementById('crossRed_' + show).style.display = 'block';
		document.getElementById('crossRed_' + show).src = '/images/submit_tick.png';
		document.getElementById('fieldSet_' + show).style.display = 'none';
		document.getElementById('closeBtn_' + show).style.display = 'block';
		document.getElementById('thanksMsg_' + show).style.display = 'block';
	}
	
	document.getElementById('captchaImg_' + show).src = '/images/captcha/' + captchaId + '.png';
	document.getElementById('captchaId_' + show).value = captchaId;
	document.getElementById('captchaInput_' + show).value = 'Enter Code';
}
 
var refreshCaptcha = function(captchaId, showId, submitSuccess, question, name, userSession)
{
	var show = showId;

	var controller = document.getElementById('captchaController_' + show).value;

	if (controller) {
		if (controller == 'index') {
			if (submitSuccess == true) {
				document.getElementById('submitAlert_' + show).style.display = 'block';
			}
			
			document.getElementById('captchaPostName_' + show).value = name;
			document.getElementById('captchaPostQuestion_' + show).value = question;
		}
	}
	
	document.getElementById('captchaImg_' + show).src = '/images/captcha/' + captchaId + '.png';
	document.getElementById('captchaId_' + show).value = captchaId;
		
	if (userSession == true) {
		document.getElementById('chatCaptcha').style.display = 'none';
		document.getElementById('captchaInput_' + show).value = 'true';
	} else {
		document.getElementById('captchaInput_' + show).value = 'Enter Code';
	}
}

/*
 * Add some helpfull String trimming addons
 */
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}
