function gtEl(id) {
  return document.getElementById(id);
}

function FillComboZ() {
  for (i=0;i<setz.length;i+=2) {
    j = setz[i+1];
    k = '';
    while (j.substr(0,6) == '&nbsp;') {
      j = j.substr(6);
      k += String.fromCharCode(160);
    }
    l = k+j;
    qqq = document.CVForm.xgeocombo;
    ooo = new Option(l,setz[i+0]);
    qqq.options[qqq.length] = ooo;
  }
}
function FillCombo(arr,qqq) {
  for (i=0;i<arr.length;i+=2) {
    ooo = new Option(arr[i+1],arr[i+0]);
    qqq.options[qqq.length] = ooo;
  }
}

function DoRadioCombo(idx) {
  var CVF = document.CVForm;
  i = CVF.xgeocount.value;
  if (idx == 100000000) {
    for (j=0;j<i;j++) {
      CVF.elements['xgv'+j].checked = false;
    }
    CVF.xgv99999999.checked = true;
  }
  if (idx == 99999999) {
    for (j=0;j<i;j++) {
      CVF.elements['xgv'+j].checked = false;
    }
    CVF.xgv99999999.checked = true;
    CVF.xgeocombo.value = 0;
    CVF.xgeocombo.disabled = false;
  }
  if (idx < 99999999) {
    if (CVF.elements['xgv'+idx].checked == true) {
      if (CVF.elements['xgvany'+idx].value == 1) {
        for (j=0;j<i;j++) {
          if (CVF.elements['xgvany'+j].value == 0) {
            CVF.elements['xgv'+j].checked = false;
          }
        }
      } else {
        for (j=0;j<i;j++) {
          if (CVF.elements['xgvany'+j].value == 1) {
            CVF.elements['xgv'+j].checked = false;
          }
        }
      }
      if (CVF.xgeocombo) {
        CVF.xgv99999999.checked = false;
        CVF.xgeocombo.value = 0;
        CVF.xgeocombo.disabled = true;
      }
    }
  }
}

function moveDingus(button) {
  if (gtEl('dingus').className != 'searchenginev') {
    return;
  }
  //dingus move ...
  buttonx = 0;
  buttony = 0;
  temp = gtEl(button);
  while (temp) {
    tempuc = temp.tagName.toUpperCase();
    buttonx += temp.offsetLeft;
    buttony += temp.offsetTop;
    if (tempuc == 'BODY') {
      break;
    }
    temp = temp.offsetParent;
  }
  gtEl('dingus').style.left = (buttonx-64)+'px';
  gtEl('dingus').style.top = (buttony-47)+'px';
  gtEl('dingus').className = 'searchenginev';
}

function uncheckBoxes() {
  for (i=0;i<document.CVForm.xtypcount.value;i++) {
  	document.CVForm.elements['xtv'+i].checked = false;
  }
  for (i=0;i<document.CVForm.xicocount.value;i++) {
  	document.CVForm.elements['xiv'+i].checked = false;
  }
}

function TabShowHide(cls1,cls2) {
  uncheckBoxes();
  gtEl('tab1').className=cls1;
  gtEl('tab2').className=cls2;
  gtEl('tab3').className=cls2;
  gtEl('tab6').className=cls2;
  if (gtEl('tab11') && (cls2 == 'searchenginei' || gtEl('maintab1').className == 'searchenginev')) {
    gtEl('tab11').className=cls2;
  }
}

function DoAdvanced() {
  TabShowHide('searchenginei','searchenginev');
  moveDingus('buttonb');
  gtEl('advtab').innerHTML = '<b>Ricerca Avanzata<\/b>';
  if (gtEl('adrtab')) {
    gtEl('adrtab').innerHTML = '<a href="#" onclick="DoSwitchTab(2);return false;">Ricerca Semplice<\/a>';
  }
  if (gtEl('maptab')) {
    gtEl('maptab').innerHTML = '<a href="#" onclick="DoSwitchTab(3);return false;">Cerca per Indirizzo<\/a>';
  }
}

function DoSimple() {
  TabShowHide('searchenginev','searchenginei');
  moveDingus('buttona');
  gtEl('advtab').innerHTML = '<a href="#" onclick="DoAdvanced();return false;">Ricerca Avanzata<\/a>';
  if (gtEl('maintab2').className == 'searchenginev' && gtEl('adrtab')) {
    gtEl('adrtab').innerHTML = '<b>Ricerca Semplice<\/b>';
  }
  if (gtEl('maintab3').className == 'searchenginev' && gtEl('maptab')) {
    gtEl('maptab').innerHTML = '<b>Cerca per Indirizzo<\/b>';
  }
}

wbcanipreload = new Image(39,39);
wbcbackpreload = new Image(204,51);

wbcajax1preload = new Image(20,20);
wbcajax2preload = new Image(20,20);
wbcajax3preload = new Image(20,20);
wbcajax4preload = new Image(20,20);
wbcajax5preload = new Image(20,20);

function PresetDates() {
  ready = 0; var CVF = '';
  if (document) {
    CVF = document.CVForm;
  	if (CVF) {
      if (gtEl('dingus') && CVF.xcheckouty) {
        if (CVF.xgeocombo) {
          FillComboZ();
        }
        FillCombo(setd,CVF.xcheckind);
        FillCombo(setm,CVF.xcheckinm);
        FillCombo(sety,CVF.xcheckiny);
        FillCombo(setd,CVF.xcheckoutd);
        FillCombo(setm,CVF.xcheckoutm);
        FillCombo(sety,CVF.xcheckouty);
        FillCombo(setg,CVF.xnumberguests);
        FillCombo(setr,CVF.xnumberrooms);

dinguscontent = '';
dinguscontent += '<table width="204" cellpadding="4" cellspacing="0" border="0">';
dinguscontent += '<tr style="height:51px">';
dinguscontent += '<td width="44" align="center" valign="top"><img id="wbcani" src="http://www.hotelsby.it/europa/images/1v.gif" width="39" height="39"><\/td>';
dinguscontent += '<td width="152" align="left" valign="top"><img src="http://www.hotelsby.it/europa/images/1v.gif" width="1" height="4"><br><font face="arial" size="2" color="#000000"><b>Stiamo cercando le Strutture Ricettive<\/b><\/font><\/td>';
dinguscontent += '<\/tr>';
dinguscontent += '<\/table>';
dingusobject = gtEl('dingus');
dingusobject.innerHTML = dinguscontent;
wbcanipreload.src = 'http://www.hotelsby.it/europa/images/loading_image_ok2.gif';
wbcbackpreload.src = 'http://www.hotelsby.it/europa/images/loading_shadow.gif';
wbcajax1preload.src = 'http://www.hotelsby.it/europa/images/shared/magnify.gif';
wbcajax2preload.src = 'http://www.hotelsby.it/europa/images/shared/searching.gif';
wbcajax3preload.src = 'http://www.hotelsby.it/europa/images/shared/searchacc.gif';
wbcajax4preload.src = 'http://www.hotelsby.it/europa/images/shared/searchgeo.gif';
wbcajax5preload.src = 'http://www.hotelsby.it/europa/images/shared/noresults.gif';
        ready = 1;
      }
    }
  }  	  	
  if (ready == 0) {
    setTimeout("PresetDates()",1000);
  } else {
    i = CVF.xgeocount.value;
    for (j=0;j<i;j++) {
      if (CVF.elements['xgvany'+j].value == 1) {
        if (CVF.elements['xgv'+j].checked == true) {
          if (CVF.xgeocombo) {
            CVF.xgeocombo.disabled = true;
          }
        }
      }
    }
    if (CVF.xcheckouty.value != 0) {
      return;
    }
    d = new Date(new Date().valueOf()+(67564800000));
    d1 = d.getDate();
    d2 = d.getMonth();
    d3 = d.getFullYear();
    CVF.xcheckind.value = d1;
    CVF.xcheckinm.value = d2+1;
    CVF.xcheckiny.value = d3;
    d = new Date(new Date().valueOf()+ (68169600000));
    d1 = d.getDate();
    d2 = d.getMonth();
    d3 = d.getFullYear();
    CVF.xcheckoutd.value = d1;
    CVF.xcheckoutm.value = d2+1;
    CVF.xcheckouty.value = d3;
    CVF.xnumberguests.value = 2;
    CVF.xnumberrooms.value = 1;
    //DoCreateDynaStuff();
    DoMainTab2();
  }
}

setTimeout("PresetDates()",1000);

function DoMainTab1() {
  gtEl('maintab1').className='searchenginev';
  if (gtEl('maintab2')) {
    gtEl('maintab2').className='searchenginei';
  }
  if (gtEl('maintab3')) {
    gtEl('maintab3').className='searchenginei';
  }
  if (gtEl('tab11')) {
    if (gtEl('tab6').className == 'searchenginev') {
	  gtEl('tab11').className='searchenginev';
    }
	if (gtEl('tab2').style.backgroundColor != '#FAF8E7') {
      gtEl('tab2').style.backgroundColor = '#FAF8E7';
    }
  }
}
function DoMainTab2() {
  gtEl('maintab1').className='searchenginei';
  if (gtEl('maintab3')) {
    gtEl('maintab3').className='searchenginei';
  }
  if (gtEl('maintab2')) {
    gtEl('maintab2').className='searchenginev';
	document.CVForm.ajaxsearch1.focus();
  }
  if (gtEl('tab11')) {
    gtEl('tab11').className='searchenginei';
	if (gtEl('tab2').style.backgroundColor != '#EEE7B9') {
      gtEl('tab2').style.backgroundColor = '#EEE7B9';
    }
  }
  if (gtEl('maptab')) {
    gtEl('maptab').innerHTML = '<a href="#" onclick="DoSwitchTab(3);return false;">Cerca per Indirizzo<\/a>';
  }
  DoSimple();
}
function DoMainTab3() {
  gtEl('maintab1').className='searchenginei';
  if (gtEl('maintab2')) {
    gtEl('maintab2').className='searchenginei';
  }
  if (gtEl('maintab3')) {
    gtEl('maintab3').className='searchenginev';
	document.CVForm.gmapsearch.focus();
  }
  if (gtEl('tab11')) {
    gtEl('tab11').className='searchenginei';
	if (gtEl('tab2').style.backgroundColor != '#EEE7B9') {
      gtEl('tab2').style.backgroundColor = '#EEE7B9';
    }
  }
  if (gtEl('adrtab')) {
    gtEl('adrtab').innerHTML = '<a href="#" onclick="DoSwitchTab(2);return false;">Ricerca Semplice<\/a>';
  }
  DoSimple();
}

function TabHtml(i,is_active) {
  var tab_names = new Array("","Ricerca&nbsp;Hotel","Ricerca&nbsp;per&nbsp;nome","Cerca&nbsp;sulla&nbsp;Mappa");
  if (is_active) {
    var tw = (i == 2) ? 142 : 136;
	return '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="6" bgcolor="#006699" align="left" valign="top"><img src="http://www.hotelsby.com/images/advisor1.gif" width="6" height="15" alt=""><br><img src="http://www.hotelsby.com/images/1v.gif" width="1" height="9" alt=""><\/td><td width="' + tw + '" bgcolor="#006699" align="center" valign="middle"><font face="arial" size="2" color="#FFFFFF"><b>' + tab_names[i] + '<\/b><\/font><\/td><td width="6" bgcolor="#006699" align="right" valign="top"><img src="http://www.hotelsby.com/images/advisor2.gif" width="6" height="15" alt=""><\/td><\/tr><\/table>';
  } else {
    return '<font face="arial" size="2" color="#FFFFFF"><a href="#" onclick="DoSwitchTab('+i+');return false;">' + tab_names[i] + '<\/a><\/font>';
  }
}

function DoSwitchTab(active_id) {
  for (i=1;i<=3;i++) {
    var tobj = gtEl('dynatab'+i);
	if (tobj) {
	  if (active_id == i) {
        tobj.innerHTML = TabHtml(i,1);
      } else {
        tobj.innerHTML = TabHtml(i,0);
      }
    }
  }
  if (active_id == 1) DoMainTab1();
  if (active_id == 2) DoMainTab2();
  if (active_id == 3) DoMainTab3();
  if (window.choosedate) window.choosedate.HideMe();
  return false;
}

function gmlist_hide() {
	gtEl('gmapaddr').innerHTML = '';
	gtEl('gmapaddrfr').style.height = '0px';
}

function DoSearchPower(gmap) {
  var CVF = document.CVForm;
  
  if (!gmap && CVF.gmapcoords) CVF.gmapcoords.value = '';


  maintab = 0;
  if (gtEl('maintab1').className!='searchenginei') {
    maintab = 1;
  }
  if (gtEl('maintab2')) {
  if (gtEl('maintab2').className!='searchenginei') {
    maintab = 2;
  }
  }
  if (gtEl('maintab3')) {
  if (gtEl('maintab3').className!='searchenginei') {
    maintab = 3;
  }
  }
  CVF.maintab.value = maintab;

  if (gtEl('tab1').className!='searchenginei') {
    uncheckBoxes();
  }

  if (maintab <= 1) {
    l = 0;
    for (i=0;i<CVF.xgeocount.value;i++) {
      if (CVF.elements['xgv'+i].checked == true) {
        l = 1;
      }
    }
    if (l == 0) {
      i = CVF.xgeocombo;
	  if (i) {
  	    if (i.options[i.selectedIndex].value == 0) {
  	      alert('L\'Area è un campo obbligatorio');
          i.focus();
          return;
        }
      } else {
        alert('L\'Area è un campo obbligatorio');
        CVF.xgv0.focus();
        return;
      }
    }
  }
  
  if (maintab == 2) {
  	if (CVF.ajaxtype.value == 0) {
        alert('Please search for a hotel or an area first !');
        return;
  	}
  	if (CVF.ajaxgeoid.value < 0) {
  	  alert('Restringi la ricerca - Troppi risultati');
  	  return;
  	}
  }

  i = CVF.xcheckind;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Giorno di Arrivo è un campo obbligatorio');
    i.focus();
    return;
  }

  i = CVF.xcheckinm;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Mese di Arrivo è un campo obbligatorio');
    i.focus();
    return;
  }

  i = CVF.xcheckiny;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Anno di Arrivo è un campo obbligatorio');
    i.focus();
    return;
  }

  iid = CVF.xcheckind;
  iim = CVF.xcheckinm;
  iiy = CVF.xcheckiny;
  jjd = iid.options[iid.selectedIndex].value;
  jjm = iim.options[iim.selectedIndex].value;
  jjy = iiy.options[iiy.selectedIndex].value;
  jjdm = getMaxDay(jjd,jjm,jjy);
  if (jjd > jjdm) {
    alert('La Data di Arrivo deve essere una data valida');
    iid.focus();
    return;
  }
  xx1 = new Date(jjy,jjm-1,jjd,0,0,0);

  xx3 = new Date(2012,1,13,0,0,0);
  if (xx1 < xx3) {
  	alert('La Data di Arrivo deve essere almeno domani');
    return;
  }

  i = CVF.xcheckoutd;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Giorno di Partenza è un campo obbligatorio');
    i.focus();
    return;
  }

  i = CVF.xcheckoutm;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Mese di Partenza è un campo obbligatorio');
    i.focus();
    return;
  }

  i = CVF.xcheckouty;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Anno di Partenza è un campo obbligatorio');
    i.focus();
    return;
  }

  iid = CVF.xcheckoutd;
  iim = CVF.xcheckoutm;
  iiy = CVF.xcheckouty;
  jjd = iid.options[iid.selectedIndex].value;
  jjm = iim.options[iim.selectedIndex].value;
  jjy = iiy.options[iiy.selectedIndex].value;
  jjdm = getMaxDay(jjd,jjm,jjy);
  if (jjd > jjdm) {
    alert('La Data di Partenza deve essere una data valida');
    iid.focus();
    return;
  }
  xx2 = new Date(jjy,jjm-1,jjd,0,0,0);

  xx4 = new Date(2012,1,14,0,0,0);
  if (xx2 < xx4) {
  	alert('La Data di Partenza deve essere almeno domani');
    return;
  }

  jjmm = iim.options[iim.selectedIndex].text;

  if (xx2 <= xx1) {
  	alert('La Data di Partenza deve essere successiva alla Data di Arrivo');
    return;
  }

  xx3 = new Date(2012,1,13,0,0,0);
  dd3 = (xx1 - xx3)/86400000;
  dd3 = Math.round(dd3);

  //if (dd3 <= 2) {
  //  brk = String.fromCharCode(13)+String.fromCharCode(10);
  //  if(confirm('ATTENZIONE ULTIMO MINUTO'+brk+brk+'Davvero vuoi fare check-in entro i prossimi 2 giorni?') == false) {
  //    return;
  //  }
  //}

  dd3 = (xx2 - xx1)/86400000;
  dd3 = Math.round(dd3);
  
  if (dd3 >= 21) {
    brk = String.fromCharCode(13)+String.fromCharCode(10);
    if(confirm('ATTENZIONE SOGGIORNO LUNGO'+brk+brk+'Stai davvero chiedendo un soggiorno di '+dd3+' giorni ?') == false) {
      return;
    }
  }

  xx3 = new Date(2014,1,13,0,0,0);
  if ((xx1 >= xx3) || (xx2 > xx3)) {
    var question1 = 'Sei sicuro di voler cercare per [LINK] ?';
    var question = question1.replace(/\[LINK\]/, jjmm+' '+jjy);
    if (question == question1) question += ' '+jjmm+' '+jjy+' ?';
    
	if (confirm(question) == false) {
      return;
    }
  }

  i = CVF.xnumberguests;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('Numero di Persone è un campo obbligatorio');
    i.focus();
    return;
  }
  
  i = CVF.xnumberkids;
  if (i) {
  j = i.options[i.selectedIndex].value;
  if (j > 0) {
    for (i=1; i<=j; i++) {
      z = CVF.elements['xchildage'+i];
	  if (z && z.options[z.selectedIndex].value >= '0') {
	  } else {
	    alert ('Child '+i+' Age is a required field');
        z.focus();
        return;
      }
	}
  }
  }

  i = CVF.xnumberrooms;
  j = i.options[i.selectedIndex].value;
  if (j == 0) {
    alert ('N. di Camere è un campo obbligatorio');
    i.focus();
    return;
  }

  i1 = CVF.xnumberguests;
  j1 = i1.options[i1.selectedIndex].value;
  i2 = CVF.xnumberrooms;
  j2 = i2.options[i2.selectedIndex].value;
  j1 -=0;
  j2 -=0;
  if (j1 < j2) {
    alert ('Il numero degli ospiti non può essere inferiore al numero delle camere.');
    CVF.xnumberguests.focus();
    return;
  }

  dowarn = 0;
  if ((j1 > 4) && (j2 == 1)) {
  	dowarn = 1;
  }
  if ((j1 > 8) && (j2 == 2)) {
  	dowarn = 1;
  }
  if ((j1 > 12) && (j2 == 3)) {
  	dowarn = 1;
  }
  if (dowarn == 1) {
  	mess = 'Vuoi davvero dire ' + j2;
  	if (j2 == 1) {
      mess = mess + ' camera per ';
  	} else {
      mess = mess + ' camere per ';
    }
    mess = mess + j1 + ' ospiti?';
    if (confirm(mess) == false) {
      return;
    }
  }
  
  if (maintab == 3) {
    if (!gmap) {
		var gmsearch = CVF.gmapsearch.value;
		
		if (!gmsearch) {
		  alert('L\'Area è un campo obbligatorio');
		  return;
		}
		
		
        var xmlHttp;
		try { // Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) { // Internet Explorer
			try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState!=4) return false;
			var xmlResponse = xmlHttp.responseText;
			var answ = eval("("+xmlHttp.responseText+")");

////////////////
			var addh = '';
			var coords = '';
			var addr = '';
			var pointcnt = 0;
			if (answ.Placemark) {
				for (i=0;i<answ.Placemark.length;i++) {
					var pnt = answ.Placemark[i].Point.coordinates;
					if (1) {
					 pointcnt++;
					 coords = '('+pnt[1]+', '+pnt[0]+')';
					 addr = answ.Placemark[i].address;
					 var addr_esc = addr.replace(/\\/g, '\\\\');
					 addr_esc = addr_esc.replace(/'/g, "\\'");
					 addh += '<font size="2" face="Arial"><a href="#" onclick="document.CVForm.gmapcoords.value=\''+coords+'\';document.CVForm.gmapsearchuser.value=document.CVForm.gmapsearch.value;document.CVForm.gmapsearch.value=\''+addr_esc+'\';DoSearchPower(\'gmap\');gmlist_hide();return false;">';
					 addh += (addr.length <= 70) ? addr : addr.substr(0,66)+'...';
					 addh += '<\/a><\/font><br>';
					}
				}
			}
			if (addh) {
				addh = '<table width="442" bgcolor="#FFFFFF" cellspacing="0" cellpadding="4" border="0" style="border: solid #660033 1px"><tr><td>'+addh+'<\/td><\/tr><\/table>';
			} else {
				alert(" Indirizzo non trovato...");
			}
			if (pointcnt > 1) {
			  gtEl('gmapaddr').innerHTML = addh;
			  gtEl('gmapaddrfr').style.height = gtEl('gmapaddr').offsetHeight + 'px';
			} else if (coords) {
			  CVF.gmapcoords.value = coords;
			  CVF.gmapsearchuser.value = CVF.gmapsearch.value;
			  CVF.gmapsearch.value = addr;
              DoSearchPower('gmap');
			}
////////////////
			
		}
		
		xmlHttp.open("GET","/cgi/geoaddr.cgi?wid=15&a="+gmsearch,true);
		xmlHttp.send(null);

		return;
    }

  }
  
  if (maintab == 1) {
    geopass = getPass(CVF.xgeocount,'xgv');
    if (geopass == '') {
      geopass = CVF.xgeocombo.options[CVF.xgeocombo.selectedIndex].text;
    }
    CVF.xgeopass.value = geopass;
  }

  CVF.xtyppass.value = getPass(CVF.xtypcount,'xtv');
  CVF.xicopass.value = getPass(CVF.xicocount,'xiv');
  CVF.xattpass.value = getPass(CVF.xattcount,'xav');

//dingus show ...
  button='buttona';
  if (gtEl('tab1').className=='searchenginei') {
    button='buttonb';
  }
  buttonx = 0;
  buttony = 0;
  temp = gtEl(button);
  while (temp) {
    tempuc = temp.tagName.toUpperCase();
    buttonx += temp.offsetLeft;
    buttony += temp.offsetTop;
    if (tempuc == 'BODY') {
      break;
    }
    temp = temp.offsetParent;
  }
  
  gtEl('dingus').style.left = (buttonx-10)+'px';
  gtEl('dingus').style.top = (buttony-47)+'px';
  gtEl('dingus').className = 'searchenginev';
  CVF.submit();
  gtEl('wbcani').src = wbcanipreload.src;

}

function getPass (inp,nam) {
  var passstr = '';
  for (i=0;i<inp.value;i++) {
  	if (document.CVForm.elements[nam+i].checked == true) {
  	  if (passstr != '') passstr += ', ';
  	  passstr += document.CVForm.elements[nam+'des'+i].value;
  	}
  }
  return passstr;
}

function getMaxDay(jjd,jjm,jjy) {
  if ((jjm == 1) || (jjm == 3) || (jjm == 5) || (jjm == 7) || (jjm == 8) || (jjm == 10) || (jjm == 12)) {
    jjdm = 31;
  }
  if ((jjm == 4) || (jjm == 6) || (jjm == 9) || (jjm == 11)) {
    jjdm = 30;
  }
  if (jjm == 2) {
    jjdm = 28;
    if ((jjy % 4) == 0) {
      jjdm = 29;
      if ((jjy % 100) == 0) {
        jjdm = 28;
        if ((jjy % 400) == 0) {
          jjdm = 29;
        }
      }
    }
  }
  return jjdm;
}

// ajax stuff ...

var xmlRun = false;
var xmlObj = null;
we_use_safari = 0;
if (navigator.userAgent.indexOf('Safari') != -1) {
  we_use_safari = 1;
}

function AJAXSearch(search,show,ajnum_this) {
  if (ajnum_this && ajnum_this < ajnum) {
    return;
  }
  search = search.replace(/\s\s*$/, '');
  if (search == ajlast) {
    return;
  }
  if (xmlRun == true) {
    xmlObj.abort();
    xmlRun = false;
  }
  if (search.length < 3) {
    List_Hide();
    List_Clear();
    currindext = 0;
    currindexg = 0;
    currindexa = 0;
    currindexi = -1;
    currindexm = 0;
    gtEl('ajaximage1').src = wbcajax1preload.src;
    return;
  }
  
  ajlast = search;
  
  if(window.XMLHttpRequest){
    xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
    xmlObj = new ActiveXObject("Msxml2.XMLHTTP");
  } else {
    alert('Sorry, cannot search - no AJAX support on this browser !');
    return;
  }
  var url = 'ht'+'tp://'+'ww'+'w.h'+'otel'+'sby.it/cgi/intajaxsearch1.cgi?webid=15&lanid=2&search='+search+'&nocache'+new Date().getTime();
  xmlObj.onreadystatechange = function() {
    if(xmlObj.readyState == 4){
      gtEl('ajaximage1').src = wbcajax1preload.src;
      var status = 0;
      try {
        if(xmlObj.status !== undefined && xmlObj.status != 0){
          status = xmlObj.status;
        } else{
          status = 9999;
        }
      }
      catch(e){
        status = 9999;
      }
      if (status == '200') {
        var res = xmlObj.responseText;
        if (res == '') {
          List_Hide();
          List_Clear();
          currindext = 0;
          currindexg = 0;
          currindexa = 0;
          currindexi = -1;
          currindexm = 0;
          if (search != '') {
            gtEl('ajaximage1').src = wbcajax5preload.src;
          }
        } else {
          List_Clear();
          List_Fill(res,show);
        }
      }
    }
  }
  xmlObj.open('GET',url,true);
  xmlObj.send('');
  xmlRun = true;
  gtEl('ajaximage1').src = wbcajax2preload.src;
}

function List_Clear() {
  gtEl('ajaxinternal1').innerHTML = '';
}

currindext = 0;
currindexg = 0;
currindexa = 0;
currindexq = '';
currindexi = -1;
currindexm = 0;

function List_Fill(data,show) {
  var o = data.split(';;');

  var didlight = 0;

  currindexm = (o.length/5);
  if (currindexm > 8) { //total height is 200, and 200/24 = 8
  	tabwid = 274;
  	tab2wid = 120;
    overwid = 418;
  } else {
  	tabwid = 292;
  	tab2wid = 120;
  	overwid = 436;
  }

  dh = '';
  dh += '<table width="'+overwid+'" border="0" cellpadding="0" cellspacing="0">';
  for (var i=0;i<o.length;i+=5) {
  	var j = (i/5);
	var parent = '';
	if ((o[i+0] == 2 || o[i+0] == 3 || o[i+0] == 9) && o[i+3].match(/ \(([^\)]*)\)$/)) {
		parent = RegExp.$1;
		o[i+3] = o[i+3].replace(/ \([^\)]*\)$/, '');
	}
	var name_esc = o[i+3].replace(/\\/g, '\\\\');
	name_esc = name_esc.replace(/'/g, "\\'");
	name_esc = name_esc.replace(/"/g, '&quot;');
    dh += '<input type="hidden" id="ajaxlistitemd'+j+'" value="'+o[i+0]+';;'+o[i+1]+';;'+o[i+2]+';;'+o[i+3]+'">';
    dh += '<tr style="height:24px">';
    dh += '<td id="ajaxlistitema'+j+'" width="24" align="left" valign="middle"';
	if ((o[i+0] == currindext) && (o[i+1] == currindexg) && (o[i+2] == currindexa)) {
      dh += ' bgcolor="#EEE7B9"';
      didlight = 1;
      currindexi = j;
    }
	dh += '>';
    if (o[i+0] == 0) {
        dh += '&nbsp;';
    } else {
      if (o[i+0] == 1) {
          dh += '<img src="http://www.hotelsby.it/europa/images/shared/searchacc.gif" width="20" height="20">';
      } else {
    	  dh += '<img src="http://www.hotelsby.it/europa/images/shared/searchgeo.gif" width="20" height="20">';
      }
    }
    dh += '<\/td>';
	dh += '<td id="ajaxlistitemb'+j+'" width="'+tabwid+'" align="left" valign="middle"';
    if ((o[i+0] == currindext) && (o[i+1] == currindexg) && (o[i+2] == currindexa)) {
      dh += ' bgcolor="#EEE7B9"';
    }
	if (o[i+0] == 0 && o[i+4] == '') {
	  dh += ' colspan="2"';
	}
	dh += '>';
    if (o[i+0] > 0) {
	  dh += '<a id="ajaxlistitemc'+j+'" class="';
      if ((o[i+0] == currindext) && (o[i+1] == currindexg) && (o[i+2] == currindexa)) {
        dh += 'link3d';
      } else {
        dh += 'link3';
      }
	  dh += '" href="javascript:DoChoose('+o[i+0]+','+o[i+1]+','+o[i+2]+',\''+name_esc+'\');">';
    }
    dh += '<font size="2" face="Verdana">';
    if (o[i+0] > 0) {
      dh += '<u>';
    }
    dh += o[i+3];
    if (o[i+0] > 0) {
      dh += '<\/u>';
    }
    dh += '<\/font>';
    if (o[i+0] > 0) {
      dh += '<\/a>';
    }
    if (parent) {
      dh += '<font size="1" face="Verdana"> (<nobr>' + parent + '<\/nobr>)<\/font>';
    }
    dh += '<\/td>';
	if (o[i+0] == 0 && o[i+4] == '') {
	} else {
	  dh += '<td id="ajaxlistiteme'+j+'" width="'+tab2wid+'" align="left" valign="middle"';
      if ((o[i+0] == currindext) && (o[i+1] == currindexg) && (o[i+2] == currindexa)) {
        dh += ' bgcolor="#EEE7B9"';
      }
	  dh += '>';
      dh += '<font size="1" face="Verdana" color="#000000">';
      if (o[i+4] == '') {
        dh += '&nbsp;';
      } else {
        dh += o[i+4];
      }
      dh += '<\/font>';
      dh += '<\/td>';
	}
    dh += '<\/tr>';
  }
  dh += '<\/table>';
  gtEl('ajaxinternal1').innerHTML = dh;

  if (didlight == 0) {
    currindext = 0;
    currindexg = 0;
    currindexa = 0;
    currindexq = '';
    currindexi = -1;
  }

  if (show == 1) {
    List_Show();
  }

}

function List_Show() {
  var l = gtEl('ajaxlist1');
  var s = gtEl('ajaxlist2');
  l.style.display = 'block';
  s.style.width = l.style.width;
  s.style.height = l.style.height;
  s.style.left = l.style.left;
  s.style.top = l.style.top;
  s.style.zIndex = l.style.zIndex - 1;
  s.style.display = 'block';
}

function List_Hide() {
  var l = gtEl('ajaxlist1');
  var s = gtEl('ajaxlist2');
  l.style.display = 'none';
  s.style.display = 'none';
  ajlast = '';
}

function Auto_Click() {
  var n = gtEl('ajaxsearch1');
  n.value = '';
  AJAXSearch(n.value,0);
  gtEl('ajaximage1').src = wbcajax1preload.src;
  document.CVForm.ajaxtype.value = 0;
  document.CVForm.ajaxaccid.value = 0;
  document.CVForm.ajaxgeoid.value = 0;
}

moving = 0;

arrowup = 0;
arrowdown = 0;
ajnum = 0;
ajlast = '';

function Auto_Keyup(evt) {
  var code = 0;
  evt = (evt) ? evt : ((event) ? event : null);
  var evver = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null );
  code = evt.keyCode;
  if (code == 38) {
    arrowup = 0;
  }
  if (code == 40) {
    arrowdown = 0;
  }
}

function ajBg(el,bg) {
  if (gtEl(el)) {
    gtEl(el).style.backgroundColor=bg;
  }
}

function Auto_Keydown(evt) {
  var l = gtEl('ajaxlist1');
  var code = 0;
  evt = (evt) ? evt : ((event) ? event : null);
  var evver = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null );
  code = evt.keyCode;
  if (moving == 1) {
    return;
  }
  if ((code == 17 && evt.ctrlKey) || (code == 18 && evt.altKey) || code == 37 || code == 39) return;
  moving = 1;
  if (code == 38) {
    if (we_use_safari) return;
	arrowup = 1;
    while (arrowup == 1) {
    if (l.style.display == 'none') {
      moving = 0;
      return;
    }
    if (currindexi != -1) {
      if (currindexi > 0) {
        ajBg('ajaxlistitema'+currindexi,'#FFFFFF');
		ajBg('ajaxlistiteme'+currindexi,'#FFFFFF');
        ajBg('ajaxlistitemb'+currindexi,'#FFFFFF');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3';
        }
        currindexi--;
        ajBg('ajaxlistitema'+currindexi,'#EEE7B9');
		ajBg('ajaxlistiteme'+currindexi,'#EEE7B9');
        ajBg('ajaxlistitemb'+currindexi,'#EEE7B9');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3d';
        }
        gtEl('ajaxinternal1').scrollTop = (currindexi*24);
        var dataset = gtEl('ajaxlistitemd'+currindexi).value;
        var dataitems = dataset.split(';;');
        currindext = dataitems[0];
        currindexa = dataitems[1];
        currindexg = dataitems[2];
        currindexq = dataitems[3];
      }        
    } else {
      if (currindexm > 0) {
      	currindexi = currindexm-1;
        ajBg('ajaxlistitema'+currindexi,'#EEE7B9');
		ajBg('ajaxlistiteme'+currindexi,'#EEE7B9');
        ajBg('ajaxlistitemb'+currindexi,'#EEE7B9');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3d';
        }
        gtEl('ajaxinternal1').scrollTop = (currindexi*24);
        var dataset = gtEl('ajaxlistitemd'+currindexi).value;
        var dataitems = dataset.split(';;');
        currindext = dataitems[0];
        currindexa = dataitems[1];
        currindexg = dataitems[2];
        currindexq = dataitems[3];
      }      		
    }
    moving = 0;
  	return;
  }
  }
  if (code == 40) {
    if (we_use_safari) return;
	arrowdown = 1;
    while (arrowdown == 1) {
    if (l.style.display == 'none') {
      moving = 0;
      return;
    }
    if (currindexi != -1) {
      if (currindexi < (currindexm-1)) {
        ajBg('ajaxlistitema'+currindexi,'#FFFFFF');
		ajBg('ajaxlistiteme'+currindexi,'#FFFFFF');
        ajBg('ajaxlistitemb'+currindexi,'#FFFFFF');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3';
        }
        currindexi++;
        ajBg('ajaxlistitema'+currindexi,'#EEE7B9');
		ajBg('ajaxlistiteme'+currindexi,'#EEE7B9');
        ajBg('ajaxlistitemb'+currindexi,'#EEE7B9');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3d';
        }
        gtEl('ajaxinternal1').scrollTop = (currindexi*24);
        var dataset = gtEl('ajaxlistitemd'+currindexi).value;
        var dataitems = dataset.split(';;');
        currindext = dataitems[0];
        currindexa = dataitems[1];
        currindexg = dataitems[2];
        currindexq = dataitems[3];
      }        
    } else {
      if (currindexm > 0) {
      	currindexi = 0;
        ajBg('ajaxlistitema'+currindexi,'#EEE7B9');
		ajBg('ajaxlistiteme'+currindexi,'#EEE7B9');
        ajBg('ajaxlistitemb'+currindexi,'#EEE7B9');
        if (gtEl('ajaxlistitemc'+currindexi)) {
          gtEl('ajaxlistitemc'+currindexi).className='link3d';
        }
        gtEl('ajaxinternal1').scrollTop = (currindexi*24);
        var dataset = gtEl('ajaxlistitemd'+currindexi).value;
        var dataitems = dataset.split(';;');
        currindext = dataitems[0];
        currindexa = dataitems[1];
        currindexg = dataitems[2];
        currindexq = dataitems[3];
      }      		
    }
    moving = 0;
  	return;
  }
  }
  if (code == 13) {
    if (l.style.display == 'none') {
      moving = 0;
      return;
    }
    if (currindexi != -1) {
      if (currindext != 0) {
        document.CVForm.ajaxtype.value = currindext;
        document.CVForm.ajaxaccid.value = currindexa;
        document.CVForm.ajaxgeoid.value = currindexg;
        var n = gtEl('ajaxsearch1');
        n.value = currindexq;
        if (currindext == 1) {
          gtEl('ajaximage1').src = wbcajax3preload.src;
        } else {
          gtEl('ajaximage1').src = wbcajax4preload.src;
        }
        List_Hide();
      }
    }
    moving = 0;
    return;
  }
  if (we_use_safari) {
    AJAXSearch(gtEl('ajaxsearch1').value,1);
  } else {
    ajnum++;
    setTimeout("AJAXSearch(gtEl('ajaxsearch1').value,1,"+ajnum+")",500);
  }
  moving = 0;
}

function DoChoose(par1,par2,par3,par4) {
  document.CVForm.ajaxtype.value = par1;
  document.CVForm.ajaxaccid.value = par2;
  document.CVForm.ajaxgeoid.value = par3;
  var n = gtEl('ajaxsearch1');
  n.value = par4;
  if (par1 == 1) {
    gtEl('ajaximage1').src = wbcajax3preload.src;
  } else {
    gtEl('ajaximage1').src = wbcajax4preload.src;
  }
  List_Hide();
}

