
var Feedback = {};
	
Feedback.onload = function() {
	
    $("#transmitMessage").click(Feedback.validateAndSubmitFeedbackForm);    
    $("#getNewCaptchaImage").click(Feedback.getANewCaptchaImage);
    
    //wait cursor during AJAX requests
    Feedback.globalAjaxCursorChange();
};

Feedback.messagesResource = "/feedback/messages";
Feedback.jcatpchaResource = "/feedback/jcaptcha";
Feedback.successPage = "http://www.getcharting.com/ThanksForFeedbackPage.html";
// hasBeenSubmitted is used to prevent double submits
Feedback.hasBeenSubmitted = false;

Feedback.validateAndSubmitFeedbackForm = function() {
	// Check for double submit
	if (Feedback.hasBeenSubmitted) {
		return true;
	}	
    // var coverageNumber = $("#coverageNumber").text();  - bug just fixed in jquery: http://dev.jquery.com/ticket/4495
    var userEmailAddressElement = document.getElementById("userEmailAddress");
    var subjectElement = document.getElementById("subject");
    var commentElement = document.getElementById("comment");
    var jcaptchaElement = document.getElementById("jcaptcha");

    if (!userEmailAddressElement.value) {
		alert("Please enter your email address");
		userEmailAddressElement.focus();
	} else if (!subjectElement.value) {
		alert("Please enter a subject for your feedback");
		subjectElement.focus();
	} else if (!commentElement.value) {
		alert("Please enter your feeback comment");
		commentElement.focus();
	} else if (!jcaptchaElement.value) {
		alert("Please enter the text you see in the image");
		jcaptchaElement.focus();
	} else {
		var data = {};
		data.jcaptcha = jcaptchaElement.value;
		data.userEmailAddress = userEmailAddressElement.value;
		data.subject = subjectElement.value;
		data.comment = commentElement.value;
		var encodedData = Feedback.encodeForumData(data);
		Feedback.sendRequest(Feedback.messagesResource, encodedData);
	}	
    return true;
};

Feedback.encodeForumData = function(data) {
	var pairs = [];
	var regexp = /%20/g;
	
	for (var name in data) {
		var value = data[name];
		var pair = encodeURIComponent(name).replace(regexp, "+") + "=" + encodeURIComponent(value).replace(regexp, "+");
		pairs.push(pair);
	}
	return pairs.join("&");
};

Feedback.sendRequest = function(uri, encodedData) {
	// The synchronouse nature of the request means that ***before*** Feedback.sendRequest
	// returns, the request is sent to the server & if there is an error the error
	// handler Feedback.feedbackError will also be called & any processing in that
	// error handler will be completed, then we exit this function, ***SO**** you need
	// to set "Feedback.hasBeenSubmitted = true;" as the error handler sets it to false
	// so that on errors (eg bad format email address) the user can submit again
	// after correcting the email address.
	Feedback.hasBeenSubmitted = true;
    $.ajax({
        type: "POST",
        url: uri,
        data: encodedData,
        async: false,
        success: Feedback.feedbackSuccess,
        error: Feedback.feedbackError
    });
};

Feedback.feedbackError = function(xhr, status, ex) {
	// On error allow re-submission of the form
	Feedback.hasBeenSubmitted = false;
	var msg = "Submission failed.\n\n";
	if (xhr.responseText) {
		msg += xhr.responseText;
	} else {
		msg += "Please check all fields.";
	}
    alert(msg);
};

Feedback.feedbackSuccess = function(data, textStatus) {
	
	location.href = Feedback.successPage;
};


Feedback.getANewCaptchaImage = function() {
	
	var jcaptchaImageElement = document.getElementById("jcaptchaImg");
	jcaptchaImageElement.src = Feedback.jcatpchaResource + '?t=' + Math.round(Math.random() * 10000);
};


Feedback.globalAjaxCursorChange = function() 
{
	$("body").bind("ajaxStart", function(){
		this.style.cursor = "wait";
	});
		     
	$("body").bind("ajaxStop", function(){
		this.style.cursor = "default";
	});	
};


