function process_contact_form()
{

  // Begin processing the AJAX request
  try
  {
    // Moz supports XMLHttpRequest. IE uses ActiveX.
    // browser detction is bad. object detection works for any browser
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new
    ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e)
  {
    // browser doesn't support ajax. handle however you want
  }

  // Get the various input fields
  var vc_FromName = document.getElementById('vc_SenderName').value;
  var vc_FromEmail = document.getElementById('vc_SenderEmail').value;
  var vc_Subject = document.getElementById('vc_Subject').value;
  var txt_Message = document.getElementById('txt_Message').value;

  // Format any ampersands with URL friendly characters
  vc_FromName = vc_FromName.replace( /&/g, '%26' );
  vc_FromEmail = vc_FromEmail.replace( /&/g, '%26' );
  vc_Subject = vc_Subject.replace( /&/g, '%26' );
  txt_Message = txt_Message.replace( /&/g, '%26' );

  // Create the parameter list
  var params = "vc_SenderName=" + vc_FromName + "&vc_SenderEmail=" + vc_FromEmail;
  var params = params + "&vc_Subject=" + vc_Subject + "&txt_Message=" + txt_Message;

  // open takes in the HTTP method and url.
  xmlhttp.open( "POST", 'modules/Contact/ajax_send_mail.php', true );
  xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
  xmlhttp.setRequestHeader( "Content-length", params.length );
  xmlhttp.setRequestHeader( "Connection", "close" );

  // the xmlhttp object triggers an event everytime the status changes
  // triggered() function handles the events
  xmlhttp.onreadystatechange = function()
  {
    // if the readyState code is 4 (Completed)
    // and http status is 200 (OK) we go ahead and get the responseText
    // other readyState codes:
    // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
    {
      // xmlhttp.responseText object contains the response.
      // Said variable can be outputted for an AJAX passthrough
      document.getElementById("contact_cell").innerHTML = xmlhttp.responseText;
    }
  }

  // send the request. if this is a POST request we would have
  // sent post variables: send("name=aleem&gender=male)
  // Moz is fine with just send(); but
  // IE expects a value here, hence we do send(null);
  xmlhttp.send(params);

}

function contact_send()
{

  // Initialize the form checker variable
  var ValidForm = true;

  // Start by clearing all of the required text
  document.getElementById('req_SenderName').innerHTML = '&nbsp;'
  document.getElementById('req_SenderEmail').innerHTML = '&nbsp;';
  document.getElementById('req_Subject').innerHTML = '&nbsp;';
  document.getElementById('req_Message').innerHTML = '&nbsp;';

  // Get the various input fields
  var vc_FromName = document.getElementById('vc_SenderName').value;
  var vc_FromEmail = document.getElementById('vc_SenderEmail').value;
  var vc_Subject = document.getElementById('vc_Subject').value;
  var txt_Message = document.getElementById('txt_Message').value;

  // Check the integrity of the from name
  if( vc_FromName.length < 2 )
  {
    document.getElementById('req_SenderName').innerHTML = 'required';
    ValidForm = false;
  }

  // Check the integrity of the email address
  if( vc_FromEmail.length == 0 )
  {
    document.getElementById('req_SenderEmail').innerHTML = 'required';
    ValidForm = false;
  }
  else if( vc_FromEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 )
  {
    document.getElementById('req_SenderEmail').innerHTML = 'invalid address';
    ValidForm = false;
  }

  // Check the integrity of the subject line
  if( vc_Subject.length < 2 )
  {
    document.getElementById('req_Subject').innerHTML = 'required';
    ValidForm = false;
  }

  // Check the integrity of the message
  if( txt_Message.length == 0 )
  {
    document.getElementById('req_Message').innerHTML = 'required';
    ValidForm = false;
  }
  else if( txt_Message.length < 10 )
  {
    document.getElementById('req_Message').innerHTML = 'message too short';
    ValidForm = false;
  }

  // If the form is still valid after the form checking, process
  // the form and send the email
  if( ValidForm )
    process_contact_form();

} 

function contact_cancel()
{
  // Start by clearing all of the required text
  document.getElementById('req_SenderName').innerHTML = '&nbsp;'
  document.getElementById('req_SenderEmail').innerHTML = '&nbsp;';
  document.getElementById('req_Subject').innerHTML = '&nbsp;';
  document.getElementById('req_Message').innerHTML = '&nbsp;';

  // Clear the various form fields
  document.getElementById('vc_SenderName').value = '';
  document.getElementById('vc_SenderEmail').value = '';
  document.getElementById('vc_Subject').value = '';
  document.getElementById('txt_Message').value = '';
}
