function doHttpRequest( ) {  // This function does the AJAX request
 
  //  Set our destination PHP page "ajaxpost.php"
  http.open("POST", "http://www.veganeatingout.com/mail.php", true);
  http.onreadystatechange = getHttpRes;

  // Make our POST parameters string
namehold = document.getElementById("name").value
namehold = namehold.replace("&", "and");
namehold = encodeURI(namehold);
feedbackhold = document.getElementById("feedback").value
feedbackhold = feedbackhold.replace("&", "and");
feedbackhold = encodeURI(feedbackhold);
  var params = "name=" + namehold+
	"&email=" + encodeURI(document.getElementById("email").value)+
	"&feedback=" + feedbackhold;

  // Set our POST header correctly
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  // Send the parms data
  http.send(params);

}

function getHttpRes( ) {
	if (http.readyState == 4 && http.status == 200) { 
	res = http.responseText;  // These following lines get the response and update the page
	document.getElementById('sent').innerHTML = res;
	wassent = "true";
  }
}

function getXHTTP( ) {
  var xhttp;
   try {   // The following "try" blocks get the XMLHTTP object for various browsers
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
 		 // This block handles Mozilla/Firefox browsers
	    try {
	      xhttp = new XMLHttpRequest();
	    } catch (e3) {
	      xhttp = false;
	    }
      }
    }
  return xhttp; // Return the XMLHTTP object
}

var http = getXHTTP(); // This executes when the page first loads.


function validate_all(thisform)
{
valid = "false"
check1 = validate_form(thisform, "name");
check2 = validate_form(thisform, "email");
check3 = validate_form(thisform, "feedback");
	if (check1 == "true" && check2 == "true" && check3 == "true")
	{
	x = document.getElementById("feedbackform");
		if (wassent == "false")
		{ doHttpRequest(); }
		else if (wassent == "true")
		{
		document.getElementById('sent').innerHTML = "<span style='color: red'>Feedback already sent!</span>";
		}
	}
	else
	{ return; }	
}

function validate_form(thisform, name)
{
x = document.getElementById(name);
with (x)
{
var y = x.name + "error";
var z = document.getElementById(y);
	if (value==null||value=="")
	{
	valid = "false";
	z.innerHTML = "Missing " + x.name + "!";
	z.style.visibility = "visible";
	}
	else
	{
	valid = "true";
	z.style.visibility = "hidden";
		if (name=="email")
		{
		a = /^((\w(\.)*(\w)*)+)@(\w(\.)*(\w)*)+(((\.co){1}(\.\w{2}){1})|(\.bus|\.us|\.com|\.biz|\.net|\.org|\.edu|\.gov|\.tv|\.go|\.uk|\.ca)){1}$/i;
		b = a.test(value);
			if (b == false)
			{
			valid = "false";
			z.innerHTML = "Invalid email!";
			z.style.visibility = "visible";
			}
		}
		if (name=="feedback")
		{
		a = /<(\S+).*>(.*)<\/\1>/;
		b = a.test(value);
			if (b == true)
			{
			valid = "false";
			z.innerHTML = "HTML not allowed!";
			z.style.visibility = "visible";
			}
		a = /\[(\S+).*\](.*)\[\/\1\]/;
		b = a.test(value);
			if (b == true)
			{
			valid = "false";
			z.innerHTML = "Code not allowed!";
			z.style.visibility = "visible";
			}
		}
	}
return valid;
}
}

wassent = "false";