var site_url = 'http://www.dnakids.co.uk/';

/* OMG make javascript functions returned by ajax parse-able by adding them to the DOM! */
function parseScript(_source) {
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
	
	// Return the cleaned source
	return source;
	
	alert('source')
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  // eval(targ+".location.href='http://dna.asteriks-dev.co.uk/"+selObj.options[selObj.selectedIndex].value+"'");
  eval(targ+".location.href='http://www.dnakids.co.uk/"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function showpuzzle(thetype){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReq.open("GET","getpuzzlee.html?flashsrc="+thetype+thetime,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("puzzlediv");
		res.innerHTML = parseScript(liveSearchReq.responseText);
	}
}

/* for the region map */
var startzindex = '2';

function resetto(targetName)
{
	document.getElementById(targetName).style.zIndex = startzindex;
	startzindex++;
}


function validate_form(theform)
{
	if(Trim(document.getElementById("fullname").value)=="") {
					alert("Please enter your full name");
					document.getElementById("fullname").focus();
					return false;
	}
	if(Trim(document.getElementById("mobtel").value)=="") {
					alert("Please enter a contact telephone number");
					document.getElementById("mobtel").focus();
					return false;
	}
	if(Trim(document.getElementById("email").value)=="") {
					alert("Please enter an email address");
					document.getElementById("email").focus();
					return false;
	}
	if(Trim(document.getElementById("dateob").value)=="") {
					alert("Please enter your date of birth");
					document.getElementById("dateob").focus();
					return false;
	}
	if(Trim(document.getElementById("makemodel").value)=="") {
					alert("Please enter your vehicle make and model");
					document.getElementById("makemodel").focus();
					return false;
	}
	if(Trim(document.getElementById("own_vehicle").value)=="") {
					alert("Please state whether you own your vehicle");
					document.getElementById("own_vehicle").focus();
					return false;
	}
	if(Trim(document.getElementById("purchase").value)=="") {
					alert("Please state if you are in a position to purchase your own disco equipment in the near future");
					document.getElementById("purchase").focus();
					return false;
	}

	return true;
}

function Trim(myval)
{
                var chklen=myval.length; 
                var pos=0;
                mychar = myval.charAt(0);

                while(pos>=0 || lstpos >=0)
                {
                                pos=myval.indexOf(" ");
                                if (pos==0)
                                {
                                                myval=myval.substring(1,chklen);
                                                chklen = myval.length;
                                                mychar = myval.charAt(0);
                                }
                                lstpos=myval.lastIndexOf(" ");

                                if (lstpos==chklen-1)
                                {               
                                                myval=myval.substring(0,chklen-1);
                                                chklen=myval.length;
                                                mychar = myval.charAt(chklen-1);
                                }
                                
                                if(mychar!=" ")
                                                break;

                }
                return myval;                                        
}



/* START MAP */
var map;

function initialize() {
	if(document.getElementById("mapdiv")){
		if (GBrowserIsCompatible()) {
			/* set up map and navigation */
			map = new GMap(document.getElementById("mapdiv"));
			//map.addControl(new GLargeMapControl());
			//map.addControl(new GMapTypeControl());
			map.centerAndZoom(new GLatLng(51.5311,-0.6742), 9);
			
			GEvent.addListener(map, "movestart", function() {
				map.clearOverlays();
			});
			
			GEvent.addListener(map, "moveend", function() {
				checkpostcodes(map);
			});
			// load the icons
			checkpostcodes(map);
		}
	}
}


function checkpostcodes(map){
	/* Get Map Boundaries */
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var nelat = northEast.lat();
	var nelong = northEast.lng();
	var swlat = southWest.lat();
	var swlong = southWest.lng();

	/* query postcodes in view at current zoom level */
	if (window.XMLHttpRequest) {
		liveSearchReq_ = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq_ = new ActiveXObject("Microsoft.XMLHTTP");	
	}
	
	var thetime= "?thetime="+new Date().getTime();
	liveSearchReq_.open("GET","boundaries.html"+thetime+"&nelat="+nelat+"&nelong="+nelong+"&swlat="+swlat+"&swlong="+swlong,false);
	liveSearchReq_.send(null);
	

	if (liveSearchReq_.readyState == 4) {
		var res = document.getElementById("theout");
		res.innerHTML = parseScript(liveSearchReq_.responseText);

		if(document.getElementById("thestring")) {
			var array = document.getElementById("thestring").value.split(',');
			var dnamearray = document.getElementById("thedealerstring").value.split(',');
			
			// start from i=1 to ignore the first item in the list (XXX,)
			// set array to hold markers
			markers = {};
			//array for each point
			for(var i=1; i<array.length; i++){ 
				//split array into 4 parts data for each point
				var subarray = array[i].split('^');
				var point = new GLatLng(subarray[0],subarray[1]);
				
				// custom icon shennanegins
				var useIcon = new GIcon();
				//useIcon.image = site_url+'images/pegs/peg_'+subarray[2]+'_off.png';
				//useIcon.iconSize = new GSize(53,59);
				//useIcon.iconAnchor = new GPoint(27,59);
				useIcon.image = site_url+'images/pegs/googlemap_pin_65.png';
				useIcon.shadow = null;				
				useIcon.iconSize = new GSize(33,36);
				useIcon.iconAnchor = new GPoint(14,36);
				
				markerOptions = {icon:useIcon};
				
				//set variable marker name
				var marker = new GMarker(point,markerOptions);
				
				//add on map
				map.addOverlay(marker);
				
				//add to array				
				markers[subarray[3]] = marker;

				// set listener
				GEvent.addListener(markers[subarray[3]], 'mouseover',
					(function(id,colour,fontcolour) {
						return function() {
							display(id,colour,fontcolour);
						};
					})(subarray[3],'#F7D30D','#FFFF00')
				);
				
				// set listener
				GEvent.addListener(markers[subarray[3]], 'mouseout',
					(function(id,colour,fontcolour) {
						return function() {
							display(id,colour,fontcolour);
						};
					})(subarray[3],'#0198FF','#ffffff')
				);
				
				// set listener
				GEvent.addListener(markers[subarray[3]], 'mouseup',
					(function(id) {
						return function() {
							fullinfo(id);
						};
					})(subarray[3])
				);				
				
				var theloc = dnamearray[i];
				
				/*
				// set listener
				GEvent.addListener(markers[subarray[3]], 'click',
					(function(theloc) {
						return function() {
							document.location.href=theloc;
						};
					})(site_url+theloc+'/')
				);
				*/
			}
		}
	}		
}


function display(id,colour,fontcolour) {
	//var thediv = "venue"+id;
	//document.getElementById(thediv).style.backgroundColor=colour;
	if (document.getElementById("venue"+id)) {
		document.getElementById("venue"+id).style.color=fontcolour;
	}
}


function fullinfo(id){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReq.open("GET","fullinfo.html?id="+id+thetime,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("theout");
		res.innerHTML = parseScript(liveSearchReq.responseText);
	}
}


function showmap(){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
		latReq = new XMLHttpRequest();
		longReq = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
		latReq = new ActiveXObject("Microsoft.XMLHTTP");
		longReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var thetime= "&thetime="+new Date().getTime();
	latReq.open("GET","get_latitude.html?id="+document.getElementById("county_id").value+thetime,false);
	latReq.send(null);
	
	longReq.open("GET","get_long.html?id="+document.getElementById("county_id").value+thetime,false);
	longReq.send(null);
	
	liveSearchReq.open("GET","show_map.html?id="+document.getElementById("county_id").value+thetime,false);
	liveSearchReq.send(null);	

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("largemap");
		res.innerHTML = parseScript(liveSearchReq.responseText);
	}
	
	initialize();
	map.centerAndZoom(new GLatLng(latReq.responseText,longReq.responseText), 7);
}

// clear point array
var markers = {};
/* END MAPS */



