function cargaContenido(selectACargar) {
  var selectAnterior = selectACargar - 1; // Obtengo el número del combo que activó el evento onChange
  var selectSeguent = selectACargar + 1;
  var valor = document.getElementById("select_"+selectAnterior).options[document.getElementById("select_"+selectAnterior).selectedIndex].value;
  var elemento;

  if (valor != 0) {
    ajax = newAjax();
    ajax.open("GET", "/ajax/get_options.php?seleccionado=" + valor + "&select=" + selectACargar + "&default=Select", true);
    ajax.onreadystatechange = function() {
      if (ajax.readyState == 1) {
        // Mientras carga elimino la opcion "Elige" y pongo una que dice "Cargando"
        elemento = document.getElementById("select_" + selectACargar);
        elemento.length = 0;
        var opcionCargando = document.createElement("option");
        opcionCargando.value = 0;
        opcionCargando.innerHTML = "Loading...";
        elemento.appendChild(opcionCargando);
        elemento.disabled = true;
      }
      if (ajax.readyState == 4) {
        // Coloco en la fila contenedora los datos que recibo del servidor
        if (selectACargar == 3) {
          document.getElementById("select_" + selectACargar + "_container").innerHTML = '<select name="select_' + selectACargar + '" id="select_' + selectACargar + '" onchange="check_other()">' + ajax.responseText + '</select>';
        }
        else {
          document.getElementById("select_" + selectACargar + "_container").innerHTML = '<select name="select_' + selectACargar + '" id="select_' + selectACargar + '" onchange="cargaContenido(' + selectSeguent + ')">' + ajax.responseText + '</select>';
        }
        
      }
    }
    ajax.send(null);
  }

  var x = 1, y = null;
  while (x <= 2) {
    valor = document.getElementById("select_" + x).options[document.getElementById("select_" + x).selectedIndex].value;
    if (valor == 0) {
      while (x <= 2) {
        y = x + 1;
        elemento = document.getElementById("select_" + y);
        elemento.length = 0;
        var opcionSelecciona = document.createElement("option");
        opcionSelecciona.value = 0;
        opcionSelecciona.innerHTML = "Choose...";
        elemento.appendChild(opcionSelecciona);
        elemento.disabled = true;
        x++;
      }
    }
    x++;
  }
}

function check_other() {
  ajax = newAjax();
  ajax.open("GET", "/ajax/is_other.php?id=" + document.getElementById('select_3').value, true);
  ajax.onreadystatechange = function() {
    if (ajax.readyState == 4) {
      if (ajax.responseText == '1') {
        document.getElementById('other_business').disabled = false;
      }
      else {
        document.getElementById('other_business').disabled = true;
      }
    }
  }
  ajax.send(null);
}

function is_empty(q) {
  if (q == '' || q == 0) {
    return true;
  }
  return false;
}

function check_form(F) {
  if (is_empty(F.email.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your email address.</p>';
    F.email.focus();
    return false;
  }
  if (is_empty(F.type.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, select a type of account.</p>';
    F.type.focus();
    return false;
  }
  if (is_empty(F.name.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your first name.</p>';
    F.name.focus();
    return false;
  }
  if (is_empty(F.surname.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your last name.</p>';
    F.surname.focus();
    return false;
  }
  if (is_empty(F.company.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your company\'s name.</p>';
    F.company.focus();
    return false;
  }
  if (is_empty(F.address.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your address.</p>';
    F.adress.focus();
    return false;
  }
  if (is_empty(F.city.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your city.</p>';
    F.city.focus();
    return false;
  }
  if (is_empty(F.state.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your State/Province/Region.</p>';
    F.state.focus();
    return false;
  }
  if (is_empty(F.zip.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, insert your Zip/Postal code.</p>';
    F.zip.focus();
    return false;
  }
  if (is_empty(F.country.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, select your country.</p>';
    F.country.focus();
    return false;
  }
  if (is_empty(F.select_1.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, select your sector.</p>';
    F.select_1.focus();
    return false;
  }
  if (is_empty(F.select_2.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, select your subsector.</p>';
    F.select_2.focus();
    return false;
  }
  if (is_empty(F.select_3.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, select your specific business.</p>';
    F.select_3.focus();
    return false;
  }
  if (F.other_business.disabled == false && is_empty(F.other_business.value)) {
    document.getElementById('error').innerHTML = '<p class="error">Please, specify your business.</p>';
    F.other_business.focus();
    return false;
  }
  if ((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(F.email.value)) == false) {
    document.getElementById('error').innerHTML = '<p class="error">Your have specified an invalid email: ' + F.email.value + '</p>';
    F.email.focus();
    return false;
  }
  if (F.ihaveread.checked == false) {
    document.getElementById('error').innerHTML = '<p class="error">You have read and accept the Terms of Service and the Privacy Policy before going on</p>';
    F.ihaveread.focus();
    return false;
  }

  F.submit();
}
