function validar_NIE_CIF_NIF(codigo){
	codigo = codigo.toUpperCase();
	var nif_correcto = controlarNIF(codigo);	//Número de Identificación Fiscal: 8 dígitos y una letra al final.
	var cif_correcto = controlarCIF(codigo);	//Código de Identificación Fiscal: una letra al comienzo y 8 dígitos.
	var nie_correcto = controlarNIE(codigo);	//Número de Identificación de Extranjeros: una letra X o T al comienzo, 7 dígitos y una letra al final.
	if(!nie_correcto && !cif_correcto && !nif_correcto){	//si los tres resultan incorrectos...
		alert("El documento de identidad no es correcto");
		return false;
	}else{	//si alguno resultó correcto...
		return true;
	}
}

function controlarNIF(texto){
	var largo = texto.length;
	var numero = texto.substr(0,largo-1);
	var letra = texto.substr(largo-1,1);
	var lista_letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
	if(letra == lista_letras.charAt(numero % 23)){
		return true;	
	}else{
		return false;	
	}
}

function controlarCIF(texto){
	var pares = 0;
    var impares = 0;
    var suma;
    var ultima;
    var unumero;
    var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
    var xxx;
    texto = texto.toUpperCase();
    var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
	if(!regular.exec(texto)){
		return false;
	}
	ultima = texto.substr(8,1);
	for (var cont = 1 ; cont < 7 ; cont ++){
		xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0";
		impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
		pares += parseInt(texto.substr(cont,1));
	}
	xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0";
	impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
         
	suma = (pares + impares).toString();
	unumero = parseInt(suma.substr(suma.length - 1, 1));
	unumero = (10 - unumero).toString();
	if(unumero == 10){
		unumero = 0;
	}         
	if ((ultima == unumero) || (ultima == uletra[unumero])){
		return true;
	}else{
		//alert('El CIF ingresado no es Válido');
		return false;
	}
}

function controlarNIE(texto){	
	var largo = texto.length;
	var primera_letra = texto[0];
	var ultima_letra = texto[largo-1];
	var numero = texto.slice(1,largo-1);
	var lista_letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
	var primera_letra_es_valida = (primera_letra == "X" || primera_letra == "T");
	var numero_es_valido = (numero.length == 7 && !isNaN(numero));
	var ultima_letra_es_valida = (ultima_letra == lista_letras.charAt(numero % 23));
	return (primera_letra_es_valida && numero_es_valido && ultima_letra_es_valida);
}

function quitarEspacios(str){
	return str.replace(/(^\s*)|(\s*$)/g,"");
}

function Validar(form){
	//variable para guardar el valor del campo que se está controlando,
	//luego de quitarle espacios incorrectos:
	var aux = ""; 

	if (form.PER_NHIJOS.value == 1) {
		aux = quitarEspacios(form.PER_EDAD_1.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 2) {
		aux = quitarEspacios(form.PER_EDAD_2.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 3) {
		aux = quitarEspacios(form.PER_EDAD_3.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 4) {
		aux = quitarEspacios(form.PER_EDAD_4.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 5) {
		aux = quitarEspacios(form.PER_EDAD_5.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 6) {
		aux = quitarEspacios(form.PER_EDAD_6.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 7) {
		aux = quitarEspacios(form.PER_EDAD_7.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 8) {
		aux = quitarEspacios(form.PER_EDAD_8.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 9) {
		aux = quitarEspacios(form.PER_EDAD_9.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 10) {
		aux = quitarEspacios(form.PER_EDAD_10.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		
		return false;
		}
	}
	
	aux = quitarEspacios(form.PER_NOMBRE.value);
	if (form.PER_NOMBRE.value == '' || aux.length == 0){
		alert('El nombre no puede quedar vacío');
		form.PER_NOMBRE.focus();
		return false;
	}
	
	if(form.PER_APELLIDO1 != null){	//en el formulario de edición de cliente este campo no existe
		aux = quitarEspacios(form.PER_APELLIDO1.value);
		if (form.PER_APELLIDO1.value == '' || aux.length == 0){
			alert('El apellido 1 no puede quedar vacío');
			form.PER_APELLIDO1.focus();
			return false;
		}
	}
	if(form.PER_APELLIDO2 != null){
		aux = quitarEspacios(form.PER_APELLIDO2.value);
		if (form.PER_APELLIDO2.value == '' || aux.length == 0){
			alert('El apellido 2 no puede quedar vacío');
			form.PER_APELLIDO2.focus();
			return false;
		}
	}

	aux = quitarEspacios(form.PER_NIF.value);
	var pais_seleccionado = form.PER_PAIS.selectedIndex;
	if(pais_seleccionado == 0){
		alert('Debe seleccionar el país');
		form.PER_PAIS.focus();	
		return false;
	}else{
		//controles del dni, sólo para españa:
		if(form.PER_PAIS[pais_seleccionado].text == "ESPAÑA"){
			if (form.PER_NIF.value == '' || aux.length == 0 || form.PER_NIF.value.length < 9){
				alert('El NIF/CIF/NIE no puede quedar vacío o tener menos de 9 dígitos');
				form.PER_NIF.focus();
				return false;
			}		
			if(!validar_NIE_CIF_NIF(aux)){
				form.PER_NIF.focus();
				return false;
			}
		}else{
			//				
		}
	
	}
	
	aux = quitarEspacios(form.PER_TELEFONO.value);	
	if (form.PER_TELEFONO.value == '' || aux.length == 0){
		alert('El teléfono no puede quedar vacío');
		form.PER_TELEFONO.focus();
		return false;
		}

	if (form.PER_DIA.value == '0'){
		alert('Debe completar su fecha de nacimiento.');
		form.PER_DIA.focus();
		return false;
	}
	
	if (form.PER_MES.value == '0'){
		alert('Debe completar su fecha de nacimiento.');
		form.PER_MES.focus();
		return false;
	}
	
	if (form.PER_ANNO.value == '0'){
		alert('Debe completar su fecha de nacimiento.');
		form.PER_MES.focus();
		return false;
	}
	
	if (form.PER_NHIJOS.value == 1) {
		aux = quitarEspacios(form.PER_EDAD_1.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_1.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 2) {
		aux = quitarEspacios(form.PER_EDAD_2.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_2.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 3) {
		aux = quitarEspacios(form.PER_EDAD_3.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_3.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 4) {
		aux = quitarEspacios(form.PER_EDAD_4.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_4.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 5) {
		aux = quitarEspacios(form.PER_EDAD_5.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_5.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 6) {
		aux = quitarEspacios(form.PER_EDAD_6.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_6.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 7) {
		aux = quitarEspacios(form.PER_EDAD_7.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_7.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 8) {
		aux = quitarEspacios(form.PER_EDAD_8.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_8.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 9) {
		aux = quitarEspacios(form.PER_EDAD_9.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_9.focus();
		return false;
		}
	}
	if (form.PER_NHIJOS.value == 10) {
		aux = quitarEspacios(form.PER_EDAD_10.value);
		if (aux.length == 0){
			alert('La edad del hijo no puede quedar vacia');
			form.PER_EDAD_10.focus();
		
		return false;
		}
	}
		
	
	aux = quitarEspacios(form.PER_TELEFONO.value);			
	if (form.EMA_EMAIL.value == '' || aux.length == 0 ){
		alert('El campo e-mail no puede quedar vacío');
		form.EMA_EMAIL.focus();
		return false;
		}
		
		
	
	if (form.EMA_EMAIL.value.indexOf('@', 0) == -1 || form.EMA_EMAIL.value.indexOf('.', 0) == -1){ 
		alert('La dirección de e-mail no es correcta');
		form.EMA_EMAIL.focus();
		return false;
		}
	
	if (form.EMA_EMAIL.value != form.EMA_EMAIL1.value ){
		alert('El e-mail no coincide, por favor verifíquelo');
		form.EMA_EMAIL1.focus();
		return false;
		}
	
	aux = quitarEspacios(form.EMA_PASS.value);	
	if(form.IDCLIENTE == null){//si es el formulario de registro de cliente
		if (form.EMA_PASS.value == ''  || aux.length == 0){
			alert('La contraseña no puede quedar vacía');
			form.EMA_PASS.focus();
			return false;
		}
	}
	
	if (form.EMA_PASS.value != ''){
		if (aux.length < 5 || aux.length > 10){
			alert('La longitud de la contraseña no es correcta');
			form.EMA_PASS.focus();
			return false;
		}
	}
	
	if (form.EMA_PASS.value != form.EMA_PASS1.value){
		alert('La contraseña no coincide, por favor verifíquela');
		form.EMA_PASS1.focus();
		return false;
	}
	
		
	if (!form.privacidad.checked && form.privacidad.value==1){
		alert('Debe leer y aceptar la política de privacidad para registrarse.');
		form.privacidad.focus();
		return false;
	}
	
	form.submit();
}


function mostrarReferencia(){
	//Si la opcion con id Conocido_1 (dentro del documento > formulario con name fcontacto >     y a la vez dentro del array de Conocido) esta activada
	if (document.form1.PER_NHIJOS.value == 1) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='none';
	document.getElementById('edad_hijos_3').style.display='none';
	document.getElementById('edad_hijos_4').style.display='none';
	document.getElementById('edad_hijos_5').style.display='none';
	document.getElementById('edad_hijos_6').style.display='none';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	} else if (document.form1.PER_NHIJOS.value == 2) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='none';
	document.getElementById('edad_hijos_4').style.display='none';
	document.getElementById('edad_hijos_5').style.display='none';
	document.getElementById('edad_hijos_6').style.display='none';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 3) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='none';
	document.getElementById('edad_hijos_5').style.display='none';
	document.getElementById('edad_hijos_6').style.display='none';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 4) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='none';
	document.getElementById('edad_hijos_6').style.display='none';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 5) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='none';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 6) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='block';
	document.getElementById('edad_hijos_7').style.display='none';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 7) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='block';
	document.getElementById('edad_hijos_7').style.display='block';
	document.getElementById('edad_hijos_8').style.display='none';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 8) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='block';
	document.getElementById('edad_hijos_7').style.display='block';
	document.getElementById('edad_hijos_8').style.display='block';
	document.getElementById('edad_hijos_9').style.display='none';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 9) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='block';
	document.getElementById('edad_hijos_7').style.display='block';
	document.getElementById('edad_hijos_8').style.display='block';
	document.getElementById('edad_hijos_9').style.display='block';
	document.getElementById('edad_hijos_10').style.display='none';
	//por el contrario, si no esta seleccionada
	}else if (document.form1.PER_NHIJOS.value == 10) {
	//muestra (cambiando la propiedad display del estilo) el div con id ‘desdeotro’
	document.getElementById('edad_hijos_1').style.display='block';
	document.getElementById('edad_hijos_2').style.display='block';
	document.getElementById('edad_hijos_3').style.display='block';
	document.getElementById('edad_hijos_4').style.display='block';
	document.getElementById('edad_hijos_5').style.display='block';
	document.getElementById('edad_hijos_6').style.display='block';
	document.getElementById('edad_hijos_7').style.display='block';
	document.getElementById('edad_hijos_8').style.display='block';
	document.getElementById('edad_hijos_9').style.display='block';
	document.getElementById('edad_hijos_10').style.display='block';
	//por el contrario, si no esta seleccionada
	} else {
	//oculta el div con id 'desdeotro'
		document.getElementById('edad_hijos_1').style.display='none';
		document.getElementById('edad_hijos_2').style.display='none';
		document.getElementById('edad_hijos_3').style.display='none';
		document.getElementById('edad_hijos_4').style.display='none';
		document.getElementById('edad_hijos_5').style.display='none';
		document.getElementById('edad_hijos_6').style.display='none';
		document.getElementById('edad_hijos_7').style.display='none';
		document.getElementById('edad_hijos_8').style.display='none';
		document.getElementById('edad_hijos_9').style.display='none';
		document.getElementById('edad_hijos_10').style.display='none';
	}
}


////////////////////////////////////////////////////////////////////
//FUNCIONES NUEVAS:



