function valida_formulario()
{
	$('comp_nombre').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_apellidos').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_mail').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_telefono').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_nombre_empresa').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_cif').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_direccion').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	$('comp_telefono_empresa').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
	error = 0;
	if (comprobar_vacio($('nombre').get('value'))==1)
	{
		$('comp_nombre').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>');
		error=1;
	}
	if (comprobar_vacio($('apellidos').get('value'))==1)
	{
		$('comp_apellidos').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>');
		error=1;
	}
	
	if (validar_vacio_mail(document.getElementById('mail').value)!=0)
	{
		error_tipo = validar_vacio_mail(document.getElementById('mail').value);
		switch(error_tipo)
		{
			case 1: { $('comp_mail').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Mail no valido</a>'); break; }
			case 2: { $('comp_mail').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Mail obligatorio</a>'); break; }
		}
		error=1;
	}
	
	if (comprobar_vacio_numero_longitud(document.getElementById('telefono').value, 9)!=0)
	{
		error_tipo = comprobar_vacio_numero_longitud(document.getElementById('telefono').value, 9);
		switch(error_tipo)
		{
			case 1: { $('comp_telefono').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>'); break; }
			case 2: { $('comp_telefono').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Sólo numeros</a>'); break; }
			case 3: { $('comp_telefono').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo de 9 num.</a>'); break; }
		}
		error=1;
	}
	
	if (comprobar_vacio($('nombre_empresa').get('value'))==1)
	{
		$('comp_nombre_empresa').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>');
		error=1;
	}
	
	if(document.getElementById('cif').value!='')
	{
		if(validar_documento(document.getElementById('cif').value)==false)
		{
			$('comp_cif').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;C.I.F. no valido</a>');
			error=1;
		}
	}
	else
	{
		$('comp_cif').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>');
		error=1;
	}
	
	if (comprobar_vacio($('direccion').get('value'))==1)
	{
		$('comp_direccion').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>');
		error=1;
	}
	
	if (comprobar_vacio_numero_longitud(document.getElementById('telefono_empresa').value, 9)!=0)
	{
		error_tipo = comprobar_vacio_numero_longitud(document.getElementById('telefono_empresa').value, 9);
		switch(error_tipo)
		{
			case 1: { $('comp_telefono_empresa').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo obligatorio</a>'); break; }
			case 2: { $('comp_telefono_empresa').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Sólo numeros</a>'); break; }
			case 3: { $('comp_telefono_empresa').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Campo de 9 num.</a>'); break; }
		}
		error=1;
	}
	
	if(error==0)
	{
		comprobar_mail(document.getElementById('mail').value);
	}
}

function comprobar_mail(mail)
{
	var url = 'fun/comprobar_mail.php';
	document.getElementById('comprobando').style.display='';
	$('comp_mail').set('html', '&nbsp;<a class="rosa_11x_sin"></a>');
	var jsonRequest = new Request.JSON(
									{
										url: url, 
										onSuccess: validar_mail,
										
									}).get({'mail': mail});
}

function enviar_formulario()
{
	var url = 'fun/guardar_usuario.php';
	document.getElementById('guardando').style.display='';
	document.getElementById('wrapper-1').style.display='none';
	var jsonRequest = new Request.JSON(
									{
										url: url, 
										onSuccess: resultado,
									}).get({'nombre': document.getElementById('nombre').value, 'apellidos': document.getElementById('apellidos').value, 'mail': document.getElementById('mail').value, 'telefono': document.getElementById('telefono').value, 'nombre_empresa': document.getElementById('nombre_empresa').value, 'cif': document.getElementById('cif').value, 'direccion': document.getElementById('direccion').value, 'telefono_empresa': document.getElementById('telefono_empresa').value});
}

function resultado(datos_usuario)
{
	document.getElementById('comprobando').style.display='none';
	document.getElementById('guardando').style.display='none';
	document.getElementById('fin').style.display='';
	window.location="index.html"; 
}

function validar_mail(data)
{
	//document.getElementById('datos').innerHTML = mostrar[0];
	//document.getElementById("tabla_cargando").style.display='none';
	//document.getElementById("tabla_datos").style.display='';
	if(data[0]!=1)
	{
		$('comp_mail').set('html', '&nbsp;<img src="./fun/img/ok.png" width="10" height="10" />');
		enviar_formulario();
	}
	else
	{
		$('comp_mail').set('html', '&nbsp;<a class="rosa_11x_sin">&nbsp;Mail en uso</a>');
	}

}

function comprobar_vacio_numero_longitud(valor, longitud)
{
	error_vacio_numero_longitud = 0;
	if(valor=='')
	{
		error_vacio_numero_longitud = 1;
	}
	else
	{
		if(isNaN(valor))
		{
		  error_vacio_numero_longitud = 2;	
		}
		else
		{
			if(valor.length!=longitud)
			{
				error_vacio_numero_longitud = 3;	
			}
		}
	}
	return error_vacio_numero_longitud;
}

function validar_vacio_mail(valor)
{
	if(valor!='')
	{
		var filtro_correo = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if(valor=='')
		{
			return 0;
		}
		else
		{
			if (filtro_correo.test(valor))
			{
				return 0;	
			}
			else
			{
				return 1;	
			}
		}
	}
	else
	{
		return 2;	
	}
}

function comprobar_vacio(valor)
{
	if(valor=='')
	{
		return error_vacio = 1;	
	}
}

function comprobar_numero(valor)
{
	if(isNaN(valor))
	{
		return error_numero = 1;	
	}
}

function comprobar_vacio_numero_longitud(valor, longitud)
{
	error_vacio_numero_longitud = 0;
	if(valor=='')
	{
		error_vacio_numero_longitud = 1;
	}
	else
	{
		if(isNaN(valor))
		{
		  error_vacio_numero_longitud = 2;	
		}
		else
		{
			if(valor.length!=longitud)
			{
				error_vacio_numero_longitud = 3;	
			}
		}
	}
	return error_vacio_numero_longitud;
}

function validar_cif(documento_cif)
{
	par = 0;
	non = 0;
	letras="ABCDEFGHKLMNPQS";
	let=documento_cif.charAt(0);

	if (!isNaN(let))
  	{
  		nif=documento_cif;
  		//validar(nif);
  		return false;
  	}

	if (documento_cif.length!=9)
  	{
  		return false;
  	}

	if (letras.indexOf(let.toUpperCase())==-1)
  	{
  		return false;
  	}

	for (zz=2;zz<8;zz+=2)
  	{
  		par = par+parseInt(documento_cif.charAt(zz));
  	}

	for (zz=1;zz<9;zz+=2)
  	{
  		nn = 2*parseInt(documento_cif.charAt(zz));
  		if (nn > 9) nn = 1+(nn-10)
		{
  			non = non+nn;
		}
	}

	parcial = par + non;

	control = (10 - ( parcial % 10));

	if (control==10)
	{
		control=0;
	}

	if (control!=documento_cif.charAt(8))
  	{
  		return false;
  	}
	return true;
}

function validar_dni(documento_dni)
{
	dni=documento_dni.substring(0,documento_dni.length-1);
	let=documento_dni.charAt(documento_dni.length-1);
	if (!isNaN(let))
 	{
  		return false;
 	}
	else
 	{
  		cadena="TRWAGMYFPDXBNJZSQVHLCKET";
  		posicion = dni % 23;
  		letra = cadena.substring(posicion,posicion+1);
  		if (letra!=let.toUpperCase())
   		{
    		return false;
   		}
 	}
	return true;

}

function validar_documento(documento)
{
	if(validar_dni(documento)==false && validar_cif(documento)==false)
	{
		return false;	
	}
	else
	{
		return true;	
	}
}