window.onload = prepareForm;

function prepareForm() {
  if(!document.getElementById) {
    return;
  };
  if(!document.getElementById("feedbackform")) {
    return;
  };
  if(document.getElementById("notification")) {
	  var container = document.getElementById("notification");
	  container.innerHTML = "<p><label for='notify'>Your feedback will be submitted directly from this page when you click the Send button below.  If you would like to receive an alert notification when your submission is complete, please check the following box.</label><input name='notify' id='notify' type='checkbox' value='y'/></p>";
  };
  document.getElementById("feedbackform").onsubmit = function() {
  	if(document.getElementById("required-feedbacktext").value == "" && document.getElementById("required-email").value == "")  
  		{
  		  alert("Please enter some feedback and your email address prior to clicking the SEND button.");
  		  return false;
  		}
  	if(document.getElementById("required-feedbacktext").value == "")  
  		{
  		  alert("Please enter some feedback prior to clicking the SEND button.");
  		  return false;
  		}
  	if(document.getElementById("required-email").value == "")  
  		{
  		  alert("Please enter your email address prior to clicking the SEND button.");
  		  return false;
  		}
		
  	else  
  		{
  
		  var data = "";
		    for (var i=0; i<this.elements.length; i++) {
		      data+= this.elements[i].name;
		      data+= "=";
		      data+= escape(this.elements[i].value);
		      data+= "&";
		      }
		    return !sendData(data);
		};
  	};
}

function sendData(data) {
  var request = getHTTPObject();
  if (request) {
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open( "POST", "/cgi-bin/massgovmail.cgi", true );
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
    return true;
  } else {
    return false;
  }
}

function parseResponse(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      if(document.getElementById("notify").checked == true)  
       {
	alert("Your feedback has been submitted.  Thank you for your feedback. The Executive Office of Housing &amp; Economic Development web team reviews all suggestions to help improve our website.  We value your input!");
	};
      var container = document.getElementById("feedbackform");
      container.innerHTML = "<p>Thank you for your feedback. The Executive Office of Housing &amp; Economic Development web team reviews all suggestions to help improve our website.  We value your input!</p>";
      prepareForm();
    }
  }
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}