var bRegistroCorrecto;
var oErrores;
var iContador;
var iPosicionY;
var bFecha1;
var bFecha2;

// Función que valida el formulario de registro
function validarRegistro()
{
	var bPass1 = false;
	var bPass2 = false;
	var bDia = false;
	var bMes = false;
	var bAnyo = false;
	var bMesCad = false;
	var bAnyoCad = false;
	bFecha1 = false;
	bFecha2 = false;
	iPosicionY = 0;
	iContador = 0;
	bRegistroCorrecto = true;
	oErrores = new Array();
	
	//Datos del usuario
	if (validarCampo('tbUsuario'))
		validarLongitud('tbUsuario', 5);
	bPass1 = validarCampo('tbPassword');
	if (bPass1)
		validarLongitud('tbPassword', 8);
	bPass2 = validarCampo('tbPasswordRepeat');
	if (bPass2)
		validarLongitud('tbPasswordRepeat', 8);
	if (bPass1 && bPass2)
		validarRepeat('tbPassword','tbPasswordRepeat');
	validarCampo('tbNombre');
	validarCampo('tbApellido1');
	validarCampo('tbApellido2');
	if (validarCampo('tbDNI'))
		validarDNI('tbDNI');
	validarCampo('cmbSexo');
	bDia = validarCampo('cmbDia');
	bMes = validarCampo('cmbMes');
	bAnyo = validarCampo('cmbAnyo');
	if (bDia && bMes && bAnyo)
		validarFecha('cmbDia','cmbMes','cmbAnyo');
	
	validarCampo('tbTelefono');
	bPass1 = false;
	bPass2 = false;
	if (validarCampo('tbEmail'))
		bPass1 = validarCorreo('tbEmail');
	if (validarCampo('tbEmailRepeat'))
		bPass2 = validarCorreo('tbEmailRepeat');
	if (bPass1 && bPass2)
		validarRepeat('tbEmail','tbEmailRepeat');
	
	if (!bRegistroCorrecto)
		iPosicionY = 140;
	
	//Dirección de residencia
	validarCampo('cmbTipoVia');
	validarCampo('tbCalle');
	validarCampo('tbNumero');
	if (validarCampo('tbCodigoPostal'))
		validarCodigoPostal('tbCodigoPostal');
	validarCampo('tbCiudad');
	
	//Datos tarjeta
	if (validarCampo('tbTarjeta'))
		validarTarjeta('tbTarjeta');
	bMesCad = validarCampo('cmbMesCaducidad');
	bAnyoCad = validarCampo('cmbAnyoCaducidad');
	if ((bMesCad) && (bAnyoCad))
		validarCaducidadTarjeta('cmbMesCaducidad','cmbAnyoCaducidad');
	//validarCampo('tbCaptcha');

	if (!bRegistroCorrecto)
	{
		if (iPosicionY==0)
			iPosicionY = 750;
		document.getElementById('trInfoCampos').style.display = "block";
		window.scrollTo(0, iPosicionY);
		
		popUp(oErrores);
	}
		
	return bRegistroCorrecto;
}

function aceptarCondiciones(oControl)
{
	document.getElementById('btnRegistro').disabled = !oControl.checked;
}

function validarCampo(asIdControl) //,asTexto)
{
	var bValor = false;
	if (document.getElementById(asIdControl)!=null)
	{
		var sValor;
		if (trim(document.getElementById(asIdControl).value)!='')
			sValor = trim(document.getElementById(asIdControl).value);
		else
			sValor = trim(document.getElementById(asIdControl).innerHTML);
		
		if ((sValor=='') || (sValor=='-1'))
		{
			esObligatorio(true,asIdControl);
			if (bRegistroCorrecto)
				document.getElementById(asIdControl).focus();			
			bRegistroCorrecto = false;

			if (((asIdControl=='cmbDia') || (asIdControl=='cmbMes') || (asIdControl=='cmbAnyo')) && (bFecha1==false))
			{
				bFecha1=true;
				asIdControl = 'laFechaNacimiento';
			}
			else if (((asIdControl=='cmbMesCaducidad') || (asIdControl=='cmbAnyoCaducidad')) && (bFecha2==false))
			{
				bFecha2=true;
				asIdControl = 'laCaducidad';
			}
			
			var sLiteral = ObtenerLiteral(asIdControl);
			if (sLiteral!='')
				oErrores[iContador++] = 'La dada ' + sLiteral + ' &eacute;s obligat&ograve;ria';
		}
		else
		{
			esObligatorio(false,asIdControl);
			bValor = true;
		}
	}
	return bValor;
}
function validarLongitud(asIdControl, aiLongitud)
{
	var bValor = false;
	if (document.getElementById(asIdControl)!=null)
	{
		var sValor = trim(document.getElementById(asIdControl).value);
		if (sValor.length<aiLongitud)
		{
			esCorrecto(false,asIdControl);
			if (bRegistroCorrecto)
				document.getElementById(asIdControl).focus();
			bRegistroCorrecto = false;
			
			var sLiteral = ObtenerLiteral(asIdControl);
			oErrores[iContador++] = 'La dada ' + sLiteral + ' t&eacute; una extensi&oacute; incorrecta';
		}
		else
		{
			esCorrecto(true,asIdControl);
			bValor = true;
		}
	}
	return bValor;
}
function esObligatorio(abValor,asIdControl)
{
	if (abValor)
	{
		document.getElementById(asIdControl).style.borderColor = "red"
		document.getElementById(asIdControl).style.backgroundColor = "#fdb994"
	}
	else
	{
		document.getElementById(asIdControl).style.borderColor = ""
		document.getElementById(asIdControl).style.backgroundColor = ""
	}
}
function esCorrecto(abValor,asIdControl)
{
	if (abValor)
	{
		document.getElementById(asIdControl).style.borderColor = ""
		document.getElementById(asIdControl).style.backgroundColor = ""
	}
	else
	{
		document.getElementById(asIdControl).style.borderColor = "blue"
		document.getElementById(asIdControl).style.backgroundColor = "#b0e0e6"
	}
}

function trim(sValor)
{
	return sValor.replace(/^\s+|\s+$/g,"");
}


//////  Validaciones  \\\\\\\
// Funcion que valida que el password coincida en los controles
function validarRepeat(asIdPass, asIdRepeat)
{
	var sPass = trim(document.getElementById(asIdPass).value);
	var sPassRepeat = trim(document.getElementById(asIdRepeat).value);
	if (sPass != sPassRepeat)
	{
		if (bRegistroCorrecto)
			document.getElementById(asIdRepeat).focus();
		bRegistroCorrecto = false;
		esCorrecto(false,asIdRepeat);
		
		if (asIdPass=='tbPassword')
			oErrores[iContador++] = 'Les CONTRASENYES no coincideixen';
		else
			oErrores[iContador++] = 'Les adreces de EMAIL no coincideixen';
	}
}

// Funcion que valida el DNI
function validarDNI(asIdControl) 
{
	var bCorrecto = true;
	
	/*if (document.getElementById(asIdControl)!=null)
	{
		var asDNI = trim(document.getElementById(asIdControl).value);
		asDNI = asDNI.toUpperCase();
		
			if(asDNI.length!=9)
				bCorrecto = false; 
			
			if (bCorrecto)
			{
				if (isNaN(asDNI.substring(0,asDNI.length-1))) 
				{
					if(asDNI.substring(0,1)!='X')
						bCorrecto = false;
					else
						asDNI= '0' + asDNI.substring(1,asDNI.length);
				}
				
				if ((bCorrecto) && (!isNaN(asDNI.substring(asDNI.length-1,asDNI.length)))) 
					bCorrecto = false;
				
				if (bCorrecto) 
				{
					var sCaracteres="TRWAGMYFPDXBNJZSQVHLCKET";
					var iPosicion = asDNI.substring(0,asDNI.length-1) % 23;
					var sLetra = sCaracteres.substring(iPosicion,iPosicion+1)
					if (sLetra!=asDNI.substring(asDNI.length-1,asDNI.length)) 
					bCorrecto = false;
				}
			}
			
			if (!bCorrecto)
			{
				if (bRegistroCorrecto)
					document.getElementById(asIdControl).focus();
				bRegistroCorrecto = false;
				esCorrecto(false,asIdControl);
				
				var sLiteral = ObtenerLiteral(asIdControl);
				oErrores[iContador++] = 'La dada ' + sLiteral + ' introdu&iuml;da &eacute;s incorrecta';
			}
	}*/
	
	return bCorrecto;
}

// Función que valida el código postal
function validarCodigoPostal(asIdControl)
{ 
	var bCorrecto = true;
	
	if (document.getElementById(asIdControl)!=null)
	{
		var sCP = trim(document.getElementById(asIdControl).value);
		
		var sExp = /^([1-4][0-9]|0[1-9]|5[0-2])[0-9][0-9][0-9]$/; 
		if (!sExp.test(sCP)) 
		{
			if (bRegistroCorrecto)
				document.getElementById(asIdControl).focus();
			bRegistroCorrecto = false;
			bCorrecto = false;
			esCorrecto(false,asIdControl);
			
			var sLiteral = ObtenerLiteral(asIdControl);
			oErrores[iContador++] = 'La dada ' + sLiteral + ' introdu&iuml;da &eacute;s incorrecta';
		}
	}
	return bCorrecto;
}

//Función que valida el email
function validarCorreo(asIdControl) 
{ 
	var bCorrecto = true;
	if (document.getElementById(asIdControl)!=null)
	{
		var sCorreo = trim(document.getElementById(asIdControl).value);
		var sInicio = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
		var sCaracter = /@[\w\-]+\./;
		var sFin = /\.[^@]{2,3}$/;
		if (((sCorreo.search(sInicio)!=-1) || (sCorreo.search(sCaracter))==-1) || (sCorreo.search(sFin)==-1))
		{
			if (bRegistroCorrecto)
				document.getElementById(asIdControl).focus();
			bRegistroCorrecto = false;
			bCorrecto = false;
			esCorrecto(false,asIdControl);
			
			var sLiteral = ObtenerLiteral(asIdControl);
			oErrores[iContador++] = 'La dada ' + sLiteral + ' introdu&iuml;da &eacute;s incorrecta';
		}
	}
	return bCorrecto;
} 

//Función que valida la fecha
function validarFecha(asIdDia,asIdMes,asIdAnyo) 
{ 
	var bCorrecto = true;
	
	if ((document.getElementById(asIdDia)!=null) && (document.getElementById(asIdMes)!=null) && (document.getElementById(asIdAnyo)!=null))
	{
		var sFecha = document.getElementById(asIdDia).value + '/' + document.getElementById(asIdMes).value + '/' + document.getElementById(asIdAnyo).value;
		var sMes31 = /^([1-3]0|[0-2][1-9]|31|[0-9])\/(1|01|3|03|5|05|7|07|8|08|10|12)\/(19[0-9][0-9]|20[0-9][0-9])$/; 
		var sMes30 = /^([1-3]0|[0-2][1-9]|[0-9])\/(4|04|6|06|9|09|11)\/(19[0-9][0-9]|20[0-9][0-9])$/; 
		var sMes28 = /^([1-2]0|[0-2][1-8]|[0-1]9|[0-9])\/(02|2)\/(19[0-9][0-9]|20[0-9][0-9])$/; 
		var sMes29 = /^([1-2]0|[0-2][1-9]|[0-9])\/(02|2)\/(1904|1908|1912|1916|1920|1924|1928|1932|1936|1940|1944|1948|1952|1956|1960|1964|1968|1972|1976|1980|1984|1988|1992|1996|2000|2004|2008|2012|2016|2020|2024|2028|2032|2036|2040|2044|2048|2052|2056|2060|2064|2068|2072)$/;

		if (!(sMes29.test(sFecha) || sMes31.test(sFecha) || sMes28.test(sFecha) || sMes30.test(sFecha))) 
		{
			if (bRegistroCorrecto)
				document.getElementById(asIdDia).focus();
			bRegistroCorrecto = false;
			bCorrecto = false;
			esCorrecto(false,asIdDia);
			esCorrecto(false,asIdMes);
			esCorrecto(false,asIdAnyo);
			
			var sLiteral = ObtenerLiteral('laFechaNacimiento');
			oErrores[iContador++] = 'La dada ' + sLiteral + ' introdu&iuml;da &eacute;s incorrecta';
		}
	}
	return bCorrecto;
}

//Función que valida la tarjeta
function validarTarjeta(asIdControl) 
{ 
	var bCorrecto = false;
	
	if (document.getElementById(asIdControl)!=null)
	{
		var sTarjeta = trim(document.getElementById(asIdControl).value);
		var iLongitud = sTarjeta.length; 
		var cifra = null; 
		var cifra_cad=null; 
		var iSuma=0;
		
		if (iLongitud!=16) 
			bCorrecto = false; 

		for (var i=0; i<iLongitud; i+=2) 
		{ 
			cifra = parseInt(sTarjeta.charAt(i))*2; 
			if (cifra > 9) 
			{ 
				cifra_cad = cifra.toString(); 
				cifra = parseInt(cifra_cad.charAt(0)) + parseInt(cifra_cad.charAt(1)); 
			} 
			iSuma += cifra; 
		} 

		for (var i=1; i<iLongitud; i+=2)
			iSuma += parseInt(sTarjeta.charAt(i));
			
		if ((iSuma % 10)==0) 
			bCorrecto = true;
		else
		{ 
			//Mastercard 
			if( (sTarjeta.length==16) && (sTarjeta.substring(0,2)==51) || (sTarjeta.substring(0,2)==52) || (sTarjeta.substring(0,2)==53) || (sTarjeta.substring(0,2)==54) || (sTarjeta.substring(0,2)==55))
				bCorrecto = true;
			else				
				bCorrecto = false;
		}
	}
	
	if (bCorrecto==false)
	{
		bRegistroCorrecto = false;
		esCorrecto(false,asIdControl);
		
		var sLiteral = ObtenerLiteral('tbTarjeta');
		oErrores[iContador++] = 'La ' + sLiteral + ' introdu&iuml;da &eacute;s incorrecta';
	}
	
	return bCorrecto;
}

function validarCaducidadTarjeta(asIdControl1,asIdControl2)
{
	var oFecha = new Date();
	var iMes = oFecha.getMonth()+1;
	var iAnyo = oFecha.getFullYear();
	var sMes;
	var sAnyo;
	
	if (trim(document.getElementById(asIdControl1).value)!='')
		sMes = trim(document.getElementById(asIdControl1).value);
	else
		sMes = trim(document.getElementById(asIdControl1).innerHTML);
		
	if (trim(document.getElementById(asIdControl2).value)!='')
		sAnyo = trim(document.getElementById(asIdControl2).value);
	else
		sAnyo = trim(document.getElementById(asIdControl2).innerHTML);
	
	if (parseInt(sMes)<iMes && parseInt(sAnyo)==iAnyo)
	{
		bRegistroCorrecto = false;
		esCorrecto(false,asIdControl1);
		esCorrecto(false,asIdControl2);
		
		var sLiteral = ObtenerLiteral('tbTarjeta');
		oErrores[iContador++] = 'La ' + sLiteral + ' introdu&iuml;da &eacute;s caducada';
	}
}

/*//Función que valida si la cadena pasada como parámetro es numérica
function esNumero(asText)
{
	alert(asText.length);
	
   //var sNumeros = '0123456789';
   //var bEsNumero = true;
   //var sChar;

   //for (i=0; i<sText.length; i++)
   //{ 
		//sChar = sText.charAt(i); 
		//alert(sChar);
      	//if (sNumeros.indexOf(sChar)==-1) 
		//	bEsNumero = false;
   //}
   
   //return bEsNumero;
}*/

function ObtenerLiteral(asIdControl)
{
	var sLiteral = '';
	var sId = asIdControl.replace('tb','la').replace('cmb','la');
	if (document.getElementById(sId)!=null)
		sLiteral = document.getElementById(sId).innerHTML.replace(':','').toUpperCase();

	return sLiteral;
}

function CompruebaEdadMod()
{
	var iDia = document.getElementById('cmbDia').value;
	var iMes = document.getElementById('cmbMes').value;
	var iAnyo = document.getElementById('cmbAnyo').value;
	var iEdad;
	if ((iDia>-1) && (iMes>-1) && (iAnyo>-1))
		iEdad = obtenerEdad(iDia,iMes,iAnyo);
		
	if (iEdad<16)
	{
		// Menores de 16 años
		//document.getElementById('tdEdad').style.display = 'block';
		document.getElementById('tdEdad').style.visibility = 'visible';
		document.getElementById('btnRegistro').disabled = true;
	}
	else if (iEdad<18)
	{
		// 16 y 17 años
		//document.getElementById('tdEdad').style.display = 'block';
		document.getElementById('tdEdad').style.visibility = 'visible';
		document.getElementById('btnRegistro').disabled = true;
	}
	else
	{
		//document.getElementById('tdEdad').style.display = 'none';
		document.getElementById('tdEdad').style.visibility = 'hidden';
		document.getElementById('btnRegistro').disabled = false;
	}
}

function CompruebaEdad()
{
	var iDia = document.getElementById('cmbDia').value;
	var iMes = document.getElementById('cmbMes').value;
	var iAnyo = document.getElementById('cmbAnyo').value;
	var iEdad;
	if ((iDia>-1) && (iMes>-1) && (iAnyo>-1))
		iEdad = obtenerEdad(iDia,iMes,iAnyo);
		
	if (iEdad<16)
	{
		// Menores de 16 años
		//document.getElementById('tdEdad').style.display = 'block';
		document.getElementById('tdEdad').style.visibility = 'visible';
		document.getElementById('chkCondicionesUso').checked = false;
		document.getElementById('chkCondicionesUso').disabled = true;
		document.getElementById('btnRegistro').disabled = true;
	}
	else if (iEdad<18)
	{
		// 16 y 17 años
		//document.getElementById('tdEdad').style.display = 'block';
		document.getElementById('tdEdad').style.visibility = 'visible';
		document.getElementById('chkCondicionesUso').checked = false;
		document.getElementById('chkCondicionesUso').disabled = true;
		document.getElementById('btnRegistro').disabled = true;
	}
	else
	{
		//document.getElementById('tdEdad').style.display = 'none';
		document.getElementById('tdEdad').style.visibility = 'hidden';
		document.getElementById('chkCondicionesUso').disabled = false;
		if(document.getElementById('chkCondicionesUso').checked)
			document.getElementById('btnRegistro').disabled = false;
		else
			document.getElementById('btnRegistro').disabled = true;
	}
}



function obtenerEdad(iDia,iMes,iAnyo)
{
	var iEdad;
	var dHoy = new Date();

	//si el año de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
	if (iAnyo<=99) 
		iAnyo +=1900 

	//resto los años de las dos fechas 
	iEdad = dHoy.getFullYear()- iAnyo - 1; //-1 porque no se si ha cumplido años ya este año 

	//si resto los meses y me da menor que 0 entonces no ha cumplido años. Si da mayor si ha cumplido 
	if (dHoy.getMonth() + 1 - iMes < 0) //+ 1 porque los meses empiezan en 0 
		return iEdad;
	if (dHoy.getMonth() + 1 - iMes > 0) 
		return iEdad + 1;

	//entonces es que eran iguales. miro los dias 
	//si resto los dias y me da menor que 0 entonces no ha cumplido años. Si da mayor o igual si ha cumplido 
	if (dHoy.getUTCDate() - iDia >= 0) 
		return iEdad + 1;

	return iEdad;
}


/* Estaciones */
function showDiv(aiTab)
{
	if (aiTab==1)
	{
		document.getElementById('divEstaciones').style.display = "none";
		document.getElementById('divImgMapa').style.display = "block";
		
		document.getElementById('tbTab1').style.backgroundColor = "#fff";
		document.getElementById('tbTab2').style.backgroundColor = "#add8e6"
	}
	else
	{
		document.getElementById('divImgMapa').style.display = "none";
		document.getElementById('divEstaciones').style.display = "block";
		
		document.getElementById('tbTab1').style.backgroundColor = "#add8e6"
		document.getElementById('tbTab2').style.backgroundColor = "#fff"		
	}
}

/* Baja */
function validarBaja()
{
	var bUser = false; 
	var bPass = false; 
	oErrores = new Array();
	bRegistroCorrecto = true;
	iPosicionY = 0;
	iContador = 0;
	
	bUser = validarCampo('tbUsuario'); 
	if (bUser)
		bUser = validarLongitud('tbUsuario', 5);
	bPass = validarCampo('tbPass');
	if (bPass)
		bPass = validarLongitud('tbPass', 8);
		
	if ((bUser==false) || (bPass==false))
	{
		popUp(oErrores);
		return false;
	}
	else
	{
		if (!confirm('Segur que desitja donar-se de baixa del servei?'))
			return false;
	}
}

function ComprobarUsuario(asPage)
{
	if (document.getElementById('tbUsuario').value.length<5) 
	{
		esCorrecto(false, document.getElementById('tbUsuario').id);
        //alert('La dada "Usuari" ha de tenir cinc car&agrave;cters com a m&iacute;nim');
        //alert('La dada "Usuari" ha de tenir cinc caràcters com a mínim');
        alert('La dada "Usuari" ha de tenir cinc caracters com a minim');
    }
    else
    {
		var sUser;
        var sUrl;
        if (asPage==0)
			sUrl = 'comprobarUsuario.aspx?usuario=';
		else
			sUrl = '../servicio/comprobarUsuario.aspx?usuario=';
		
		sUser = window.showModalDialog(sUrl + document.Form1.tbUsuario.value,'','dialogWidth: 430px;dialogHeight: 150px;center: yes;resize: no;status: yes;help: no');
		if (sUser!=null)
			document.Form1.tbUsuario.value = sUser;
        
    }
}



/* Comprobar usuario */
function comprobarUsuarioPopup()
{
	if (document.Form1.tbUsuario.value.length<5) 
	{
		esCorrecto(false, document.Form1.tbUsuario.id);
        //alert('La dada "Usuari" ha de tenir cinc car&agrave;cters com a m&iacute;nim');
        //alert('La dada "Usuari" ha de tenir cinc caràcters com a mínim');
        alert('La dada "Usuari" ha de tenir cinc caracters com a minim');
        return false;
    }
}

/* Popup */
function popUp(aoErrores)
{
	var i;
	var iAlto = 100;
	var iAncho = 370;
	var sErrores = '';
	var sSalto = '<br>';
	
	for (i=0;i<aoErrores.length;i++)
	{
		sErrores += aoErrores[i] + sSalto;
		iAlto += 13;
	}	
	document.getElementById('popup_inner').style.display = 'block';
	document.getElementById('popup_inner').style.width = iAncho + 'px';
	document.getElementById('popup_inner').style.height = iAlto + 'px';
	//document.getElementById('popup_inner').style.marginTop = iPosicionY + 150 + 'px';
	
	
	/*if (document.body.scrollHeight<window.screen.availHeight)
		document.getElementById('popup_capa').style.height = window.screen.availHeight;
	else
		document.getElementById('popup_capa').style.height = document.body.scrollHeight;*/
	//document.getElementById('popup_capa').style.height = window.screen.availHeight;
	
	document.getElementById('popup_capa').style.height = document.body.scrollHeight;
	document.getElementById('popup_capa').style.width = document.body.offsetWidth;
	//document.body.style.overflow='hidden';
	document.body.style.overflow='visible';
	
	//document.getElementById('popup_capa').style.display='inline'	
	document.getElementById('popup_capa').style.display = 'block';
		
	document.getElementById('popup_contenedor').style.display = 'block';
	document.getElementById('popupTexto').innerHTML = sErrores;
	document.getElementById('WucPopup1_btnAceptar').focus();	
}
function cerrar()
{
	//document.body.style.overflow='visible';
	document.getElementById('popup_capa').style.display = 'none';
	document.getElementById('popup_contenedor').style.display = 'none';
	document.getElementById('popup_inner').style.display = 'none';
	return false;
}

/* Password */
function recuperarPassword()
{
	oErrores = new Array();
	bRegistroCorrecto = true;
	iPosicionY = 0;
	iContador = 0;
	
	if(validarCampo('tbCorreo'))
		validarCorreo('tbCorreo');

	if (!bRegistroCorrecto)
		popUp(oErrores);

	return bRegistroCorrecto;
}

/* Login */
function validarLogin()
{
	oErrores = new Array();
	bRegistroCorrecto = true;
	iPosicionY = 0;
	iContador = 0;
	
	if(validarCampo('WucMenuDerecha1_tbUsuarioLogin'))
		validarLongitud('WucMenuDerecha1_tbUsuarioLogin', 5);
		
	if(validarCampo('WucMenuDerecha1_tbPasswordLogin'))
		validarLongitud('WucMenuDerecha1_tbPasswordLogin', 8);
		
	if (!bRegistroCorrecto)
		popUp(oErrores);

	return bRegistroCorrecto;
}

/* Nivel del password */
function comprobarNivel(sPassword)
{
	var l = 0;  
	var minusculas = hayMinusculas(sPassword);
	var mayusculas = hayMayusculas(sPassword);
	var numeros = hayNumeros(sPassword);
	var simbolos = haySimbolos(sPassword);
	var vocales = hayVocales(sPassword);

	// 1: longitud entre valores
	// 0: fuera de valores (BAJA)
	if ((sPassword.length >= 6) && (sPassword.length <=16))
	{
		l++;
		document.Form1.seguridadNula.style.visibility="hidden";				  	
		document.Form1.seguridadBaja.style.visibility="visible";
		document.Form1.seguridadMedia.style.visibility="visible";
		document.Form1.seguridadAlta.style.visibility="visible";
	}
	
	if (sPassword.length < 8)
	{
		l = 0;
		eliminarSeguridad();
		if ((8 - sPassword.length) != 1)
			document.Form1.seguridadNula.value='Manquen ' + (8 - sPassword.length) + ' caracters';
		else
			document.Form1.seguridadNula.value='Manca ' + (8 - sPassword.length) + ' caracter';
			
		document.Form1.seguridadNula.style.visibility="visible";
		document.Form1.seguridadBaja.style.visibility="hidden";
		document.Form1.seguridadMedia.style.visibility="hidden";
		document.Form1.seguridadAlta.style.visibility="hidden";				  	
		return l;
	}
	
	if (sPassword.length >=16)				 				  
		l++;

	// +0: un tipo
	// +1: dos tipos
	// +2: tres tipos
	// +3: cuatro tipos  
	// minusculas
	if (minusculas != 0)
	{  	
	// minusculas + mayusculas
	if (mayusculas != 0)
	{
		l++;
		// minusculas + mayusculas + numeros
		if(numeros != 0)
		{
			l++;
			// minusculas + mayusculas + numeros + simbolos
			if (simbolos != 0) 			
				l++;  			
		}
		// minusculas + mayusculas
		else 
		{ 		
			// minusculas +  mayusculas + simbolos
			if (simbolos != 0)
				l++;
		}  		
	}
	// minusculas
	else
	{
		// minusculas + numeros
		if(numeros != 0)
		{
			l++;
			// minusculas + numeros + simbolos
			if (simbolos != 0)
				l++;
		}
		// minusculas
		else
		{
			// minusculas + simbolos
			if (simbolos != 0)
				l++;	
		}  	
	}
	}
	else
	{
	// mayusculas
	if (mayusculas != 0)
	{
		l++;
		// mayusculas + numeros
		if(numeros != 0)
		{
			l++;
			// mayusculas + numeros + simbolos
			if (simbolos != 0) 			
				l++;  			
		}
		// mayusculas
		else 
		{ 		
			// mayusculas + simbolos
			if (simbolos != 0)
				l++;
		}  		
	}
	// nada
	else
	{
		// numeros
		if(numeros != 0)
		{
			l++;
			// numeros + simbolos
			if (simbolos != 0)
				l++;
		}
		// nada
		else
		{
			// simbolos
			if (simbolos != 0)
				l++;	
		}  	
	}
	}			  
		
	// -1 si la contraseña está formada por un tipo
	if (minusculas == sPassword.length)
	l--;
	if (mayusculas == sPassword.length)
	l--;
	if (numeros == sPassword.length)
	l--;
	if (simbolos == sPassword.length)
	l--;

	
	// -1 si se supera un porcentaje en grupos pequeños
	if (vocales >= (sPassword.length/2))
	l--;
	if (numeros >= (sPassword.length/2))
	l--;		  

	mostrarSeguridad(l);
	return l; 
}

function eliminarSeguridad()
{
	document.Form1.seguridadBaja.style.background="#DFDFDF";
	document.Form1.seguridadBaja.style.color="#666666";
	document.Form1.seguridadBaja.style.borderColor="#666666";
	document.Form1.seguridadBaja.value="";
	
	document.Form1.seguridadMedia.style.background="#DFDFDF";
	document.Form1.seguridadMedia.style.color="#666666";
	document.Form1.seguridadMedia.style.borderColor="#666666";
	document.Form1.seguridadMedia.value="";
	
	document.Form1.seguridadAlta.style.background="#DFDFDF";
	document.Form1.seguridadAlta.style.color="#666666";
	document.Form1.seguridadAlta.style.borderColor="#666666";
	document.Form1.seguridadAlta.value="";
	
	return;						  						  				
}
				
function mostrarSeguridad(seguridad)
{
	document.Form1.seguridadNula.style.visibility="hidden";				  	
	document.Form1.seguridadBaja.style.visibility="visible";
	document.Form1.seguridadMedia.style.visibility="visible";
	document.Form1.seguridadAlta.style.visibility="visible";				
	
	// Seguridad Baja
	if (seguridad <= 0)
	{
			document.Form1.seguridadBaja.style.background="#FF9999";
			document.Form1.seguridadBaja.style.color="#FF0000";
			document.Form1.seguridadBaja.style.borderColor="#FF0000";
			document.Form1.seguridadBaja.value="Baixa";
	}
	else
	{
			document.Form1.seguridadBaja.style.background="#DFDFDF";
			document.Form1.seguridadBaja.style.color="#666666";
			document.Form1.seguridadBaja.style.borderColor="#666666";
			document.Form1.seguridadBaja.value="";
	}
	
	// Seguridad Media	
	if ((seguridad > 0) && (seguridad < 3))
	{
			document.Form1.seguridadMedia.style.background="#FFFF9F";
			document.Form1.seguridadMedia.style.color="#DFDF00";
			document.Form1.seguridadMedia.style.borderColor="#DFDF00";
			document.Form1.seguridadMedia.value="Mitjana";
	}
	else
	{
			document.Form1.seguridadMedia.style.background="#DFDFDF";
			document.Form1.seguridadMedia.style.color="#666666";
			document.Form1.seguridadMedia.style.borderColor="#666666";
			document.Form1.seguridadMedia.value="";
	}
	
	// Seguridad Alta	
	if (seguridad >= 3)
	{
			document.Form1.seguridadAlta.style.background="#A8FFA8";
			document.Form1.seguridadAlta.style.color="#00A800";
			document.Form1.seguridadAlta.style.borderColor="#00A800";
			document.Form1.seguridadAlta.value="Alta";
	}
	else
	{
			document.Form1.seguridadAlta.style.background="#DFDFDF";
			document.Form1.seguridadAlta.style.color="#666666";
			document.Form1.seguridadAlta.style.borderColor="#666666";
			document.Form1.seguridadAlta.value="";
	}				
}

function hayMinusculas(p)
{
	var v = 'abcdefghijklmnopqrstuvwxyz';
	var l = 0;
	for (i = 0; i < p.length; i++)
		if (v.indexOf(p.charAt(i)) != -1) l+=1;
		
	return l;	
}

function hayMayusculas(p)
{
	var v = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var l = 0;
	for (i = 0; i < p.length; i++)
		if (v.indexOf(p.charAt(i)) != -1) l+=1;
		
	return l;
}

function hayNumeros(p)
{
	var v = '0123456789';
	var l = 0;
	for (i = 0; i < p.length; i++)
		if (v.indexOf(p.charAt(i)) != -1) l+=1;
		
	return l;
}

function haySimbolos(p)
{
	var v = "@#ñÑçÇ";
	var l = 0;
	for (i = 0; i < p.length; i++)
		if (v.indexOf(p.charAt(i)) != -1) l++;							
		
	return l;
}

function hayVocales(p)
{
	var v = 'aeiouAEIOU';
	var l = 0;
	for (i = 0; i < p.length; i++)
		if (v.indexOf(p.charAt(i)) != -1) l+=1;
		
	return l;
}


