
// 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('#country').val() == '') { addError('country');$j('#country').focus(); bError = true; } else { removeErrorClass('country'); }
  if ($j('#zip_code').val() == '') { addError('zip_code');$j('#zip_code').focus(); bError = true; } else { removeErrorClass('zip_code'); }
  if ($j('#email_address').val() == ''  || !email_regex.test($j('#email_address').val())) { addError('email_address');$j('#email_address').focus(); bError = true; } else { removeErrorClass('email_address'); }
  if ($j('#name').val() == '') { addError('name'); $j('#name').focus();bError = true; } else { removeErrorClass('name'); }

  if (bError == true)
  {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else
  {

		//organize the data properly
		var data = 'a=enews' + '&name=' + $j('#name').val() + '&email_address=' + $j('#email_address').val() + '&zip_code=' + $j('#zip_code').val() + '&country=' + $j('#country').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('#name').val('');
					$j('#zip_code').val('');
					$j('#email_address').val('');
					$j('#divEnewsletter_Form').hide('fast');
					$j('#divEnewsConfirmation').show('fast');
				}
				else
				{
					alert('Sorry, unexpected error. Please try again.');
					return false;
				}
			}
		});    
  }
}
