// Copyright © 2009 Invoke Media

$(document).ready(function() {
	$("#formContainer .submit").click(function(action) {
	submitForm('formContainer','contactForm');
								
// username - 4-10 chars, uc, lc, and underscore only.
function checkName(strng) {

   var illegalChars = /[^A-Za-z0-9 _-]/; // allow letters, numbers, and underscores
   
   if (strng != "") {
   var error = "";
	   if ((strng.length < 2) || (strng.length > 20)) {
	
		  error = "The name is the wrong length.<br />";
	   } else if (illegalChars.test(strng)) {
	
		  error = "The name contains illegal characters.<br />";
	   } 
   return error;
   }
}    

// email
function checkEmail(strng) {

   if (strng != "") {
   var error="";
	   var emailFilter=/^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,6}$/;
	   if (!(emailFilter.test(strng))) {
	
		  error = "Please enter a valid email address.<br />";
	   } else {
	
		  //test email for illegal characters
		  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			 if (strng.match(illegalChars)) {
	
				error = "The email address contains illegal characters.<br />";
			 }
	   }
	   return error;    
   }
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone(strng) {

   if (strng != "") {
   var error = "";

	   var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	   if (isNaN(parseInt(stripped))) {
	
		  error = "The phone number contains illegal characters.<br />";
	
	   }
	   if (!(stripped.length >= 10)) {
	
		  error = "The phone number is the wrong length.<br />Make sure you included an area code.<br />";
	   } 
	   return error;
   }
}



// non-empty textbox

function isEmpty(strng) {

   var error = "";
   if (strng.length == 0) {
	  error = "The mandatory text area has not been filled in.<br />"
   }
   return error;	  
}

function submitForm(e, form) {
	var theForm = $(form);
	var container = $(e);
	
	var why = "";
	if($("#name").val()) {why += checkName($("#name").val())};
	if($("#email").val()) {why += checkEmail($("#email").val())};
	if($("#emailNewsletter").val()) {why += checkEmail($("#emailNewsletter").val())};
	if($("#phone").val()) {why += checkPhone($("#phone").val())};
	
	if($("#email").val() == '' && $("#emailNewsletter").val() == '') {
		why += 'at least one email address must be filled';
	}

   if(why != "") {
	  $("#formContainer #errorMsgBox").html(why);
	  return false;
   } else {
	  $("#formContainer #errorMsgBox").html('Sending Form Information.');
	  /*$("#contactForm").send({
		 update: $("errorMsgBox"),
		 onRequest: function() {
			container.addClass('ajax-loading');
		 },
		 onComplete: function() {
			container.removeClass('ajax-loading');
		 }
	  });*/
	  
	var inputs = [];
	var contactForm = $("#formContainer #contactForm");
	
	$(':input', contactForm).each(function() {
		inputs.push(this.name + '=' + escape(encodeURIComponent(this.value)));
	})
var datums = contactForm.find(':input').serialize();
	jQuery.ajax({
		data: datums, //inputs.join('&'),
		url: contactForm.attr('action'),
		timeout: 10000,
		type: 'POST',
		error: function() {
			console.log("Something Failed...");
			},
		success: function(r) {
			//do something with the response 'r' text
			$("#formContainer #contactForm").html("")
	  		$("#formContainer #errorMsgBox").html(r);
			}
		})

   }
}


/*$('#contactForm').submit(function() {
	return false;
})*/
	action.preventDefault();

	});
});
