   function checkEmail() {

    var email = document.getElementById('emailaddress');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
     alert('Please provide a valid email address');
     email.focus();
     return false;
    }else{
     return true;
    }
   }

   function checkInput(inputName, errorMessage)
   {
    var inputVar = document.getElementById(inputName);
    if (inputVar.value == '')
    {
     alert(errorMessage);
     inputVar.focus();
     return false;
    } else {
     return true;
    }
   }

   var http_request = false;
   function makePOSTRequest(url, parameters) {

      var contactform = document.getElementById("contactform");
      contactform.style.visibility="hidden";

      var postingform = document.getElementById("postingform");
      postingform.style.visibility="visible";

      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      //var postingform = document.getElementById("postingform");
      //postingform.style.visibility="hidden";

      if (http_request.readyState == 4) {
         if (http_request.status == 200) {

            if (http_request.responseText == "error")
            {
             alert("You have made a mistake entering spam protection code");

             var contactform = document.getElementById("contactform");
             contactform.style.visibility="visible";

             var postingform = document.getElementById("postingform");
             postingform.style.visibility="hidden";

             var postedform = document.getElementById("postedform");
             postedform.style.visibility="hidden";

            } else {

             //result = http_request.responseText;
             //document.getElementById('myspan').innerHTML = result;            

             var postedform = document.getElementById("postedform");
             postedform.style.visibility="visible";

             location.href = "thankyou";

            }

         } else {
            alert('There was a problem with the request.');
            var contactform = document.getElementById("contactform");
            contactform.style.visibility="visible";

            var postingform = document.getElementById("postingform");
            postingform.style.visibility="hidden";

            var postedform = document.getElementById("postedform");
            postedform.style.visibility="hidden";

         }
      }
   }
   
   function get(obj) {

      var poststr = "name=" + encodeURI( document.getElementById("yourname").value ) + 
                    "&email=" + encodeURI( document.getElementById("emailaddress").value ) +
                    "&subject=" + encodeURI( document.getElementById("subject").value ) + 
                    "&comments=" + encodeURI( document.getElementById("comments").value ) +
                    "&keystring=" + encodeURI( document.getElementById("code").value );

      if (checkInput('yourname', 'Please let us know your name'))
      {
       if (checkEmail())
       {
        if (checkInput('subject', 'Please privide email subject'))
        {
         if (checkInput('comments', 'You forgot to include your message'))
         {
          if (checkInput('code', 'Please type in spam protection code'))
          {
           makePOSTRequest('contact.php', poststr);
          }
         }
        }
       }
      }
   }

