///number
var numberOK = "0123456789";
var bedOK = "0123456789-";
var decimalOK = "0123456789.";
var checkphonenr = "0123456789-/() ";
var checkpass = "0123456789-_qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}
function trimAll( strValue ) {
var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
//check email
function check_email(e) 
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	var i=0;
	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
		{ 
			return (false);
		}
	} 
		
	if (document.images) 
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) 
		{
			return (-1);
		} 
	}	
}
//registration 
function verifyregister()
{
	document.getElementById('error').style.visibility='hidden';
	document.getElementById('error').innerHTML='Los campos en rojo son obligatorios.';
	document.getElementById('f1').style.color='';
	document.getElementById('f2').style.color='';
	document.getElementById('f3').style.color='';
	document.getElementById('f4').style.color='';
	document.getElementById('f5').style.color='';
	document.getElementById('f6').style.color='';
	document.getElementById('f7').style.color='';
	document.getElementById('f8').style.color='';
	document.getElementById('f11').style.color='';
	document.getElementById('f12').style.color='';
	document.getElementById('f13').style.color='';
	document.getElementById('f14').style.color='';
	document.getElementById('f15').style.color='';
	document.getElementById('f16').style.color='';
	document.getElementById('f17').style.color='';
	var a,b,c,d,e,f,g,h,k,l,m,n,p,r,t,s,u,e2,acode;
	a=document.forms[0].account.value;
	b=document.forms[0].password.value;
	c=document.forms[0].repeat_pwd.value;
	d=document.forms[0].primerapellido.value;
	e=document.forms[0].fechadenacimiento.value;
	e2=document.forms[0].edad.value;
	f=document.forms[0].nombre.value;
    acode=document.forms[0].code.value;
	m=document.forms[0].phone1.value;
	n=document.forms[0].email.value;
	p=document.forms[0].direccion.value;
	r=document.forms[0].ciudad.value;
	t=document.forms[0].codigopostal.value;
	u=document.forms[0].phone2.value;
	if(trimAll(acode)=='' || trimAll(a)=='' || trimAll(b)=='' || trimAll(c)==''|| trimAll(d)==''||trimAll(e)==''||trimAll(e2)==''|| trimAll(f)==''|| trimAll(m)==''|| trimAll(n)==''|| trimAll(p)==''|| trimAll(r)==''|| trimAll(t)=='')
	{
	if(trimAll(acode) =='' ) 
	 {
 		document.getElementById('f17').style.color='red';
		document.forms[0].code.focus();
	 }
	 if(trimAll(t) =='' ) 
	 {
 		document.getElementById('f16').style.color='red';
		document.forms[0].codigopostal.focus();
	 }
	 if(trimAll(r) =='' ) 
	 {
 		document.getElementById('f15').style.color='red';
		document.forms[0].ciudad.focus();
	 }
	  if(trimAll(p) =='' ) 
	 {
 		document.getElementById('f14').style.color='red';
		document.forms[0].direccion.focus();
	 }
	   if(trimAll(n) =='' ) 
	 {
 		document.getElementById('f13').style.color='red';
		document.forms[0].email.focus();
	 }
	  if(trimAll(m) =='' ) 
	 {
 		document.getElementById('f11').style.color='red';
		document.forms[0].phone1.focus();
	 }

	 if(trimAll(e2) =='' ) 
	 {
 		document.getElementById('f6').style.color='red';
		document.forms[0].edad.focus();
	 }
	  if(trimAll(e) =='') 
	 {
 		document.getElementById('f2').style.color='red';
	 }
	
	
	  if(trimAll(f) =='' ) 
	 {
 		document.getElementById('f5').style.color='red';
		document.forms[0].nombre.focus();
	 }
	  if(trimAll(d) =='' ) 
	 {
 		document.getElementById('f5').style.color='red';
		document.forms[0].primerapellido.focus();
	 }
	 if(trimAll(c) =='' ) 
	 {
 		document.getElementById('f4').style.color='red';
		document.forms[0].repeat_pwd.focus();
	 }
	  if(trimAll(b) =='' ) 
	 {
 		document.getElementById('f3').style.color='red';
		document.forms[0].password.focus();
	 }
	   if(trimAll(a) =='' ) 
	 {
 		document.getElementById('f1').style.color='red';
		document.forms[0].account.focus();
	 }

	  document.getElementById('error').style.visibility='visible';
	  return false;
	}
/*	if(trimAll(a)!='') 
	 		{
					for (i = 0;  i < a.length;  i++)
						{
						ch = a.charAt(i);
						for (j = 0;  j < checkpass.length;  j++)
						if (ch == checkpass.charAt(j))
						break;
						if (j == checkpass.length)
						{	
						document.getElementById("error").innerHTML="El nombre de usuario no puede contener espacios ni caracteres especiales.";
						document.getElementById('f1').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].account.focus();
						 return false;
						break;
						}
					}
			}*/
			if(trimAll(b)!='') 
	 		{
					for (i = 0;  i < b.length;  i++)
						{
						ch = b.charAt(i);
						for (j = 0;  j < checkpass.length;  j++)
						if (ch == checkpass.charAt(j))
						break;
						if (j == checkpass.length)
						{	
						document.getElementById("error").innerHTML="La contrase&ntilde;a no puede contener espacios ni caracteres especiales.";
						document.getElementById('f3').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].password.focus();
						 return false;
						break;
						}
					}
			}
		if(b.length<7)
		{
			document.getElementById("error").innerHTML="La contrase&ntilde;a debe tener un m&iacute;nimo de 7 caracteres.";
			document.getElementById('f3').style.color='red';
			document.getElementById('error').style.visibility='visible';
			document.forms[0].password.focus();
			return false;
		}
		if(trimAll(b) != trimAll(c))
			{
			document.getElementById('error').innerHTML="Las contrase&ntilde;a no coinciden.";
			document.getElementById('error').style.visibility='visible';
			document.getElementById('f3').style.color='red';
			document.getElementById('f4').style.color='red';
			document.forms[0].password.focus();
			return false;
			}
			
			if(trimAll(e2)!='') 
	 		{
					for (i = 0;  i < e2.length;  i++)
						{
						ch = e2.charAt(i);
						for (j = 0;  j < numberOK.length;  j++)
						if (ch == numberOK.charAt(j))
						break;
						if (j == numberOK.length)
						{	
						document.getElementById("error").innerHTML="El formato de edad no es correcto.";
						document.getElementById('f6').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].edad.focus();
						 return false;
						break;
						}
					}
			}
			if(document.forms[0].categoria.options[0].selected==true && document.forms[0].enfermedad.options[0].selected==true)
			{
				document.getElementById("error").innerHTML="Por favor, seleccione el tipo de enfermedad.";
				document.getElementById('f8').style.color='red';
				document.getElementById('error').style.visibility='visible';
				 return false;
				
			}
			if(trimAll(m)!='') 
	 		{
					for (i = 0;  i < m.length;  i++)
						{
						ch = m.charAt(i);
						for (j = 0;  j < checkphonenr.length;  j++)
						if (ch == checkphonenr.charAt(j))
						break;
						if (j == checkphonenr.length)
						{	
						document.getElementById("error").innerHTML="El formato de tel&eacute;fono no es correcto.";
						document.getElementById('f11').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].phone1.focus();
						 return false;
						break;
						}
					}
			}
			
			if(trimAll(u)!='') 
	 		{
					for (i = 0;  i < u.length;  i++)
						{
						ch = u.charAt(i);
						for (j = 0;  j < checkphonenr.length;  j++)
						if (ch == checkphonenr.charAt(j))
						break;
						if (j == checkphonenr.length)
						{	
						document.getElementById("error").innerHTML="El formato de tel&eacute;fono no es correcto.";
						document.getElementById('f12').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].phone2.focus();
						 return false;
						break;
						}
					}
			}
			if(!check_email(n)) 
			 {
				 document.getElementById("error").innerHTML="Por favor, introduzca una direcci&oacute;n de e-mail v&aacute;lida.";
				 document.getElementById('f13').style.color='red';
				 document.getElementById('error').style.visibility='visible';
				 document.forms[0].email.focus();
				 return false;
			}
			if(trimAll(t)!='') 
	 		{
					for (i = 0;  i < t.length;  i++)
						{
						ch = t.charAt(i);
						for (j = 0;  j < numberOK.length;  j++)
						if (ch == numberOK.charAt(j))
						break;
						if (j == numberOK.length)
						{	
						document.getElementById("error").innerHTML="Por favor introduzca un codigo postal v&aacute;lido.";
						document.getElementById('f16').style.color='red';
						document.getElementById('error').style.visibility='visible';
		 				document.forms[0].codigopostal.focus();
						 return false;
						break;
						}
					}
			}
			if(e2<16)
			{
				alert('Si el paciente es menor de edad y de acuerdo a la normativa legal vigente el representante legal debe ser quien rellene los datos de este formulario. Sin embargo, los datos reflejados aquí deben ser los del paciente.');
				return false;
			}
			
	document.forms[0].submit();
}

function changesecuritycode() ///functie pt schimbarea codului de siguranta
{
	var antifloodcode = document.getElementById('antifloodcode');
	
	if (antifloodcode)
	{
		antifloodcode.src +='?'+ Math.round(Math.random()*100000);
	}

	return false;
}
function verlostpassword()
{
var a,b,c;
document.getElementById('error').style.visibility='hidden';
document.getElementById('error').innerHTML="Los campos en rojo son obligatorios.";
document.getElementById('f1').style.color='';
document.getElementById('f2').style.color='';
document.getElementById('f3').style.color='';
a=document.forms[0].code.value;
b=document.forms[0].uname.value;
c=document.forms[0].email.value;
if(trimAll(a)=='')
		{
		document.getElementById('error').innerHTML="Los campos en rojo son obligatorios. ";
		document.getElementById('error').style.visibility='visible';
		document.getElementById('f1').style.color='red';
		document.forms[0].code.focus();
		return false;
		}
if(trimAll(b)=='')
		{
		document.getElementById('error').innerHTML="Los campos en rojo son obligatorios. ";
		document.getElementById('error').style.visibility='visible';
		document.getElementById('f2').style.color='red';
		document.forms[0].uname.focus();
		return false;
		}
if(trimAll(c)=='')
		{
		document.getElementById('error').innerHTML="Los campos en rojo son obligatorios. ";
		document.getElementById('error').style.visibility='visible';
		document.getElementById('f3').style.color='red';
		document.forms[0].email.focus();
		return false;
		}
		if(!check_email(c)) 
		{
		 document.getElementById("error").innerHTML="Por favor, introduzca una direcci&oacute;n de e-mail v&aacute;lida.";
		 document.getElementById('f3').style.color='red';
		 document.getElementById('error').style.visibility='visible';
		 document.forms[0].email.focus();
		 return false;
		}
		document.forms[0].submit();
}

function showenferm()
{
	if( document.forms[0].categoria.options[0].selected==true)	
	{
		document.getElementById('enfermedadid').style.visibility='visible';
	}
	else
		{
			document.forms[0].enfermedad.options[0].selected=true
			document.getElementById('enfermedadid').style.visibility='hidden';
		}
}
function verifyregister2()
{
	if(document.forms[0].disclaimer.checked==false)
			{
				 document.getElementById("error").innerHTML="Para continuar debe leer y aceptar las condiciones que aquí se exponen.";
				 document.getElementById('error').style.visibility='visible';
				 return false;
			}
	document.forms[0].submit();
}