var scorri=0;
var s;
function carica(visitatori,loggati,nomi,totale){
    s=setInterval("slide("+visitatori+","+loggati+",'"+nomi+"',"+totale+")",100);
}
function slide(visitatori,loggati,nomi,totale){
	var str;
        if(loggati>0)
            str="                                                                                                                                      "+(visitatori+loggati)+" visitatori di cui "+loggati+" loggati "+nomi+"."+totale+" visite in tutto.";
        else
            str="                                                                                                               "+(visitatori+loggati)+" visitatori."+totale+" visite in tutto.";
	if (scorri<=str.length){
		var st=str.slice(scorri);
		str+=str.charAt(scorri);
		document.getElementById("pacciu").value=st;
	}
	else scorri=0;
	scorri++;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function controlla(conControlloProvenienza){
        if(!document.getElementById("si").checked){
            alert("Per iscriversi bisogna accettare le condizioni di trattamento dati personali.");
            return;
        }
        var nomcognom=trim(document.getElementById("Nome_cognome").value);
        var from=trim(document.getElementById("From").value);
        var squadra=trim(document.getElementById("Squadra").value);
        var user=trim(document.getElementById("Username").value);
        var provenienza=conControlloProvenienza?trim(document.getElementById("Provenienza").value):null;
        if(nomcognom==""||from==""||from.indexOf("@")==-1||from.indexOf(".")==-1||squadra==""||user==""||(conControlloProvenienza&&provenienza=="")){
                var giovane="Giovane,";
                if(nomcognom==""){
                    giovane+=" e il nome e cognome?";
                }
                if(from==""){
                    giovane+=" e la mail?";
                }
                if(from.indexOf("@")==-1||from==""||from.indexOf(".")==-1){
                    giovane+=" ma la mail ti sembra corretta?";
                }
                if(squadra==""){
                    giovane+=" e la squadra?";
                }
                if(user==""){
                    giovane+=" e lo username?";
                }
                if(conControlloProvenienza&&provenienza==""){
                    giovane+=" e la provenienza?";
                }
                var err=document.getElementById("errore");
                while(err.hasChildNodes())
                    err.removeChild(err.lastChild);
                err.appendChild(document.createTextNode(giovane));
        }
        else{
                document.forms[0].submit();
        }
}

function ricarica(url,element){
    element.form.action=url;
    element.form.submit();
}
function nonvuoto(bottone){
    var coll=bottone.form.elements;
    for(i=0;i<coll.length;i++){
        if(coll[i].type.toLowerCase()=="select-one"&&coll[i].selectedIndex==0){
            alert("Attenzione!Mancano ancora uno o piu' giocatori!!!");
            return;
        }
    }
    bottone.form.submit(); 
}
function eguaglia(fo){
    if(fo.form.elements[0].value!=""&&fo.form.elements[0].value==fo.form.elements[1].value){
            document.getElementById("passwords").submit();
    }else
        alert("ERRORE:campi nuova password e ripeti nuova password non validi!");
}

function conferma(testo,indirizzo){
    if(window.confirm(testo)==true){
        location.href=indirizzo;
    }
}

function confermasta(el,valTemp,nessuno){
    var coll=el.form.elements;
    for(i=0;i<coll.length;i++){
        if(coll[i].type.toLowerCase()=="text"){
            val=Number(trim(coll[i].value));
            if(val==null||val==""){
                alert("Furbone..devi mettere un valore per l'asta...");
                return;
            }else{
                if((!nessuno&&valTemp>=val)||(nessuno&&valTemp>val))
                    alert("Offerta immessa piu' bassa del costo attuale del giocatore!");
                else
                    el.form.submit();
            }
        }
    }
}

function seleziona(mod){
    for (i=0;i<mod.form.length;i++){
        if(mod.form.elements[i].type.toLowerCase()=="checkbox"){
                mod.form.elements[i].checked=true;
        }
        if(mod.form.elements[i].type.toLowerCase()=="submit"&&document.getElementById("data").value!=""){
            mod.form.elements[i].disabled=false;
        }
    }
}

function proponi(select,solostessoruolo,ruolocal1,ruolocal2){
    if(solostessoruolo&&ruolocal1!=ruolocal2)
        alert("ATTENZIONE!!!O tu o l'utente che ha il calciatore richiesto ha il numero minimo di calciatori nel ruolo di "+ruolocal1+". In questo caso si puo' chiedere lo scambio solo con giocatori dello stesso ruolo.");
    else
        select.form.submit();
}
function deseleziona(mod){
    for (i=0;i<mod.form.length;i++){
        if(mod.form.elements[i].type.toLowerCase()=="checkbox"){
            mod.form.elements[i].checked=false;
        }
        if(mod.form.elements[i].type.toLowerCase()=="submit"){
            mod.form.elements[i].disabled=true;
        }
    }
}

function attiva(mod){
    var inattivo=true;
    for (i=0;i<mod.form.length;i++){
        if(mod.form.elements[i].type.toLowerCase()=="checkbox"&&mod.form.elements[i].checked==true&&document.getElementById("data").value!=""){
                inattivo=false;
        }
    }
    document.getElementById("submit").disabled=inattivo;
}

function formazione(modu,difensori,difensorid,centrocampisti,centrocampistid,attaccanti,attaccantid,irigori){
    var indice=Number(modu.value);
    if(indice==0)
        return;
    var documento=modu.ownerDocument;
    var sel="select";
    var zeroelem="option";
    while(documento.getElementById("difesa").hasChildNodes())
            documento.getElementById("difesa").removeChild(documento.getElementById("difesa").lastChild);
    while(documento.getElementById("centrocampo").hasChildNodes())
            documento.getElementById("centrocampo").removeChild(documento.getElementById("centrocampo").lastChild);
    while(documento.getElementById("attacco").hasChildNodes())
            documento.getElementById("attacco").removeChild(documento.getElementById("attacco").lastChild);
    var indexdife,indexcent,indexatta;
    switch(indice){
        case 1:{
            indexdife=3;
            indexcent=4;
            indexatta=3;
        }break;
        case 2:{
            indexdife=3;
            indexcent=5;
            indexatta=2;
        }break;
        case 3:{
            indexdife=4;
            indexcent=3;
            indexatta=3;
        }break;
        case 4:{
            indexdife=4;
            indexcent=4;
            indexatta=2;
        }break;
        case 5:{
            indexdife=4;
            indexcent=5;
            indexatta=1;
        }break;
        case 6:{
            indexdife=5;
            indexcent=3;
            indexatta=2;
        }break;
        default:{
            indexdife=5;
            indexcent=4;
            indexatta=1;
        }break;
    }
    for(i=0;i<indexdife;i++){
        difes=documento.createElement(sel);
        documento.getElementById("difesa").insertBefore(difes,null);
        difes.name="difesa"+i;
        difes.onchange=function onchange(event) {togli(this);};
        zero=documento.createElement(zeroelem);
        difes.insertBefore(zero,null);
        zero.value="0";
        zero.selected=true;
        for(ii=0;ii<difensori.length;ii++){
            difensore=documento.createElement(zeroelem);
            difensore.setAttribute("name",difensori[ii]);
            difensore.setAttribute("value",difensorid[ii]);
            difes.insertBefore(difensore,null);
            difnome=documento.createTextNode(difensori[ii]);
            difensore.insertBefore(difnome,null);
        }
        if(irigori){
            difes=documento.createElement(sel);
            documento.getElementById("difesa").appendChild(difes);
            difes.name="difesa"+i+"_r";
            difes.onchange=function onchange(event) {togli(this);};
            zero=documento.createElement(zeroelem);
            difes.appendChild(zero);
            zero.value="0";
            zero.selected=true;
            for(ii=1;ii<=11;ii++){
              difensore=documento.createElement(zeroelem);
              difensore.setAttribute("name",ii);
              difensore.setAttribute("value",ii);
              difes.appendChild(difensore);
              difnome=documento.createTextNode(ii);
              difensore.appendChild(difnome);
            }
         }
    }
    for(i=0;i<indexcent;i++){
        centrocamp=documento.createElement(sel);
        documento.getElementById("centrocampo").insertBefore(centrocamp,null);
        centrocamp.setAttribute("name","centrocampo"+i);
        centrocamp.onchange=function onchange(event) {togli(this);};
        zero=documento.createElement(zeroelem);
        centrocamp.insertBefore(zero,null);
        zero.setAttribute("value","0");
        zero.setAttribute("selected",true);
        for(ii=0;ii<centrocampisti.length;ii++){
            centrocampista=documento.createElement(zeroelem);
            centrocampista.setAttribute("name",centrocampisti[ii]);
            centrocampista.setAttribute("value",centrocampistid[ii]);
            centrocamp.insertBefore(centrocampista,null);
            cennome=documento.createTextNode(centrocampisti[ii]);
            centrocampista.insertBefore(cennome,null);
        }
        if(irigori){
            centrocamp=documento.createElement(sel);
            documento.getElementById("centrocampo").appendChild(centrocamp);
            centrocamp.name="centrocampo"+i+"_r";
            centrocamp.onchange=function onchange(event) {togli(this);};
            zero=documento.createElement(zeroelem);
            centrocamp.appendChild(zero);
            zero.value="0";
            zero.selected=true;
            for(ii=1;ii<=11;ii++){
              centrocampista=documento.createElement(zeroelem);
              centrocampista.setAttribute("name",ii);
              centrocampista.setAttribute("value",ii);
              centrocamp.appendChild(centrocampista);
              cennome=documento.createTextNode(ii);
              centrocampista.appendChild(cennome);
            }
         }
    }
    for(i=0;i<indexatta;i++){
        attacc=documento.createElement(sel);
        documento.getElementById("attacco").insertBefore(attacc,null);
        attacc.setAttribute("name","attacco"+i);
        attacc.onchange=function onchange(event) {togli(this);};
        zero=documento.createElement(zeroelem);
        attacc.insertBefore(zero,null);
        zero.setAttribute("value","0");
        zero.setAttribute("selected",true);
        for(ii=0;ii<attaccanti.length;ii++){
            attaccante=documento.createElement(zeroelem);
            attaccante.setAttribute("name",attaccanti[ii]);
            attaccante.setAttribute("value",attaccantid[ii]);
            attacc.insertBefore(attaccante,null);
            attnome=documento.createTextNode(attaccanti[ii]);
            attaccante.insertBefore(attnome,null);
        }
        if(irigori){
            attacc=documento.createElement(sel);
            documento.getElementById("attacco").appendChild(attacc);
            attacc.name="attacco"+i+"_r";
            attacc.onchange=function onchange(event) {togli(this);};
            zero=documento.createElement(zeroelem);
            attacc.appendChild(zero);
            zero.value="0";
            zero.selected=true;
            for(ii=1;ii<=11;ii++){
              attaccante=documento.createElement(zeroelem);
              attaccante.setAttribute("name",ii);
              attaccante.setAttribute("value",ii);
              attacc.appendChild(attaccante);
              attnome=documento.createTextNode(ii);
              attaccante.appendChild(attnome);
            }
         }
    }    
}

function togli(sel){
    var indice=sel.selectedIndex;
    if(indice==0){
        return;
    }
    var nomeselez=sel.value;
    var lung=sel.options.length;
    var coll=sel.form.elements;
    for(i=0;i<coll.length;i++){
        if(coll[i].type.toLowerCase()=="select-one"&&coll[i].name!=sel.name&&coll[i].name!='scegli'){
            var ind2=coll[i].selectedIndex;
            var pr=coll[i].value;
            if(ind2!=0&&pr==nomeselez){
                alert("Elemento gia'inserito!");
                sel.selectedIndex=0;
                break;
            }
        }
    }
}
function mostraMenu(menuCorrente,visibile) {
      var obj = document.getElementById(menuCorrente);

      var browser = navigator.appName;

      if (browser != "Microsoft Internet Explorer") {

            if((visibile&&obj.style.visibility != "collapse")||(!visibile&&obj.style.visibility == "visible")) {
                obj.style.visibility = "collapse";

            } else {

                  obj.style.visibility = "visible";

            }

      }
      if((visibile&&obj.style.display != "none")||(!visibile&&obj.style.display == "block")) {

            obj.style.display = "none";

      } else {

            obj.style.display = "block";

      }

}
/*
function mostraMenu(menuCorrente) {
			if (document.getElementById) {
				questoMenu = document.getElementById(menuCorrente).style
				if (questoMenu.display != "none") {
                                        questoMenu.display = "none"
				}
				else {
					questoMenu.style.display = "block"
				}
				return false
			}
			else {
				return true
			}
		}


*/
/* 
     Script  tratto dal libro "JavaScript and DHTML Cookbook" - Capitolo 8-11
     Pubblicato da O'Reilly & Associates
     Copyright 2003 Danny Goodman
	  Riprodurre questa nota per qualunque riutilizzo del codice.
	*/
	function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Puoi inserire solo numeri senza virgola!"); 
        return false;
    }
    return true;
}

function lettersOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 65 || charCode > 90) && 
        (charCode < 97 || charCode > 122)) {
        alert("Puoi inserire solo lettere!");
        return false;
    }
    return true;
}
function show_calendar(str_target, str_datetime) {
	var arr_months = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
		"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];
	var week_days = ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>Calendar</title>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"'+document.cal.time.value);\">"+
		"<img src=\"../immagini/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" colspan=\"5\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"'+document.cal.time.value);\">"+
		"<img src=\"../immagini/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current date
					str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"white\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'+document.cal.time.value; window.close();\">"+
					"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'+document.cal.time.value; window.close();\">"+
					"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"<form name=\"cal\">\n<tr><td colspan=\"7\" bgcolor=\"#87CEFA\">"+
		"<font color=\"White\" face=\"tahoma, verdana\" size=\"2\">"+
		"Time: <input type=\"text\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+
		"\" size=\"8\" maxlength=\"8\"></font></td></tr>\n</form>\n" +
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", 
		"width=200,height=250,status=no,resizable=yes,top=200,left=200");
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
	var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
	if (!re_date.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
	return (new String (
			dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getFullYear()+" "));
}
function dt2tmstr (dt_datetime) {
	return (new String (
			dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
}

