// JavaScript Document
function init()  {  
	//assegna le funzioni di rollover ai menu con id "navx" dove x è da 1 a nNav
	var tag = "";
	var nNav=1;
	var imgs,j,i;  
	var inputs = Array();
	
	// Loop through all images, check if they contain the class roll  
	for (j=1;j<=nNav;j++) {
		uls=document.getElementById('nav'+j)
		if (uls){
			imgs=uls.getElementsByTagName('img');  
			for(i=0;i<imgs.length;i++){  
				if(/roll/.test(imgs[i].className)){  
					// add the function roll to the parent Element of the image  
					if(tag!='' && imgs[i].getAttribute('name')==tag) {
						sr = imgs[i].src
						ft = sr.substring(sr.lastIndexOf('.'), sr.length);  
						imgs[i].src= sr.replace(ft, '_on'+ft);;
					}else{
						imgs[i].parentNode.onmouseover=function(){roll(this);};  
						imgs[i].parentNode.onmouseout=function(){roll(this);};  
					}
				}  
			}  
		}
	}
	//se c'è il box delle news scorrevole assegna i comportamenti di cambio variabile
	var mc = document.getElementById("marqueecontainer");
	if (mc){
		initializemarquee();
		mc.onmouseover=function(){copyspeed=pausespeed;};
		mc.onmouseout=function(){copyspeed=marqueespeed;};	
	}
	//se c'è una form assegna inizia la validazione o per i cesti il controllo delle quantita
	var formv = document.getElementsByTagName("form");
	for(i=0;i<formv.length;i++)  
	  {
		  var nome = formv[i].getAttribute('id');
		  if(/valida/.test(formv[i].className))
		  {
		  	var valid = new Validation(nome, {immediate : true, onFormValidate : formCallback});
		  	Validation.addAllThese([
							['validate-password', 'La password deve avere pi&ugrave; di 6 caratteri e non pu&ograve; essere \'password\' o uguale al nome utente', {
								minLength : 6,
								notOneOf : ['password','PASSWORD','1234567','0123456'],
								notEqualToField : 'username_'
							}],
							['validate-codfisc', 'Codice fiscale errato', {
								maxLength : 16								
							}],
							['validate-password-confirm', 'La password inserita non coincide con la prima password.', {
								minLength : 0,
								equalToField : 'password_'
							}]
						]);
		  }
		  if(/cesto/.test(formv[i].className))
		  {
			  var idform = nome.replace('form','');
			  inputs[idform] = formv[i].getElementsByTagName("input");
			  a = 0;
			  for(n=0;n<inputs[idform].length;n++)  
	  			{
					if(/qta/.test(inputs[idform][n].className))
					  {
						  inputs[idform][n].onblur=function(){
							  	q=this.getAttribute("id");
								o=document.getElementById(q.replace("quantita","occupa")).value;
								q=q.replace(this.getAttribute("name")+"_","");
								maxn = document.getElementById('max_nprod_form'+q).value;
								verificaTotale(q,maxn,this,a,o);  	
							};
					  a++;
					  }
				}
		  }
		   if (nome=="form_contatti")
		  	formv[i].onsubmit=function(){submit_contatti('result_contattaci','phpmyformmail.php');};
	  }
   //se c'è un'area per la comparsa e scomparsa dei div qui la configura
	div_effetto = document.getElementById("effetto");
	if (div_effetto) {
		links =  div_effetto.getElementsByTagName("a");
		var attivita = new Array();
		var nt = "";
		i=0; 
		
		if(/blind/.test(links[i].className))  {		
			j=links[i].getAttribute('id').replace("toggle","");
			attivita[j] = new Fx.Slide('attivita_'+j);$('toggle'+j).addEvent('click', function(e){
				e = new Event(e);				
				h = this.getAttribute('id').replace("toggle","");
				attivita[h].toggle();
				if (nt!="") attivita[nt].toggle();
				if (h!=nt){
					nt = h;
				}else{
					nt="";
				}
				e.stop();
			});
			attivita[j].hide();
		}
	}
	 
	 //cerca campi con classe "vuota" e gli assegna l'effetto che al focus il campo si vuota
	 inputs = document.getElementsByTagName("input");
	 var initVal = "";
	 for(i=0;i<inputs.length;i++)  
	  {  	 
		 if(/vuota/.test(inputs[i].className))  
		   {
				inputs[i].onfocus=function(){initVal = this.value;this.value="";};  
				inputs[i].onblur=function(){if (this.value=="") this.value=initVal;};
		   }  else inputs[i].onfocus=function(){this.select();}
	  }
	  //alert("init!");
	  document.getElementById("nav1").style.display = "block";
 }  
 
 //funzioni richiamate dalla init
function roll(o)  {  
	var i, isnode, src, ftype, newsrc, nownode; 
	// loop through all childNodes  
	for (i=0;i<o.childNodes.length;i++)  {  
		nownode=o.childNodes[i];  
		// if the node is an element and an IMG set the variable and exit the loop  
		if(nownode.nodeType==1 && /img/i.test(nownode.nodeName))  {  
			isnode=i;  
			break;  
		}  
	}  
	// check src and do the rollover 
	src = o.childNodes[isnode].src;  
	ftype = src.substring(src.lastIndexOf('.'), src.length);  
	if(/_on/.test(src))  {  
		newsrc = src.replace('_on','');  
		disp = "none";
	}else{  
		newsrc = src.replace(ftype, '_on'+ftype);  
		disp = "block";
	}  
	o.childNodes[isnode].src=newsrc;  
	ulin = o.getElementsByTagName("ul");
	if(ulin[0]){
		ulin[0].style.display = disp;
	}
 }  
   
   function verificaTotale(qq,nmax, sel,aa,sp)
   {
	   var totale = 0;
	   formp = document.getElementById("form"+q);
	   inputss = formp.getElementsByTagName("input");
		for(m=0;m<inputss.length;m++)  
	  	{
			if(/qta/.test(inputss[m].className))
			  {
				idd=inputss[m].getAttribute("id");
				oo=idd.replace("quantita","occupa");
				spazio =  document.getElementById(oo).value;
				totale+=(inputss[m].value*spazio);
			  }
		}
		if (totale > nmax || !totale)
		{
			showtrail(200,1,"<b>Attenzione! </b><br />prodotti consentiti: "+nmax+"<br />prodotti inseriti: "+totale+" <br />&Egrave; stato sostituito l'ultimo valore inserito con il massimo consentito dalla confezione scelta");
			sel.value = (nmax-(totale-(sel.value*sp)))/sp;
			setTimeout(function(){sel.focus();sel.select();},10)
		} else {
			hidetrail();	
		}
   }
/* window.onload=function(){  
  init();  
 }  
 */
 /***********************************************
* Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var delayb4scroll=2000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var marqueespeed=1 //Specify marquee scroll speed (larger is faster 1-10)
var pauseit=1 //Pause marquee onMousever (0=no. 1=yes)?

////NO NEED TO EDIT BELOW THIS LINE////////////

var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var actualheight=''

function scrollmarquee(){
if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
else
cross_marquee.style.top=parseInt(marqueeheight)+8+"px"
}

function initializemarquee(){
cross_marquee=document.getElementById("vmarquee")
cross_marquee.style.top=0
marqueeheight=document.getElementById("marqueecontainer").offsetHeight
actualheight=cross_marquee.offsetHeight
if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
cross_marquee.style.height=marqueeheight+"px"
cross_marquee.style.overflow="scroll"
return
}
setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
}
function formCallback(result, form) {
	window.status = "convalida del modulo '" + form.id + "': responso = " + result;
}
function codiceFISCALE(cfins)
   {
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var 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 false;
   return true;
   }
   function CtrPartIVA(PIVA) {
    if (PIVA.length!=11) { 
        return false;
    } else {
        for (i=0;i<PIVA.length;i++) {
            if ((PIVA.charAt(i)<"0")||(PIVA.charAt(i)>"9"))
                return false;
        } 
    }
    return true; 
}
//showtrail
function gettrailobj(idt)
{
	if (document.getElementById) return document.getElementById(idt).style
	else if (document.all) return document.all.trailimagid.style
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	document.onmousemove=""
	document.getElementById('tcont').innerHTML=""
	gettrailobj("trailimageid").visibility="hidden"
	gettrailobj("trailimageid").left=-1000
	gettrailobj("trailimageid").top=0
}


function showtrail(width,height,file)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{
		w=width
		h=height
		followmouse()	
		gettrailobj("trailimageid").visibility="visible"
		gettrailobj("trailimageid").width=w+"px"//gettrailobj("tcont").width//
		gettrailobj("trailimageid").height=gettrailobj("tcont").height//h+"px"
		document.getElementById('tcont').innerHTML=file
		document.onmousemove=followmouse
	}
}


function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{

		var xcoord=20
		var ycoord=20

		if (typeof e != "undefined")
		{
			xcoord+=e.pageX
			ycoord+=e.pageY
		}
		else if (typeof window.event !="undefined")
		{
			xcoord+=truebody().scrollLeft+event.clientX
			ycoord+=truebody().scrollTop+event.clientY
		}

		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

		if (xcoord+w+3>docwidth)
		xcoord=xcoord-w-(20*2)

		if (ycoord-truebody().scrollTop+h>truebody().clientHeight)
		ycoord=ycoord-h-20;

		gettrailobj("trailimageid").left=xcoord+"px"
		gettrailobj("trailimageid").top=ycoord+"px"

	}
	

}


 function submit_contatti(obj) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var edtemail=document.getElementById("edtemail").value;
	var edtmessaggio=document.getElementById("edtmessaggio").value;
	var edtrecipient=document.getElementById("edtrecipient").value;
	var edtsubject=document.getElementById("edtsubject").value;
	
	if(edtemail == ""){ 
		alert("Inserire l'email.");
		return false;
	}
	
	if(!emailPattern.test(edtemail)){	
		alert("L'indirizzo "+ edtemail + " non è corretto.");
		return false; 
		}	
		
	if(edtmessaggio == ""){ 
		alert("Inserire il messaggio.");
		return false;
	}   
	
	var poststr = "email=" + encodeURI( edtemail ) + "&messaggio=" + encodeURI( edtmessaggio ) + "&recipient=" + encodeURI( edtrecipient ) + "&subject=" + encodeURI( edtsubject ) ;
		document.getElementById('result_contattaci').innerHTML = '<div style="width:24px;margin:20px auto"><img src="images/loadinfo.gif" /></div>'; 
		makePOSTRequest('phpmyformmail.php', poststr,'result_contattaci');
}

//end showtrail

// e qui parte tutto
if (window.addEventListener){
	window.addEventListener("load", init, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", init);
}
else if (document.getElementById){
	window.onload=init;
}


