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 == "")  
  		{
  		  alert("Please enter some feedback 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 on our site content.  We are always working to improve our site.  If you would like to contact the governor’s office about anything else, please visit our contact us page.");
	};
      var container = document.getElementById("feedbackform");
      container.innerHTML = "<p>Thank you for your feedback on our site content.  We are always working to improve our site.</p><p>If you would like to contact the governor's office about anything else, please visit our <a href='/?pageID=gov3utilities&sid=Agov3&U=Agov3_contact_us'>contact us</a> page.</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;
}