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;

function joinMailingList() {
   if (validateEmail(document.getElementById('mailingListForm').emailAddress) == false) {
      return;
   }
   http = createRequestObject();
   
   var emailAddress = document.getElementById('mailingListForm').emailAddress.value;
   document.getElementById('mailingListForm').joinButton.disabled=true; 
   document.getElementById('mailingListForm').joinButton.value='Sending...';

   http.open('GET', './scripts/joinMailingList.php?emailAddress=' + emailAddress);
   http.onreadystatechange = handleJoinResponse;
   http.send(null);
}

function handleJoinResponse() {
  
   if(http.readyState == 4){
      var response = http.responseText;

      document.getElementById('joinInfoParagraph').style.fontStyle = 'italic';
      document.getElementById('joinInfoParagraph').innerHTML = response;
      document.getElementById('mailingListForm').joinButton.value='Join';
      document.getElementById('mailingListForm').emailAddress.value='';
      document.getElementById('mailingListForm').joinButton.disabled=false; 
      
   }
}


function getDirections() {
   http = createRequestObject();

   var startingAddress = document.getElementById('directionsForm').myAddress1.value + ", " +
	                 document.getElementById('directionsForm').myAddress2.value + ", " +
	                 document.getElementById('directionsForm').myCity.value     + ", " +
	                 document.getElementById('directionsForm').myState.value    + ". " +
	                 document.getElementById('directionsForm').myZip.value;


   var endingAddress = "1724 Woodlawn Drive, Baltimore MD 21207";	                      
   document.getElementById('directionsForm').directionsButton.disabled=true; 
   document.getElementById('directionsForm').directionsButton.value='Getting Directions...';

   http.open('GET', './scripts/getDirections.php?saddr=' + startingAddress + '&daddr=' + endingAddress);
   http.onreadystatechange = handleDirectionsResponse;                             
   http.send(null);
}


function handleDirectionsResponse() {
   var newWindow;
   var urlOfNewWindow = "";




   if(http.readyState == 4){
      
      urlOfNewWindow = http.responseText;         

      newWindow=window.open(urlOfNewWindow,'myNewWindow','width=700,height=500,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');

      document.getElementById('directionsForm').directionsButton.value='Get Directions';      
      document.getElementById('directionsForm').directionsButton.disabled=false;       
   
      if (window.focus) {
         newWindow.focus();
      }

   }
}



function contactUs() {
   http = createRequestObject();

   var yourName    = document.getElementById('contactUsForm').yourName.value;   
   var yourEmail   = document.getElementById('contactUsForm').yourEmail.value;
   var yourPhone   = document.getElementById('contactUsForm').yourPhone.value;   
   var yourComment = document.getElementById('contactUsForm').yourComment.value; 
   
   document.getElementById('contactUsForm').contactButton.disabled=true; 
   document.getElementById('contactUsForm').contactButton.value='Sending...';

   http.open('GET', './scripts/contactUs.php?yourName=' + yourName + '&yourEmail=' + yourEmail + '&yourPhone=' + yourPhone + '&yourComment=' + yourComment);
   http.onreadystatechange = handleContactUsResponse;
   http.send(null);
}

function handleContactUsResponse() {
  
   if(http.readyState == 4){
      var response = http.responseText;

      document.getElementById('contactResultParagraph').style.fontStyle = 'italic';
      document.getElementById('contactResultParagraph').innerHTML = response;
      document.getElementById('contactUsForm').contactButton.value='Contact us';
      document.getElementById('contactUsForm').yourName.value='';
      document.getElementById('contactUsForm').yourEmail.value='';
      document.getElementById('contactUsForm').yourPhone.value='';
      document.getElementById('contactUsForm').yourComment.value='';      
      document.getElementById('contactUsForm').contactButton.disabled=false; 
      
   }
}
