var peticion = null;
var idmex = -1;
var DataPartite = 0;
var CampionatoPartite = 0;
var StatoPartite = 0;
var ValorScommessa = 0;
var ID_ANT = -1;
var ID_ANT_STAT = -1;
var ORDERN_ANT = "MAX";
var ORDERN_ANT_STAT = "MAX";
var contOb = 0;
var contObstat = 0;
var ObPartite = new Array();
var ObStatistiche = new Array();
var th1,th2,th3,th4,th5,th6,th7,th8,th9,th10,th11,th12;
var thstat1,thstat2,thstat3,thstat4,thstat5,thstat6,thstat7,thstat8,thstat9;
var in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12;
var instat1,instat2,instat3,instat4,instat5,instat6,instat7,instat8,instat9;
var loaded = false;

Number.prototype.toString = function(){
  if (this < 10) {
    return '0' + this;
  }
  else {
    return this;
  }
}

window.onload = function(){
  //mezzo secondo
  //loaded = false;
  //window.setTimeout('showLoadingImage()', 1000);
  
  creaHeadTabla();
  creaHeadTablaStat();

  descargaMatch();
  intervalo = setInterval(descargaMatch, 100000);

  descargaStatistiche();
}
function getTypeScommessa(tipostat){
	switch(tipostat){
		case 'UA': t = 'Under/Menos Away/Ospite';break;
		case 'UH': t = 'Under/Menos Home/Casa';break;
		case 'OH': t = 'Over/Mas/Piu Home/Casa';break;
		case 'OA': t = 'Over/Mas/piu Away/Ospite';break;
		case 'WH': t = 'Win/Vittoria Home/Casa';break;
		case 'WA': t = 'Win/Vittoria Away/Ospite';break;
		case 'DH': t = 'Draw/Empate/Pareggio Home/Casa';break;
		case 'DA': t = 'Draw/Empate/Pareggio Away/Ospite';break;
		case 'LH': t = 'Lose/Derota/Sconfitta Home/Casa';break;
		case 'LA': t = 'Lose/Derota/Sconfitta Away/Ospite';break;
	}
	return t;
}
function startLoading() {
    loaded = false;
    window.setTimeout('showLoadingImage()', 1000);
}
function showLoadingImage() {
  var el = document.getElementById("loading_box");
  if (el && !loaded) {
      el.innerHTML = '<img src="/images/loading.gif">';
      new Effect.Appear('loading_box');
  }
}
function stopLoading() {
    Element.hide('loading_box');
    loaded = true;
}
/***********************************************************    CHAT    ***********************************************************************/
function InsertMessaggio(){
    messaggio = messaggio.replace("?"," ");
	new Ajax.Request('InsertMessaggio.php',
	{method: 'post',
	asynchronous: true,
	parameters: 'nick='+nick+'&messaggio='+messaggio+'&room=indexmain',
	onSuccess : procesaRespuestaMessaggio});
}
function procesaRespuestaMessaggio(respuesta){
	$('resmex').innerHTML = respuesta.responseText;
}
function selezionaMessaggio(){
	messaggio = $F("mex");
	nick = $F("nick");
	Field.clear("mex");
	Field.focus("mex");
	InsertMessaggio();
}
/**********************************************************************************************************************************/
function descargaStatistiche(){
	
	ObStatistiche = new Array();
	
	new Ajax.Request('getStatistiche.php',
	{method: 'post',
	asynchronous: true,
	parameters: 'campionato='+0,
	onSuccess : procesaRespuestaStatistiche,
	onCreate : startLoading,
    onComplete : stopLoading});
}
function procesaRespuestaStatistiche(respuesta){
	muestraStat(respuesta.responseText);
}
function selezionaTH(ID){
	in1.style.backgroundColor = '#153e7e';
	in2.style.backgroundColor = '#153e7e';
	in3.style.backgroundColor = '#153e7e';
	in4.style.backgroundColor = '#153e7e';
	in5.style.backgroundColor = '#153e7e';
	in6.style.backgroundColor = '#153e7e';
	in7.style.backgroundColor = '#153e7e';
	in8.style.backgroundColor = '#153e7e';
	in9.style.backgroundColor = '#153e7e';
	in10.style.backgroundColor = '#153e7e';
	in11.style.backgroundColor = '#153e7e';
	in12.style.backgroundColor = '#153e7e';
	
	switch(ID){
		case 0:	in1.style.backgroundColor = 'red';break;
		case 1:	in2.style.backgroundColor = 'red';break;
		case 2:	in3.style.backgroundColor = 'red';break;
		case 3:	in4.style.backgroundColor = 'red';break;
		case 4:	in5.style.backgroundColor = 'red';break;
		case 5:	in6.style.backgroundColor = 'red';break;
		case 6:	in7.style.backgroundColor = 'red';break;
		case 7:	in8.style.backgroundColor = 'red';break;
		case 8:	in9.style.backgroundColor = 'red';break;
		case 9:	in10.style.backgroundColor = 'red';break;
		case 10: in11.style.backgroundColor = 'red';break;
		case 11: in12.style.backgroundColor = 'red';break;
		case 12: in13.style.backgroundColor = 'red';break;
	}
}
function selezionaTHStat(ID){
	instat1.style.backgroundColor = '#153e7e';
	instat2.style.backgroundColor = '#153e7e';
	instat3.style.backgroundColor = '#153e7e';
	instat4.style.backgroundColor = '#153e7e';
	instat5.style.backgroundColor = '#153e7e';
	instat6.style.backgroundColor = '#153e7e';
	instat7.style.backgroundColor = '#153e7e';
	instat8.style.backgroundColor = '#153e7e';
	instat9.style.backgroundColor = '#153e7e';
	
	switch(ID){
		case 0:	instat1.style.backgroundColor = 'red';break;
		case 1:	instat2.style.backgroundColor = 'red';break;
		case 2:	instat3.style.backgroundColor = 'red';break;
		case 3:	instat4.style.backgroundColor = 'red';break;
		case 4:	instat5.style.backgroundColor = 'red';break;
		case 5:	instat6.style.backgroundColor = 'red';break;
		case 6:	instat7.style.backgroundColor = 'red';break;
		case 7:	instat8.style.backgroundColor = 'red';break;
		case 8:	instat9.style.backgroundColor = 'red';break;
		case 9:	in10.style.backgroundColor = 'red';break;
	}
}
function getCampo(ID,O){
	switch(ID){
		case 0:	campo = O.data;break;
		case 1:	campo = O.paese;break;
		case 2:	campo = O.campionato;break;
		case 3:	campo = O.min;break;
		case 4:	campo = O.teamhome;break;
		case 5:	campo = O.gi;break;
		case 6:	campo = O.go;break;
		case 7:	campo = O.teamaway;break;
		case 8:	campo = O.stato;break;
		case 9:	campo = O.scommessa;break;
		case 10: campo = O.prob;break;
		case 11: campo = O.prob;break;
	}
	return campo;
}
function getCampoStat(ID,O){
	//Linea costruita : $campionato|$squadra|$tipostat|$linea|$partite|$ValoreStat|$PesoStat|$PesoRisk|$ScommesseCorrette|$ScommesseSbagliate
	switch(ID){
		//case 0:	campo = O.campionato;break;
		case 0:	campo = O.squadra;break;
		case 1:	campo = O.tipostat;break;
		case 2:	campo = O.linea;break;
		case 3:	campo = O.partite;break;
		case 4:	campo = O.ValoreStat;break;
		case 5:	campo = O.PesoStat;break;
		case 6:	campo = O.PesoRisk;break;
		case 7:	campo = O.ScommesseCorrette;break;
		case 8:	campo = O.ScommesseSbagliate;break;
	}
	return campo;
}
function OrderTableStat(ID){

	selezionaTHStat(ID);

	var c = 0;
	var i = 0;
	var TEMP = new Array();
	var MAX;
	var IDX = -1;
	var ss ;
	var ORDEN;
	
	if (ID_ANT_STAT == ID){
		if (ORDERN_ANT_STAT == "MAX"){
			ORDEN = "MIN";
		}
		else{
			ORDEN = "MAX";
		}
	}
	else{
		ORDEN = "MIN";
	}
		
	while (i < ObStatistiche.length){
		c = 0;IDX = -1;	
		
		if (ORDEN == "MAX"){
			MAX = "";
		}
		else{
			MAX = "ZZZZZZZZZZZZZZZ";
		}
		
		while (c < ObStatistiche.length){
		
			var oggetto = ObStatistiche[c];
			var campo = getCampoStat(ID,oggetto); //parametrizar il campo.
			var orden = oggetto.orden;
			//alert(campo+"  "+orden);
			if (orden == 0){
				switch(ORDEN){
					case "MAX":
						if (campo > MAX){
							MAX = campo;
							IDX = c;
						}
						break;
					case "MIN":
						if (campo < MAX){
							MAX = campo;
							IDX = c;
						}
						break;
				}
			}
			c++;
		}
		//alert(ObPartite[IDX].teamhome);
		ObStatistiche[IDX].orden = 1;
		
		TEMP[i] = ObStatistiche[IDX];
		i++;
	}
	ObStatistiche = TEMP;
	TEMP = null;
	i = 0;
	var stringa = "";
	//Linea costruita : $campionato|$squadra|$tipostat|$linea|$partite|$ValoreStat|$PesoStat|$PesoRisk|$ScommesseCorrette|$ScommesseSbagliate
	while (i < ObStatistiche.length){
		
		//alert(ObPartite[i].teamhome);
		stringa = stringa + ObStatistiche[i].campionato+"|"+ObStatistiche[i].squadra+"|"+ObStatistiche[i].tipostat+"|"+ObStatistiche[i].linea+"|"+
		                    ObStatistiche[i].partite+"|"+ObStatistiche[i].ValoreStat+"|"+ObStatistiche[i].PesoStat+"|"+ObStatistiche[i].PesoRisk+"|"+
		                    ObStatistiche[i].ScommesseCorrette+"|"+ObStatistiche[i].ScommesseSbagliate+"|";
		i++;
	}
	
	//alert(stringa);	
	muestraStat(stringa);
	
	ID_ANT_STAT = ID;
	ORDERN_ANT_STAT = ORDEN;
}
function OrderTable(ID){

	selezionaTH(ID);
	
	var c = 0;
	var i = 0;
	var TEMP = new Array();
	var MAX;
	var IDX = -1;
	var ss ;
	var ORDEN;
	
	if (ID_ANT == ID){
		if (ORDERN_ANT == "MAX"){
			ORDEN = "MIN";
		}
		else{
			ORDEN = "MAX";
		}
	}
	else{
		ORDEN = "MIN";
	}
		
	while (i < ObPartite.length){
		c = 0;IDX = -1;	
		
		if (ORDEN == "MAX"){
			MAX = "";
		}
		else{
			MAX = "ZZZZZZZZZZZZZZZ";
		}
		
		while (c < ObPartite.length){
		
			var oggetto = ObPartite[c];
			var campo = getCampo(ID,oggetto); //parametrizar il campo.
			var orden = oggetto.orden;
			//alert(campo+"  "+orden);
			if (orden == 0){
				switch(ORDEN){
					case "MAX":
						if (campo > MAX){
							MAX = campo;
							IDX = c;
						}
						break;
					case "MIN":
						if (campo < MAX){
							MAX = campo;
							IDX = c;
						}
						break;
				}
			}
			c++;
		}
		//alert(ObPartite[IDX].teamhome);
		ObPartite[IDX].orden = 1;
		
		TEMP[i] = ObPartite[IDX];
		i++;
	}
	ObPartite = TEMP;
	TEMP = null;
	i = 0;
	var stringa = "";
	
	//Linea costruita : $codice_partita|$datafull|$paese|$campionato|$min|$home|$gi|$go|$away|$stato|$scommessa|$p
	
	while (i < ObPartite.length){
		//alert(ObPartite[i].teamhome);
		stringa = stringa + ObPartite[i].codice+"|"+ObPartite[i].data+"|"+ObPartite[i].paese+"|"+ObPartite[i].campionato+"|"+
		                    ObPartite[i].min+"|"+ObPartite[i].teamhome+"|"+ObPartite[i].gi+"|"+ObPartite[i].go+"|"+
		                    ObPartite[i].teamaway+"|"+ObPartite[i].stato+"|"+ObPartite[i].scommessa+"|"+ObPartite[i].prob+"|"+
		                    ObPartite[i].prevgi+"|"+ObPartite[i].prevgo+"|";
		i++;
	}
	//alert(stringa);	
	muestraMatch(stringa);
	
	ID_ANT = ID;
	ORDERN_ANT = ORDEN;
}
function selezionatoCampionatoPartite(){
	CampionatoPartite = $F('campionatopartite');
	descargaMatch();
}
function selezionaReset(){
	CampionatoPartite = 0;
	DataPartite = 0;
	StatoPartite = 0;
	ValorScommessa = 0;
	descargaMatch();
}
function selezionatoDatePartite(){
	DataPartite = $F('datapartite');
	descargaMatch();
}
function selezionatoValorScommessa(){
	ValorScommessa = $F('valorpartite');
	descargaMatch();
}
function selezionatoStatoPartite(){
	StatoPartite = $F('statopartite');
	//alert(StatoPartite);
	descargaMatch();
}
function descargaMatch(){

    ObPartite = new Array();

  	if (peticion == null) {
    if (window.XMLHttpRequest) {
      peticion = new XMLHttpRequest();
    }
    else {
      peticion = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else {
    peticion.abort();
  }
  //alert(CampionatoPartite);
  peticion.onreadystatechange = procesaMatch;
  var url = 'getlivematch.php'+'?nocache='+Math.random()+'&campionato='+CampionatoPartite+'&data='+DataPartite+'&stato='+StatoPartite+'&valor='+ValorScommessa;
  //alert(url);
  peticion.open('get', url, true);
  peticion.send(null);
}
 
function procesaMatch(){
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      muestraMatch(peticion.responseText);
    }
  }

} 
function getRiskScommessa(prevgi,prevgo){
	var pgi =  parseFloat(prevgi);
	var pgo =  parseFloat(prevgo);
	var t = " ";
	
	if (pgi == 0 || pgo == 0){
		t = 'risk';
	}
	if (pgi > 3 || pgo > 3){
		t = 'risk';
	}
	if (pgi+pgo > 6){
		t = 'risk';
	}
	if (pgi+pgo < 0.5){
		t = 'risk';
	}
	return t;
}
function getTestoScommessa(gi,go,scommessa){
	var t = "";
	var totgoal = 0;
	totgoal = parseInt(gi) + parseInt(go);
	var totgoal1 = parseInt(gi) - 1;
	
	if (scommessa == 'Under 2.5'){
		if (totgoal < 3){t='win';}else{t='lose';}
	}
	if (scommessa == 'Over 2.5'){
		if (totgoal > 2){t='win';}else{t='lose';}
	}
	if (scommessa == 'Win Home 0'){
		if (gi > go){t='win';}else{t='lose';}
	}
	if (scommessa == 'Win Home -1'){
		if (totgoal1 > go){t='win';}else{t='lose';}
	}
	if (scommessa == 'Draw 0'){
		if (gi == go){t='win';}else{t='lose';}
	}
	if (scommessa == 'Draw -1'){
		if (totgoal1 == go){t='win';}else{t='lose';}
	}
	if (scommessa == 'Win Away 0'){
		if (gi < go){t='win';}else{t='lose';}
	}
	if (scommessa == 'Win Away -1'){
		if (totgoal1 < go){t='win';}else{t='lose';}
	}
	//alert(gi+"-"+go+" "+t+" "+scommessa+" "+totgoal+" "+totgoal1);
	return t;
}
function muestraMatch(Match){
	
	/*
	Linea costruita : $codice_partita|$datafull|$paese|$campionato|$min|$home|$gi|$go|$away|$stato|$scommessa|$p
	Cardinalita = 11
	*/
	//alert(Match);
	var partite = Match.split("|");
	var tabla= document.getElementById("tablamatch");
	var filas = tabla.childNodes.length;
	var cont = 0;
	var codice;	var data;var paese;var campionato;var teamhome;
	var gi = 0;var go = 0;var teamaway;var stato;var scommessa;var prob;
	var min;var prevgi;var prevgo;
	var k = 0;
	
	for(var a=0; a<filas; a++)
	{
		var nodo = tabla.childNodes[0];
		tabla.removeChild(nodo);
	}
	
	contOb = 0;
	
	if (partite[0] != -1){
		while (cont < partite.length-1){
	
			codice = partite[cont];
			data = partite[cont+1];
			paese = partite[cont+2];
			campionato = partite[cont+3];
			min = partite[cont+4]+"'";
			teamhome = partite[cont+5];
			gi = partite[cont+6];
			go = partite[cont+7];
			teamaway = partite[cont+8];
			stato = partite[cont+9];
			scommessa = partite[cont+10];
			prob = partite[cont+11];
			probtesto = partite[cont+11]+"%";
			prevgi = partite[cont+12];
			prevgo = partite[cont+13];
			
			//creo l' oggetto partita
			var ObPartita = new Object();
			
			ObPartita.codice = codice;
			ObPartita.data = data;
			ObPartita.paese = paese;
			ObPartita.campionato = campionato;
			ObPartita.min = partite[cont+4];
			ObPartita.teamhome = teamhome;
			ObPartita.gi = gi;
			ObPartita.go = go;
			ObPartita.teamaway = teamaway;
			ObPartita.stato = stato;
			ObPartita.scommessa = scommessa;
			ObPartita.prob = prob;
			ObPartita.probtesto = probtesto;
			ObPartita.prevgi = prevgi;
			ObPartita.prevgo = prevgo;
			ObPartita.orden = 0;
			
			//alert("El ID del objeto es "+ ObPartita.codice);
			
			ObPartite[contOb] = ObPartita;
			contOb = contOb + 1;
			
			if (stato == 'Final'){
				min = " ";
			}
			
			var tr   = document.createElement("tr");
			
			if (k == 0){
				tr.style.backgroundColor = '#F2F2F2';
				k = 1;
			}
			else{
				tr.style.backgroundColor = '#F9F9F9';
				k = 0;
			}
			
			var td1  = document.createElement("td");
			var td2  = document.createElement("td");
			var td3  = document.createElement("td");
			var td4  = document.createElement("td");
			var td5  = document.createElement("td");
			var td6  = document.createElement("td");
			var td7  = document.createElement("td");
			var td8  = document.createElement("td");
			var td9  = document.createElement("td");
			var td10  = document.createElement("td");
			var td11  = document.createElement("td");
			
			var apartita  = document.createElement("a");
			apartita.setAttribute('href', 'BrowserDettaglioPartita.php?codicePartita='+codice);
			apartita.setAttribute('target', 'nuovo');
			var txt1 = document.createTextNode(data);
			apartita.appendChild(txt1);		
			
			var txt2 = document.createTextNode(paese);
			var txt3 = document.createTextNode(campionato);
			var txt4 = document.createTextNode(min);
			var txt5 = document.createTextNode(teamhome+" ("+prevgi+")");
			var txt6 = document.createTextNode(" "+gi+" ");
			var txt7 = document.createTextNode(" "+go+" ");
			var txt8 = document.createTextNode(teamaway+" ("+prevgo+")");
			var txt9 = document.createTextNode(stato);
			var txt10 = document.createTextNode(scommessa);
			var txt11 = document.createTextNode(probtesto);
			
			td4.style.fontStyle = "italic";
			td4.align = "center";
			td6.align = "center";
			td7.align = "center";
			td11.align = "center";
			
			td1.style.color = '#41627e';
			td1.style.fontSize = 10;
			td2.style.color = '#71677e';
			td2.style.fontSize = 10;
			td3.style.color = '#898989';
			td3.style.fontSize = 10;
			td4.style.color = '#71677e';
			td4.style.fontSize = 10;
			td5.style.color = '#71677e';
			td5.style.fontSize = 10;
			td6.style.color = '#71677e';
			td6.style.fontSize = 10;
			td7.style.color = '#71677e';
			td7.style.fontSize = 10;
			td8.style.color = '#71677e';
			td8.style.fontSize = 10;
			td9.style.color = '#71677e';
			td9.style.fontSize = 10;
			td10.style.color = '#71677e';
			td10.style.fontSize = 10;
			td11.style.color = '#71677e';
			td11.style.fontSize = 10;
			
			if (stato == 'Live'){
				td9.style.fontStyle = "italic";
				td9.style.color = "#8A0808";
				td4.style.color = "#8A0808";
			}
			
			if (gi > go){
				td5.style.color = 'green';
			}
			if (gi < go){
				td8.style.color = "green";
			}
			
			if (prob > 50 && prob <70){
				td11.style.color = 'blue';
			}
			if (prob >= 70 && prob <90){
				td11.style.color = 'orange';
			}
			if (prob >= 90){
				td11.style.color = 'red';
			}
			
			var testo = " ";
			if (stato == 'Final'){
				if (prob > 50){
					testo = getTestoScommessa(gi,go,scommessa);
				}
			}
			else{
				testo = getRiskScommessa(prevgi,prevgo);
			}
			var tdris  = document.createElement("td");
			tdris.style.fontStyle = "italic";
			if (testo == 'win'){
				tdris.style.color = 'green';
			}
			else{
				tdris.style.color = 'red';
			}
			var txtris = document.createTextNode(testo);
			tdris.appendChild(txtris);
				
			td1.appendChild(apartita);
			td2.appendChild(txt2);
			td3.appendChild(txt3);
			td4.appendChild(txt4);
			td5.appendChild(txt5);
			td6.appendChild(txt6);
			td7.appendChild(txt7);
			td8.appendChild(txt8);
			td9.appendChild(txt9);
			td10.appendChild(txt10);
			td11.appendChild(txt11);
			
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.appendChild(td5);
			tr.appendChild(td6);
			tr.appendChild(td7);						
			tr.appendChild(td8);
			tr.appendChild(td9);
			tr.appendChild(td10);
			tr.appendChild(tdris);
			tr.appendChild(td11);
				
			tabla.appendChild(tr);		
			
			cont = cont + 14;
		}
	}
	else{
		var tr  = document.createElement("tr");
		var td  = document.createElement("td");
		var txt = document.createTextNode("No Select Match in the last 2 day or Day Select");
		td.appendChild(txt);
		tr.appendChild(td);
		tabla.appendChild(tr);
	}
}
/*
cabecera della tabla
*/

function creaHeadTabla(){
	var HEAD = document.getElementById("headtabla");
	//alert(document.getElementById("headtabla"));
	var tr  = document.createElement("tr");
	tr.style.backgroundColor = '#153e7e';
	
	//style='background-color:#153e7e;font: bold 10px Arial;color:white;width:100%'
	
	in1  = document.createElement("input");
	in2  = document.createElement("input");
	in3  = document.createElement("input");
	in4  = document.createElement("input");
	in5  = document.createElement("input");
	in6  = document.createElement("input");
	in7  = document.createElement("input");
	in8  = document.createElement("input");
	in9  = document.createElement("input");
	in10  = document.createElement("input");
	in11  = document.createElement("input");
	in12  = document.createElement("input");
	
	var gko = navigator.userAgent.toLowerCase();
	
	if (gko.indexOf('gecko')!=-1){ //si soporta gecko, es Mozilla, Netscape, Safari, etc
		in1.setAttribute('onclick','OrderTable(0)');
		in2.setAttribute('onclick','OrderTable(1)');
		in3.setAttribute('onclick','OrderTable(2)');
		in4.setAttribute('onclick','OrderTable(3)');
		in5.setAttribute('onclick','OrderTable(4)');
		in6.setAttribute('onclick','OrderTable(5)');
		in7.setAttribute('onclick','OrderTable(6)');
		in8.setAttribute('onclick','OrderTable(7)');
		in9.setAttribute('onclick','OrderTable(8)');
		in10.setAttribute('onclick','OrderTable(9)');
		in11.setAttribute('onclick','OrderTable(10)');
		in12.setAttribute('onclick','OrderTable(11)');
	}
	else{ //es I Explroer
		in1['onclick']=new Function('OrderTable(0)');
		in2['onclick']=new Function('OrderTable(1)');
		in3['onclick']=new Function('OrderTable(2)');
		in4['onclick']=new Function('OrderTable(3)');
		in5['onclick']=new Function('OrderTable(4)');
		in6['onclick']=new Function('OrderTable(5)');
		in7['onclick']=new Function('OrderTable(6)');
		in8['onclick']=new Function('OrderTable(7)');
		in9['onclick']=new Function('OrderTable(8)');
		in10['onclick']=new Function('OrderTable(9)');
		in11['onclick']=new Function('OrderTable(10)');
		in12['onclick']=new Function('OrderTable(11)');
	}
	
	th1  = document.createElement("th");
	in1.setAttribute('type', 'button');
	in1.setAttribute('value', 'Date');
	in1.style.backgroundColor = '#153e7e';
	in1.style.color = 'white';
	//in1.style.fontStyle = 'Arial';
	in1.style.fontSize = 10;
	in1.style.width = '100%';
	th1.appendChild(in1);	
	
	th2  = document.createElement("th");
	in2.setAttribute('type', 'button');
	in2.setAttribute('value', 'State');
	//in2.setAttribute('onclick', 'OrderTable(1)');
	in2.style.backgroundColor = '#153e7e';
	in2.style.color = 'white';
	//in2.style.fontStyle = "arial";
	in2.style.fontSize = 10;
	in2.style.width = '100%';
	th2.appendChild(in2);	
	
	th3  = document.createElement("th");
	in3.setAttribute('type', 'button');
	in3.setAttribute('value', 'League');
	//in3.setAttribute('onclick', 'OrderTable(2)');
	in3.style.backgroundColor = '#153e7e';
	in3.style.color = 'white';
	//in3.style.fontStyle = "arial";
	in3.style.fontSize = 10;
	in3.style.width =  '100%';
	th3.appendChild(in3);	
	
	th4  = document.createElement("th");
	in4.setAttribute('type', 'button');
	in4.setAttribute('value', 'Time');
	//in4.setAttribute('onclick', 'OrderTable(3)');
	in4.style.backgroundColor = '#153e7e';
	in4.style.color = 'white';
	//in4.style.fontStyle = "arial";
	in4.style.fontSize = 10;
	in4.style.width =  '100%';
	th4.appendChild(in4);	
	
	th5 = document.createElement("th");
	in5.setAttribute('type', 'button');
	in5.setAttribute('value', 'T. Home');
	//in5.setAttribute('onclick', 'OrderTable(4)');
	in5.style.backgroundColor = '#153e7e';
	in5.style.color = 'white';
	//in5.style.fontStyle = "arial";
	in5.style.fontSize = 10;
	in5.style.width =  '100%';
	th5.appendChild(in5);	
	
	th6  = document.createElement("th");
	in6.setAttribute('type', 'button');
	in6.setAttribute('value', ' - ');
	//in6.setAttribute('onclick', 'OrderTable(5)');
	in6.style.backgroundColor = '#153e7e';
	in6.style.color = 'white';
	//in6.style.fontStyle = "arial";
	in6.style.fontSize = 10;
	in6.style.width =  '100%';
	th6.appendChild(in6);	
	
	th7  = document.createElement("th");
	in7.setAttribute('type', 'button');
	in7.setAttribute('value', ' - ');
	//in7.setAttribute('onclick', 'OrderTable(6)');
	in7.style.backgroundColor = '#153e7e';
	in7.style.color = 'white';
	//in7.style.fontStyle = "arial";
	in7.style.fontSize = 10;
	in7.style.width =  '100%';
	th7.appendChild(in7);	
	
	th8  = document.createElement("th");
	in8.setAttribute('type', 'button');
	in8.setAttribute('value', 'T. Away');
	//in8.setAttribute('onclick', 'OrderTable(7)');
	in8.style.backgroundColor = '#153e7e';
	in8.style.color = 'white';
	//in8.style.fontStyle = "arial";
	in8.style.fontSize = 10;
	in8.style.width = '100%';
	th8.appendChild(in8);	
	
	th9  = document.createElement("th");
	in9.setAttribute('type', 'button');
	in9.setAttribute('value', 'Status');
	//in9.setAttribute('onclick', 'OrderTable(8)');
	in9.style.backgroundColor = '#153e7e';
	in9.style.color = 'white';
	//in9.style.fontStyle = "arial";
	in9.style.fontSize = 10;
	in9.style.width = '100%';
	th9.appendChild(in9);	
	
	th10  = document.createElement("th");
	in10.setAttribute('type', 'button');
	in10.setAttribute('value', 'Bet');
	//in10.setAttribute('onclick', 'OrderTable(9)');
	in10.style.backgroundColor = '#153e7e';
	in10.style.color = 'white';
	//in10.style.fontStyle = "arial";
	in10.style.fontSize = 10;
	in10.style.width = '100%';
	th10.appendChild(in10);	
	
	th11  = document.createElement("th");
	in11.setAttribute('type', 'button');
	in11.setAttribute('value', 'Tip');
	//in11.setAttribute('onclick', 'OrderTable(10)');
	in11.style.backgroundColor = '#153e7e';
	in11.style.color = 'white';
	//in11.style.fontStyle = "arial";
	in11.style.fontSize = 10;
	in11.style.width =  '100%';
	th11.appendChild(in11);	
	
	th12  = document.createElement("th");
	in12.setAttribute('type', 'button');
	in12.setAttribute('value', '%');
	//in12.setAttribute('onclick', 'OrderTable(11)');
	in12.style.backgroundColor = '#153e7e';
	in12.style.color = 'white';
	//in12.style.fontStyle = "arial";
	in12.style.fontSize = 10;
	in12.style.width =  '100%';
	th12.appendChild(in12);
	
	tr.appendChild(th1);
	tr.appendChild(th2);
	tr.appendChild(th3);
	tr.appendChild(th4);
	tr.appendChild(th5);
	tr.appendChild(th6);
	tr.appendChild(th7);
	tr.appendChild(th8);
	tr.appendChild(th9);
	tr.appendChild(th10);
	tr.appendChild(th11);
	tr.appendChild(th12);
	
	HEAD.appendChild(tr);	
}

/*
TABLA STATICHE PARTITE CALCIO
*/

function muestraStat(Stat){
	//alert(Stat);
	/*
	Linea costruita : $campionato|$squadra|$tipostat|$linea|$partite|$ValoreStat|$PesoStat|$PesoRisk|$ScommesseCorrette|$ScommesseSbagliate
	Cardinalita = 10
	*/
	//alert(Stat);
	
	var statistiche = Stat.split("|");
	
	var tabla= document.getElementById("tablastat");
	
	var filas = tabla.childNodes.length;
	
	var cont = 0;
	var campionato = "";var squadra = "";
	var tipostat = 0;var linea = 0;var partite = 0;var ValoreStat = 0;var PesoStat = 0;
	var PesoRisk = 0;var ScommesseCorrette = 0;var ScommesseSbagliate = 0;
	var k = 0;
	
	for(var a=0; a<filas; a++)
	{
		var nodo = tabla.childNodes[0];
		tabla.removeChild(nodo);
	}
	
	contObstat = 0;
	
	if (statistiche[0] != -1){
		while (cont < statistiche.length-1){
	
			campionato = statistiche[cont];
			squadra = statistiche[cont+1];
			tipostat = statistiche[cont+2];
			linea = statistiche[cont+3];
			partite = statistiche[cont+4];
			ValoreStat = statistiche[cont+5];
			PesoStat = statistiche[cont+6];
			PesoRisk = statistiche[cont+7];
			ScommesseCorrette = statistiche[cont+8];
			ScommesseSbagliate = statistiche[cont+9];
			
			//creo l' oggetto partita
			var ObStatistica = new Object();
			
			ObStatistica.campionato = campionato;
			ObStatistica.squadra = squadra;
			ObStatistica.tipostat = tipostat;
			ObStatistica.linea = linea;
			ObStatistica.partite = partite;
			ObStatistica.ValoreStat = ValoreStat;
			ObStatistica.PesoStat = PesoStat;
			ObStatistica.PesoRisk = PesoRisk;
			ObStatistica.ScommesseCorrette = ScommesseCorrette;
			ObStatistica.ScommesseSbagliate = ScommesseSbagliate;
			ObStatistica.orden = 0;

			//alert("El ID del objeto es "+ ObPartita.codice);
			
			ObStatistiche[contObstat] = ObStatistica;
			contObstat = contObstat + 1;
			
			var tr   = document.createElement("tr");
			
			if (k == 0){
				tr.style.backgroundColor = '#F2F2F2';
				k = 1;
			}
			else{
				tr.style.backgroundColor = '#F9F9F9';
				k = 0;
			}
			
			var t = getTypeScommessa(tipostat);
			
			var td1  = document.createElement("td");
			td1.setAttribute('title', campionato);
			var td2  = document.createElement("td");
			td2.setAttribute('title', t);
			var td3  = document.createElement("td");
			td3.setAttribute('title', 'Line Goal / Handicap Goal ');
			var td4  = document.createElement("td");
			td4.setAttribute('title', 'Matches/Numero Partite/Partidos');
			var td5  = document.createElement("td");
			td5.setAttribute('title', 'Frequency / Numero Statistica / Estadisticas');
			var td6  = document.createElement("td");
			td6.setAttribute('title', '% Statistics');
			var td7  = document.createElement("td");
			td7.setAttribute('title', '% Risk / Riesgo');
			var td8  = document.createElement("td");
			td8.setAttribute('title', 'Win / Ganodoras / Vincente Bet / Apuestas / Scommesse');
			var td9  = document.createElement("td");
			td9.setAttribute('title', 'Lose / Perdidas / Persa Bet / Apuestas / Scommesse');
			
			var aresumen  = document.createElement("a");
			aresumen.setAttribute('href', 'ResumeSquadra.php?codicesquadra=-1&squadra='+squadra+'&HA=H&hp=0&linea=2.5');
			aresumen.setAttribute('target', 'nuovo');
			var txt1 = document.createTextNode(squadra);
			aresumen.appendChild(txt1);
			
			//var txt1 = document.createTextNode(squadra);
			var txt2 = document.createTextNode(tipostat);
			var txt3 = document.createTextNode(linea);
			var txt4 = document.createTextNode(partite);
			var txt5 = document.createTextNode(ValoreStat);
			var txt6 = document.createTextNode(PesoStat+'%');
			var txt7 = document.createTextNode(PesoRisk+'%');
			var txt8 = document.createTextNode(ScommesseCorrette);
			var txt9 = document.createTextNode(ScommesseSbagliate);

			/*td4.style.fontStyle = "italic";
			td4.align = "center";
			td6.align = "right";
			td7.align = "left";
			td11.align = "center";*/
			
			td2.align = "left";
			td3.align = "center";
			td4.align = "center";
			td5.align = "center";
			td6.align = "center";
			td7.align = "center";
			td8.align = "center";
			td9.align = "center";						
			
			td1.style.color = '#41627e';
			td1.style.fontSize = 10;
			td2.style.color = '#71677e';
			td2.style.fontSize = 10;
			td3.style.color = '#898989';
			td3.style.fontSize = 10;
			td4.style.color = '#71677e';
			td4.style.fontSize = 10;
			td5.style.color = '#71677e';
			td5.style.fontSize = 10;
			td6.style.color = '#71677e';
			td6.style.fontSize = 10;
			td7.style.color = '#71677e';
			td7.style.fontSize = 10;
			td8.style.color = '#71677e';
			td8.style.fontSize = 10;
			td9.style.color = '#71677e';
			td9.style.fontSize = 10;
				
			td1.appendChild(aresumen);
			td2.appendChild(txt2);
			td3.appendChild(txt3);
			td4.appendChild(txt4);
			td5.appendChild(txt5);
			td6.appendChild(txt6);
			td7.appendChild(txt7);
			td8.appendChild(txt8);
			td9.appendChild(txt9);
			
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.appendChild(td5);
			tr.appendChild(td6);
			tr.appendChild(td7);						
			tr.appendChild(td8);
			tr.appendChild(td9);
				
			tabla.appendChild(tr);		
			
			cont = cont + 10;
		}
	}
	else{
		var tr  = document.createElement("tr");
		var td  = document.createElement("td");
		var txt = document.createTextNode("No Select Stat in the last 2 day or Day Select");
		td.appendChild(txt);
		tr.appendChild(td);
		tabla.appendChild(tr);
	}
}
function creaHeadTablaStat(){
	//alert("...");
	var HEADSTAT = document.getElementById("headstat");
	//alert(document.getElementById("headstat"));
	var tr  = document.createElement("tr");
	tr.style.backgroundColor = '#153e7e';
	
	//style='background-color:#153e7e;font: bold 10px Arial;color:white;width:100%'
	
	instat1  = document.createElement("input");
	instat2  = document.createElement("input");
	instat3  = document.createElement("input");
	instat4  = document.createElement("input");
	instat5  = document.createElement("input");
	instat6  = document.createElement("input");
	instat7  = document.createElement("input");
	instat8  = document.createElement("input");
	instat9  = document.createElement("input");
	
	var gko = navigator.userAgent.toLowerCase();
	
	if (gko.indexOf('gecko')!=-1){ //si soporta gecko, es Mozilla, Netscape, Safari, etc
		instat1.setAttribute('onclick','OrderTableStat(0)');
		instat2.setAttribute('onclick','OrderTableStat(1)');
		instat3.setAttribute('onclick','OrderTableStat(2)');
		instat4.setAttribute('onclick','OrderTableStat(3)');
		instat5.setAttribute('onclick','OrderTableStat(4)');
		instat6.setAttribute('onclick','OrderTableStat(5)');
		instat7.setAttribute('onclick','OrderTableStat(6)');
		instat8.setAttribute('onclick','OrderTableStat(7)');
		instat9.setAttribute('onclick','OrderTableStat(8)');
	}
	else{ //es I Explroer
		instat1['onclick']=new Function('OrderTableStat(0)');
		instat2['onclick']=new Function('OrderTableStat(1)');
		instat3['onclick']=new Function('OrderTableStat(2)');
		instat4['onclick']=new Function('OrderTableStat(3)');
		instat5['onclick']=new Function('OrderTableStat(4)');
		instat6['onclick']=new Function('OrderTableStat(5)');
		instat7['onclick']=new Function('OrderTableStat(6)');
		instat8['onclick']=new Function('OrderTableStat(7)');
		instat9['onclick']=new Function('OrderTableStat(8)');
	}
	
	thstat1  = document.createElement("th");
	instat1.setAttribute('type', 'button');
	instat1.setAttribute('value', 'Team');
	instat1.setAttribute('title', 'Team');
	instat1.style.backgroundColor = '#153e7e';
	instat1.style.color = 'white';
	//instat1.style.fontStyle = 'Arial';
	instat1.style.fontSize = 10;
	instat1.style.width = '100%';
	thstat1.appendChild(instat1);	
	
	thstat2  = document.createElement("th");
	instat2.setAttribute('type', 'button');
	instat2.setAttribute('value', 'Type');
	//instat2.setAttribute('onclick', 'OrderTable(1)');
	instat2.style.backgroundColor = '#153e7e';
	instat2.style.color = 'white';
	//instat2.style.fontStyle = "arial";
	instat2.style.fontSize = 10;
	instat2.style.width = '100%';
	thstat2.appendChild(instat2);	
	
	thstat3  = document.createElement("th");
	instat3.setAttribute('type', 'button');
	instat3.setAttribute('value', 'Line');
	//instat3.setAttribute('onclick', 'OrderTable(2)');
	instat3.style.backgroundColor = '#153e7e';
	instat3.style.color = 'white';
	//instat3.style.fontStyle = "arial";
	instat3.style.fontSize = 10;
	instat3.style.width =  '100%';
	thstat3.appendChild(instat3);	
	
	thstat4  = document.createElement("th");
	instat4.setAttribute('type', 'button');
	instat4.setAttribute('value', 'M.');
	//instat4.setAttribute('onclick', 'OrderTable(3)');
	instat4.style.backgroundColor = '#153e7e';
	instat4.style.color = 'white';
	//instat4.style.fontStyle = "arial";
	instat4.style.fontSize = 10;
	instat4.style.width =  '100%';
	thstat4.appendChild(instat4);	
	
	thstat5 = document.createElement("th");
	instat5.setAttribute('type', 'button');
	instat5.setAttribute('value', 'Stat');
	//instat5.setAttribute('onclick', 'OrderTable(4)');
	instat5.style.backgroundColor = '#153e7e';
	instat5.style.color = 'white';
	//instat5.style.fontStyle = "arial";
	instat5.style.fontSize = 10;
	instat5.style.width =  '100%';
	thstat5.appendChild(instat5);	
	
	thstat6  = document.createElement("th");
	instat6.setAttribute('type', 'button');
	instat6.setAttribute('value', 'Tip');
	//instat6.setAttribute('onclick', 'OrderTable(5)');
	instat6.style.backgroundColor = '#153e7e';
	instat6.style.color = 'white';
	//instat6.style.fontStyle = "arial";
	instat6.style.fontSize = 10;
	instat6.style.width =  '100%';
	thstat6.appendChild(instat6);	
	
	thstat7  = document.createElement("th");
	instat7.setAttribute('type', 'button');
	instat7.setAttribute('value', 'Risk');
	//instat7.setAttribute('onclick', 'OrderTable(6)');
	instat7.style.backgroundColor = '#153e7e';
	instat7.style.color = 'white';
	//instat7.style.fontStyle = "arial";
	instat7.style.fontSize = 10;
	instat7.style.width =  '100%';
	thstat7.appendChild(instat7);	
	
	thstat8  = document.createElement("th");
	instat8.setAttribute('type', 'button');
	instat8.setAttribute('value', 'Win');
	//instat8.setAttribute('onclick', 'OrderTable(7)');
	instat8.style.backgroundColor = '#153e7e';
	instat8.style.color = 'white';
	//instat8.style.fontStyle = "arial";
	instat8.style.fontSize = 10;
	instat8.style.width = '100%';
	thstat8.appendChild(instat8);	
	
	thstat9  = document.createElement("th");
	instat9.setAttribute('type', 'button');
	instat9.setAttribute('value', 'Lose');
	//instat9.setAttribute('onclick', 'OrderTable(8)');
	instat9.style.backgroundColor = '#153e7e';
	instat9.style.color = 'white';
	//instat9.style.fontStyle = "arial";
	instat9.style.fontSize = 10;
	instat9.style.width = '100%';
	thstat9.appendChild(instat9);	
	
	tr.appendChild(thstat1);
	tr.appendChild(thstat2);
	tr.appendChild(thstat3);
	tr.appendChild(thstat4);
	tr.appendChild(thstat5);
	tr.appendChild(thstat6);
	tr.appendChild(thstat7);
	tr.appendChild(thstat8);
	tr.appendChild(thstat9);
	
	HEADSTAT.appendChild(tr);	
}

