function onLoad() {

  //preload loading bar gif and rollovers
  preloadingbar= new Image(220,19);
  preloadingbar.src="images/loading_bar.gif";
  myrollover = new Image(75,25);
  myrollover.src="images/menubutton_bg_rollover.jpg";
  mymiddlearrow = new Image(158,7);
  mymiddlearrow.src = "images/downarrow_middle.gif";
  myrightarrow = new Image(158,7);
  myrightarrow.src = "images/downarrow_right.gif";

  // establish the startup status of the popup window
  popupminmaxstate="max";
  popupnarrowstate="max";

  // flags whether a layer is loaded or not, to enforce mutual exclusion of
  // map symbolisation and rendering.
  layerloading = 0;
  //alert('onload'+ layerloading);

  // establish an array of layers
  mylayers=new Array()

  // establish an array of 'parent' crime objects, which any map object
  // added to the application can search through to look for something
  // to connect to to form a web (see dolayer function below
  crimescenes = new Array();

  // establish an array of 'parent' residence objects, which any map object
  // added to the application can search through to look for something
  // to connect to to form a web (see dolayer function below
  residences = new Array();
  residence_starts = new Array();
  residence_ends = new Array();



  // establish an array of webs, which are basically layers,
  // which link some map objects to others (see dolayer function below)
  mywebs = new Array();


  //establish arrays to contain the icon types
  mymapicons = new Array();

  //establish arrays to contain the vertices of the polyline linkages or 'webs'
  myvertices = new Array();


/***********************************************
* Drag and Drop Script: © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var dragobject={
z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
initialize:function(){
document.onmousedown=this.drag
document.onmouseup=function(){this.dragapproved=0}
},
drag:function(e){
var evtobj=window.event? window.event : e
this.targetobj=window.event? event.srcElement : e.target
if (this.targetobj.className=="drsElement"){
this.dragapproved=1
if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
if (evtobj.preventDefault)
evtobj.preventDefault()
document.onmousemove=dragobject.moveit
}
},
moveit:function(e){
var evtobj=window.event? window.event : e
if (this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
return false
}
}
}

dragobject.initialize()


// welcome popup

popupcontrol('show','showcontent.php?WC_ID=1');


}


// function to toggle layer visibility
function togglelayer(layernumber) {

	var visibilityflag = frames.layers_iframe.document.getElementById('layervisibilityflag'+layernumber);
	var visibilityimage = frames.layers_iframe.document.getElementById('layervisibilityimage'+layernumber);
	var webvisibilityimage = frames.layers_iframe.document.getElementById('webvisibilityimage'+layernumber);
	//alert(visibilityflag);

	if (visibilityflag.value == 1) {
		visibilityimage.src = 'images/layer_off.gif';
		//webvisibilityimage.style.display="hidden";
  		for (var i = 0; i < mylayers[layernumber].length; i++) {
			map.removeOverlay(mylayers[layernumber][i]["marker"]);
 		}
		toggleweb(layernumber,'off');
		visibilityflag.value = 0;

	}

	else if (visibilityflag.value == 0) {
		visibilityimage.src = 'images/layer_on.gif';
		//webvisibilityimage.style.display="block";
  		for (var i = 0; i < mylayers[layernumber].length; i++) {
			map.addOverlay(mylayers[layernumber][i]["marker"]);
 		}
		toggleweb(layernumber,'on');
		visibilityflag.value = 1;
	}


//alert('layer'+webvisibilityimage.style.display);

}


// function to toggle the visibility of webs
function toggleweb(layernumber,force) {

    var visibilityflag = frames.layers_iframe.document.getElementById('layervisibilityflag'+layernumber);
	var webvisibilityflag = frames.layers_iframe.document.getElementById('webvisibilityflag'+layernumber);
	var webvisibilityimage = frames.layers_iframe.document.getElementById('webvisibilityimage'+layernumber);

    //if we are being called from the interface and the layer is off do nothing
    if (!force && visibilityflag.value == 0) return;

	//alert(mywebs[layernumber].length);
	//alert(mywebs[layernumber]);

	if ((webvisibilityflag.value == 1) || (force == "off")) {
		webvisibilityimage.src = 'images/layer_links_off.gif';
  		for (var f = 0; f < mywebs[layernumber].length; f++) {
			//alert(mywebs[layernumber][f]);
            if ((mywebs[layernumber][f] != null) && (mywebs[layernumber][f].length != 0)) {
                map.removeOverlay(mywebs[layernumber][f]);
                map.removeOverlay(mylayers[layernumber][f]["marker"]);
            }
 		}
		webvisibilityflag.value = 0;

	}

	else if ((webvisibilityflag.value == 0) || (force == "on")) {
		webvisibilityimage.src = 'images/layer_links_on.gif';
  		for (var k = 0; k < mywebs[layernumber].length; k++) {

			if ((mywebs[layernumber][k] != null) && (mywebs[layernumber][k].length != 0)) {
				map.addOverlay(mywebs[layernumber][k]);
                map.addOverlay(mylayers[layernumber][k]["marker"]);
			}
 		}
		webvisibilityflag.value = 1;
	}

//alert('web '+webvisibilityimage.style.display);

}



// function to turn layer off
function killlayer(layernumber) {


//alert('Layer feature count is '+ mylayers[layernumber].length);
//alert('Web feature count is '+ mywebs[layernumber].length);

  	for (var i = 0; i < mylayers[layernumber].length; i++) {
		map.removeOverlay(mylayers[layernumber][i]["marker"]);
 	}
  	for (var f = 0; f < mywebs[layernumber].length; f++) {
		// map.removeOverlay(mywebs[layernumber][f]["marker"]);
		map.removeOverlay(mywebs[layernumber][f]);
 	}

	var divIdName = "layerdiv"+layernumber;

//alert('Div ID is '+ divIdName);

	var d = frames.layers_iframe.document.getElementById('legend');

//alert('Div is '+d);

	var olddiv = frames.layers_iframe.document.getElementById(divIdName);
	d.removeChild(olddiv);

}



// this one maps results from the search bar on the left into the legend and onto the google map
function dolayer(mysearch, auto, layername) {

	//alert('start '+layerloading);

	// layer loading semaphore

	if (layerloading == 1) {
		alert('Please wait until the previous map layer has loaded.');
		var layerloading = 0;
		//alert('should change to zero '+layerloading);
		return false;

	}

	layerloading = 1;

	//alert('then '+layerloading);

	// make sure there's a reasonable amount of layers

	layercount = frames.layers_iframe.document.getElementById('layercounter').value;
	if (layercount > 19) {
		alert('You have reached the maximum number of layers.');
		return false;
	}


	if (!auto) {

		// if not an auto load by an admin, we make sure they've given it a name, important for the legend
		layername = frames.searchform_iframe.document.getElementById('layername_input').value;
		if ((layername == null) || (layername.length == 0)) {
			alert('Please specify a name for the layer to be mapped.');
			return false;

		}

	} // end if

   if ((!mysearch || mysearch.length == 0) && (!layername || layername.length == 0)) return false;

	// OK ready to go. Focus on the map and load a loading icon over the top of it:
    	var Icon = new GIcon();
    	Icon.image = "images/loading_bar.gif";
    	Icon.iconSize = new GSize(220, 19);
    	Icon.iconAnchor = new GPoint(110, 10);

    	var loadingbar = new GMarker(map.getCenter(), Icon);
	map.addOverlay(loadingbar);

    //

	// switch the right-hand panel of the application to the layers list
	changeright('layers');

	// repackage the search term to point towards the xml outputter
	xmlsource = repackagesearch(mysearch);

	//now we get the google map to plot it
	// first setting the appropriate icon
	var colorarray = new Array('red','blue','green','darkorange','magenta','yellow','khaki','black','brown','grey','aqua','chartreuse','purple','salmon','khaki','goldenrod','firebrick','magenta','slategrey','chocolate');

	// establish a parent array that will define layers
	mylayers[layercount] = new Array();

	// establish a parent array that will define webs of lines
	mywebs[layercount] = new Array();

	// establish a crime scene array that will define points to draw lines towards
	var crimescenes = new Array();

	// establish a crime scene array that will define points to draw lines towards
	myvertices[layercount] = new Array();

	// establish a parent array that will define map icons
	mymapicons[layercount] = new Array();

	// then use google's ajax thing stick it on the map
	// this code very messy and inefficient, must clean or rewrite.



	GDownloadUrl(xmlsource, function(data, responseCode) {
  		mylayers[layercount]["xml"] = GXml.parse(data);
  		mylayers[layercount]["markers"] = mylayers[layercount]["xml"].documentElement.getElementsByTagName("event");

//alert('44');

		// loop through each of the records returned by our script
  		for (var i = 0; i < mylayers[layercount]["markers"].length; i++) {

			// establish the subarray that will hold individual records
			mylayers[layercount][i] = new Array();

			// establish the subarray that will hold individual webs
			mywebs[layercount][i] = new Array();

			// establish the individual markers
			mymapicons[layercount][i] = new GIcon();
			mymapicons[layercount][i].iconSize = new GSize(24, 24);
			mymapicons[layercount][i].iconAnchor = new GPoint(12, 12);
			mymapicons[layercount][i].infoWindowAnchor = new GPoint(10, 10);

			//retrieve the lat and long vars
			mylayers[layercount][i]["point"] = new GLatLng(parseFloat(mylayers[layercount]["markers"][i].getAttribute("lng")), parseFloat(mylayers[layercount]["markers"][i].getAttribute("lat")));

			// get the principal type identifier... is it a mapped event, or just an address. check for 'ad' or 'ev'
			mylayers[layercount][i]["rectype"] = String(mylayers[layercount]["markers"][i].getAttribute("type"));
			mylayers[layercount][i]["addressid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ad_id"));


			//if it's an event
			if (mylayers[layercount][i]["rectype"]=="ev") {
				// get the vars related specifically to events: type, id, linkage type to address, and whether it's the main crime location
				mylayers[layercount][i]["evtype"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("evtype"));
				mylayers[layercount][i]["eventid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ev_id"));
				mylayers[layercount][i]["eventadid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("evad_id"));
				mylayers[layercount][i]["ismain"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("main"));


				// now checking to see whether it's the "main" crime location, symbolise appropriately, otherwise use secondary symbol
				if (mylayers[layercount][i]["ismain"]=="1") {
					mymapicons[layercount][i].image = "getIcon.php?evtype="+mylayers[layercount][i]["evtype"]+"&color="+colorarray[layercount];
					mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
					map.addOverlay(mylayers[layercount][i]["marker"]);

					//here we keep the lat/long and main status in as vars in the crimescene array, so that subsequent
					//non-main addresses can look it up and draw a line back to it to create webs
					crimescenes[mylayers[layercount][i]["eventid"]] = mylayers[layercount][i]["point"];

				}

				else if (mylayers[layercount][i]["ismain"]=="0") {



					//get some potential variables related to non-main events
					mylayers[layercount][i]["offadd"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("isoffenderaddress"));

					//now let's establish its relationship to the crime scene, if any, and draw the web
					if (crimescenes[mylayers[layercount][i]["eventid"]]) {
						//alert('and has a relationship to a main...');

						if (mylayers[layercount][i]["offadd"] =="1") {

							//then it's the offender's address and should be symbolised as a residence

							mymapicons[layercount][i].image = "getIvIcon.php?ivtype=99&color="+colorarray[layercount];
							mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);



						} else {


							//not a crime scene or an offenders address, just a related event e.g. an arrest. give it a generic icon.

							mymapicons[layercount][i].image = "getIcon.php?evtype=60&color="+colorarray[layercount];
							mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
							map.addOverlay(mylayers[layercount][i]["marker"]);


						}


						// now draw the connecting line for the web
						myvertices[layercount][i] = new Array();
						myvertices[layercount][i].push(crimescenes[mylayers[layercount][i]["eventid"]]);
						myvertices[layercount][i].push(mylayers[layercount][i]["point"]);

						mywebs[layercount][i] = new GPolyline(myvertices[layercount][i], colorarray[layercount], 3);
						//map.addOverlay(mywebs[layercount][i]);

					}


					else {
						//alert('and has NO relationship to a main...');
						mymapicons[layercount][i].image = "getIcon.php?evtype="+mylayers[layercount][i]["evtype"]+"&color="+colorarray[layercount];
						mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
						map.addOverlay(mylayers[layercount][i]["marker"]);
					}
				}

			//	map.addOverlay(mylayers[layercount][i]["marker"]);
				mylayers[layercount][i]["marker"].bindInfoWindowHtml("<iframe src=\"eventbubble.php?EV_ID="+mylayers[layercount][i]["eventid"]+"&LNK_EVAD_ID="+mylayers[layercount][i]["eventadid"]+"&AD_ID="+mylayers[layercount][i]["addressid"]+"\" style=\"margin-right:10px;\" frameborder=\"0\"/>");

			}

			else if (mylayers[layercount][i]["rectype"]=="ad") {
				mymapicons[layercount][i].image = "getIcon.php?evtype=49&color="+colorarray[layercount];
				mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
				map.addOverlay(mylayers[layercount][i]["marker"]);
				mylayers[layercount][i]["marker"].bindInfoWindowHtml("<iframe src=\"addressbubble.php?AD_ID="+mylayers[layercount][i]["addressid"]+"\" style=\"margin-right:10px;\" frameborder=\"0\"/>");

			}  //end if event or address


			// now let's process the results for searches for an individual
			else if (mylayers[layercount][i]["rectype"]=="iv") {

				// establish the variables particular to this record type
				mylayers[layercount][i]["rec2type"] = String(mylayers[layercount]["markers"][i].getAttribute("type2"));
				mylayers[layercount][i]["individualid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("iv_id"));
				mylayers[layercount][i]["ivtype"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ivtype"));
				mylayers[layercount][i]["start"] = String(mylayers[layercount]["markers"][i].getAttribute("start_date"));
				mylayers[layercount][i]["end"] = String(mylayers[layercount]["markers"][i].getAttribute("end_date"));

				// establish an empty array to hold all of his/her residences, if it doesn't already exist
				if (!residences[mylayers[layercount][i]["individualid"]]) {
					residences[mylayers[layercount][i]["individualid"]] = new Array();
					residence_starts[mylayers[layercount][i]["individualid"]] = new Array();
					residence_ends[mylayers[layercount][i]["individualid"]] = new Array();
				}

				// this is for if it's just an eventless address, e.g. a residence
				if (mylayers[layercount][i]["rec2type"]=="ad") {

					mylayers[layercount][i]["ismain"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("main"));
					mylayers[layercount][i]["ivadid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ivad_id"));

					if (mylayers[layercount][i]["ismain"]=="1") {

						//here we keep the lat/long and start/end in as vars in the residences array, so that subsequent
						//non-main addresses can look it up and draw a line back to it to create webs

						residences[mylayers[layercount][i]["individualid"]].push(mylayers[layercount][i]["point"]);
						residence_starts[mylayers[layercount][i]["individualid"]].push(mylayers[layercount][i]["start"]);
						residence_ends[mylayers[layercount][i]["individualid"]].push(mylayers[layercount][i]["end"]);


					} // end of mylayers[layercount][i]["ismain"]=1

					else if (mylayers[layercount][i]["ismain"]=="0") {

						if (residences[mylayers[layercount][i]["individualid"]].length > 0) {
							//alert('and has a relationship to a main...');
							//not a residence just a related event e.g. an arrest.

							myvertices[layercount][i] = new Array();

							// loop through each of the records returned by our script
  							for (var r = 0; r < residences[mylayers[layercount][i]["individualid"]].length; r++) {

								//checking that the dates are congruent
								if (
								    //condition 1 to establish temporal congruency
								    ((mylayers[layercount][i]["start"] <= residence_starts[mylayers[layercount][i]["individualid"]][r])
								    &&
								    (mylayers[layercount][i]["end"] >= residence_starts[mylayers[layercount][i]["individualid"]][r]))
								    ||
								    //condition 2
								    ((mylayers[layercount][i]["start"] >= residence_starts[mylayers[layercount][i]["individualid"]][r])
								    &&
								    (mylayers[layercount][i]["start"] <= residence_ends[mylayers[layercount][i]["individualid"]][r]))

								        ) {
									// now draw the connecting line for the web
									myvertices[layercount][i].push(mylayers[layercount][i]["point"]);
									myvertices[layercount][i].push(residences[mylayers[layercount][i]["individualid"]][r]);
									myvertices[layercount][i].push(mylayers[layercount][i]["point"]);

								}
							}


							mywebs[layercount][i] = new GPolyline(myvertices[layercount][i], colorarray[layercount], 3);
							map.addOverlay(mywebs[layercount][i]);



						}

					} // end of mylayers[layercount][i]["ismain"]=0


				//alert("getIvIcon.php?ivtype="+mylayers[layercount][i]["ivtype"]+"&color="+colorarray[layercount]);
				mymapicons[layercount][i].image = "getIvIcon.php?ivtype="+mylayers[layercount][i]["ivtype"]+"&color="+colorarray[layercount];
				mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
				mylayers[layercount][i]["marker"].bindInfoWindowHtml("<iframe src=\"individualbubble_ad.php?IV_ID="+mylayers[layercount][i]["individualid"]+"&LNK_IVAD_ID="+mylayers[layercount][i]["ivadid"]+"&AD_ID="+mylayers[layercount][i]["addressid"]+"\" style=\"margin-right:10px;\" frameborder=\"0\"/>");


				} // end of individual as address


				else if (mylayers[layercount][i]["rec2type"]=="ev") {
//alert('iv ev');
					mylayers[layercount][i]["eventid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ev_id"));
					mylayers[layercount][i]["eventadid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("evad_id"));
					mylayers[layercount][i]["ivevid"] = parseInt(mylayers[layercount]["markers"][i].getAttribute("ivev_id"));

						if (residences[mylayers[layercount][i]["individualid"]].length > 0) {
							//alert('and has a relationship to a main...');
							//not a residence just a related event e.g. an arrest.

							myvertices[layercount][i] = new Array();

							// loop through each of the records returned by our script
  							for (var r = 0; r < residences[mylayers[layercount][i]["individualid"]].length; r++) {

								//checking that the dates are congruent
								if (
								    //condition 1 to establish temporal congruency
								    ((mylayers[layercount][i]["start"] <= residence_starts[mylayers[layercount][i]["individualid"]][r])
								    &&
								    (mylayers[layercount][i]["end"] >= residence_starts[mylayers[layercount][i]["individualid"]][r]))
								    ||
								    //condition 2
								    ((mylayers[layercount][i]["start"] >= residence_starts[mylayers[layercount][i]["individualid"]][r])
								    &&
								    (mylayers[layercount][i]["start"] <= residence_ends[mylayers[layercount][i]["individualid"]][r]))

								        ) {
									// now draw the connecting line for the web
									myvertices[layercount][i].push(mylayers[layercount][i]["point"]);
									myvertices[layercount][i].push(residences[mylayers[layercount][i]["individualid"]][r]);
									myvertices[layercount][i].push(mylayers[layercount][i]["point"]);

								}
							}


							mywebs[layercount][i] = new GPolyline(myvertices[layercount][i], colorarray[layercount], 3);
							map.addOverlay(mywebs[layercount][i]);
                            frames.layers_iframe.document.getElementById('webvisibilityimage'+layercount).src = 'images/layer_links_on.gif';
                            frames.layers_iframe.document.getElementById('webvisibilityflag'+layercount).value = 1;

						}

					mymapicons[layercount][i].image = "getIvIcon.php?ivtype="+mylayers[layercount][i]["ivtype"]+"&color="+colorarray[layercount];
					mylayers[layercount][i]["marker"] = new GMarker(mylayers[layercount][i]["point"], mymapicons[layercount][i]);
					mylayers[layercount][i]["marker"].bindInfoWindowHtml("<iframe src=\"individualbubble_ev.php?IV_ID="+mylayers[layercount][i]["individualid"]+"&EV_ID="+mylayers[layercount][i]["eventid"]+"&LNK_EVAD_ID="+mylayers[layercount][i]["eventadid"]+"&LNK_IVEV_ID="+mylayers[layercount][i]["ivevid"]+"&AD_ID="+mylayers[layercount][i]["addressid"]+"\" style=\"margin-right:10px;\" frameborder=\"0\"/>");

				} // end of individual as event


				map.addOverlay(mylayers[layercount][i]["marker"]);


			} // end of type individual processing

  		} // end of record loop

	map.removeOverlay(loadingbar);


	}); //end of gdownloadurl


	// OK now lets add it to the legend.
	//addElement(layername, 'getIcon.php?evtype=49&color='+colorarray[layercount], 'legendentry', layercount);
	var ni = frames.layers_iframe.document.getElementById('legend');
	var newdiv = frames.layers_iframe.document.createElement('div');
	var divIdName = "layerdiv"+layercount;
	newdiv.setAttribute('id',divIdName);
	newdiv.setAttribute('class','legendentry');
	newdiv.className = 'legendentry';
	newdiv.innerHTML = "<div class='legendimages'><div class='legendimage'><a href='javascript:void(0)' onClick='parent.killlayer("+layercount+")'><img src='images/layer_close.gif'></a></div><div class='legendimage'><a href='javascript:void(0)' onClick='parent.togglelayer("+layercount+")'><img id='layervisibilityimage"+layercount+"' src='images/layer_on.gif'></a><input type='hidden' id='layervisibilityflag"+layercount+"' value='1'></div><div class='legendimage'><a href='javascript:void(0)' onClick='parent.toggleweb("+layercount+")'><img id='webvisibilityimage"+layercount+"' src='images/layer_links_off.gif'></a><input type='hidden' id='webvisibilityflag"+layercount+"' value='0'></div></div><div class='legendimage_mapicon' style='background:"+colorarray[layercount]+";'></div><p class='layertitle'>"+layername+"</p>";
	ni.appendChild(newdiv);


	if (!auto) {

		// get rid of the map button
		frames.searchform_iframe.document.getElementById('mapper').style.visibility = 'hidden';

	}


	// ... and finally flag concretely the number of layers in the mapspace
	layercount = parseInt(layercount);
	mynewlayercount = (layercount + 1);
	frames.layers_iframe.document.getElementById('layercounter').value = mynewlayercount;


	// reset layer loading semaphore
	layerloading = 0;
	//alert('finish '+layerloading);
	return true;

}







// Here we set an arbitrary limit on records on-screen

function checkrecordcount(resultcount) {


	if (resultcount == 0) {



	}

	else {

		if (resultcount < 10001) {
			document.write("<div id='mapper'><p class='searchresults'>Provide a layer name</p>");
			document.write("<input type='text' name='layername_input' class='layername_input' id='layername_input' value='' title='Layer Name'" +
                            "onkeypress='if (event.which == 13 || event.keycode == 13 ) setTimeout(function(){return parent.dolayer(self.location.href);},0);'>");
			document.write("<input value='Map &gt;' class='button' type='button' onclick='return parent.dolayer(self.location.href)'></div>");
		}

		if (resultcount > 10000) {
			document.write("<p class='searchresults'>Too many records to map in a layer (over 1000); please refine your search.</p>");
		}
	}

}



// this one changes the url of a given iframe

function changesearch(iframedest,position) {

	frames.searchform_iframe.document.location.href = iframedest;

	//alert(document.getElementById('arrowcontainer_left').style.backgroundImage);

	if (position == "left") {document.getElementById('arrowcontainer_left').style.backgroundImage="url(images/downarrow_left.gif)"};
	if (position == "middle") {document.getElementById('arrowcontainer_left').style.backgroundImage="url(images/downarrow_middle.gif)"};
	if (position == "right") {document.getElementById('arrowcontainer_left').style.backgroundImage="url(images/downarrow_right.gif)"};


}

function changeright(dest) {

	//alert(document.getElementById('arrowcontainer_right').style.backgroundImage);

	if (dest == 'layers') {
		document.getElementById('features_iframe').style.display = 'none';
		document.getElementById('legend_iframe').style.display = 'none';
		document.getElementById('layers_iframe').style.display = 'block';
		document.getElementById('arrowcontainer_right').style.backgroundImage="url(images/downarrow_middle.gif)";
	}

	if (dest == 'features') {
		document.getElementById('legend_iframe').style.display = 'none';
		document.getElementById('layers_iframe').style.display = 'none';
		document.getElementById('features_iframe').style.display = 'block';
		document.getElementById('arrowcontainer_right').style.backgroundImage="url(images/downarrow_left.gif)";
	}

	if (dest == 'legend') {
		document.getElementById('features_iframe').style.display = 'none';
		document.getElementById('layers_iframe').style.display = 'none';
		document.getElementById('legend_iframe').style.display = 'block';
		document.getElementById('arrowcontainer_right').style.backgroundImage="url(images/downarrow_right.gif)";

		//alert(frames.legend_iframe.document.location.href);
		if (frames.legend_iframe.document.location.href !== 'http://acl.arts.usyd.edu.au/harlem/legend.php') {
			frames.legend_iframe.document.location.href = 'legend.php';
			//alert('changing');
		}


	}


}



// takes the current URL of the page, clips it after the ? symbol,
// then replaces the search name with another and calls a new URL. the function
// is useful for passing search results to a new page for processing, e.g. to map them

function repackagesearch(oldurl) {

		// first we need to strip everything up to and including the last slash.
		//alert('Repackaging '+oldurl);
		var clipstart = ((oldurl.lastIndexOf('/'))+1);
		var clipend = oldurl.length
		mycall = oldurl.substring(clipstart,clipend)

		//stick the new url on the front
		mycall = 'xml_'+mycall;

		//get rid of those pesky &amp; things
		getridofme = '&amp;'
		getridofme = '/'+getridofme+'/g';
		putmein = '&'
		mycall = mycall.replace(eval(getridofme),putmein);

		//mycall = 'http://acl.arts.usyd.edu.au/harlem/'+mycall;
		//alert('Calling '+mycall);

		return mycall;

}


function popupcontrol(action,url) {

	//alert('OK');
	//alert('popupminmaxstate '+popupminmaxstate);
	//alert('popupnarrowstate '+popupnarrowstate);

	if (action=="hide") {
		document.getElementById('maincontentpopup').style.display = 'none';
		frames.popupcontent.document.location.href = 'blank.html';
	}

	if (action=="show") {
		document.getElementById('maincontentpopup').style.display = 'block';
		frames.popupcontent.document.location.href = url;
		map.closeInfoWindow();
	}

	if (action=="minmax") {

		if (popupminmaxstate=="max") {
			//alert('maxsize');
			document.getElementById('popupcontent').style.width = '110px';
			document.getElementById('popupcontent').style.height = '40px';
			document.getElementById('maincontentpopup').style.width = '120px';
			document.getElementById('maincontentpopup').style.height = '80px';
			popupminmaxstate="min";
			popupnarrowstate="max";
		}

		else if (popupminmaxstate=="min") {
			//alert('minsize');
			document.getElementById('popupcontent').style.width = '370px';
			document.getElementById('popupcontent').style.height = '340px';
			document.getElementById('maincontentpopup').style.width = '380px';
			document.getElementById('maincontentpopup').style.height = '380px';
			popupminmaxstate="max";
			popupnarrowstate="max";
		}


	}

	if (action=="narrow") {

		if (popupnarrowstate=="max") {
			//alert('maxnarrow');
			document.getElementById('popupcontent').style.width = '200px';
			document.getElementById('popupcontent').style.height = '340px';
			document.getElementById('maincontentpopup').style.width = '210px';
			document.getElementById('maincontentpopup').style.height = '380px';
			popupminmaxstate="max";
			popupnarrowstate="min";
		}

		else if (popupnarrowstate=="min") {
			//alert('minnarrow');
			document.getElementById('popupcontent').style.width = '370px';
			document.getElementById('popupcontent').style.height = '340px';
			document.getElementById('maincontentpopup').style.width = '380px';
			document.getElementById('maincontentpopup').style.height = '380px';
			popupminmaxstate="max";
			popupnarrowstate="max";
		}


	}


}


function keywordsearch() {

	var searchval = document.getElementById('keywordsearch').value;
	searchstring = "http://acl.arts.usyd.edu.au/harlem/eventsearch.php?_EVENTSEARCH_FRONTEND_search=1&EVENTSEARCH_FRONTEND_EV_KEYWORD="+searchval;
	alert(searchstring);
	changesearch(searchstring,'left');


}


function savesearch() {

	if (frames.searchform_iframe.document.getElementById('searchedflag')) {
		var searchtype = frames.searchform_iframe.document.getElementById('searchedflag').value;
		var searchname = frames.searchform_iframe.document.getElementById('layername_input').value;
		var searchstring = frames.searchform_iframe.document.location.href;

		if ((searchname.length == 0) || (searchname == null)) {
			alert('Specify a layer name first');
			return false;
		}


		frames.popupcontent.document.getElementById('search_type').value = searchtype;
		frames.popupcontent.document.getElementById('search_name').value = searchname;
		frames.popupcontent.document.getElementById('search_string').value = searchstring;

		frames.popupcontent.document.forms[0].submit();


	} else {

		alert('No active searches to save. Do a search first.')
	};
}
