/////////////////////////////////////////////////////////////////////////////
// Funciones de soporte
/////////////////////////////////////////////////////////////////////////////

var ie = document.all? true : false;

function addEvent(obj, ev, funct) {
  if (ie) obj.attachEvent('on' + ev, funct)
  else obj.addEventListener(ev, funct, true);
};

function innerTxt(tag) {
  var r = '';
  if (ie) r = tag.innerText;
  else {
    var t = tag.childNodes;
    if (t.length > 0)
      for(var x =0; x < t.length; x++) r += t[x].nodeValue || '';
    else
      r = tag.innerHTML;
  }
  return r;
};

function $F(id) {
  var c = document.getElementById(id)
  if (id=='privacitat') if (c.checked == true) c.value = true; else c = null
  if (id=='pais') c.options[c.selectedIndex].value = c.options[c.selectedIndex].text
  if (id=='origen') c.options[c.selectedIndex].value = c.options[c.selectedIndex].text
  return c? c.value : null
}

/////////////////////////////////////////////////////////////////////////////
// Links
/////////////////////////////////////////////////////////////////////////////

var web = "E v a s i ó n ,  d e s c u b r e   a v e n t u r a s";

function onmouse_out() {
  defaultStatus = web;
  return true;
};

function onmouse_over(e) {
  var tag = ie? event.srcElement : this;
  defaultStatus = tag.title || innerTxt(tag) || tag.alt || web;
  if (!ie) e.preventDefault();
  return true;
};

/////////////////////////////////////////////////////////////////////////////
// MENU
/////////////////////////////////////////////////////////////////////////////

function clearCurrentLink(){
  var m = document.getElementById('menu');
  if (m) {
    var a = m.getElementsByTagName("a");
    for(var i=0;i<a.length;i++)
      if(a[i].href == window.location.href.split("#")[0]) {
        var n = a[i].parentNode
        n.className =  "menuSelected"
        n.innerHTML = n.innerHTML.replace(/(^\s*)|(\s*$)/g, "")
        removeNode(a[i]);
      }
  }
}

function removeNode(n){
  if(n.hasChildNodes())
      for(var i=0;i<n.childNodes.length;i++)
          n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
  n.parentNode.removeChild(n);
}

/////////////////////////////////////////////////////////////////////////////
// quisom
/////////////////////////////////////////////////////////////////////////////

function img_roll() {
  var i = document.getElementsByTagName('img');
  for (var x = 0; x < i.length; x++) {
    var ix = i[x];
    if (ix.src.indexOf('img/quisom/') > -1) {
      var p = new Image(60,65);
      p.src=ix.src.replace('bn','color');
      addEvent(ix, 'mouseover', function() {
        var t = ie? event.srcElement : this;
        t.src = t.src.replace('bn','color')
      });
      addEvent(ix, 'mouseout', function() {
        var t = ie? event.srcElement : this;
        t.src = t.src.replace('color','bn')
      });
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// formularis
/////////////////////////////////////////////////////////////////////////////

function formulari_club() {
  var f = document.getElementById('form_club');
  if (f) {
    f.onsubmit = function() {
      if ($F('nom_sencer') && $F('dni') && $F('adreca') && $F('cp') && $F('poblacio') &&
          $F('provincia') && $F('pais') && $F('email') && $F('telefon') && $F('naixement') &&
          $F('origen') && $F('conta') && $F('privacitat') && ($F('naixement') != 'dd/mm/aaaa')) {
      }
      else {
        alert("Alguno de los campos obligatorios marcados con (*) no se ha rellenado\n"+
              "Se han de rellenar todos los campos obligatorios para formalizar el alta.")
        return false
      }
    }
    var n = document.getElementById('naixement');
    if (n) { n.onfocus = function() { if (this.value == 'dd/mm/aaaa') this.value='' } }
  }
}

function formulari_sorteig() {
  var f = document.getElementById('form_sorteig');
  if (f) {
    f.onsubmit = function() {
      if ($F('nom') && $F('dni') && $F('adreca') && $F('cp') && $F('poblacio') &&
          $F('provincia') && $F('email') && $F('telefon') && $F('naixement') &&
          $F('privacitat') && $F('activitat') && ($F('naixement') != 'dd/mm/aaaa')) {
      }
      else {
        alert("Alguno de los campos obligatorios marcados con (*) no se ha rellenado\n"+
              "Se han de rellenar todos los campos obligatorios para formalizar el alta.")
        return false
      }
    }

    var n = document.getElementById('naixement');
    if (n) { n.onfocus = function() { if (this.value == 'dd/mm/aaaa') this.value='' } }
  }
}

/////////////////////////////////////////////////////////////////////////////
// On load
/////////////////////////////////////////////////////////////////////////////

function onwindow_load() {
  var l = document.links;
  for (var x = 0; x < l.length; x++) {
    addEvent(l[x], 'mouseover', onmouse_over);
    addEvent(l[x], 'mouseout', onmouse_out);
  }
  defaultStatus = web;
  clearCurrentLink();
  img_roll();
  formulari_club();
  formulari_sorteig();
  seccio(1);
  rotatepromos()
};

addEvent(window, 'load', onwindow_load);

function seccio(off) {
  var s = document.getElementById('seccio');
  if (s) {
    var t = s.innerHTML
    if (t) {
      var l = document.createElement('div')
      var ls = l.style
      ls.position = 'absolute'
      ls.top = s.offsetTop + off + 'px'
      ls.left = s.offsetParent.offsetLeft + s.offsetLeft + off + 'px'
      ls.width = s.offsetWidth + 'px';
      ls.color = '#FFA500'
      l.innerHTML = t
      s.appendChild(l)
    }
  }
}

function rotatepromos() {
  function r() {
    value = Math.ceil(Math.random()*10)
    return (value > 5)? r() : value
  }

  var p = document.getElementById('promoflash');
  if (p) {
    var a = p.getElementsByTagName('A')[0]
    var i = p.getElementsByTagName('IMG')[0]
    var n = r()

    i.style.display = 'inline'
    a.href = 'img/promos/19_0' + n + 'gran.jpg'
    i.src  = 'img/promos/tn/19_0' + n + 'petit.jpg'
    //var f = 'promos/16_promo0' + r() + '.swf'
    //var so = new SWFObject(f, "flashmovie", "242", "214", "7", "#ffffff");
    //so.addParam("movie", f);
    //so.write("promoflash");
  }
}