function validate_form()
{
	var error = false;
	var str = "";
	$('#error_message').html('');

	if($('#firstname').val() == "")
	{
		str += "<br />Firstname is required";
		error = true;
	}
	if($('#lastname').val() == "")
	{
		str += "<br />Lastname is required";
		error = true;
	}
	if(! isValidEmailAddress($('#email').val()))
	{
		str += "<br />Email is required";
		error = true;
	}
	/*if($('#address').val() == "")
	{
		str += "<br />Address 1 is required";
		error = true;
	}
	if($('#address2').val() == "")
	{
		str += "<br />Address 2 is required";
		error = true;
	}
	if($('#town').val() == "")
	{
		str += "<br />Town is required";
		error = true;
	}*/
	if($('#postcode').val() == "")
	{
		str += "<br />Postcode is required";
		error = true;
	}
	var optin_status = $('#terms').attr('checked');
	if(!optin_status)
	{
		str += "<br />Agree to the Terms and Conditions";
		error = true;
	}

	if(error)
	{
		$('#error_message').html('There was a problem with this form:'+str);
		return false;
	}
	else
	{
		return true;
	}
}

function isValidEmailAddress(emailAddress)
{
   /* Check for empty address or invalid characters */
   if (emailAddress == "" || hasInvalidChar(emailAddress))
   {
      return false;
   }

   /* check for presence of the @ character */
   var atPos = emailAddress.indexOf("@", 1)
   if (atPos == -1)
   {
      return false;
   }
   
   /* Check that there are no more @ characters */
   if (emailAddress.indexOf("@", atPos + 1) > -1)
   {
      return false;
   }

   /* Check for the presence of a dot somewhere after @ */
   var dotPos = emailAddress.indexOf(".", atPos + 1);
   if (dotPos == -1)
   {
      return false;
   }

   /* Check for presence of two or more characters after last dot */
   var lastDotPos = emailAddress.lastIndexOf(".");
   if (lastDotPos + 3 >  emailAddress.length)
   {
      return false;
   }
   return true;
}

function hasInvalidChar(emailAddress)
{
   var invalidChars = "/;:,"; // this list is not complete

   for (var k = 0; k < invalidChars.length; k++)
   {
      var ch = invalidChars.charAt(k);
      if (emailAddress.indexOf(ch) > -1)
      {
         return true;
      }
   }
   return false;
}