function addEvent(obj, type, fn) { 
  // thanks to John Resig
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn](window.event); }
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}

addEvent(document, "mousemove", getMousePos);

// mouse tracking
var posX, posY;

function getMousePos(e) { 
  // thanks to Peter-Paul Koch
  if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posX = e.pageX;
		posY = e.pageY;
	}	else if (e.clientX || e.clientY) {
		posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
}

  
function preloadLogos() {
  // precarga de logotipos
  var mouse1 = new Image();
  mouse1.src = "img/minilogo1over.png";
  var mouse2 = new Image();
  mouse2.src = "img/minilogo2over.png";   
  var mouse3 = new Image();
  mouse3.src = "img/minilogo3over.png";
  var mouse4 = new Image();
  mouse4.src = "img/minilogo4over.png";
  var mouse5 = new Image();
  mouse5.src = "img/minilogo5over.png";
  var mouse6 = new Image();
  mouse6.src = "img/minilogo6over.png";
  
  // comportamientos
  var links = document.getElementsByTagName('img');
  var ids = new Array();
  for (i=0; i<links.length; i++) {
    if (links[i].getAttribute("id") && links[i].className == 'minilogo') {
      
      var moSrc = links[i].src.substring(0, links[i].src.lastIndexOf('.'));
      moSrc += 'over.png';
      links[i].moSrc = moSrc;
      links[i].origSrc = links[i].src;
      
      var moBig = links[i].id;
      moBig += 'big';
      links[i].moBig = moBig;
      
      links[i].onmouseover = function () {     
	      this.src = this.moSrc;
	      var biglogo = this.moBig.toString();
	      e = document.getElementById(biglogo);
	      e.style.position = 'absolute';
        e.style.top = posY + 'px'; // encima del ratón (logo mide 140px de alto)
        e.style.left = posX - 200 + 'px'; // logo centrado (mide 190px de ancho)
        e.style.zIndex = 1;
        showhide(biglogo);
      }
    
      links[i].onmouseout = function () {
        this.src = this.origSrc;
	      var biglogo = this.moBig.toString();
	      showhide(biglogo);
      }
      
    }
  }    
}

// showhide logos
var state = 'none';

function showhide(layer) {
  state = (state == 'block') ? 'none' : 'block';
  // IE 4 or 5 (or 6 beta)
  if (document.all) eval("document.all." + layer + ".style.display = state");
  // NETSCAPE 4 or below
  if (document.layers) document.layers[layer].display = state;
  // main function  
  if (document.getElementById && !document.all) {
  theDiv = document.getElementById(layer);
  theDiv.style.display = state;
  }
}

// abrir en nueva ventana
function externalLinks() {
  if (!document.getElementsByTagName) return false;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
  }
}

// abrir/cerrar fieldsets
$(document).ready(function(){

  $("#toggle_obligatorio").click(function () {
        $("#obligatorio").toggle();
  });
  
  $("#toggle_opcional").click(function () {
        $("#opcional").toggle();
  });
  
  $("#toggle_obligatorio2").click(function () {
        $("#obligatorio2").toggle();
  });
  
  $("#toggle_opcional2").click(function () {
        $("#opcional2").toggle();
  });
  
  $("#toggle_opcional3").click(function () {
        $("#opcional3").toggle();
  });
  
});

// función para validar formularios de contacto
function validateContact() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact.name,           'div_name_suscrip',         true))  errs += 1;
  if (!validateEmail(document.forms.contact.email,            'div_email_suscrip',        true))  errs += 1;
  if (!validatePresent(document.forms.contact.asunto,         'div_asunto_suscrip',       true))  errs += 1;
  if (!validatePresent(document.forms.contact.destinatario,   'div_destinatario_suscrip', true))  errs += 1;
  if (!validatePresent(document.forms.contact.comments,       'div_comments',             true))  errs += 1;
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  return (errs==0);
};

function validateNewsletter() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact.name,       'div_name_suscrip',     true))  errs += 1;
  if (!validatePresent(document.forms.contact.apellidos,  'div_surname_suscrip',  true))  errs += 1;
  if (!validateEmail(document.forms.contact.email,        'div_email_suscrip',    true))  errs += 1;
  if (!validatePresent(document.forms.contact.empresa,    'div_empresa_suscrip',  true))  errs += 1;
  if (!validatePresent(document.forms.contact.puesto,     'div_puesto_suscrip',   true))  errs += 1;
  if (!document.forms.contact.aceptar.checked) { alert('No has aceptado la política de protección de datos de OTH.'); return false; }
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  //if (errs==0) { alert('Gracias, ' + document.forms.contact.name.value + '. En breve recibirás un email con tus datos.'); return true; }
  return (errs==0);
};

function validateNoNewsletter() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact2.name2,     'div_name2',  true))  errs += 1;
  if (!validateEmail(document.forms.contact2.email2,      'div_email2', true))  errs += 1;
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  //if (errs==0) { alert('Se te ha enviado un email a [' + document.forms.contact2.email2.value + '] para que verifiques tu baja en la Newsletter de OTH.'); return true; }
  return (errs==0);
};

function validateRecom() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact.name1,     'div_name1',   true))  errs += 1;
  if (!validateEmail(document.forms.contact.email1,      'div_email1',  true))  errs += 1;
  if (!validatePresent(document.forms.contact.name2,     'div_name2',   true))  errs += 1;
  if (!validateEmail(document.forms.contact.email2,      'div_email2',  true))  errs += 1;
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  return (errs==0);
};

function editSubscriber() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact.name,       'div_name',     true))  errs += 1;
  if (!validatePresent(document.forms.contact.apellidos,  'div_surname',  true))  errs += 1;
  if (!validateEmail(document.forms.contact.email,        'div_email',    true))  errs += 1;
  if (!validatePresent(document.forms.contact.empresa,    'div_empresa',  true))  errs += 1;
  if (!validatePresent(document.forms.contact.puesto,     'div_puesto',   true))  errs += 1;
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  //if (errs==0) { alert('Gracias, ' + document.forms.contact.name.value + '. En breve recibirás un email con tus datos.'); return true; }
  return (errs==0);
};

function checkFeedback() {
  var elem;
  var errs=0;
  if (!validatePresent(document.forms.contact.name,       'div_name',       true))  errs += 1;
  if (!validatePresent(document.forms.contact.comments,   'div_comments',   true))  errs += 1;
  if (errs>1)  alert('Hay '+ errs +' campos con errores.');
  if (errs==1) alert('Hay un campo incorrecto.');
  return (errs==0);
};
