function validate(adv,num) 
	{

	if (adv=='admin_clan_general')
		{
		var clan2,clan3;
        clan2 = document.getElementById('clan2');
        clan3 = document.getElementById('clan3');
		if (clan2.options[clan2.selectedIndex].value==0) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		if (clan3.options[clan3.selectedIndex].value==0) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		}
	if (adv=='admin_clan_guerra')
		{
		var guerra7,guerra8;
		guerra7 = document.getElementById('guerra7');
        guerra8 = document.getElementById('guerra8');
		if (guerra7.options[guerra7.selectedIndex].value==0) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		if (guerra8.options[guerra8.selectedIndex].value==0) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		}
	if ((adv=='admin_leggende_new') || (adv=='admin_leggende_edt'))
		{
		var titolo,testo;
		titolo  = document.getElementById('titolo');
		testo   = document.getElementById('testo');
		if (titolo.value.length<4) 
			{	alert('Titolo troppo corto !! (Minimo 4 caratteri)'  ); return false;	}
		if (GetLength()>num)
			{	alert('Testo troppo lungo !! (Max '+num+' caratteri)'  ); return false;	}
		}
	if ((adv=='admin_leggende_edt'))
		{
		var app_bio, voto, posiz;
		app_bio  = document.getElementById('app_bio');
		voto     = document.getElementById('voto');
		posiz    = document.getElementById('posizione');
        if (!(app_bio==null))
            {
            if (app_bio.options[app_bio.selectedIndex].value==-1) 
			    {	alert('Selezionare uno STATO valido !!'      ); return false;	}
		    if (voto.options[voto.selectedIndex].value==-1) 
			    {	alert('Selezionare un VOTO valido !!'        ); return false;	}
		    if (posiz.options[posiz.selectedIndex].value==-1) 
			    {	alert('Selezionare una POSIZIONE valida !!'  ); return false;	}
            }
		}
	if (adv=='admin_link_add')
		{
		var titolo,href,image;
		titolo  = document.getElementById('titolo');
		href	= document.getElementById('href'  );
		image   = document.getElementById('image' );
		if (titolo.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' );	return false;	}
		if (href.value.length<5) 
			{	alert('Indirizzo troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}
		}
	if (adv=='admin_link_edt')
		{
		var titolo,href,image,sezione;
		titolo  = document.getElementById('titolo' );
		href	= document.getElementById('href'   );
		image   = document.getElementById('image'  );
		sezione = document.getElementById('sezione');
		if (titolo.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' );	return false;	}
		if (href.value.length<5) 
			{	alert('Indirizzo troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (sezione.options[sezione.selectedIndex].value==-1) 
			{	alert('Selezionare una sezione valida !!'	 ); return false;	}
		}
	if (adv=='admin_link_sez')
		{
		var titolo;
		titolo  = document.getElementById('titolo');
		if (titolo.value.length<4) 
			{	alert('Nome Sezione troppo corto !! (Minimo 4 caratteri)' ); return false;	}
		}
	if (adv=='admin_menu_lst')
		{
		var nome,titolo,href,image,sezione,permesso;
		nome    = document.getElementById('nome'  );
		titolo  = document.getElementById('titolo');
		href	= document.getElementById('href'  );
		image   = document.getElementById('image' );
		sezione = document.getElementById('sezione' );
		permesso= document.getElementById('permesso');
		if (nome.value.length<4) 
			{	alert('Nome elemento troppo corto !! (Minimo 4 caratteri)' );	return false;	}
		if (titolo.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' );	return false;	}
		if (href.value.length<5) 
			{	alert('Indirizzo troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (sezione.options[sezione.selectedIndex].value==-1) 
			{	alert('Selezionare una sezione valida !!'	 ); return false;	}
		if (permesso.options[permesso.selectedIndex].value==-1) 
			{	alert('Selezionare un permesso valido !!'	 ); return false;	}
		}
	if (adv=='admin_menu_sez')
		{
		var titolo;
		titolo = document.getElementById('titolo');
		if (titolo.value.length<4) 
			{	alert('Nome Sezione troppo corto !! (Minimo 4 caratteri)' ); return false;	}
		}
	if (adv=='admin_msg')
		{
		var testo,destinatario;
		testo = document.getElementById('testo');
        desti = document.getElementById('destinatario');
        if (desti.value.length<=0)
            {	alert('Destinatario non valido !!'   );   return false;	}
        if (testo.value.length<=0)
            {	alert('Testo troppo corto !! (Min 1 carattere)'   );   return false;	}
		if (testo.value.length>240) 
			{	alert('Testo troppo lungo !! (Max 240 caratteri)' );   return false;	}
		}
	if ((adv=='admin_news_new') || (adv=='admin_news_edt'))
		{
		var titolo,testo;
		titolo = document.getElementById('titolo');
		testo  = document.getElementById('testo');
		if (titolo.value.length<4) 
			{	alert('Titolo troppo corto !! (Minimo 4 caratteri)'  ); return false;	}
		if (GetLength()>num)
			{	alert('Testo troppo lungo !! (Max ' + num + ' caratteri)'  ); return false;	}
		}
	if (adv=='admin_profilo_edit')
		{
		var username,password,titolo,classe,permesso,clan,sito;
		username = document.getElementById('username');
		password = document.getElementById('password');
		titolo   = document.getElementById('titolo'  );
        classe   = document.getElementById('classe'  );
		permesso = document.getElementById('permesso');
		clan     = document.getElementById('clan'    );
		sito     = document.getElementById('sito'    );
		if (username.value.length<4)
			{	alert('Username troppo corto !! (Minimo 4 caratteri)');	return false;	}
		if ((password.value.length<4) && (password.value!=''))
			{	alert('Password troppo corta !! (Minimo 4 caratteri)'); return false;	}
		if (titolo.value.length<10) 
			{	alert('Titolo troppo corto !! (Minimo 10 caratteri)' ); return false;	}
		if (classe.options[classe.selectedIndex].value==-1)
			{	alert('Selezionare una classe valida !!'			 ); return false;	}
		if (permesso.options[permesso.selectedIndex].value==-1) 
			{	alert('Selezionare un permesso valido !!'			 ); return false;	}
		if (clan.options[clan.selectedIndex].value==-1) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		if (sito.options[sito.selectedIndex].value==-1) 
			{	alert('Selezionare un avatar predefinito valido !!'  ); return false;   }
		}
	if ((adv=='admin_quest_new') || (adv=='admin_quest_edt'))
		{
		var image,titolo,testo,stato,quest_m,quest_s;
		image  = document.getElementById('image');
		titolo = document.getElementById('titolo');
		testo  = document.getElementById('testo');
		stato  = document.getElementById('stato');
        quest_m= document.getElementById('quest_max');
        quest_s= document.getElementById('quest_sub');
		if (titolo.value.length<4) 
			{	alert('Titolo troppo corto !! (Minimo 4 caratteri)' );	return false;	}
		if (GetLength()<10) 
			{	alert('Testo Quest troppo corto !! (Minimo 10 caratteri)'); return false;	}
		if (GetLength()>num)
			{	alert('Testo troppo lungo !! (Max '+num+' caratteri)'  ); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}
		if (stato.options[stato.selectedIndex].value == 0) 
			{	alert('Selezionare uno Stato valido !!'	 ); return false;	}
        if (!(validate_int(quest_m.value)))
            {   alert('Inserire un Numero di Partecipanti valido !!'); return false;   }
        if (!(validate_int(quest_s.value)))
            {   alert('Inserire un Numero di Riserve valido !!'); return false;   }
		}
	if (adv=='admin_quest_sub')
		{
		var pg;
		pg = document.getElementById('pg');
		if (pg.value.length<4) 
			{	alert('Nome Giocatore troppo corto !! (Minimo 4 caratteri)' );	return false;	}
        }
	if (adv=='admin_segnalazione')
		{
		var titolo,testo;
		titolo = document.getElementById('titolo');
		testo  = document.getElementById('testo');
		if (titolo.value.length<4) 
			{	alert('Titolo troppo corto !! (Minimo 4 caratteri)' );	return false;	}
		if (testo.value.length>2000) 
			{	alert('Testo troppo lungo !! (Max 2000 caratteri)'  );   return false;	}
		}
	if ((adv=='admin_sondaggio_new') || (adv=='admin_sondaggio_edt'))
		{
		var titolo;
		titolo = document.getElementById('titolo');
		if (titolo.length<10) 
			{	alert('Titolo Sondaggio troppo corto !! (Minimo 10 caratteri)' ); return false;	}
		}
	if ((adv=='admin_users_add') || (adv=='admin_users_edt'))
		{
		var username,password,titolo,classe,permesso,clan,sito;
		username = document.getElementById('username');
		password = document.getElementById('password');
		titolo   = document.getElementById('titolo'  );
		classe   = document.getElementById('classe'  );
		permesso = document.getElementById('permesso');
		clan     = document.getElementById('clan'    );
		sito     = document.getElementById('sito'    );
        
		if (username.value.length<4)
			{	alert('Username troppo corto !! (Minimo 4 caratteri)');	return false;	}
		if ((password.value.length<4) && (password.value!=''))
			{	alert('Password troppo corta !! (Minimo 4 caratteri)'); return false;	}
		if (titolo.value.length<10) 
			{	alert('Titolo troppo corto !! (Minimo 10 caratteri)' ); return false;	}
		if (classe.options[classe.selectedIndex].value == -1) 
			{	alert('Selezionare una classe valida !!'			 ); return false;	}
		if (permesso.options[permesso.selectedIndex].value == -1) 
			{	alert('Selezionare un permesso valido !!'			 ); return false;	}
		if (clan.options[clan.selectedIndex].value == -1) 
			{	alert('Selezionare un clan valido !!'				 ); return false;	}
		if (sito.options[sito.selectedIndex].value == -1) 
			{	alert('Selezionare un avatar predefinito valido !!'  ); return false;   }
		}

	if (adv=='dwn_add')
		{
		var titolo,file,image;
		titolo  = document.getElementById('titolo');
		file	= document.getElementById('file');
		image   = document.getElementById('image');
		if (titolo.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' );	return false;	}
		if (file.value.length<5) 
			{	alert('Nome file troppo corto !!'); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}
		}
	if (adv=='dwn_edt')
		{
		var titolo,file,image,sezione;
		titolo  = document.getElementById('titolo');
		file	= document.getElementById('file');
		image   = document.getElementById('image');
		sezione = document.getElementById('sezione');
		if (titolo.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' );	return false;	}
		if (file.value.length>0 && file.value.length<5) 
			{	alert('Nome file troppo corto !!'); return false;	}
		if (image.value.length<5) 
			{	alert('Nome immagine troppo corto !! (Minimo 5 caratteri)'); return false;	}		
		if (sezione.options[sezione.selectedIndex].text=='--------------------------------------') 
			{	alert('Selezionare una Sezione valida !!'	 ); return false;	}
		}
	if (adv=='dwn_sez')
		{
		var titolo, inform, direct;
		titolo = document.getElementById('titolo');
   		inform = document.getElementById('informazioni');
		direct = document.getElementById('directory');
		if (titolo.value.length<4) 
			{	alert('Nome Sezione troppo corto !! (Minimo 4 caratteri)' ); return false;	}
		if (direct.value.length<4) 
			{	alert('Descrizione troppo corta !! (Minimo 4 caratteri)' ); return false;	}
		if (direct.value.length<4) 
			{	alert('Nome Directory troppo corto !! (Minimo 4 caratteri)' ); return false;	}
		}
	if (adv=='rad')
		{
		var descrizione;
		testo=myform.testo.value;
		if (testo.length>255)
			{	alert('Testo troppo lungo !! (Massimo 255 caratteri)');	return false;	}
		}	
	if (adv=='upd_add')
		{
		var image;
		image	= document.getElementById('image');
		if (image.value.length<5) 
			{	alert('Nome file troppo corto !!'); return false;	}
		}
    if (adv=='upd_edt')
		{
		var sezione;
        sezione = document.getElementById('sezione');
        if (sezione.options[sezione.selectedIndex].value==0)
			{	alert('Sezione non valida !!'); return false;	}
		}
	if (adv=='upd_sez')
		{
		var direct;
		direct = document.getElementById('directory');
		if (direct.value.length<3) 
			{	alert('Nome Directory troppo corto !! (Minimo 3 caratteri)' ); return false;	}
		}

	set_mod(adv,num);    
	return true;
	}

function set_mod(adv,num)
	{
	if (adv=='rad')
		{
		myform.testo.value=set_apici(myform.testo.value);
		myform.testo.value=set_br   (myform.testo.value);
		}
	return true
	}

function set_apici(str,num)
	{
	if (num!=0)
		{
		var res = "";
		var p = 0;
		while (str.indexOf("'") > 0)
			{
			p = str.indexOf("'");
			if (str.charAt(p+1) == "'") 
				{
				res = res + str.substr(0,p) + "''" + "''"
				str = str.substr(p+2,str.length+1);
				} 
			else
				{
		        res = res + str.substr(0,p) + "''";
			    str = str.substr(p+1,str.length+1);        
				}
		 	}  		
		return(res + str);
		}
	else
		{
		return str;
		}
	}

function set_br(str)
{ 
 str = str.replace(/<\/p>/g,"");
 str = str.replace(/<p>/g,"<br>");
 str = str.replace(/\r\n+/g,"<br>");
 return str;
}

function validate_int(value)
{
  var re = new RegExp("^(\\d+)$");
  var m = re.exec(value);

  if (m == null) return false;
  else return true;
}
