// JavaScript per news che scorrono

// run init when the window loads.....
addEvent(window, "load", init);

function init() {
	if (document.getElementById) {
		tck = document.getElementById("all");
		if (tck.getElementsByTagName("div").length > 0) {
			actual = 0;
			step = 2;
			speed = 30;
			delay = 0;
			news = new Array();
			// build an array of news - eg every seperate div provided in the div with
			// id = "all". 
			for (i = 0; i < tck.getElementsByTagName("div").length; i++) {
				news[i] = tck.getElementsByTagName("div")[i];
				news[i].style.left = tck.offsetWidth;
			}
			// start the news rolling ....
			rollNews();
			// add listeners for when mouse goes over tck to stop and when it leaves 
			// tck to start again
			addEvent(tck, "mouseover", stopNews);
			addEvent(tck, "mouseout", rollNews);
		}
	}
}

function rollNews() {
	// move left edge to left a bit
	news[actual].style.left = parseInt(news[actual].style.left) - step + "px";
	
	if (parseInt(news[actual].style.left) == tck.offsetWidth % step) {
		// if that movement hasnt taken us off the edge of the div then wait
		// a bit and move it again.
		tick = setTimeout("rollNews()",delay);
	}
	else {
		// if it has taken us over the edge then move to the next item in news array
		if (parseInt(news[actual].style.left) <= 0-news[actual].offsetWidth) {
			actual++;
			// if at end of array then knock it back to start
			if (actual == news.length) {actual = 0;}
			news[actual].style.left = tck.offsetWidth;
		}
		// wait a bit and try again.
		tick = setTimeout("rollNews()",speed);
	}
}

function stopNews() {
	clearTimeout(tick);
}

function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}



// controlla il numero di caratteri massimi
//Controllo caratteri massimi nella textarea di inserimento editoriale
function countAreaChars(areaName,counter,limit)
{
if (areaName.value.length>limit)
areaName.value=areaName.value.substring(0,limit);
else
counter.value = limit - areaName.value.length;
}


//conferma eliminazione
function CheckDelete (url,mess)
{
  if (confirm(mess))
  {
   parent.location=url;
  }
  else
  {
    return ("#topic");
  }
}


// anteprima commenti del blog
var nuovariga = /\n/g;
function sostituiscitextdiv() {
var NuovoTesto = document.getElementById("commento").value;
NuovoTesto = NuovoTesto.replace(/</gi, "&lt;");
NuovoTesto = NuovoTesto.replace(/>/gi, "&gt;");
NuovoTesto = NuovoTesto.replace(nuovariga, "<br />");
/*NuovoTesto = NuovoTesto.replace(/\[/g, "<");
NuovoTesto = NuovoTesto.replace(/\]/g, ">"); */
var DivElement = document.getElementById("anteprima");
DivElement.innerHTML = NuovoTesto;
}

// controlla inserimento commenti
function commento_ini() {
	var nome_v = document.getElementById("nome");
	var email_v = document.getElementById("email");
	var commento_v = document.getElementById("commento");
	if (nome_v.value=="") {
		alert("Scrivi il tuo nome e cognome, grazie.");
		nome_v.focus();
		return false;
		} 
	if (email_v.value=="") {
		alert("Scrivi la tua email.");
		email_v.focus();
		return false;
		} 
		else
		{
		if (email_v.value.indexOf('@', 0) == -1 || email_v.value.indexOf('.', 0) == -1 || email_v.value.length < 6)
			{ 
			alert("L'indirizzo e-mail inserito non è valido. Ricontrollalo, grazie.");
			email_v.focus();
			return false;	
			}
		}
	if (commento_v.value=="") {
		alert("Scrivi il tuo commento, grazie.");
		commento_v.focus();
		return false;
		} 
	}
	
// invia ad un amico

function invia(titolo,url){
	var dominio='www.paoloziliani.it/';
	var subjPrefix="Articolo su www.paoloziliani.it: "+titolo;
	var bodyPrefix="Puoi trovare questo articolo all'indirizzo:\n\n";
	var bodyFooter="\n\n----------\nIl blog di Paolo Ziliani: "+dominio;
	var subj=escape(subjPrefix);
	var body=escape(bodyPrefix+"http://"+dominio+url+bodyFooter);
	var popup=window.open("mailto:?subject="+subj+"&body="+body,"mail");
}

// controlla i radio buttom
function radio_button_checker(varform, mess) {
  var radio_choice = false;

  for (var counter=0; counter<varform.length; counter++) {
    if (varform[counter].checked) radio_choice = true; 
  }

  if (!radio_choice) {
    alert(mess);
    return false;
  }
  return true;
}