window.onload = function()
{
	d = document;
	if ( !d.imagenes )
		d.imagenes = [];
	
	var imagenes = document.getElementsByTagName( 'img' );
	
	// Prefijar im?genes b?sicas como rollover
	for ( i = 0; i < imagenes.length; i++ )
	{
		imagen = imagenes[i];
		abuelo = imagen.parentNode.parentNode.id;
		// Men? de navegaci?n ( de cabecera y pie ) y baners de portada
		if ( ( abuelo == 'baners' ) || ( abuelo == 'cabecera' ) || ( abuelo == 'pie' ) )
			if ( imagenes[i].id != 'logo' )
				imagenes[i].className = 'rollover';
	}
	
	// Precarga y comportamiento de rollover
	for ( i = 0; i < imagenes.length; i++ )
	{
		imagen = imagenes[i];
		if ( imagen.className == 'rollover' )
		{
			nombre = imagen.src.split( '/on/' ).join( '/' );
			d.imagenes[nombre] = [];
			d.imagenes[nombre]['off'] = new Image( imagen.width, imagen.height );
			d.imagenes[nombre]['off'].src = imagen.src;
			d.imagenes[nombre]['on'] = new Image( imagen.width, imagen.height );
			d.imagenes[nombre]['on'].src = ( imagen.src == nombre ) ? nombre.substring( 0, nombre.lastIndexOf( '/' ) ) + '/on' + nombre.substring( nombre.lastIndexOf( '/' ) ) : nombre ;
			
			imagen.onmouseover = rollover;
			imagen.onmouseout = rollover;
		}
	}
	
	if ( document.getElementById )
	{
		// Formulario de contacto
		if ( formulario = document.getElementById( 'contactar' ) )
			prepararContacto( formulario );

		// Formulario de la oficina virtual
		if ( formulario = document.getElementById( 'oficina' ) )
			prepararOficina( formulario );
		
		// Mapa de sedes
		if ( document.getElementById( 'mapa' ) )
			prepararMapaSedes();
		
		// Globos del pie
		if ( document.getElementById( 'red' ) )
			enableTooltips( 'red' );
	}
};

// Mapa de sedes //
function prepararMapaSedes()
{
	var maps = document.getElementsByTagName( 'map' );
	for ( i = 0; i < maps.length; i++ )
	{
		map = maps[i];
		if ( map.name == 'sedes' )
		{
			areas = map.childNodes;
			for ( j = 0; j < areas.length; j++ )
			{
				area = areas[j];
				if ( area.alt )
				{
					area.onclick = fijarSedes;
					area.onmouseout = ocultarSedes;
					area.onmouseover = mostrarSedes;
				}
			}
			break;
		}
	}
}

var sedesFijas = false; // Al salir de la regi?n se ocultan
function fijarSedes() { sedesFijas = !sedesFijas; }

function mostrarSedes()
{
	var areas = this.parentNode.childNodes;
	for ( i = 0; i < areas.length; i++ )
	{
		area = 'sedes_' + areas[i].alt
		if ( document.getElementById( area ) )
			document.getElementById( area ).style.display = ( area == 'sedes_' + this.alt ) ? 'block' : 'none' ;
	}
}

function ocultarSedes()
{
	if ( !sedesFijas )
	{
		var areas = this.parentNode.childNodes;
		for ( i = 0; i < areas.length; i++ )
		{
			area = 'sedes_' + areas[i].alt
			if ( document.getElementById( area ) )
				document.getElementById( area ).style.display = 'none' ;
		}
	}
}


// Formulario de contacto //
function prepararContacto( formulario )
{
	var inputs = formulario.getElementsByTagName( 'input' );
	
	for ( i = 0; i < inputs.length; i++ )
	{
		input = inputs[i];
		if ( input.type == 'button' )
		{
			accion = input.value.toLowerCase();
			if ( accion == 'borrar' )
				input.onclick = borrarContacto;
			else if ( accion == 'enviar' )
				input.onclick = enviarContacto;
		}
	}
}

function borrarContacto()
{
	campos = this.form.elements;
	
	for ( i = 0; i < campos.length; i++ )
	{
		campo = campos[i];
		if ( campo.type == 'text' || campo.type == 'textarea' )
			campo.value = '';
	}
}

function enviarContacto()
{
	var mensaje = '';
	var formulario = this.form;
	
	if ( formulario.nombre.value.trim().length == 0 )
		mensaje += '\n- Tu nombre es un campo obligatorio.';
	if ( formulario.correo.value.trim().length == 0 )
		mensaje += '\n- Correo es un campo obligatorio.';
	else if ( !formulario.correo.value.esCorreoElectronico() )
		mensaje += '\n- Correo no tiene un formato adecuado.';
	if ( formulario.mensaje.value.trim().length == 0 )
		mensaje += '\n- Mensaje es un campo obligatorio.';
	if ( formulario.telefono.value.trim().length == 0 )
		mensaje += '\n- Telefono en es un campo obligatorio.';
	
	if ( mensaje.length > 0 )
		alert( 'Revise los siguientes campos, por favor:\n' + mensaje );
	else
	{
		formulario.enviador.disabled = true; // Evita que pulsen dos veces seguidas
		formulario.submit();
	}
}

// Formulario de oficina //
function prepararOficina( formulario )
{
	var inputs = formulario.getElementsByTagName( 'input' );
	
	if ( formulario.action.indexOf( 'tipo=si' ) > -1 )
	{
		for ( i = 0; i < inputs.length; i++ )
		{
			input = inputs[i];
			if ( input.type == 'button' )
			{
				accion = input.value.toLowerCase();
				if ( accion == 'borrar' )
					input.onclick = borrarOficinaAutorizacion;
				else if ( accion == 'enviar' )
					input.onclick = enviarOficinaAutorizacion;
			}
		}
	}
	else if ( formulario.action.indexOf( 'tipo=no' ) > -1 )
	{
		for ( i = 0; i < inputs.length; i++ )
		{
			input = inputs[i];
			if ( input.type == 'button' )
			{
				accion = input.value.toLowerCase();
				if ( accion == 'borrar' )
					input.onclick = borrarOficinaSolicitud;
				else if ( accion == 'enviar' )
					input.onclick = enviarOficinaSolicitud;
			}
		}
	}	
}

function borrarOficinaAutorizacion() 	// Cliente
{
	campos = this.form.elements;
	
	for ( i = 0; i < campos.length; i++ )
	{
		campo = campos[i];
		if ( campo.type == 'text' || campo.type == 'textarea' )
			campo.value = '';
		else if ( campo.type == 'checkbox' )
			campo.checked = false;
	}
}

function borrarOficinaSolicitud() 		// No cliente
{
	campos = this.form.elements;
	
	for ( i = 0; i < campos.length; i++ )
	{
		campo = campos[i];
		if ( campo.type == 'text' || campo.type == 'textarea' )
			campo.value = '';
	}
}

function enviarOficinaAutorizacion() 	// Cliente
{
	var mensaje = '';
	var formulario = this.form;
	
	if ( formulario.cliente.value.trim().length == 0 )
		mensaje += '\n- Cliente es un campo obligatorio.';
	if ( formulario.recogida.value.trim().length == 0 )
		mensaje += '\n- Recogida en es un campo obligatorio.';
	if ( formulario.nombre.value.trim().length == 0 )
		mensaje += '\n- Nombre en es un campo obligatorio.';
	if ( formulario.direccion.value.trim().length == 0 )
		mensaje += '\n- Direccion en es un campo obligatorio.';
	if ( formulario.provincia.value.trim().length == 0 )
		mensaje += '\n- Provincia en es un campo obligatorio.';
	if ( formulario.poblacion.value.trim().length == 0 )
		mensaje += '\n- Poblacion en es un campo obligatorio.';
	if ( formulario.cp.value.trim().length == 0 )
		mensaje += '\n- Codigo Postal en es un campo obligatorio.';
	if ( formulario.telefono.value.trim().length == 0 )
		mensaje += '\n- Telefono en es un campo obligatorio.';
	if ( formulario.horario.value.trim().length == 0 )
		mensaje += '\n- Horario de recogida en es un campo obligatorio.';
	if ( formulario.autoriza.value.trim().length == 0 )
		mensaje += '\n- Autoriza de recogida en es un campo obligatorio.';
	if ( formulario.fecha.value.trim().length == 0 )
		mensaje += '\n- Fecha de recogida en es un campo obligatorio.';
	
	if ( mensaje.length > 0 )
		alert( 'Revise los siguientes campos, por favor:\n' + mensaje );
	else
	{
		formulario.enviador.disabled = true; // Evita que pulsen dos veces seguidas
		formulario.submit();
	}
}

function enviarOficinaSolicitud() 		// No cliente
{
	var mensaje = '';
	var formulario = this.form;
	
	if ( formulario.cliente.value.trim().length == 0 )
		mensaje += '\n- Cliente es un campo obligatorio.';
	if ( formulario.contacto.value.trim().length == 0 )
		mensaje += '\n- Persona de contacto en es un campo obligatorio.';
	if ( formulario.telefono.value.trim().length == 0 )
		mensaje += '\n- Telefono en es un campo obligatorio.';
	if ( formulario.mensaje.value.trim().length == 0 )
		mensaje += '\n- Informacion que desea recibir en es un campo obligatorio.';

	if ( mensaje.length > 0 )
		alert( 'Revise los siguientes campos, por favor:\n' + mensaje );
	else
	{
		formulario.enviador.disabled = true; // Evita que pulsen dos veces seguidas
		formulario.submit();
	}
}

// Comportamiento de rollover
function rollover()
{
	var imagenes = document.imagenes;
	var nombre = this.src.split( '/on/' ).join( '/' );

	if ( imagenes )
		if ( imagenes[nombre] )
			this.src = imagenes[nombre][ ( this.src == nombre ) ? 'on' : 'off' ].src;
}