// JS code for Contact Form - http://www.coursesweb.net/php-mysql/
var cform = document.getElementById('cform');       // get the form
var csubmit = document.getElementById('csubmit');   // get the submit button

// Function that take and check form data
function Validate() {
  // takes data from each field
  var file_php = cform.action;
  file_php = file_php.split('/').pop();	  	// takes the name of the php file
  var name = cform.name.value;
  var email = cform.email.value;
  var phone = cform.phone.value;
  var message = cform.message.value;

  // Validate the fields data
  var regx_mail = /^([a-zA-Z0-9]+[a-zA-Z0-9._%-]*@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4})$/;    // RegExp for e-mail address
  if (name.length<2 || name.length>40) {
    alert('Name must contain between 2 and 40 characters');
    cform.name.select();
  }
  else if (email.search(regx_mail)==-1){
    alert('Add a valid e-mail address');
    cform.email.select();
  }
  else if (phone.length<3 || phone.length>70) {
    alert('Add a phone, between 3 and 70 characters');
    cform.phone.select();
  }
  else if (message.length<5 || message.length>500) {
    alert('Add a message, between 5 and 500 characters');
    cform.message.select();
  }
  else {
    // add 'Sending...' notification in Send button, and disable it
    csubmit.value = 'Sending...';
    csubmit.setAttribute("disabled", "disabled");

    // Define a string witth data to be sent to ajax function
    var  datele = 'name='+name+'&email='+email+'&phone='+phone+'&message='+message;
    set_codas();          // Access the function to update the anti-spamm code
    ajaxrequest(file_php, datele);    // calls ajaxrequest()
  }

  return false;
}

// function that returns the XMLHttpRequest object according to browser
function get_XmlHttp() {
  // the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
  var xmlHttp = null;

  if(window.XMLHttpRequest) {    // For Forefox, Opera, Safari, ...
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {  // For Internet Explorer
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  return xmlHttp;
}

// Ajax function, send data to a php file and display the server response
function ajaxrequest(php_file, datele) {
  var cerere_http =  get_XmlHttp();    // calls the function for the XMLHttpRequest object

  cerere_http.open("POST", php_file, true);      // Create the request

  // adds  a header to tell the PHP script to recognize the data as is sent via POST
  cerere_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  cerere_http.send(datele);    // calls the send() method, and send data in parameter

    // Check request status. If the response is received completely, will be returned
  cerere_http.onreadystatechange = function() {
    if (cerere_http.readyState == 4) {
      // If the php script response contains "Error:", display it in the title of the form
      // Otherwise, it displays instead form
      if(cerere_http.responseText.indexOf("Error:")!=-1) {
        // add 'Send' value in submit button button, and remove disabled
        csubmit.value = 'Send';
        csubmit.removeAttribute("disabled");
        document.getElementById('fc_titlu').innerHTML = cerere_http.responseText;
      }
      else {
        cform.innerHTML = cerere_http.responseText;
      }
    }
  }
  return false;
}

cform.onsubmit = function () {return Validate();};           // register "onsubmit" event at form

