// valida e-mail

function ValidaEmail(stringa)
{ 
  if (stringa=="")
	return(true)
  var carrattere=""
  var testo =0
  var vannobene = "abcdefghijklmnopqrstuvwxyz_@.ABCDEFGHIJKLMNOPQRSTUVWXYZƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789";
  var checkStr = stringa;
  var aposto = true;
  var chiocciola = 0
  var punto = 0
  for (i = 0;  i < checkStr.length;  i++)
  {
	 carattere = checkStr.charAt(i);
	 for (j = 0;  j < vannobene.length;  j++)
	 {
		if (carattere == vannobene.charAt(j))
		{
		  if (carattere=="@")
		  {
			 if (testo==0)
				aposto=false;
			 chiocciola = chiocciola +1;
			 testo = -1;
		  }
		  if (carattere==".")
		  {
			 if (testo==0)
				aposto=false;
			 punto= punto +1
			 testo = -1;
		  }
		  testo = testo + 1;
		  break;
		}

		if (j == vannobene.length)
		{
		  aposto = false;
		  break;
		}
	 }
   }
	if (!aposto || chiocciola !=1 || punto ==0 || testo < 2 || testo >3 )
   {
		return (false);
   }
return (true);
}

function ValidaNumeri(checkStr)
{ 
  if (checkStr=="")
	return(true)

  var carrattere="";
  var vannobene = "0123456789/,.";
  var bolAposto = true;
  var bolTrovato = false;

  for (i = 0;  i < checkStr.length;  i++)
  {
		carattere = checkStr.charAt(i);
		bolTrovato = false;

	    for (j = 0;  j < vannobene.length;  j++)
		{
			if (carattere == vannobene.charAt(j))
			{		
				bolTrovato = true;
				break;
			}		
		}
		
		if (!bolTrovato ) 
		{
			bolAposto = false;
			break;
		}
  }		

  if (!bolAposto )
  {
    return (false);
  }
return (true);
}

//Controllo user Id
function CheckPrivate()
{
	if (document.admin.a_usr.value.length == 0)
	{
		alert("ATTENZIONE: Username mancante");
		document.admin.a_usr.focus();
		return ;
	}
	
	if (document.admin.a_pwd.value.length == 0)
	{
		alert("ATTENZIONE: Password mancante.");
		document.admin.a_pwd.focus();
		return ;
	}
				
	document.admin.submit();
}

//Controllo inserimanto Direttivo
function CheckDirettivo(){
	var c = document.Direttivo;
	c.action = "/private/priv_DirAdd.asp";
	
	if (c._nome.value == ""){
		alert(error);
		c._nome.focus();
		return false;
	}

	if (c._cognome.value.replace(/^ +/, "") == ""){
		alert(error);
		c._cognome.focus();
		return false;
	}
	if (ValidaEmail(c._email.value) == false){
		alert(mail);
		c._email.focus();
		return false;
	}
}
//elimina direttivo
function deleteDire(ind){
	if (!confirm(confirm_del)){
		return;
	}
	location.href="priv_DirDel.asp?DIRE_ID=" + ind;
}
//Controllo modifica Direttivo
function CheckDirMod(){
	var c = document.DirMod;
	c.action = "/private/priv_DirAddMod.asp";
	
	if (c._nome.value == ""){
		alert(error);
		c._nome.focus();
		return false;
	}

	if (c._cognome.value.replace(/^ +/, "") == ""){
		alert(error);
		c._cognome.focus();
		return false;
	}
	if (c._commento.value.replace(/^ +/, "") == ""){
		alert(error);
		c._commento.focus();
		return false;
	}
	if (c._email.value.replace(/^ +/, "") == ""){
		alert(error);
		c._email.focus();
		return false;
	}
	if (ValidaEmail(c._email.value) == false){
		alert(mail);
		c._email.focus();
		return false;
	}
	
}

//Controllo inserimento evento
function CheckEventi(){
	var c = document.Eventi;
	c.action = "/private/priv_EveAdd.asp";
	
	if (c._evento.value == ""){
		alert(error);
		c._evento.focus();
		return false;
	}

	if (c._organizzatore.value.replace(/^ +/, "") == ""){
		alert(error);
		c._organizzatore.focus();
		return false;
	}
	if (c._luogo.value.replace(/^ +/, "") == ""){
		alert(error);
		c._luogo.focus();
		return false;
	}
	if (c._email.value.replace(/^ +/, "") == ""){
		alert(error);
		c._email.focus();
		return false;
	}
	if (ValidaEmail(c._email.value) == false){
		alert(mail);
		c._email.focus();
		return false;
	}

}
//elimina Evento
function deleteEven(ind){
	if (!confirm(confirm_del)){
		return;
	}
	location.href="priv_EveDel.asp?EVEN_ID=" + ind;
}
//Controllo Modifica evento
function CheckEveMod(){
	var c = document.EventiMod;
	c.action = "/private/priv_EveAddMod.asp";
	
	if (c._evento.value == ""){
		alert(error);
		c._evento.focus();
		return false;
	}

	if (c._organizzatore.value.replace(/^ +/, "") == ""){
		alert(error);
		c._organizzatore.focus();
		return false;
	}
	if (c._luogo.value.replace(/^ +/, "") == ""){
		alert(error);
		c._luogo.focus();
		return false;
	}
	if (c._email.value.replace(/^ +/, "") == ""){
		alert(error);
		c._email.focus();
		return false;
	}
	if (ValidaEmail(c._email.value) == false){
		alert(mail);
		c._email.focus();
		return false;
	}

}
//Controllo inserimanto ALBUM
function CheckAlbum(){
	var c = document._Album;
	c.action = "/private/priv_AlbAdd.asp";
	
	if (c._album.value == ""){
		alert(error);
		c._album.focus();
		return false;
	}
}
//Controllo inserimanto IMMAGINE ALBUM

function CheckAlbuImg(){
	var c = document._AlbuImg;
	c.action = "/private/priv_AlbImgAdd.asp";
	
	if(c._desc.value.length>250){
		alert(desc);
		c._desc.focus();
		return false;
	}
	
	if(c._file.value.replace(/^ +/, "") == ""){
		alert(file);
		c._file.focus();
		return false;
	}
	
	nomeFile = c._file.value;
	if (nomeFile == "") return;
	
	extFile = nomeFile.substr(nomeFile.lastIndexOf(".")+1);
	extFile = extFile.toLowerCase();
	
	permesse = "#jpg#jpe#jpeg#gif#";
	
	if (permesse.indexOf("#" + extFile + "#") == -1) {
		alert(format);
		c._file.focus();
		return false;
	}  

}
//elimina galleria
function deleteGall(ind){
	if (!confirm(confirm_del)){
		return;
	}
	location.href="priv_AlbDel.asp?ALBU_ID=" + ind;
}

//elimina singola immagine
function deleteImg(ind){
	if (!confirm(confirm_del)){
		return;
	}
	location.href="priv_ImgDel.asp?GALL_ID=" + ind;
}

/*controllo articoli*/
function CheckArticoli(){
	var c = document.articoli;
	c.action = "/private/priv_ArtAdd.asp";

	if (c._titolo.value == ""){
		alert(error);
		c._titolo.focus();
		return false;
	}

	if (c._articolo.value == ""){
		alert(error);
		c._articolo.focus();
		return false;
	}

	//if(c._articolo.value.length>250){
	//	alert(desc);
	//	c._articolo.focus();
	//	return false;
	//}
	
	
}

/*Formattazione testo*/

link_text_prompt = "Inserisci il testo da mostrare (optional)";
link_url_prompt = "Inserisci l'indirizzo internet per il link.";
link_email_prompt = "Inserisci l'indirizzo e-mail per il link";
link_b_prompt = "Inserisci il testo da visualizzare in grassetto";
link_i_prompt = "Inserisci il testo da visualizzare in corsivo";
link_u_prompt = "Inserisci il testo da visualizzare sottolineato";
tag_prompt = "Inserisci l'indirizzo internet della foto:";

var text = "";
AddTxt = "";

function TestoAttivo(selectedtext) {
        text = (document.all) ? document.selection.createRange().text : document.getSelection();
        if (selectedtext.createTextRange) {
            selectedtext.caretPos = document.selection.createRange().duplicate();
        }
        return true;
}

function AggiungiTesto(NewCode,theform,Campo) {
	if (theform.elements[Campo].createTextRange && theform.elements[Campo].caretPos) {
		var caretPos = theform.elements[Campo].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
	} else {
		theform.elements[Campo].value+=NewCode;
	}
	setfocus(theform,Campo);
	AddTxt = "";
}


function CreaLink(theform,thetype,Campo){
	if (text) { var dtext=text; } else { var dtext=""; }
	
	linktext = prompt(link_text_prompt,dtext);
	var prompttext;

	if (thetype == "URL") {
		prompt_text = link_url_prompt;
		prompt_contents = "http://";
	} else {
		prompt_text = link_email_prompt;
		prompt_contents = "";
	}
	
	linkurl = prompt(prompt_text,prompt_contents);

	if ((linkurl != null) && (linkurl != "")) {
		if ((linktext != null) && (linktext != "")) {
			AddTxt = "[" + thetype + "]" + linkurl + "[name]" + linktext + "[/name][/" + thetype + "] ";
			AggiungiTesto(AddTxt,theform,Campo);
		} else {
			AddTxt = "[" + thetype + "]" + linkurl + "[name]" + linkurl + "[/name][/" + thetype + "] ";
			AggiungiTesto(AddTxt,theform,Campo);
		}
	}
}

function formatText(theform,thetype,Campo){
	switch (thetype){		
		case "B":
						prompt_text = link_b_prompt;
						prompt_contents = "";
						break;
		case "I":
						prompt_text = link_i_prompt;
						prompt_contents = "";
						break;
		case "U":
						prompt_text = link_u_prompt;
						prompt_contents = "";
						break;
		default : alert("I\'m sure it was great");
	}
	
	linkurl = prompt(prompt_text,prompt_contents);

	if ((linkurl != null) && (linkurl != "")) {
		AddTxt = "[" + thetype + "]" + linkurl + "[/" + thetype + "] ";
		AggiungiTesto(AddTxt,theform,Campo);
	}
}


function setfocus(theform,Campo) {
	theform.elements[Campo].focus();
}
//elimina articolo
function deleteArt(ind){
	if (!confirm(confirm_del)){
		return;
	}
	location.href="priv_ArtDel.asp?ARTI_ID=" + ind;
}
/*controllo modifica articoli*/
function CheckModArt(){
	var c = document.articoli;
	c.action = "/private/priv_ArtAddMod.asp";

	if (c._titolo.value == ""){
		alert(error);
		c._titolo.focus();
		return false;
	}

	if (c._articolo.value == ""){
		alert(error);
		c._articolo.focus();
		return false;
	}

	//if(c._articolo.value.length>500){
	//	alert(desc);
	//	c._articolo.focus();
	//	return false;
	//}
	
	
}


