﻿var xhr_object = new Array(null, null, null, null, null);
var lastobj = null;

var scrOfX = 0, scrOfY = 0;
var mouse_x=0; var mouse_y=0;

function getScrollXY()
{
  if( typeof( window.pageYOffset ) == 'number' )
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
}

function mouseMove (evt)
{
  getScrollXY();

	if (document.all)
  {
    mouse_x=event.clientX + scrOfX;
    mouse_y=event.clientY + scrOfY;
	}
  else if (document.getElementById)
  {
		mouse_x=evt.clientX + scrOfX;
    mouse_y=evt.clientY + scrOfY;
	}
}
if (document.layers || document.all)
  document.onmousemove = mouseMove;
if (document.addEventListener)
  document.addEventListener('mousemove', mouseMove, true);

//******************************************************************************
function changeLangue(option, page)
{
  var langue = option[option.selectedIndex].value;
  document.location.href = '/cotes2/' + langue + '/' + page;
}

//******************************************************************************
function gardeMatchs(valeur)
{
  reste = new Array();
  for (i = 1; i < 5; i++)
  {
    var tab = document.getElementsByName('ligne_'+i);
    for (idx = 0; idx < tab.length; idx++)
    {
      var obj = tab[idx];
      if (obj != null)
      {
        var reg=new RegExp("-", "g");
        var tableau=obj.value.split(reg);
        id = tableau[0];
        idtab = tableau[1]!=null?tableau[1]:0;

        obj2 = document.getElementById('match_'+id)
        if (obj2 != null)
        {
          if (reste[idtab] == null)
            reste[idtab] = 0;
          if (valeur>=i)
            reste[idtab]++;
          obj2.style.display = valeur<i?"none":"";
        }
      }
    }
  }
  for (id in reste)
  {
    obj2 = document.getElementById('vide_'+id);
    if (obj2 != null)
      obj2.style.display = reste[id]>0?"none":"";
  }
  return false;
}

var affevt = false;
//******************************************************************************
function affichetop()
{
  affevt = !affevt;
  var tab = document.getElementsByName('evt_top');
  for (idx = 0; idx < tab.length; idx++)
  {
    var obj = tab[idx];
    if (obj != null)
    {
      obj2 = document.getElementById('evt_'+obj.value);
      if (obj2 != null)
        obj2.style.display = affevt?"":"none";
    }
  }
  obj = document.getElementById('lientop');
  obj.innerHTML = affevt?texte_cachersuite:texte_voirsuite;
  return true;
}

//******************************************************************************
// Fonctions de gestion du coupon
//******************************************************************************
var coupons = new Array();

function showCoupon()
{
  obj = document.getElementById('coupons');
  var s = '';
  var cook = '';
  for (id in coupons)
  {
    nom = coupons[id]['nom'];
    pays = coupons[id]['pays'];
    sport = coupons[id]['sport'];
    typecote = coupons[id]['typecote'];
    idmatch = coupons[id]['idmatch'];

    switch(parseInt(typecote)){
      case 1: tc = sport==2?'12':'1N2'; break;
      case 2: tc = 'U/O'; break;
      case 3: tc = 'Double'; break;
      case 4: tc = 'HT/FT'; break;
      default : tc = typecote; break;
    }

    s += '<tr><td><img src="images/ballon' + sport + '.png" height="16" width="16" alt=""/></td>' +
         '<td><a href="' + convertiUrl(pays + ' ' + nom) + '-m' + idmatch + '.php">' + nom + '</a> (' + tc + ')</td>' +
         '<td><a href="#" onclick="return retireCoupon(\'' + id + '\')">-</a> </td></tr>';
    cook += idmatch + '#' + nom + '#' + pays + '#' + sport + '#' + typecote +'##';
  }

  if (s != '')
    obj.innerHTML = '<b><a href="coupons.php">' + texte_coupon +'</a></b><br/><table>' + s + '</table>' +
                    '<a href="#" onclick="effaceCoupon()">' + texte_touteffacer + '</a>';
  else
    obj.innerHTML = '';

  //var expDate = new Date();
  //expDate.setTime(expDate.getTime() + (5 * 1000));
  //expDate.setTime(expDate.getTime());
  //document.cookie = 'favoris=' + cook + ";expires=" + expDate.toGMTString() + ';path=/';
  document.cookie = 'favoris=' + cook + ';path=/';
}

//******************************************************************************
function chargeCoupon()
{
  nom = 'favoris';
  debut = document.cookie.indexOf(nom + "=")
  if (debut >= 0)
  {
    debut += nom.length + 1;
    fin = document.cookie.indexOf(";",debut);
    if (fin < 0) fin = document.cookie.length;
    s = unescape(document.cookie.substring(debut,fin));

    var reg=new RegExp("##", "g");
    var tableau=s.split(reg);

    for (id in tableau)
    {
      s = tableau[id];
      if (s != '')
      {
        reg=new RegExp("#", "g");
        var elts = s.split(reg);

        coupon = new Array();
        coupon['nom'] = elts[1];
        coupon['pays'] = elts[2];
        coupon['sport'] = elts[3];
        coupon['typecote'] = elts[4];
        coupon['idmatch'] = elts[0];
        coupons.push(coupon);
      }
    }

    showCoupon();
  }
}

//******************************************************************************
function ajoutCoupon(nomMatch, idMatch, nomPays, sport, typecote)
{
  i = 0;
  for (id in coupons) i++;
  if (i < 6)
  {
    coupon = new Array();
    coupon['nom'] = nomMatch;
    coupon['pays'] = nomPays;
    coupon['sport'] = sport;
    coupon['typecote'] = typecote;
    coupon['idmatch'] = idMatch;
    coupons.push(coupon);
    showCoupon();
  }
  else
    alert(texte_limite6);
  return false;
}

//******************************************************************************
function retireCoupon(id)
{
  delete(coupons[id]);

  showCoupon();

  return false;
}

//******************************************************************************
function effaceCoupon()
{
  for (id in coupons)
    delete(coupons[id]);

  showCoupon();

  return false;
}

//******************************************************************************
// Ensemble de fonctions pour la gestion des listbox en haut de page
//******************************************************************************
function changeSport(option, lg)
{
  var sport = option[option.selectedIndex].value;

  obj = document.getElementById('menuPays');
  obj.style.display = sport==2?"none":"";

  appelAjax(null, 'ajax_stats.php?sport='+sport+'&lg='+lg, true, callbackSport, 3);
  return false;
}

//******************************************************************************
function setListe(nomListe, nomChoix, texteReponse)
{
  obj = document.getElementById(nomListe);

  while(obj.length>0)
    obj.options[0] = null;

  opt = new Option(nomChoix, 0);
  obj.options[obj.length] = opt;

  var table = texteReponse.split('##');
  for (i=0; i < table.length; i++)
    if (table[i] != '')
    {
      elt = table[i].split('$');
      opt = new Option(elt[1], elt[0]);
      obj.options[obj.length] = opt;
    }
}
//******************************************************************************
var callbackSport = function getCallbackSport()
{
  if ((xhr_object[3].readyState == 4))
  {
    document.body.style.cursor = 'default';

    var reponse = xhr_object[3].responseText.split('#$#');
    setListe('menuPays', texte_choixpays + ' ...', reponse[0]);
    setListe('menuChamp', texte_choixchamp + ' ...', reponse[1]);
  }
}

//******************************************************************************
function changePays(option, optionsport, lg)
{
  var pays = option[option.selectedIndex].value;
  var sport = optionsport[optionsport.selectedIndex].value;
  appelAjax(null, 'ajax_stats.php?sport='+sport+'&pays='+pays+'&lg='+lg, true, callbackPays, 3);
  return false;
}

//******************************************************************************
var callbackPays = function getCallbackPays()
{
  if ((xhr_object[3].readyState == 4))
  {
    document.body.style.cursor = 'default';
    setListe('menuChamp', texte_choixchamp + ' ...', xhr_object[3].responseText);
  }
}

//******************************************************************************
function envoi_detail(formu)
{
  var objP = document.getElementById('menuPays');

  paystext = '';
  if (objP.options.selectedIndex >= 0)
  {
    var pays = objP.options[objP.options.selectedIndex];
    if (pays.value>0)
      paystext = pays.text+'-';
  }

  var objC = document.getElementById('menuChamp');
  var idchamp = objC.options[objC.options.selectedIndex].value;
  if (idchamp > 0) {
    var nomchamp = objC.options[objC.options.selectedIndex].text;
    url = convertiUrl(paystext+nomchamp)+'-c'+idchamp + '.php';
    document.location.href=url;
  }
  return false;
}

//******************************************************************************
function appelAjax(obj, url, retour, fn, type)
{
  if (xhr_object[type] == null)
  {
    if(window.XMLHttpRequest)
      xhr_object[type] = new XMLHttpRequest();
    else if(window.ActiveXObject)
      xhr_object[type] = new ActiveXObject("Microsoft.XMLHTTP");
    else
      return true;
  }

  xhr_object[type].open("GET", url, true);
  if (retour)
    xhr_object[type].onreadystatechange = fn;
  xhr_object[type].send(null);

  if (retour)
  {
    document.body.style.cursor = 'wait';
    /*if (obj != null)
      obj.style.cursor = 'wait';*/
    lastobj=obj;
  }

  return false;
}

//******************************************************************************
function convertiUrl(chaine)
{
  var c1 = 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ';
  var c2 = 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy';

  chaine2 = '';
  for (i = 0; i < chaine.length; i++)
  {
    if ((posi = c1.indexOf(chaine.charAt(i)))>=0)
      chaine2 += c2.charAt(posi);
    else
      chaine2 += chaine.charAt(i);
  }

  var reg=new RegExp("[^a-zA-Z0-9]", "g");
  chaine2 = chaine2.replace(reg, '-');
  var reg=new RegExp("-{2,}", "g");
  chaine2 = chaine2.replace(reg, '-');
  var reg=new RegExp("^-", "g");
  chaine2 = chaine2.replace(reg, '');
  var reg=new RegExp("-$", "g");
  chaine2 = chaine2.replace(reg, '');

  chaine2 = chaine2.toLowerCase();

  return chaine2;
}

//******************************************************************************
function ouvre_surebet(idmatch, typecote)
{
  window.open('pop_surebet.php?idm=' + idmatch +'&type=' + typecote, 'Surebet', 'width=400, height=200');
}

//******************************************************************************
function calcul(cote1, cote2, cote3)
{
  misetotal = document.getElementById('misetotal').value;

  c1 = cote2*cote3;
  c2 = cote1*cote3;
  c3 = cote1*cote2;

  total = c1 + (cote2 != 1?c2:0) + (cote3 != 1?c3:0);

  t1 = Math.round(100*c1*misetotal / total) / 100;
  document.getElementById('cote1').innerHTML = t1;
  if (cote2 > 1)
    document.getElementById('cote2').innerHTML = Math.round(100*c2*misetotal / total) / 100;
  if (cote3 > 1)
    document.getElementById('cote3').innerHTML = Math.round(100*c3*misetotal / total) / 100;
  document.getElementById('gain').innerHTML = Math.round(100*(t1*cote1-misetotal))/100;

  return false;
}