/*
**********************************************************************************
*                                                                                *
*                                CAMPI OBBLIGATORI                               *
*                                                                                *
**********************************************************************************
*/
// Gestione elementi
function controlla_elemento(nomeCampo) {
	if (nomeCampo == "") {
		alert("I campi contrassegnati con l'asterisco sono obbligatori");
		return false;
	}
}

// Gestione commenti
function controlla_commento(codice) {
	if (codice == "") {
		alert("Il codice č obbligatorio");
		return false;
	}
}

// Gestione messaggi utenti
function controlla_messaggi(nome, messaggio, codice) {
	if (nome == "" || messaggio == "" || codice == "") {
		alert("I campi contrassegnati con l'asterisco sono obbligatori");
		return false;
	}
}


// Iscrizione ML
function controllaMailing(nome,cognome,mail) {

	if(nome == "" || cognome == "" || mail == "") {
		alert("I campi contrassegnati dall'asterisco sono obbligatori");
		return false;
	}
	if(mail != "") {
		if(!(controllaMail(mail))){
			return false;
		}
	}
	
}


/*
**********************************************************************************
*                                                                                *
*                            RIMUOVI FILE CARICATI                               *
*                                                                                *
**********************************************************************************
*/
function rimuoviFile(valore, nomeCampo, numFile) {
	//alert(valore + " - " + nomeCampo + " - " + numFile);
	document.getElementById(['txt_' + nomeCampo + numFile]).value = valore;
}

/*
**********************************************************************************
*                                                                                *
*                              IMPOSTA CHECKBOX                                  *
*                                                                                *
**********************************************************************************
*/
// Imposto il valore dei campi hidden nelle form con upload immagini
function impostaValore(nome, valore) {
	var nomeCampo;
	nomeCampo = "txt_" + nome;

	document.getElementById([nomeCampo]).value = valore;
}

function impostaValore2(indice, valore) {
	var nome = "txt_vis" + indice;
	document.getElementById([nome]).value = valore;
}


/*
**************************************************************
*                                                            *
*                    CONTROLLO CAMPO MAIL                    * 
*                                                            *
**************************************************************
*/
function controllaMail(indirizzo) {
	if (indirizzo.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
		alert('Il campo email ' + indirizzo + ' non ha un formato valido');		
		//indirizzo.focus();		
	} 
	else {
		return true;
	}
}

/*
**********************************************************************************
*                                                                                *
*                   FUNZIONI PER LE CANCELLAZIONE NOTIZIE                        *
*                                                                                *
**********************************************************************************
*/
// Cancellazione Notizie
function elimina() {
	if (!confirm("Procedere con l'eliminazione?")) {
		return false;
	}
}

/*
**********************************************************************************
*                                                                                *
*                            SOSTITUISCI FILE MODULO                             *
*                                                                                *
**********************************************************************************
*/
function caricaModulo() {
	if (!confirm("Attenzione!\nCaricando un nuovo file verrā eliminato quello attuale.\nSi vuole procedere?")) {
		return false;
	}
}

/*
**********************************************************************************
*                                                                                *
*                                ELENCO FILTRATO                                 *
*                                                                                *
**********************************************************************************
*/
// Sezioni
function filtraElenco(url,idfilter,idel,idaz,idlay) {
	var w = window.location.replace(url + "?_idfilter=" + idfilter + "&_idel=" + idel + "&_idaz=" + idaz + "&_idlay=" + idlay);
}


/*
**********************************************************************************
*                                                                                *
*                                CAMPI OBBLIGAORI                                *
*                                                                                *
**********************************************************************************
*/
// Layout Sito
function lay_CSS(descrizione, html_file, css_file) {
	if (descrizione == "" || html_file == "" || css_file == "") {
		alert("I campi contrassegnati dall'asterisco sono obbligatori");
		return false;
	}
}

function lay_Moduli(descrizione, modulo_file) {
	if (descrizione == "" || modulo_file == "") {
		alert("I campi contrassegnati dall'asterisco sono obbligatori");
		return false;
	}
}

/*
**********************************************************************************
*                                                                                *
*                                      POPUP                                     *
*                                                                                *
**********************************************************************************
*/
function apriPostit(idelemento,idlayout,idazione,idallegato) {

	var w = window.open("../includes/div_popup.asp?_idel=" + idelemento + "&_idlay=" + idlayout + "&_idaz=" + idazione + "&_idall=" + idallegato, "Descrizione", "width:200 height:200");
	w.focus();
}

/*
**********************************************************************************
*                                                                                *
*                                   REDIRECT                                     *
*                                                                                *
**********************************************************************************
*/
function setRedir(pagina) {
	var p = pagina;
	var t=setTimeout('pageRedir("' + p + '")',4000);
}

function pageRedir(pagina) {
	document.location.replace(pagina);
}

/*
**********************************************************************************
*                                                                                *
*                           DESTINATARI MAILING LIST                             *
*                                                                                *
**********************************************************************************
*/

//Invio mailinglist
function gestione_inviomailing(destinatari, oggetto) {
	if(destinatari == "" || oggetto == "") {
		alert("I campi contrassegnati dall'asterisco sono obbligatori");
		return false;
	}
}

function popupDestinatari() {
	var w = window.open("rubrica.asp", "_blank", "width=575, height=400, resizable, status, scrollbars");
}

function inserisciMail() {

	opener.document.getElementById("Adestinatari").value = document.getElementById("eledestA").value;
	opener.document.getElementById("CCdestinatari").value = document.getElementById("eledestCC").value;
	opener.document.getElementById("BCCdestinatari").value = document.getElementById("eledestBCC").value;		
	this.close();
	opener.document.getElementById("Adestinatari").focus();
}

function selezionaTutti(cont,valore) {

	var strA = "";
	var strCC = "";
	var strBCC = "";
	var nomeCheck = valore.name;
	var idCheck = valore.id;
	var valChek = "";
	var ynChek = valore.checked;
	
	//alert("nomeCheck: " + nomeCheck + "\nidCheck: " + idCheck + "\nvalChek: " + valChek + "\nynChek: " + ynChek);
		
	if(ynChek) {
		valChek = true;
	}
	else {
		valChek = false;
	}
		//Seleziono tutti i contatti
	switch (nomeCheck) {
		case "A":
			// Svuoto l'array prima di riempirlo
			for(var i=0; i<arrDestinatariA.length; i++) {
				arrDestinatariA[i] = "";
			}
			arrDestinatariA.sort();			
		
			for(i=1; i<parseInt(cont)+1; i++) {
				arrDestinatariA.push(document.getElementById([nomeCheck + 'mail' + i]).value);
				document.getElementById([nomeCheck + 'mail' + i]).checked = valChek;
			}
			
			for(var i=0; i<arrDestinatariA.length; i++) {
				if(strA == "") {
					strA = strA + arrDestinatariA[i];
				}
				else {
					strA = strA + "," + arrDestinatariA[i];		
				}
			}
			if(ynChek) {
				document.getElementById(['eledest' + nomeCheck]).value=strA;			
			}
			else {
				document.getElementById(['eledest' + nomeCheck]).value="";						
			}
		break;

		case "CC":
			// Svuoto l'array prima di riempirlo
			for(var i=0; i<arrDestinatariCC.length; i++) {
				arrDestinatariCC[i] = "";
			}
			arrDestinatariCC.sort();			

			for(i=1; i<parseInt(cont)+1; i++) {
				arrDestinatariCC.push(document.getElementById([nomeCheck + 'mail' + i]).value);
				document.getElementById([nomeCheck + 'mail' + i]).checked = valChek;
			}
			
			for(var i=0; i<arrDestinatariCC.length; i++) {
				if(strCC == "") {
					strCC = strCC + arrDestinatariCC[i];
				}
				else {
					strCC = strCC + "," + arrDestinatariCC[i];		
				}
			}
			if(ynChek) {
				document.getElementById(['eledest' + nomeCheck]).value=strCC;			
			}
			else {
				document.getElementById(['eledest' + nomeCheck]).value="";						
			}

		break;
		
		case "BCC":
			// Svuoto l'array prima di riempirlo
			for(var i=0; i<arrDestinatariBCC.length; i++) {
				arrDestinatariBCC[i] = "";
			}
			arrDestinatariBCC.sort();			
		
			for(i=1; i<parseInt(cont)+1; i++) {
				arrDestinatariBCC.push(document.getElementById([nomeCheck + 'mail' + i]).value);
				document.getElementById([nomeCheck + 'mail' + i]).checked = valChek;
			}
			
			for(var i=0; i<arrDestinatariBCC.length; i++) {
				if(strBCC == "") {
					strBCC = strBCC + arrDestinatariBCC[i];
				}
				else {
					strBCC = strBCC + "," + arrDestinatariBCC[i];		
				}
			}
			if(ynChek) {
				document.getElementById(['eledest' + nomeCheck]).value=strBCC;			
			}
			else {
				document.getElementById(['eledest' + nomeCheck]).value="";						
			}
		break;
	}
}

function aggiungiDestinatari(valore) {
	
	var strA = "";
	var strCC = "";
	var strBCC = "";
	var nomeCheck = valore.name;
	var idCheck = valore.id;
	var valChek = valore.value;
	var ynChek = valore.checked;
	
	//alert("nomeCheck: " + nomeCheck + "\nidCheck: " + idCheck + "\nvalChek: " + valChek + "\nynChek: " + ynChek);
	
	//Gli array sono definiti nella pagina rubrica.asp
	document.getElementById(['tutti' + nomeCheck]).checked = false;
	
	if(ynChek) {
		//Inserisco l'indirizzo nell'array
		switch (nomeCheck) {
			case "A":
				arrDestinatariA.push(valChek);
			break;

			case "CC":
				arrDestinatariCC.push(valChek);
			break;
			
			case "BCC":
				arrDestinatariBCC.push(valChek);
			break;
		}
	}
	else {
		//Tolgo l'indirizzo dall'array
		switch (nomeCheck) {
			case "A":
				for(var i=0; i<arrDestinatariA.length; i++) {
					if(arrDestinatariA[i] == valChek) {
						arrDestinatariA[i] = "";
					}
				}
				arrDestinatariA.sort();			
			break;

			case "CC":
				for(var i=0; i<arrDestinatariCC.length; i++) {
					if(arrDestinatariCC[i] == valChek) {
						arrDestinatariCC[i] = "";
					}
				}
				arrDestinatariCC.sort();			
			break;
			
			case "BCC":
				for(var i=0; i<arrDestinatariBCC.length; i++) {
					if(arrDestinatariBCC[i] == valChek) {
						arrDestinatariBCC[i] = "";
					}
				}
				arrDestinatariBCC.sort();			
			break;
		}		
	}

	//Separo gli elementi con le virgole e inserisco nel campo nascosto
	switch (nomeCheck) {
		case "A":
			for(var i=0; i<arrDestinatariA.length; i++) {
				if(strA == "") {
					strA = strA + arrDestinatariA[i];
				}
				else {
					strA = strA + "," + arrDestinatariA[i];		
				}
			}
			document.getElementById(['eledest' + nomeCheck]).value=strA;
		break;

		case "CC":
			for(var i=0; i<arrDestinatariCC.length; i++) {
				if(strCC == "") {
					strCC = strCC + arrDestinatariCC[i];
				}
				else {
					strCC = strCC + "," + arrDestinatariCC[i];		
				}
			}
			document.getElementById(['eledest' + nomeCheck]).value=strCC;
		break;
		
		case "BCC":
			for(var i=0; i<arrDestinatariBCC.length; i++) {
				if(strBCC == "") {
					strBCC = strBCC + arrDestinatariBCC[i];
				}
				else {
					strBCC = strBCC + "," + arrDestinatariBCC[i];		
				}
			}
			document.getElementById(['eledest' + nomeCheck]).value=strBCC;			
		break;
	}

}

/*
**********************************************************************************
*                                                                                *
*                                      UTENTI                                    *
*                                                                                *
**********************************************************************************
*/
function iscrizione_utente(nome,cognome,email,cap,userid,pwd,pwd2){

	if(nome == "" || cognome == "" || email == "" || userid == "" || pwd == "" || pwd2 == "") {
		alert("I campi contrassegnati dall'asterisco sono obbligatori");
		return false;
	}
	if(email != "" ){
		if(!(controllaMail(email))){
			return false;
		}
	}
	if(cap != "" ){
		if(isNaN(cap)){
			alert("Attenzione, il cap deve essere numerico");
			return false;
		}
	}
	
	if(pwd2 != pwd) {
		alert("Attenzione, le due password non coincidono");
		return false;
	}
}