
// This is the javascript validation for the Contact form (contact.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostEnewsForm()
{
  var bError = false;
  // regex for email validation
  var email_regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  if ($j('#enews_zip').val() == '') { addError('enews_zip');$j('#enews_zip').focus(); bError = true; } else { removeErrorClass('enews_zip'); }
  if ($j('#enews_email').val() == ''  || !email_regex.test($j('#enews_email').val())) { addError('enews_email');$j('#enews_email').focus(); bError = true; } else { removeErrorClass('enews_email'); }
  if ($j('#enews_first_name').val() == '') { addError('enews_first_name'); $j('#enews_first_name').focus();bError = true; } else { removeErrorClass('enews_first_name'); }
  if ($j('#enews_last_name').val() == '') { addError('enews_last_name'); $j('#enews_last_name').focus();bError = true; } else { removeErrorClass('enews_last_name'); }
  
  if (bError == true)
  {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else
  {

		//organize the data properly
		var data = 'a=enews' + '&enews_first_name=' + $j('#enews_first_name').val() + '&enews_last_name=' + $j('#enews_last_name').val() + '&enews_email=' + $j('#enews_email').val() + '&enews_zip=' + $j('#enews_zip').val();
		//alert(data);
		//send data via ajax
		$j.ajax(
		{
			//this is the php file that processes the data and send mail
			url: "ajax_handler.php",	
			
			//GET method is used
			type: "GET",
			
			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html)
			{
				//if process.php returned 1/true (send mail success)
				if (html==1) 
				{
					$j('#enews_first_name').val('');
					$j('#enews_last_name').val('');
					$j('#enews_zip').val('');
					$j('#enews_email').val('');
					$j('#divEnewsletter_Form').hide('fast');
					$j('#divEnewsConfirmation').show('fast');
				}
				else
				{
					alert('Sorry, unexpected error. Please try again.');
					return false;
				}
			}
		});    
  }
}

