// JavaScript Document

var destination = false;
var fresh;
var geocoder;
var lat;
var local_height = 380;
var long;
var map;
var origin = false;
var postcode;
var radius;

function drawCircle(lat, lng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity, map) {
	var d2r = Math.PI/180;
	var r2d = 180/Math.PI;
	var Clat = radius * 0.014483;  // Convert statute miles into degrees latitude
	var Clng = Clat/Math.cos(lat*d2r); 
	var Cpoints = [];
	for (var i=0; i < 33; i++) { 
		var theta = Math.PI * (i/16); 
		Cy = lat + (Clat * Math.sin(theta)); 
		Cx = lng + (Clng * Math.cos(theta)); 
		var P = new GPoint(Cx,Cy); 
		Cpoints.push(P); 
	}

	var polygon = new GPolygon(Cpoints, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity);
	map.addOverlay(polygon);
}

function get_local(fr){
	var _tr = window.locale.trs;
		fresh = fr;
		remove_local_suggestions();
    if (document.getElementById('local_post_code').value == '') {
        alert(window.locale.trs.PLEASE_ENTER_VALID_PICKUP_POSTCODE);
    } else if (document.getElementById('local_origin').checked == false && document.getElementById('local_destination').checked == false) {
  	    alert(window.locale.trs.SELECT_DEST_OR_ORIGIN_LOCAL_RADIUS);
  	} else {
			
		postcode = document.getElementById('local_post_code').value;
		radius = document.getElementById('local_radius').value;
  	
  	if (GBrowserIsCompatible()){
  		map = new GMap2(document.getElementById("map2"));
  		if(!geocoder){
  			geocoder = new GClientGeocoder();
  		}
  		map.addControl(new GSmallMapControl());
  		geocoder.reset();
  		var cc = window.locale.locale.split('_');
  		geocoder.setBaseCountryCode(cc[1]||'GB');
  		geocoder.getLocations(postcode, get_local_response);		
  	}
  	}
}

function get_local_response(result){
	var http;
	var params;
	if (result.Status.code == G_GEO_SUCCESS) {
		if (result.Placemark.length > 1) {
			/*var local = document.getElementById('local');
			local.style.height = local.offsetHeight+(result.Placemark.length*16)+40+'px';*/
			var div = document.createElement('div');
			div.setAttribute('class', 'clear');
			div.setAttribute('className', 'clear');
			//local.appendChild(div);
			div = document.createElement('div');
			div.setAttribute('id', 'local_suggest');
			var p = document.createElement('p');
			p.setAttribute('class', 'title');
			p.setAttribute('className', 'title');
			var text = document.createTextNode(window.locale.trs.DID_YOU_MEAN);
			p.appendChild(text);
			div.appendChild(p);
			for (var i=0; i<result.Placemark.length; i++) {
				p = document.createElement('p');
				var a = document.createElement('a');
				a.setAttribute('href', 'javascript:set_postcode("'+result.Placemark[i].address+'");');
				text = document.createTextNode(result.Placemark[i].address);
				a.appendChild(text);
				p.appendChild(a);
				div.appendChild(p);
			}
			document.getElementById('radius_suggest_temp').appendChild(div);
		}
		else {
			postcode = result.Placemark[0].address;
			set_radius();
		}
	}
	else {
		var reason = window.locale.trs.CODE;
		if (reasons[result.Status.code]) {
			reason = reasons[result.Status.code]
		}
		alert(window.locale.trs.COULD_NOT_FIND+' "' +document.getElementById('local_post_code').value+ '"');
	}
}

function get_local_result(http){
	var _tr = window.locale.trs;
	if(http.readyState == 4 && http.status == 200){
		var response = http.responseText;
		if(response){			
			if(window.ActiveXObject){
				//IE
				var doc = new ActiveXObject("Microsoft.XMLDOM");
				doc.async = "false";
				doc.loadXML(response);
			} else {
				//Mozilla, Firefox, Opera, etc.
				var parser = new DOMParser();
				var doc = parser.parseFromString(response, "text/xml");
			}
					
			if(document.getElementById('sub_box_results_display'))
				$('html,body').animate({scrollTop: $("#sub_box_results_display").offset().top},'slow');	
				
			// documentElement always represents the root node
			var x = doc.documentElement;
			if(x.childNodes[0].childNodes[0].nodeName != 'errorno') {
				
				var search_results = document.getElementById('search_results');
				search_results.innerHTML = "";
				
				if (x.childNodes[0].childNodes[0].childNodes[3].firstChild
					&& x.childNodes[0].childNodes[0].childNodes[3].firstChild.nodeValue == '2') {
					init_shiply_instant_reverse_result_set_header();
				}
				else {
				
				set_result_display(x.childNodes[1].childNodes.length, x.childNodes[0].childNodes[0].childNodes[0].firstChild.nodeValue);
				set_pagination_display(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue, x.childNodes[1].childNodes.length);
				var table = document.createElement('table');
				table.setAttribute('id', 'search_results_table');
				table.setAttribute('cellspacing', '0');
				table.setAttribute('cellpadding', '0');
				table.setAttribute('class', 'bids_table');
				table.setAttribute('className', 'bids_table');
				table.setAttribute('width', '100%');
				var tbody = document.createElement('tbody');
				var tr = document.createElement('tr');
				tr.setAttribute('class', 'header');
				tr.setAttribute('className', 'header');
				var td = document.createElement('td');
				td.setAttribute('class', 'shipment cell first');
				td.setAttribute('className', 'shipment cell first');
				td.setAttribute('height', '27');
				text = document.createTextNode(_tr.SHIPMENT);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'origin cell');
				td.setAttribute('className', 'origin cell');
				text = document.createTextNode(_tr.ORIGIN);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'destination cell');
				td.setAttribute('className', 'destination cell');
				text = document.createTextNode(_tr.DESTINATION);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'miles cell');
				td.setAttribute('className', 'miles cell');
				a = document.createElement('a');
				a.setAttribute('href', '/search.php?s=0&sort_by=distance%20DESC');
				text = document.createTextNode(_tr.MILES);
				a.appendChild(text);
				td.appendChild(a);
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('class', 'date cell');
				td.setAttribute('className', 'date cell');
				a = document.createElement('a');
				a.setAttribute('href', '/search.php?s=0&sort_by=date%20DESC');
				text = document.createTextNode(_tr.DATE);
				a.appendChild(text);
				td.appendChild(a);
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('class', 'bids cell last');
				td.setAttribute('className', 'bids cell last');
				text = document.createTextNode(_tr.BIDS);
				td.appendChild(text);
				tr.appendChild(td);
				
				tbody.appendChild(tr);
				
				}
				
				if (x.childNodes[2]) {
					if (x.childNodes[0].childNodes[0].childNodes[3].firstChild
						&& x.childNodes[0].childNodes[0].childNodes[3].firstChild.nodeValue == '2') {
						init_shiply_instant_reverse_featured_result_set(x.childNodes[2], x.childNodes[0].childNodes[0].childNodes[4].firstChild.nodeValue);
					}
					else {
						
  				var j = true;
          for(var i = 0; i < x.childNodes[2].childNodes.length; i++){
				    tr = document.createElement('tr');
  					if(j){
  								tr.setAttribute('class', 'featured_listing featured_dark');
  								tr.setAttribute('className', 'featured_listing featured_dark');
  								j = false;
  							} else {
  							  tr.setAttribute('class', 'featured_listing featured_light');
  								tr.setAttribute('className', 'featured_listing featured_light');
  								j = true;
  							}
  					td = document.createElement('td');
  					td.setAttribute('class', 'shipment cell first');
  					td.setAttribute('className', 'shipment cell first');
  					td.setAttribute('height', '53');

  					//Add bid span
						bidspan = document.createElement('span');
						var bidid = x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue;

						bidspan.setAttribute('id', 'show'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue);
						bidspan.setAttribute('class', 'showmoresearchinfo');
            bidspan.setAttribute('title', 'Click to expand');
            bidspan.setAttribute('onclick', 'showbox(\'listing'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'\',\''+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'\')');

						var text = document.createTextNode("+");
						bidspan.appendChild(text);

						td.appendChild(bidspan);

  					if(x.childNodes[2].childNodes[i].childNodes[13].firstChild){
  						var a = document.createElement('a');
  						a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[2].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
  						var img = document.createElement('img');
  						if (x.childNodes[2].childNodes[i].childNodes[13].firstChild.nodeValue > 13827) {
                img.setAttribute('src', 'http://images.shiply.com/uploads/'+x.childNodes[2].childNodes[i].childNodes[13].firstChild.nodeValue+'_thumb');
              } else {
                img.setAttribute('src', '/uploads/'+x.childNodes[2].childNodes[i].childNodes[13].firstChild.nodeValue+'_thumb');
              }            
  						a.appendChild(img);
  						td.appendChild(a);
  					}
  					var a = document.createElement('a');
  					a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[2].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
  					var text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[23].firstChild.nodeValue);
  					a.appendChild(text);
  					td.appendChild(a);
  					/*if(x.childNodes[2].childNodes[i].childNodes[14].firstChild){
  						if(parseInt(x.childNodes[2].childNodes[i].childNodes[15].firstChild.nodeValue) == 1){
  							var img = document.createElement('img');
  							img.setAttribute('class', 'ebay');
  							img.setAttribute('className', 'ebay');
  							img.setAttribute('src', '/images/auction_won.gif');
							img.setAttribute('onMouseover', 'showhint(\'eBay auction item won\', this, event, \'150px\')');
  							td.appendChild(img);
  						} else if(parseInt(x.childNodes[2].childNodes[i].childNodes[15].firstChild.nodeValue) == 2){
  							var img = document.createElement('img');
  							img.setAttribute('class', 'ebay');
  							img.setAttribute('className', 'ebay');
  							img.setAttribute('src', '/images/auction.gif');
							img.setAttribute('onMouseover', 'showhint(\'User currently bidding on eBay auction - item not yet won\', this, event, \'200px\')');
  							td.appendChild(img);
  						}
  					}*/
  					var p = document.createElement('span');
  					p.setAttribute('class', 'category');
  					p.setAttribute('className', 'category');
  					if(x.childNodes[2].childNodes[i].childNodes[25].firstChild && x.childNodes[2].childNodes[i].childNodes[9].firstChild.nodeValue != "Other"){
  						text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[9].firstChild.nodeValue+' > '+x.childNodes[2].childNodes[i].childNodes[25].firstChild.nodeValue);
  					} else {
  						text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[9].firstChild.nodeValue);
  					}
  					//p.appendChild(text);
  					//td.appendChild(p);
  					tr.appendChild(td);
  					td = document.createElement('td');
  					td.setAttribute('class', 'origin cell');
  					td.setAttribute('className', 'origin cell');
  					var postcode = x.childNodes[2].childNodes[i].childNodes[8].firstChild.nodeValue;
  					p = document.createElement('p');
  					text = document.createTextNode(postcode);
  					p.appendChild(text);
  					td.appendChild(p);
  					/*if(x.childNodes[2].childNodes[i].childNodes[17].firstChild && x.childNodes[2].childNodes[i].childNodes[17].firstChild.nodeValue == 1){
  						var img = document.createElement('img');
  						img.setAttribute('src', '/images/int/'+window.locale.language+'/search_cal.gif');
  						td.appendChild(img);
  						p = document.createElement('p');
  						text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[18].firstChild.nodeValue+' - '+x.childNodes[2].childNodes[i].childNodes[19].firstChild.nodeValue);
  						p.appendChild(text);
  						td.appendChild(p);
  					}*/
  					tr.appendChild(td);
  					td = document.createElement('td');
  					td.setAttribute('class', 'destination cell');
  					td.setAttribute('className', 'destination cell');
  					var postcode = x.childNodes[2].childNodes[i].childNodes[12].firstChild.nodeValue;
  					p = document.createElement('p');
  					text = document.createTextNode(postcode);
  					p.appendChild(text);
  					td.appendChild(p);
  					/*if(x.childNodes[2].childNodes[i].childNodes[20].firstChild && x.childNodes[2].childNodes[i].childNodes[20].firstChild.nodeValue == 1){
  						var img = document.createElement('img');
  						img.setAttribute('src', '/images/int/'+window.locale.language+'/search_cal.gif');
  						td.appendChild(img);
  						p = document.createElement('p');
  						text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[21].firstChild.nodeValue+' - '+x.childNodes[2].childNodes[i].childNodes[22].firstChild.nodeValue);
  						p.appendChild(text);
  						td.appendChild(p);
  					}*/
  					tr.appendChild(td);
  					td = document.createElement('td');
  					td.setAttribute('class', 'miles cell');
  					td.setAttribute('className', 'miles cell');
  					text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[10].firstChild.nodeValue);
  					td.appendChild(text);
  					tr.appendChild(td);
  					
  					td = document.createElement('td');
  					td.setAttribute('class', 'date cell');
  					td.setAttribute('className', 'date cell');
  					text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[6].firstChild.nodeValue);
  					td.appendChild(text);
  					tr.appendChild(td);
  					
					td = document.createElement('td');
  					td.setAttribute('class', 'bids cell last');
  					td.setAttribute('className', 'bids cell last');
  					a = document.createElement('a');					
  				        a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[2].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/#bids');		
  					text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[26].firstChild.nodeValue);
  					a.appendChild(text);
  					td.appendChild(a);
  					tr.appendChild(td);
					
					tbody.appendChild(tr);
					
  					
  					// Add invisible bid row
	          trbid = document.createElement('tr');
	          tdbid = document.createElement('td');

	          tdbid.setAttribute('colspan', 7);
	          tdbid.setAttribute('id', 'listing'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue);
	          tdbid.setAttribute('class', 'showlistinginfo');
	          tdbid.setAttribute('style', 'display:none;');

	          trbid.appendChild(tdbid);
	          tbody.appendChild(trbid);
  					
  				}
  				tr = document.createElement('tr');
          tr.setAttribute('class', 'featured_bottom');
          tr.setAttribute('className', 'featured_bottom');
          td = document.createElement('td');
          td.setAttribute('colspan', 7);
          td.setAttribute('class', 'table_span');
		  td.setAttribute('className', 'table_span');
          text = document.createTextNode('Featured Shipments! ');
          td.appendChild(text);
          a = document.createElement('a');
          a.setAttribute('href', '/myshiply.php?featured=1');
          text = document.createTextNode('Promote your shipment listings »');
          a.appendChild(text);
          td.appendChild(a);
          tr.appendChild(td);
          tbody.appendChild(tr);
  			}
					
			}
			
				var j = true;
			if (x.childNodes[0].childNodes[0].childNodes[3].firstChild
				&& x.childNodes[0].childNodes[0].childNodes[3].firstChild.nodeValue == '2') {
				init_shiply_instant_reverse_result_set(x.childNodes[1], x.childNodes[0].childNodes[0].childNodes[4].firstChild.nodeValue);
			}
			else {
				for(var i = 0; i < x.childNodes[1].childNodes.length; i++){
					tr = document.createElement('tr');
					if(j){
						tr.setAttribute('class', 'dark');
						tr.setAttribute('className', 'dark');
						j = false;
					} else {
						tr.setAttribute('class', 'light');
						tr.setAttribute('className', 'light');
						j = true;
					}
					td = document.createElement('td');
					td.setAttribute('class', 'shipment cell first');
					td.setAttribute('className', 'shipment cell first');
					td.setAttribute('height', '53');
					

					//Add bid span
						bidspan = document.createElement('span');
						var bidid = x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue;

						bidspan.setAttribute('id', 'show'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue);
						bidspan.setAttribute('class', 'showmoresearchinfo');
            bidspan.setAttribute('title', 'Click to expand');
            bidspan.setAttribute('onclick', 'showbox(\'listing'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'\',\''+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'\')');

						var text = document.createTextNode("+");
						bidspan.appendChild(text);

						td.appendChild(bidspan);

					if(x.childNodes[1].childNodes[i].childNodes[13].firstChild){
						var a = document.createElement('a');
						a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[1].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
						var img = document.createElement('img');
						if (x.childNodes[1].childNodes[i].childNodes[13].firstChild.nodeValue > 13827) {
              img.setAttribute('src', 'http://images.shiply.com/uploads/'+x.childNodes[1].childNodes[i].childNodes[13].firstChild.nodeValue+'_thumb');
            } else {
              img.setAttribute('src', '/uploads/'+x.childNodes[1].childNodes[i].childNodes[13].firstChild.nodeValue+'_thumb');
            }            
						a.appendChild(img);
						td.appendChild(a);
					}
					var a = document.createElement('a');
					a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[1].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
					if (x.childNodes[1].childNodes[i].childNodes[23].firstChild) {
					  var text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[23].firstChild.nodeValue);
					}
					else {
            var text = document.createTextNode("");
          }
					a.appendChild(text);
					td.appendChild(a);
					/*if(x.childNodes[1].childNodes[i].childNodes[14].firstChild){
						if(parseInt(x.childNodes[1].childNodes[i].childNodes[15].firstChild.nodeValue) == 1){
							var img = document.createElement('img');
							img.setAttribute('class', 'ebay');
							img.setAttribute('className', 'ebay');
							img.setAttribute('src', '/images/auction_won.gif');
							img.setAttribute('onMouseover', 'showhint(\'eBay auction item won\', this, event, \'150px\')');
							td.appendChild(img);
						} else if(parseInt(x.childNodes[1].childNodes[i].childNodes[15].firstChild.nodeValue) == 2){
							var img = document.createElement('img');
							img.setAttribute('class', 'ebay');
							img.setAttribute('className', 'ebay');
							img.setAttribute('src', '/images/auction.gif');
							img.setAttribute('onMouseover', 'showhint(\'User currently bidding on eBay auction - item not yet won\', this, event, \'200px\')');
							td.appendChild(img);
						}
					}*/
					var p = document.createElement('span');
					p.setAttribute('class', 'category');
					p.setAttribute('className', 'category');
					if(x.childNodes[1].childNodes[i].childNodes[25].firstChild && x.childNodes[1].childNodes[i].childNodes[9].firstChild.nodeValue != "Other"){
						text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[9].firstChild.nodeValue+' > '+x.childNodes[1].childNodes[i].childNodes[25].firstChild.nodeValue);
					} else {
						text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[9].firstChild.nodeValue);
					}
					//p.appendChild(text);
					//td.appendChild(p);
					tr.appendChild(td);
					td = document.createElement('td');
					td.setAttribute('class', 'origin cell');
					td.setAttribute('className', 'origin cell');
					var postcode = x.childNodes[1].childNodes[i].childNodes[8].firstChild.nodeValue;
					p = document.createElement('p');
					text = document.createTextNode(postcode);
					p.appendChild(text);
					td.appendChild(p);
					/*if(x.childNodes[1].childNodes[i].childNodes[17].firstChild && x.childNodes[1].childNodes[i].childNodes[17].firstChild.nodeValue == 1){
						var img = document.createElement('img');
						img.setAttribute('src', '/images/search_cal.gif');
						td.appendChild(img);
						p = document.createElement('p');
						text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[18].firstChild.nodeValue+' - '+x.childNodes[1].childNodes[i].childNodes[19].firstChild.nodeValue);
						p.appendChild(text);
						td.appendChild(p);
					}*/
					tr.appendChild(td);
					td = document.createElement('td');
					td.setAttribute('class', 'destination cell');
					td.setAttribute('className', 'destination cell');
					var postcode = x.childNodes[1].childNodes[i].childNodes[12].firstChild.nodeValue;
					p = document.createElement('p');
					text = document.createTextNode(postcode);
					p.appendChild(text);
					td.appendChild(p);
					/*if(x.childNodes[1].childNodes[i].childNodes[20].firstChild && x.childNodes[1].childNodes[i].childNodes[20].firstChild.nodeValue == 1){
						var img = document.createElement('img');
						img.setAttribute('src', '/images/search_cal.gif');
						td.appendChild(img);
						p = document.createElement('p');
						text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[21].firstChild.nodeValue+' - '+x.childNodes[1].childNodes[i].childNodes[22].firstChild.nodeValue);
						p.appendChild(text);
						td.appendChild(p);
					}*/
					tr.appendChild(td);
					td = document.createElement('td');
					td.setAttribute('class', 'miles cell');
					td.setAttribute('className', 'miles cell');
					text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[10].firstChild.nodeValue);
					td.appendChild(text);
					tr.appendChild(td);
					
					td = document.createElement('td');
					td.setAttribute('class', 'date cell');
					td.setAttribute('className', 'date cell');
					text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[6].firstChild.nodeValue);
					td.appendChild(text);
					tr.appendChild(td);
					td = document.createElement('td');
					td.setAttribute('class', 'bids cell last');
					td.setAttribute('className', 'bids cell last');
					a = document.createElement('a');					
				        a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[1].childNodes[i].childNodes[24].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/#bids');		
					text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[26].firstChild.nodeValue);
					a.appendChild(text);
					td.appendChild(a);
					tr.appendChild(td);
					tbody.appendChild(tr);
					
					// Add invisible bid row
          trbid = document.createElement('tr');
          tdbid = document.createElement('td');

          tdbid.setAttribute('colspan', 7);
          tdbid.setAttribute('id', 'listing'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue);
          tdbid.setAttribute('class', 'showlistinginfo');
          tdbid.setAttribute('style', 'display:none;');

          trbid.appendChild(tdbid);
          tbody.appendChild(trbid);
					
				}
				table.appendChild(tbody);
				search_results.appendChild(table);
			}
				var footer = set_footer_display(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue, x.childNodes[1].childNodes.length);
				search_results.innerHTML = search_results.innerHTML+footer;
			} else {
				var search_results = document.getElementById('search_results');
				search_results.innerHTML = "";
				set_result_display(0,0);
				set_pagination_display(0,0);
				var table = document.createElement('table');
				table.setAttribute('id', 'search_results_table');
				table.setAttribute('cellspacing', '0');
				table.setAttribute('cellpadding', '0');
				table.setAttribute('class', 'bids_table');
				table.setAttribute('className', 'bids_table');
				table.setAttribute('width', '100%');
				var tbody = document.createElement('tbody');
				var tr = document.createElement('tr');
				tr.setAttribute('class', 'header');
				tr.setAttribute('className', 'header');
				var td = document.createElement('td');
				td.setAttribute('class', 'shipment cell first');
				td.setAttribute('className', 'shipment cell first');
				text = document.createTextNode(_tr.SHIPMENT);
				td.appendChild(text);
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('class', 'origin cell');
				td.setAttribute('className', 'origin cell');
				text = document.createTextNode(_tr.ORIGIN);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'destination cell');
				td.setAttribute('className', 'destination cell');
				text = document.createTextNode(_tr.DESTINATION);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'miles cell');
				td.setAttribute('className', 'miles cell');
				a = document.createElement('a');
				a.setAttribute('href', '/search.php?s=0&sort_by=distance%20DESC');
				text = document.createTextNode(_tr.MILES);
				a.appendChild(text);
				td.appendChild(a);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'miles_added cell');
				td.setAttribute('className', 'miles_added cell');
				text = document.createTextNode(_tr.MILES_ADDED);
				td.appendChild(text);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'date cell');
				td.setAttribute('className', 'date cell');
				a = document.createElement('a');
				a.setAttribute('href', '/search.php?s=0&sort_by=date%20DESC');
				text = document.createTextNode(_tr.DATE);
				a.appendChild(text);
				td.appendChild(a);
				tr.appendChild(td);
				td = document.createElement('td');
				td.setAttribute('class', 'bids cell last');
				td.setAttribute('className', 'bids cell last');
				text = document.createTextNode(_tr.BIDS);
				td.appendChild(text);
				tr.appendChild(td);
				tbody.appendChild(tr);
				tr = document.createElement('tr');
				td = document.createElement('td');
				td.setAttribute('colspan', '8');
				td.colSpan = 8;
				td.setAttribute('class', 'table_span');
				td.setAttribute('className', 'table_span');
				var center = document.createElement('center');
				var text = document.createTextNode('- '+_tr.NO_RESULTS+' -');
				center.appendChild(text);
				td.appendChild(center);
				tr.appendChild(td);
				tbody.appendChild(tr);
				table.appendChild(tbody);
				search_results.appendChild(table);
				var footer = set_footer_display(0,0);
				search_results.innerHTML = search_results.innerHTML+footer;
			}
		}
	}
}

function set_radius() {
	if (fresh == 1) {
		var search_results = document.getElementById('search_results');
		search_results.innerHTML = "";
		var p = document.createElement('p');
		p.setAttribute('class', 'calc');
		p.setAttribute('classname', 'calc');
		var text = document.createTextNode(window.locale.trs.CALCULATING+'...');
		p.appendChild(text);
		var img = document.createElement('img');
		img.setAttribute('src', '/images/working.gif');
		img.setAttribute('class', 'working');
		search_results.appendChild(p);
		search_results.appendChild(img);
	}

	if(document.getElementById('local_origin').checked){
		origin = true;
	} else {
		origin = false;
	}
	if(document.getElementById('local_destination').checked){
		destination = true;
	} else {
		destination = false;
	}
		
	/*var local = document.getElementById('local');
	local.style.height = local_height+'px';*/
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map2"));
		if(!geocoder){
			geocoder = new GClientGeocoder();
		}
		 map.addControl(new GSmallMapControl());
		geocoder.reset();
		geocoder.setBaseCountryCode('GB');
		if (fresh ==  1) {
			geocoder.getLatLng(postcode, get_radius_response);
		}
		else {
			geocoder.getLatLng(postcode, get_radius_response2);
		}
	}
}

function get_radius_response(point) {
	if(point){
		lat = point.lat();
		long = point.lng();
		map.setCenter(point, 7);
		if (radius == 5) {
      map.setZoom(10);
    } else if (radius == 10) {
      map.setZoom(9);
    } else if (radius == 20) {
      map.setZoom(8);
    } else if (radius == 30 || radius == 40) {
      map.setZoom(7);
    } else if (radius == 60 || radius == 100) {
      map.setZoom(6);
    } else if (radius == 125 || radius == 150 || radius == 175 || radius == 200 || radius == 225 || radius == 250) {
      map.setZoom(5);
    }   
		drawCircle(lat, long, radius, "#000000", 1, 0.75, "#FF0000",.3, map);		
		
    if(navigator.appName == "Microsoft Internet Explorer"){
			http = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			http = new XMLHttpRequest();
		}
		params = "latitude="+escape(lat)+"&longitude="+escape(long)+"&radius="+escape(radius)+"&origin="+escape(origin)+"&destination="+escape(destination)+"&local_post_code="+escape(postcode);
		var  url = '/ajax/search_local.php';
		if(window.locale.language != 'en')
			url ='/'+window.locale.language+url;
			
		http.open('POST',url);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = function(){ get_local_result(http); };
		http.send(params);
	}
}

function get_radius_response2(point){
  var http;
	var params;
  	
	if(point){
		lat = point.lat();
		long = point.lng();
		map.setCenter(point, 7);		
		if (radius == 5) {
      map.setZoom(10);
    } else if (radius == 10) {
      map.setZoom(9);
    } else if (radius == 20) {
      map.setZoom(8);
    } else if (radius == 30 || radius == 40) {
      map.setZoom(7);
    } else if (radius == 60 || radius == 100) {
      map.setZoom(6);
    } else if (radius == 125 || radius == 150 || radius == 175 || radius == 200 || radius == 225 || radius == 250) {
      map.setZoom(5);
    }     
		drawCircle(lat, long, radius, "#000000", 1, 0.75, "#FF0000", .3, map);
	}
}

function remove_local_suggestions() {
	postcode = false;
	if(document.getElementById('local_suggest')){
		document.getElementById('local_suggest').parentNode.removeChild(document.getElementById('local_suggest'));
	}
	/*var local = document.getElementById('local');
	local.style.height = local_height+'px';*/
}

function set_postcode(p) {
	postcode = p;
	document.getElementById('local_suggest').parentNode.removeChild(document.getElementById('local_suggest'));
	/*var local = document.getElementById('local');
	local.style.height = local_height+'px';*/
	document.getElementById('local_post_code').value = postcode;
	set_radius();
}
