/* call init function on page load */
addLoadListener(init);

function init() {
  var ajax = getXMLHttpRequestObject();
  
  if (ajax) {
  	//checks emailreponse div tags exist on the page
    if (document.getElementById('emailresponse')) {  
      /* bind an onsubmit event to the addressForm button 
      pass the validemail value through to the email_results_ajax.cfm page using GET
      */
      document.getElementById('addressForm').onsubmit = function() {
        var validemail = document.getElementById('validemail').value;
        ajax.open('get', 'email_results_ajax.cfm?validemail=' + encodeURIComponent(validemail));

        ajax.onreadystatechange = function() {
          handleResponse(ajax);
        }

        ajax.send(null); 
        return false; 
      }     
    }    
  } 
} 
//handles the response from the email_results_ajax.cfm page
function handleResponse(ajax) {
  if (ajax.readyState == 4) {
  
 	//if all is ok output all response text from email_results_ajax.cfm to the emailreponse div on the page
    if ((ajax.status == 200) || (ajax.status == 304) ) {
	
      var emailresponse = document.getElementById('emailresponse');
      emailresponse.innerHTML = ajax.responseText;     
      var emailsignupcontent = document.getElementById('emailsignupcontent');
      emailresponse.style.display = 'block';
      emailsignupcontent.style.display = 'none';
      //equalise columns again after content size change
           
    } else {
      // else submit the form as normal
      document.getElementById('addressForm').submit();
    }   
  }   
} 
