var btnWhichButton;

function moveslide(slide)
{
  switch (slide){
    case "a": 
       $("#slideb").stop().animate({ marginLeft: "230px",width:"190px"  },"fast" );
       $("#slidec").stop().animate({ marginLeft: "290px" },"fast" );
       $("#slided").stop().animate({ marginLeft: "430px",width:"150px"  },"fast" );
       break;
    
    case "b":
      $("#slideb").stop().animate({ marginLeft: "100px"  },"fast" );
      $("#slidec").stop().animate({ marginLeft: "290px"  },"fast" );
      $("#slided").stop().animate({ marginLeft: "430px",width:"150px"  },"fast" );
      break;
 
    case "c":
      $("#slideb").stop().animate({ marginLeft: "100px"  },"fast" );
      $("#slidec").stop().animate({ marginLeft: "200px",width:"280px"  },"fast" );
      $("#slided").stop().animate({ marginLeft: "430px",width:"150px"  },"fast" );
      break;
 
    case "d":
      $("#slideb").stop().animate({ marginLeft: "140px"},"fast" );
      $("#slidec").stop().animate({ marginLeft: "260px"  },"fast" );
      $("#slided").stop().animate({ marginLeft: "350px",width:"230px"  },"fast" );
      break;
  }
}

function returnslide()
{
  $("#slideb").stop().animate({ marginLeft: "140px"  },"fast" );
  $("#slidec").stop().animate({ marginLeft: "290px",width:"234px"  },"fast" );
  $("#slided").stop().animate({ marginLeft: "430px",width:"150px"  },"fast" );
}

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 checkCampiLink(nomeForm)
{	
	if(btnWhichButton.value == 'Annulla') return true;	

	var nomeLink = document[nomeForm].NomeLink.value;	
  var url   = document[nomeForm].Url.value;  

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

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

  return true;
}

function checkValidUrl(strUrl)
{
  var RegexUrl = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  return RegexUrl.test(strUrl);
}

function checkCampiBanner(nomeForm)
{
  if(btnWhichButton.value == 'Annulla') return true;
  
  var nomeimmagine = document[nomeForm].foto.value;     
	var url = document[nomeForm].UrlImmagine.value;	
  var radioObj = getCheckedValue(document[nomeForm].tipooperazione);
  
  if(radioObj=="1") return true; 
  
  if ((nomeimmagine == "undefined") || (trim(nomeimmagine) == ""))
  {
  	alert("Errore: Il campo Immagine e\' obbligatorio.");
   	document[nomeForm].foto.focus();
    return false;
  }
  else if((get_estensione(nomeimmagine)!="JPG") &&
					(get_estensione(nomeimmagine)!="JPEG")&& 
					(get_estensione(nomeimmagine)!="GIF") &&
					(get_estensione(nomeimmagine)!="PNG"))
  {
			alert("L'estensione del file "+nomeimmagine+" non e' consentita (jpg/jpeg/gif/png)");
			return false;                                                                  
	}

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

  return true;
}

function checkCampiNews(nomeForm)
{	
	if(btnWhichButton.value == 'Annulla') return true;	

	var titoloNews = document[nomeForm].TitoloNews.value;
	var testoNews  = tinyMCE.get("TestoNews").getContent();
  var nomeimmagine = document[nomeForm].foto.value;
   
	if ((titoloNews == "undefined") || (trim(titoloNews) == ""))
  {
  	alert("Errore: Il campo Titolo News e\' obbligatorio.");
   	document[nomeForm].TitoloNews.focus();
    return false;
  }

  if ((testoNews == "undefined") || (trim(testoNews) == ""))
  {
  	alert("Errore: Il campo Testo News e\' obbligatorio.");
   	tinyMCE.execCommand("mceAddControl", false, "TestoNews");
    tinyMCE.execCommand("mceFocus", false, "TestoNews");
    return false;
  }
  
  if ((nomeimmagine != "undefined") && (trim(nomeimmagine) != ""))
  {
    if((get_estensione(nomeimmagine)!="JPG") &&
					(get_estensione(nomeimmagine)!="JPEG")&& 
					(get_estensione(nomeimmagine)!="GIF") &&
					(get_estensione(nomeimmagine)!="PNG"))
		{
		  alert("L'estensione del file "+nomeimmagine+" non e' consentita (jpg/jpeg/gif/png)");
      return false;
    }
  }

  return true;
}

function checkCampiPagine(nomeForm)
{
  if(btnWhichButton.value == 'Annulla') return true;
  
  var tipoPagina = getCheckedValue(document[nomeForm].linguaggio);
  var nomePaginaIta = document[nomeForm].NomePaginaITA.value;
  var nomePaginaEng = document[nomeForm].NomePaginaENG.value;
  var nomeimmagine = document[nomeForm].foto.value;
  var nomeallegato = document[nomeForm].allegato.value;
  
  if ((nomePaginaIta == "undefined") || (trim(nomePaginaIta) == ""))
  {
    if((tipoPagina==0)||(tipoPagina==2))
    {
      alert(tipoPagina);
  	  alert("Errore: Il campo Nome Pagina (ITA) e\' obbligatorio.");
   	  document[nomeForm].NomePaginaITA.focus();
      return false;
    }
  }
  
  if ((nomePaginaEng == "undefined") || (trim(nomePaginaEng) == ""))
  {
    if((tipoPagina==1)||(tipoPagina==2))
    {
  	 alert("Errore: Il campo Nome Pagina (ENG) e\' obbligatorio.");
   	 document[nomeForm].NomePaginaENG.focus();
     return false;
    }
  }
  
  if ((nomeimmagine != "undefined") && (trim(nomeimmagine) != ""))
  {
    if((get_estensione(nomeimmagine)!="JPG") &&
					(get_estensione(nomeimmagine)!="JPEG")&& 
					(get_estensione(nomeimmagine)!="GIF") &&
					(get_estensione(nomeimmagine)!="PNG"))
		{
		  alert("L'estensione del file "+nomeimmagine+" non e' consentita (jpg/jpeg/gif/png)");
      return false;
    }
  }
  
  if ((nomeallegato != "undefined") && (trim(nomeallegato) != ""))
  {
    if(get_estensione(nomeallegato)!="PDF")
		{
		  alert("L'estensione del file "+nomeallegato+" non e' consentita (pdf)");
      return false;
    }
  }
  
  return true;
}

function checkCampiInvioMail(nomeForm)
{	
	if(btnWhichButton.value == 'Annulla') return true;	
		
	var nome = document[nomeForm].nome.value;
	var cognome = document[nomeForm].cognome.value;
	var email = document[nomeForm].email.value;	
	var telefono  = document[nomeForm].telefono.value;	

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

  if ((cognome == "undefined") || (trim(cognome) == ""))
  {
  	alert("Errore: Il campo Cognome e\' obbligatorio.");
   	document[nomeForm].cognome.focus();
    return false;
  }
  
  if ((email == "undefined") || (trim(email) == ""))
  {
  	alert("Errore: Il campo Email e\' obbligatorio.");
   	document[nomeForm].email.focus();
    return false;
  }
  else if(!isEmail(email))
  {
  	alert("Errore: Il formato dell'Email non e\' corretto");
   	document[nomeForm].email.focus();
    return false;
  }
  
  if ((telefono == "undefined") || (trim(telefono) == ""))
  {
  	alert("Errore: Il campo Telefono e\' obbligatorio.");
   	document[nomeForm].telefono.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;
  }
}

function disableField()
{
  document.frmGestBanner.foto.disabled = true;
  document.frmGestBanner.UrlImmagine.disabled = true;
  return true
}

function enableField()
{
  document.frmGestBanner.foto.disabled = false;
  document.frmGestBanner.UrlImmagine.disabled = false;
  return true
}

 
function controlla(valore)
{	
  if(valore==0)
  {
    document.frmGestPagina.NomePaginaENG.value = "";
    tinyMCE.get('TestoENG').setContent("");
    tinyMCE.get('TestoExtraENG').setContent("");
    $("#nomeita,#testoita,#testoextraita").fadeIn();
    $("#nomeeng,#testoeng,#testoextraeng").fadeOut();
  }
  else if(valore==1)
  {
    document.frmGestPagina.NomePaginaITA.value = "";
    tinyMCE.get('TestoITA').setContent("");    
    tinyMCE.get('TestoExtraITA').setContent("");                
    $("#nomeita,#testoita,#testoextraita").fadeOut();
    $("#nomeeng,#testoeng,#testoextraeng").fadeIn();
  }
  else
  {            
    $("#nomeita,#testoita,#testoextraita").fadeIn();
    $("#nomeeng,#testoeng,#testoextraeng").fadeIn();
  }
}

