var site_url = 'http://www.cooperbmw.co.uk/';

function gotodealership() {
	document.location.href='dealerships/';
}

function gotoused() {
	document.location.href='used/';
}

function gotothedealership() {
	document.location.href=site_url+document.getElementById("thedealership").value;
}

/* START MAP */
/* enable jscript in returned ajax content */
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) {}
	}
	/* Return cleaned source */
	return source;
}


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(53.52895,-2.2336), 11);
			
			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(G_DEFAULT_ICON);
				useIcon.iconSize = new GSize(20,23);
				useIcon.iconAnchor = new GPoint(10, 23);
				useIcon.shadow = null;	
				useIcon.image = site_url+'images/pegs/peg_'+subarray[2]+'_off.png';
				
				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) {
						return function() {
							display(id,colour);
						};
					})(subarray[3],'#3379A4')
				);
				
				// set listener
				GEvent.addListener(markers[subarray[3]], 'mouseout',
					(function(id,colour) {
						return function() {
							display(id,colour);
						};
					})(subarray[3],'#cccccc')
				);
				
				var theloc = dnamearray[i];
				
				// set listener
				GEvent.addListener(markers[subarray[3]], 'click',
					(function(theloc) {
						return function() {
							document.location.href=theloc;
						};
					})('http://www.cooperbmw.co.uk/'+theloc+'/')
				);
			}
		}
	}		
}


function display(id,colour) {
	var thediv = "dealer"+id;
	document.getElementById(thediv).style.backgroundColor=colour;
}


// clear point array
var markers = {};
/* END MAPS */


function signup(dealerid) {
	if(document.getElementById('signemail').value == '' | document.getElementById('signname').value == '') {
		alert('Please enter your name and a valid email address.');
	} else {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
	
		thestring = '&id='+dealerid;
	
		try {
			if (document.getElementById('signemail').value != '') {
				thestring = thestring+'&signemail='+document.getElementById('signemail').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('signname').value != '') {
				thestring = thestring+'&signname='+document.getElementById('signname').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('signtel').value != '') {
				thestring = thestring+'&signtel='+document.getElementById('signtel').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('signcomments').value != '') {
				thestring = thestring+'&signcomments='+document.getElementById('signcomments').value;
			}
		} catch (err) {}	
		
		thetime= '?thetime='+new Date().getTime();
		liveSearchReq.open('GET','queryform.cfm'+thetime+thestring,false);
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			resultbox = document.getElementById('contactus');
			resultbox.innerHTML = liveSearchReq.responseText;
		}		
	}
}


function service() {
	if(document.getElementById('servicefullname').value == '' | document.getElementById('servicetel').value == '' | document.getElementById('serviceemail').value == '' | document.getElementById('servicecarreg').value == '' | document.getElementById('servicedealership').value == 'XXXX') {
		alert('Please enter your name, a valid email address, contact number and select a dealership.');
	} else {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
	
		thestring = '';
	
		try {
			if (document.getElementById('servicefullname').value != '') {
				thestring = thestring+'&servicefullname='+document.getElementById('servicefullname').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicetel').value != '') {
				thestring = thestring+'&servicetel='+document.getElementById('servicetel').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('serviceemail').value != '') {
				thestring = thestring+'&serviceemail='+document.getElementById('serviceemail').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicepref_date').value != '') {
				thestring = thestring+'&servicepref_date='+document.getElementById('servicepref_date').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicepref_time').value != '') {
				thestring = thestring+'&servicepref_time='+document.getElementById('servicepref_time').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicecarreg').value != '') {
				thestring = thestring+'&servicecarreg='+document.getElementById('servicecarreg').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicemodel_range').value != '') {
				thestring = thestring+'&servicemodel_range='+document.getElementById('servicemodel_range').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicemodel_var').value != '') {
				thestring = thestring+'&servicemodel_var='+document.getElementById('servicemodel_var').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicedealership').value != '') {
				thestring = thestring+'&servicedealership='+document.getElementById('servicedealership').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('servicequery').value != '') {
				thestring = thestring+'&servicequery='+document.getElementById('servicequery').value;
			}
		} catch (err) {}

		thetime= '?thetime='+new Date().getTime();
		liveSearchReq.open('GET','service_process.cfm'+thetime+thestring,false);
		liveSearchReq.send(null);
		
		if (liveSearchReq.readyState == 4) {
			/*
			resultbox = document.getElementById("booking");
			resultbox.innerHTML = parseScript(liveSearchReq.responseText);
			*/
			document.location.href='service_ok.html'
		}
	}
}


function getvariant(type,thestring) {
	if (type != 'XXXX') {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
	
		thetime= '?thetime='+new Date().getTime();
		liveSearchReq.open('GET','variant_div.cfm'+thetime+'&type='+type+thestring,false);
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			resultbox = document.getElementById('variantdiv');
			resultbox.innerHTML = liveSearchReq.responseText;
		}
	} else {
		alert('Please select a Valid Model Type')
	}
}


function newcar(model,thewidth) {
	if(document.getElementById('newname').value == '' | document.getElementById('newemail').value == '' | document.getElementById('newdealership').value == '') {
		resultbox = document.getElementById('enquire');
		resultbox.style.borderColor = '#3379A4';
		alert('Please enter your name, a valid email address and select a dealership.');		
	} else {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
	
		thestring = '&model='+model;
	
		try {
			if (document.getElementById('newname').value != '') {
				thestring = thestring+'&newname='+document.getElementById('newname').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('newtel').value != '') {
				thestring = thestring+'&newtel='+document.getElementById('newtel').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('newemail').value != '') {
				thestring = thestring+'&newemail='+document.getElementById('newemail').value;
			}
		} catch (err) {}


		try {
			if (document.getElementById('newdealership').value != '') {
				thestring = thestring+'&newdealership='+document.getElementById('newdealership').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('newcomments').value != '') {
				thestring = thestring+'&newcomments='+document.getElementById('newcomments').value;
			}
		} catch (err) {}		

		thetime= '?thetime='+new Date().getTime();
		if (thewidth == 'wide'){
			liveSearchReq.open('GET','new_enquire_process.cfm'+thetime+thestring,false);
		} else {
			liveSearchReq.open('GET','new_enquire_process_wide.cfm'+thetime+thestring,false);
		}
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			resultbox = document.getElementById('enquire');
			resultbox.innerHTML = parseScript(liveSearchReq.responseText);
		}		
	}
}


function mailto() {
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
	}
		
	thetime= '?thetime='+new Date().getTime();
	liveSearchReq.open('GET','formate.cfm'+thetime,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		resultbox = document.getElementById('enquire');
		resultbox.style.borderColor = '#3379A4';
		resultbox.innerHTML = liveSearchReq.responseText;
	}		
}


function formate() {
	if(document.getElementById('email1').value == '' && document.getElementById('email2').value == '') {
		alert('Please complete at least one email address.');
	} else {
		if (window.XMLHttpRequest) {
			call12 = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			call12 = new ActiveXObject('Microsoft.XMLHTTP');
		}

		try {
			if (document.getElementById('page')) {
				thestring = '&carreg='+document.getElementById('page').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('email1').value != '') {
				thestring = thestring+'&email1='+document.getElementById('email1').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('email2').value != '') {
				thestring = thestring+'&email2='+document.getElementById('email2').value;
			}
		} catch (err) {}

		thetime= '?thetime='+new Date().getTime();
		call12.open('GET','mailfull.cfm'+thetime+thestring,false);
		call12.send(null);

		if (call12.readyState == 4) {
			resultbox = document.getElementById('enquire');
			resultbox.innerHTML = parseScript(call12.responseText);
		}
	}
}


/* handles the search */
function dosearch(howmany){
	// if doing a model/var search
	if (document.forms.thesearch.type) {
		var thepage = document.forms.thesearch.type.value;
	} else {
		var thepage = 'XXXX';
	}
	
	if (document.forms.repage) {
		var repage = document.forms.repage.toshow.value;
	} else {
		var repage = 5;
	}
	
	var  res = document.getElementById("results");
	res.innerHTML = '<div style="width:150px; margin: 0 auto; padding-top:150px;" align="center"><img src="1.gif" alt="Loading cars"><br /><br />Loading next '+repage+' cars</div>';
	
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	/* check for special or used age listings OR crit values OR model_type - set thestring as a default blank */
	var thestring =""
	
	try {
		if (document.forms.thesearch.used) {
			var thestring = thestring+"&used="+document.forms.thesearch.used.value
		}
	} catch (err) {}
	
	try {
		if (document.forms.thesearch.special) {
			var thestring = thestring+"&special="+document.forms.thesearch.special.value
		}
	} catch (err) {}
	
	try {
		if (document.forms.thesearch.model_type) {
			var thestring = thestring+"&model_type="+document.forms.thesearch.model_type.value
		}
	} catch (err) {}
	
	try {
		if (document.forms.thesearch.type) {
			var thestring = thestring+"&type="+document.forms.thesearch.type.value
		}
	} catch (err) {}	
	
	try {
		if (document.forms.thesearch.topvar_type) {
			var thestring = thestring+"&topvar_type="+document.forms.thesearch.topvar_type.value
		}
	} catch (err) {}	
	
	try {
		if (document.forms.thesearch.the_dealername.value) {
			var thestring = thestring+"&the_dealername="+document.forms.thesearch.the_dealername.value
		}
	} catch (err) {}	
	
	
	var thecolours = ""
	
	if (document.forms.thesearch.chosencolours) {
		/* build a string of colour selections */
		for (i=0; i<document.forms.thesearch.chosencolours.length; i++){
			if (document.forms.thesearch.chosencolours[i].checked==true)
			var thecolours = thecolours+document.forms.thesearch.chosencolours[i].value+",";
		}
	}
	
	// does the search nhave colours? if so append to thestring
	if (thecolours.length) {
		var thestring = thestring+"&colours="+thecolours
	}
	
	var thetime= "&thetime="+new Date().getTime();
	
	// if colour choices are avalable - its a normal search
	if (document.getElementById('page').value == 'used'){
		liveSearchReq.open("GET","dosearch.html?reg_date="+document.forms.thesearch.reg_date.value
		   +"&page="+thepage
		   +"&max_price="+document.forms.thesearch.max_price.value
		   +"&min_price="+document.forms.thesearch.min_price.value
		   +"&transmission="+document.forms.thesearch.transmission.value
		   +"&fuel="+document.forms.thesearch.fuel.value
		   +"&max_miles="+document.forms.thesearch.max_miles.value
		   +"&avail_status="+document.forms.thesearch.avail_status.value
		   +"&rows="+howmany
		   +thestring
		   +thetime
		   ,false);
	} else {
		liveSearchReq.open("GET","dosearch.html?"
			+"page=specialonly"
			+"&rows=1"
			,false);
	}

	liveSearchReq.send(null);


	if (liveSearchReq.readyState == 4) {
		res.innerHTML = liveSearchReq.responseText;
	}
	
	
	/* populate the variants_row div */
	if (window.XMLHttpRequest) {
		variantsliveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		variantsliveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	try {
		if (document.forms.thesearch.model_type) {
			var thetime = thetime+"&selectedvar="+document.forms.thesearch.model_type.value
		}
	} catch (err) {}
	
	try {
		if (document.forms.thesearch.special) {
			var thetime = thetime+"&special=true"
		}
	} catch (err) {}	
	
	// if doing a model/var search get relevant dropdown for that model
	if (thepage != 'XXXX') {
		variantsliveSearchReq.open("GET","variant_only_drop.cfm?type="+document.forms.thesearch.type.value+thetime,false);
		variantsliveSearchReq.send(null);
	
		if (variantsliveSearchReq.readyState == 4) {
			var  res = document.getElementById("variants_row");
			res.innerHTML = variantsliveSearchReq.responseText;
	
			var  res2 = document.getElementById("variants_desc");
			res2.innerHTML = 'Variant';
		}
	}
	resetshowsperpagenosearch();
	addclear();
}


function resetshowsperpagenosearch(){
	/* check the form exists before you try to use it */
	try {
		if (document.forms.repage.toshow && document.forms.sortby.recordcount) {
			if (window.XMLHttpRequest) {
					liveSearchReqcounter = new XMLHttpRequest();
				} else if (window.ActiveXObject) {
					liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
				}

			var thetime= "&thetime="+new Date().getTime();
			liveSearchReqcounter.open("GET","resetshowsperpage.html?shows="+document.forms.repage.toshow.value
							   +"&records="+document.forms.sortby.recordcount.value
							   +thetime
							   ,false);
			liveSearchReqcounter.send(null);
		
			if (liveSearchReqcounter.readyState == 4) {
				var  resultbox = document.getElementById("pageslist");
				resultbox.innerHTML = liveSearchReqcounter.responseText;
			}
		}
	} catch (err) {}
}


function changesort(){
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET","resetsort.html?sort="+document.forms.sortby.sortbyint.value+thetime,false);
	liveSearchReqcounter.send(null);
	dosearch(1);
}


function clearsearch() {
	try {
		if (document.forms.thesearch.model_type.value) {
			document.forms.thesearch.model_type.value = 'XXXX'
			document.forms.thesearch.model_type.selected = 'No Preference'
		}
		
		//clear sessions
		if (window.XMLHttpRequest) {
			liveSearchReqcounter = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		var thetime= "?&thetime="+new Date().getTime();
		liveSearchReqcounter.open("GET","cleartop.html"+thetime,false);
		liveSearchReqcounter.send(null);			
	} catch (err) {}
}


function resetshowsperpage(recordcount){
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET","resetshowsperpage.html?shows="+document.forms.repage.toshow.value
					   +"&records="+recordcount
					   +thetime
					   ,false);
	liveSearchReqcounter.send(null);
	/*
	if (liveSearchReqcounter.readyState == 4) {
		var  resultbox = document.getElementById("pageslist");
		resultbox.innerHTML = liveSearchReqcounter.responseText;
	}
	*/
	dosearch(1);
}


function getdist() {
	if (document.getElementById('thecode').value != '') {
		if (window.XMLHttpRequest) {
			liveSearchReqcounter = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		var thetime= "&thetime="+new Date().getTime();
		liveSearchReqcounter.open("GET","resetsort.html?sort=distance&postcode="+document.getElementById('thecode').value+thetime,false);
		liveSearchReqcounter.send(null);
	
		dosearch(1);
	}
}


function compare(id) {
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var res = document.getElementById(id);
	res.innerHTML = '<a href="javascript:void(0)" onClick="top.location.href=\'compare/\'"><img src="images/compare.jpg" at="Add to compare list"></a>';
	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET","comparelist.html?id="+id+thetime,false);
	liveSearchReqcounter.send(null);
}


function remove(id) {
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET","comparelist.html?remove="+id+thetime,false);
	liveSearchReqcounter.send(null);

	var  res = document.getElementById("compared");
	res.innerHTML = parseScript(liveSearchReqcounter.responseText);
}


function enquire() {
	if(document.getElementById('enquiryfullname').value == '' | document.getElementById('enquirytel').value == '' | document.getElementById('enquiryemail').value == '') {
		alert('Please enter your name, a valid email address and contact number.');
	} else {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
	
		thestring = '';
	
		try {
			if (document.getElementById('enquiryfullname').value != '') {
				thestring = thestring+'&enquiryfullname='+document.getElementById('enquiryfullname').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('enquirytel').value != '') {
				thestring = thestring+'&enquirytel='+document.getElementById('enquirytel').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('enquiryemail').value != '') {
				thestring = thestring+'&enquiryemail='+document.getElementById('enquiryemail').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('dealership').value != '') {
				thestring = thestring+'&dealership='+document.getElementById('dealership').value;
			}
		} catch (err) {}		

		thetime= '?thetime='+new Date().getTime();
		liveSearchReq.open('GET','compare_process.cfm'+thetime+thestring,false);
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			resultbox = document.getElementById("booking");
			resultbox.innerHTML = liveSearchReq.responseText;
		}		
	}
}


function variant(type) {
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET",type+"_drop.cfm?type="+thetime,false);
	liveSearchReqcounter.send(null);	
	
	res1 = document.getElementById('onename');
	res1.innerHTML = type;

	res2 = document.getElementById('onedrop');
	res2.innerHTML = liveSearchReqcounter.responseText;
	
	if (type == 'Variant') {
		res1 = document.getElementById('variants_desc');
		res1.innerHTML = '';
	
		res2 = document.getElementById('variants_row');
		res2.innerHTML = '';	
	}
}


function ValidateForm(form){
	if(form.firstname.value == "" || form.surname.value == "" || form.tel.value == "" || form.email.value == "" || form.dealership.value == ""){
	alert('Please enter a Firstname,Surname,Telephone Number, Email Address and select your nearest dealership');
	return false;
	}
	return true;
}


function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}


function changelinkref(theimage,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","setlink.cfm?theimage="+theimage+"&id="+id+thetime,false);
	liveSearchReq.send(null);
	
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("thediv");
		res.innerHTML =  liveSearchReq.responseText;
	}
}


function generate() {
	var res = document.getElementById("pdficon");
	res.innerHTML = '<strong>Generating file please wait.</strong><br />The file will prompt you to save it, this process may take up to around 30 seconds depending on the quality of your internet connection.';
}


function updateprefs() {
	if (window.XMLHttpRequest) {
		liveSearchReqcounter = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReqcounter = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	liveSearchReqcounter.open("GET","store_colprefs.html?colby="+document.forms.colprefs.colby.value+"&orderby="+document.forms.colprefs.orderby.value+"&theid="+document.forms.colprefs.theid.value+thetime,false);
	liveSearchReqcounter.send(null);
	
	if (liveSearchReqcounter.readyState == 4) {
		document.forms.colprefs.submit();
	}
}


function quickvars() {
	if (window.XMLHttpRequest) {
		quickvar = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		quickvar = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var thetime= "&thetime="+new Date().getTime();
	quickvar.open("GET","quick_variant_only_drop.cfm?type="+document.forms.quickform.type.value+thetime,false);
	quickvar.send(null);
		
	var  res = document.getElementById("quick_variants_row");
	res.innerHTML = quickvar.responseText;

	var  res2 = document.getElementById("quick_variants_desc");
	res2.innerHTML = 'Variant';
}


function vid(vidurl) {
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
	}
		
	thetime= '?thetime='+new Date().getTime();
	liveSearchReq.open('GET','getvids.html'+thetime+"&thevid="+vidurl+"&autoplay=true",false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		resultbox = document.getElementById('thevid');
		resultbox.style.borderColor = '#3379A4';
		resultbox.innerHTML = parseScript(liveSearchReq.responseText);
	}
}


function clearall() {
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
	}
		
	thetime= '?thetime='+new Date().getTime();
	liveSearchReq.open('GET','clear_query.html',false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		document.location.href='used/'
	}
}


function addclear() {
	if (document.getElementById('clearoff')){
		resultbox = document.getElementById('clearoff');
		resultbox.innerHTML = '<img src="images/Clearcriteriabutton.jpg" onclick="clearall()" style="cursor:pointer;">';
	}
}


function getdealerships(){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
	}
		
	thetime= '&thetime='+new Date().getTime();
	liveSearchReq.open("GET","closest_dealers.html?postcode="+document.getElementById('formpostcode').value+thetime,false);
	liveSearchReq.send(null);
	
	if (liveSearchReq.readyState == 4) {
		resultbox = document.getElementById('theresults');
		resultbox.innerHTML = parseScript(liveSearchReq.responseText);
	}
}


function getdata(){
	resultbox = document.getElementById('carinfo');
	if (document.getElementById('carreg').value != '') {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
			
		thetime= '&thetime='+new Date().getTime();
		liveSearchReq.open("GET","hpi.html?thereg="+document.getElementById('carreg').value+thetime,false);
		liveSearchReq.send(null);
		
		if (liveSearchReq.readyState == 4) {
			resultbox.innerHTML = parseScript(liveSearchReq.responseText);
		}
	} else {
		resultbox.innerHTML = '';
	}
}














/*	Unobtrusive Flash Objects (UFO) */
var UFO = {
	req: ["movie", "width", "height", "majorversion", "build"],
	opt: ["play", "loop", "menu", "quality", "scale", "salign", "wmode", "bgcolor", "base", "flashvars", "devicefont", "allowscriptaccess", "seamlesstabbing", "allowfullscreen", "allownetworking"],
	optAtt: ["id", "name", "align"],
	optExc: ["swliveconnect"],
	ximovie: "ufo.swf",
	xiwidth: "215",
	xiheight: "138",
	ua: navigator.userAgent.toLowerCase(),
	pluginType: "",
	fv: [0,0],
	foList: [],
		
	create: function(FO, id) {
		if (!UFO.uaHas("w3cdom") || UFO.uaHas("ieMac")) return;
		UFO.getFlashVersion();
		UFO.foList[id] = UFO.updateFO(FO);
		UFO.createCSS("#" + id, "visibility:hidden;");
		UFO.domLoad(id);
	},

	updateFO: function(FO) {
		if (typeof FO.xi != "undefined" && FO.xi == "true") {
			if (typeof FO.ximovie == "undefined") FO.ximovie = UFO.ximovie;
			if (typeof FO.xiwidth == "undefined") FO.xiwidth = UFO.xiwidth;
			if (typeof FO.xiheight == "undefined") FO.xiheight = UFO.xiheight;
		}
		FO.mainCalled = false;
		return FO;
	},

	domLoad: function(id) {
		var _t = setInterval(function() {
			if ((document.getElementsByTagName("body")[0] != null || document.body != null) && document.getElementById(id) != null) {
				UFO.main(id);
				clearInterval(_t);
			}
		}, 250);
		if (typeof document.addEventListener != "undefined") {
			document.addEventListener("DOMContentLoaded", function() { UFO.main(id); clearInterval(_t); } , null); // Gecko, Opera 9+
		}
	},

	main: function(id) {
		var _fo = UFO.foList[id];
		if (_fo.mainCalled) return;
		UFO.foList[id].mainCalled = true;
		document.getElementById(id).style.visibility = "hidden";
		if (UFO.hasRequired(id)) {
			if (UFO.hasFlashVersion(parseInt(_fo.majorversion, 10), parseInt(_fo.build, 10))) {
				if (typeof _fo.setcontainercss != "undefined" && _fo.setcontainercss == "true") UFO.setContainerCSS(id);
				UFO.writeSWF(id);
			}
			else if (_fo.xi == "true" && UFO.hasFlashVersion(6, 65)) {
				UFO.createDialog(id);
			}
		}
		document.getElementById(id).style.visibility = "visible";
	},
	
	createCSS: function(selector, declaration) {
		var _h = document.getElementsByTagName("head")[0]; 
		var _s = UFO.createElement("style");
		if (!UFO.uaHas("ieWin")) _s.appendChild(document.createTextNode(selector + " {" + declaration + "}")); // bugs in IE/Win
		_s.setAttribute("type", "text/css");
		_s.setAttribute("media", "screen"); 
		_h.appendChild(_s);
		if (UFO.uaHas("ieWin") && document.styleSheets && document.styleSheets.length > 0) {
			var _ls = document.styleSheets[document.styleSheets.length - 1];
			if (typeof _ls.addRule == "object") _ls.addRule(selector, declaration);
		}
	},
	
	setContainerCSS: function(id) {
		var _fo = UFO.foList[id];
		var _w = /%/.test(_fo.width) ? "" : "px";
		var _h = /%/.test(_fo.height) ? "" : "px";
		UFO.createCSS("#" + id, "width:" + _fo.width + _w +"; height:" + _fo.height + _h +";");
		if (_fo.width == "100%") {
			UFO.createCSS("body", "margin-left:0; margin-right:0; padding-left:0; padding-right:0;");
		}
		if (_fo.height == "100%") {
			UFO.createCSS("html", "height:100%; overflow:hidden;");
			UFO.createCSS("body", "margin-top:0; margin-bottom:0; padding-top:0; padding-bottom:0; height:100%;");
		}
	},

	createElement: function(el) {
		return (UFO.uaHas("xml") && typeof document.createElementNS != "undefined") ?  document.createElementNS("http://www.w3.org/1999/xhtml", el) : document.createElement(el);
	},

	createObjParam: function(el, aName, aValue) {
		var _p = UFO.createElement("param");
		_p.setAttribute("name", aName);	
		_p.setAttribute("value", aValue);
		el.appendChild(_p);
	},

	uaHas: function(ft) {
		var _u = UFO.ua;
		switch(ft) {
			case "w3cdom":
				return (typeof document.getElementById != "undefined" && typeof document.getElementsByTagName != "undefined" && (typeof document.createElement != "undefined" || typeof document.createElementNS != "undefined"));
			case "xml":
				var _m = document.getElementsByTagName("meta");
				var _l = _m.length;
				for (var i = 0; i < _l; i++) {
					if (/content-type/i.test(_m[i].getAttribute("http-equiv")) && /xml/i.test(_m[i].getAttribute("content"))) return true;
				}
				return false;
			case "ieMac":
				return /msie/.test(_u) && !/opera/.test(_u) && /mac/.test(_u);
			case "ieWin":
				return /msie/.test(_u) && !/opera/.test(_u) && /win/.test(_u);
			case "gecko":
				return /gecko/.test(_u) && !/applewebkit/.test(_u);
			case "opera":
				return /opera/.test(_u);
			case "safari":
				return /applewebkit/.test(_u);
			default:
				return false;
		}
	},
	
	getFlashVersion: function() {
		if (UFO.fv[0] != 0) return;  
		if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
			UFO.pluginType = "npapi";
			var _d = navigator.plugins["Shockwave Flash"].description;
			if (typeof _d != "undefined") {
				_d = _d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
				var _m = parseInt(_d.replace(/^(.*)\..*$/, "$1"), 10);
				var _r = /r/.test(_d) ? parseInt(_d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
				UFO.fv = [_m, _r];
			}
		}
		else if (window.ActiveXObject) {
			UFO.pluginType = "ax";
			try { // avoid fp 6 crashes
				var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}
			catch(e) {
				try { 
					var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					UFO.fv = [6, 0];
					_a.AllowScriptAccess = "always"; // throws if fp < 6.47 
				}
				catch(e) {
					if (UFO.fv[0] == 6) return;
				}
				try {
					var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				}
				catch(e) {}
			}
			if (typeof _a == "object") {
				var _d = _a.GetVariable("$version"); // bugs in fp 6.21/6.23
				if (typeof _d != "undefined") {
					_d = _d.replace(/^\S+\s+(.*)$/, "$1").split(",");
					UFO.fv = [parseInt(_d[0], 10), parseInt(_d[2], 10)];
				}
			}
		}
	},

	hasRequired: function(id) {
		var _l = UFO.req.length;
		for (var i = 0; i < _l; i++) {
			if (typeof UFO.foList[id][UFO.req[i]] == "undefined") return false;
		}
		return true;
	},
	
	hasFlashVersion: function(major, release) {
		return (UFO.fv[0] > major || (UFO.fv[0] == major && UFO.fv[1] >= release)) ? true : false;
	},

	writeSWF: function(id) {
		var _fo = UFO.foList[id];
		var _e = document.getElementById(id);
		if (UFO.pluginType == "npapi") {
			if (UFO.uaHas("gecko") || UFO.uaHas("xml")) {
				while(_e.hasChildNodes()) {
					_e.removeChild(_e.firstChild);
				}
				var _obj = UFO.createElement("object");
				_obj.setAttribute("type", "application/x-shockwave-flash");
				_obj.setAttribute("data", _fo.movie);
				_obj.setAttribute("width", _fo.width);
				_obj.setAttribute("height", _fo.height);
				var _l = UFO.optAtt.length;
				for (var i = 0; i < _l; i++) {
					if (typeof _fo[UFO.optAtt[i]] != "undefined") _obj.setAttribute(UFO.optAtt[i], _fo[UFO.optAtt[i]]);
				}
				var _o = UFO.opt.concat(UFO.optExc);
				var _l = _o.length;
				for (var i = 0; i < _l; i++) {
					if (typeof _fo[_o[i]] != "undefined") UFO.createObjParam(_obj, _o[i], _fo[_o[i]]);
				}
				_e.appendChild(_obj);
			}
			else {
				var _emb = "";
				var _o = UFO.opt.concat(UFO.optAtt).concat(UFO.optExc);
				var _l = _o.length;
				for (var i = 0; i < _l; i++) {
					if (typeof _fo[_o[i]] != "undefined") _emb += ' ' + _o[i] + '="' + _fo[_o[i]] + '"';
				}
				_e.innerHTML = '<embed type="application/x-shockwave-flash" src="' + _fo.movie + '" width="' + _fo.width + '" height="' + _fo.height + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' + _emb + '></embed>';
			}
		}
		else if (UFO.pluginType == "ax") {
			var _objAtt = "";
			var _l = UFO.optAtt.length;
			for (var i = 0; i < _l; i++) {
				if (typeof _fo[UFO.optAtt[i]] != "undefined") _objAtt += ' ' + UFO.optAtt[i] + '="' + _fo[UFO.optAtt[i]] + '"';
			}
			var _objPar = "";
			var _l = UFO.opt.length;
			for (var i = 0; i < _l; i++) {
				if (typeof _fo[UFO.opt[i]] != "undefined") _objPar += '<param name="' + UFO.opt[i] + '" value="' + _fo[UFO.opt[i]] + '" />';
			}
			var _p = window.location.protocol == "https:" ? "https:" : "http:";
			_e.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + _objAtt + ' width="' + _fo.width + '" height="' + _fo.height + '" codebase="' + _p + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + _fo.majorversion + ',0,' + _fo.build + ',0"><param name="movie" value="' + _fo.movie + '" />' + _objPar + '</object>';
		}
	},
		
	createDialog: function(id) {
		var _fo = UFO.foList[id];
		UFO.createCSS("html", "height:100%; overflow:hidden;");
		UFO.createCSS("body", "height:100%; overflow:hidden;");
		UFO.createCSS("#xi-con", "position:absolute; left:0; top:0; z-index:1000; width:100%; height:100%; background-color:#fff; filter:alpha(opacity:75); opacity:0.75;");
		UFO.createCSS("#xi-dia", "position:absolute; left:50%; top:50%; margin-left: -" + Math.round(parseInt(_fo.xiwidth, 10) / 2) + "px; margin-top: -" + Math.round(parseInt(_fo.xiheight, 10) / 2) + "px; width:" + _fo.xiwidth + "px; height:" + _fo.xiheight + "px;");
		var _b = document.getElementsByTagName("body")[0];
		var _c = UFO.createElement("div");
		_c.setAttribute("id", "xi-con");
		var _d = UFO.createElement("div");
		_d.setAttribute("id", "xi-dia");
		_c.appendChild(_d);
		_b.appendChild(_c);
		var _mmu = window.location;
		if (UFO.uaHas("xml") && UFO.uaHas("safari")) {
			var _mmd = document.getElementsByTagName("title")[0].firstChild.nodeValue = document.getElementsByTagName("title")[0].firstChild.nodeValue.slice(0, 47) + " - Flash Player Installation";
		}
		else {
			var _mmd = document.title = document.title.slice(0, 47) + " - Flash Player Installation";
		}
		var _mmp = UFO.pluginType == "ax" ? "ActiveX" : "PlugIn";
		var _uc = typeof _fo.xiurlcancel != "undefined" ? "&xiUrlCancel=" + _fo.xiurlcancel : "";
		var _uf = typeof _fo.xiurlfailed != "undefined" ? "&xiUrlFailed=" + _fo.xiurlfailed : "";
		UFO.foList["xi-dia"] = { movie:_fo.ximovie, width:_fo.xiwidth, height:_fo.xiheight, majorversion:"6", build:"65", flashvars:"MMredirectURL=" + _mmu + "&MMplayerType=" + _mmp + "&MMdoctitle=" + _mmd + _uc + _uf };
		UFO.writeSWF("xi-dia");
	},

	expressInstallCallback: function() {
		var _b = document.getElementsByTagName("body")[0];
		var _c = document.getElementById("xi-con");
		_b.removeChild(_c);
		UFO.createCSS("body", "height:auto; overflow:auto;");
		UFO.createCSS("html", "height:auto; overflow:auto;");
	},

	cleanupIELeaks: function() {
		var _o = document.getElementsByTagName("object");
		var _l = _o.length
		for (var i = 0; i < _l; i++) {
			_o[i].style.display = "none";
			for (var x in _o[i]) {
				if (typeof _o[i][x] == "function") {
					_o[i][x] = null;
				}
			}
		}
	}

};

if (typeof window.attachEvent != "undefined" && UFO.uaHas("ieWin")) {
	window.attachEvent("onunload", UFO.cleanupIELeaks);
}

function specialcontact() {
	var e = document.getElementById("dealership");
	var theval = e.options[e.selectedIndex].value;	
	
	if(document.getElementById('firstname').value == '' | document.getElementById('email').value == '' | theval == '') {
		resultbox = document.getElementById('contact');
		resultbox.style.borderColor = '#3379A4';
		alert('Please enter your name, a valid email address and select a dealership.');
	} else {
		if (window.XMLHttpRequest) {
			liveSearchReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
		}
		
		thestring = '';		
	
		try {
			if (document.getElementById('title').value != '') {
				thestring = thestring+'&title='+document.getElementById('title').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('firstname').value != '') {
				thestring = thestring+'&firstname='+document.getElementById('firstname').value;
			}
		} catch (err) {}

		try {
			if (document.getElementById('surname').value != '') {
				thestring = thestring+'&surname='+document.getElementById('surname').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('tel').value != '') {
				thestring = thestring+'&tel='+document.getElementById('tel').value;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('email').value != '') {
				thestring = thestring+'&email='+document.getElementById('email').value;
			}
		} catch (err) {}
		
		try {
			if (theval != '') {
				thestring = thestring+'&dealership='+theval;
			}
		} catch (err) {}
		
		try {
			if (document.getElementById('query').value != '') {
				thestring = thestring+'&query='+document.getElementById('query').value;
			}
		} catch (err) {}
		
		thetime= '?thetime='+new Date().getTime();
		
		liveSearchReq.open('GET','special_contact_process.html'+thetime+thestring,false);
		liveSearchReq.send(null);
	
		if (liveSearchReq.readyState == 4) {
			resultbox = document.getElementById('contact');
			resultbox.innerHTML = parsereturned(liveSearchReq.responseText);
		}		
	}
}

function youvid(thevid){
	if (window.XMLHttpRequest) {
		liveSearchReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject('Microsoft.XMLHTTP');
	}

	thetime= '?thetime='+new Date().getTime();
	liveSearchReq.open('GET','youvid.cfm'+thetime+'&vid='+thevid,false);
	liveSearchReq.send(null);

	if (liveSearchReq.readyState == 4) {
		resultbox = document.getElementById('theatre');
		resultbox.innerHTML = liveSearchReq.responseText;
	}
}
