function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Non è stato possibile impostare un gestore di eventi");
  }
}
addEvent(window,'load',init,false)
function init(){
	gohpbrands();
	if (document.getElementById('menusx')){
		 preOn();
		 getChildren();
		}
	if (document.getElementById('hp-offerte')){
		toggleProposte();
		slideOfferte();
		}
	if (document.getElementById('hp-destinazioni')){
		var verFlash=checkFlash('hp-destinazioni');
			verFlash=true;
			if (verFlash==false) {
				document.getElementById('hp-destinazioni').innerHTML="<a href=\"/qg/pages/destinazione.jsp\"><img src=\"/qg/img/noFlashHp.jpg\" /></a>";
				}
		}
	if (document.getElementById('thumbs')){
		qgGallery.setup();
		}
	if (document.getElementById('fcontinenti')){
		optsContinenti=document.getElementById('fcontinenti');
		optsContinenti.onchange=function(){setupOptions('fcontinenti')}
		optsNazioni=document.getElementById('fnazioni');
		optsNazioni.onchange=function(){setupOptions('fnazioni')}
	}
	if (document.getElementById('btn-play-egitto')){
		document.getElementById('btn-play-egitto').onclick=function(){playvideo('/qg/video/video_egitto.html','egitto','590px','370px')}
		}
	if (document.getElementById('btn-play-libia')){
		document.getElementById('btn-play-libia').onclick=function(){playvideo('/qg/video/video_libia.html','videolibia','590px','370px')}

		}
	if (document.getElementById('btn-play-siria')){
		document.getElementById('btn-play-siria').onclick=function(){playvideo('/qg/video/video_siria.html','videosiria','590px','370px')}

		}
	if (document.getElementById('btn-play-cina')){
		document.getElementById('btn-play-cina').onclick=function(){playvideo('/qg/video/video_cina.html','videocina','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-australia')){
		document.getElementById('btn-play-australia').onclick=function(){playvideo('/qg/video/video_australia.html','videoaustralia','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-botswana')){
		document.getElementById('btn-play-botswana').onclick=function(){playvideo('/qg/video/video_botswana.html','videobotswana','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-brasile')){
		document.getElementById('btn-play-brasile').onclick=function(){playvideo('/qg/video/video_brasile.html','videobrasile','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-senegal')){
		document.getElementById('btn-play-senegal').onclick=function(){playvideo('/qg/video/video_senegal.html','videosenegal','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-mali')){
		document.getElementById('btn-play-mali').onclick=function(){playvideo('/qg/video/video_mali.html','videomali','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-argentina')){
		document.getElementById('btn-play-argentina').onclick=function(){playvideo('/qg/video/video_patagonia.html','videopatagonia','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-loira')){
		document.getElementById('btn-play-loira').onclick=function(){playvideo('/qg/video/video_loira.html','videoloira','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-uzbekistan')){
		document.getElementById('btn-play-uzbekistan').onclick=function(){playvideo('/qg/video/video_uzbekistan.html','videouzbekistan','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-yemen')){
		document.getElementById('btn-play-yemen').onclick=function(){playvideo('/qg/video/video_yemen.html','videoyemen','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-caledonia')){
		document.getElementById('btn-play-caledonia').onclick=function(){playvideo('/qg/video/video_nuova_caledonia.html','nuova_caledonia','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-portogallo')){
		document.getElementById('btn-play-portogallo').onclick=function(){playvideo('/qg/video/video_portogallo.html','portogallo','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-romania')){
		document.getElementById('btn-play-romania').onclick=function(){playvideo('/qg/video/video_romania.html','romania','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-india')){
		document.getElementById('btn-play-india').onclick=function(){playvideo('/qg/video/video_india.html','india','590px','370px','scrollbars=no')}

		}
	if (document.getElementById('btn-play-istria')){
		document.getElementById('btn-play-istria').onclick=function(){playvideo('/qg/video/video_istria.html','istria','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-marocco')){
		document.getElementById('btn-play-marocco').onclick=function(){playvideo('/qg/video/video_marocco.html','marocco','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-isole_vergini')){
		document.getElementById('btn-play-isole_vergini').onclick=function(){playvideo('/qg/video/video_isole_vergini.html','isole_vergini','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-islanda')){
		document.getElementById('btn-play-islanda').onclick=function(){playvideo('/qg/video/video_islanda.html','islanda','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-nepal')){
		document.getElementById('btn-play-nepal').onclick=function(){playvideo('/qg/video/video_nepal.html','nepal','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-peru')){
		document.getElementById('btn-play-peru').onclick=function(){playvideo('/qg/video/video_peru.html','peru','590px','370px','scrollbars=no')}
	}
	if (document.getElementById('btn-play-malesia')){
		document.getElementById('btn-play-malesia').onclick=function(){playvideo('/qg/video/video_malesia.html','Malaysia','590px','370px','scrollbars=no')}
	}
	
}

function playvideo(url,nome,w,h,scrollbars){
	currWidth=screen.width;
	currHeight=screen.height;
	wleft=(currWidth/2)-240;
	wtop=(currHeight/2)-240;
	fin=window.open(url,nome,"width="+w+",height="+h+",left="+wleft+",top="+wtop+","+scrollbars);
	}



//gestisce select di destinazioni per scelta continenti e scelta paesi
	var xmlHttp;
	function setupOptions(selects){
		if (selects=='fcontinenti'){
			valore=document.getElementById(selects).value;
			queryServer('getNazioni',valore);
		}
		else if (selects=='fnazioni'){
                    if(!document.getElementById("nolink"))
			location.href=document.getElementById(selects).value;
		}
	}
	function createXMLHttpRequest() {
		if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		}
	}
	function createQueryString(valcn,valnz){
		var queryString = "cnte=" + valcn;
		return queryString;
	}
	function queryServer(selects,valore){
	var valnz=document.getElementById('fnazioni').value;
	switch(selects){
		case "getContinenti" : valcn=valore;valnz=null;
		break;
		case "getNazioni" : valcn=document.getElementById('fcontinenti').value;
		break;
		}

		var url = "/qg/dMapAjax.do?"

		+ createQueryString(valcn);
		//alert(url)
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
	function handleStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			updateList();
			}
		}
	}
	function updateList(){
	var root = xmlHttp.responseXML.getElementsByTagName("root");
	var type = root[0].getAttribute('type');
	if (type=='continenti'){
		var obj=document.getElementById('fcontinenti');
	}
	else if(type=='nazioni'){
		var obj=document.getElementById('fnazioni');
	}
	clearList(obj);
	var items = xmlHttp.responseXML.getElementsByTagName("elemento");
	var option = null;
	//crea una option vuota con indicazione scelta nazione
	optionDefault=document.createElement("option");
			optionDefault.setAttribute('value','');
			optionDefault.appendChild;
			optionDefault.appendChild(document.createTextNode('Seleziona una nazione'));
			obj.appendChild(optionDefault);
		for(var i=0; i<items.length; i++) {
			option=document.createElement("option");
			gotopage=items[i].getAttribute('url');
			option.setAttribute('value',gotopage);
			option.appendChild;
			option.appendChild(document.createTextNode(items[i].firstChild.nodeValue));
			obj.appendChild(option);
		}
		obj.style.display="block";
		var label=document.getElementById('label-nazioni');
		label.style.display="block"
	}
	function clearList(obj){
		while(obj.childNodes.length>0) {
		obj.removeChild(obj.childNodes[0]);
		}
	}

//gestisce note in pagina destinazioni (tour)
function setTogglesEvents(){
			var cprezzi=document.getElementById('partenze-prezzi');
			var btnsToggle=cprezzi.getElementsByTagName('a');
			for (i=0;i<btnsToggle.length;i++){
				if (btnsToggle[i].name=='xopen'){
					cNotes=btnsToggle[i].nextSibling.nextSibling;
					nuovoId="nts"+i;
					cNotes.id=nuovoId;
					btnsToggle[i].onclick=new Function( "toggleNotes("+i+",'mostra');");
				}
				if (btnsToggle[i].name=='xclose'){
					var idDivNote=i-1;// tolgo uno perchè così ottengo id assegnato sopra a box-note (il link xclose viene beccato sempre alla seconda passata del for perchè è il secondo nell'html)
					btnsToggle[i].onclick=new Function( "toggleNotes("+idDivNote+",'nascondi');");
				}
			}
		}
		function toggleNotes(nuovoId,azione){
		var elid="nts"+nuovoId;
		var nodoNotes=document.getElementById(elid);
		if (azione=='mostra'){
			nodoNotes.style.display="block";
			}
		else if (azione=='nascondi'){
			nodoNotes.style.display="none";
			}
		}


//gestisce gallery
var qgGallery = {
	arrThumbs : [],
        arrDown : [],
        arrNames: [],
	path : "/qg/fileViewAction.do?xclass=Multimediafile&field=file&width=400&id=",
        dpath: "/qg/fileViewAction.do?xclass=Multimediafile&field=file&id=",
	fext : "",
	counter : 0,
	setup : function(){
		var thumbs=document.getElementById('thumbs').getElementsByTagName('img');
		var prev=document.getElementById('gall-prev');
		var next=document.getElementById('gall-next');
		for(i=0;i<thumbs.length;i++) {
			idThumb=thumbs[i].id;
                        idDown=thumbs[i].getAttribute("rel");
                        nname=thumbs[i].getAttribute("name");
			this.arrThumbs.push(idThumb);
                        if(idDown)
                          this.arrDown.push(idDown);
                        if(nname)
                          this.arrNames.push(nname);
			thumbs[i].onclick=new Function( "qgGallery.showPics('" + idThumb + "','" + idDown + "','"+nname+"');" );
			prev.onclick=new Function( "qgGallery.showPics('prev','','');");
			next.onclick=new Function( "qgGallery.showPics('next','','');");
		}
	},
	showPics : function(azione,idd,thename){
			var zoom=document.getElementById('zoomed');
                        var down=document.getElementById('gal-down-link');
                        var title1=document.getElementById('gal-title-div');
			if (azione=='next'){
				if (this.counter==this.arrThumbs.length-1){return;}
				qgGallery.counter++;
				zoom.src=this.path+this.arrThumbs[this.counter]+this.fext;
                                if(title1) {
                                  title1.innerHTML=this.arrNames[this.counter];
                                }
                                if(down) {
                                   down.href=this.dpath+this.arrDown[this.counter];
                                }
			}
			else if(azione=='prev'){
			if (this.counter==0){return;}
				this.counter--;
				zoom.src=this.path+this.arrThumbs[this.counter]+this.fext;
                                if(title1) {
                                  title1.innerHTML=this.arrNames[this.counter];
                                }
                                if(down) {
                                   down.href=this.dpath+this.arrDown[this.counter];
                                }
			}
			else {
				zoom.src=this.path+azione+this.fext;
                                if(title1) {
                                  title1.innerHTML=thename;
                                }
                                if(down) {
                                   down.href=this.dpath+idd;
                                }
			}
		}
};

//gestisce rullo per le offerte in HP
var clipSpacer=[0,850,0,0]
var counterOfferte=0;
var completed=false;
var spacerTimerID;
function setSpacer(){
var spacer=document.getElementById('spacer');
	// 103 è l'altezza del div spacer che è quello che appare e scompare
	if (clipSpacer[2]<=103) {
	spacer.style.display='block';
			clipSpacer[2]+=5;
			spacer.style.clip='rect('+clipSpacer.join('px ')+'px)';
			spacerTimerID=window.setTimeout("setSpacer()",30);
			return;
			}
			if (spacerTimerID){
			clearTimeout(spacerTimerID);
			}
if (clipSpacer[0]<=103) {
			clipSpacer[0]+=5;
			spacer.style.clip='rect('+clipSpacer.join('px ')+'px)';
			spacerTimerID=window.setTimeout("setSpacer()",20);
			return;
			}
			if (spacerTimerID){
			clearTimeout(spacerTimerID);
			}

	clipSpacer=[0,850,0,0]

}
function setRullo(){
	var rullo=new Array();
	var elrullo=document.getElementById('hp-offerte').getElementsByTagName('div');
 	for (i=0;i<elrullo.length;i++){
 	if (elrullo[i].className=='chp-offerte'){
		rullo.push(elrullo[i].id);
		}
 }
 return rullo;
}
  function slideOfferte(){
          var rullo=setRullo();
   var nrOfferte=rullo.length;
   if(nrOfferte==1) {
    //non esegue il rullo
    return;
   }
          if (counterOfferte==0) {
                          var coToHide=1;
                          if (completed){ //fa in modo che venga nascosta l'ultima news dopo aver effettuato un loop completo
                             coToHide=nrOfferte-1;
                          }
                  }
          else {
                  var coToHide=(counterOfferte)-1;
                  }
          var coToShow=counterOfferte;
                          setSpacer();
                          offertaToShow=document.getElementById(rullo[coToShow]);
                          offertaToHide=document.getElementById(rullo[(coToHide)]);
                          offertaToShow.style.display='block';
                          offertaToHide.style.display='none';
                          counterOfferte++;

                  if (counterOfferte==nrOfferte){
                          counterOfferte=0;
                          completed=true;
                  }
          window.setTimeout('slideOfferte()',13000);
  }


//gestisce cambi di visualizzazione nel box proposte in HP
function toggleProposte(){
	var giorni=document.getElementById('cproposte-giorni');
	//var tipo=document.getElementById('cproposte-tipologia');
	var prezzo=document.getElementById('cproposte-prezzo');
	var all_links=document.getElementsByTagName('a');
	var clipRett=[0,0,4,0]
	for (i=0;i<all_links.length;i++){
		switch(all_links[i].className) {
		case "goprezzo":
		all_links[i].onclick=function(){
			//tipo.style.display="none";
			giorni.style.display="none";
			prezzo.style.display="block";
                        document.getElementById("searchType").value='1';
			return false;
			}
		break;
		case "gotipo":
		all_links[i].onclick=function(){
			prezzo.style.display="none";
			giorni.style.display="none";
			//tipo.style.display="block";
			return false;
			}
		break;
		case "gogiorni":
		all_links[i].onclick=function(){
			//tipo.style.display="none";
			prezzo.style.display="none";
			giorni.style.display="block";
                        document.getElementById("searchType").value='0';
			return false;
			}
		break;
		}
	}
}

//gestisce cambi di stile nel menu di sx (fa apparire bordo bottom)
function getChildren(){
		var uls=new Array();
	var padre=document.getElementById('menusx');
	var figli=padre.getElementsByTagName("li");
		for (i=0;i<figli.length;i++){
			if (figli[i].className=='on'){
				var ulonpage=figli[i].childNodes;
					for(j=1;j<ulonpage.length;j++){
						if (ulonpage[j].nodeName.toLowerCase()=='ul'){
							uls.push(ulonpage[j]);
						}
					}
				}
			}
		if (uls.length==1) {
			uls[0].style.paddingBottom="18px";
			uls[0].style.backgroundImage="url(../img/interne/bg_menusx_seclevel.gif)";
			uls[0].style.backgroundPosition="right bottom";
			uls[0].style.backgroundRepeat="no-repeat";
		}
		else if (uls.length>1){
			uls[1].style.paddingBottom="18px";
			uls[1].style.backgroundImage="url(../img/interne/bg_menusx_terlevel.gif)";
			uls[1].style.backgroundPosition="right bottom";
			uls[1].style.backgroundRepeat="no-repeat";
		}
}


function gohpbrands(){
	if (document.getElementById('go-hpbrands')){
	var menu=document.getElementById('go-hpbrands');
	menu.onchange=function(){self.location=this.options[this.selectedIndex].value}
	}
}

//gestisce visualizzazione menu top
function preOn(){
	var contmenu=document.getElementById('topmenu');
	var acceso=contmenu.getElementsByTagName("LI");
		for(i=0;i<acceso.length;i++){
			if (acceso[i].previousSibling&&acceso[i].className=='on'){
					if (document.all){
						fratello=acceso[i].previousSibling.className='preon';//questo vale per IE
					}
					else{
						fratello=acceso[i].previousSibling.className='preon';//gecko vede un ulteriore fratello per cui..
						}
				}
			}
	}



function check() {

var formOk=true
var nomeform=document.getElementById('registrazione')
var els = nomeform.elements
for (i=0; i<els.length;i++) {

    if (els[i].value=="" && els[i].type!='checkbox' && els[i].type!='hidden'
        && els[i].type!='button' && els[i].type!='submit') {
        alert("Attenzione: il campo "+els[i].name+"  deve essere compilato");
        els[i].focus();
        els[i].value="";
        formOk=false;
        break;
        }
    if (els[i].name=='mailinglist'&& els['mailinglist'].checked!=true)
            {
            alert("Attenzione Se non accetta l'iscrizione alla newsletter non possiamo inviare il form. E' un campo obbligatorio");
            els['mailinglist'].focus();
            els['mailinglist'].value="";
            formOk=false;
            break;
            }
   if (els[i].name=='privacy'&& els['privacy'].checked!=true)
            {
            alert("Attenzione Se non accetta la privacy non possiamo inviare il form. E' un campo obbligatorio");
            els['mailing'].focus();
            els['mailing'].value="";
            formOk=false;
            break;
            }
        if (els[i].name=="email") {
           var see = els[i].value.indexOf("@");
           var see1 = els[i].value.lastIndexOf("@");
           var see2 = els[i].value.lastIndexOf(".")
              if((see!=-1&&see1!=-1)&&(see==see1)&&see>1&&(see2>see1&&see)) {
                for (k=0;k<els[i].value.length;k++)
                    {
                     switch(els[i].value.charAt(k))
                     {
                         case "*" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "/" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "\\":     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "à" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "é" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "è" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "ò" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case "ì" :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                         case " " :     alert("Attenzione: si stanno inserendo caratteri non validi per un indirizzo email");formOk=false;
                        break;
                    }
                break; //interrompe il ciclo sulla stringa con var k
                }
            }//if
        else {
             alert("Si prega di inserire un indirizzo valido per la mail!");
             els[i].focus();
             els[i].value="";
             formOk=false;
             break;
         }//else
      }//if
    }//chiude ciclo principale
return formOk;
}


//Motore di ricerca

checkAdvanced = function() {
  ret = true;
  f = document.getElementById("fsearch");
  if((f.fromDay.value==-1 || f.fromMonth.value==-1 || f.fromYear.value==-1) &&
      f.codcountry.selectedIndex==0 && f.fromPrice.selectedIndex==0 && f.toPrice.selectedIndex==0
      && f.codtipology.selectedIndex==0) {
    ret = false;
    alert("Attenziona: specifica almeno un criterio di ricerca.")
  }else
    if(f.codcountry.value==-1 || f.codcountry=='' || f.codcountry.value==0) {
    ret = false;
     alert("Attenzione: specificare la nazione di destinazione")

   }
  return ret;
}

checkSimple = function() {
  ret = true;
  f = document.getElementById("fhp");
  returnString = "";
  //ricerca per data
 /* if(f.fromDay.value==-1 || f.fromMonth.value==-1 || f.fromYear.value==-1) {
    ret = false;
    returnString = "Attenzione: specificare correttamente la data di partenza"
  }else if(f.codcountry.value==-1 || f.codcountry=='' || f.codcountry.value==0) {
    ret = false;
    returnString = "Attenzione: specificare la nazione di destinazione"
  }*/


  if(!ret)
    alert(returnString);
  else
    f.submit();

  return ret;
}

function checkFlash(mvc){
var versione=false;
var flashinstalled = 0;
var flashversion = 0;
var isIE=false;
var flash7IE;
if (navigator.plugins && navigator.plugins.length)
{
	x = navigator.plugins["Shockwave Flash"];
	if (x)
	{
		flashinstalled = 2;
		y = x.description;
		flashversion = y.charAt(y.indexOf('.')-1);
	}
	else {
		flashinstalled = 1; //qui flash non è installato
		}
	if (navigator.plugins["Shockwave Flash 2.0"]) //solo per flash 2
		{
			flashinstalled = 2;
			flashversion = 2;
		}
	}
else if (navigator.mimeTypes && navigator.mimeTypes.length) //controllo il myme type per quei browser che nn hanno array di plugin
	{
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin)
			flashinstalled = 2;
		else
			flashinstalled = 1;
	}
else {
	//solo per IE
			try {
					isIE=true;//se entra qui e imposta a true è solo IE
					flash7IE = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
					if (!flash7IE){
						versione=flash7IE;
						}
					else{
						versione=true;
						}
				}
			catch(e){
				versione=false;
				}
	}
// se non è IE....
if (!isIE) {
	flashNonIE=parseInt(flashversion);
		if (flashNonIE>6) {
			versione=true;
			}
			else {
				 versione=flashNonIE;
				 }
	}
	return versione;
}
