var nombreEstablecimiento, direccion, localidad, cpostal, provincia, telefono1, telefono2, fax, web, email;
var regexNombreEstablecimiento, regexDireccion, regexLocalidad, regexCpostal, regexProvincia, regexTelefono1, regexTelefono2, regexFax, regexWeb, regexEmail;
var suma = 0;
var errores = new Array();
var campo, sec;
var numSubcampos;
var salida;
function error(n){
	errores.push(n);//Almaceno el error
	document.getElementById(sec).style.backgroundColor = "#FF0000";//Titular de la seccion
	if(campo != ""){//El campo de La seccion
		document.getElementById(campo).style.borderColor = "#FF0000";
		document.getElementById(campo).style.borderWidth = 1+"px";
		document.getElementById(campo).style.borderStyle = "solid";
		document.getElementById(campo).style.color = "#FF0000";
	}
}
function bien(){
	if(campo != ""){//El campo de La seccion
		document.getElementById(campo).style.borderColor = "#CCCC00";
		document.getElementById(campo).style.borderWidth = 1+"px";
		document.getElementById(campo).style.borderStyle = "solid";
		document.getElementById(campo).style.color = "#CCCC00";
	}else{//Titular de la seccion que no sea la seccion 2
		if(sec != "sec2"){
			document.getElementById(sec).style.backgroundColor = "#CCCC00";
		}
	}
	if(numSubcampos != 9 && sec == "sec2"){//Solo Seccion "2. Establecimiento". Bien pero Aun hay errores en los campos, falla alguno de los 9
		document.getElementById(sec).style.backgroundColor = "#FF0000";//pongo titular en no ok
	}else{//Bien y no hay errores, los 8 campos bien, pongo titular en ok
		document.getElementById(sec).style.backgroundColor = "#CCCC00";
	}
	suma ++;	
}
function limpiaErrores(){
	errores = new Array();
}
function espera(){
	salida = "<b><br/><img src='img/flecha.gif' />Enviando los datos al servidor, espere...</b>";
	document.getElementById("salida").style.display = "";
	document.getElementById("salida").firstChild.style.borderColor = "#FFFFFF";
	document.getElementById("salida").firstChild.style.color = "#969A00";
	document.getElementById("salida").firstChild.innerHTML = salida;
}
function validaCliente(){
	numSubcampos = 0;
	suma = 0; 
	/*Validacion, 18 = ok*/
	validaTipoEstablecimiento();
	validaNombreEstablecimiento();//1
	validaComunidad();//2
	validaProvincia();//3
	validaLocalidad();//4
	validaDireccion();//5	
	validaCpostal();//6	
	validaTelefono1();//7
	validaEmail();//8
	validaContacto();//9
	validaDescripcionGeneral();
	validaDescripcionAmpliada();
	validaAccesibilidad();
	validaMascotas();
	validaCategoria();
	validaUbicacion();
	validaLocalizacion();
	validaQuever();
	
	/**/
	//validaTelefono2();
	//validaFax();
	//validaWeb();
	//validaFoto();
	//validaTarifas();
	
	if (suma == 18){//Los campos que valida son 17 si no error.
		document.getElementById("salida").style.display = "none";//Oculto la salida
		if (validaCondiciones()){
			if(validaFoto()){
				espera();
				document.getElementById("form").action = "form2.php";
				document.getElementById("form").submit();	
			}else{
				alert("Atención se requiere que las imagenes sean de formato .jpg");
			}
		}else{
			alert('Compruebe que ha leido y aceptado las Condiciones Generales.');
		}
	}else{
		salida = "<b>Atención debe revisar los siguientes errores:</b><br/>";
		for(var m=0;m<errores.length;m++){
			//salida = salida + (m+1) + ". " + errores[m] + "<br/>";
			salida = salida + errores[m] + "<br/>";
		}
		limpiaErrores();
		document.getElementById("salida").style.display = "";
		//document.getElementById("salida").firstChild.firstChild.nodeValue = salida;
		document.getElementById("salida").firstChild.innerHTML = salida;
	}
}
/*-----------------------------------------------------------------------------------*/
function validaTipoEstablecimiento(){
	sec = 'sec1';//El titular de la seccion "H5"
	campo = '';//No tiene campos input
	var tipoa=document.getElementById('tipoa').checked;
	var tipob=document.getElementById('tipob').checked;
	var tipoc=document.getElementById('tipoc').checked;
	var tipod=document.getElementById('tipod').checked;
	var tipoe=document.getElementById('tipoe').checked;
	var tipof=document.getElementById('tipof').checked;
	
	if (tipoa!=true && tipob!=true && tipoc!=true && tipod!=true && tipoe!=true && tipof!=true){
		error("1. <a href='#s1'>Tipo de establecimiento</a>: Seleccione un Tipo de Establecimiento");
	}else{
		bien();
	}
}
function validaNombreEstablecimiento(){
	sec = 'sec2';
	campo = 'nombrestablecimiento';
	nombreEstablecimiento = document.getElementById(campo).value;
	regexNombreEstablecimiento = "^[A-Za-z\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA\xD1\x2D]+$";
/*	
	if (nombreEstablecimiento.match(regexNombreEstablecimiento)!=null){
		numSubcampos ++;
		bien();
	}else{		
		error("2. <a href='#s2'>Establecimiento</a>: Debe especificar un Nombre de Establecimiento");
	}
*/
	if (nombreEstablecimiento != ""){
		numSubcampos ++;
		bien();
	}else{		
		error("2. <a href='#s2'>Establecimiento</a>: Debe especificar un Nombre de Establecimiento");
	}
}
function validaComunidad(){
	sec = 'sec2';
	campo = 'comunidad2';
	comunidad = document.getElementById(campo).value;
	regexComunidad = "^[A-Za-z\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA.]+$";
	if (comunidad.match(regexComunidad)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar la Comunidad Autónoma");
	}
}
function validaProvincia(){
	sec = 'sec2';
	campo = 'provincia2';
	provincia = document.getElementById(campo).value;
	regexProvincia = "^[A-Za-z\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA]+$";
	if (provincia.match(regexProvincia)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar la Provincia");
	}
}
function validaLocalidad(){
	sec = 'sec2';
	campo = 'localidad';
	localidad = document.getElementById(campo).value;
	regexLocalidad = "^[A-Za-z,.()\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA\xD1\x2D]+$";
	/*
	if (localidad.match(regexLocalidad)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Deve revisar su Localidad");
	}
	*/
	if (localidad != ""){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar su Localidad");
	}		
}
function validaDireccion(){
	sec = 'sec2';
	campo = 'direccion';
	direccion = document.getElementById(campo).value;
	regexDireccion = "^[A-Za-z0-9,.()\xBA\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA\xD1]+$";
	/*
	if (direccion.match(regexDireccion)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar la Dirección");
	}
	*/
	if (direccion != ""){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar la Dirección");
	}	
}
function validaCpostal(){
	sec = 'sec2';
	campo = 'cpostal';
	cpostal = document.getElementById(campo).value;
	regexCpostal = "^[0-9]+$";
	if (cpostal.match(regexCpostal)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar el Código Postal");
	}
}
function validaTelefono1(){
	sec = 'sec2';
	campo = 'telefono1';
	telefono1 = document.getElementById(campo).value;
	regexTelefono1 = "^[0-9()+\x20]+$";
	if (telefono1.match(regexTelefono1)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar el Teléfono 1");
	}
}
function validaEmail(){
	sec = 'sec2';
	campo = 'email';
	email = document.getElementById(campo).value;
	regexEmail = "^([0-9a-zA-Z\xF1\x2D]+(?:[._][0-9a-zA-Z\xF1\x2D]+)*)\@([0-9a-zA-Z\xF1\x2D]+(?:[._-][0-9a-zA-Z\x2D]+)*(?:[.][0-9a-zA-Z]{2,3}))$";
	if (email.match(regexEmail)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe revisar su E-mail");
	}
}
function validaContacto(){
	sec = 'sec2';
	campo = 'contacto';
	provincia = document.getElementById(campo).value;
	regexProvincia = "^[A-Za-z\x20\xE1\xE9\xED\xF3\xFA\xF1\xC1\xC9\xCD\xD3\xDA\xD1\x2D]+$";
	if (provincia.match(regexProvincia)!=null){
		numSubcampos ++;
		bien();
	}else{
		error("2. <a href='#s2'>Establecimiento</a>: Debe poner una persona de Contacto");
	}
}
function validaDescripcionGeneral(){//<---------------------------------------------------------------------------------------------------
	sec = 'sec3';
	campo = 'descripciongeneral';
	if (document.getElementById(campo).value != ""){
		bien();
	}else{
		error("3. <a href='#s3'>Descripción general</a>: Debe poner una breve descripción de su Alojamiento");
	}
}
function validaDescripcionAmpliada(){
	sec = 'sec4';
	campo = 'descripcionampliada';
	if (document.getElementById(campo).value != ""){
		bien();
	}else{
		error("4. <a href='#s4'>Descripción ampliada</a>: Debe poner una descripción Ampliada");
	}
}
function validaAccesibilidad(){
	sec = 'sec7';
	campo = '';
	var accea=document.getElementById('accea').checked;
	var acceb=document.getElementById('acceb').checked;
	var accec=document.getElementById('accec').checked;
	var acced=document.getElementById('acced').checked;
	
	if (accea!=true && acceb!=true && accec!=true && acced!=true){
		error("7. <a href='#s7'>Condiciones de accesibilidad</a>: Seleccione un tipo de Accesibilidad");
	}else{
		bien('');
	}
}
function validaMascotas(){
	sec = 'sec8';
	campo = '';
	var mascotaa=document.getElementById('mascotaa').checked;
	var mascotab=document.getElementById('mascotab').checked;
	
	if (mascotaa!=true && mascotab!=true){
		error("8. <a href='#s8'>Admite mascotas</a>: Seleccione si admite mascotas o no");
	}else{
		bien('');
	}
}
function validaCategoria(){
	sec = 'sec9';
	campo = '';
	var tipoa=document.getElementById('alojamientocompartido').checked;
	var tipob=document.getElementById('casacompleta').checked;
	var tipoc=document.getElementById('habitaciones').checked;
	var tipod=document.getElementById('hotelrural').checked;
	var tipoe=document.getElementById('apartamentos').checked;
	var tipof=document.getElementById('hotel').checked;
	var tipog=document.getElementById('hostal').checked;
	var tipoh=document.getElementById('pension').checked;
	var tipoi=document.getElementById('albergues').checked;
	var tipoj=document.getElementById('camping').checked;
	var tipok=document.getElementById('bungalows').checked;
	var tipol=document.getElementById('aventura').checked;
	var tipom=document.getElementById('actividades').checked;
	var tipon=document.getElementById('restaurante').checked;
	var tipoo=document.getElementById('otros').value;
	var tipop=document.getElementById('categoria').value;

	if (tipoa!=true && tipob!=true && tipoc!=true && tipod!=true && tipoe!=true && tipof!=true && tipog!=true && tipoh!=true && tipoi!=true && tipoj!=true && tipok!=true && tipol!=true && tipom!=true && tipon!=true && tipoo=="" && tipop==""){
		error("9. <a href='#s9'>Categoría</a>: Seleccione una Categoría");
	}else{
		bien();
	}
}
function validaUbicacion(){
	sec = 'sec10';
	campo = '';
	var tipoa=document.getElementById('playa').checked;
	var tipob=document.getElementById('rural').checked;
	var tipoc=document.getElementById('montana').checked;
	var tipod=document.getElementById('urbano').checked;
	var tipoe=document.getElementById('interior').checked;
	var tipof=document.getElementById('ubicacion1').checked;
	var tipog=document.getElementById('ubicacion2').checked;
	var tipoh=document.getElementById('ubicacion3').checked;
	
	if ((tipoa!=true && tipob!=true && tipoc!=true && tipod!=true && tipoe!=true) || (tipof!=true && tipog!=true && tipoh!=true)){
		error("10. <a href='#s10'>Ubicación</a>: Seleccione una Ubicación");
	}else{
		bien();
	}
}
function validaLocalizacion(){
	sec = 'sec11';
	campo = 'localizacion';
	if (document.getElementById(campo).value != ""){
		bien('');
	}else{
		error("11. <a href='#s11'>Localización</a>: Debe poner una Localización");
	}
}
function validaQuever(){
	sec = 'sec12';
	campo = 'quever';
	if (document.getElementById(campo).value != ""){
		bien('');
	}else{
		error("12. <a href='#s12'>Qué ver</a>: Debe poner una descripción de Que ver");
	}
}
function validaCondiciones(){
	var m = document.getElementById("condiciones").checked;
	return m;
}
//-----------------------------------------
function validaFoto(){//Solo si hay fotos me aseguro que sean .jpg
/*
	sec = 'sec5';
	campo1 = document.getElementById('fotografia1').value;
	campo2 = document.getElementById('fotografia2').value;
	campo3 = document.getElementById('fotografia3').value;
	campo4 = document.getElementById('fotografia4').value;
	campo5 = document.getElementById('fotografia5').value;
	if (campo1 != "" || campo2 != "" || campo3 != "" || campo4 != "" || campo5 != ""){
		if((campo1 != "" && campo1.split('.')[1] != 'jpg') || (campo2 != "" && campo2.split('.')[1] != 'jpg') || (campo3 != "" && campo3.split('.')[1] != 'jpg') || (campo4 != "" && campo4.split('.')[1] != 'jpg') || (campo5 != "" && campo5.split('.')[1] != 'jpg')){
			return false;
		}else{
			return true;
		}
	}else{
		return true;	
	}
	*/
	return true;
}
function limpiaImg(){
	document.getElementById('fotografia1').value = null;
	document.getElementById('fotografia2').value = null;
	document.getElementById('fotografia3').value = null;
	document.getElementById('fotografia4').value = null;
	document.getElementById('fotografia5').value = null;
}
if(navigator.appName.indexOf("Explorer") != -1 || navigator.appName.indexOf("Opera") != -1){
	document.getElementById('botonLimpia').style.display = "none";
}