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' ) ) //|| ( abuelo = 'pagina' ) ) 
			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' );
	}

	//hora();	
	dg0 = new Image();dg0.src = "../img/clock/dg0.gif";
	dg1 = new Image();dg1.src = "../img/clock/dg1.gif";
	dg2 = new Image();dg2.src = "../img/clock/dg2.gif";
	dg3 = new Image();dg3.src = "../img/clock/dg3.gif";
	dg4 = new Image();dg4.src = "../img/clock/dg4.gif";
	dg5 = new Image();dg5.src = "../img/clock/dg5.gif";
	dg6 = new Image();dg6.src = "../img/clock/dg6.gif";
	dg7 = new Image();dg7.src = "../img/clock/dg7.gif";
	dg8 = new Image();dg8.src = "../img/clock/dg8.gif";
	dg9 = new Image();dg9.src = "../img/clock/dg9.gif";
	dgam= new Image();dgam.src= "../img/clock/dgam.gif";
	dgpm= new Image();dgpm.src= "../img/clock/dgpm.gif";
	dgc = new Image();dgc.src = "../img/clock/dgc.gif";
	dotime();
};

// 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;
}

function hora(){
fecha = new Date()
var hora=fecha.getHours();
var minutos=fecha.getMinutes();
var segundos=fecha.getSeconds();
if(hora<10){ hora='0'+hora;}
if(minutos<10){minutos='0'+minutos; }
if(segundos<10){ segundos='0'+segundos; }
fech=hora+":"+minutos+":"+segundos;
document.getElementById('hora').innerHTML=fech;
fecha.setSeconds(fecha.getSeconds()+1);
setTimeout("hora()",1000);

}

function dotime(){ 
theTime=setTimeout('dotime()',1000);
d = new Date();
hr= d.getHours()+100;
mn= d.getMinutes()+100;
se= d.getSeconds()+100;
if(hr==100){hr=112;am_pm='am';}
else if(hr<112){am_pm='am';}
else if(hr==112){am_pm='pm';}
else if(hr>112){am_pm='pm';hr=(hr-12);}
tot=''+hr+mn+se;
document.hr1.src = '../img/clock/dg'+tot.substring(1,2)+'.gif';
document.hr2.src = '../img/clock/dg'+tot.substring(2,3)+'.gif';
document.mn1.src = '../img/clock/dg'+tot.substring(4,5)+'.gif';
document.mn2.src = '../img/clock/dg'+tot.substring(5,6)+'.gif';
document.se1.src = '../img/clock/dg'+tot.substring(7,8)+'.gif';
document.se2.src = '../img/clock/dg'+tot.substring(8,9)+'.gif';
document.ampm.src= '../img/clock/dg'+am_pm+'.gif';}
