// JavaScript Document

function f_lavagna()	{	
//[HomePage].Lavagna - legge: 'lavagne/lavagna.xml
var Mostra = ""; var StringaXml = DSO_lavagna_xml.XMLDocument;
var i;

Mostra += "<div class='Article_black' id='red'>"
	Mostra += "<H4>&nbsp;</H4>"
		Mostra += "<div class='ArticleBody_black'>"
			for (i=0; i<StringaXml.documentElement.childNodes.length; i++) {
				
				torneo = StringaXml.documentElement.childNodes(i).childNodes(4).text;
			 //Mostra += "<table class='ruler' id='lavagna_quote' border='0' cellpadding='2' cellspacing='0'>"
		if (torneo != "") {
			Mostra += "<p>"
			//Mostra += "<span align='left' id='l_torneo'>"+ StringaXml.documentElement.childNodes(i).childNodes(4).text +"</span>"
			Mostra += "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td id='l_torneo'>" + torneo + "</td></tr></table>"
			Mostra += "</p>"
			}//chiude if
		else {
		Mostra += "<p align='center'>"
					 Mostra += "<table id='lavagna_quote' class='ruler' width='95%' border='0' cellpadding='0' cellspacing='0'>"
						 Mostra += "<tr>"
							Mostra += "<td id='l_quote_left'>" + StringaXml.documentElement.childNodes(i).childNodes(1).text + "</td>"
							Mostra += "<td id='play1'>" + StringaXml.documentElement.childNodes(i).childNodes(0).text + "</td>"
							Mostra += "<td width='1%'>|</td>"
							Mostra += "<td id='play2'>" + StringaXml.documentElement.childNodes(i).childNodes(2).text + "</td>"
							Mostra += "<td id='l_quote_right'>" + StringaXml.documentElement.childNodes(i).childNodes(3).text + "</td>"
						 Mostra += "</tr>"
					 Mostra += "</table>"
		Mostra += "</p>"
		}

			}
		Mostra += "</div>"
		Mostra += "<div class='ArticleFooter_black'>"
			Mostra += "<p>&nbsp;</p>"
		Mostra += "</div>"
Mostra += "</div>"

lavagna_giornaliera.innerHTML = Mostra;
}

//----------------------------------------
function f_bill_summ_current_year()	{
	
//Estraggo l'anno a 4 cifre
var my_date = new Date();

var anno_incorso=my_date.getFullYear();

//flag_onemonths=1;Gestisce quando si ha un solo mese, in pratica a Gennaio...basta metterlo == 1
mesi_vet=["gen08", "feb08", "mar08"/*, "apr07", "mag07", "giu07"*/];//ultimo elemento = mese in corso
nomi_vet=["Gennaio", "Febbraio", "Marzo"/*, "Aprile", "Maggio", "Giugno"*/];
nomi_bill_summ=["GENNAIO_08", "FEBBRAIO_08", "MARZO_08"/*, "APRILE_07", "MAGGIO_07", "GIUGNO_07"*/];
//------------------------------------------------------------
var Mostra = "";
//Gestisce quando si ha un solo mese, in pratica a Gennaio.
if (mesi_vet.length == 1)	
			{
			solo_gennaio="../xml/bilancio/betting_tennis_mesecorrente.xml";
			calcola_utile_mesecorrente(solo_gennaio);
			//ritornano questi valori da calcola_utile_mesecorrente
			utile_solo_gennaio=utile_totale;
			stakes_solo_gennaio=stakes_totale;
			tot_win_solo_gennaio=count_win;
			tot_tot_solo_gennaio=count_tot;
			tot_qmedia_solo_gennaio=somma_quote;
			//Calcoli
			tot_utile=Math.round((utile_solo_gennaio)*100)/100;
			tot_stakes=Math.round((stakes_solo_gennaio)*100)/100;
			tot_roi=Math.round((tot_utile/tot_stakes)*10000)/100;
			tot_win=tot_win_solo_gennaio;
			tot_tot=tot_tot_solo_gennaio;
			tot_perc=Math.round((tot_win/tot_tot)*10000)/100;
			tot_qmedia=Math.round(((tot_qmedia_solo_gennaio)/(tot_tot))*100)/100;
			}
			
			
			else {

//Imposta il file da cui prende il bilancio  MESI PRECEDENTI A QUELLO CORRENTE
//----------------------------------------------------------------------------

//Imposta il file da cui prende il bilancio
file_bilancio_annuale="../xml/bilancio/bill_summ/bill_summ_" + anno_incorso + ".xml";
//Richiama la funzione che fà i calcoli relativi per estrarre utile e stakes
calcola_utile(file_bilancio_annuale);
//appoggia i valori in altre variabili per richiamare poi la stessa funzione
utile_mesi_prec=utile_totale;
stakes_mesi_prec=stakes_totale;
win_mesi_prec=win_totale;
tot_mesi_prec=tot_totale;
perc_mesi_prec=perc_totale;
qmedia_mesi_prec=qmedia_totale;
nmesi = count_mesi;//mesi totali che stanno sull'xml
nmesi_nulli = count_mesi_nulli;//mesi nulli....dove non si è giocato
j=0;

//Ripete gli stessi passi di prima MESE CORRENTE
//----------------------------------------------

//betting_corr_xml="../xml/bilancio/betting_" + mesi_vet[k] + ".xml"
betting_corr_xml="../xml/bilancio/betting_tennis_mesecorrente.xml"

//Richiama la funzione che fà i calcoli relativi per estrarre utile e stakes
calcola_utile_mesecorrente(betting_corr_xml);

//appoggia i valori in altre variabili per richiamare poi la stessa funzione
utile_mese_corr=utile_totale;
stakes_mese_corr=stakes_totale;
win_mese_corr=count_win;
tot_mese_corr=count_tot;
q_totale=somma_quote;
qmedia_mese_corr = (q_totale/tot_mese_corr);

//check = (qmedia_mese_corr);
//-------------------------------------
//Variabili visualizzate
tot_utile=Math.round((utile_mesi_prec+utile_mese_corr)*100)/100;
tot_stakes=Math.round((stakes_mesi_prec+stakes_mese_corr)*100)/100;
tot_roi=Math.round((tot_utile/tot_stakes)*10000)/100;
tot_win=(win_mesi_prec+win_mese_corr);
tot_tot=(tot_mesi_prec+tot_mese_corr);
tot_perc=Math.round((tot_win/tot_tot)*10000)/100;
//tot_qmedia=((qmedia_mesi_prec+qmedia_mese_corr)/(nmesi + 1));
tot_qmedia=Math.round(((qmedia_mesi_prec+qmedia_mese_corr)/(nmesi - nmesi_nulli))*100)/100;
}//-----------------------fine dell'else
//------------------------

Mostra += "<div class='Article' id='red'>"
//Mostra += "<H6><a href='html/bilancio.htm'>TENNIS</a> da Gennaio 2006</H6>"
Mostra += "<H4><font color='#000000'>Bilancio " + anno_incorso + "</font></H4>"
Mostra += "<div class='ArticleBody'>"
Mostra += "<p>"
	if (tot_utile <= 0)	
			{
			Mostra += "<I>Utile: </I><font color='red'><b>" + tot_utile + "</b></font> unità"
			}
			else {
			Mostra += "<I>Utile: </I><font color='green'><b>" + tot_utile + "</b></font> unità"
			}
Mostra += "</p>"
Mostra += "<p>"
	if (tot_roi <= 0)	
			{
			Mostra += "<I>ROI: </I><font color='red'><b>" + tot_roi + "%" + "</b></font>"
			}
			else {
			Mostra += "<I>ROI: </I><font color='green'><b>" + tot_roi + "%" + "</b></font>"
			}
Mostra += "</p>"
Mostra += "<p>"
	Mostra += "<I>Stakes: </I><b>" + tot_stakes + "</b> unità"
Mostra += "</p>"
Mostra += "<p>"
	Mostra += "<I>Win: </I><b>" + tot_win + "</b>"
Mostra += "</p>"
Mostra += "<p>"
	Mostra += "<I>Tot: </I><b>" + tot_tot + "</b>"
Mostra += "</p>"
Mostra += "<p>"
	Mostra += "<I>%: </I><b>" + tot_perc + " %</b>"
Mostra += "</p>"
Mostra += "<p>"
	Mostra += "<I>Qmedia: </I><b>" + tot_qmedia + "</b>"
Mostra += "</p>"
/*
Mostra += "<p>"
	Mostra += "<I><b>controllo var: </b></I><b>" + check + "</b>"
Mostra += "</p>"
*/
Mostra += "</div>"
Mostra += "<div class='ArticleFooter'>"
Mostra += "<p align='center' class='sommario_footer'>"
	//Mostra += "<a href='/html/bilancio.php'><I>Bilancio</I></a><font color='#999999'>&bull;</font><a href='html/bilancio_2005.htm'><I>Archivio</I></a><font color='#999999'>&bull;</font><a href='#'><I>Tornei</I></a>"
	Mostra += "<a href='/html/bilancio.php'><I>Bilancio</I></a><font color='#999999'>&bull;</font><a href='/html/bilancio_tornei.php'><I>Tornei</I></a>"
Mostra += "</p>"
Mostra += "</div>"
Mostra += "</div>"

// scrive nel td con id=bill_summ_2005
bilancio.innerHTML = Mostra;
}
//---------------------------------------------
function calcola_utile(file)	{
	var utile_txt = " ";var stakes_txt = " ";var win_txt = " ";
	var utile_num, stakes_num, win_num;
	utile_totale=0; stakes_totale=0;win_totale=0;tot_totale=0;perc_totale=0;qmedia_totale=0;count_mesi=0;count_mesi_nulli=0;

	//somma_utile_anno=0; somma_stakes_anno=0; roi=0;
	//var somma_utile_mese=0;var somma_stakes_mese=0;
	
	objXml = new ActiveXObject("Microsoft.XMLDOM");
	objXml.async = false;
	objXml.load(file);
	// Leggo tutti i nodi del file col metodo getElementsByTagName()
	var utile = objXml.getElementsByTagName("U_UTILE");//questo è un array.
	var stakes = objXml.getElementsByTagName("U_GIOCATE");//questo è un array.
	//var rofinv = objXml.getElementsByTagName("ROI");//questo è un array.
	var win = objXml.getElementsByTagName("WIN");//questo è un array.
	var tot = objXml.getElementsByTagName("TOT");//questo è un array.
	var perc = objXml.getElementsByTagName("PERC_REALIZZAZIONE");//questo è un array.
	var qmedia = objXml.getElementsByTagName("Q_MEDIA");//questo è un array.
	
	for (i=0; i<win.length; i++)
			{
			utile_txt = utile[i].text;
			utile_num = parseFloat(utile_txt);//trasforma la stringa in valore numerico
			utile_totale = utile_totale + utile_num;
			//----------------
			stakes_txt = stakes[i].text;
			stakes_num = parseFloat(stakes_txt);//trasforma la stringa in valore numerico
			stakes_totale = stakes_totale + stakes_num;
			//----------------
			win_txt = win[i].text;
			win_num = parseFloat(win_txt);
			win_totale = win_totale + win_num;
			//----------------
			tot_txt = tot[i].text;
			tot_num = parseFloat(tot_txt);
			tot_totale = tot_totale + tot_num;
			//----------------
			perc_txt = perc[i].text;
			perc_num = parseFloat(perc_txt);
			perc_totale = perc_totale + perc_num;
			//----------------
			qmedia_txt = qmedia[i].text;
			qmedia_num = parseFloat(qmedia_txt);
			qmedia_totale = qmedia_totale + qmedia_num;
			count_mesi = count_mesi + 1;
			//------------- Conta i mesi dove non si è scommesso
					if (utile_txt==0){
						count_mesi_nulli = count_mesi_nulli + 1;
						}
			}//chiude il for
}
//-------------------------------------------------------
//---------------------------------------------
function calcola_utile_mesecorrente(file)	{
	var utile_txt = " ";var stakes_txt = " ";var esito_txt = " ";var quota_txt = " ";
	var utile_num, stakes_num, esito_num, quota_num;
	utile_totale=0; stakes_totale=0;count_win=0;count_tot=0;somma_quote=0;
	
	objXml = new ActiveXObject("Microsoft.XMLDOM");
	objXml.async = false;
	objXml.load(file);
	// Leggo tutti i nodi del file col metodo getElementsByTagName()
	var utile = objXml.getElementsByTagName("U_UTILE");//questo è un array.
	var stakes = objXml.getElementsByTagName("U_GIOCATE");//questo è un array.
	var esito = objXml.getElementsByTagName("ESITO");//questo è un array.
	var odd = objXml.getElementsByTagName("QUOTA");//questo è un array.
	
	for (i=0; i<utile.length; i++)
			{
			utile_txt = utile[i].text;
			utile_num = parseFloat(utile_txt);//trasforma la stringa in valore numerico
			utile_totale = utile_totale + utile_num;
			//----------------
			stakes_txt = stakes[i].text;
			stakes_num = parseFloat(stakes_txt);//trasforma la stringa in valore numerico
			stakes_totale = stakes_totale + stakes_num;
			//---------------- calcola i colpi OK e quelli KO per sapere i WIN
			esito_txt = esito[i].text;
			esito_num = parseFloat(esito_txt);//trasforma la stringa in valore numerico
			if (esito_num == 1){
						count_win = count_win + 1;
						}
			//---------------- calcola i colpi totali per sapere il TOT
			if (esito_num == 1 | esito_num == 0){
						count_tot = count_tot + 1;
						}
			//---------------- calcola Q_MEDIA
			quota_txt = odd[i].text;
			quota_num = parseFloat(quota_txt);//trasforma la stringa in valore numerico
			if (esito_num == 1 | esito_num == 0 ){//il void NON fà media sulle quote
						somma_quote = somma_quote + quota_num;
						}
				}
}
//-------------------------------------------------------
function match_preview() {
//[HomePage].BetInCorso - legge: 'bilancio/betting_xxxyy.xml'
oggi = new Date()
data_yyyy = oggi.getDate() + "_" + oggi.getMonth() + "_" + oggi.getYear(); 
miadata = "MATCH_" + data_yyyy;
var Mostra = ""; var StringaXml = DSO_bet_preview.XMLDocument;
var i;
var player1,quota,player2,unita;
var flag;
var count=0;

Mostra += "<table id='contenitore_preview' width='100%' border='0'>"
Mostra += "<tr><td>&nbsp;</td><td width='45px'>Quota</td><td width='45px'>Unità</td></tr>"

for (i=0; i<StringaXml.documentElement.childNodes.length; i++) {
		testo = StringaXml.documentElement.childNodes(i).nodeName;
		player1=StringaXml.documentElement.childNodes(i).childNodes(2).text;
		quota=StringaXml.documentElement.childNodes(i).childNodes(5).text;
		player2=StringaXml.documentElement.childNodes(i).childNodes(3).text;
		unita=StringaXml.documentElement.childNodes(i).childNodes(11).text;
		//Gestione giocata multipla
		gioco = StringaXml.documentElement.childNodes(i).childNodes(4).text;
		multipla = StringaXml.documentElement.childNodes(i).childNodes(6).text;
		if (testo == miadata) {
						if (gioco=="singola"){
						
						Mostra += "<tr><td>"
						//Mostra += "<dd><p><b>" + player1 + "</b>@<b>" + quota + "</b> su " + player2 + "</p></dd>" ........PREC
						//Mostra += "<p><b>" + player1 + "</b>@<b>" + quota + "</b> su " + player2 + "</p>" .....OK
						Mostra += "<p><b>" + player1 + "</b></p>"
						Mostra += "</td>"
						Mostra += "<td>" + quota + "</td>"
						Mostra += "<td>" + unita + "</td>"
						Mostra += "</tr>"
						}
						else if (gioco=="multipla"){
							Mostra += "<tr><td>"
							Mostra += "<p><b>" + multipla + ", </b> <b>" + unita + "</b>u" + "  <b>" + quota + "</b></p>"
							Mostra += "</td></tr>"
						//Mostra += "<dd><b>" + multipla + ", </b> <b>" + unita + "</b>u" + "  <b>" + quota + "</b></dd>" ........PREC
							}//chiude if interno per giocata multipla
				count=count+1;
						}//chiude if
			else {flag="NOBET"}
		}
if (flag == "NOBET" && count==0) {
	//Mostra += "<dd><img src='images/nobet.gif'></dd>"
	//Mostra += "<dd><p>&nbsp;</p><p>NO BET</p></dd>"	
	Mostra += "<tr><td>"
	Mostra += "<p><b>NO BET</b></p>"
	Mostra += "</td>"
	Mostra += "<td>&nbsp;</td>"
	Mostra += "<td>&nbsp;</td>"
	Mostra += "</tr>"
}
Mostra += "</table>"
Mostra += "<table id='contenitore_preview_bottom' width='100%' border='0'>"
//Mostra += "<dd id='link_leggi'><a href='html/lavagna.htm'>leggi...</a></dd>"
Mostra += "<tr><td>"
Mostra += "<p id='link_leggi'><a href='/html/lavagna.php'>dettagli...</a></p>"
Mostra += "</td></tr>"
//Mostra += "<dd id='link_leggi'><a href='html/lavagna.php'>dettagli...</a></dd>"
//Mostra += "</dl>"

//Mostra += "<tr><td><table width='100%' border='0'><tr><td >&nbsp;</td><td width='80%'>&nbsp;</td><td id='preview_right_corner'>&nbsp;</td></tr></table>"
//Mostra += "</td></tr></table>"
Mostra += "</table>"
bet_preview.innerHTML = Mostra;
}
//------------------------------
function esposizione_tot() {
//Chiama f_esposizione()
var Mostra = ""; 

file_betting_tennis="xml/bilancio/betting_tennis_mesecorrente.xml";
file_betting_calcio="xml/bilancio/calcio_betting_unico.xml";

exp_tennis=f_esposizione(file_betting_tennis);//Esposizione tennis--------
exp_calcio=f_esposizione(file_betting_calcio);//Esposizione calcio--------

exp_totale=exp_tennis+exp_calcio;
//Mostra += "<p align='right'> Esposizione = <b>" + exp_totale + "</b> unità</p>" 
Mostra += "<p> Esposizione = <b>" + exp_totale + "</b> u</p>" 
esp.innerHTML = Mostra;
}
//------------------------------
//-----------------------------------------------------------------------------------
function f_esposizione(file) {
	oggi = new Date()
	data_yyyy = oggi.getDate() + "_" + oggi.getMonth() + "_" + oggi.getYear(); 
	miadata = "MATCH_" + data_yyyy;
//Load file xml	
	objXml = new ActiveXObject("Microsoft.XMLDOM");
	objXml.async = false;
	objXml.load(file);
//crea un array di tutti i nodi con miadata
	var daily_betting = objXml.getElementsByTagName(miadata);//prende solo i nodi miadata....active bet
	
	var esposizione_totale = 0; 
	var counter = 0;
	var somma_quote = 0;
	
	for (i=0;i<daily_betting.length;i++)
		{
		var quota = parseFloat(daily_betting[i].childNodes[5].text);//[5]
		var u_giocate = parseFloat(daily_betting[i].childNodes[11].text);//[11]

		esposizione_totale = Math.round((esposizione_totale + u_giocate)*10)/10;
		somma_quote = Math.round((somma_quote + quota)*100)/100;//arrotonda a 2 cifre
		counter = counter + 1;
	}//fine del for
	if	(counter==0) {counter=1;}
	
	var quota_media=Math.round((somma_quote/counter)*100)/100;//arrotonda a due cifre decimali
	
	//scrivi.document.writeln("- <b>Bet in corso</b>: esposizione= <b>" + esposizione_totale + "</b>u. , q.media= " + quota_media + "<br \/>");
	return esposizione_totale;
}
//-----------------------------------------------------------------------------------------------------
function tableruler()
{
 if (document.getElementById && document.createTextNode)
  {
   var tables=document.getElementsByTagName('table');
   for (var i=0;i<tables.length;i++)
   {
    if(tables[i].className=='ruler')
    {
     var trs=tables[i].getElementsByTagName('tr');
     for(var j=0;j<trs.length;j++)
     {
      if(trs[j].parentNode.nodeName=='TBODY' && trs[j].parentNode.nodeName!='TFOOT')
       {
       trs[j].onmouseover=function(){this.className='ruled';return false}
       trs[j].onmouseout=function(){this.className='';return false}
     }
    }
   }
  }
 }
}
//------------------------------
//------------------------------------------------
// FUNZIONI PER L'ARCHIVIAZIONE DEI VARI ANNI
//------------------------------------------------
//xml sorgente: bill_summ_archivio.xml - colonna destra tennis.php "Archivio Bilanci"
function f_bill_summ_arch()	{

var Mostra = ""; var StringaXml = DSO_archivio_bilanci.XMLDocument;
var i;
var anno, u_utile, u_giocate;

Mostra += "<div class='Article' id='red'>"
Mostra += "<H4><font color='#000000'>Archivio Bilanci</font></H4>"
Mostra += "<div class='ArticleBody'>"
Mostra += "<p>"
Mostra += "<table width='100%' border='0'><tr><td id='anni_h'><b>A</b>nno</td><td id='anni_utile_h'><b>U</b>tile</td><td id='anni_stakes_h'><b>S</b>takes</td><td id='anni_roi_h'><b>R</b>OI</td></tr></table>"
Mostra += "</p>"

for (i=0; i<StringaXml.documentElement.childNodes.length; i++) {
	anno = StringaXml.documentElement.childNodes(i).childNodes(0).text;
	u_utile = parseFloat(StringaXml.documentElement.childNodes(i).childNodes(1).text);
	u_giocate = parseFloat(StringaXml.documentElement.childNodes(i).childNodes(2).text);
	roi=Math.round((u_utile/u_giocate)*10000)/100;


Mostra += "<p>"
Mostra += "<table id='archivio_bilanci' width='100%' border='0'>"
Mostra += "<tr>"
Mostra += "<td id='arch_anno'>"
	Mostra += "<a href='../html/bilancio_" + anno + "/bilancio_" + anno + ".php?id_anno=" + anno + "' target='_parent'>" + anno +"</a>"
//Mostra += "</span></p>"
Mostra += "</td>"

	if (u_utile < 0)	
			{
			Mostra += "<td id='arch_utile'><font color='red'><b>" + u_utile + "</font></td>"
			}
			else {
			Mostra += "<td id='arch_utile'><font color='green'><b>" + u_utile + "</b></font></td>"
			}

Mostra += "<td id='arch_stakes'>" + u_giocate + "</td>"

	if (roi < 0)	
			{
			Mostra += "<td id='arch_roi'><font color='red'><b>" + roi + "%" + "</b></font></td>"
			}
			else {
			Mostra += "<td id='arch_roi'><font color='green'><b>" + roi + "%" + "</b></font></td>"
			}
Mostra += "</tr></table></p>"
}
Mostra += "</div>"
Mostra += "<div class='ArticleFooter'>"
Mostra += "<p align='center' class='sommario_footer'>&nbsp;"
Mostra += "</p>"
Mostra += "</div>"
Mostra += "</div>"

archive_bilanci.innerHTML = Mostra;
}
//------------------------------ END FUNZIONI DI ARCHIVIAZIONE --------------------------------**************
//***********************	MAIN	***************************************
f_lavagna();
f_bill_summ_current_year();
//esposizione_tot();
match_preview();
//f_tournaments();
//f_month_tournaments();
f_bill_summ_arch();
tableruler();
//------------------------------------------------- end -----------------------------------------------
