// Page object variables
var frmFeedback;
var divSubmitResult;
var completeFormInstruction;

// init() function
window.onload=function() {
	// Initialise object variables
	frmFeedback=document.getElementById('frmFeedback');
	divSubmitResult=document.getElementById('divSubmitResult');
	completeFormInstruction=document.getElementById('completeFormInstruction');
	
	// Assign event handlers	
	if(navigator.appVersion.indexOf('MSIE 6')==-1 && navigator.appVersion.indexOf('MSIE 5')==-1) {
		// IE 6 messes up positioning of some page elements after AJAX submit, so don't bother with it
		frmFeedback.onsubmit=frmFeedback_onsubmit;
	}

};

// Event handlers
function frmFeedback_onsubmit() {
	// Hide the form
	completeFormInstruction.style.display='none';
	divSubmitResult.innerHTML='Sending...';
	frmFeedback.style.display='none';
	divSubmitResult.style.display='block';
	
	// Prepair form for AJAX submission
	var options={
		method: "post",
		parameters: getRequestBody(frmFeedback).replace('method=form', 'method=ajax'),
		onSuccess: function(xhr, json) {
			if(xhr.responseText=='error') {
				// Server error of some kind. Maybe DB? Submitting the form normally probably won't help
				divSubmitResult.innerHTML='<p class=\"error\">Sorry, there was a problem storing your request.</p>\n';
			}
			else {
				divSubmitResult.innerHTML='<p>Thank you. Your information has been received and we will call you back.</p>\n';
			}
		},
		onFailure: function(xhr, json) {
			// Ajax failed. Submit the old fasioned way
			frmFeedback.onsubmit=null;
			frmFeedback.submit();
		}
	};
	
	// Submit form
	var request=new Ajax.Request(frmFeedback.action, options);
	
	return(false);
}
