function validateEmail(field, allowEmpty) {

   if(field == null || trim(field) == "") {
      if(allowEmpty == true) {
         return true;
      }
      else {
         return false;      
      }
   }
   
   
   with (field) {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
      if (apos<1||dotpos-apos<2) {
         
         return false;
      }
      else {
         return true;
      }
   }
} 


function validateTextField (field) {
/*   
   This first if statement is added because if an authenticated user posts comments, 
   then, the username, email, url fields are not present. Instead they are replaced 
   by hidden, system, obscurely named, input fields. Therefore the non-present fields do
   not need validation

*/   
   if(field == null) {
      return true;
   }
   if (trim(field) == "") {
      return false;
   }
   return true;
}


function trim(field) {
   with (field) {
      var str = value;
//      alert ("in here -" + value + "-");   
      while(''+str.charAt(0)==' ') {str=str.substring(1,str.length);}
      while(''+str.charAt(str.length-1)==' ') {str=str.substring(0,str.length-1);}
   }
   
//   alert ("in here2 -" + str + "-");      
   return str;

}


function validateCommentForm(formObject) {
/*
   var captcha = document.getElementById('captcha');
   
   
   if ((trim (captcha) != "faking captcha") )  {
   
     alert ('Please enter "faking captcha" in the Security field below');
     return false;
   }
*/   
   
   var validate = validateTextField(document.getElementById('name'));

   
   if (validate == false) {
      alert ("The name field is required");
      return false;
   }
   
   validate = validateEmail(document.getElementById('email'), true);
   if (validate == false) {
      alert("The value in the email field is invalid");
      return false;
   }   
        
   
   validate = validateTextField(document.getElementById('comment'));

   if (validate == false) {
      alert ("The comment field is required");
      return false;
   }  
   formObject.submit();
   return true;
}



function validateAndSendContactForm(formObject) {
 
   
   var validate = validateTextField(document.getElementById('name'));
   
   if (validate == false) {
      alert ("The name field is required");
      return false;
   }
   
   validate = validateEmail(document.getElementById('email'), false);
   if (validate == false) {
      alert("The value in the email field is invalid");
      return false;
   }   
        
   
   validate = validateTextField(document.getElementById('comment'));

   if (validate == false) {
      alert ("The comment field is required");
      return false;
   }  
   
   
   contactUs(formObject);
   return true;
}


function reloadTextDiv(previewDivName, dynamicTextObject) {

   //var newText = document.getElementById(dynamicTextObject).value;
   var newText = dynamicTextObject.value;
   newText = newText.replace(/\n/g, '<br />');
   var previewDivObject = document.getElementById(previewDivName);
   previewDivObject.innerHTML = newText;
}



/* Begin AJAX */

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}


var http;

var formObjectGlobal;

function contactUs(formObject) {
   http = createRequestObject();
   formObjectGlobal = formObject;

   var name    = formObjectGlobal.name.value;   
   var email   = formObjectGlobal.email.value;   
   var comment = formObjectGlobal.comment.value; 
   
   formObjectGlobal.contactButton.disabled=true; 
   formObjectGlobal.contactButton.value='Sending...';

   http.open('GET', 'http://www.orbitshakers.com/scripts/contactUs.php?yourName=' + name + '&yourEmail=' + email + '&yourComment=' + comment);
//   alert ("afterOpen");
   http.onreadystatechange = handleContactUsResponse;
//   alert ("aftersentstatechange");   
   http.send(null);
//   alert ("aftersend=null");   
}

function handleContactUsResponse() {
  
//   alert ("in handle contactus");   
   if(http.readyState == 4){
      var response = http.responseText;

      document.getElementById('contactResultText').style.fontStyle = 'italic';
      document.getElementById('contactResultText').innerHTML = response;
      formObjectGlobal.contactButton.value='Send Message';
      formObjectGlobal.name.value='';
      formObjectGlobal.email.value='';     
      formObjectGlobal.comment.value='';      
      formObjectGlobal.contactButton.disabled=false; 
      
   }
}

/* End AJAX */