function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "Il codice fiscale dovrebbe essere lungo 16 caratteri";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.I caratteri validi sono le lettere e le cifre.";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return "Inserire un codice fiscale corretto: "+
			"il codice di controllo non corrisponde.";
	return "";
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La partita IVA dovrebbe essere lunga 11 caratteri";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'. I caratteri validi sono le cifre.";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "Inserire una partita IVA corretto: " +
			"il codice di controllo non corrisponde. ";
	return '';
}



function displayage(yr, mon, day, unit, decimal, round){
	
var one_day=1000*60*60*24;
var one_month=1000*60*60*24*30;
var one_year=1000*60*60*24*30*12;
	
	
today=new Date()
var pastdate=new Date(yr, mon-1, day)

var countunit=unit
var decimals=decimal
var rounding=round

finalunit=(countunit=="giorni")? one_day : (countunit=="mesi")? one_month : one_year
decimals=(decimals<=0)? 1 : decimals*10

if (unit!="anni"){
if (rounding=="rounddown")
return Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals;
else
return Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals;
}
else{
yearspast=today.getFullYear()-yr-1
tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
pastdate.setFullYear(today.getFullYear())
pastdate2=new Date(today.getFullYear()-1, mon-1, day)
tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
return yearspast+tail;
}
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Michael Leigeber | http://www.leigeber.com/Licensed under: U.S. Copyright
 */

// form validation function //
function validate_reg_user(form) {
  
  mesi_lettere = new Array();
		mesi_lettere[1] = 'A';
		mesi_lettere[2] = 'B';
		mesi_lettere[3] = 'C';
		mesi_lettere[4] = 'D';
		mesi_lettere[5] = 'E';
		mesi_lettere[6] = 'H';
		mesi_lettere[7] = 'L';
		mesi_lettere[8] = 'M';
		mesi_lettere[9] = 'P';
		mesi_lettere[10] = 'R';
		mesi_lettere[11] = 'S';
		mesi_lettere[12] = 'T';
  
  var username = form.username2.value;
  var password = form.password2.value;
  var confermapassword = form.confermapassword.value;
 
  var cognome = form.cognome.value;
  
  
  var name = form.nome.value;
  var indirizzo = form.indirizzo.value;
 
 var nazione = form.nazione.value;
 var regione = form.regione.value;
 var provincia = form.provincia.value;
 
/* var giornonascita = form.giornonasita.value;
 
		if(parseInt(giornonascita) < 10)
			{
				giornonascita = "0" + giornonascita;
			}
		
	var mesenasita = form.mesenasita.value;
	var annonasita = form.annonasita.value;
	
	//alert(giornonascita + "/" + mesenasita + "/" + annonasita);
	
	eta_utente = displayage(parseInt(annonasita), parseInt(mesenasita), parseInt(giornonascita), "anni", 0, "rounddown");
	
	var stringaconfronto = annonasita.substring(2) + mesi_lettere[parseInt(mesenasita)] + giornonascita;
 
  var cf = form.codicefiscale.value;
  var pi = form.partitaiva.value;

  var estrazionestringacodicefiscale = cf.substring(6, 11);*/

  var cap = form.cap.value;
  var citta = form.citta.value;
  var email = form.email.value;

  var privacy = form.privacy.checked;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  
  if(username == "") {
    inlineMsg('username2','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci un username.</span>',2);
    return false;
  }

  
  if(password == "") {
    inlineMsg('password2','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci una password.</span>',2);
    return false;
  }

  
  if(confermapassword == "") {
    inlineMsg('confermapassword','<span class="testo_interno"><strong>Attenzione</strong><br />Non hai confermato la password.</span>',2);
    return false;
  }
  
  if(confermapassword != password) {
    inlineMsg('confermapassword','<span class="testo_interno"><strong>Attenzione</strong><br />Non hai confermato correttamente la password.</span>',2);
    return false;
  }
 
  
  if(cognome == "") {
    inlineMsg('cognome','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo cognome.</span>',2);
	return false;
  }
  
  if(name == "") {
    inlineMsg('nome','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo nome.</span>',2);
    return false;
  }

//alert(eta_utente);
/* if (parseInt(eta_utente) < 18)
  {
	inlineMsg('giornonasita','<span class="testo_interno"><strong>Attenzione</strong><br />Secondo la data inserita, non sei maggiorenne.</span>',2);
	return false;
  }

if ((cf == "") && (pi == ""))
	{
		inlineMsg('codicefiscale','<span class="testo_interno"><strong>Attenzione</strong><br />Inserici il codice fiscale o la partita IVA.</span>',2);
		return false;
	}
else
	{
		if(cf != "")
			{
				checkcf = ControllaCF(cf);
				if(checkcf != '')
					{
						inlineMsg('codicefiscale','<span class="testo_interno"><strong>Attenzione</strong><br />' + checkcf + '</span>',2);
						return false;
					}
				
				if (stringaconfronto != estrazionestringacodicefiscale)
					{
						inlineMsg('codicefiscale','<span class="testo_interno"><strong>Attenzione</strong><br />la data di nascita inserita non corrisponde al codice fiscale</span>',2);
						return false;
					}
				
			}
		if(pi != "")
			{
				checkpi = ControllaPIVA(pi);
				if(checkpi != '')
					{
						inlineMsg('partitaiva','<span class="testo_interno"><strong>Attenzione</strong><br />' + checkpi + '</span>',2);
						return false;
					}
			}
	}*/
	
  if(indirizzo == "") {
    inlineMsg('indirizzo','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo indirizzo.</span>',2);
    return false;
  }
 
 if(cap == "") {
    inlineMsg('cap','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo cap.</span>',2);
    return false;
  }
 
  if(citta == "") {
    inlineMsg('citta','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la tua citt&agrave;.</span>',2);
    return false;
  }
  
  
  if (nazione == 14)
	{
		if(regione == 0)
			{
				inlineMsg('regione','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la tua regione.</span>',2);
				return false;
			}
		if(provincia == 0)
			{
				inlineMsg('provincia','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la tua provincia.</span>',2);
				return false;
			} 
	}
  
  
  if(email == "") {
    inlineMsg('email','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la tua mail.</span>',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<span class="testo_interno"><strong>Attenzione</strong><br />Hai inserito una mail non valida.</span>',2);
    return false;
  }

  if(!privacy){
	  inlineMsg('privacy','<span class="testo_interno"><strong>Attenzione</strong><br />Non hai accettato le condizioni.</span>');
    return false;
  }
  
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80);
  arrow.src = "images/msg_arrow.gif";
}


