try{
   req = new XMLHttpRequest();
}
catch (e){
  try{
	   req = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e){
	 try{
		 req = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch (failed){
		req = null;
	}
 }  
}

xreq_dest="additional_content";

request_running=false;
request_busy_cnt=0;

function handleReceiveStat(){            
  play=false;
	switch(req.readyState) {
	case 4:
	if(req.status!=200) {
		request_running=false;
	}else{   
		//alert(req.responseText);
		scroll_y=find_div(xreq_dest).scrollTop;
		//more_debug(scroll_y+" ");
		if (document.getElementById(xreq_dest)) {
			if (document.getElementById(xreq_dest).innerHTML != req.responseText)
				play=true;
			document.getElementById(xreq_dest).innerHTML = req.responseText;
			document.getElementById(xreq_dest).style.display="";
			
			find_div(xreq_dest).scrollTop=scroll_y;
		}
		if (xreq_dest=="additional_content")
		  location.href="#anchor_additional_content";
		xreq_dest="additional_content";
		request_running=false;
	}
	break;
	
	default: 
		return false;
	break;     
  }
	if (document.title.indexOf("Online Tracker")==-1)
	  if (play) play_sound(2);
};

function xrequest(url,destination) {
	document.getElementById(destination).innerHTML = "";
  if (!request_running) {
    debug("url: "+url);
    request_running=true;
    xreq_dest=destination;
		//if (destination!="tracker_btn" && url.indexOf('observer.php')==-1 && url.indexOf('visitor_detail.php')==-1)
		  //document.getElementById(xreq_dest).innerHTML=document.getElementById("loader_span").innerHTML;
    req.open("GET", url, true);
    req.onreadystatechange = handleReceiveStat;
    //"http://"+location.hostname+"/"+
    req.send(" ");
    //alert(url);
  } else {
    window.setTimeout("xrequest('"+url+"','"+destination+"')",1500+Math.round(Math.random()*3000));
	debug("xrequest('"+url+"','"+destination+"')");
	request_busy_cnt++;
	if (request_busy_cnt>100) {
	  request_running=false;
	  request_busy_cnt=0;
	}
  }
}

function xrequest_find_dest(url,dest_start) {
  obj=dest_start;
  while (obj.id.toLowerCase().indexOf("info_content")==-1 && obj.id.toLowerCase().indexOf("center_content")==-1) {
    obj=obj.parentNode;
  }
  xreq_dest=obj.id;
	document.getElementById(xreq_dest).innerHTML=document.getElementById("loader_span").innerHTML;
	if (!request_running) {
		req.open("GET", url, true);
		req.onreadystatechange = handleReceiveStat;
		//"http://"+location.hostname+"/"+
		req.send(" ");
		request_running=true;
	} else {
    window.setTimeout("xrequest('"+url+"','"+xreq_dest+"')",1500+Math.round(Math.random()*3000));
		debug("xrequest('"+url+"','"+destination+"')");
		request_busy_cnt++;
		if (request_busy_cnt>100) {
			request_running=false;
			request_busy_cnt=0;
		}
  }
  //alert(url);
}

function find_div(dest_start) {
  obj=document.getElementById(dest_start);
  if (!obj) return document.body;
  if (obj.id.indexOf("scr_win_")!=-1) return obj;
  elems=obj.getElementsByTagName("div");
  for (i=0;i<elems.length;i++)
    if (elems[i].id.indexOf("scr_win_")!=-1) {
	  return elems[i];
	  break;
	}
  return obj;
}

dblkomma='"';



comma='"';
and='&';
function show_info_window(posx,posy,title,content,parent_name) {
  create_info_window(posx,posy,title,content,parent_name);
  /*parent=document.getElementById(parent_name);
  posy=getypos(parent);
  layer=document.getElementById("info_window");
  layer.style.left=posx+"px";
  layer.style.top=posy+"px";
  document.getElementById("info_title").innerHTML=title;
  document.getElementById("info_content").innerHTML=content;
  layer.style.visibility="visible";*/
}

function hide_info_window(aparent) {
  pobj=document.getElementById(aparent);
  for (i=0;i<info_windows_list.length;i++) {
    if (info_windows_list[i].parent_object==pobj) {
      info_windows_list[i].style.visibility="hidden";
	}
  }
}

info_win_cnt=0;
info_windows_list=Array();

window_indent=0;

function create_info_window(posx,posy,title,content,parent_name) {
  info_win_cnt++;
  new_window = document.createElement("div");
  if (parent_name!="") {
    layer_source=document.getElementById("info_window_small");
    aparent=document.getElementById(parent_name);
    posy=getypos(aparent);
    new_window.parent_object=aparent;
	new_window.manual_moved=false;
	new_window.win_type="small";
  } else {
    layer_source=document.getElementById("info_window");
	new_window.win_type="big";
    new_window.manual_moved=true;
	window_indent+=20;
	posx+=window_indent;
	posy+=window_indent;
  }
  
  new_window.innerHTML=layer_source.innerHTML;
  new_window.id="createdlayer_"+info_win_cnt;
  //new_window.style.classname="movingwin";
  
  new_window.style.position="absolute";
  if (document.all) {
    new_window.style.left=posx;
    new_window.style.top=posy;
    new_window.style.height="30";
  } else {
    new_window.style.left=posx+"px";
    new_window.style.top=posy+"px";
    new_window.style.height="30px";
  }
  new_window.style.zIndex=get_next_z();
  spans=new_window.getElementsByTagName('*');
  for (i=0;i<spans.length;i++) {
    if (spans[i].tagName.toLowerCase()=="span") {
	  spans[i].id="cl_"+info_win_cnt+"_"+spans[i].id;
	}
  }
  document.getElementById("appendshere").appendChild(new_window);
  if (new_window.win_type=="small") {
    document.getElementById("cl_"+info_win_cnt+"_"+"info_content_small").innerHTML=content;
  } else {
    document.getElementById("cl_"+info_win_cnt+"_"+"info_title").innerHTML=title;
	if (content!="")
	  document.getElementById("cl_"+info_win_cnt+"_"+"info_content").innerHTML=content;
  }
  document.getElementById("createdlayer_"+info_win_cnt).style.visibility="visible";
  info_windows_list.push(document.getElementById("createdlayer_"+info_win_cnt));
  force_image_load("createdlayer_"+info_win_cnt);
  adjust_info_windows();
  return "cl_"+info_win_cnt+"_"+"info_content";
}

function big_info_window(title,url) {
	play_sound(2);
  if (!request_running) {
    w=(document.all)?document.body.clientWidth:window.innerWidth;
    //h=(document.all)?window.offsetHeight:window.innerHeight;
    sb=pageOffset();
    px=Math.round((w-800)/2);
    py=Math.round(sb.top+200);
    //((h-500)/2)+
    //alert(h);
    dest=create_info_window(px,py,title,"","");
    xrequest(url,dest);
  }
}

window_intervals=new Array();

function big_info_window_repeat(title,url,sound) {
	if (!sound)
		play_sound(2);
  if (!request_running) {
    w=(document.all)?document.body.clientWidth:window.innerWidth;
    //h=(document.all)?window.offsetHeight:window.innerHeight;
    sb=pageOffset();
    px=Math.round((w-800)/2);
    py=Math.round(sb.top+200);
    //((h-500)/2)+
    //alert(h);
    dest=create_info_window(px,py,title,"","");
    xrequest(url,dest);
    interval=window.setInterval("xrequest('"+url+"','"+dest+"')",10000);
    window_intervals.push(new Array("createdlayer_"+info_win_cnt,interval));
  } else
    window.setTimeout("big_info_window_repeat('"+title+"','"+url+"',true)",1500+Math.random()*3000);
}

force_load_image=null;
function force_image_load(layer_name) {
  if (!document.all) return;
  layer=document.getElementById(layer_name);
  elems=layer.getElementsByTagName('*');
  for (i=0;i<elems.length;i++) {
    if (elems[i].tagName.toLowerCase()=="img") {
	  //alert(elems[i].src);
	  force_load_image=elems[i];
	  setTimeout("force_load_image.src='"+elems[i].src+"'",100);
	}
  }
}

function adjust_info_windows() {
  for (i=0;i<info_windows_list.length;i++) {
    if (!info_windows_list[i].manual_moved)
      info_windows_list[i].style.top=getypos(info_windows_list[i].parent_object)+"px";
  }
}

function close_this_window(obj) {
	play_sound(1);
  while (obj.tagName.toLowerCase()!="div")
    obj=obj.offsetParent;
  obj.style.visibility="hidden";
  obj.innerHTML='';
  s="";
  for (i=0;i<window_intervals.length;i++) {
    s+=window_intervals[i][0]+" : ";
    if (window_intervals[i][0]==obj.id) {
	  //alert("clearing "+window_intervals[i][1]);
	  window.clearInterval(window_intervals[i][1]);
	  window_intervals[i][0]="none";
	}
  }
  window_indent-=20;
}

drag_obj=null;
drag_starting=false;
drag_mx=0;
drag_my=0;
drag_maxz=0;
main_event=null;
draggin=false;
mx=0;
my=0;

function dragging(ev) {
  ev = ev || window.event;
  var mousePos = mouseCoords(ev);
  mx=mousePos.x;
  my=mousePos.y;
  if (drag_obj!=null) {
    debug(drag_mx + " " + drag_my);
	draggin=true;
    drag_obj.style.left=(mousePos.x-drag_mx)+"px";
	drag_obj.style.top=(mousePos.y-3-drag_my)+"px";
  }
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function get_next_z() {
  if (drag_maxz<info_win_cnt+10)
    drag_maxz=info_win_cnt+10;
    drag_maxz++;
  return drag_maxz;
}

function start_drag(obj) {
  if (drag_obj==null) {
    while (obj.tagName.toLowerCase()!="div")
      obj=obj.offsetParent;
    drag_obj=obj;	
    obj.manual_moved=true;
	obx=getxpos(drag_obj);
	oby=getypos(drag_obj);
	drag_mx=mx-obx;
	drag_my=my-oby;
    drag_obj.style.zIndex=get_next_z();
    draggin=true;
  }
}

function drag_to_mouse() {
  drag_mx=50;
  drag_my=5;
}

function focus_window(obj) {
  while (obj.tagName.toLowerCase()!="div")
    obj=obj.offsetParent;
  obj.style.zIndex=get_next_z();
}


function stop_drag(ev) {
  if (draggin) {
    drag_obj=null;
	draggin=false;
  }
}

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function play_sound(no) {
	var m = InternetExplorer ? window.flsound : window.document.embeds["eflsound"];
	if (m)
	  m.GotoFrame(no+3);
}

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

function sendInquiry(fromBtn) {
	form = ge('inquiryForm');
	d = new Date();
	url ="index.php?sendInquiry&id="+form.elements.adId.value+"&email="+encodeURIComponent(form.elements.email.value)+"&name="+encodeURIComponent(form.elements.name.value)+"&phone="+encodeURIComponent(form.elements.phone.value)+"&message="+encodeURIComponent(form.elements.message.value)+
	"&salutation="+(form.elements.salutation.type=="hidden"?form.elements.salutation.value:form.elements.salutation.options[form.elements.salutation.options.selectedIndex].value)+"&visit="+(form.elements.visit && form.elements.visit.checked?"1":"0")+"&moreInfo="+(form.elements.moreInfo.checked?"1":"0")+"&rtime="+d.getTime();
	xrequest(url,"inquiryFormDiv");
	fromBtn.disabled=true;
}

function logout(fromBtn) {
	form = ge('loginForm');
	d = new Date();
	url ="index.php?logout&rtime="+d.getTime();
	xrequest(url,"loginFormDiv");
	fromBtn.disabled=true;
	window.setTimeout('location.href=location.href+"";',1500);
}

function login(fromBtn) {
	form = ge('loginForm');
	d = new Date();
	url ="index.php?login&email="+encodeURIComponent(form.elements.email.value)+"&password="+encodeURIComponent(form.elements.password.value)+"&rememberMe="+(form.elements.rememberMe.checked?"1":"0")+"&rtime="+d.getTime();
	xrequest(url,"loginFormDiv");
	fromBtn.disabled=true;
	//if (document.location.href.indexOf("registerForm")!=-1 || document.location.href.indexOf("details")!=-1 || document.location.href.indexOf("browse")!=-1)
	  window.setTimeout('reloadifLoggedin()',1000);
}

function reloadifLoggedin() {
  if (document.getElementById('loggedinNow') && document.getElementById('loggedinNow').innerHTML == "yes") location.href=location.href+""; else
    window.setTimeout('reloadifLoggedin',2000);
}

function debug(s) {
	
}

function saveSearch() {
	xrequest("index.php?saveSearch","saveSearchLink");
}

function toFavorites(id) {
	xrequest("index.php?toFavorites&id="+id,"listRememberButton_"+id);
	window.setTimeout("getFavorites()",1400);
}
function removeFavorites(id) {
	xrequest("index.php?removeFavorites&id="+id,"listRememberButton_"+id);
}
function changeEquipment(field) {
	xrequest("index.php?getSearchAttributes&immoType="+field.value,"searchEquipment");
}

function getFavorites() {
	xrequest("index.php?myFavorites","myFavorites");
}

function myFavorites() {
	if (ge("myFavorites").innerHTML == "")
	  xrequest("index.php?myFavorites","myFavorites"); else
		  ge("myFavorites").innerHTML = "";
}
function delFavorite(id) {
  xrequest("index.php?myFavorites&del="+id,"myFavorites");
}

function mySearches() {
	if (ge("mySearches").innerHTML == "")
	  xrequest("index.php?mySearches","mySearches"); else
		  ge("mySearches").innerHTML = "";
}

function delSearch(id) {
  xrequest("index.php?mySearches&del="+id,"mySearches"); 
}

function delPic(id) {
	if (confirm("Wollen Sie dieses Bild wirklich löschen?"))
	  xrequest("index.php?delPic="+id,"pic_"+id); 
}

function delNomade(id) {
	if (confirm("Wollen Sie diesen Nomaden wirklich aus der Datenbank entfernen?"))
	  xrequest("index.php?delNomade="+id,"nomadenEntries"); 
}

function openClose(elem,url) {
	ge(elem).style.display = ge(elem).style.display == "none"?"":"none";
	xrequest("index.php?showHideDiv="+elem+"&displayState="+ge(elem).style.display,"");
	if (url) window.setTimeout("callUrl('"+url+"')",500);
}

function callUrl(url) { location.href= url; }