// JavaScript Document
var scrollamount=0;
var margine=40;
var start=130;

var nuovomargine=0;
var quantity=0;

var comodo=0;

var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
  }    

function scrolling(){
	scrollamount=window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	if (myHeight>=720){
		setTimeout("scroller()", 700);
	}
}

function scroller(){
	var box=document.getElementById('colonnadx');
	if (scrollamount>130){
		nuovomargine=40+(scrollamount-start);
		quantity=nuovomargine-margine;
		smoothscroll();
		//margine=nuovomargine;
	}else{
		if (Math.round(margine)!=40){
			quantity=(margine-40)*-1;
			smoothscroll();
		}
	}
}

function smoothscroll(){
	var box=document.getElementById('colonnadx');
	if (Math.round(quantity)!=0){
		margine=margine+(quantity/120);
		box.style.marginTop=margine+"px";
		quantity=quantity-(quantity/120);
		setTimeout("smoothscroll();", 100);
	}
}
function focuscontatti(){
	if (document.getElementById('img-nome')){
		document.contact.nome.focus();
	}
}
if (document.attachEvent){
	window.attachEvent("onload",initializemarquee);
	window.attachEvent("onload",preloader);
	window.attachEvent("onscroll",scrolling);
	window.attachEvent("onload",focuscontatti);
		
}else{
	window.addEventListener('load',initializemarquee,false);
	window.addEventListener('load',preloader,false);
	window.addEventListener('scroll',scrolling,false);
	window.addEventListener('load',focuscontatti,false);
}

function colore(nome){
	var el=document.getElementById('colore');
	el.setAttribute("href","templates/"+nome+".css");
//	delCookie('colore');
	setCookie('colore',nome+".css",1);
//	document.location.reload();
}

function dimensione(act){
	var el=document.getElementById('dimensione');
	var el2=document.getElementsByTagName('body');
	var el3=el2[0];
	switch (act) {
		case "=": 
			//el.setAttribute("href","templates/13.css");
			el3.style.fontSize="13px";
			setCookie('dimensione','13',1); 
			break
		case "+": 
			var attuale=getCookie('dimensione');
			if (attuale=="") {
				attuale=13;
			}
			var nuovo=Number(attuale)+1;
			if (nuovo<17){
				//el.setAttribute("href","templates/"+nuovo+".css");
				el3.style.fontSize=nuovo+"px";
				setCookie('dimensione',nuovo,1);
			}
			break
		case "-":
			var attuale=getCookie('dimensione');
			if (attuale=="") {
				attuale=13;
			}
			var nuovo=Number(attuale)-1;
			if (nuovo>9){
				//el.setAttribute("href","templates/"+nuovo+".css");
				el3.style.fontSize=nuovo+"px";
				setCookie('dimensione',nuovo,1);
			}
			break
	}
}
var activebox="pulsanti"
function mostrachat(){
	var pulsanti=document.getElementById('pulsanti');
	var chat=document.getElementById('webchat');
	var box=document.getElementById('callme');
	pulsanti.style.display="none";
	box.style.height="255px";
	chat.style.display="block";
	activebox="chat";
}

function mostrapulsanti(){
	var pulsanti=document.getElementById('pulsanti');
	var chat=document.getElementById('webchat');
	var box=document.getElementById('callme');
	box.style.height="200px";
	chat.style.display="none";
	pulsanti.style.display="block";
	activebox="pulsanti";
}


function setCookie(sNome, sValore, iGiorni) {
  var dtOggi = new Date();
  var dtExpires = new Date();
  dtExpires.setTime
  (dtOggi.getTime() + 24 * iGiorni * 3600000);
  document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}

function delCookie(sNome) {
  setCookie(sNome, "");
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) { 
      return (unescape(asCookie[1]));
	}
  }
  // SE non esiste il cookie richiesto
  return("");
}


/***********************************************
* 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)?
var x=0
////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=138+"px";
marqueeheight=document.getElementById("marqueecontainer").offsetHeight;
actualheight=cross_marquee.offsetHeight;
setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll);
}

function preloader(){
	image1 = new Image();
	image2 = new Image();
	image3 = new Image();
	image4 = new Image();
	image1.src="immagini/add-msn-hover.jpg";
	image2.src="immagini/webchat-hover.jpg";
	image3.src="immagini/add-outlook-hover.jpg";
	image3.src="immagini/ink-hover.jpg";
}
var newsopen=true;
function colonnabox(){
	var el=document.getElementById('news');
	var el2=document.getElementById('marqueecontainer');
	var immagine=document.getElementById('img1');
	if (newsopen==true) {
		el.style.height="25px";
		el2.style.display="none";
		immagine.src='immagini/boxchiuso.gif';
		immagine.alt="Mostra"
		newsopen=false;
	}else{
		el.style.height="200px";
		el2.style.display="block";
		immagine.src='immagini/boxaperto.gif';
		immagine.alt="Nascondi"
		newsopen=true;
	}
}
var utentiopen=true;
function colonnabox2(){
	var el=document.getElementById('utenti');
	var immagine=document.getElementById('img2');
	if (utentiopen==true){
		el.style.height="25px";
		immagine.src='immagini/boxchiuso.gif';
		immagine.alt="Mostra"
		utentiopen=false;
	}else{
		el.style.height="75px";
		immagine.src='immagini/boxaperto.gif';
		immagine.alt="Nascondi"
		utentiopen=true;
	}
}
var callmeopen=true;
function colonnabox3(){
	var el=document.getElementById('callme');
	var el2=document.getElementById('chatbot');
	var immagine=document.getElementById('img3');
	if (callmeopen==true){
		el.style.height="25px";
		el2.style.visibility="hidden";
		immagine.src='immagini/boxchiuso.gif';
		immagine.alt="Mostra"
		callmeopen=false;
	}else{
		if (activebox=="pulsanti"){
			el.style.height="200px";
		}else{
			el.style.height="255px";
		}
		el2.style.visibility="visible";
		immagine.src='immagini/boxaperto.gif';
		immagine.alt="Nascondi"
		callmeopen=true;
	}
}

var testresults
function checkemail(){
var str=document.contact.email.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
testresults=false
}
return (testresults)
}

function inviamodulo() {
	var obbligo=true;
	var mailvalida=true;
	var privacy=true;
	var errimg
	for (i=0; i<document.contact.elements.length-1; i++) {
		var campo=document.contact.elements[i];
		if (campo.name=='nome'||campo.name=='comune'||campo.name=='tel'||campo.name=='email'||campo.name=='richiesta'){
			errimg=document.getElementById('img-'+campo.name);
			if (campo.value==''){
				obbligo=false;
				errimg.src='immagini/errore.jpg';
			}else{
				errimg.src='immagini/erroreok.jpg';
			}
		}
		if (obbligo==true){
			errimg=document.getElementById('img-email');
			if (!checkemail()){
				errimg.src='immagini/errore.jpg';
				mailvalida=false;
			}else{
				errimg.src='immagini/erroreok.jpg';
				mailvalida=true;
			}
		}
		if (obbligo==true&&mailvalida==true){
			var cbox=document.contact.datipersonali;
			if (cbox.checked==false){
				privacy=false;
			}else{
				privacy=true;
			}
		}
	}
	var messaggio=document.getElementById('messaggio');
	if (obbligo==false){
		messaggio.style.backgroundColor="#FFFFCC";
		messaggio.style.border="1px solid #ffcc00";
		messaggio.style.backgroundImage="url(immagini/warning.jpg)";
		messaggio.innerHTML="I campi indicati sono obbligatori.";
		messaggio.style.display="block";
	}else{
		if (mailvalida==false){
			messaggio.style.backgroundColor="#FFFFCC";
			messaggio.style.border="1px solid #ffcc00";
			messaggio.style.backgroundImage="url(immagini/warning.jpg)";
			messaggio.innerHTML="L\'indirizzo e-mail inserito non &egrave; corretto.";
			messaggio.style.display="block";
		}else{
			if (privacy==false){
				messaggio.style.backgroundColor="#FFFFCC";
				messaggio.style.border="1px solid #ffcc00";
				messaggio.style.backgroundImage="url(immagini/warning.jpg)";
				messaggio.innerHTML="Prego fornire il consenso al trattamento dei dati.";
				messaggio.style.display="block";
			}else{
				messaggio.style.display="none";
				setCookie('inviamail','inviare',1);
				document.contact.submit();
			}
		}
	}
}
function ringrazia(){
	var messaggio=document.getElementById('messaggio');
	messaggio.style.display="block";
	messaggio.style.backgroundColor="#d0f7c0";
	messaggio.style.border="1px solid #009036";
	messaggio.style.backgroundImage="url(immagini/ok.jpg)";
	messaggio.innerHTML="Grazie per avermi contattato!";
}


function segnala(id){
	var el=document.getElementById('img-'+id);
	el.src='immagini/errore.jpg';
}
function desegnala(id){
	var el=document.getElementById('img-'+id);
	el.src='immagini/erroreok.jpg';
}

function messaggia(id){
	var messaggio=document.getElementById('messaggio');
	switch (id) {
		case 'obbligo':
			messaggio.style.backgroundColor="#FFFFCC";
			messaggio.style.border="1px solid #ffcc00";
			messaggio.style.backgroundImage="url(immagini/warning.jpg)";
			messaggio.innerHTML="I campi indicati sono obbligatori.";
			messaggio.style.display="block";
		break;
		
		case 'email':
			messaggio.style.backgroundColor="#FFFFCC";
			messaggio.style.border="1px solid #ffcc00";
			messaggio.style.backgroundImage="url(immagini/warning.jpg)";
			messaggio.innerHTML="L\'indirizzo e-mail inserito non &egrave; corretto.";
			messaggio.style.display="block";
		break;
		
		case 'privacy':
			messaggio.style.backgroundColor="#FFFFCC";
			messaggio.style.border="1px solid #ffcc00";
			messaggio.style.backgroundImage="url(immagini/warning.jpg)";
			messaggio.innerHTML="Prego fornire il consenso al trattamento dei dati.";
			messaggio.style.display="block";
		break;
		
		case 'inviato':
			messaggio.style.backgroundColor="#d0f7c0";
			messaggio.style.border="1px solid #009036";
			messaggio.style.backgroundImage="url(immagini/ok.jpg)";
			messaggio.innerHTML="<strong>Modulo inviato</strong>, grazie.";
			messaggio.style.display="block";
		break;
	
	}
}






function xmlhttpPost(strURL) {
	
	var formname="contact"
	var responsediv="messaggio"
	
    var xmlHttpReq = false;
    var self = this;
	var risultato="";
	var pulsante=document.getElementById('pulsante')
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			// Quando pronta, visualizzo la risposta del form
            //updatepage(self.xmlHttpReq.responseText,responsediv);
			
			var campiobbl=new Array('nome','comune','tel','email','richiesta','securitycode')
			var primocampo=false;
			risultato=self.xmlHttpReq.responseText;
			var arr_ris=risultato.split("|");
			if (arr_ris[0]!="") {
				for (i=0; i<campiobbl.length; i++){
					if (arr_ris[0].indexOf(campiobbl[i])==-1){
						desegnala(campiobbl[i]);
					}else{
						segnala(campiobbl[i]);
						if (primocampo==false) {
							var pc=document.contact.elements[campiobbl[i]];
							pc.focus();
							primocampo=true;
						}
						messaggia('obbligo');
						pulsante.value="Invia";
						pulsante.disabled=false;
					}
				}
			}else{
				for (i=0; i<campiobbl.length; i++){
					desegnala(campiobbl[i]);
				}
				if (arr_ris[1]=="mailko"){
					segnala('email');
					messaggia('email');
					pulsante.value="Invia";
					pulsante.disabled=false;
				}else{
					if (arr_ris[2]=="privacyko"){
						messaggia('privacy');
						pulsante.value="Invia";
						pulsante.disabled=false;
					}else{
						messaggia('inviato');
						pulsante.value="Messaggio inviato";
					}
				}
			}
			
			
        }
		else{
			// In attesa della risposta del form visualizzo il msg di attesa
			//updatepage(responsemsg,responsediv);
			pulsante.value="Invio in corso";
			pulsante.disabled=true;

		}
    }
    self.xmlHttpReq.send(getquerystring(formname));
}

function getquerystring(formname) {
    var form = document.forms[formname];
	var qstr = "";

    function GetElemValue(name, value) {
        qstr += (qstr.length > 0 ? "&" : "")
            + escape(name).replace(/\+/g, "%2B") + "="
            + escape(value ? value : "").replace(/\+/g, "%2B");
			//+ escape(value ? value : "").replace(/\n/g, "%0D");
    }
	
	var elemArray = form.elements;
    for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
        var elemType = element.type.toUpperCase();
        var elemName = element.name;
        if (elemName) {
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
					|| elemType == "BUTTON"
					|| elemType == "RESET"
					|| elemType == "SUBMIT"
					|| elemType == "FILE"
					|| elemType == "IMAGE"
                    || elemType == "HIDDEN")
                GetElemValue(elemName, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                GetElemValue(elemName, 
                    element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                GetElemValue(elemName, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        GetElemValue(elemName,
                            option.value ? option.value : option.text);
                }
        }
    }
    return qstr;
}
function updatepage(str,responsediv){
    document.getElementById(responsediv).innerHTML = str;
}

function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
  var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
  var int_windowTop = (screen.height - a_int_windowHeight) / 2;
  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
    }
}
var focused=false;
function cancella(act){
	var el=document.getElementById('campocerca');
	var el2=document.getElementById('invia');
	var val=el.value;
	if (act=='0'){
		if (val=='Cerca...'){
			el.value='';
			el.style.color='#000000';
			el2.style.backgroundPosition="bottom right";
			focused=true;
		}
	}else{
		if (val==''){
			el.value='Cerca...';
			el.style.color='#999999';
			el2.style.backgroundPosition="top right";
			focused=false;
		}
	}
}
function note3(){
	var el=document.getElementById('campocerca');
	var el2=document.getElementById('invia');
	if (el.value=="Cerca..."){
		el.value="";
		el2.style.backgroundPosition="bottom right";
	}
}
function note4(){
	var el=document.getElementById('campocerca');
	var el2=document.getElementById('invia');
	if (focused==false) {
		if (el.value==""){
			el.value="Cerca...";
			el2.style.backgroundPosition="top right";
		}
	}
}
function note5(){
	focused=true;
	var el=document.getElementById('campocerca');
	var el2=document.getElementById('invia');
	el.style.color='#000000';
	el2.style.backgroundPosition="bottom right";
}

function verifica(){
	var el=document.getElementById('campocerca');
	var val=el.value;
	if (val=='Cerca...'){
		el.value='';
	}
}