var btnWhichButton;

function checkLogin()
{
	var username    = document.formLogin.username.value;
	var passwd    = document.formLogin.password.value;	
	
	if ((username == "undefined") || (trim(username) == ""))
  {
  	alert("Il campo username e\' obbligatorio.");
    document.formLogin.username.focus();
    return false;
  }
  
  if ((passwd == "undefined") || (trim(passwd) == ""))
  {
  	alert("Il campo password e\' obbligatorio.");
    document.formLogin.password.focus();
    return false;
  }  
  return true;
}

function isnumber(data)
{
	var numstr="0123456789";
	var thischar;
	var counter=0;
		
	for(var i=0;i<data.length;i++)
	{
		thischar=data.substring(i,i+1)
		if(numstr.indexOf(thischar) != -1)
		{
			counter++;
		}
	}
		
	if(counter==data.length)
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function checkUploadFile(data)
{	
	if(btnWhichButton.value == 'Annulla') return true;
	
	var i=1;
	var nomefile;
	var nome;
	var errore ="";
	for (i=1;i<=data;i++)
	{
		nome = "foto_"+i;
		nomefile = document.formUpload[nome].value;
		if ((nomefile=="undefined") || (trim(nomefile)==""))
		{
			errore = errore+"Il File "+i+" deve essere valorizzato\n";
		}
		else if((get_estensione(nomefile)!="JPG") &&
					  (get_estensione(nomefile)!="JPEG")&& 
					  (get_estensione(nomefile)!="GIF") &&
					  (get_estensione(nomefile)!="PNG"))
		{
			errore = errore+"L\'estensione del file "+nomefile+" non e\' consentita (jpg/jpeg/gif/png)\n";
		}	
	}
	
	if(errore!="")
	{
		alert(errore);
		return false;
	}
	else
	{
		return true;
	}
}

function checkExtension(data)
{
	var nome;
	var nomefile;
	var i=1;
	var cont=0;
	var errore ="";
	
	if(btnWhichButton.value == 'Annulla') return true;
	
	for (i=1;i<=data;i++)
	{
		nome = "foto_"+i;
		nomefile = document.formUpload[nome].value;
		if ((nomefile!="undefined") && (trim(nomefile)!=""))
		{						
			if((get_estensione(nomefile)!="JPG") &&
				 (get_estensione(nomefile)!="JPEG")&& 
				 (get_estensione(nomefile)!="GIF") &&
				 (get_estensione(nomefile)!="PNG"))
			{
				errore = errore+"L\'estensione del file "+nomefile+" non e\' consentita (jpg/jpeg/gif/png)\n";
			}
		}
		else
		{
			cont++;
		}
	}
	if(cont==data)
	{
		alert("Attenzione nessun file selezionato per l'upload");
		return false;
	}
	if(errore!="")
	{
		alert(errore);
		return false;
	}	
	return true;		
}

function get_estensione(path)
{
  posizione_punto=path.lastIndexOf(".");
	lunghezza_stringa=path.length;
	estensione=path.substring(posizione_punto+1,lunghezza_stringa);
	return estensione.toUpperCase();
}

function checkCampiCambioPassword()
{	
	if(btnWhichButton.value == 'Annulla') return true;
	
	var password    	 = document.frmCambioPasswordUser.Passwd.value;	
	var newPassword    = document.frmCambioPasswordUser.NewPasswd.value;	
	var retNewPassword = document.frmCambioPasswordUser.RetypeNewPasswd.value;

	if ((password == "undefined") || (trim(password) == ""))
  {
  	alert("Il campo Password e\' obbligatorio.");
    document.frmCambioPasswordUser.Passwd.focus();
    return false;
  }

  if ((newPassword == "undefined") || (trim(newPassword) == ""))
  {
  	alert("Il campo nuova password e\' obbligatorio.");
    document.frmCambioPasswordUser.NewPasswd.focus();
    return false;
  }

	if(newPassword!=retNewPassword)
  {
  	alert("Attenzione la nuova password non coincide con quella ridigitata");
  	document.frmCambioPasswordUser.NewPasswd.focus();
  	return false;
  }
  return true;
}


function checkCampiNewsOfferte(nomeform)
{
	if(btnWhichButton.value == 'Annulla') return true;
		
	var titoloNewsOfferta = document[nomeform].TitoloNewsOfferta.value;	

	if ((titoloNewsOfferta == "undefined") || (trim(titoloNewsOfferta) == ""))
  {
  	alert("Il campo testo per il titolo news/offerta e' obbligatorio");
    document[nomeform].TitoloNewsOfferta.focus();
    return false;
  }
	return true;
}

function checkCampiGare(nomeform)
{
	if(btnWhichButton.value == 'Annulla') return true;
  	
	var nomeGara 						= document[nomeform].NomeGara.value;
	var descrizioneGara 		= tinyMCE.get("DescrizioneGara").getContent(); 
	var dataSvolgimentoGara = document[nomeform].DataSvolgimentoGara.value;
	var nomeFileOrari       = document[nomeform].fileOrari.value;

	if ((nomeGara == "undefined") || (trim(nomeGara) == ""))
  {
  	alert("Il campo testo per il nome gara e' obbligatorio");
    document[nomeform].NomeGara.focus();
    return false;
  }

	if ((descrizioneGara == "undefined") || (trim(descrizioneGara) == ""))
  {
  	alert("Il campo testo per la descrizione gara e' obbligatorio");
  	tinyMCE.execCommand("mceAddControl", false, "DescrizioneGara");
    tinyMCE.execCommand("mceFocus", false, "DescrizioneGara");
    return false;
  }

  if ((dataSvolgimentoGara == "undefined") || (trim(dataSvolgimentoGara) == ""))
  {
  	alert("Il campo testo per la data svolgimento gara e' obbligatorio");
    tinyMCE.get("DescrizioneGara").focus();
    return false;
  }
  
  if ((nomeFileOrari!= "undefined") && (trim(nomeFileOrari)!=""))
  {
    if(get_estensione(nomeFileOrari)!="PDF")
    { 
      alert("Attenzione l\'estensione del file "+nomeFileOrari+" non e\' consentita (pdf)");
      return false;
    }
  }

	return true;
}

function checkInsClassifica()
{
	if(btnWhichButton.value == 'Annulla') return true;

	var indexSelected = document.frmInsClassifica.idgara.selectedIndex;	
	var fileSelected = "undefined";
	var nomeCampo;
	var chkExtension=true,cont=0;
	
	if ((indexSelected==-1)||(indexSelected==0))
	{
		alert("Impossibile effettuare operazioni: Nessuna gara e' stata selezionata");
		return false;
	}
	
	for (i=1;i<=11;i++)
	{
		nomeCampo = "fileClassifica"+i;		
		fileSelected  = document['frmInsClassifica'][nomeCampo].value;	
		if ((fileSelected != "undefined") && (trim(fileSelected) != ""))
		{
			cont++;
			if (get_estensione(fileSelected)!="PDF")
			{				
				chkExtension = false;				
				break;
			}
		}
	}

	if(cont==0)
	{		
		alert("Impossibile effettuare operazioni: almeno un file di classifica deve essere selezionato");
		return false;
	}
	else if (chkExtension==false)
	{
		alert('Attenzione sono ammessi soltanto file con estensione (.pdf)\r\nVerificare il file in posizione '+i);
		return false;
	}
	return true;
}

function checkCampiComboItem(nomeform,nomecampoid)
{
	if(btnWhichButton.value == 'Annulla') return true;		

	var indexSelected = document[nomeform][nomecampoid].selectedIndex;
	
	if (indexSelected==-1)
	{
		alert("Impossibile effettuare operazioni: Nessun elemento selezionato");
		return false;
	}

	return true;
}

function confirmDel(nomeform,nomecampoid,msg)
{
	if(btnWhichButton.value == 'Annulla') return true;
	
	var indexSelected = document[nomeform][nomecampoid].selectedIndex;
	
	if (indexSelected==-1)
	{
		alert("Impossibile effettuare la cancellazione: Nessun elemento selezionato");
		return false;
	}
	
	var esito=window.confirm(msg);
	return esito;
}

function checkIsChecked(field)
{
	if(btnWhichButton.value == 'Annulla') return true;

	var i=0;
	var result = false;

	for (i = 0; i < field.length; i++)
	{
		if (field[i].checked == true)
		{
			result = true;
		}
	}
	if(field.checked == true)
	{
		result = true;
	}

	if(result == true)
	{
		var esito=window.confirm("Confermi la cancellazione?");
		if(esito)
		{
			result = true;
		}
		else
		{
			result = false;
		}
	}
	else
	{
		alert("Errore: Nessun elemento selezionato");
	}
	return result;
}

function checkPrenotazione()
{
  var nome = document.frmPrenotaCampo.Nome.value;
  var cognome = document.frmPrenotaCampo.Cognome.value;
  var email = document.frmPrenotaCampo.Email.value;
  var telefono = document.frmPrenotaCampo.Telefono.value;
  //var partecipanti = document.frmPrenotaCampo.numpartecipanti.value;
  var dataPrenotazione =  document.frmPrenotaCampo.DataPrenotazioneCampo.value;
  var orarioPrenotazione = document.frmPrenotaCampo.orarioprenotazione.value;

  if ((nome == "undefined") || (trim(nome) == ""))
  {
  	alert("Attenzione il campo Nome e' obbligatorio");
    document.frmPrenotaCampo.Nome.focus();
    return false;
  }
  
  if ((cognome == "undefined") || (trim(cognome) == ""))
  {
  	alert("Attenzione il campo Cognome e' obbligatorio");
    document.frmPrenotaCampo.Cognome.focus();
    return false;
  }
  
  if ((email == "undefined") || (trim(email) == ""))
  {
  	alert("Attenzione il campo Email e' obbligatorio");
    document.frmPrenotaCampo.Nome.focus();
    return false;
  }
  else if(isEmail(email)==false)
  {
    alert("Attenzione l'email inserita non e' valida");
    document.frmPrenotaCampo.Email.focus();
    return false;
  }
  
  if ((telefono == "undefined") || (trim(telefono) == ""))
  {
  	alert("Attenzione il campo Telefono e' obbligatorio");
    document.frmPrenotaCampo.Telefono.focus();
    return false;
  }
  
  if ((dataPrenotazione == "undefined") || (trim(dataPrenotazione) == ""))
  {
  	alert("Attenzione il campo Data Richiesta Prenotazione e' obbligatorio");
    document.frmPrenotaCampo.DataPrenotazioneCampo.focus();
    return false;
  }

  var formatDate = dataPrenotazione.substr(6,2)+"/"+dataPrenotazione.substr(4,2)+"/"+dataPrenotazione.substr(0,4);
  var esito=window.confirm("Riepilogo dati prenotazione\n\nNome: "+nome+"\nCognome: "+cognome+"\nTelefono: "+telefono+"\nEmail: "+email+"\nPartecipanti: "+partecipanti+"\nData: "+formatDate+"\nOrario: "+orarioPrenotazione+"\n\nInviare richiesta?");
  return esito;
}

function checkScoreOro()
{
  if(btnWhichButton.value == 'Annulla') return true;

	var nomefile = document.frmAggiornaScoreOro.fileScoreOro.value;
	var esito = true;
  
  if ((nomefile=="undefined") || (trim(nomefile)==""))
  {
    esito=window.confirm("Attenzione il file attualmente memorizzato verra\' eliminato\nContinuare?");    
  }
  else if(get_estensione(nomefile)!="PDF")
  { 
    alert("Attenzione l\'estensione del file "+nomefile+" non e\' consentita (pdf)");
    esito=false;
  }   
  else
  {
    esito=window.confirm("Attenzione il file attualmente memorizzato verra\' sostituito da quello selezionato\nContinuare?");    
  }
  return esito;
}

function isEmail(email)
{
	if (email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	{
		return true;
	}
	return false;
}

function trim(stringa)
{
	while (stringa.substring(0,1) == ' ')
	{
  	stringa = stringa.substring(1, stringa.length);
  }
  while (stringa.substring(stringa.length-1, stringa.length) == ' ')
  {
  	stringa = stringa.substring(0,stringa.length-1);
  }
  return stringa;
}

function validateFloat(obj, nLength, nPrecision)
{
	var strVal = new String(obj.value);
  var nIndexOfDot = strVal.indexOf('.');
  var nValidLength = nIndexOfDot==-1?strVal.length:strVal.length+1
  
  if(nValidLength<strVal.length)
  {
  	alert('Maximum length can be '+nLength);
    strVal = strVal.substring(0,nValidLength);
  }
  
  if(strVal.charAt(strVal.length-1)!='.' || nIndexOfDot!=(strVal.length-1))
  {
  	if(isNaN(parseFloat(strVal)))
 		{
    	strVal='0';
    }
    obj.value=parseFloat(strVal);
  }
  
  if(-1!=nIndexOfDot && strVal.substring(nIndexOfDot+1).length>nPrecision)
  {
  	strVal=strVal.substring(0, strVal.length-1);
   	obj.value=strVal;
  }
}