function in_array(needle,haystack){
	var found=false,i=0;
	while(!found && i<haystack.length){
		if(haystack[i]==needle)
			found=true;
		else
			i++;
	}
	return found;
}

function sizeOf(associative_array){
	var cpt=0;
	for(var i in associative_array){
		cpt++;
	}
	return cpt;
}

function trim(s) {
	if(typeof(s)=='string' && s!='')
		s=s.replace(/^\s+/, '').replace(/\s+$/, '')
    return s;
}		

function meterToKM(number,precision){
	return (Math.round((number/1000)*Math.pow(10,precision))/Math.pow(10,precision));
}

function htmlEntities(s){
	if(typeof(s)=='string' && s!=''){
		var temp='';
		while(temp!=s){
			if(temp!='')
				s=temp;
			temp = s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
		}
		delete temp;
	}
	
	return s;
}

function nl2br(text){
	if(typeof(text)=='string' && text!=''){
		text = text.replace(/\r\n/g,'<br/>');
		text = text.replace(/\n/g,'<br/>');
	}
	
	return text;
}

function getTextNodes(text){
	var res = new Array();
	if(typeof(text)=='string' && text!=''){
		var temp = text.split("\r\n");
		if(temp.length==1)
			temp = text.split("\n");
		for(var i=0;i<temp.length;i++){
			res.push(document.createTextNode(temp[i]));
			if(i<temp.length-1)
				res.push(document.createElement('br'));
		}
	}
	return res;
}

function truncate(text,numChar){
	if(text.length>numChar){
		text=text.substring(0,numChar);
		text=text.substring(0,text.lastIndexOf(' '));
		text+='...';
	}
	return text;
}

function maxlength(text,nbChar){
	if(text.length<nbChar)
		return true;
	else
		return false;
}

function openPopUp(page,name,width,height){
	var top=Math.round((screen.height-height)/2);
	var left=Math.round((screen.width-width)/2);
	window.open(page,name,'toolbar=no,status=no,location=no,menubar=no,personalbar=no,scrollbars=yes,resizable=no,alwaysRaised=yes,width='+width+',height='+height+',top='+top+',left='+left);
}

function openAjaxDiv(type,data){
	var div=document.getElementById('display_ajax');
	div.style.top=Math.floor(((window.innerHeight?window.innerHeight:document.documentElement.clientHeight)-div.offsetHeight)/2)+'px';
	div.style.left=Math.floor(((window.innerWidth?window.innerWidth:document.documentElement.clientWidth)-div.offsetWidth)/2)+'px';
	document.getElementById('header_ajax_title').innerHTML='';
	document.getElementById('loading_state').style.display='block';
	div.style.visibility='visible';
	loadAjaxDivData(type,data);
}

function closeAjaxDiv(){
	document.getElementById('display_ajax').style.visibility='hidden';
}

function loadAjaxDivData(type,data){
	if(type=='flag')
		file='../scripts/flagged_poi.php';
	else if(type=='user')
		file='../scripts/user_poi.php';
	var xhr = new ZHTTPRequest("POST","xml",true);
	xhr.setCompleteCallback(
			function(xml_doc)
			{
				document.getElementById('loading_state').style.display='none';
				if( xml_doc != null )
				{
					document.getElementById('header_ajax_title').innerHTML=xml_doc.getElementsByTagName('title')[0].firstChild.nodeValue
					document.getElementById('body_ajax').innerHTML=xml_doc.getElementsByTagName('content')[0].firstChild.nodeValue
				}
			}
	);
	xhr.setFailureCallback(
						function(){alert("Request failed, please try again later")}
	);
	xhr.query('<?print PHPSCRIPTS_DIRNAME; ?>edit_poi.php',data);
}

function isIE(){
	if(navigator.appName=='Microsoft Internet Explorer')
		return true;
	else
		return false;
}