function getName()
{
	var nick = getCookie("usrHP");
	if (nick == null) {
		return "";
	}
	else {
		return nick;
	}
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function azzerra() {
	
		
	
	for(
		var i = 0,
		re = /checkbox/i,
		checkbox = document.getElementsByTagName("input");
		i < checkbox.length; i++) 
		{
			if(re.test(checkbox[i].type))
			{
				checkbox[i].checked = false;
				
				
			}
			
			if(checkbox[i].name.indexOf('portieriT')!=-1)
					var ckpt = checkbox[i];
			if(checkbox[i].name.indexOf('difensoriT')!=-1)
					var ckdt = checkbox[i];
			if(checkbox[i].name.indexOf('centrocampistiT')!=-1)
					var ckct = checkbox[i];
			if(checkbox[i].name.indexOf('attaccantiT')!=-1)
					var ckat = checkbox[i];
			if(checkbox[i].name.indexOf('portieriR')!=-1)
					var ckpr = checkbox[i];
			if(checkbox[i].name.indexOf('difensoriR')!=-1)
					var ckdr = checkbox[i];
			if(checkbox[i].name.indexOf('centrocampistiR')!=-1)
					var ckcr = checkbox[i];
			if(checkbox[i].name.indexOf('attaccantiR')!=-1)
					var ckar = checkbox[i];
		}
		
	
	ckpt.value='0';
	ckdt.value='0';
	ckct.value='0';
	ckat.value='0';
	ckpr.value='0';
	ckdr.value='0';
	ckcr.value='0';
	ckar.value='0';

}

function checkT(){

	var npt=0;
	var ndt=0;
	var nct=0;				
	var nat=0;
																	
	for(
		var i = 0,
		re = /checkbox/i,
		checkbox = document.getElementsByTagName("input");
		i < checkbox.length; i++) 
		{
			if(re.test(checkbox[i].type))
			{
				
				if((checkbox[i].name.indexOf('ar_tit')!=-1) &&(checkbox[i].checked))
				{
					if(checkbox[i].id.indexOf('P')!=-1)
							npt++;
					if(checkbox[i].id.indexOf('D')!=-1)
							ndt++;
					if(checkbox[i].id.indexOf('C')!=-1)
							nct++;
					if(checkbox[i].id.indexOf('A')!=-1)
							nat++;
				}
				
			}
			
			if(checkbox[i].name.indexOf('portieriT')!=-1)
					var ckpt = checkbox[i];
			if(checkbox[i].name.indexOf('difensoriT')!=-1)
					var ckdt = checkbox[i];
			if(checkbox[i].name.indexOf('centrocampistiT')!=-1)
					var ckct = checkbox[i];
			if(checkbox[i].name.indexOf('attaccantiT')!=-1)
					var ckat = checkbox[i];
		}
		
	
	ckpt.value=npt;
	ckdt.value=ndt;
	ckct.value=nct;
	ckat.value=nat;


}

function checkR(){

	var npr=0;
	var ndr=0;
	var ncr=0;				
	var nar=0;
				
	for(
		var i = 0,
		re = /checkbox/i,
		checkbox = document.getElementsByTagName("input");
		i < checkbox.length; i++) 
		{
			if(re.test(checkbox[i].type))
			{
				if((checkbox[i].name.indexOf('ar_ris')!=-1) &&(checkbox[i].checked))
				{
					if(checkbox[i].id.indexOf('P')!=-1)
							npr++;
					if(checkbox[i].id.indexOf('D')!=-1)
							ndr++;
					if(checkbox[i].id.indexOf('C')!=-1)
							ncr++;
					if(checkbox[i].id.indexOf('A')!=-1)
							nar++;
				}
				
			}
			
			if(checkbox[i].name.indexOf('portieriR')!=-1)
					var ckpr = checkbox[i];
			if(checkbox[i].name.indexOf('difensoriR')!=-1)
					var ckdr = checkbox[i];
			if(checkbox[i].name.indexOf('centrocampistiR')!=-1)
					var ckcr = checkbox[i];
			if(checkbox[i].name.indexOf('attaccantiR')!=-1)
					var ckar = checkbox[i];
		}
	
	
	ckpr.value=npr;
	ckdr.value=ndr;
	ckcr.value=ncr;
	ckar.value=nar;
		
	
}


function conferma()
{
	
	
	
	for(
		var i = 0,
		re = /checkbox/i,
		checkbox = document.getElementsByTagName("input");
		i < checkbox.length; i++) 
		{
			
			
			if(checkbox[i].name.indexOf('portieriT')!=-1)
			{
					var ckpt = checkbox[i];
					var npt=ckpt.value;					
			}
			if(checkbox[i].name.indexOf('difensoriT')!=-1)
			{
					var ckdt = checkbox[i];
					var ndt=ckdt.value;
								}
			if(checkbox[i].name.indexOf('centrocampistiT')!=-1)
			{
					var ckct = checkbox[i];
					var nct=ckct.value;
								}
			if(checkbox[i].name.indexOf('attaccantiT')!=-1)
			{
					var ckat = checkbox[i];
					var nat=ckat.value;					
			}			
			if(checkbox[i].name.indexOf('portieriR')!=-1)
			{
					var ckpr = checkbox[i];
					var npr=ckpr.value;
					
			}
			if(checkbox[i].name.indexOf('difensoriR')!=-1)
			{
					var ckdr = checkbox[i];
					var ndr=ckdr.value;
					
			}
			if(checkbox[i].name.indexOf('centrocampistiR')!=-1)
			{
					var ckcr = checkbox[i];
					var ncr=ckcr.value;
					
			}
			if(checkbox[i].name.indexOf('attaccantiR')!=-1)
			{
					var ckar = checkbox[i];
					var nar=ckar.value;
			}
		}
	var numTit = parseInt(npt)+parseInt(ndt)+parseInt(nct)+parseInt(nat);
	var numRis = parseInt(npr)+parseInt(ndr)+parseInt(ncr)+parseInt(nar);
	
	
	if(numTit<11)
		alert("DEVI SCHIERARE 11 GIOCATORI TITOLARI");
	else if(numTit>11)
	{
		alert("HAI SCHIERATO PIU' DI 11 GIOCATORI TITOLARI");
	}
	else 
	{
		if(numRis<7)
		{
			alert("DEVI SCHIERARE 7 GIOCATORI IN PANCHINA");
		}
		else if(numRis>7)
		{
			alert("HAI SCHIERATO PIU' DI 7 GIOCATORI IN PANCHINA");
		}
		else
		{
			if (parseInt(npr)!=1)
			{
				alert("DEVI SCHIERARE 1 PORTIERE IN PANCHINA");
			} else if (parseInt(ndr)!=2)
			{
				alert("DEVI SCHIERARE 2 DIFENSORI IN PANCHINA");	
			} else if (parseInt(ncr)!=2)
			{
				alert("DEVI SCHIERARE 2 CENTROCAMPISTI IN PANCHINA");	
			} else if (parseInt(nar)!=2)
			{
				alert("DEVI SCHIERARE 2 ATTACCANTI IN PANCHINA");	
			} else
				{
					
					for(
					var i = 0,
					re = /checkbox/i,
					checkbox = document.getElementsByTagName("form");
					i < checkbox.length; i++) 
					{
						if(checkbox[i].id.indexOf('formMain')!=-1)
						{
							
							checkbox[i].submit();
							
						}
					}
				}
		}
		
	}
		
	

}


function checkField()
{
	
	var email = document.formMain.email.value;
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
	   alert("Inserire un indirizzo email corretto.");
	   return false;
	}
	
	var error = false;
	
	var nome = document.formMain.nome.value;
	var tipoR = document.formMain.oggetto.value;
	var msg = document.formMain.messaggio.value;
	var ctrl = document.formMain.ctrl.value;
	
	if( (ctrl != '') || (nome == '') || (tipoR == '') || (msg == '') ) error = true;
	
	if(error){
		document.getElementById("err").style.display = "block";
	}
	else{
		document.getElementById("err").style.display = "none";
		document.formMain.action = "/mail.php?req=m";
   		document.formMain.submit();
	}	

}

function checkSubscript()
{
	var squadra = document.formSubs.squadra.value;
	var user = document.formSubs.user.value;
	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}	
	
	var url="../../check.php?type=used&usr=" + user + "&sq="+squadra;
	
	xmlhttp.onreadystatechange=getResult;

	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type","text/html; charset=iso-8859-1");
	xmlhttp.send(null);
}


function getResult()
{
	if (xmlhttp.readyState==4)
	{
		var  check = xmlhttp.responseText;
		
		if(check == 1)
		{
			document.getElementById("errSquadra").style.display = "block";
			alert("ATTENZIONE: Il nome della squadra scelto � gi� in uso");
		}
		else if(check == 2)
		{
			document.getElementById("errUser").style.display = "block";
			alert("ATTENZIONE: Il nome utente scelto � gi� in uso");
		}
		else if(check == 3)
		{
			document.getElementById("errUser").style.display = "block";
			document.getElementById("errSquadra").style.display = "block";
			alert("ATTENZIONE: Il nome utente e il nome della squadra scelto sono gi� in uso");
		}
		else if(check == 0) 
		{
			document.getElementById("errSquadra").style.display = "none";
			document.getElementById("errUser").style.display = "none";
			
			var squadra = document.formSubs.squadra.value;
			var user = document.formSubs.user.value;
			var psw = document.formSubs.psw.value;
			var psw2 = document.formSubs.psw2.value;
			var pref = document.formSubs.pref.value;
			var email = document.formSubs.email.value;
			var email2 = document.formSubs.email2.value;
			var nome = document.formSubs.nome.value;
			var cognome = document.formSubs.cognome.value;
			var indirizzo = document.formSubs.indirizzo.value;
			var citta = document.formSubs.citta.value;
			var prov = document.formSubs.prov.value;
			var cap = document.formSubs.cap.value;
			var tel = document.formSubs.tel.value;
			var dataN = document.formSubs.mydate_id.value;
			var antispam = document.formSubs.antispam.value;
			var checkInfo = document.formSubs.checkInfo.checked;
			var ctrl = document.formSubs.ctrl.value;
			
			//alert(squadra);alert(user);alert(psw);alert(psw2);alert(pref);alert(email);alert(email2);alert(nome);alert(cognome);

			if( (ctrl != '') || (antispam != '8')){
			   document.getElementById("err").style.display = "block";
			   return;
			}
			else document.getElementById("err").style.display = "none";
			
			if (!(checkInfo)) {
			   alert("ATTENZIONE: Non hai sottoscritto l'informativa sulla privacy e le condizioni di utilizzo del sito.");
			   return;
			}
			
			//alert("preregmail");
			var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
			if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
			   alert("ATTENZIONE: L'indirizzo email inserito non e' corretto.");
			   return;
			}
			
			//alert("prepwd");
			if (psw  != psw2 ) {
			   alert("ATTENZIONE: Le password inseriti non corrispondono.");
			   return;
			}
			
			//alert("preeqmail");
			if (email != email2) {
			   alert("ATTENZIONE: Gli indirizzi email inseriti non corrispondono.");
			   return;
			}
			
			//alert("prefield");
			//alert(squadra);alert(user);alert(psw);alert(psw2);alert(pref);alert(email);alert(email2);alert(nome);alert(cognome);alert(indirizzo);alert(citta);alert(prov);alert(cap);alert(tel);alert(dataN);
			//alert("prefieldOk");
			if( (squadra == '') || (user == '') || (psw == '') || (psw2 == '') || (pref == '') || (email == '') || (email2 == '') || (nome == '') || (cognome == '') || (indirizzo == '') || (citta == '') || (prov == '') || (cap == '') || (tel == '') || (dataN == '') )
			{
				alert("ATTENZIONE: E' necessario compilare tutti i campi del modulo.");
				return;
			}
			else{
				//alert("end");
				document.getElementById("err").style.display = "none";
				document.formSubs.action = "/mail.php?req=s";
				document.formSubs.submit();
			}

		}
	}
}

function gioTopFlop(form,page)
{
	
	var val=form.cat.options[form.cat.options.selectedIndex].value;
	var newLink = "";
	if(page == 'mgr')
	{
		newLink = "/fantacalcio/top-fanta-allenatori-della-" + val + "-giornata/";
	}
	else 
	{
		var type = "top";	
		if (window.location.href.indexOf("flop") != -1) var type = "flop";
		newLink = "/fantacalcio/" + type +"-11-della-" + val + "-giornata-di-serie-a/";
	}
	window.location.href = newLink;

}


function cambiaStagione(form)
{ 

	var val=form.sta.options[form.sta.options.selectedIndex].value; 
	
	var newLink = "/fantacalcio/statistiche/portieri/M/ord-14/" + val ;	
	
	window.location.href = newLink;

}


function changeGiornata(form) 
{ 
	var val=form.cat.options[form.cat.options.selectedIndex].value; 
	
	var query = window.location.search.substring(1);
	
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			
			if(key == 'sta')
				stag = parms[i].substring(pos+1);
				
			if(key == 'ruo')
				ruo = parms[i].substring(pos+1);
			
		}
	}
	
	var newLink = '';	
	newLink = window.location.href.substring(0,window.location.href.indexOf('?')) + "?Giornata=" + val ;
	
	if(ruo != '')
		newLink += "&ruo=" + ruo;
		
	if(stag != '')
		newLink += "&sta=" + stag;

	window.location.href = newLink;
} 

function controllaOfferta(min,max)
{
	
  var offerta = document.getElementById('offerta').value;
  if(offerta != null)
  {
      offerta=offerta||0;

      if (!isNaN(offerta ) && offerta % 1==0)
      {
         if(offerta<min)
         {
                      alert('Offerta troppo bassa');
         }
         else 
         {
            if(offerta>max)
            {
                      alert('Offerta troppo alta');
            }
            else
            {
			  var formOff = document.getElementById('formOffri');
			  if (verify(formOff )) formOff.submit();
      	    }
          }

      }
      else
      {
        alert("Valore inserito non corretto. Inserire un valore intero");
      }

  }  	
  						
}


function controllaScambia(min,max,max2)
{

  var offerta = document.getElementById('crediti1').value;
  var offerta2 = document.getElementById('crediti2').value;
  
  if(offerta != null)
  {
      offerta=offerta||0;

      if (!isNaN(offerta ) && offerta % 1==0)
      {
         if(offerta<min)
         {
                      alert('Offerta troppo bassa');
         }
         else 
         {
            if((offerta>max)||(offerta2>max2))
            {
                      alert('Offerta troppo alta');
            }
            else
            {          
              document.getElementById('formScambia').submit();
      	    }
          }

      }
      else
      {
        alert("Valore inserito non corretto. Inserire un valore intero");
      }

  }  	
  				
}

function cambiaModulo(form) 
{ 
	//alert("cambia Mod");
	
	var val=form.cat.options[form.cat.options.selectedIndex].value; 
	
	//alert(val);
	
	var numD = val.substring(0,1);
	var numC = val.substring(1,2);
	var numA = val.substring(2,3);
	//alert(numD);
	//alert(numC);
	//alert(numA);
	
} 

var legendaCount=0;
function hide2(hidden,team)
{
	alert("hidden: " + hidden);
	alert("team: " + team);
} 
	
	
function hide(hidden,team)
{
	
	var isVisible = '';//'block';
	var isVisibleRight =  'none';
	var isVisibleLeft = '';//'block';
	
	if(hidden == true)
	{
		isVisible = 'none';
		isVisibleRight = '';//'block';
		isVisibleLeft =  'none';
		legendaCount = legendaCount-1;
	}
	else
	{
		legendaCount = legendaCount+1;
	}
	
	if(legendaCount == 0)
	{
		//document.getElementById("legenda").style.display = 'none';
	}
	else
	{
		//document.getElementById("legenda").style.display = 'block';
	}
	
	var idTeam = 'det';
	var idLeft = 'det_left';
	var idRight = 'det_right';
	
	if(team == 1)
	{
		idTeam = 'det1';
		idLeft = 'det1_left';
		idRight = 'det1_right';
	}
	else if(team == 2)
	{
		idTeam = 'det2';
		idLeft = 'det2_left';
		idRight = 'det2_right';
	}
	
	checkbox = document.getElementsByTagName("td");
		
	for( var i = 0; i < checkbox.length; i++) 
		{	
			
				if(checkbox[i].id != null)
				{
					if(checkbox[i].id.indexOf(idLeft)!=-1)
					{
				
						checkbox[i].style.display = isVisibleLeft ;		
					}
					else if(checkbox[i].id.indexOf(idRight)!=-1)
					{
						
						checkbox[i].style.display = isVisibleRight ;		
					}
					else if(checkbox[i].id.indexOf(idTeam)!=-1)
					{
						
						checkbox[i].style.display = isVisible ;		
					}
				}
		}
	
}

function subForm() { document.formForm.submit(); }

var n=0;
function verify(myForm)
{ 

	if (n==0) {
		//alert("Your from submitted!");
		n++;
		var button = document.getElementById('submBttn');
		button.value = "Invio in corso...";
		button.disabled = "disabled";
		return true;
	} 
	else {
		alert("Operazione in corso..");
		return false;
	} 
	
	//document.myForm.action ='actionVendi.php';
    //document.myForm.submit();
	
	//myForm.onsubmit = function() { return false;};

	return true;
}

function textCounter(field,cntfield,maxlimit) {

	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
	cntfield.value = maxlimit - field.value.length;
}

function tradeMgr(str)
{
	var formSc = document.getElementById('formScambia');
	var url="/campionato/actionScambia/?op=" + str;
	
	if(str == "PROP")
	{
		idTeam1 = formSc.squadra1.value;
		idTeam2 = formSc.squadra2.value;
		idP1_1 = formSc.team1_1.options[formSc.team1_1.options.selectedIndex].value;
		idP1_2 = formSc.team1_2.options[formSc.team1_2.options.selectedIndex].value;
		idP1_3 = formSc.team1_3.options[formSc.team1_3.options.selectedIndex].value;
		idP2_1 = formSc.team2_1.options[formSc.team2_1.options.selectedIndex].value;
		idP2_2 = formSc.team2_2.options[formSc.team2_2.options.selectedIndex].value;
		idP2_3 = formSc.team2_3.options[formSc.team2_3.options.selectedIndex].value;
		creditsT1 = document.getElementById('crediti1').value;
		if (creditsT1 == "" ) creditsT1 = 0;
		creditsT2 = document.getElementById('crediti2').value;
		if (creditsT2 == "" ) creditsT2 = 0;
		url += "&s1=" + idTeam1 + "&s2=" + idTeam2 + "&p1_1=" + idP1_1 + "&p1_2=" + idP1_2 + "&p1_3=" + idP1_3 + "&p2_1=" + idP2_1 + "&p2_2=" + idP2_2 + "&p2_3=" + idP2_3 + "&creds1=" + creditsT1 + "&creds2=" + creditsT2;
		
		formSc.team1_1.disabled = "disabled";
		formSc.team1_2.disabled = "disabled";
		formSc.team1_3.disabled = "disabled";
		formSc.team2_1.disabled = "disabled";
		formSc.team2_2.disabled = "disabled";
		formSc.team2_3.disabled = "disabled";
		document.getElementById('crediti1').disabled = "disabled";
		document.getElementById('crediti2').disabled = "disabled";
		document.getElementById('scambia').disabled = "disabled";		
		
	}
	else if(str == "ACC")
	{
		idTrade = document.getElementById('idTr').value;
		url += "&idT=" + idTrade;
		document.getElementById('accettaTr').disabled = "disabled";
		document.getElementById('rifiutaTr').disabled = "disabled";		
	}
	else if(str == "REF")
	{
		idTrade = document.getElementById('idTr').value;
		url += "&idT=" + idTrade;
		document.getElementById('accettaTr').disabled = "disabled";
		document.getElementById('rifiutaTr').disabled = "disabled";
	}
	else if(str == "ANN")
	{
		idTrade = document.getElementById('idTr').value;
		url += "&idT=" + idTrade;
		document.getElementById('anullaTr').disabled = "disabled";		
	}
	
	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}	
	
	
	xmlhttp.onreadystatechange=tradeResp;

	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type","text/html; charset=iso-8859-1");
	xmlhttp.send(null);

}

function tradeResp()
{
	if (xmlhttp.readyState==4)
	{
		var msg = xmlhttp.responseText;
		var msgArray = msg.split("**-**");
		var exit = msgArray[0];
		var exitMessage = msgArray[1];
		
		document.getElementById("mess").innerHTML = exitMessage;
		
	}
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {
	  var xmlhttp = new XMLHttpRequest();
	  //xmlhttp.overrideMimeType("text/html; charset=iso-8859-1");
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return xmlhttp;
	  }
	if (window.ActiveXObject)
	  {
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			return ajaxRequest;
		} catch (e) {
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						return ajaxRequest;
		}
	  
		
	  }
	return null;
}

