  //<![CDATA[
var geocoder = null;
var map = null;
var hya = null;
var hya_point = null; // here you are pont
var polygonOverlay = null;
var hereyouareMarkerOptions = null;
var infoBoxRestaurantId = "";
var gmapsLoaded = false;
var gmapsPagename = null;
var gmapsCanvasname = null;
var TICKETS = new Array();
TICKETS[0] = "<img src=icons/ticket.gif>";
TICKETS[1] = "<img src=icons/sodexo.gif>";
TICKETS[2] = "<img src=icons/cheque.gif>";
TICKETS[3] = "<img src=icons/ucs.gif>";
var CARDS = new Array();
CARDS[0] = "<img src=icons/visa.gif>";
CARDS[1] = "<img src=icons/mc.gif>";
CARDS[2] = "<img src=icons/amex.gif>";
var ownmenu = new Array();
var isIE = document.all;

function setUser(userdata, rload) {
  var dataarr = Array();
  dataarr = userdata.split("&");
  for (i = 0; i < dataarr.length; i++) {
    dataarr[i] = dataarr[i].replace("=","='");
    dataarr[i] = "U"+dataarr[i]+"'";
    eval(dataarr);
  }
  getAjaxInfo("updateuser.php?UserID="+UUserID+"&"+userdata,rload);
}

function getVal(elementName) {
  var obj = document.getElementsByName(elementName);

  if ( (obj[0].type == "radio") ) {
    for (var i = 0; i < obj.length; i++) {
      if (obj[i].checked)
        return obj[i].value;
    }
  } else if ( (obj[0].type == "checkbox") ) {
    for (var i = 0; i < obj.length; i++) {
      if (obj[i].checked)
        return obj[i].checked;
    }
  }

  return obj[0].value;
}

function getObj(elementName) {
  var obj = document.getElementsByName(elementName);
  if( (obj[0].type == "radio") ||
      (obj[0].type == "checkbox") ){
    return obj;
  }
  return obj[0];
}

function isArray(obj) {
  return obj.constructor == Array;
}

function setVal(elementName, elementValue) {
  var obj = getObj(elementName);

  if ( isArray(obj) && 
       ( (obj[0].type == "radio") || 
         (obj[0].type == "checkbox")
       )
     ) {
    for (var i = 0; i < obj.length; i++) {
      obj[i].checked = false;
      if (obj[i].value == elementValue) {
        obj[i].checked = true;
      }
    }    
    return;
  }
  obj.value = elementValue;
  return;  
}

function getFieldValue(elementName) {
  var obj = document.getElementsByName(elementName);
  return obj[0].value;
}

function getStyleById(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
  return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
  return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
  return document.layers[objectId];
    } else {
  return false;
    }
}

function getObjectById(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
  return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
  return document.all(objectId);
    } else if (document.layers && document.layers[objectId]) {
  return document.layers[objectId];
    } else {
  return false;
    }
}

function setCoords() {
  if ( hya_point ) {
	  var dist = getVal("distance");
    //ha ugyan az, akkor nem dolgozunk feleslegesen
      setUser("CoordX="+hya_point.y+'&CoordY='+hya_point.x+'&Dist='+dist+'&UseMap=1');
      if (dist == 250) { map.setZoom(16);
      } else if (dist == 500) { map.setZoom(15);
      } else if (dist == 750) { map.setZoom(15);
      } else if (dist == 1000) { map.setZoom(14);
      } else if (dist > 1000) { map.setZoom(13);
      }
      map.panTo(hya_point);
      map.clearOverlays();
      drawCircle(parseFloat(hya_point.x),parseFloat(hya_point.y),parseFloat(dist));
      // beolvassuk a létező éttermeket és kitesszük őket a térképre a körön belül.
      // ne legyen cookie keres, mert az nem fog jol frissulni
      query = "getRestaurantsByCoords.php?lat="+parseFloat(hya_point.x)+"&long="+parseFloat(hya_point.y)+"&dist="+parseFloat(dist)+"&onlywithmenu="+UOnlyMenus;
      query += "&usemap="+UUseMap+"&order="+UListOrder;
      getAjaxInfo(query);
  } else {
    setUser('UseMap=1');
    alert(TXT["map.nocoords"]);
    return false;
  }
}

function initialize(pagename, canvasname) {
  gmapsPagename = pagename;
  gmapsCanvasname = canvasname;
  if (gmapsPagename == "map") initgmaps(gmapsPagename, gmapsCanvasname);
}

function initgmaps(pagename, canvasname) {
  var init_lat = TXT["map.basex"];
  var init_lng = TXT["map.basey"];

  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById(canvasname));
    map.setCenter(new GLatLng(init_lat, init_lng), 15);
    geocoder = new GClientGeocoder();
    map.addControl(new GSmallMapControl());

    // csinalunk egy masik markert
    var hereyouareIcon = new GIcon(G_DEFAULT_ICON);
		hereyouareIcon.image = "elements/here.png";
		hereyouareIcon.iconSize = new GSize(32, 32);
		hereyouareIcon.iconAnchor = new GPoint(15,31);
		hereyouareIcon.shadow = "";
 		hereyouareMarkerOptions = { icon:hereyouareIcon };
 		
 		if (pagename == "map") {
      GEvent.addListener(map,"click", function(overlay,point) {
        if (overlay == null) {
          hya_point = point;
          setCoords();
        }
      });
    }
    gmapsLoaded = true;
  }
}

function getAddress() {
  var address = getVal("address");
      
  if (geocoder && (address != '')) {
    geocoder.getLatLng(
      ("Hungary, "+address),
      function(point) {
        if (!point) {
          alert(address + " " + TXT["map.noaddress"]);
        } else {
          setUser('Address='+address);
          hya_point = point;
          setCoords();
        }
      }
    );
  }
}

function searchAddress() {
  var address = getVal("address");

  if (geocoder && (address != '')) {
    geocoder.getLatLng(
      ("Hungary, "+address),
      function(point) {
        if (!point) {
          alert(address + " " + TXT["map.noaddress"]);
        } else {
          map.panTo(point);
          map.clearOverlays();
          hya = new GMarker(point,hereyouareMarkerOptions);
          map.addOverlay(hya);
          document.uploadForm.coordx.value = point.y;
          document.uploadForm.coordy.value = point.x;
        }
      }
    );
  }
}

function showSpot() {
  x = getVal("coordx");
  y = getVal("coordy");
  point = new GLatLng(x,y);
  map.panTo(point);
  map.clearOverlays();
  hya = new GMarker(point,hereyouareMarkerOptions);
  map.addOverlay(hya);
}

function drawLine(x1,y1,x2,y2) {
  var polyline = new GPolyline([
    new GLatLng(y1, x1),
    new GLatLng(y2, x2)
  ], "#cc0000", 4);
  map.addOverlay(polyline);
}

function drawCircle(bx,by,r) {
  hya_point = new GLatLng(by,bx);
  hya = new GMarker(hya_point,hereyouareMarkerOptions);
  map.addOverlay(hya);
  if (r <= 2500) {
    coordinates = new Array();
    xd = 1000*111.322;
    yd = 1000*Math.cos(by*3.141592/180)*111.322;
    for (i=0; i <= 24; i++) {
      pntr = (i*15)*3.141592/180;
      coordinates[i] = new GLatLng(by+r/xd*Math.cos(pntr),bx+r/yd*Math.sin(pntr));
    }
    polygonOverlay = new GPolyline(coordinates, "#ff8822", 4);
    map.addOverlay(polygonOverlay);
  }
}

function drawFullCircle(bx,by,r,opacity) {
  coordinates = new Array();
  xd = 1000*111.322;
  yd = 1000*Math.cos(by*3.141592/180)*111.322;
  for (var j=0; j <= 12; j++) {
    pntr = (j*30)*3.141592/180;
    coordinates[j] = new GLatLng(by+r/xd*Math.cos(pntr),bx+r/yd*Math.sin(pntr));
  }
  polygonOverlay = new GPolygon(coordinates, "#000000", 0, 0, "#0000FF", opacity);
  //polygonOverlay = new GPolygon(coordinates, "#f33f00", 5, 1, "#ff0000", 0.2);
  map.addOverlay(polygonOverlay);

}

function scroll_map_to(lat,lng) {
  var point = new GLatLng(lat,lng);

  var colorMarker = new GIcon(G_DEFAULT_ICON);
	colorMarker.image = "markers/b1.gif";
	colorMarker.iconSize = new GSize(16, 32);
	colorMarker.iconAnchor = new GPoint(7,32);
	colorMarker.shadow = "";
	var colorMarkerOptions = { icon:colorMarker };

  var marker = new GMarker(point, colorMarkerOptions);
  map.panTo(point);
  map.clearOverlays();
  map.addOverlay(marker);
  
  if (UCoordX != 0) {
    drawCircle(UCoordY,UCoordX,UDist);
   	drawLine(lng,lat,hya_point.x,hya_point.y);
  }
}

function toggle_info_box(restaurantId, infobox_options) {
  if (!gmapsLoaded) initgmaps(gmapsPagename, gmapsCanvasname);
  // kiirjuk az informaciot az ablakba
  var oi = getObjectById('infobox');
  var si = getStyleById('infobox');
  if ( (si.visibility == 'visible') &&
       (infoBoxRestaurantId == restaurantId) ) {
      si.visibility = 'hidden';
      return;
  }
  si.visibility = 'visible';
 	getAjaxInfo("getRestaurantInfo.php?restaurantId="+restaurantId+"&"+infobox_options);
  infoBoxRestaurantId = restaurantId;
}

function getExtraInfo(restaurantId) {  
  stt(event, "<center><br><br><b>"+TXT["loading"]+"</b><br><br><img src='elements/ajax.gif'></center><br><br>");
 	getAjaxInfo("getExtraInfo.php?restaurantId="+restaurantId);
}

function close_info_box() {
  var si = getStyleById('infobox');
  si.visibility = 'hidden';
  var loaderBox = getStyleById('loaderBox');
  loaderBox.visibility = 'hidden';
}

function setDefaultCoords() {
  if (UAddress != "") {
    setVal("address", UAddress);    
  }
  if (UOnlyMenus == 1) {
    setVal("order", "distance");
  }
  if (UCoordX != 0) {    
    hya_point = new GLatLng(UCoordX,UCoordY);
    map.setCenter(hya_point);
    setVal("distance", UDist);
    if (UOnlyMenus != 1)
      setVal("onlyWithMenu", true);
    else
      setVal("onlyWithMenu", "on");
    if (UDist == 250) { map.setZoom(16);
    } else if (UDist == 500) { map.setZoom(15);
    } else if (UDist == 750) { map.setZoom(15);
    } else if (UDist == 1000) { map.setZoom(14);
    } else if (UDist > 1000) { map.setZoom(13);
    }
    map.clearOverlays();
    drawCircle(UCoordY,UCoordX,UDist);
    query = "getRestaurantsByCoords.php?lat="+UCoordY+"&long="+UCoordX+"&dist="+UDist+"&onlywithmenu="+UOnlyMenus+"&usemap="+UUseMap+"&order="+UListOrder;
    getAjaxInfo(query);
  }
}

function toggleFavoriteFood() {
  UUseFood = UUseFood == 0 ? 1 : 0;
  setUser("UseFood="+UUseFood,true);  
}

function toggleMapFilter() {
  UUseMap = UUseMap == 0 ? 1 : 0;
  setUser("UseMap="+UUseMap,true);
}

function setOnlyWithMenu() {
  if (getVal("onlyWithMenu") == "on")
    setUser("OnlyMenus=0");
  else
    setUser("OnlyMenus=1");
}

function setRestList(restName, restAddress, restMenu) {
	var restData = new Array();
	restData['name'] = restName;
	restData['address'] = restAddress;
	restData['menu'] = restMenu;	
	return restData;
}

function drawDots() {
  var colorMarker = new GIcon(G_DEFAULT_ICON);
	colorMarker.shadow = "";	
	var colorMarkerOptions = { icon:colorMarker };
	var averagex = 0;
	var averagey = 0;
	var opacity = 0.75 / coords.length;
  for (var i=0; i < coords.length; i++) {
    var point = new GLatLng(coords[i]['x'],coords[i]['y']);
    var marker = new GMarker(point, colorMarkerOptions);
    map.addOverlay(marker);
    //drawFullCircle(coords[i]['y'],coords[i]['x'],coords[i]['d'],opacity);
    averagex = averagex + coords[i]['x'];
    averagey = averagey + coords[i]['y'];    
  }
  averagex = averagex / coords.length;
  averagey = averagey / coords.length;
  map.setCenter(new GLatLng(averagex, averagey), 15);
}

function addRestaurantMarker(lat, lng, restaurantId, i) {

  var colorMarker = new GIcon(G_DEFAULT_ICON);
  if (i == undefined)
  	colorMarker.image = "markers/b.gif";
	else
  	colorMarker.image = "markers/b"+i+".gif";
	colorMarker.iconSize = new GSize(16, 32);
	colorMarker.iconAnchor = new GPoint(7,32);
	colorMarker.shadow = "";
	var colorMarkerOptions = { icon:colorMarker, 
		tooltip: restaurantId };

  var point = new GLatLng(lat,lng);
  var marker = new GMarker(point, colorMarkerOptions);

  if (i != undefined) {
    GEvent.addListener(marker,"click", function() {
      toggle_info_box(restaurantId, 'withmenu=1');
    });
  } else {
    GEvent.addListener(marker,"click", function() {
      toggle_info_box(restaurantId, "");
    });
  }

  GEvent.addListener(marker,"mouseover", function() {
    marker.openInfoWindow("<b>"+restList[restaurantId]["name"]+"</b><br>"+restList[restaurantId]["address"]);
  });

  GEvent.addListener(marker,"mouseout", function() {
    marker.closeInfoWindow();
  });
  
  map.addOverlay(marker);
}

function setDefaultFavoriteFood() {
  if (UFood != "") {
    var foodChecks = getObj("food");
    for (i=0; i < foodChecks.length; i++) {    
      if (UFood.indexOf(foodChecks[i].value) >= 0) {
        foodChecks[i].checked = true;
      }
    }
  }
}

function saveFavoriteFood() {
  var foodChecks = getObj("food");
  UFood = "";
  for (i=0; i < foodChecks.length; i++) {    
    if (foodChecks[i].checked) {
      UFood += foodChecks[i].value+"|";
    }
  }
  setUser("UseFood=1&Food="+UFood);
}

function stt(e, txt) {
  var styleObject = getStyleById("toolTip");
  var htmlObject = getObjectById("toolTip");
	var mouseX = 0;
	var mouseY = 0;
  if (!e) e = window.event;
	mouseX = isIE ? (e.clientX + document.documentElement.scrollLeft) : e.pageX;
	mouseY = isIE ? (e.clientY + document.documentElement.scrollTop) : e.pageY;

  htmlObject.innerHTML = txt;
  styleObject.top = 16+mouseY+"px";
  styleObject.left = 16+mouseX+"px";
  styleObject.visibility = "visible";
	return true;
}

function sttc(event, inArr) {
  var i;
  var txt = "";
  for (i=0; i<CARDS.length; i++) {
    if (inArr.charAt(i) == "1") {
      txt += CARDS[i]+" ";
    }
  }
  stt(event, txt);
}

function sttt(event, inArr) {
  var i;
  var txt = "";
  for (i=0; i<TICKETS.length; i++) {
    if (inArr.charAt(i) == "1") {
      txt += TICKETS[i]+" ";
    }
  }
  stt(event, txt);
}

function htt() {
  var tts = getStyleById('toolTip');
  tts.visibility="hidden";
  return false;
}

function checkEnter(e)
{
  var keynum;
  var keychar;
  var numcheck;
  
  if ( window.event ) // IE
	{
  	keynum = e.keyCode;
	}
  else if ( e.which ) // Netscape/Firefox/Opera
	{
	  keynum = e.which;
	}
	if ( keynum == 13 )
    getAddress();  
}

function setFocus(obj)
{
  input = document.getElementById(obj);
  setSelectionRange ( input, input.value.length, input.value.length );
}

function setSelectionRange ( input, selectionStart, selectionEnd ) 
{
  if ( input.setSelectionRange ) 
  {
    input.focus();
    input.setSelectionRange ( selectionStart, selectionEnd );
  }
  else if ( input.createTextRange ) 
  {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function updateAdvertCounter(webAddress) {  
 	getAjaxInfo("updateAdvertCounter.php?webAddress="+webAddress);
}

function checkEmail() {
  var emailFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (document.recommendForm.email.value.length > 0) {
    if (emailFilter.test(document.recommendForm.email.value) != true) { 
      alert ('A beírt e-mail cím helytelen formátumú.');
      return false;
    }
  }
  document.recommendForm.submit();
}
/*
function showobject(obj) {
  obj = getStyleById(obj);
  obj.visibility = "visible";
}
*/
function hideobject(obj) {
  obj = getStyleById(obj);
  obj.display = "none";
}

function showomd(objid) {
  if (ownmenu[objid-1] > 0) {
    getStyleById("ownmenudel"+objid).visibility = "visible";
  }
}

function hideomd(objid) {
  if (ownmenu[objid-1] > 0) {
    getStyleById("ownmenudel"+objid).visibility = "hidden";
  }
}

function scaleownmenu(noscroll) {
  clearTimeout();
  obj = getObjectById("ownmenu");
  newsize = obj.offsetHeight;
  obj2 = getStyleById("ownmenuframe");
  oldsize = Number(obj2.height.replace("px",""));  
  if (oldsize == '') oldsize = 36;
  if (noscroll) {
    obj2.height = ""+newsize+"px";
  /*	window.scrollBy(0,(newsize-oldsize));*/
    return;
  }
  if (obj2.height == '') obj2.height = 36;
  oldsize = Number(obj2.height.replace("px",""));  
  step = newsize-oldsize;
  step = Number(Math.abs(step)/step)*10;
  if (Math.abs(newsize-oldsize) < Math.abs(step)) {
    step = step/10;
  }
  //e = window.event;
  oldsize += step;
  obj2.height = ""+oldsize+"px";
	window.scrollBy(0,step);
  if (oldsize == newsize) {
    return;
  }
  setTimeout('scaleownmenu(0)',10);
}

function showownmenuhelp () {
  menus = 0;
  for (i = 0; i<7; i++) {
    if (ownmenu[i] > 0) menus++;
  }
  if (menus == 0) {
    getStyleById("ownmenuhelp").display = "block";
    getStyleById("ownmenuhelps").display = "none";
  }
}
function hideownmenuhelp () {
  getStyleById("ownmenuhelp").display = "none";
  getStyleById("ownmenuhelps").display = "block";
}

function selectmenu(day, menuid) {  
  if (menuid == 0) menuid = ownmenu[day-1];
  var menuliststyle = getStyleById(""+day+"_"+menuid);
  if (menuid == ownmenu[day-1]){
    menuid = 0;
    ownmenu[day-1] = menuid;
    showownmenuhelp();
    getObjectById("ownmenu_"+day).innerHTML = "&nbsp;";
    getObjectById("ownrestaurant_"+day).innerHTML = "&nbsp;";
    menuliststyle.backgroundColor="";
    menuliststyle.color="";
  } else {
    var oldmenuliststyle = getStyleById(""+day+"_"+ownmenu[day-1]);
    oldmenuliststyle.backgroundColor="";
    oldmenuliststyle.color="";
    menuliststyle.backgroundColor="#7fbc2d";
    menuliststyle.color="white";
    getObjectById("ownmenu_"+day).innerHTML = getObjectById(""+day+"_"+menuid).innerHTML;
    ownmenu[day-1] = menuid;
    hideownmenuhelp();
  }
  getAjaxInfo("updateOwnMenu.php?UserID="+UUserID+"&day="+day+"&menuid="+menuid);
  scaleownmenu();
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

    //]]>
