
offset_top = 124;
offset_left = 188;

var browserType = BrowserSniff();

function fillList( formName, listName, strOptions ){
  // fill any list with options
  
  // always insert selection prompt
  var lst = document.forms[formName][listName];
  lst.disabled = true;

  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  for( var i = 0; i < aOptionPairs.length; i++ ){
    if (aOptionPairs[i].indexOf('~') != -1) {
      var aOptions = aOptionPairs[i].split('~');
      lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }  
  }
  
  lst.disabled = false;
}

function emptyList( formName, listName ){
  var lst = document.forms[formName][listName];
  lst.options.length = 0;
}

function BrowserSniff(){
  if (document.layers) return "NS";
  if (document.all) return "IE";
  if (document.getElementById) return "MOZ";
  return "OTHER";

}

function getAbsElementLeft(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetLeft+188;
  else return eval(id).offsetLeft+offset_left;
}


function getAbsElementTop(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetTop+94;
  else return eval(id).offsetTop+offset_top;
}


function getElementTop(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetTop;
  else return eval(id).offsetTop;
}


function getElementLeft(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetLeft;
  else return eval(id).offsetLeft;
}

function getElementRight(id) {
  return getElementLeft(id) + getElementWidth(id);
}

function getElementBottom(id) {
  return getElementTop(id) + getElementHeight(id);
}

function getElementHeight(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetHeight;
  else return eval(id).clientHeight;
}

function getElementWidth(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).offsetWidth;
  else return eval(id).clientWidth;
}


function setElementPlace(id, x, y)
{
  if (browserType=="NS" || browserType=="MOZ") {
	document.getElementById(id).style.top=y+"px";
	document.getElementById(id).style.left=x+"px";
  } else {
	eval(id).style.top = y;
	eval(id).style.left = x;
  }
}

function setIframe(id, valu)
{
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).src=valu;
  	else eval("document.all."+id).src=valu;
}

function setElementHeight(id, elementHeight) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).style.height=elementHeight;
  else eval(id).style.height = elementHeight;
}


function setElementWidth(id, elementWidth) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).style.width=elementWidth;
  else eval(id).style.width = elementWidth;
}


function setElementTop(id, elementTop) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).style.top=elementTop+"px";
  else eval(id).style.top = elementTop;
}

function setElementLeft(id, elementLeft) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).style.left=elementLeft+"px";
  else eval(id).style.left = elementLeft;
}

function setElementRight(id, elementRight) {
  setElementWidth(id, elementRight-getElementLeft(id));
}

function setElementBottom(id, elementBottom) {
  setElementHeight(id, elementBottom-getElementTop(id));
}

function setDisplay(id, what)
{
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).style.display=what;
  else eval(id).style.display = what;
}

function setElementBgColor(id, elementColor) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).bgColor = elementColor
  else eval(id).style.backgroundColor = elementColor;
}

function setImgSrc(id, src) {
  if (browserType=="NS" || browserType=="MOZ") document.getElementById(id).src = src;
  else eval(id).src = src;
}


function getImgSrc(id) {
  if (browserType=="NS" || browserType=="MOZ") return document.getElementById(id).src;
  else return eval(id).src;
}


function ZLev(lev) {
	if (lev==0) return 1;
	if (lev==1) return 6;
	if (lev==2) return 8;
	if (lev==3) return 9;
	return 1;
}


function cbShowMap( strRet ) {
	arr = strRet.split(",");
	Lat = arr[0];
	Long = arr[1];
	ZoomLevel = ZLev(level);
	ShowMap(1);
}


function BrowseUpdate(level)
{
	if (level<1) return;

	var co = null;
	var ret = null;

	// 1 = province
	// 2 = town
	// 3 = suburb
	// 4 = street

	region = document.forms['areaform']['region'].options[document.forms['areaform']['region'].selectedIndex].value;
	if (level>1) town = document.forms['areaform']['town'].options[document.forms['areaform']['town'].selectedIndex].value;
		else town="-1";
	if (level>2) suburb = document.forms['areaform']['suburb'].options[document.forms['areaform']['suburb'].selectedIndex].value;
		else suburb="-1";
	if (level>3) {
		ret = document.forms['areaform']['street'].options[document.forms['areaform']['street'].selectedIndex].value;
		arr = ret.split(",");
		Lat = arr[0];
		Long = arr[1];
		ZoomLevel = 10;

		ShowMap(1);
	} else {
		jsrsExecute( '/mapping_rs.asp', cbShowMap, 'getLL' , new Array(new String(level), region, town, suburb) );
	}
}



function ActivateButton()
{
	setDisplay("idButton", "inline");
}


function OnButton()
{
	areaform.val_region.value = areaform.region.options[areaform.region.selectedIndex].text;
	areaform.val_town.value = areaform.town.options[areaform.town.selectedIndex].text;
	areaform.val_suburb.value = areaform.suburb.options[areaform.suburb.selectedIndex].text;
	areaform.val_street.value = areaform.street.options[areaform.street.selectedIndex].text;
	areaform.submit();
}


function px(inp)
{
	pixels=0;
	if (inp.substr(inp.length-2)=="px") pixels = Number(inp.substr(0,inp.length-2));

	return pixels;
}


function mousewheel()
{
	if (event.wheelDelta >= 120) {
		if (CurrTab > 1) CurrTab--;
	} else if (event.wheelDelta <= -120) {
		if (CurrTab < NUMTABS) CurrTab++;
	}
	showtab(CurrTab);
}


function handleresize()
{
	tabheight = getElementHeight("tabcell");
	for (loop=1;loop<=NUMTABS;loop++) {
		setElementHeight("section"+loop+"_1", 250);
	}
	controlresize();
	remainwidth = getElementWidth("maintable")-184;
	setElementWidth("imgcell",remainwidth);
}


function controlresize()
{
	tabheight = getElementHeight("tabcell");
	heightleft = tabheight - (NUMTABS-1)*25 - 2;
//	businessform.buslist.size=(heightleft-64)/13-1;
//	searchresults.list.size=(heightleft-56)/13-1;
}

function cbPrintMap(bm)
{
	window.open("printmap.asp?num="+bm,"printpage","width="+MapWidth+",height="+MapHeight);
}

function cbPrintMapAnanzi(bm)
{
	window.open("printmapananzi.asp?num="+bm,"printpage","width="+MapWidth+",height="+MapHeight);
}


function PrintMap()
{
//	bm = MakeBookmark(LastMap+",O=1");
//	bm = MakeBookmark(LastMap);
//	window.open("printmap.asp?num="+bm,"printpage","width="+MapWidth+",height="+MapHeight);
	jsrsExecute( '/mapping_rs.asp', cbPrintMap, 'makeBookmark', LastMap );
}

function PrintMapAnanzi()
{
//	bm = MakeBookmark(LastMap+",O=1");
//	bm = MakeBookmark(LastMap);
//	window.open("printmapananzi.asp?num="+bm,"printpage","width="+MapWidth+",height="+MapHeight);
	jsrsExecute( '/mapping_rs.asp', cbPrintMapAnanzi, 'makeBookmark', LastMap );
}


function UpdateStripad()
{
	var serverURL = "mapping_rem.asp";
	var co = RSExecute(serverURL,"getClosestStreet",Lat,Long,1);
    var ret = co.return_value;

	window.status = "ad: "+ret[0][1]+" "+ret[0][2]+" "+ret[0][3];
// use the following for the image
//	<img border=0 id="adimg" onclick="WHATEVER" onmousemove="MouseMove();">
	return 1;
}

function ShowMap(log_browse)
{
	Lat = Math.floor(Lat);
	Long = Math.floor(Long);

	elL = getAbsElementLeft("imgcell")+getElementWidth("imgcell")/2-48;
	elT = getAbsElementTop("imgcell")+getElementHeight("imgcell")/2-63;
	setElementPlace("loadingoverlay", elL, elT);
	setDisplay("loadingoverlay", "inline");
	BusyFlag = 1;
	setElementPlace("arrow_nw", getAbsElementLeft("imgcell")+2, getAbsElementTop("imgcell")+2);
	setElementPlace("arrow_ne", getAbsElementLeft("imgcell")+getElementWidth("imgcell")-2-30, getAbsElementTop("imgcell")+2);
	setElementPlace("arrow_se", getAbsElementLeft("imgcell")+getElementWidth("imgcell")-2-30, getAbsElementTop("imgcell")+getElementHeight("imgcell")-6-30);
	setElementPlace("arrow_sw", getAbsElementLeft("imgcell")+2, getAbsElementTop("imgcell")+getElementHeight("imgcell")-6-30);
	setElementPlace("arrow_n", getAbsElementLeft("imgcell")+getElementWidth("imgcell")/2-30, getAbsElementTop("imgcell")+2);
	setElementPlace("arrow_e", getAbsElementLeft("imgcell")+getElementWidth("imgcell")-2-30, getAbsElementTop("imgcell")+getElementHeight("imgcell")/2-30);
	setElementPlace("arrow_s", getAbsElementLeft("imgcell")+getElementWidth("imgcell")/2-30, getAbsElementTop("imgcell")+getElementHeight("imgcell")-6-30);
	setElementPlace("arrow_w", getAbsElementLeft("imgcell")+2, getAbsElementTop("imgcell")+getElementHeight("imgcell")/2-30);
//	setElementPlace("zoom_in", getElementLeft("imgcell")+2, getAbsElementTop("imgcell")+getElementHeight("imgcell")/2+30+8);
//	setElementPlace("zoom_out", getElementLeft("imgcell")+2, getAbsElementTop("imgcell")+getElementHeight("imgcell")/2+30+8+getElementHeight("zoomin_img"));
//	setElementPlace("printer", getElementLeft("imgcell")+2, getAbsElementTop("imgcell")+getElementHeight("imgcell")/2+30+8+getElementHeight("zoomin_img")*2);

	MapHeight = getElementHeight("imgcell");
	MapWidth = getElementWidth("imgcell")-8;

	// add dots
	dotString="";
	for (loop=0;loop<document.dotform.markers.length;loop++) {
		dotString=dotString+",M="+document.dotform.markers[loop].value;
	}

	LastRandom = Math.random();

	LastMap="X="+Lat+",Y="+Long+",S="+ZoomLevel+",W="+MapWidth+",H="+MapHeight+dotString+",RND="+LastRandom;
	setImgSrc("mapimg", MapServBase+LastMap);

	dX=(MapWidth/1120)*(ZoomFactor[ZoomLevel])*8;
	dY=(MapHeight/1120)*(ZoomFactor[ZoomLevel])*8;
	DEGx=(MapWidth/1120)*(ZoomFactor[ZoomLevel])*10;
	DEGy=(MapHeight/1120)*(ZoomFactor[ZoomLevel])*10;
	Xdelta=dX;
	if (Xdelta==0) Xdelta=1;
	Ydelta=dY;
	if (Ydelta==0) Ydelta=1;
	
	setIframe("adframe", 'LocationAd.asp?x='+Lat+'&y='+Long+'&s='+ZoomLevel);
	setIframe("adframe2", 'LocationAd.asp?x='+Lat+'&y='+Long+'&s='+ZoomLevel);

	NeedData = 1;

	if (log_browse) {
		region = document.forms['areaform']['region'].options[document.forms['areaform']['region'].selectedIndex].value;
		if (level>=1) town = document.forms['areaform']['town'].options[document.forms['areaform']['town'].selectedIndex].value;
			else town="-1";
		if (level>=2) suburb = document.forms['areaform']['suburb'].options[document.forms['areaform']['suburb'].selectedIndex].value;
			else suburb="-1";
		jsrsExecute( 'mapping_rs.asp', cbLogBrowseCB, 'logBrowse' , new Array(new String(level), region, town, suburb));
	}
}

function cbLogBrowseCB(parm)
{
//	alert(parm);
}


HotSpots = new Array ("","");

function ZoomHot(num)
{
	spl = HotSpots[num-1].split("|");
	rurl = spl[0];
	rurl = rurl.replace(/\&/gi,"!");
	rurl = rurl.replace(/\?/gi,"$");
	url = "ButtonAdClick.asp?url="+rurl+"&dbref="+spl[1];
	window.open(url,"_blank");
}


function cbImageLoaded( strRet ) {
  window.status = '';

	ret = strRet.split("|");

	if (ret!="") {
		for (loop=0;loop<ret.length-1;loop++) {
			arr = ret[loop].split(",");
			HotSpots[loop]=arr[4]+"|"+arr[5];
			ref=loop+1;
			name = "hot"+ref;
			setElementLeft(name, Number(getAbsElementLeft("imgcell"))+Number(arr[0]));
			setElementTop(name, Number(getAbsElementTop("imgcell"))+Number(arr[1]));
			setElementWidth(name, Number(arr[2])-Number(arr[0]));
			setElementHeight(name, Number(arr[3])-Number(arr[1]));
			name = "hotimg"+ref;
			setElementWidth(name, Number(arr[2])-Number(arr[0]));
			setElementHeight(name, Number(arr[3])-Number(arr[1]));
		}
	}

//	UpdateStripad();
	setDisplay("loadingoverlay", "none");

	NeedData = 0;
	BusyFlag = 0;
}


function ImageLoaded()
{
	if (!NeedData) return;
	jsrsExecute( 'mapping_rs.asp', cbImageLoaded, 'getMapInfo' , String(LastRandom));
}


function init()
{
	if (browserType=="NS" || browserType=="MOZ") {
		document.getElementById('mapimg').onmousemove=MouseMove;
		document.getElementById('mapimg').onmousedown=MapClick;
		document.getElementById('placedot').onmousemove=MouseMove;
		document.getElementById('placedot').onmousedown=MapClick;
	}
	
	controlresize();
	handleresize();
	showtab(2);
	LoadParms();
	ShowMap(0);
	LoadRegions();
}


function AddDot()
{
	if (document.forms['dotform']['annotation'].value=="") {
		alert("You need to add an annotation");
		return;
	}
	ix = document.forms['dotform']['markers'].length;
	p1 = document.forms['dotform']['markertype'].options[document.forms['dotform']['markertype'].selectedIndex].value;
	p2 = Math.round(document.forms['dotform']['latitude'].value*10000);
	p3 = -Math.round(document.forms['dotform']['longitude'].value*10000);
	annotation = document.forms['dotform']['annotation'].value;
	val = p1+p2+p3+annotation;
	document.forms['dotform']['markers'].options[ix] = new Option(annotation, val);
	MapLocked = false;
	ShowMap(0);
}


function showtab(tabnum)
{
	CurrTab = tabnum;
	for (loop=1;loop<=NUMTABS;loop++) {
		if (loop==tabnum) {
//			setDisplay("section"+loop+"_0a", "block");
			tabheight = getElementHeight("tabcell");
			setElementHeight("section"+loop+"_1a", (tabheight - (NUMTABS-1)*27 - 2 - 62));
//			setDisplay("section"+loop+"_0b", "block");
			setDisplay("section"+loop+"_1", "block");
		} else {
//			setDisplay("section"+loop+"_0a", "none");
//			setDisplay("section"+loop+"_0b", "none");
			setDisplay("section"+loop+"_1", "none");
		}
	}
	if (CurrTab==4) {
		LoadRegions();
	}
	if (CurrTab==5) {
		setDisplay("placedot", "inline");
	} else {
		setDisplay("placedot", "none");
	}
}


function ProcessMouseMove(mouseX, mouseY)
{
	placeX=mouseX-getAbsElementLeft("imgcell")-2;
	placeY=mouseY-getAbsElementTop("imgcell")-2;

	rX = placeX/getElementWidth("mapimg");
	rY = placeY/getElementHeight("mapimg");

	CursorLong = Math.round((Long-DEGy/2+DEGy*rY)*10)/10000;
	CursorLat = Math.round((Lat-DEGx/2+DEGx*rX)*10)/10000;
	
	if (CurrTab==5) {
		if (MapLocked) return;
		document.forms['dotform']['latitude'].value=CursorLat;
		document.forms['dotform']['longitude'].value=-CursorLong;
		setElementPlace("placedot", mouseX-getElementWidth("placedot")/2, mouseY-getElementHeight("placedot")/2);
	} else { 
	}
}



function MouseMove(e)
{
	if (browserType=="NS" || browserType=="MOZ") {
		ProcessMouseMove(e.clientX, e.clientY);
	} else {
		ProcessMouseMove(event.clientX, event.clientY);
	}
}


function MapClick(e)
{
	if (CurrTab==5) {
//		MapLocked=!MapLocked;
		AddDot();
	} else {
		Lat = Math.floor(CursorLat*1000);
		Long = Math.floor(CursorLong*1000);
		ShowMap(0);
	}
}

