function addEvent(obj,ev,fn){
	if(obj.addEventListener) {
		// metodo w3c
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) {
		// metodo IE
		obj.attachEvent('on'+ev, fn);
	} else {
		// se i suddetti metodi non sono applicabili
		// se esiste gia' una funzione richiamata da quel gestore evento
		if(typeof(obj['on'+ev])=='function'){
			// salvo in variabile la funzione gia' associata al gestore
			var f=obj['on'+ev];
			// setto per quel gestore una nuova funzione 
			// che comprende la vecchia e la nuova
			obj['on'+ev]=function(){if(f)f();fn()}
		}
		// altrimenti setto la funzione per il gestore
		else obj['on'+ev]=fn;
	}
}

function openWin(content,nome,w,h,isscroll,isresize) { // pagina, larghezza, altezza, se-scrollabile, se-ridimensionabile
	var t = (screen.height) ? (screen.height-h)/2 : 0;
	var l = (screen.width) ? (screen.width-w)/2 : 0; 
	finestra=window.open(content,nome,'scrollbars=' + isscroll + ',resizable=' + isresize + ',width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ',status=no,location=no,toolbar=no');
	finestra.focus();
}

function apriSegnala() {
	sliderSegnala.toggle()	
} 

function aggiungiPreferiti(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function ctrlCheckboxNl(){
	var chk = false;
	mn = document.forms['iscriviti'].menu;
	for (var i=0; i<mn.length; i++) {
		if (mn[i].checked == true) {
			chk = true;
			break;
		} else {
			chk = false;
		}
	}
	if (chk == false){
		alert("Selezionare almeno un settore");
		return false;
	}
}

function selAllNl() {
	frm = document.forms['iscriviti'];
	if (frm.snd.checked == true) {
		v = true;
	} else {
		v = false;
	}
	
	for (var i=0; i<frm.elements.length; i++) {
		frm.elements[i].checked = v;
	}
}

function home() {  
	if(document.all && document.getElementById)
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage('http://www.aostasera.it');
}

function validaCheckbox(groupbox, minchecked, maxchecked){ //nomebox, min check, max check
	e = document.getElementsByName(groupbox);
	var k=0;
	for(var i=0; i<e.length; i++)
		if(e[i].type=="checkbox" && e[i].checked) k++;
		if(minchecked > 1) { c = ' caselle' } else { c = ' casella' };
		if(k>maxchecked) { alert("Puoi selezionare al massimo " + maxchecked + c); return false; }
		if(k<minchecked) { alert("Devi selezionare almeno " + minchecked + c); return false; }
	return true;
}

function stampa(){
	if(window.print){
		window.print();
	} else {
		alert("Il browser non supporta il metodo STAMPA. Seleziona Stampa dal menu File.");
	}
}

function updatePasswordStrength(campo) {
	var password = document.formiscrizione.let_password.value;
	var strength = 0;

	// easy_guesses: strings that should not be used in password
	var easy_guesses = new Array();
	easy_guesses.push('password'); // does this need to be localized?
	easy_guesses.push('youtube');
	var email_words = document.formiscrizione.let_email.value.match(/\w+/g); // contiguous words contained in email
	if (email_words)
		easy_guesses = easy_guesses.concat(email_words);
	if (document.formiscrizione.let_login.value)
		easy_guesses.push(document.formiscrizione.let_login.value);

	locase_matches = password.match(/[a-z_]/g); // lowercase and '_' matches
	digit_matches = password.match(/[0-9]/g);   // numeric matches
	upcase_matches = password.match(/[A-Z]/g);  // uppercase matches
	special_matches = password.match(/\W/g);    // special matches (not in a-z, A-Z, 0-9, _)

	if (password.length>5) {
		// for less than 5, leave strength at 0 since password too short

		// 1 point for each character more than 5
		strength += password.length - 5;

		// 1 point for each upcase character mixed with lowercase
		if (locase_matches && upcase_matches)
			strength += upcase_matches.length;

		// 1 point for each numeric character mixed with lowercase
		if (locase_matches && digit_matches)
			strength += digit_matches.length;

		// 1 point for each special characters
		if (special_matches)
			strength += special_matches.length;

		// 2 bonus points if mix of letters, numbers and special
		if ((locase_matches || upcase_matches) && special_matches && digit_matches)
			strength += 2;
	}

	// Reset strength to 0 if any easy guess in password (easy guess should be more than 3 chars)
	for (var i=0; i < easy_guesses.length; ++i) {
		if (easy_guesses[i].length>3 && (password.indexOf(easy_guesses[i])!=-1)) {
			strength=0;
			break;
		}
	}

	var pstrength_elem = document.getElementById('password_strength');
	var pstrength_text = document.getElementById('password_strength_text');
	if (password.length==0) {
		pstrength_elem.className = 'password_nessuna';
		pstrength_text.innerHTML = 'Nessuna';
	}
	else if (strength<3) {
		pstrength_elem.className = 'password_insufficiente';
		pstrength_text.innerHTML = 'Insufficiente';
	}
	else if (strength<7) {
		pstrength_elem.className = 'password_discreta';
		pstrength_text.innerHTML = 'Discreta';
	}
	else if (strength<10) {
		pstrength_elem.className = 'password_buona';
		pstrength_text.innerHTML = 'Buona';
	}
	else {
		pstrength_elem.className = 'password_ottima';
		pstrength_text.innerHTML = 'Ottima';
	}
}

function clearMessages() {
	Spry.$$("p.msg").forEach(function(campo) { Spry.Utils.setInnerHTML(campo, ''); });
}

function checkLength(campo, maxchars, label, nota) { //campo da contare, nr caratteri consentiti, elemento dove scrivere il numero di caratteri digitati, div per il messaggio
	elemento=document.getElementById(campo.name);
	velemento=elemento.value
    if (velemento.length > maxchars) {
    	elemento.value=velemento.substr(0,maxchars);
    	elemento.blur();
		Spry.Utils.removeClassName(nota,'verde');
		Spry.Utils.addClassName(nota,'rosso');
		Spry.Utils.addClassName(nota,'error');
		Spry.Utils.setInnerHTML(nota, "Numero massimo di caratteri consentito raggiunto");
	} else {
		Spry.Utils.removeClassName(nota,'rosso');
		Spry.Utils.removeClassName(nota,'error');
		Spry.Utils.setInnerHTML(nota, "");
    }
    document.forms[campo.form.name].elements[label].value=elemento.value.length;
}

/* http://www.constile.org/DHTML/ridimensionare_i_caratteri/ */
function setFontSize(size) {
	var testo = document.getElementById('testo');
	var percentuale = "110%"; // default
	if (size == 1) percentuale = "95%";
	if (size == 2) percentuale = "110%";
	if (size == 3) percentuale = "140%";
	testo.style.fontSize = percentuale;
}

// controlla se il tipo di registrazione e' di azienda o privato
function switchTipo() {
	if (document.getElementById('tP').checked) {
		document.getElementById('livprivato').className = 'visibile';
		document.getElementById('livazienda').className = 'invisibile';
		document.getElementById('nome').innerHTML = "Nome";
		document.getElementById('cogn').innerHTML = "Cognome";
		return 'P';
	}
	if (document.getElementById('tA').checked) {
		document.getElementById('livprivato').className = 'invisibile';
		document.getElementById('livazienda').className = 'visibile';
		document.getElementById('nome').innerHTML = "Nome referente";
		document.getElementById('cogn').innerHTML = "Cognome referente";
		return 'A';
	}
}

