// add_employee.js

/*  This page does all the magic for applying
 *  Ajax to an "add an employee" form.
 *  The form data is sent to a PHP 
 *  script using the POST method.
 *  The PHP script sends back a response in XML format.
 */
 
window.onload = init;
// Function that adds the Ajax layer:
function init() {

  // Get an XMLHttpRequest object:
  var ajax = getXMLHttpRequestObject();
  
  // Attach the function call to the form submission, if supported:
  if (ajax) {

    // Check for DOM support:
    if (document.getElementById('newsletter_form')) {
  
      // Add an onsubmit event handler to the form:
    
 document.getElementById('newsletter_form').onsubmit = function() {
        // Call the PHP script.
        // Use the POST method.
        
        // Open the connection:
        ajax.open('post', 'add_newsletter_xml.php');
        
        // Function that handles the response:
        ajax.onreadystatechange = function() {
          // Pass it this request object:
          handleResponse(ajax);
		}

        // Assemble all the form data:
        var fields = ['email_newsletter'];
        for (var i = 0; i < fields.length; i++) {
          fields[i] = fields[i] + '=' +
encodeURIComponent(document.getElementById(fields[i]).value);
        }
        var values = fields.join('&');

        // Set the request headers:
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        // Send the request along with the data:
        ajax.send(values);
      
        return false; // So form isn't submitted.

      } // End of anonymous function.
	  
	 
		  
		  //INTEREST FORM
      document.getElementById('interest_form').onsubmit = function() {
		  // Open the connection:
        ajax.open('post', 'interest_form_xml.php');
        
        // Function that handles the response:
        ajax.onreadystatechange = function() {
          // Pass it this request object:
          handleResponse2(ajax);
        }

        // Assemble all the form data:
        var fields = ['bachata','tango', 'samba', 'zouk', 'other','text_other'];
        for (var i = 0; i < fields.length; i++) {
			if(document.getElementById(fields[i]).type=="checkbox")
			{
			if(!document.getElementById(fields[i]).checked)	
			{
				fields[i] = fields[i] + '=' ;
								}
			else
			{
				fields[i] = fields[i] + '=' + encodeURIComponent(document.getElementById(fields[i]).value);

				
				}
			}
			else
			{
          		fields[i] = fields[i] + '=' + encodeURIComponent(document.getElementById(fields[i]).value);
        	}
		}
        var values = fields.join('&');

        // Set the request headers:
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        // Send the request along with the data:
        ajax.send(values);
      
        return false; // 
		  
		  }// End contact_us_form
    } // End of DOM check.
    
  } // End of ajax IF.

} // End of init() function.

// Function that handles the response from the PHP script:
function handleResponse(ajax) {

  // Check that the transaction is complete:
  if (ajax.readyState == 4) {
  
    // Check for a valid HTTP status code:
    if ((ajax.status == 200) || (ajax.status == 304) ) {
      
            
      // Reset all the labels:
      document.getElementById('email_newsletter').className = 'newletter_input';
      

      // Get the XML data:
      var data = ajax.responseXML;
      
      // Get the main response:
      var message = data.getElementsByTagName('result');
      
      // Get all the errors:
      var errors = data.getElementsByTagName('error');

      // Temp variable to use in the loop:
      var temp = false;
      
      // Loop through each error:
      for (var i = 0; i < errors.length; i++) {
      
        // Get the error value:
       
        
        // Change the class:
        document.getElementById('email_newsletter').className = 'error_input';
		document.getElementById('email_newsletter').focus();
        
      } // End of FOR loop.
  
      // Put the received response in the DOM:
	  alert(message[0].firstChild.nodeValue);
     

    } else { // Bad status code, submit the form.
      document.getElementById('emp_form').submit();
    }
    
  } // End of readyState IF.
  
} // End of handleResponse() function.

//RESPONSE TO INTEREST FORM REQUEST
function handleResponse2(ajax) {

  // Check that the transaction is complete:
  if (ajax.readyState == 4) {
  
    // Check for a valid HTTP status code:
    if ((ajax.status == 200) || (ajax.status == 304) ) {
      
      var results = document.getElementById('results1');
      
      // Reset all the labels:
      document.getElementById('checkbox_selection_label').className = 'interest_label';
      document.getElementById('textarea_label').className = 'interest_label';
            

      // Get the XML data:
      var data = ajax.responseXML;
      
      // Get the main response:
      var message = data.getElementsByTagName('result');
      
      // Get all the errors:
      var errors = data.getElementsByTagName('error');

      // Temp variable to use in the loop:
      var temp = false;
      
      // Loop through each error:
      for (var i = 0; i < errors.length; i++) {
      
        // Get the error value:
        temp = errors[i].firstChild.nodeValue;
        
        // Change the class:
        document.getElementById(temp + '_label').className = 'interest_error';
        
      } // End of FOR loop.
	  
	  
      // Put the received response in the DOM:
      results.innerHTML = message[0].firstChild.nodeValue;
      
      // Make the results box visible:
      results.style.display = 'block';
		
    } else { // Bad status code, submit the form.
      document.getElementById('interest_form').submit();
    }
    
  } // End of readyState IF.
  
} // End of handleResponse
