function setSelect(f,v)
{
	for(i=0,n=f.length;i<n && !(f[i].value==v || f[i].value=='' && f[i].text==v);i++);
	if(i<n) if(f.selectedIndex!=i) f.selectedIndex=i;
}

function setCheckbox(f,v)
{
	f.checked=(f.value==v)
}

function setRadio(f,v)
{
	for(i=0,n=f.length;i<n && f[i].value!=v;i++);
	if(i<n) if(!f[i].checked) f[i].checked=true;
}


function trim(s)
{
	n=s.length;
	for(i=0,c=s.charAt(i  ); i<n && (c==' ' || c=='\r' || c=='\n' || c=='\t'); i++,c=s.charAt(i  ));
	for(j=n,c=s.charAt(j-1); j>i && (c==' ' || c=='\r' || c=='\n' || c=='\t'); j--,c=s.charAt(j-1));
	return(j-i==n?s:s.substring(i,j))
}

function searchBar(url)
{
	wth=500;hgt=380;
	w=window.open(url,'','left='+(screen.width-wth)/2+',top='+(screen.height-hgt)/2+',width='+wth+',height='+hgt)
	w.focus()
}

timerID=0;
function showAddr()
{
	AJAX('Addr','Fn=Address&Address='+Search.Address.value);Addr.style.display='block';
}

function timerAddr()
{
	if(timerID) window.clearTimeout(timerID);
	if(Search.Address.value.length>=2) timerID=window.setTimeout(showAddr,500);
}

function suggestSearch()
{
	if(Search.Search.value.length>=2)
	{
		AJAX('autoSearch','Fn=suggestSearch&Typed='+Search.Search.value);
		autoSearch.style.display='block';		
	}
	if(Search.Search.value.length<2)
	{
		autoSearch.style.display='none';
	}
}

function setSuggestion(suggestion)
{
	Search.Search.value=suggestion;
	autoSearch.style.display='none';
}

function companyListSearch(f)
{
	if(f.value.length>=2)
	{
		AJAX('companyListAuto','Fn=companyListSuggest&Typed='+List.Seek.value+'&tTitle='+List.tTitle.value+'&comment='+List.Comment.value+'&LID='+List.LID.value);
		companyListAuto.style.display='block';		
	}
	if(f.value.length<2)
	{
		companyListAuto.style.display='none';
	}
}

function guidesListSearch()
{
	if(Seek.Search.value.length>=2)
	{
		AJAX('guideListAuto','Fn=guidesListSuggest&Typed='+Seek.Search.value);
		guideListAuto.style.display='block';		
	}
	if(Seek.Search.value.length<2)
	{
		guideListAuto.style.display='none';
	}
}

function setGuideSuggestion(suggestion)
{
	Seek.Search.value=suggestion;
	guideListAuto.style.display='none';
	Seek.find.focus();
}

function guidesSearchResult()
{
	AJAX('gSearchResult','Fn=guidesSearchResult&Typed='+Seek.Search.value);
	gSearchResult.style.display='block';
}

function guidesInCategorySearch(cat)
{
	AJAX('gSearchResult','Fn=guidesInCategory&category='+cat);
	gSearchResult.style.display='block';
}

function addCompanyToList(company,id)
{
	Listing.Search.focus();
//	alert('Fn=addCompanyToList&name='+company+'&id='+id+'&title='+List.Title.value+'&comment='+List.Comment.value+'&LID='+List.LID.value);	
	AJAX('theListDiv','Fn=addCompanyToList&name='+company+'&id='+id+'&tTitle='+List.tTitle.value+'&comment='+List.Comment.value+'&LID='+List.LID.value);	
	
	//companyListAuto.style.display='none';
}


function tabSet(name,state)
{
	if(state)
	{
		eval(name+"_L").src="/Img/mFgLft.gif"
		eval(name+"_M").className="TabFG"
		eval(name+"_R").src="/Img/mFgRgt.gif"
	}
	else
	{
		eval(name+"_L").src="/Img/mBgLft.gif"
		eval(name+"_M").className="TabBG"
		eval(name+"_R").src="/Img/mBgRgt.gif"
	}
}

function checkUsername(v)
{
	s=v.value;n=s.length;
	for(i=0;i<n && (s.charAt(i)>='a' && s.charAt(i)<='z' || s.charAt(i)>='A' && s.charAt(i)<='Z' || s.charAt(i)>='0' && s.charAt(i)<='9' || s.charAt(i)=='æ' || s.charAt(i)=='Æ' || s.charAt(i)=='ø' || s.charAt(i)=='Ø' || s.charAt(i)=='å' || s.charAt(i)=='Å');i++);
	if(i!=n || n<3 || n>64) {alert('Brukernavn er ugyldig! (3-64 bokstaver/tall)');v.focus();return(false);}
	return(true);
}


function checkMobNr(v)
{
	s=v.value;n=s.length;
	for(i=0;i<n && (s.charAt(i)>='0' && s.charAt(i)<='9');i++);
	if(i!=n || n!=8) {alert('Mobilnr er ugyldig! (8 tall)'  );v.focus();return(false);}
	return(true);
}

function checkEmail(v)
{
	s=v.value;n=s.length;
	if(n<6 || n>64)         {alert('Email er ugyldig! (6-64 tegn)'               );v.focus();return(false);}
	return(true);
}

function checkPassword(v)
{
	s=v.value;n=s.length;
	if(n<4 || n>16)         {alert('Passord er ugyldig! (4-16 tegn)'               );v.focus();return(false);}
	return(true);
}

function resize(ID)
{
	oBody=document.body;
	oFrame=document.getElementById(ID)
	
	x=oFrame.offsetLeft; wth=oBody.clientWidth -x-1;
	y=oFrame.offsetTop;  hgt=oBody.clientHeight-y-1;
	
	oFrame.style.position="absolute" 
	//oFrame.style.left  =x;
	//oFrame.style.top   =y;
        oFrame.style.width =wth;
        oFrame.style.height=hgt;
}

function AJAX(id,prm)
{	
	url="/AJAX.asp?"+(prm?prm:"")
	
	var req = (window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"))
	req.open("GET", url, false);
	req.send(null);

	o=document.getElementById(id)
	o.innerHTML=req.responseText
}

function pop(url)
{
	wth=screen.width-250;
	hgt=screen.height-200;
	w=window.open(url,'main','left=100,top=50,width='+wth+',height='+hgt+',scrollbars=yes,resizable=yes,location=yes,status=yes')
	w.focus()
}

function pict(title,url)
{
	wth=800;
	hgt=600;
	w=window.open('Show.asp?Title='+title+'&URL='+url,'main','left='+(screen.width-wth)/2+',top='+(screen.height-hgt)/2+',width='+wth+',height='+hgt+',scrollbars=no,resizable=yes,location=no,status=no')
	w.focus()
}

function tips(prm)
{
	w=window.open('Tips.asp?'+prm,'','left=50,top=50,scrollbars=yes,resizable=yes,location=yes,status=yes');
	w.focus()
}

function warn(prm)
{
	w=window.open('Warning.asp?'+prm,'','left=50,top=50,scrollbars=yes,resizable=yes,location=yes,status=yes');
	w.focus()
}

function loadMap(pts,x,y,zoom)
{
	function createMarker(ID,Str,x,y,type)
	{
		if(type==0)
		{
			icon.image="/Img/MapIcon/mx.png";
			icon.iconSize = new GSize(28, 30);
			icon.iconAnchor = new GPoint(0, 30);
		}
		else
		{
			icon.image="/Img/MapIcon/m"+type+".png";
			icon.iconSize = new GSize(20, 25);
			icon.iconAnchor = new GPoint(17, 25);
		}
		var marker=new GMarker(new GLatLng(y,x),icon);
		marker.ID=ID;
		marker.Str=Str;
		GEvent.addListener(marker, "click"    , function(){clickMap(marker.ID);});
		GEvent.addListener(marker, "mouseover", function(){overMap(marker.ID);});
		GEvent.addListener(marker, "mouseout" , function(){overMap(0);        });
		return marker;
	}

	if(pts.length==0) return;

	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
	GEvent.addListener(map, "dragend", function(){center=map.getCenter();zoom=map.getZoom();dragMap(center.lng(),center.lat(),zoom);});

	for(i=0,x1=x2=pts[0].X,y1=y2=pts[0].Y; i<pts.length; i++)
	{
		pX=pts[i].X;if(pX<x1) x1=pX; if(pX>x2) x2=pX;
		pY=pts[i].Y;if(pY<y1) y1=pY; if(pY>y2) y2=pY;
	}
	O=(x && y?new GLatLng(y,x):new GLatLng(0.5*(y1+y2),0.5*(x1+x2)));
	if(zoom==null)
	{
		p1=new GLatLng(y1,x1);p2=new GLatLng(y2,x2);
		zoom=map.getBoundsZoomLevel(new GLatLngBounds(p1, p2));
		if(zoom>15) zoom=15;
	}
	map.setCenter(O,zoom);

	var icon = new GIcon();
	for(i=0,n=pts.length,nr=1;i<n;i++) if(pts[i])
	{
		if(pts[i].Type=="X")
			type=0;
		else
			type=(nr++);
			
		ID=Str=pts[i].ID;
		pX=pts[i].X;
		pY=pts[i].Y;
		for(j=i+1;j<n;j++) if(pts[j]) if(pts[j].X==pX && pts[j].Y==pY)
		{
			Str=(Str!=""?Str+","+pts[j].ID:pts[j].ID);
			pts[j]=null;
		}

		map.addOverlay(createMarker(ID,Str,pX,pY,type));
	}

	return(map);
}

function loadMap2(pts,mapname,x,y,zoom)
{

	function createMarker(ID,Str,x,y,type)
	{
		if(type==0)
		{
			icon.image="/Img/MapIcon/mx.png";
			icon.iconSize = new GSize(28, 30);
			icon.iconAnchor = new GPoint(0, 30);
		}
		else
		{
			icon.image="/Img/MapIcon/m"+type+".png";
			icon.iconSize = new GSize(20, 25);
			icon.iconAnchor = new GPoint(17, 25);
		}
		var marker=new GMarker(new GLatLng(y,x),icon);
		marker.ID=ID;
		marker.Str=Str;
		GEvent.addListener(marker, "click"    , function(){clickMap(marker.ID);});
		GEvent.addListener(marker, "mouseover", function(){overMap(marker.ID);});
		GEvent.addListener(marker, "mouseout" , function(){overMap(0);        });
		return marker;
	}
	var browser=navigator.appName;
	if(zoom==null)
	{
		if (browser=="Microsoft Internet Explorer")
		{
			zoom=13;
		}
	}
	if(pts.length==0) return;

	var map = new GMap2(document.getElementById(mapname));
	map.addControl(new GSmallMapControl());
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
//	map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	GEvent.addListener(map, "dragend", function(){center=map.getCenter();zoom=map.getZoom();dragMap(center.lng(),center.lat(),zoom);});

	for(i=0,x1=x2=pts[0].X,y1=y2=pts[0].Y; i<pts.length; i++)
	{
		pX=pts[i].X;if(pX<x1) x1=pX; if(pX>x2) x2=pX;
		pY=pts[i].Y;if(pY<y1) y1=pY; if(pY>y2) y2=pY;
	}
	O=(x && y?new GLatLng(y,x):new GLatLng(0.5*(y1+y2),0.5*(x1+x2)));
	if(zoom==null)
	{
		p1=new GLatLng(y1,x1);p2=new GLatLng(y2,x2);
		zoom=map.getBoundsZoomLevel(new GLatLngBounds(p1, p2));
		if(zoom>15) zoom=15;
		//alert(zoom);
	}
//	alert(O);
	map.setCenter(O,zoom);

	var icon = new GIcon();
	for(i=0,n=pts.length,nr=1;i<n;i++) if(pts[i])
	{
		if(pts[i].Type=="X")
			type=0;
		else
			type=(nr++);
			
		ID=Str=pts[i].ID;
		pX=pts[i].X;
		pY=pts[i].Y;
		for(j=i+1;j<n;j++) if(pts[j]) if(pts[j].X==pX && pts[j].Y==pY)
		{
			Str=(Str!=""?Str+","+pts[j].ID:pts[j].ID);
			pts[j]=null;
		}

		map.addOverlay(createMarker(ID,Str,pX,pY,type));
	}

	return(map);
}


function clickMap(ID)
{
	if(ID) 
	{
		window.location="Company.asp?CompanyID="+ID;
	}
}

function overMap(ID)
{
	if(ID)
	{
		AJAX("PopupCompany","Fn=PopupCompany&CompanyID="+ID);
		o=document.getElementById("PopupCompany")
		o.style.left=document.body.scrollLeft+event.clientX-275;
		o.style.top =document.body.scrollTop +event.clientY-25;
		//o.innerHTML=s
		o.style.display='block'
	}
	else
	{
		o=document.getElementById("PopupCompany")
		o.style.display='none'
	}
}

function setAddress(address,city,x,y)
{
	Search.Address.value=address;
	Search.MapX.value=x;
	Search.MapY.value=y;
	Addr.style.display='none';
}

function setPlace(address,city,x,y)
{
	setAddress(address,city,x,y)
	Place.style.display='none';
}

function showPlace()
{
	if(Place.style.display=='none')
	{
		AJAX('Place','Fn=Place');
		Place.style.display='block';
	}
	else
		Place.style.display='none';
}


