my_global_div="";
tot_div_cron = 0;//da eliminare!
tot_div_cron_incorso = -1;//da eliminare!
div_ora=0;
global_vars = new Array();
global_vars['cronaca_txt'] = new Array();
global_vars['cronaca_stats'] = new Array();
global_vars['cronache_lette'] = new Array();

// JavaScript Document
function Trim(stringa) {
return stringa.replace(/\s+$|^\s+/g,"");
}
function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}


function verifica_data (strdata) {
   
   //var strdata = gg+"/"+mm+"/"+aa;
   arrdata = strdata.split("/");
   gg = arrdata[0];
   mm = arrdata[1];
   aa = arrdata[2];
   data = new Date(aa,mm-1,gg);
   daa=data.getFullYear().toString();
   dmm=(data.getMonth()+1).toString();
      dmm=dmm.length==1?"0"+dmm:dmm
   dgg=data.getDate().toString();
      dgg=dgg.length==1?"0"+dgg:dgg
   dddata=dgg+"/"+dmm+"/"+daa
   if (dddata!=strdata) return false;
	else return true;

}

// Serve a passare da una pagina all'altra dell'elenco degli articoli
function jump2(n, id_form) {
 myform = document.getElementById(id_form);
 myform.limit.value = n;
 myform.submit();
}


function ShowAndHide(id1,id2){
	
if(document.getElementById){
var el1=document.getElementById(id1);
var el2=document.getElementById(id2);
if(el1.style.display=="none"){
 el1.style.display="block";
 if (id2 != "") el2.style.display="none";
}
else{
 el1.style.display="none";
 if (id2 != "") el2.style.display="block";
}
}
}

function hide_div (iddiv) {
 document.getElementById(iddiv).style.display='none';
}

function myClock()
{
	var interval = 1000;
    var time_data = (parseInt(document.getElementById('clock_day').value)*1000);
	var time_new_data = parseInt(time_data)+parseInt(interval);
	document.getElementById('clock_day').value = parseInt(time_new_data/1000);
	var new_data = new Date(time_new_data);
	var ora1 = new_data.toTimeString();
	var ora = ora1.substring(0,8);
	var dataora = new_data.toDateString()+" "+ora;//mese+"/"+giorno+"/"+anno+" "+hh+":"+mm+":"+ss;
	document.getElementById("my_clock").innerHTML = dataora;
    timeout_clock = window.setTimeout("myClock()", 1000);
}

function waiting_msg() {
 return '<img src="images/ajax-loader_small.gif" alt="please wait" />';
}

// Serve a passare da una pagina all'altra dell'elenco degli articoli
function jump(n) {  
 document.formcerca.limit.value = n;
 document.formcerca.submit();
}


function ordina (var_ordinamento, form) {
		var myform = document.getElementById(form);
		myform.orderby.value = var_ordinamento;
		myform.submit();
}

function checkEmail(email)
{
  var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (goodEmail) good = true;
  else good = false;
  
  if (Trim(email) == "") good = true;
  return good;
}


function noHtml(txt) {
	var cont;
    a = txt.indexOf('<');
    b = txt.indexOf('>');
    len = txt.length;
    c = txt.substring(0, a);
    if(b == -1) {
       b = a;
    }
    d = txt.substring((b + 1), len);
    txt = c + d;
    cont = txt.indexOf('<');
    if (cont != -1) {
      txt = noHtml(txt);
    }
    return txt;
}



function remove_badchar (str) {

 if (Trim(str) != "") {
 // per prima cosa elimino i tag html
 var res = str;
 //res = noHtml(str);
 //res = res.replace (/"/g, '&quot;');
 // trasformo le & in un codice riconoscibile: "*:and:*"
 res = res.replace (/&/g, '*:and:*');
 } else res=str;
 
return res;
	
}




// questa funzione mostra l'alert e invia la form se non ci sono errori
function check_alerts (vars) {
 str = vars[0];
 id_form = vars[1];
 myform = document.getElementById(id_form);
 if (str != "") {
	 alert(str);
 } else myform.submit();
}





function add_bookmark(cosa, id, flag) {
 agent.call('','add_bookmark', 'add_bookmark_ret', session_id, cosa, id, flag);
}
function add_bookmark_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var div_ret = arr_ret[0];
  var msg_ret = arr_ret[1];
  document.getElementById(div_ret).innerHTML = msg_ret;
 } 
}

function del_bookmark(cosa, id) {
 agent.call('','del_bookmark', 'del_bookmark_ret', session_id, cosa, id);
}
function del_bookmark_ret(ret) {
 myelement = document.getElementById(ret).style.display="none";
 //alert(ret);
}


function pop_skill(idPers, rep, tipskill) {
 var bott=document.getElementById("pop_but_"+idPers+"_"+rep+"_"+tipskill);
 bott.disabled=true;
 agent.call('','pop_skill', 'pop_skill_ret', session_id, idPers, rep, tipskill);
}
function pop_skill_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var rep = arr_ret[0];
  var tipskill = arr_ret[1];
  var valskill = arr_ret[2];
  var rest_bonus = arr_ret[3];
  var idPers = arr_ret[4];
  var newpop_skill = arr_ret[5];
  var mydiv = 'pop_div_'+idPers+'_'+rep+'_'+tipskill;
  agent.call('','view_skill', mydiv, valskill, 1);
  document.getElementById("div_bonus_sum_"+idPers).innerHTML = rest_bonus;
  var bott=document.getElementById("pop_but_"+idPers+"_"+rep+"_"+tipskill);
  bott.value = " + "+newpop_skill;
  bott.disabled=false;
 }
}

function sfida(id) {
 var err="";
 var casaTrasf = document.getElementById('sfida_dove').value;
 var sfida_data = document.getElementById('sfida_data').value;
 var sfida_ora = document.getElementById('sfida_ora').value;
 
 if (!verifica_data(sfida_data)) err="Formato data non corretto";
 if (Trim(sfida_ora)=="") err="Inserisci l'orario";
 
 if (err!="") alert(err);
 else {
	 document.getElementById('sfida_dove').disabled=true;
	 document.getElementById('sfida_data').disabled=true;
	 document.getElementById('sfida_ora').disabled=true;
	 document.getElementById('sfida_but').disabled=true;
	 agent.call('','sfida', 'sfida_ret', session_id, id, casaTrasf, sfida_data, sfida_ora);
 }
}
function sfida_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
	document.getElementById('sfida_dove').disabled=false;
	document.getElementById('sfida_data').disabled=false;
	document.getElementById('sfida_ora').disabled=false;
	document.getElementById('sfida_but').disabled=false;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var msg = arr_ret[0];
  document.getElementById('div_sfida_ret').innerHTML=msg;
 }
}


function sfida_conf(id, div_n, onoff) {
 var nomediv = 'div_sfida_conf_ret_'+div_n;
 document.getElementById(nomediv).innerHTML=waiting_msg();
 agent.call('','sfida_conf', nomediv, session_id, id, onoff);
}

function lista_amichevole() {
 var err="";
 var dal = document.getElementById('dal').value;
 var al = document.getElementById('al').value;
 var ora_dal = document.getElementById('ora_dal').value;
 var ora_al = document.getElementById('ora_al').value;
 
 if (!verifica_data(dal) || !verifica_data(al)) err="Formato data non corretto";
 if (Trim(ora_dal)=="" || Trim(ora_al)=="") err="Inserisci orario";
 
 if (err!="") alert(err);
 else {
	 document.getElementById('lista_amich_but').disabled=true;
	 agent.call('','lista_amichevole', 'lista_amichevole_ret', session_id, dal, al, ora_dal, ora_al);
 }
}
function lista_amichevole_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
	document.getElementById('lista_amich_but').disabled=false;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var msg = arr_ret[0];
  document.getElementById('div_lista_amichevole_ret').innerHTML=msg;
 }
}

function lista_amichevole_del () {
 agent.call('','lista_amichevole_del', 'lista_amichevole_del_ret', session_id);	
}
function lista_amichevole_del_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var msg = arr_ret[0];
  document.getElementById('div_lista_amichevole_del').innerHTML=msg;
 }
}

function send_msg(player_to) {
 var err="";
 var butt = document.getElementById('mail_butt');
 var obj = remove_badchar(document.getElementById('mail_oggetto').value);
 var testo = remove_badchar(document.getElementById('mail_body').value);
 var quote = remove_badchar(document.getElementById('mail_quote').value);
 if (quote==0 || document.getElementById('mail_quote').checked==false) quote = 0;
 if (Trim(obj)=="" || Trim(testo)=="") err= "Insert object and body";
 if (testo.length>1000) err = "Message too long, max 1000 chars.";
 if (err!="") alert (err);
 else {
	 butt.disabled=true;
	 agent.call('','send_msg', 'div_send_msg_ret', session_id, obj, testo, player_to, quote);
 }
}

function player_notes(id_player) {
 var err="";
 var butt = document.getElementById('player_notes_butt');
 var testo = remove_badchar(document.getElementById('note1').value);

 if (testo.length>1000) err = "Message too long, max 1000 chars.";
 if (err!="") alert (err);
 else {
	 butt.disabled=true;
	 agent.call('','player_notes', 'player_notes_ret', session_id, testo, id_player);
 }
}
function player_notes_ret(ret) {
 var butt = document.getElementById('player_notes_butt');
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  ShowAndHide('a_player_notes_ret','div_player_notes_ret');
 } 
 butt.disabled=false;
}

function display_first_res() {
document.getElementById('first_res_hid_1').style.display='block';
document.getElementById('first_res_hid_2').style.display='block';
document.getElementById('first_res_a').style.display='none';
}

function buy_obj (id, onoff) {
 if (!window.confirm('Sicuro?')) return false;
 //alert(id);
 agent.call('','buy_obj', 'buy_obj_ret', session_id, id, onoff);
}
function buy_obj_ret (ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var soldi = arr_ret[0];
  document.getElementById('div_soldi').innerHTML=soldi;
  alert("OK");
 }
}

function mostra_div_cron(){
 document.getElementById("div_cron_"+div_ora).style.display='block';
 window.location="#ahref_"+div_ora;
 div_ora=div_ora+1;
 if (div_ora>=tot_div_cron) {
  document.getElementById("link_cron_continua").style.display='none';
  document.getElementById("link_cron_mostratutto").style.display='none';
 }
}

function mostra_tutti_div_cron(){
 for(c=div_ora;c<tot_div_cron;c=c+1) {
  document.getElementById("div_cron_"+div_ora).style.display='block';
  div_ora=div_ora+1;
 }
 document.getElementById("link_cron_continua").style.display='none';
 document.getElementById("link_cron_mostratutto").style.display='none';
}


function centro_iscr (id_centro, onoff) {
 if (!window.confirm('Sicuro?')) return false;
 document.getElementById('div_centro_iscr').innerHTML=waiting_msg();
 //alert(id);
 agent.call('','centro_iscr', 'centro_iscr_ret', session_id, id_centro, onoff);
}
function centro_iscr_ret (ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
	document.getElementById('div_centro_iscr').innerHTML="";
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var soldi = arr_ret[0];
  //document.getElementById('div_soldi').innerHTML=soldi;
  window.location.reload();
 }
}

function search_player(cosa) {
 if (cosa=="all") {
	 var thisform = document.form_search_player;
	 var mydiv = document.getElementById("div_search_player");
	 var mydiv_name = "div_search_player";
 } else if (cosa=="pers") {
	 var thisform = document.form_search_player2;
	 var mydiv = document.getElementById("div_search_player2");
	 var mydiv_name = "div_search_player2";
 }
	if (Trim(thisform.str_search_player.value)=="") return false;
	var str = remove_badchar(thisform.str_search_player.value);
	mydiv.innerHTML= waiting_msg();
	agent.call('','search_player', mydiv_name, session_id, str, cosa);
}

function set_mod_email() {
 ShowAndHide('a_modema_ret','div_modema_ret');
 document.getElementById("form_cambiapwd").mod_email.value=1;
 document.getElementById("form_cambiapwd").email.value = "";
 document.getElementById("form_cambiapwd").email2.value = "";
}

function set_mod_pwd() {
 ShowAndHide('a_cambiapwd_ret','div_cambiapwd_ret');
 document.getElementById("form_cambiapwd").mod_pwd.value=1;
 document.getElementById("form_cambiapwd").pwd_old.value = "";
 document.getElementById("form_cambiapwd").pwd.value = "";
 document.getElementById("form_cambiapwd").pwd2.value = "";
}

function check_azione (chi) {

 var err="";
 var D_tiro = 0;
 var D_passaggio = 0;
 var D_rimbalzo = 0;
 var C_tiro = 0;
 var C_passaggio = 0;
 var C_rimbalzo = 0;
 var A_tiro = 0;
  
 if (chi=="dif_att") {
  D_tiro = document.getElementById("azione_dif_D_tiro").value;
  D_passaggio = document.getElementById("azione_dif_D_passaggio").value;
  D_rimbalzo = document.getElementById("azione_dif_D_rimbalzo").value;
  C_tiro = document.getElementById("azione_att_C_tiro").value;
  C_passaggio = document.getElementById("azione_att_C_passaggio").value;
  C_rimbalzo = document.getElementById("azione_att_C_rimbalzo").value;
  A_tiro = document.getElementById("azione_att_A_tiro").value;
 } else if (chi=="dif") {
  D_tiro = document.getElementById("azione_"+chi+"_D_tiro").value;
  D_passaggio = document.getElementById("azione_"+chi+"_D_passaggio").value;
  D_rimbalzo = document.getElementById("azione_"+chi+"_D_rimbalzo").value;
 } else if (chi=="att") {
  C_tiro = document.getElementById("azione_"+chi+"_C_tiro").value;
  C_passaggio = document.getElementById("azione_"+chi+"_C_passaggio").value;
  C_rimbalzo = document.getElementById("azione_"+chi+"_C_rimbalzo").value;
  A_tiro = document.getElementById("azione_"+chi+"_A_tiro").value;
 } else {
  D_tiro = document.getElementById("azione_"+chi+"_D_tiro").value;
  D_passaggio = document.getElementById("azione_"+chi+"_D_passaggio").value;
  D_rimbalzo = document.getElementById("azione_"+chi+"_D_rimbalzo").value;
  C_tiro = document.getElementById("azione_"+chi+"_C_tiro").value;
  C_passaggio = document.getElementById("azione_"+chi+"_C_passaggio").value;
  C_rimbalzo = document.getElementById("azione_"+chi+"_C_rimbalzo").value;
  A_tiro = document.getElementById("azione_"+chi+"_A_tiro").value;
 }
 
 var totale_D = parseInt(D_tiro)+parseInt(D_passaggio)+parseInt(D_rimbalzo);
 var totale_C = parseInt(C_tiro)+parseInt(C_passaggio)+parseInt(C_rimbalzo);
 var totale_A = parseInt(A_tiro);
 if (totale_D>0 && totale_D!=100) err = "Def must be 100%";
 if (totale_C>0 && totale_C!=100) err = "Mid must be 100%";
 if (totale_A>0 && totale_A!=100) err = "For must be 100%";
 return err;
}

function salva_formazione (tag, pkey) {
 var mydiv_dif = document.getElementById('formazione_sel_dif');
 var mydiv_att = document.getElementById('formazione_sel_att');
 var mybut = document.getElementById('form_but');
 var mywait = document.getElementById('div_form_waiting');
 var myform = document.getElementById('formazione_form');
 var tact_schdif = ""
 if (myform.tact_schdif[1].checked) tact_schdif = "tiri";
 else if (myform.tact_schdif[2].checked) tact_schdif = "passaggi";
 var sel_form = mydiv_dif.value+"-"+mydiv_att.value;
 
 if (tag!="default") {
   var err_azione="";
   var stile = new Array();
   stile[0] = new Array();
   stile[0][0] = new Array();
   stile[1] = new Array();
   stile[1][0] = new Array();
   stile[1][1] = new Array();
   stile[0][0][0] = document.getElementById('azione_dif_D_tiro').value;
   stile[0][0][1] = document.getElementById('azione_dif_D_passaggio').value;
   stile[0][0][2] = document.getElementById('azione_dif_D_rimbalzo').value;
   stile[1][0][0] = document.getElementById('azione_att_C_tiro').value;
   stile[1][0][1] = document.getElementById('azione_att_C_passaggio').value;
   stile[1][0][2] = document.getElementById('azione_att_C_rimbalzo').value;
   stile[1][1][0] = document.getElementById('azione_att_A_tiro').value;
   if (document.getElementById('stile_dif_0').checked==true) stile[0] = '';
   if (document.getElementById('stile_att_0').checked==true) stile[1] = '';
   
   if (stile[0]!="") {
	   err_azione = check_azione("dif");
       if (err_azione!="") {
		alert(err_azione);
	    return false;
	   }
   }
   if (stile[1]!="") {
	   err_azione = check_azione("att");
       if (err_azione!="") {
		alert(err_azione);
	    return false;
	   }
   }
   
 } else var stile = "";
 
 document.getElementById('div_form_ok').style.display='none';
 mybut.disabled=true;
 mywait.innerHTML = waiting_msg();
 agent.call('','salva_formazione', 'salva_formazione_ret', session_id, sel_form, tag, pkey,  tact_schdif, stile);
}

function salva_formazione_ret (ret) {
 var mybut = document.getElementById('form_but');
 var mywait = document.getElementById('div_form_waiting');
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var sel_form = arr_ret[0];
 } 
 mybut.disabled=false;
 mywait.innerHTML = "";
 document.getElementById('div_form_ok').style.display='block';
 window.setTimeout("hide_div('div_form_ok')", 10000);
}

function salva_azione (form, idpers) {
 var err_azione = check_azione(idpers);
 if (err_azione!="") {
  alert(err_azione);
  return false;
 }
 form.sub0.disabled=true;
 return true;
}


function set_campo(id,onoff){
 if (!window.confirm('Sicuro?')) return false;
 var mydiv = document.getElementById('div_set_campo');
 mydiv.innerHTML= waiting_msg();
 agent.call('','set_campo', 'set_campo_ret', session_id, id,onoff);
}
function set_campo_ret (ret) {
 var mydiv = document.getElementById('div_set_campo');
 if (ret.substr(0,10) != "__RETURN__") {
  //alert(ret);	 
  mydiv.innerHTML = ret;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var msg = arr_ret[0];
  mydiv.innerHTML = msg;
  window.location.reload();
 }
}

function ripara(id_inv,id_bil){
 if (!window.confirm('Sicuro?')) return false;
 var mydiv = document.getElementById('span_ripara_'+id_inv);
 mydiv.innerHTML= waiting_msg();
 agent.call('','ripara', 'ripara_ret', session_id, id_inv, id_bil);
}
function ripara_ret (ret) {
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var id_inv = arr_ret[0];
  var altri_ric = arr_ret[1];
  var mydiv = document.getElementById('div_ripara_'+id_inv);
  mydiv.style.display = "none";
  if (altri_ric=="") {
  var mydiv2 = document.getElementById('div_bil_rotto');
  mydiv2.style.display = "none";
  }
 }
}


//questa è per chiamare funzioni che ricevono solo un id(0 per tralasciare) e onoff
//il messaggio di ritorno finisce nel "div_"+nome_func
function my_onoff_func(id, onoff, nome_func){
 if (!window.confirm('Sicuro?')) return false;
 my_global_div = document.getElementById('div_'+nome_func);
 my_global_div.innerHTML= waiting_msg();
 /*if (id>0) agent.call('',nome_func, 'div_'+nome_func, session_id, id,onoff);
 else agent.call('',nome_func, 'div_'+nome_func, session_id, onoff);*/
 if (id>0) agent.call('',nome_func, 'my_onoff_func_ret', session_id, id,onoff);
 else agent.call('',nome_func, 'my_onoff_func_ret', session_id, onoff);
}
function my_onoff_func_ret(ret) {
 my_global_div.innerHTML = ret;
 window.location.reload();
}

function change_face(idPers, pezzo, quale) {
 var myimg = document.getElementById('img_f_'+pezzo+'_'+idPers);
 var nomeimg = "images/faces/"+pezzo+"_"+quale+".png";
 myimg.src = nomeimg;
}


function check_toto() {
 var segno = "";
 var cont = 0;
 var doppie = 0;
 var triple = 0;
 var err = "";
 var colonne = 0;
 var prezzo = 0;
 var precprezzo = parseFloat(document.formtoto.precprezzo.value);
 if (isNaN(precprezzo)) precprezzo = 0;
 for (c=1;c<=13;c=c+1) {
	 segno = "";
	 if (document.getElementById("pron_"+c+"_1").checked) segno = segno+"1";
	 if (document.getElementById("pron_"+c+"_X").checked) segno = segno+"X";
	 if (document.getElementById("pron_"+c+"_2").checked) segno = segno+"2";
	 if (segno!="") cont = cont + 1;
	 if (segno.length==2) doppie = doppie + 1;
	 if (segno.length==3) triple = triple + 1;
	 //alert("gara "+c+": "+segno+", len:"+segno.length);
 }
 if (cont!=13) {
	 err = err + "Please insert all results\n";
 }
 colonne = Math.pow(2,doppie)*Math.pow(3,triple);
 prezzo = toto_prez_col * colonne;
 
 if (err == "" && colonne<toto_min_col) err = err + "Inserisci almeno una doppia\n";
 if (err == "" && colonne>toto_max_col) err = err + "Hai giocato troppe doppie e triple\n";
 if (mysoldi<(prezzo-precprezzo)) err = err + "Non hai abbastanza soldi\n";
 
 if (err!="") {
	alert(err);
	return false;
 }
 if (prezzo>0) return window.confirm("Price: "+prezzo+"\nAre You Sure?");//Doppie:"+doppie+", Triple:"+triple+", Colonne: "+colonne+", prezzo 1 colonna: "+toto_prez_col+", 
 else return true;

}



function licenzia (id) {
	var mybut = document.getElementById('licenzia_conf');
	mybut.disabled=true;
	agent.call('','licenzia', 'licenzia_ret', session_id, id);
}
function licenzia_ret (ret) {
 var mydivret = document.getElementById('div_licenz_ret');
 if (ret.substr(0,10) != "__RETURN__") {
  mydivret.innerHTML = ret;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  window.location.reload();
 }
}

function pescata () {
	var mybut = document.getElementById('pescata_but');
	mybut.disabled=true;
	agent.call('','pescata', 'pescata_ret', session_id);
}
function pescata_ret (ret) {
 var mydivret = document.getElementById('div_pescata');
 if (ret.substr(0,10) != "__RETURN__") {
  mydivret.innerHTML = ret;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var pescataHTML = ret;
  mydivret.innerHTML = pescataHTML;
 }
}

function pescata_conf (onoff, id, contra_durata) {
	if (contra_durata==1) {
	 var contra_durata = parseInt(document.getElementById('contra_durata').value);
	 if (isNaN(contra_durata)) {
		 alert("Insert a number");
		 return false;
	 }
	}
	var mybut1 = document.getElementById('pescata_no_but');
	var mybut2 = document.getElementById('pescata_but2');
	mybut1.disabled=true;
	mybut2.disabled=true;
	agent.call('','pescata_conf', 'pescata_conf_ret', session_id, onoff, id, contra_durata);
}
function pescata_conf_ret (ret) {
 var mybut1 = document.getElementById('pescata_no_but');
 var mybut2 = document.getElementById('pescata_but2');
 var mydivret = document.getElementById('div_pescata_ret');
 var mydivret_canc = document.getElementById('div_pescata');
 if (ret.substr(0,10) != "__RETURN__") {
  mydivret.innerHTML = ret;
 } else {
  //alert(ret);
  ret = ret.substr(10);
  mydivret.innerHTML = ret;
  mydivret_canc.style.display="none";
 }
 mybut1.disabled=false;
 mybut2.disabled=false;
}



function rilancia (id) {
	 var market_durata = parseInt(document.getElementById('market_durata').value);
	 var market_ingaggio = parseInt(document.getElementById('market_ingaggio').value);
	 var market_prezzo = parseInt(document.getElementById('market_prezzo').value);
	 if (isNaN(market_durata) || isNaN(market_ingaggio) || isNaN(market_prezzo)) {
		 alert("Insert a number");
		 return false;
	 }
	var mybut1 = document.getElementById('market_but');
	mybut1.disabled=true;
	agent.call('','rilancia', 'rilancia_ret', session_id, id, market_durata, market_ingaggio, market_prezzo);
}
function rilancia_ret (ret) {
 var mybut1 = document.getElementById('market_but');
 if (ret.substr(0,10) != "__RETURN__") {
  var mydivret = document.getElementById('div_market_offer_ret');
  mydivret.innerHTML = ret;
 } else {
  var mydivret_canc = document.getElementById('div_market_offer');
  //alert(ret);
  ret = ret.substr(10);
  mydivret_canc.style.display="none";
  window.location.reload();
 }
 mybut1.disabled=false;
}


function vendi (id) {
	 var market_prezzobase = parseInt(document.getElementById('market_prezzobase').value);
	 if (isNaN(market_prezzobase)) {
		 alert("Insert a number");
		 return false;
	 }
	var mybut1 = document.getElementById('vendi_conf');
	mybut1.disabled=true;
	agent.call('','vendi', 'vendi_ret', session_id, id, market_prezzobase);
}
function vendi_ret (ret) {
 var mybut1 = document.getElementById('vendi_conf');
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);
 } else {
  var mydivret_canc = document.getElementById('div_vendi_ret');
  //alert(ret);
  ret = ret.substr(10);
  mydivret_canc.style.display="none";
  window.location.reload();
 }
 mybut1.disabled=false;
}

function sponsor_off(onoff) {
 if (window.confirm('Sure?')) {
	 document.getElementById('sponsor_onoff').value = onoff;
	 document.getElementById('form_sponsor').submit();
 }
}

function showSkillAz(id, cosa) {

 var mydivskill = document.getElementById('div_playerskill_'+id);
 var mydivskill_bott = document.getElementById('player2_botshow_skill');
 var mydivazioni = document.getElementById('div_playerazioni_'+id);
 var mydivazioni_bott = document.getElementById('player2_botshow_azioni');
 if (cosa=="skill") {
	 mydivskill.style.display = "block";
	 mydivazioni.style.display = "none";
	 mydivskill_bott.className = "player2_botshow_act";
	 mydivazioni_bott.className = "player2_botshow";
 } else {
	 mydivskill.style.display = "none";
	 mydivazioni.style.display = "block";
	 mydivskill_bott.className = "player2_botshow";
	 mydivazioni_bott.className = "player2_botshow_act";
 }

}



function send_gb(serie, girone, id_player) {
 var err="";
 var butt = document.getElementById('gb_butt');
 var testo = remove_badchar(document.getElementById('gb_body').value);
 if (Trim(testo)=="") return false;
 if (testo.length>1000) err = "Message too long, max 1000 chars.";
 if (err!="") alert (err);
 else {
	 butt.disabled=true;
	 agent.call('','send_gb', 'send_gb_ret', session_id, serie, girone, testo, id_player);
 }
}
function send_gb_ret (ret) {
 var butt = document.getElementById('gb_butt');
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
 } else {
  //alert(ret);
  ret = ret.substr(10);
  var arr_ret = ret.split("__");
  var serie = arr_ret[0];
  var girone = arr_ret[1];
  var id_player = arr_ret[2];
  var compatta = 1;
  if (id_player>0) compatta = 0;
  else compatta = 1;
  show_gb(serie, girone, id_player, 0, compatta);
  document.getElementById('gb_body').value="";
  if (id_player>0) ShowAndHide('div_send_gb','div_send_gb2');
 }
 butt.disabled=false;
}

function show_gb(serie, girone, id_player, limit, compact) {
	 agent.call('','show_gb', 'div_gb_ret', session_id, serie, girone, id_player, limit, compact);
}


function msg_sel () {
 var quanti = document.getElementById('count_msg').value;
 for (c=1;c<=quanti;c=c+1) {
	document.getElementById('delmsg_'+c).checked=true;
 }
}

function msg_del () {
 var quanti = document.getElementById('count_msg').value;
 var elenco_id = "";
 for (c=1;c<=quanti;c=c+1) {
	if (document.getElementById('delmsg_'+c).checked==true) {
		elenco_id = elenco_id + document.getElementById('delmsg_'+c).value+",";
	}
 }
 if (elenco_id!="") {
	 ShowAndHide('a_eliminasel','img_waiting');
	 agent.call('','msg_del', 'msg_del_ret', session_id, elenco_id);
 }
}
function msg_del_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert("Error: "+ret);	 
 } else {
	ret = ret.substr(10);
	var arr_ret = ret.split("__");
	var elenco_id_str = arr_ret[0];
	var elenco_id = elenco_id_str.split(",");
	var quanti = elenco_id.length;
	for (c=0;c<quanti;c=c+1) {
	 if (elenco_id[c]!="") document.getElementById('tr_'+elenco_id[c]).style.display="none";
	}
	ShowAndHide('a_eliminasel','img_waiting');
	//window.location.reload();	
 }
}


function gb_del(gbid, ahref) {
 if (!window.confirm('Sicuro?')) return false;
 ahref.innerHTML=waiting_msg();
 agent.call('','gb_del', 'gb_del_ret', session_id, gbid);
}
function gb_del_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert("Error: "+ret);	 
 } else {
	ret = ret.substr(10);
	var arr_ret = ret.split("__");
	var id = arr_ret[0];
	document.getElementById('gb_box_'+id).style.display="none";
 }
}


function eventi_del_giorno (dataoggi) {
 agent.call('','eventi_del_giorno', 'div_eventi_del_giorno_'+dataoggi, dataoggi);
}


function set_mouse_pointer(obj)
{
obj.style.cursor='pointer';
}

function unserbar_anteprima(GAME_URL, id_player, GAME_NAME) {
 var userbar_lingua = document.getElementById('userbar_lingua').value;
 var userbar_tipo = document.getElementById('userbar_tipo').value;
 var userbar_img = document.getElementById('userbar_img').value;
 var HTML_container = document.getElementById('userbar_code_HTML');
 var BBC_container = document.getElementById('userbar_code_BBC');
 var userbar_anteprima_img = document.getElementById('userbar_anteprima_img');
 var userbar_code = new Array();
 //var userbar_gameurl = GAME_URL+userbar_lingua+'/player.php?id='+id_player;
 var userbar_gameurl = GAME_URL;
 var html_src = GAME_URL+userbar_lingua+'/usebar.php?id='+id_player+'&a='+userbar_tipo+'&b='+userbar_img;
 userbar_code['HTML'] = '<a href="'+userbar_gameurl+'"><img src="'+html_src+'" alt="'+GAME_NAME+'" border="0" width="350" height="19" /></a>';
 userbar_code['BBC'] = '[url='+userbar_gameurl+'][img]'+GAME_URL+userbar_lingua+'/usebar.php?id='+id_player+'&a='+userbar_tipo+'&b='+userbar_img+'[/img][/url]';	
 
 HTML_container.value = userbar_code['HTML'];
 BBC_container.value = userbar_code['BBC'];
 userbar_anteprima_img.src = html_src;
}


function live_add(id, onoff) {
 if (onoff==0) {
	document.getElementById("live_del_"+id).innerHTML=waiting_msg();
 }
 agent.call('','live_add', 'live_add_ret', session_id, id, onoff);
}
function live_add_ret (ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);	 
 } else {
	ret = ret.substr(10);
	var arr_ret = ret.split("__");
	var id = arr_ret[0];
	var onoff = arr_ret[1];
	if (onoff==1) {
		document.getElementById("add_live_div_"+id).innerHTML=waiting_msg();
		window.location = "home.php?v=fl";
	} else if (onoff==0) {
		document.getElementById("live_"+id).style.display='none';
		global_vars['cronache_lette'][id]==1;
	}
 }
}

function get_cronaca(id, set_minuto) {
 if (typeof(global_vars['cronaca_txt'][id])!="undefined") {
	 alert("ho gia caricato la cronaca della partita: "+id);
	 return false;
 }
 document.getElementById('livetxt_'+id).innerHTML = waiting_msg();
 agent.call('','get_cronaca', 'get_cronaca_ret', session_id, id, set_minuto);
}
function get_cronaca_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	//alert("Error: "+ret);	 
 } else {
	ret = ret.substr(10);
	var arr_ret = ret.split("__");
	var id = arr_ret[0];
	var cronaca_txt = arr_ret[1];
	var mydiv = document.getElementById('livetxt_'+id);
	var appotxt = cronaca_txt;
	var str_separator = '<a name="ahref_';
	var arrCron = appotxt.split(str_separator);
	if (arrCron.length>0) {
	 for (m=0;m<(arrCron.length);m=m+1) {
      arrCron[m] = str_separator+arrCron[m];
	 }
	 //alert(arrCron[(arrCron.length)-1]);
	 global_vars['cronaca_txt'] = arrCron;
	 document.getElementById('livetxt_'+id).innerHTML = global_vars['cronaca_txt'][(global_vars['cronaca_txt'].length)-1];
	}
 }
}

function get_stats(id) {
 if (typeof(global_vars['cronaca_stats'][id])!="undefined") {
	 alert("ho gia caricato la partita: "+id);
	 return false;
 }
 agent.call('','get_stats', 'get_stats_ret', session_id, id);
}
function get_stats_ret(ret) {
 if (ret.substr(0,10) != "__RETURN__") {
	//alert("Error: "+ret);
 } else {
	ret = ret.substr(10);
	var arr_ret = ret.split("__");
	var id = arr_ret[0];
	var casa_gol = arr_ret[1];
	var trasf_gol = arr_ret[2];
	var chihasegnato = arr_ret[3];
	var minuto = parseInt(arr_ret[4]);
	var msgcron = "";
	document.getElementById("casa_gol_"+id).innerHTML = casa_gol;
	document.getElementById("trasf_gol_"+id).innerHTML = trasf_gol;
	
	//in corso
	if (chihasegnato>-1) {
		//msgcron = document.getElementById('live_frase_3').innerHTML+" "+(minuto+1)+": "+document.getElementById('live_frase_2').innerHTML+": "+document.getElementById('nomi_'+id+'_'+chihasegnato).value;
		msgcron = document.getElementById('live_frase_3').innerHTML+" "+(minuto+1);
		if (chihasegnato==0) {
		 document.getElementById("casa_gol_"+id).className='live_goal_score';
		 document.getElementById("trasf_gol_"+id).className='live_goal';
		} else {
		 document.getElementById("casa_gol_"+id).className='live_goal';
		 document.getElementById("trasf_gol_"+id).className='live_goal_score';
		}
	//tavolino
	} else if (chihasegnato==-101) {
		msgcron = document.getElementById('live_frase_4').innerHTML;
		global_vars['cronache_lette'][id]=1;
		document.getElementById('livetxt_'+id).innerHTML = '<p>'+msgcron+'</p>';
	//non ancora giocata
	} else if (chihasegnato==-102) {
		msgcron = document.getElementById('live_frase_5').innerHTML;
	//prepartita
	} else if (chihasegnato==-103) {
		msgcron = document.getElementById('live_frase_6').innerHTML;
		document.getElementById('livetxt_'+id).innerHTML = '<p>'+msgcron+'</p>';
	//partita finita
	} else {
		msgcron = document.getElementById('live_frase_1').innerHTML;
		global_vars['cronache_lette'][id]=1;
		document.getElementById("casa_gol_"+id).className='live_goal';
		document.getElementById("trasf_gol_"+id).className='live_goal';
	}
	document.getElementById('livetxt2_'+id).innerHTML = msgcron;
	//alert(ret);
 }
}

function live_aggiorna (quante) {
	for (c=0;c<=quante;c=c+1) {
		var id = document.getElementById('liveid_'+c).value;
		var attivo = document.getElementById('liveattivo_'+id).value;
		if (global_vars['cronache_lette'][id]==1) {
			continue;
		}
		//if (attivo=="1" || attivo=="2") 
		get_cronaca(id, 1);
		get_stats(id);
		if (attivo=="2") document.getElementById('liveattivo_'+id).value = "0";
	}
	global_vars['live_clock'] = window.setTimeout("live_aggiorna("+quante+")", 60000);
}

/*function live_showhidden (quale) {
	var quante = document.getElementById('live_quante').value;
	for (c=0;c<=quante;c=c+1) {
		var id = document.getElementById('liveid_'+c).value;
		var mydiv = document.getElementById('livetxt_'+id);
		if (c==quale) {
		 mydiv.style.display = "block";
		 document.getElementById('liveattivo_'+id).value="1";
		} else {
		 mydiv.style.display = "none";
		 document.getElementById('liveattivo_'+id).value="0";
		}
	}
}*/
function live_showhidden (quale) {
		var id = document.getElementById('liveid_'+quale).value;
		var mydiv = document.getElementById('livetxt_'+id);
		var mybutt = document.getElementById('live_showhidden_butt_'+id);
	    if (mydiv.style.display == "block") {
			mydiv.style.display = "none";
			mybutt.innerHTML = "[+]";
			document.getElementById('liveattivo_'+id).value="0";
		} else {
			mydiv.style.display = "block";
			mybutt.innerHTML = "[-]";
			document.getElementById('liveattivo_'+id).value="1";
		}
}

function idFROMuserid(ahref, userid) {
	//ahref.style.display='none';
	agent.call('','idFROMuserid', 'idFROMuserid_ret', userid);
}
function idFROMuserid_ret(ret) {
	//window.location = "player.php?id="+ret;
	window.open("player.php?id="+ret,'_blank');
}

function form_showstile(div_who) {
	var mydiv = document.getElementById('div_'+div_who.name);
	var radiob_0 = document.getElementById(div_who.name+'_0');
	var radiob_1 = document.getElementById(div_who.name+'_1');
	if (radiob_1.checked==true) mydiv.style.display='block';
	else mydiv.style.display='none';
}

function aa_sel(skill_s) {
	var myform = document.getElementById('form_aa');
	myform.ss.value = skill_s;
	myform.submit();
}
function aa_sub(flag) {
	var myform = document.getElementById('form_aa_confirm');
	var idc = myform.id.value;
	var ss = myform.ss.value;
	var idp = myform.id_personaggio.value;
	myform.aa_but.disabled = true;
	myform.aa_but_canc.disabled = true;
	if (flag==1) agent.call('','allenamento_avanazato', 'aa_sub_ret', session_id, ss, idp);
	else window.location='centro.php?id='+idc+'&v=aa&aaid='+idp;
}
function aa_sub_ret(ret) {
 var myform = document.getElementById('form_aa_confirm');
 var idc = myform.id.value;
 if (ret.substr(0,10) != "__RETURN__") {
  alert(ret);	 
  myform.aa_but.disabled = false;
  myform.aa_but_canc.disabled = false;
 } else {
	window.location='centro.php?id='+idc+'&v=aa';
 }
}