var emailaddress = 'indirizzo email';
function clearNewsletterField() {
	if($F('nl_email') == emailaddress) {
		$('nl_email').value = '';
	}
}

function popolateNewsletterField() {
	if($F('nl_email') == '') {
		$('nl_email').value = emailaddress;
	}
}

/* Controlla se un indirizzo email è valido */
function valideEmail(IdElement) {
	var regExp = '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$';
	return new RegExp(regExp).test($(IdElement).value);
}

/* Iscrive un indirizzo email alla newsletter */
function newsletterSend()
{
	var msg = 0;
	$('nl_error').style.background = 'red';

	if(	$F('nl_email') == emailaddress ||
		$F('nl_email') == '' ||
		!valideEmail('nl_email'))
	{
		msg = 'Inserisci un indirizzo email valido';
	}

	if(msg) {
		error(msg, 'nl_error');
	}
	else {
		hideError('nl_error');
		submitNewsletter();
// 		$('nl_span').innerHTML = 'In invio...';
	}

	return false;
}

/* Linka ad una pagina; nel caso di target=1 apre una nuova finestra */
function link(url, target)
{
	if(target == 1)	window.open(url, "", "");
	else			document.location.href = url;
}


/* Invia la mail dei contatti */
function contactSend() {
	var msg = 0;
	if(!$F('name')) {
		msg = 'Inserisci il tuo nome.';
	}
	if(!$F('email') && !msg) {
		msg = 'Inserisci l\'indirizzo email.';
	}
	if(!msg && !valideEmail('email')) {
		msg = 'Inserisci un indirizzo email corretto.';
	}
	if(!msg && !$F('subject')) {
		msg = 'Inserisci un oggetto alla email.';
	}
	if(!msg && $F('text').length < 60) {
		msg = 'Inserisci un testo di almeno 60 caratteri.';
	}
	if(!msg && $F('text').length > 1000) {
		msg = 'Non puoi inserire un testo maggiore di 1000 caratteri.';
	}
	if(msg)
		error(msg);
	else {
		$('ct_invia').value = '  Email in invio...  ';
		sendContactMail();
	}
	return false;
}

/* Visualizza messaggio d'errore */
function error(msg, errorField) {
	errorField = errorField ? errorField : 'error';
	$(errorField).innerHTML = msg;
	$(errorField).style.display = 'block';
	Effect.Pulsate(errorField);
}

/* Nascondi il messaggio d'errore */
function hideError(errorField) {
	errorField = errorField ? errorField : 'error';
	$(errorField).innerHTML = '';
	$(errorField).style.display = 'none';
}

/* Conta il numero di caratteri nella textarea passata */
function caratteri_disponibili(textarea_id, numero_id, maxlength)
{
	if(!maxlength) maxlength = 500;
	num = maxlength;
	var val = document.getElementById(textarea_id).value;
	var len = maxlength - val.length;
		
	if(len < 0) {
		document.getElementById(textarea_id).value = val.substring(0, maxlength);
		len = 0;
	}

	document.getElementById(numero_id).value = len;
}

/* Conta i caratteri della textarea passata */
function sumTextLen(textarea_id, numero_id)
{
	if(!$(numero_id))
		alert('Inserisci un campo per il contatore');
	var len = $F(textarea_id).length;
	$(numero_id).value = len;
}

/* Iscrizione al concorso Fotografando */
function fotografandoSend() {
	var msg = 0;
	if(
		!$F('Candidato_Nome') ||
		!$F('Candidato_Cognome') ||
		!$F('Scuola_Nome') ||
		!$F('Scuola_Indirizzo') ||
		!$F('Scuola_CAP') ||
		!$F('Scuola_Citta') ||
		!$F('Scuola_Telefono') ||
		!$F('Scuola_Email') ||
		!$F('Professore_Nome') ||
		!$F('Professore_Cognome') ||
		!$F('Professore_Telefono') ||
		!$F('Professore_Cellulare') ||
		!$F('Professore_Email')		
	)	msg = 'Tutti i campi sono obligatori.';
	
	if(msg)
		error(msg);
	else {
		$('fotografando_invia').value = '  Iscrizione in invio...  ';
		sendFotografandoMail();
	}
	return false;
}


