function getXMLRequest(url) {
  xmlhttp = null;
  if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
  }
  if (xmlhttp != null) {
    xmlhttp.open("GET", url, false)
    xmlhttp.send(null);
    return xmlhttp.responseText;
  }
  return null;
}

var response = null;

var gt = false;
var glogin = null;
var gnombre = null;
var gapellido = null;
var gano_nac = null;

function checkAvailability(t, divname) {
  glogin = encodeURIComponent(document.getElementById('loginreg').value);
  gnombre = encodeURIComponent(document.forms['new_account'].nombre.value);
  gapellido = encodeURIComponent(document.forms['new_account'].apellidos.value);
  gano_nac = document.forms['new_account'].ano_nac.value;
  gt = t;
  url = 'checkuser.php?login='+glogin+'&nombre='+gnombre+'&apellido='+gapellido+'&ano_nac='+gano_nac;

  xmlhttp = null;
  if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
  }
  if (xmlhttp != null) {
    xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState==4) {
        response = xmlhttp.responseText;
				var html = '';
        if (response == '') {
			if (gt) {
            	//html += '<p class="si">El username <strong>'+glogin+'</strong> está disponible.</p>';
            	html += '<p class="si">'+msgError178.replace(/LOGIN/i, glogin)+'</p>';
				setTimeout("MM_showHideLayers('"+divname+"','','hide')", 2000);
			}
        } else { 
        	if (gt) {
          		//html += '<p class="no">El username <strong>'+glogin+'</strong> no está disponible.</p>';
          		html += '<p class="no">'+msgError179.replace(/LOGIN/i, glogin)+'</p>';
        	}
        	html += '<p>'+msgError180+':</p>';
        	html += '<ul>'
        	options = response.split(' ');
        	for (i=0; i<options.length; i++)
          		html += '<li><a href="#" onclick="setUsername(this.innerHTML,\''+divname+'\'); return false;">'+options[i]+'</a></li>';
        	html += '</ul>';
        	html += '<p><a href="#" onclick="setUsername(\'\',\''+divname+'\'); return false;">'+msgError181+'</a></p>';
        }
        if (html != '') {
			MM_showHideLayers(divname,'','show');
			if (divname=='reguser') {
				b='result_availability';
				divname = divname+'1';
			} else {
				b='result_availability1';
				divname = 'reguser';
			}
        	document.getElementById(b).innerHTML = html;
        	MM_showHideLayers(divname,'','hide');
		}
      }
    }
    xmlhttp.send(null);
  }
}

function setUsername(str, divname) {
  document.getElementById('loginreg').value = str;
  MM_showHideLayers(divname,'','hide');
}

var errorRegisterForm = false;
var errorLoginForm = false;
var errorEditForm = false;
var controlRegisterForm = 0;
var controlLoginForm = 0;
var controlEditForm = 0;

function clearErrorsRegisterForm() {
  if (controlRegisterForm == 0)
    controlRegisterForm = 1;
  else {
    form = document.forms['new_account'];
    color = '#fff';
    form.mail.style.backgroundColor = color;
    form.nombre.style.backgroundColor = color;
    form.apellidos.style.backgroundColor = color;
    form.dia_nac.style.backgroundColor = color;
    form.mes_nac.style.backgroundColor = color;
    form.ano_nac.style.backgroundColor = color;
    form.sexo.style.backgroundColor = color;
    form.mail.style.backgroundColor = color;
    form.pais.style.backgroundColor = color;
	form.poblacion.style.backgroundColor = color;
	form.provincia.style.backgroundColor = color;
	form.codigo_postal.style.backgroundColor = color;
    form.login.style.backgroundColor = color;
    form.clave.style.backgroundColor = color;
    form.conf_clave.style.backgroundColor = color;
    form.codecheck.style.backgroundColor = color;
	document.getElementById('msgerrorbox').style.display = 'none';
    MM_showHideLayers('regpass','','hide');
    MM_showHideLayers('regemailalt','','hide');
    controlRegisterForm = 0;
  } 
}

function clearErrorsLoginForm() {
  if (controlLoginForm == 0)
    controlLoginForm = 1;
  else {
    color = '#fff';
    form = document.forms['login_account'];
    form.login.style.backgroundColor = color;
    form.clave.style.backgroundColor = color;
    controlLoginForm = 0;
  }
}

function clearErrorsEditForm() {
  if (controlEditForm == 0)
    controlEditForm = 1;
  else {
    form = document.forms['edit_account'];
    color = '#fff';
    form.mail.style.backgroundColor = color;
    form.nombre.style.backgroundColor = color;
    form.apellidos.style.backgroundColor = color;
    form.dia_nac.style.backgroundColor = color;
    form.mes_nac.style.backgroundColor = color;
    form.ano_nac.style.backgroundColor = color;
    form.sexo.style.backgroundColor = color;
    form.mail.style.backgroundColor = color;
    form.pais.style.backgroundColor = color;
    form.clave.style.backgroundColor = color;
    form.conf_clave.style.backgroundColor = color;
	form.codigo_postal.style.backgroundColor = color;
    controlEditForm = 0;
  } 
}

function setMsgErrorBox(msg) {
	var html = "<div class=\"tit\">"+msgError129+"</div>"+msgError130+""+msg;
	document.getElementById('msgerrorbox').innerHTML = html;
}

function validateRegisterForm(form) {
  color = '#ffc8c8';
  errorRegisterForm = false;
  msgError = "";
  error183 = false;
  errorNombre = false;
  errorMail = false;
  errorUsername = false;
  
  if ((!form.nombre.value) || (/^ +$/.test(form.nombre.value))) {
    form.nombre.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  // Pode conter caracteres alfanuméricos, hífen '-' , apóstrofe ''' , ponto '.' e espaço ' '  
  if (!/^[0-9a-zA-Z\.\-\'\ ãáâéêíõóôúüçÃÁÂÉÊÍÕÓÔÚÜÇ]*$/.test(form.nombre.value)) {
    form.nombre.style.backgroundColor = color;
    errorRegisterForm = true;
	error183 = true;
  }
  if ((!form.apellidos.value) || (/^ +$/.test(form.apellidos.value))) {
    form.apellidos.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  // Pode conter caracteres alfanuméricos, hífen '-' , apóstrofe ''' , ponto '.' e espaço ' '  
  if (!/^[0-9a-zA-Z\.\-\'\ ãáâéêíõóôúüçÃÁÂÉÊÍÕÓÔÚÜÇ]*$/.test(form.apellidos.value)) {
    form.apellidos.style.backgroundColor = color;
    errorRegisterForm = true;
	error183 = true;
  }
  // Nombre + Apellidos deve conter 5 ou mais caracteres 
  if ((form.nombre.value.length + form.apellidos.value.length) < 6) {
	form.nombre.style.backgroundColor = color;
	form.apellidos.style.backgroundColor = color;
	errorRegisterForm = true;
	errorNombre = true;
  }
  if (!form.dia_nac.value) {
    form.dia_nac.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (!form.mes_nac.value) {
    form.mes_nac.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (!form.ano_nac.value) {
    form.ano_nac.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (!form.sexo.value) {
    form.sexo.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (!form.mail.value || !/^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(form.mail.value)) {
    form.mail.style.backgroundColor = color;
    errorRegisterForm = true;
	errorMail = true;
  }     
  if (!form.pais.value) {
    form.pais.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  // check for codigo_postal OR ciudad/estado
  if (form.pais.value == 56) {
  	if (document.getElementById('ciudadestado').style.display == 'block') {
      if (!form.provincia.value) {
	    form.provincia.style.backgroundColor = color;
	    errorRegisterForm = true;
      }
      if (!form.poblacion.value) {
	    form.poblacion.style.backgroundColor = color;
	    errorRegisterForm = true;
      }	  
	} else {	
      if (!form.codigo_postal.value) {
	    form.codigo_postal.style.backgroundColor = color;
	    errorRegisterForm = true;
	  }
	}
  } else {
    if (!form.poblacion.value) {
	  form.poblacion.style.backgroundColor = color;
	  errorRegisterForm = true;
    }
  }
  /*	1. O login deve ter no mínimo 3 caracteres e no máximo 20 caracteres; 
	2. O login não pode começar com um caracter diferente de letra;  
	3. O login pode conter somente letras não acentuadas, dígitos, "_" (underline), "." (ponto);
	4. O login não pode terminar com "_" (underline) e "." (ponto); 
	5. O login não pode ter combinações de "_ _" (underline - underline), "_." (underline - ponto), "._" (ponto - underline) e ".." (ponto - ponto). */
  if ((!form.login.value) ||
	  (form.login.value.length < 3) ||
	  (form.login.value.length > 20) ||
	  (!/^[a-zA-Z]*$/.test(form.login.value.substring(0,1))) ||
	  (!/^[0-9a-zA-Z\.\_]*$/.test(form.login.value)) ||
	  (form.login.value.substring(form.login.value.length-1,form.login.value.length) == "_") ||
	  (form.login.value.substring(form.login.value.length-1,form.login.value.length) == ".") ||
	  (form.login.value.indexOf("__") > -1) ||
	  (form.login.value.indexOf("_.") > -1) ||
	  (form.login.value.indexOf("._") > -1) ||
	  (form.login.value.indexOf("..") > -1)
	 ) {
	form.login.style.backgroundColor = color;
	errorRegisterForm = true;
	errorUsername = true;
  }
  if (!form.clave.value || !form.conf_clave.value) {
    form.clave.style.backgroundColor = color;
    form.conf_clave.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (!form.codecheck.value || !/^[0-9]{6}$/.test(form.codecheck.value)) {
    form.codecheck.style.backgroundColor = color;
    errorRegisterForm = true;
  }
  if (form.clave.value != form.conf_clave.value) {
    MM_showHideLayers('regpass','','show'); 
    errorRegisterForm = true;
  }
  controlRegisterForm = 0;
  if (errorRegisterForm) {
  	if (errorNombre) { msgError += "<br />* " + msgErrorNombre; }
  	if (error183) { msgError += "<br />* " + msgError183; }
	if (errorMail) { msgError += "<br />* " + msgErrorMail; }
	if (errorUsername) { msgError += "<br />* " + msgErrorUser; }
	setMsgErrorBox(msgError);
	controlRegisterForm = 1;
    return false;
  } else
    return true;
}

function validateLoginForm(form) {
  color = '#ffc8c8';
  errorLoginForm = false;  
  cfocus = false;
  if (!form.login.value) {
    form.login.style.backgroundColor = color;
    errorLoginForm = true;
	if (!cfocus) { form.login.focus(); cfocus=true; }
  }
  if (!form.clave.value) {
    form.clave.style.backgroundColor = color;
    errorLoginForm = true;
	if (!cfocus) { form.clave.focus(); cfocus=true; }
  }       
  controlLoginForm = 0;
  if (errorLoginForm)
    return false;
  else
    return true;
}

function validateEditForm(form) {
  color = '#ffc8c8';
  errorEditForm = false;
  cfocus = false;
  
  // Pode conter caracteres alfanuméricos, hífen '-' , apóstrofe ''' , ponto '.' e espaço ' '  
  if (!form.nombre.value || !/^[0-9a-zA-Z\.\-\'\ ãáâéêíõóôúüçÃÁÂÉÊÍÕÓÔÚÜÇ]*$/.test(form.nombre.value)) {
    form.nombre.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.nombre.focus(); cfocus=true; }
  }
  // Pode conter caracteres alfanuméricos, hífen '-' , apóstrofe ''' , ponto '.' e espaço ' '  
  if (!form.apellidos.value || !/^[0-9a-zA-Z\.\-\'\ ãáâéêíõóôúüçÃÁÂÉÊÍÕÓÔÚÜÇ]*$/.test(form.apellidos.value)) {
    form.apellidos.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.apellidos.focus(); cfocus=true; }
  }
  // Nombre + Apellidos deve conter 5 ou mais caracteres 
  if ((form.nombre.value.length + form.apellidos.value.length) < 6) {
	form.nombre.style.backgroundColor = color;
	form.apellidos.style.backgroundColor = color;
	errorEditForm = true;
	if (!cfocus) { form.nombre.focus(); cfocus=true; }
  }
  if (!form.codigo_postal.value) {
    form.codigo_postal.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.codigo_postal.focus(); cfocus=true; }
  }  
  if (!form.dia_nac.value) {
    form.dia_nac.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.dia_nac.focus(); cfocus=true; }
  }
  if (!form.mes_nac.value) {
    form.mes_nac.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.mes_nac.focus(); cfocus=true; }
  }
  if (!form.ano_nac.value) {
    form.ano_nac.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.ano_nac.focus(); cfocus=true; }
  }
  if (!form.sexo.value) {
    form.sexo.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.sexo.focus(); cfocus=true; }
  }
  if (!form.mail.value || !/^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(form.mail.value)) {
    form.mail.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.mail.focus(); cfocus=true; }
  }     
  if (!form.pais.value) {
    form.pais.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.pais.focus(); cfocus=true; }
  }
  if ((!form.clave.value || !form.conf_clave.value) || (form.clave.value != form.conf_clave.value)) {
    form.clave.style.backgroundColor = color;
    form.conf_clave.style.backgroundColor = color;
    errorEditForm = true;
	if (!cfocus) { form.clave.focus(); cfocus=true; }
  }
  controlEditForm = 0;
  if (errorEditForm) {
  	controlEditForm = 1;
    return false;
  } else
    return true;
}

function showCorrectFields(id_country) {
	if (id_country == 56) {
		document.getElementById('regestado').innerHTML = document.getElementById('liststate').innerHTML;
		document.getElementById('ciudadestado').style.display='none';
		document.getElementById('codigo_postal').style.display='block';
		dpass='regpass';
	} else {
		document.getElementById('regestado').innerHTML = document.getElementById('inputstate').innerHTML;
		document.getElementById('ciudadestado').style.display='block';
		document.getElementById('codigo_postal').style.display='none';
		dpass='regpass2';
	};
}