// JavaScript Document
// Requires prototype framework v.1.6+

/* -------------------------------------------------------------------
|
|	You may want to add the following rule to your CSS. This will be
|	the style that applies only to your error messages.
|
|	.ajaxForm_error { color: red; }
|
---------------------------------------------------------------------- */

// Modify variables to suit your needs.
// Redirection to a success page.
var redirect = false;
// Page to redirect to on Success, relative of site root
var redirectTo = 'thankyou.html';
// Id of the div or any html element that either the success or error message should appear in.
var messageDiv = 'notify'; 
// ID of the form being submitted
var formId = 'contact-form'; 
// Path to the form to email script
var scriptPath = 'scripts/formtoemailpro.php'; 

// No need to edit below this line
// ------------------------------------------------------------------------------------------------

function processForm(e) {
		Event.stop(e);
		
		var errs = $(formId).select('.ajaxForm_error');		
		errs.each(function(s) {
						   s.innerHTML = '';
						   });
		var formValues = $(formId).serialize(true);
		new Ajax.Request(scriptPath, {
						 method: 'post',
						 parameters: formValues,
						 onSuccess: function(transport) {
							var result = transport.responseText;
							if(result.include('Thank you')) {
								if(redirect) {
									var url = window.location.hostname;
									window.location = "http://"+url+"/staging/"+redirectTo;
								}
								$(messageDiv).innerHTML = result;
								formId.reset();
							} else {
								$(messageDiv).className += ' ajaxForm_error';
								$(messageDiv).innerHTML = result;	
							}
						 }
						 });
		
		function checkRequired() {
			var fail = false;
			var reqs = $(formId).select('.required');
			reqs.each(function(r) {
							   if(r.value=='' || r.value == false) {
									r.insert({after: '<span class="ajaxForm_error"> This field cannot be blank</span>'});
									fail = true;  
							   }
							   });
			if(fail) {
				$(messageDiv).className += ' ajaxForm_error';
				$(messageDiv).innerHTML = 'Error(s) encountered, please see above.';
				Event.stop(e);
				return false;
			} else {
				return true;	
			}
		}
}

document.observe("dom:loaded", function() {
	Event.observe(formId, 'submit', processForm);
									   });