/**==========================================================================
 ** FINDHOMESVALIDATE.JS
 **==========================================================================**/

/**==========================================================================
 ** SETUP
 **==========================================================================**/

function setupFindHomesValidate() {

  if(!document.getElementById) { return false; }
  if(!document.createElement) { return false; }
  if(!document.createTextNode) { return false; }

  document.getElementById('submitform').onclick = function () { return validateForm(); }
}

/**==========================================================================
 ** validateForm()
 **==========================================================================**/

function validateForm() {

  /* Check contact details */

  if(! (document.getElementById('contactdetails').value)) {
    alert('Please enter an e-mail address or phone number so I can contact you with your search results.');
    return false;
  }

  if(document.getElementById('contacttype').value == 'email') {
    var email = document.getElementById('contactdetails').value;
    if(email.indexOf('@') == -1) {
      alert('Please enter a valid e-mail address.  (E.g.: yourname@provider.com).');
      return false;
    }

    if(email.indexOf('.') == -1) {
      alert('Please enter a valid e-mail address.  (E.g.: yourname@provider.com).');
      return false;
    }
  }

  /* Check name and area */

  if(! (document.getElementById('name').value)) {
    alert('Please enter your name.');
    return false;
  }

  if(! (document.getElementById('area').value)) {
    alert('Please describe the area you are interested in.');
    return false;
  }

  /* Check that a home type is selected */

  var allInputs = document.getElementsByTagName('INPUT');
  var checkedCount = 0;
  for(var i=0; i<allInputs.length; i++) {
    if(allInputs[i].checked == true) { checkedCount++; }
  }
  if(checkedCount==0) {
    alert('Please select one or more types of home that you are interested in.');
    return false;
  }

  return true;
}

