// JavaScript Document

function Trim(stringa) {
return stringa.replace(/\s+$|^\s+/g,"");
}

function verifica_data (gg,mm,aa) {
   
   var strdata = gg+"/"+mm+"/"+aa;
   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;

}

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 ShowAndHide(id1,id2){
	
if(document.getElementById){
el1=document.getElementById(id1);
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 menu_set(item_id, pref_item_menu) {
	var tot_item_menu = document.getElementById('tot_item_menu').value;
	var mydisp;
	var myclass;
	for (c=1;c<=tot_item_menu;c=c+1) {
		if (c==item_id) {
			mydisp = "block";
			myclass = "item_active";
		} else {
			mydisp = "none";
			myclass = "";
		}
		document.getElementById(pref_item_menu+'_tm_item_'+c).className=myclass;
		document.getElementById(pref_item_menu+'_tm_div_'+c).style.display=mydisp;
	}
}

/* swap image */
function swappa(element, value)
{
element.src = 'images' + "/" + element.id + (value ? "_f2" : "") + ".gif";
}



function apri (cosa) {
	help = window.open('html/help.php?cosa='+cosa+'','Help','width=410,height=220');
	help.focus();
	return false;
}



// 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) {

		document.formcerca.orderby.value = var_ordinamento;
		document.formcerca.submit();

}


function vota (id, voto, id_member) {
 
 agent.call('','vota','links_divVoto'+id, id, voto, id_member);

}


function links_del (id) {

 var ok = window.confirm('Sicuro?');
 if (ok) ok = window.confirm('Sicuro sicuro? :P');
 if (ok) {
  document.formcerca.action.value = 'delete'+id;
  document.formcerca.submit();
 }
}





function tsLog_set_points () {
 document.getElementById('points_1').value = 10;
 document.getElementById('points_2').value = 8;
 document.getElementById('points_3').value = 6;
 document.getElementById('points_4').value = 5;
 document.getElementById('points_5').value = 4;
 document.getElementById('points_6').value = 3;
 document.getElementById('points_7').value = 2;
 document.getElementById('points_8').value = 1;
 
 for (c=9;c<=20;c=c+1) {
  document.getElementById('points_'+c).value = 0;
 }
}


function tsLog_check_points () {
 var myval;
 var err = "";
 var myalert = "";
 for (c=1;c<=20;c=c+1) {
   // add 0's
   myval=document.getElementById('points_'+c).value;  
   if (isNaN(myval) || Trim(myval) == "") document.getElementById('points_'+c).value = 0;
   //points chech
   /*
   for (h=1;h<c;h=h+1) {
   alert("confronto "+c+"("+document.getElementById('points_'+c).value+") e "+h+"("+document.getElementById('points_'+h).value+")");
    if (document.getElementById('points_'+c).value > 0 && document.getElementById('points_'+c).value >= document.getElementById('points_'+h).value) myalert+="Some position gets more or equal points than a previous one\n";
	h=c;
   }*/
 }
 
 if (myalert != "") return window.confirm(myalert+"\nContinue?");
 return true;

}



function showdiv_rank(n){
	var disptxt;
	var colore;
	for(c=1;c<=5;c=c+1) {
		if (n==c) {
			disptxt = "block";
			colore = "#333333";
		} else {
			disptxt = "none";
			colore = "#000000";
		}
	document.getElementById('nr_ranking'+c).style.display = disptxt;
	document.getElementById('nr_mainlink'+c).style.color = colore;
	}
	
}


function mostra_attivi() {
 
var mybg = "";
var n_inct = document.getElementById("n_inct").value;
var n_act = document.getElementById("n_act").value;
 
 //nascondo inattivi
 for (c=0;c<n_inct;c=c+1) {
  document.getElementById("inct_"+c).style.display="none";
 }
 //ricoloro le righe
 for (c=0;c<n_act;c=c+1) {
  if (c%2==0) mybg = "#FFFFFF";
  else mybg = "#F4F4F4";
  document.getElementById("td1_act_"+c).innerHTML=(c*1+1)+".";
  document.getElementById("act_"+c).style.background=mybg;
 }
 document.getElementById("link_show_act1").style.display="none";
 
}





function mostra_inattivi() {
 
var mybg = "";
var n_inct = document.getElementById("n_inct").value;
var n_act = document.getElementById("n_act").value;
 
 //mostro inattivi
 for (c=0;c<n_inct;c=c+1) {
  document.getElementById("inct_"+c).style.display="block";
 }
 //ricoloro le righe
 for (c=0;c<n_act;c=c+1) {
  if (c%2==0) mybg = "#FFFFFF";
  else mybg = "#F4F4F4";
  document.getElementById("td1_act_"+c).innerHTML=(c*1+1)+".";
  document.getElementById("act_"+c).style.background=mybg;
 }
 document.getElementById("link_show_act1").style.display="none";
 
}

function noHtml(txt) {
    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
 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;
}

function remove_badchar_strict (str) {
 if (Trim(str) != "") {
 // per prima cosa elimino i tag html
 res = noHtml(str);
 // elimino robaccia
 res = res.replace (/\\/g, '');
 res = res.replace (/"/g, '');
 res = res.replace (/'/g, '');
 res = res.replace (/&/g, '');
 } else res=str;
return res;
}

function invita_amico() {
 var div_amico = document.getElementById("div_send_friend");
 div_amico.style.display="block";
}

function invita_amico2() {
 var err="";
 var nome = remove_badchar_strict(document.getElementById("invita_nome").value);
 var email = remove_badchar_strict(document.getElementById("invita_email").value);
 if (Trim(nome)=="" || Trim(email)=="") err="Inserisci il tuo nome e l'indirizzo email dell'amico";
 else if (!checkEmail(email)) err= "Indirizzo email non valido";
 
 if (err!="") {
	alert(err);
	return false;
 } else {
	 document.getElementById("send_friend_butt").disabled=true;
	 agent.call('','invita_amico3','invita_amico2_ret', nome, email);
 }
}
function invita_amico2_ret(ret) {
 if (ret!="ok") alert(ret);
 else {
	alert("Email inviata correttamente!");
	document.getElementById("invita_email").value="";
	document.getElementById("div_send_friend").style.display="none";
 }
document.getElementById("send_friend_butt").disabled=false;
}


function uni_set_dif_planet(id_pianeta, dif_planet) {
 agent.call('','uni_set_dif_planet','uni_set_dif_planet_ret', id_pianeta, dif_planet);
}
function uni_set_dif_planet_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
 	 var my_a;
 	 var id_pianeta = arr_ret[0];
	 var dif_planet = arr_ret[1];
	 for (c=0;c<3;c=c+1) {
	  my_a = document.getElementById("dif_plan_"+id_pianeta+"_"+c);
	  if (c==dif_planet) my_a.style.border = "1px solid #000000";
	  else my_a.style.border = "0";
	 }
 }
}


function uni_set_email_sfida(id_pianeta) {
 var avvisa_email_sfida = document.getElementById("avvisa_email_sfida").checked;
 if (avvisa_email_sfida==true) avvisa_email_sfida = 1;
 else avvisa_email_sfida=0;
 agent.call('','uni_set_email_sfida','uni_set_email_sfida_ret', id_pianeta, avvisa_email_sfida);
}
function uni_set_email_sfida_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
 }
}

function uni_set_color(id_pianeta) {
 var pref_color = document.getElementById("pref_color").value;
 agent.call('','uni_set_color','uni_set_color_ret', pref_color);
}
function uni_set_color_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
	 var pref_color = arr_ret[0];
	 document.getElementById("pref_color_img").style.background = pref_color;
 }
}

function uni_set_mood(id_pianeta) {
 var uni_umore = document.getElementById("uni_umore").value;
 document.getElementById("img_uni_mood").src = 'images/ajax_load_small.gif';
 agent.call('','uni_set_mood','uni_set_mood_ret', uni_umore);
}
function uni_set_mood_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
	 var uni_umore = arr_ret[0];
	 document.getElementById("img_uni_mood").src = np_folder+'/images/uni/mood_'+uni_umore;
 }
}

function set_mail_updates(id_pianeta) {
 var mail_updates = document.getElementById("mail_updates").checked;
 if (mail_updates==true) mail_updates=1;
 agent.call('','set_mail_updates','set_mail_updates_ret', mail_updates);
}
function set_mail_updates_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
	 var mail_updates = arr_ret[0];
 }
}



function lista_nera(action, id) {
 if (id>0 && action=="remove") {
  var p_val = id;
 } else {
  var p_sel = z_dif.getSelectedText();
  var p_vis = z_dif.getComboText();
  var p_val = z_dif.getActualValue();
  if (p_sel!=p_vis || isNaN(p_val) || Trim(p_val)=="") {
	alert("Seleziona correttamente il giocatore"); 
	return false;
  }
 }
 document.getElementById("LN_but").disabled=true;
 document.getElementById("div_lista_nera").innerHTML=waiting_msg();
 agent.call('','lista_nera','lista_nera_ret', p_val, action);
}

function lista_nera_ret(ret) {
 if (ret=="session_expired") {
  window.location.reload();
  alert("Attenzione! La pagina sarà aggiornata per ripristinare la sessione");
 } else if (ret.substr(0,10) != "__RETURN__") {
	alert(ret);
 } else {
	 ret = ret.substr(10);
	 arr_ret = ret.split("__");
	 var ID_MEMBER = arr_ret[0];
	 //alert("Hai inserito "+ID_MEMBER);
 }
 z_dif.setComboText("");
 document.getElementById("LN_but").disabled=false;
 show_lista_nera();
}

function show_lista_nera() {
 agent.call('','show_lista_nera','div_lista_nera');
}


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