/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                               MAILING LIST                                                              *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
function controllaMailing(arrayCampi) {

	var arrayValori = new Array();
	var contaFalse = 0;
	arrayValori = arrayCampi;

	for(var x=0; x<arrayValori.length; x++) {
		nomeCampo = arrayValori[x];
		if (js_trim(document.getElementById([nomeCampo]).value) == '') {
			alert('Il campo ' + nomeCampo + ' è obbligatorio');
			contaFalse = contaFalse+1;
		}
		
		if (js_trim(document.getElementById([nomeCampo]).id) == 'mail') {
			if(!(controllaMail(document.getElementById([nomeCampo]).value))) {
				contaFalse = contaFalse+1;
			}
		}
	}
	
	return contaFalse > 0 ?  false : true;
}


function controllaRubrica(conta) {
	
	for (i=1; i<conta+1; i++) {
		if(document.getElementById(['nome' + i]).value == '' || document.getElementById(['cognome' + i]).value == '' || document.getElementById(['cognome' + i]).value == '') {
			alert(genCampiVuoti);
			return false;
		}

		if(document.getElementById(['mail' + i]).value != '') {
			if(!(controllaMail(document.getElementById(['mail' + i])))){
				return false;
			}
		}
	}	
}

//Invio mailinglist
function gestione_inviomailing(destinatari, oggetto) {
	if(destinatari == '' || oggetto == '') {
		alert(genCampiVuoti);
		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;
	}

}
