//maps.js version 914
//905 adds directions link to popup for sites off-campus
//906 adds tabbed popups
//907 adds jquery and shadowbox eyecandy
//908 adds show-hide code NOT using onOpenFn
//911 remove float from shadowbox link, uppercase 1st letter of each tab
//912 removed building descriptions from info tabs line 218 ish and changed Info tab to Photo tab
//913 Temporarily removed KML link.

//914

function shadowbox(n) {
	Shadowbox.open({content:"./photos/"+n+".jpg",player: "img",title:markers['Building'][markerref[n]].name});
}
function onLoad() {
	preparedata();
	do_menuchoices();
	var startsearch = startsearch || [];
	var startmenu = startmenu || [];
	var startmarkers = startmarkers || [];
	if (startsearch.length>0) onsearch(startsearch);
	else if (startmenu.length>0) do_menu(startmenu);
	else do_popularmenu();
	if (startmarkers.length>0) show_markers(showmarkers);

	if(document.getElementById("map")) {
		if (GBrowserIsCompatible()) {
			map= new GMap2(document.getElementById("map"));
			map.enableScrollWheelZoom();
			map.enableContinuousZoom(); 
			var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(53.3405979, -6.2627649), new GLatLng(53.3465085, -6.2384748)), 13, "© Trinity College Dublin, Base maps © ERA Maptec Ltd");
			var copyrightCollection = new GCopyrightCollection('TCD Map');
			copyrightCollection.addCopyright(copyright);
			var tilelayers = [new GTileLayer(copyrightCollection , 0, 18)];
			tilelayers[0].getTileUrl = function(a,b){ return "tcd_map/"+b+"/"+a.x+"-"+a.y+".png"; }
			var custommap = new GMapType(tilelayers, new GMercatorProjection(20), "TCD Map", {errorMessage:" "});
			map.addMapType(custommap);
			map.setCenter(STARTPOS, 17, custommap);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
		}
	    else {
			alert("Sorry, the Google Maps API is not compatible with this browser");
	    }
	}
}

//to be databased
var popularplaces=[56,59,198];
var popularnames=["Old Library","Campanile","Samuel Beckett Theatre"];

var mainCampus=new GLatLngBounds(new GLatLng(53.34,-6.26),new GLatLng(53.346,-6.2499));

//globals
var map;
var http = getHTTPObject();
function httpexists(url) {
	exists=false;
	http.open("GET",'httpexists-proxy.php?httpexistsurl=' + url, false);
	//http.open("GET", url, false);
	http.send(null);
	if (http.status == 200) {
		exists=true;
	}
	return exists;
}
//associative arrays of objects
var markers=[];
var markerref=[]; //ref back from building table numbering
var markertypes=new Array('Building','Parking','Entrances','Loops','Enabled_toilets','Ramps');
var screenmarkertypes=new Array('Building','Parking','Entrances','Hearing aid loops','Enabled toilets','Accessible entrances');
for (i=0;i<markertypes.length;i++) { 
	markers[markertypes[i]]=[];
	markers[markertypes[i]].visible=false;
	markers[markertypes[i]].screenname=screenmarkertypes[i];
}
var places=[]; //sorted collections of places
var placetypes=new Array('Lecture_theatres','Libraries','Pacr');
var screenplacetypes=new Array('Lecture theatres','Libraries','Public access computer rooms');
for (i=0;i<placetypes.length;i++) { 
	places[placetypes[i]]=[];
	places[placetypes[i]].screenname=screenplacetypes[i];
}
var AZ=[]; //places collected from DB
//object definitions


function place(name,building,info) {
	//class that represents an entity that can appear in a menu
	this.name=name;
	this.building=building;
	this.info=info;
}

function marker(lat,lng,type,data,num) {
	//class that represents a place on the map
	this.pos = new GLatLng(lat,lng);
	this.icon=new GIcon(G_DEFAULT_ICON);
	this.data=data;
	this.type=type;
	switch (type) {
		case "Parking":
			this.icon.image="img/marker_accessible_parking.png";
			this.icon.iconSize = new GSize(30, 30);
			this.icon.iconAnchor = new GPoint(13, 29);
			this.icon.shadow="img/marker_shadow.png";
			break;
		case 'Entrances':
			this.icon.image="img/marker_entrance.png";
			this.icon.iconSize = new GSize(30, 30);
			this.icon.iconAnchor = new GPoint(13, 29);
			this.icon.shadow="img/marker_shadow.png";
			this.entrancetype=data;
			break;
		case 'Loops':
			this.icon.image="img/marker_loop.png";
			this.icon.iconSize = new GSize(30, 30);
			this.icon.iconAnchor = new GPoint(13, 29);
			this.icon.shadow="img/marker_shadow.png";
			break;
		case 'Enabled_toilets':
			this.icon.image="img/marker_accessible_toilet.png";
			this.icon.iconSize = new GSize(30, 30);
			this.icon.iconAnchor = new GPoint(13, 29);
			this.icon.shadow="img/marker_shadow.png";
			break;
		case 'Ramps':
			this.icon.image="img/marker_ramp.png";
			this.icon.iconSize = new GSize(30, 30);
			this.icon.iconAnchor = new GPoint(13, 29);
			this.icon.shadow="img/marker_shadow.png";
			break;
		default:
			this.num=data; //official building number
			break;
	}
	this.marker=new GMarker(this.pos,{draggable:EDITABLE,icon:this.icon});
	this.marker.parent=this;
	this.marker.type=type;
	this.marker.num=num; //index in table it was created from
	this.show = function (tab,place){
		if (EDITABLE) GEvent.addListener(this.marker,"dragend",set_markerposition);

		if (this.pos.lat()==0&&this.pos.lng()==0) {
			this.pos=map.getCenter();
			this.marker.pos=this.pos;

		}
		
		if (type=="Building") { //construct tabs
			this.dispphoto=httpexists("./photos/small/"+this.num+".jpg");
			if (this.places.length>1||this.description) { //multi-tab
				this.marker.showtab = function(p) {
				if (p) place=undefined;
				this.openInfoWindowTabsHtml([new GInfoWindowTab("Building", this.parent.getPlacesTab(place)),new GInfoWindowTab("More", this.parent.getInfoTab(place))],{selectedTab:0});
				//this.openInfoWindowTabs([new GInfoWindowTab("Info", this.parent.getInfoTab())],{selected:0});
				setTimeout("showhider();",1000); 
				}
			} //single tab
			else this.marker.showtab = function(p) {
				if (p) place=undefined;
				this.openInfoWindowTabsHtml([new GInfoWindowTab("Building", this.parent.getPlacesTab(place))],{selectedTab:0});
				}
			this.marker.tabvisible=false;
			this.marker.toggletab = function() {
				if (this.tabvisible) { 
					this.closeInfoWindow();
					this.tabvisible=false;
				}
				else {
					this.showtab();
					this.tabvisible=true;
				}
			}
			this.marker.reshowtab = function() {
				this.parent.marker.showtab(true);
			}
			GEvent.addListener(this.marker,"click",this.marker.reshowtab);
		}
		if (type=="Entrances") { //construct tabs
			this.marker.showtab = function(p) {
				this.openInfoWindowTabsHtml([new GInfoWindowTab("Entrance", this.parent.getEntranceTab(place))],{selectedTab:0});
			}
			this.marker.tabvisible=false;
			this.marker.toggletab = function() {
				if (this.tabvisible) { 
					this.closeInfoWindow();
					this.tabvisible=false;
				}
				else {
					this.showtab();
					this.tabvisible=true;
				}
			}
			GEvent.addListener(this.marker,"click",this.marker.toggletab);
		}
		if (type=="Enabled_toilets") { //construct tabs
			if (this.data !="") {
				this.marker.showtab = function(p) {
					this.openInfoWindowTabsHtml([new GInfoWindowTab("Toilet", this.parent.getToiletTab(place))],{selectedTab:0});
				}
				this.marker.tabvisible=false;
				this.marker.toggletab = function() {
					if (this.tabvisible) { 
						this.closeInfoWindow();
						this.tabvisible=false;
					}
					else {
						this.showtab();
						this.tabvisible=true;
					}
				}
				GEvent.addListener(this.marker,"click",this.marker.toggletab);
			}
		}
		map.addOverlay(this.marker);
		var boundscheck=map.getBounds();
		if ((!boundscheck.contains(this.pos))&&this.type=="Building") //only zoom to accomodate buildings
		{
			boundscheck.extend(this.pos);
			var newzoom = map.getBoundsZoomLevel(boundscheck); //.extend(newmarker.getPoint()));
			if (newzoom<15) map.setMapType(G_NORMAL_MAP);
			map.setZoom(newzoom);
			map.setCenter(boundscheck.getCenter());
		}

		if (tab) this.marker.showtab();
	}

	this.hide = function() {

		map.removeOverlay(this.marker);


	}
	this.baloonvisible=false
	this.places=[];
	this.sortPlaces=function() {this.places.sort(function(a,b) {if (a.name<b.name) return -1; else return 1; }); }
	this.getPlacesTab=function(place) {
		var popup="<div id='map_building'><h4>"+this.name+"</h4>";
		popup+="<div id='map_building_inner'>";
			if (this.dispphoto) {
					popup+="<div id='map_building_photo'>";
					popup+="<a href='javascript:shadowbox("+this.num+")'><img src='./photos/small/"+this.num+".jpg' style='display:block'; width='40%'; height='40%'></a>"; 
					popup+="</div>";
				}
				if (place) {
				popup+="<div id='map_building_in_this'>";
				//alert(place); //place is false when undefined
				// Check whether info is a link.
				if (place.info.match(/^\s*http/)) {
					popup +="<a href='"+place.info+"'>"+place.name+"</a>";
				}
				else {
					popup +=place.name;
				}
				popup+=" is in this building.</div>";
			}
			if (this.opening_hours) {
				popup+="<div id='map_building_opening_hours'>";
				popup +='<h5>Building Opening Hours:</h5>';
				popup +='<div>'+this.opening_hours+"</div>";
				popup+="</div>";
			}
			if ((this.accessibility)||(this.accessibility_url)) {
				popup+="<div id='map_building_access'>";
				if (this.accessibility) {
					popup+="<h5>Accessibility: </h5><div>"+this.accessibility+" ";
					if (this.accessibility_url) popup+="(<a href='http://www.tcd.ie/disability/accessibility/Building-check/"+this.accessibility_url+"'>more&nbsp;info</a>)</div>";
				
				}
				else if (this.accessibility_url) popup+="<div><a href='http://www.tcd.ie/disability/accessibility/Building-check/"+this.accessibility_url+"'>Accessibility&nbsp;info</a></div>";
				popup+="</div>";
			}
			popup+="</div>";
			
			popup+="<div id='map_building_permalink'>";
			var linkname;
			if (place) linkname=place.name;
			else linkname=this.name;
			popup+="<a href='map.php?q="+urlencode(linkname)+"' >Permalink</a>";
			lat=this.pos.lat();
			lng=this.pos.lng();
			
			if (!mainCampus.containsLatLng(this.pos)) {
				bd='"'+urlencode(this.name)+'"';
				popup+="    <a href='javascript:directions("+this.pos.lat()+","+this.pos.lng()+","+bd+")'>Directions</a> ";
			}
			popup+="</div>";

			return popup;
	}
	this.getInfoTab=function(place) {
		popup="";

		popup+="<div id='map_info'>";
		popup+="<div id='map_info_inner'><bd>"; //<div id='map_popup_inner'>";
		var placeoffset=0;
		if (place) placeoffset=1;
		if (this.places.length-placeoffset) {
			popup+="<h4>Also in this building</h4><ul>";
			for (i=0;i<this.places.length;i++) {
				if ((!place) || ((place) && (place.name != this.places[i].name))) {
					if (this.places[i].info.match(/^\s*http/)) {
						popup +="<li><a href='"+this.places[i].info+"'>"+this.places[i].name+"</a></li>";
					}
					else {
						popup +="<li>"+this.places[i].name+"</li>";
					}
				}
			}
			popup+="</ul>";
		}
		
		//if (dispphoto && this.description) 
		if (this.description)  popup+="<h4>Info</h4>"+this.description;

		popup+="</bd></div></div>"; //</div>";
		//alert(popup);
		return popup;
	}
	this.getEntranceTab=function(place) {
		popup="";

		popup+="<div id='map_entrancetab'>";
		popup+="<div id='map_entrancetab_inner'><bd>"; //<div id='map_popup_inner'>";
		popup+="<h4>Entrance</h4>"
		popup+="<br> "+this.data;
		/*
		if (this.entrancetype=="P") popup+="pedestrians";
		else if (this.entrancetype=="V") popup+="pedestrians and vehicles";
		else if (this.entrancetype=="K") popup+="keyholders";
		*/
		popup+="</bd></div></div>"; //</div>";
		//alert(popup);
		return popup;
	}
	this.getToiletTab=function(place) {
		popup="";

		popup+="<div id='map_toilettab'>";
		popup+="<div id='map_toilettab_inner'><bd>"; //<div id='map_popup_inner'>";
		popup+="<h4>Enabled toilet</h4>"
		popup+="<br>"+this.data;
		popup+="</bd></div></div>";
		//alert(popup);
		return popup;
	}
}
//13100 ?        Sl     0:03      0    24 136463 51504  2.4 /usr/lib/firefox-3.0.10/firefox
//prototypes save little memory
/*
		//http.onreadystatechange = setOutput;
		this.popup+=this.description;
		this.popup+="</div>"; //</div>";
		//return this.popup;
		return img;
	}
	
}
setOutput=function(){
	//some mad scoping or naming issue
		if(http.readyState == 4){
			//alert(oDest);
			popup+="<img src="+url+">";
		}
}
*/



//UI functions with nicer names for info bar
function show_place(type,index) {
	if (places[type][index].name==markers['Building'][places[type][index].building].name)
		markers['Building'][places[type][index].building].show(true);
	else	markers['Building'][places[type][index].building].show(true,places[type][index]);
}
function show_building(index) {
	markers['Building'][index].show(true);
}
function toggle_place(type,marker,index) {
	if (markers['Building'][places[type][marker].building].visible) {
		if(markers['Building'][places[type][marker].building].index==index)  {
			markers['Building'][places[type][marker].building].hide();
		}
	}
	else {
		markers['Building'][places[type][marker].building].show(true);
		markers['Building'][places[type][marker].building].index=index;
	}
}

function show_markers(type) {
	//disabled show_markers from re-centring map as it is only used for markers other than buildings
	
	/*
	newboxy1=map.getCenter().lat();
	newboxx1=map.getCenter().lng();
	newboxy2=newboxy1;
	newboxx2=newboxx1;
	*/
	if (!markers[type].visible) {
		/*
		for(var i=0;i<markers[type].length;i++) {
			

			newboxx1=Math.min(newboxx1,markers[type][i].pos.lng());
			newboxy1=Math.min(newboxy1,markers[type][i].pos.lat());
			newboxx2=Math.max(newboxx2,markers[type][i].pos.lng());
			newboxy2=Math.max(newboxy2,markers[type][i].pos.lat());

		}
		map.setCenter(new GLatLng((newboxy1+newboxy2)/2,(newboxx1+newboxx2)/2));
		*/
		for(var i=0;i<markers[type].length;i++) {
		markers[type][i].show();
		}
	}
	else for(var i=0;i<markers[type].length;i++) markers[type][i].hide();
	markers[type].visible=!markers[type].visible;
}
function hidemarker(marker) {
	markers["Building"][marker].hide();
}
function clearmarkers() {
	for (i=0;i<markertypes.length;i++) { 
		for (j=0;j<markers[markertypes[i]].length;j++) {
			markers[markertypes[i]][j].hide;
		}
	}
}
function onsearch(query)
{
	places["search"]=[];
	var foundbuildings=new Array();
	fb=0;
	for (var i=0;i<26;i++) {
		for (var j=0;j<places[String.fromCharCode(i+65)].length;j++) {
			if (places[String.fromCharCode(i+65)][j].name.toLowerCase().indexOf(query.toLowerCase())>-1) {
				places["search"].push(places[String.fromCharCode(i+65)][j]);
				if (foundbuildings[places[String.fromCharCode(i+65)][j].building]!="1") fb++;
				foundbuildings[places[String.fromCharCode(i+65)][j].building]="1";
			}
		}
	}
	//auto-open if 1 search result
	if (fb==1) {
		clearmarkers();
		markers["Building"][places["search"][0].building].show(true,places["search"][0]);
	}
	places["search"].screenname=query;
	places["search"].sort(function(a,b) {if (a.name<b.name) return -1; else return 1; });
	do_menu("search");
	//if (places["search"].length<4) {
	//	clearmarkers();
	//	for (i=0;i<places["search"].length;i++) {
	//		markers["building"][places["search"][i].building].show();
	//	}
	//}
}
//functions to populate menus and arrays
function do_menuchoices() {

	var menu="<ul>";
	for (var i=0;i<26;i++) {
		letter=String.fromCharCode(i+65);
		menu+="<li><a href='javascript:do_menu(\""+letter+"\");'>"+letter+" </a></li>"; 
	}
	menu+="</ul>";
	document.getElementById("searchoptions").innerHTML=menu;
	menu="<form id='form' method='POST' action='map.php' onsubmit='quicklink(this.childNodes[0].childNodes[1]);return false'><div><label for='quicklinks'>Quicklinks: </label><select name='quicklinks' id='quicklinks'><option selected='selected'>Search for lectures, libraries, pacr...</option>";
	for (i=0;i<placetypes.length;i++) menu+="<option value='"+placetypes[i]+"'>"+places[placetypes[i]].screenname+"</option>";
	menu+="</select> <input type='submit' value='Go' /></form>";
	document.getElementById("quicklinkswrap").innerHTML=menu;
}
function quicklink(owner) {
	name=owner.options[owner.selectedIndex].value;
	for (i=1;i<markertypes.length;i++) if (name==markertypes[i]) show_markers(name);
	for (i=0;i<placetypes.length;i++) if (name==placetypes[i]) do_menu(name);
	resize_map_and_results();
}
var do_menu_flag = false;
function do_menu(type) {
	var title="<h1>"+places[type].length+" results for ";
	if (type.length==1) title+="letter ";
	title+=places[type].screenname;
	title+="</h1>";

	if(document.getElementById("searchresulttitle")) {
		document.getElementById("searchresulttitle").innerHTML=title;
		var content="<ul>";

		for(var i=0;i<places[type].length;i++) {
			content+="<li><a href='javascript:show_place(\""+type+"\","+i+"); '>"+places[type][i].name+"</a></li>";
		}
		content+="</ul>";
		document.getElementById("searchresults").innerHTML=content;
		if(do_menu_flag) {
			resize_map_and_results();
		}
		else {
			do_menu_flag = true;
		}
		if (DEBUG) alert(content);
	}
}
function do_popularmenu() {
	var title="<h1>Popular places</h1>";

	if(document.getElementById("searchresulttitle")) {
		document.getElementById("searchresulttitle").innerHTML=title;
		var content="<ul><center>";

		for(var i=0;i<popularplaces.length;i++) {
		
			var bind;
			for (var j=0;j<markers['Building'].length;j++) { if (markers['Building'][j].num==popularplaces[i]) { bind=j; break; }}
			content+="<li>"
			content+="<a href='javascript:show_building("+bind+"); '>";
			content+="<img src=./photos/thumbs/"+popularplaces[i]+".jpg><br>";
			content+=popularnames[i]+"</a>";
			content+="</li>";
		}
		content+="</center></ul>";
		document.getElementById("searchresults").innerHTML=content;
		if(do_menu_flag) {
			resize_map_and_results();
		}
		else {
			do_menu_flag = true;
		}
		if (DEBUG) alert(content);
	}
	//Shadowbox.setup("a.img");

}

function preparedata() {
	
	//create AZ
	for (var i=0;i<26;i++) {
		places[String.fromCharCode(i+65)]=[];
		places[String.fromCharCode(i+65)].screenname=String.fromCharCode(i+65);
	}
	//Create alphabetical lists of places including exceptions
	for (var i=0;i<AZ.length;i++)
	{
		if (AZ[i].info=="") AZ[i].info=AZ[i].name;
		
		 var j=0;
		 var startlet=0;
		 
		 while (AZ[i].name.charCodeAt(j) <64) j++; //find 1st non-number
		 
		 startlet=AZ[i].name.charCodeAt(j) -65; 
		 if (startlet>25) startlet-=32; //select small letters (which shouldn't even be there)
		 
		 if ((startlet==96)||(startlet==128)) startlet=0; //catch Á
		 if (startlet==104) startlet=4; //catch É
		 if (startlet==108) startlet=8; //catch Í
		 
		 //find array number of building index as buildingindex number can be non-consecutive
		 var bb=AZ[i].building;
		 for (var j=0; j<markers['Building'].length;j++)
		 {
		 	if (markers['Building'][j].num==bb) {
				numplaces=places[String.fromCharCode(startlet+65)].length;
				places[String.fromCharCode(startlet+65)].push(AZ[i]); //add reference to place
				places[String.fromCharCode(startlet+65)][numplaces].building=j; //resolve reference to building array
				places[String.fromCharCode(startlet+65)][numplaces].index=i; //resolve reference to building array
				markers['Building'][j].places.push(AZ[i]);
			}
		 }
		 
		 if (AZ[i].name.slice(0,3)=="St.")
		 {
			 startlet=AZ[i].name.charCodeAt(4) -65;
			 if (startlet>25) startlet-=32; //select small letters
			 //we know its doesn't start with ÁÉÍ
			 for (var j=0; j<markers['Building'].length;j++)
			 {
				if (markers['Building'][j].num==bb) {
					numplaces=places[String.fromCharCode(startlet+65)].length;
					places[String.fromCharCode(startlet+65)].push(AZ[i]); //add reference to place
					places[String.fromCharCode(startlet+65)][numplaces].building=j; //resolve reference to building array
					markers['Building'][j].places.push(AZ[i]);
				}
			 }
		 }
	}
	//add buildings
	for (var i=0;i<markers['Building'].length;i++)
	{
		var j=0;
		 var startlet=0;
		 
		 while (markers['Building'][i].name.charCodeAt(j) <64) j++; //find 1st non-number
		 
		 startlet=markers['Building'][i].name.charCodeAt(j) -65; 
		 if (startlet>25) startlet-=32; //select small letters (which shouldn't even be there)
		 
		 if ((startlet==96)||(startlet==128)) startlet=0; //catch Á
		 if (startlet==104) startlet=4; //catch É
		 if (startlet==108) startlet=8; //catch Í
		 
		 numplaces=places[String.fromCharCode(startlet+65)].length;
		 places[String.fromCharCode(startlet+65)].push(new place(markers['Building'][i].name,i,markers['Building'][i].info));
		 places[String.fromCharCode(startlet+65)][numplaces].building=i; //resolve reference to building array
					
	}
	//sort results alphabetically
	for (i=0;i<26;i++) {
		letter=String.fromCharCode(i+65);
		places[letter].sort(function(a,b) { if (a.name<b.name) return -1; else return 1;});
	}
	//resolve other tables references
	for (i=0;i<placetypes.length;i++) {
		for (j=0;j<places[placetypes[i]].length;j++) {
			for (k=0; k<markers['Building'].length;k++)
			if (places[placetypes[i]][j].building==markers['Building'][k].num) {
				places[placetypes[i]][j].building=k; //resolve reference to building array
				break;
			}
		}
		places[placetypes[i]].sort(function(a,b) {if (a.name<b.name) return -1; else return 1; });
		
	}
	//create html popups
	for (var i=0;i<markers['Building'].length;i++) markers['Building'][i].sortPlaces();

}

// directions
// better end location for tcd
// location filter where?
function directions(lat,lng,place){
	Gd=new GDirections(map,document.getElementById("searchresults"));
	document.getElementById("searchresulttitle").innerHTML="<h1>From Trinity College to "+place+"</h1>";
	document.getElementById("searchresults").innerHTML="";
	Gd.load('from: 53.3445,-6.259 to: '+lat+','+lng);
}
//marker database functions
function add_marker(type)
{
	if (!markers[type].visible) {
		for(var i=0;i<markers[type].length;i++) markers[type][i].show();
		markers[type].visible=true;
	}
	var pos=map.getCenter();
	markers[type].push(new marker(pos.lat(),pos.lng(),type,markers[type].length));
	markers[type][markers[type].length-1].show();
	var result=http.open("GET","addmarker.php?table="+type+"&lat="+pos.lat()+"&lng="+pos.lng()+"&index="+((markers[type].length)-1));
	if (DEBUG) alert("addmarker.php?table="+type+"&lat="+pos.lat()+"&lng="+pos.lng()+"&index="+((markers[type].length)-1));
	http.send(null);
	if ((result==0)&&DEBUG) alert("problem with saving new marker to database");
}
function set_markerposition() {
	var result=http.open("GET","setmarkerpos.php?table="+this.type+"&index="+this.num+"&lat="+this.getPoint().lat()+"&lng="+this.getPoint().lng() );
	if (DEBUG) alert("setmarkerpos.php?table="+this.type+"&index="+this.num+"&lat="+this.getPoint().lat()+"&lng="+this.getPoint().lng() );
	http.send(null);
	if ((result==0)&&DEBUG) alert("problem with altering marker in database");

}
function urlencode (string) {
       string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
 
        return escape(utftext);
}
function getHTTPObject() {
  var xmlhttp;
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

