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)==""))
		{
			continue;
		}
		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 checkExtensionAllegato()
{	
	var nomefile;
	var nome;
	var errore ="";

	if(btnWhichButton.value == 'Annulla') return true;
	
	nome = "allegato";
	nomefile = document.formUpload[nome].value;	

	if ((nomefile!="undefined") && (trim(nomefile)!=""))
	{
		if((get_estensione(nomefile)!="DOC")&&			  
			 (get_estensione(nomefile)!="PDF"))
		{
			errore = errore+"L\'estensione del file "+nomefile+" non e\' consentita (doc/pdf)\n";
		}		
	}
	else
	{
		errore = errore + "Attenzione nessun file selezionato per l'upload\n";		
	}

	if(errore!="")
	{
		alert(errore);
		return false;
	}
	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 checkCampiCasiClinici(nomeForm)
{	
	if(btnWhichButton.value == 'Annulla') return true;	
 
  var nomeCasoClinico     = document[nomeForm].NomeCasoClinico.value;   
	var tipoCasoClinicoIta  = document[nomeForm].TipoCasoClinicoIta.value;
	var dettagliCasoClinico = tinyMCE.get("DettagliCliniciIta").getContent();

  var fotoPreTrattamento  = document[nomeForm].fotoPreTrattamento.value;
  var fotoPostTrattamento = document[nomeForm].fotoPostTrattamento.value;
  
  var opzioniPreTrattamento  = getCheckedValue(document[nomeForm].GroupOpzioniFotoPre);
  var opzioniPostTrattamento = getCheckedValue(document[nomeForm].GroupOpzioniFotoPost);
  
  if ((nomeCasoClinico == "undefined") || (trim(nomeCasoClinico) == ""))
  {
  	alert("Errore: Il campo Nome Caso Clinico e\' obbligatorio.");
   	document[nomeForm].NomeCasoClinico.focus();
    return false;
  }

	if ((tipoCasoClinicoIta == "undefined") || (trim(tipoCasoClinicoIta) == ""))
  {
  	alert("Errore: Il campo Tipo Caso Clinico (Ita) e\' obbligatorio.");
   	document[nomeForm].TipoCasoClinicoIta.focus();
    return false;
  }

  if ((dettagliCasoClinico == "undefined") || (trim(dettagliCasoClinico) == ""))
  {
  	alert("Errore: Il campo Dettagli Clinici(Ita) e\' obbligatorio.");
   	tinyMCE.execCommand("mceAddControl", false, "DettagliCliniciIta");
    tinyMCE.execCommand("mceFocus", false, "DettagliCliniciIta");
    return false;
  }

  if ((fotoPreTrattamento != "undefined") && (trim(fotoPreTrattamento) != ""))
  {
    if((get_estensione(fotoPreTrattamento)!="JPG") &&
		   (get_estensione(fotoPreTrattamento)!="JPEG")&& 
		   (get_estensione(fotoPreTrattamento)!="GIF") &&
		   (get_estensione(fotoPreTrattamento)!="PNG"))
    {
			alert("L\'estensione del file "+fotoPreTrattamento+" non e\' consentita (jpg/jpeg/gif/png)");
			return false;
		}
  }
  else
  {
      if (((opzioniPreTrattamento == "undefined") || (trim(opzioniPreTrattamento) == ""))||(opzioniPreTrattamento=="1"))
      {
      	alert("Errore: Il campo Foto Pre Trattamento e\' obbligatorio.");
      	return false;
      }
  }
    
  if ((fotoPostTrattamento != "undefined") && (trim(fotoPostTrattamento) != ""))
  {
    if((get_estensione(fotoPostTrattamento)!="JPG") &&
		   (get_estensione(fotoPostTrattamento)!="JPEG")&& 
		   (get_estensione(fotoPostTrattamento)!="GIF") &&
		   (get_estensione(fotoPostTrattamento)!="PNG"))
    {
			alert("L\'estensione del file "+fotoPostTrattamento+" non e\' consentita (jpg/jpeg/gif/png)");
			return false;
		}
  }
  else
  {    
    if (((opzioniPostTrattamento == "undefined") || (trim(opzioniPostTrattamento) == ""))||(opzioniPostTrattamento=="1"))
    {
    	alert("Errore: Il campo Foto Post Trattamento e\' obbligatorio.");
    	return false;
    }
  }

  return true;
}

function checkCampiContatti(nomeForm)
{
  var cognome = document[nomeForm].cognome.value;     
	var nome    = document[nomeForm].nome.value;	
	var telefono = document[nomeForm].telefono.value;
	var email    = document[nomeForm].email.value;
	var messaggio = document[nomeForm].messaggio.value;
    
  if ((cognome == "undefined") || (trim(cognome) == ""))
  {
  	alert("Errore: Il campo Cognome e\' obbligatorio.");
   	document[nomeForm].cognome.focus();
    return false;
  }

	if ((nome == "undefined") || (trim(nome) == ""))
  {
  	alert("Errore: Il campo Nome e\' obbligatorio.");
   	document[nomeForm].nome.focus();
    return false;
  }

  if ((telefono == "undefined") || (trim(telefono) == ""))
  {
  	alert("Errore: Il campo Telefono e\' obbligatorio.");
    return false;
  }

  if ((email == "undefined") || (trim(email) == ""))
  {
  	alert("Errore: Il campo Email e\' obbligatorio.");
    return false;
  }
  else if(!isEmail(email))
  {
  	alert("Errore: Il formato dell'email e\' scorretto.");
    return false;
  }
  
  if ((messaggio == "undefined") || (trim(messaggio) == ""))
  {
  	alert("Errore: Il campo Messaggio e\' obbligatorio.");
    return false;
  }

  return true;
}

function checkCampiRicerca(nomeForm)
{			
	var criteriRicerca = document[nomeForm].CriteriRicerca.value;
	 
	if ((criteriRicerca == "undefined") || (trim(criteriRicerca) == ""))
  {
  	alert("Errore: Per effettuare la ricerca e\' necessario specificare i criteri desiderati");
   	document[nomeForm].CriteriRicerca.focus();
    return false;
  }
  return true;
}

function checkCampiComboItem(nomeform,nomecampoid)
{
	if(btnWhichButton.value == 'Annulla') return true;		

	var indexSelected = document[nomeform][nomecampoid].selectedIndex;
  var valueSelected = document[nomeform][nomecampoid].value;
   
	if ((indexSelected==-1)||(valueSelected=='undefined')||(trim(valueSelected)==""))
	{
		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 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 getCheckedValue(radioObj)
{
  if(!radioObj)
	 return "";
	
  var radioLength = radioObj.length;
	if(radioLength == undefined)
	 if(radioObj.checked)
			return radioObj.value;
	 else
	 	 return "";
	 	 
	for(var i = 0; i < radioLength; i++)
  {
		if(radioObj[i].checked)
    {
			return radioObj[i].value;
		}
	}
	return "";
}

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;
  }
}


