var route_fresh;
var origin;
var destination;
var origin_height;
var destination_height;
var route_height = 380;

var _tr = window.locale.trs;
var reasons = Array();
reasons[G_GEO_SUCCESS]            = _tr.SUCCESS;
reasons[G_GEO_MISSING_ADDRESS]    = _tr.G_GEO_MISSING_ADDRESS;
reasons[G_GEO_UNKNOWN_ADDRESS]    = _tr.G_GEO_SERVER_ERROR;
reasons[G_GEO_UNAVAILABLE_ADDRESS]= _tr.G_GEO_UNAVAILABLE_ADDRESS;
reasons[G_GEO_BAD_KEY]            = _tr.G_GEO_BAD_KEYG_GEO_BAD_KEY;
reasons[G_GEO_TOO_MANY_QUERIES]   = _tr.G_GEO_TOO_MANY_QUERIES;
reasons[G_GEO_SERVER_ERROR]       = _tr.G_GEO_SERVER_ERROR;

function update_display(val) {
  window.location.href = "search.php?s=0&display=" +val;
}

function all_locations() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {
    http = new XMLHttpRequest();
	}
  http.abort();
	document.getElementById('search_results').innerHTML = '<p class="calc">'+window.locale.trs.CALCULATING+'...</p><img src="/images/working.gif" class="working">';
  http.open("GET", "../../ajax/search_all_locations.php", true);
  http.onreadystatechange=function() {
    if (http.readyState == 4) {
      window.location.href = "search.php";
    }
  }
  http.send(null);
}

function by_uk() {
	  if (navigator.appName == "Microsoft Internet Explorer") {
	    http = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  else {
	    http = new XMLHttpRequest();
		}
	  http.abort();
		document.getElementById('search_results').innerHTML = '<p class="calc">'+window.locale.trs.CALCULATING+'...</p><img src="/images/working.gif" class="working">';
	  http.open("GET", "../../ajax/search_by_uk.php", true);
	  http.onreadystatechange=function() {
	    if (http.readyState == 4) {
	      window.location.href = "search.php";
	    }
	  }
	  http.send(null);
}

function set_origin(addr) {
	origin = addr;
	document.getElementById('origin_suggest').parentNode.removeChild(document.getElementById('origin_suggest'));
	var along_route = document.getElementById('along_route');
	along_route.style.height = (along_route.offsetHeight-origin_height)+'px';
	document.getElementById('route_from_post_code').value = addr;
	if(origin && destination){
		set_route();
	}
}

function set_destination(addr) {
	destination = addr;
	document.getElementById('destination_suggest').parentNode.removeChild(document.getElementById('destination_suggest'));
	var along_route = document.getElementById('along_route');
	along_route.style.height = (along_route.offsetHeight-destination_height)+'px';
	document.getElementById('route_to_post_code').value = addr;
	if(origin && destination){
		set_route();
	}
}

function set_route() {
	var along_route = document.getElementById('along_route');
	along_route.style.height = route_height+'px';
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		gdir = new GDirections(map, document.getElementById("directions"));
		
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		gdir.load("from: " +origin +" to: " +destination);
	}
}

function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert('Unknown address');
	}
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
		alert('Unknown error');
	}
	else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
		alert('Unknown error');
	}
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {	     
		alert('Unknown error');
	}
	else {	   
		alert('Unknown error');
	}
}

function remove_suggestions(){
	origin = false;
	destination = false;
	origin_height = false;
	destination_height = false;
	if (document.getElementById('origin_suggest')){ document.getElementById('origin_suggest').parentNode.removeChild(document.getElementById('origin_suggest')); }
	if (document.getElementById('destination_suggest')){ document.getElementById('destination_suggest').parentNode.removeChild(document.getElementById('destination_suggest')); }
	var along_route = document.getElementById('along_route');
	along_route.style.height = route_height+'px';
}

function get_route(from_country, from_post_code, to_country, to_post_code, fresh) {
  var map;
  var gdir;
  var geocoder = null;
  var addressMarker;
	route_fresh = fresh;
	
	remove_suggestions();
  
  if (from_post_code == '') {
    alert('Please enter the start town or post code');
    return;
  } else if (to_post_code == '') {
    alert('Please enter the end town or post code');
    return;
  }
	
	if (GBrowserIsCompatible()){
		map = new GMap2(document.getElementById("map"));
		if(!geocoder){
			geocoder = new GClientGeocoder();
		}
		map.addControl(new GSmallMapControl());
		geocoder.reset();
		geocoder.setBaseCountryCode('GB');
		
		geocoder.getLocations(from_post_code +", " +from_country, get_origin_response);
		
		gdir = new GDirections(map, document.getElementById("directions"));
	}
	
	function get_origin_response(result){
		if (result.Status.code == G_GEO_SUCCESS) {
			if (result.Placemark.length > 1) {
				var along_route = document.getElementById('along_route');
				along_route.style.height = along_route.offsetHeight+(result.Placemark.length*16)+80+'px';
				origin_height = (result.Placemark.length*16)+80;
				var div = document.createElement('div');
				div.setAttribute('class', 'clear');
				div.setAttribute('className', 'clear');
				along_route.appendChild(div);
				div = document.createElement('div');
				div.setAttribute('id', 'origin_suggest');
				var p = document.createElement('p');
				p.setAttribute('class', 'title');
				p.setAttribute('className', 'title');
				var text = document.createTextNode('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_origin("'+result.Placemark[i].address+'");');
					text = document.createTextNode(result.Placemark[i].address);
					a.appendChild(text);
					p.appendChild(a);
					div.appendChild(p);
				}
				div.style.display = 'none';
				document.getElementById('origin_suggest_temp').appendChild(div);
			}
			else {
				origin = result.Placemark[0].address;
			}
			geocoder.getLocations(to_post_code +", " +to_country, get_destination_response);
		}
		else {
			var reason = "Code " +result.Status.code;
			if (reasons[result.Status.code]) {
				reason = reasons[result.Status.code]
			} 
			alert('Could not find "' +from_post_code +'" ' +reason);
		}
	}
	
	function get_destination_response(result){
		if (result.Status.code == G_GEO_SUCCESS) {
			if (result.Placemark.length > 1) { 
				var along_route = document.getElementById('along_route');
				along_route.style.height = along_route.offsetHeight+(result.Placemark.length*16)+80+'px';
				destination_height = (result.Placemark.length*16)+80;
				var div = document.createElement('div');
				div.setAttribute('class', 'clear');
				div.setAttribute('className', 'clear');
				along_route.appendChild(div);
				div = document.createElement('div');
				div.setAttribute('id', 'destination_suggest');
				var p = document.createElement('p');
				p.setAttribute('class', 'title');
				p.setAttribute('className', 'title');
				var text = document.createTextNode('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_destination("'+result.Placemark[i].address+'");');
					text = document.createTextNode(result.Placemark[i].address);
					a.appendChild(text);
					p.appendChild(a);
					div.appendChild(p);
				}
				div.style.display = 'none';
				document.getElementById('destination_suggest_template').appendChild(div);
			}
			else {
				destination = result.Placemark[0].address;
			}
		}
		else {
			var reason = "Code " +result.Status.code;
			if (reasons[result.Status.code]) {
				reason = reasons[result.Status.code]
			} 
			alert('Could not find "' +to_post_code +'" ' +reason);
		}
		if (document.getElementById('origin_suggest') || document.getElementById('destination_suggest')) {
			if (document.getElementById('origin_suggest')){ document.getElementById('origin_suggest').style.display = 'block'; }
			if (document.getElementById('destination_suggest')){ document.getElementById('destination_suggest').style.display = 'block'; }
		} else {
			set_route();
		}
	}
}

function onGDirectionsLoad(){
		
		var http;
		var along;
		var off_route;
		
		destination = false;
		origin = false;

		document.getElementById("getDistance").innerHTML = "<b>"+window.locale.trs.CURRENT_MILEAGE+":</b> "+gdir.getSummaryHtml();    

    if (route_fresh == 1) {
    	if(navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        http = new XMLHttpRequest();
      }
      off_route = document.getElementById("off_route").value;
      if (document.getElementById("along1").checked) {
      along = 1;
      } else if (document.getElementById("along2").checked) {
      along = 2;
      }
      var from_lat = gdir.getGeocode(0).Point.coordinates[0];
      var from_long = gdir.getGeocode(0).Point.coordinates[1];
      var to_lat = gdir.getGeocode(1).Point.coordinates[0];
      var to_long = gdir.getGeocode(1).Point.coordinates[1];
      var route_from_country = document.getElementById("route_from_country").value;
      var route_from_post_code = document.getElementById("route_from_post_code").value;
      var route_to_country = document.getElementById("route_to_country").value;
      var route_to_post_code = document.getElementById("route_to_post_code").value;
      
      http.abort();
      document.getElementById('search_results').innerHTML = '<p class="calc">'+window.locale.trs.CALCULATING+'...</p><img src="/images/working.gif" class="working">';
      var  url = '/ajax/search_route.php';
	  if(window.locale.language != 'en')
	     url ='/'+window.locale.language+url;
	  url = url+"?fresh_search=1&from_lat="+from_lat+"&from_long="+from_long+"&to_lat="+to_lat+"&to_long="+to_long+"&off_route="+off_route+"&along="+along+"&route_from_country="+route_from_country+"&route_from_post_code="+route_from_post_code+"&route_to_country="+route_to_country+"&route_to_post_code="+route_to_post_code+"&current_mileage="+gdir.getSummaryHtml()+"&ccode="+window.locale.language;
      http.open("GET", url, true);
      
      http.onreadystatechange=function() {
      if(http.readyState == 4) {
        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");
					}
								
					// documentElement always represents the root node
					var x = doc.documentElement;
					if(x.childNodes[0].childNodes[0].nodeName != 'errorno'){
						var _tr = window.locale.trs;
						var search_results = document.getElementById('search_results');
						search_results.innerHTML = "";
						var div = document.createElement('div');
						div.setAttribute('id', 'results_pager');
						var p = document.createElement('p');
						var text = document.createTextNode('Results: 1 - '+(x.childNodes[1].childNodes.length)+' of '+x.childNodes[0].childNodes[0].childNodes[0].firstChild.nodeValue+', Page: 1 ');
						p.appendChild(text);
						if(parseInt(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue) > 1){
    					var total_pages = parseInt(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue);
    					var results_per_page = x.childNodes[1].childNodes.length;
    					for(var i = 2; i <= total_pages; i++){
    						var a = document.createElement('a');
    						var start = (i-1)*results_per_page;
    						a.setAttribute('href', '/search.php?s='+start);
    						var a_text = document.createTextNode(i);
    						a.appendChild(a_text);
    						p.appendChild(a);
    						var text = document.createTextNode(' ');
    						p.appendChild(text);
    					}
    					var next = results_per_page;
    					var last = results_per_page*(total_pages-1);
    					var a = document.createElement('a');
    					a.setAttribute('href', '/search.php?s='+next);
    					var a_text = document.createTextNode('next >');
    					a.appendChild(a_text);
    					p.appendChild(a);
    					var text = document.createTextNode(' ');
    					p.appendChild(text);
    					var a = document.createElement('a');
    					a.setAttribute('href', '/search.php?s='+last);
    					var a_text = document.createTextNode('>>');
    					a.appendChild(a_text);
    					p.appendChild(a);
    				}
						div.appendChild(p);
						search_results.appendChild(div);
						var div = document.createElement('div');
						div.setAttribute('id', 'results_save_search');
						if (parseInt(x.childNodes[0].childNodes[0].childNodes[2].firstChild.nodeValue) == 1) {
    					var form = document.createElement('form');
    					form.setAttribute('action', '/search.php');
    					form.setAttribute('method', 'post');
    					var input = document.createElement('input');
    					input.setAttribute('type', 'submit');
    					input.setAttribute('name', 'alert');
    					input.setAttribute('value', window.locale.trs.SAVE_SEARCH_EMAIL);
    					form.appendChild(input);
    					div.appendChild(form);
    				} else {
    					var br = document.createElement('br');
    					div.appendChild(br);
    				}
						search_results.appendChild(div);
						var div = document.createElement('div');
						div.setAttribute('class', 'clear');
						div.setAttribute('className', 'clear');
						search_results.appendChild(div);
						var table = document.createElement('table');
						table.setAttribute('cellspacing', '0');
						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');
						td.setAttribute('className', 'shipment cell');
						text = document.createTextNode(_tr.SHIPMENT);
						td.appendChild(text);
						tr.appendChild(td);
						td = document.createElement('td');
						td.setAttribute('class', 'bids cell');
						td.setAttribute('className', 'bids cell');
						text = document.createTextNode(_tr.BIDS);
						td.appendChild(text);
						tr.appendChild(td);
						td = document.createElement('td');
						td.setAttribute('class', 'price cell');
						td.setAttribute('className', 'price cell');
						var a = document.createElement('a');
						a.setAttribute('href', '/search.php?s=0&sort_by=price%20DESC');
						text = document.createTextNode(_tr.PRICE);
						a.appendChild(text);
						td.appendChild(a);
						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');
            a = document.createElement('a');
            a.setAttribute('href', '/search.php?s=0&sort_by=miles_added%20ASC');
            text = document.createTextNode(_tr.MILES_ADDED);
            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);
						tbody.appendChild(tr);
            if (x.childNodes[2]) {
      				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');
  								tr.setAttribute('className', 'featured_listing');
  								tr.setAttribute('bgcolor', '#ffc15f');
  								j = false;
  							} else {
  							  tr.setAttribute('class', 'featured_listing');
  								tr.setAttribute('className', 'featured_listing');
  								tr.setAttribute('bgcolor', '#ffd38f');
  								j = true;
  							}
  							td = document.createElement('td');
  							td.setAttribute('class', 'shipment cell');
  							td.setAttribute('className', 'shipment cell');
  							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[25].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[25].firstChild.nodeValue+'/'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
  							var text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[24].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[26].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', 'bids cell');
  							td.setAttribute('className', 'bids cell');
  							a = document.createElement('a');							
  							a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[2].childNodes[i].childNodes[25].firstChild.nodeValue+'/'+x.childNodes[2].childNodes[i].childNodes[0].firstChild.nodeValue+'/#bids');
  								
  							text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[27].firstChild.nodeValue);
  							a.appendChild(text);
  							td.appendChild(a);
  							tr.appendChild(td);
  							td = document.createElement('td');
  							td.setAttribute('class', 'price cell');
  							td.setAttribute('className', 'price cell');
  							if(x.childNodes[2].childNodes[i].childNodes[3].firstChild){
  								p = document.createElement('p');
  								text = document.createTextNode(window.locale.currencySymbol+x.childNodes[2].childNodes[i].childNodes[3].firstChild.nodeValue);
  								var img = document.createElement('img');
  								img.setAttribute('src', '/images/int/'+window.locale.language+'/max.gif');
  								p.appendChild(text);
  								td.appendChild(p);
  								td.appendChild(img);
  							} else {
  								var img = document.createElement('img');
  								img.setAttribute('src', '/images/int/'+window.locale.language+'/flexible.gif');
  								td.appendChild(img);
  							}
  							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.nodeValue == "choose"){
  								var img = document.createElement('img');
  								img.setAttribute('src', '/images/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.nodeValue == "choose"){
  								var img = document.createElement('img');
  								img.setAttribute('src', '/images/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', 'miles_added cell');
  							td.setAttribute('className', 'miles_added cell');
  							text = document.createTextNode(x.childNodes[2].childNodes[i].childNodes[23].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', 'clear');
  							td.setAttribute('className', 'clear');
  							tr.appendChild(td);
  							tbody.appendChild(tr);
              }
              tr = document.createElement('tr');
              tr.setAttribute('class', 'featured_bottom');
              tr.setAttribute('className', 'featured_bottom');
              td = document.createElement('td');
              td.setAttribute('colspan', 7);
              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;
						for(var i = 0; i < x.childNodes[1].childNodes.length; i++){
							tr = document.createElement('tr');
							if(j){
								tr.setAttribute('class', 'highlight');
								tr.setAttribute('className', 'highlight');
								j = false;
							} else {
								j = true;
							}
							td = document.createElement('td');
							td.setAttribute('class', 'shipment cell');
							td.setAttribute('className', 'shipment cell');
							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[25].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[25].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/');
							var text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[24].firstChild.nodeValue);
							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[26].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', 'bids cell');
							td.setAttribute('className', 'bids cell');
							a = document.createElement('a');							
							a.setAttribute('href', window.locale.baseUrl+'/transport/'+x.childNodes[1].childNodes[i].childNodes[25].firstChild.nodeValue+'/'+x.childNodes[1].childNodes[i].childNodes[0].firstChild.nodeValue+'/#bids');
								
							text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[27].firstChild.nodeValue);
							a.appendChild(text);
							td.appendChild(a);
							tr.appendChild(td);
							td = document.createElement('td');
							td.setAttribute('class', 'price cell');
							td.setAttribute('className', 'price cell');
							if(x.childNodes[1].childNodes[i].childNodes[3].firstChild){
								p = document.createElement('p');
								text = document.createTextNode(window.locale.currencySymbol+x.childNodes[1].childNodes[i].childNodes[3].firstChild.nodeValue);
								var img = document.createElement('img');
								img.setAttribute('src', '/images/int/'+window.locale.language+'/max.gif');
								p.appendChild(text);
								td.appendChild(p);
								td.appendChild(img);
							} else {
								var img = document.createElement('img');
								img.setAttribute('src', '/images/int/'+window.locale.language+'/flexible.gif');
								td.appendChild(img);
							}
							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.nodeValue == "choose"){
								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.nodeValue == "choose"){
								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', 'miles_added cell');
							td.setAttribute('className', 'miles_added cell');
							text = document.createTextNode(x.childNodes[1].childNodes[i].childNodes[23].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', 'clear');
							td.setAttribute('className', 'clear');
							tr.appendChild(td);
							tbody.appendChild(tr);
						}
						table.appendChild(tbody);
						search_results.appendChild(table);
						var p = document.createElement('p');
						var text = document.createTextNode('Results: 1 - '+(x.childNodes[1].childNodes.length)+' of '+x.childNodes[0].childNodes[0].childNodes[0].firstChild.nodeValue+', Page: 1 ');
						p.appendChild(text);
						if(parseInt(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue) > 1){
    					var total_pages = parseInt(x.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue);
    					var results_per_page = x.childNodes[1].childNodes.length;
    					for(var i = 2; i <= total_pages; i++){
    						var a = document.createElement('a');
    						var start = (i-1)*results_per_page;
    						a.setAttribute('href', '/search.php?s='+start);
    						var a_text = document.createTextNode(i);
    						a.appendChild(a_text);
    						p.appendChild(a);
    						var text = document.createTextNode(' ');
    						p.appendChild(text);
    					}
    					var next = results_per_page;
    					var last = results_per_page*(total_pages-1);
    					var a = document.createElement('a');
    					a.setAttribute('href', '/search.php?s='+next);
    					var a_text = document.createTextNode('next >');
    					a.appendChild(a_text);
    					p.appendChild(a);
    					var text = document.createTextNode(' ');
    					p.appendChild(text);
    					var a = document.createElement('a');
    					a.setAttribute('href', '/search.php?s='+last);
    					var a_text = document.createTextNode('>>');
    					a.appendChild(a_text);
    					p.appendChild(a);
    				}
						search_results.appendChild(p);
					} else {
						var search_results = document.getElementById('search_results');
						search_results.innerHTML = "";
						var div = document.createElement('div');
						div.setAttribute('id', 'results_pager');
						var p = document.createElement('p');
						var text = document.createTextNode('Results: 0');
						p.appendChild(text);
						div.appendChild(p);
						search_results.appendChild(div);
						var div = document.createElement('div');
						div.setAttribute('id', 'results_save_search');
						if (parseInt(x.childNodes[1].firstChild.nodeValue) == 1) {
    					var form = document.createElement('form');
    					form.setAttribute('action', '/search.php');
    					form.setAttribute('method', 'post');
    					var input = document.createElement('input');
    					input.setAttribute('type', 'submit');
    					input.setAttribute('name', 'alert');
    					input.setAttribute('value', window.locale.trs.SAVE_SEARCH_EMAIL);
    					form.appendChild(input);
    					div.appendChild(form);
    				} else {
    					var br = document.createElement('br');
    					div.appendChild(br);
    				}
						search_results.appendChild(div);
						var div = document.createElement('div');
						div.setAttribute('class', 'clear');
						div.setAttribute('className', 'clear');
						search_results.appendChild(div);
						var table = document.createElement('table');
						table.setAttribute('cellspacing', '0');
						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');
						td.setAttribute('className', 'shipment cell');
						text = document.createTextNode(_tr.SHIPMENT);
						td.appendChild(text);
						tr.appendChild(td);
						td = document.createElement('td');
						td.setAttribute('class', 'bids cell');
						td.setAttribute('className', 'bids cell');
						text = document.createTextNode(_tr.BIDS);
						td.appendChild(text);
						tr.appendChild(td);
						td = document.createElement('td');
						td.setAttribute('class', 'price cell');
						td.setAttribute('className', 'price cell');
						var a = document.createElement('a');
						a.setAttribute('href', '/search.php?s=0&sort_by=price%20DESC');
						text = document.createTextNode(_tr.PRICE);
						a.appendChild(text);
						td.appendChild(a);
						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);
						tbody.appendChild(tr);
						tr = document.createElement('tr');
						td = document.createElement('td');
						td.setAttribute('colspan', '8');
						td.colSpan = 8;
						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);
					}
				}
  		}
		}
		http.send(null);     
	}
}
    
    
function checkall(){

  var check_all = false;
  if (!document.getElementById('general_goods').checked) {
    check_all = true;
  }
  if (!document.getElementById('household_moves').checked) {
    check_all = true;
  }
  if (!document.getElementById('vehicles').checked) {
    check_all = true;
  }
  if (!document.getElementById('boats').checked) {
    check_all = true;
  }
  if (!document.getElementById('special_care').checked) {
    check_all = true;
  }
  if (!document.getElementById('freight').checked) {
    check_all = true;
  }
  if (!document.getElementById('pets').checked) {
    check_all = true;
  }
  if (!document.getElementById('plants').checked) {
    check_all = true;
  }
  if (!document.getElementById('other_misc').checked) {
    check_all = true;
  }
  var i = 1;
  for(i=1; i<=58; i++) {
    if (i == 42) { i++; }
    if (!document.getElementById('cat_'+i).checked) {
      check_all = true;
    }
  }
  
  var i = 1;
  for(i=1; i<=58; i++) {
    if (i == 42) { i++; }
    document.getElementById('cat_'+i).checked=check_all;
  }

   document.getElementById('general_goods').checked = check_all;
   document.getElementById('household_moves').checked = check_all;
   document.getElementById('vehicles').checked = check_all;
   document.getElementById('boats').checked = check_all;
   document.getElementById('special_care').checked = check_all;
   document.getElementById('freight').checked = check_all;
   document.getElementById('pets').checked = check_all;
   document.getElementById('plants').checked = check_all;
   document.getElementById('other_misc').checked = check_all;
}
    
function check(group){

if (group == 1) {
 if (document.getElementById('general_goods').checked == true) {
   for(i=1; i<=12; i++) {
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=1; i<=12; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 2) {
 if (document.getElementById('household_moves').checked == true) {
   for(i=13; i<=16; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=13; i<=16; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 3) {
 if (document.getElementById('vehicles').checked == true) {
   for(i=17; i<=30; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=17; i<=30; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 4) {
 if (document.getElementById('boats').checked == true) {
   for(i=31; i<=35; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=31; i<=35; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 5) {
 if (document.getElementById('special_care').checked == true) {
   for(i=36; i<=41; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=36; i<=41; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 6) {
 if (document.getElementById('freight').checked == true) {
   for(i=42; i<=45; i++) {
      if (i == 42) { i++; }
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=42; i<=45; i++) {
      if (i == 42) { i++; }
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 7) {
 if (document.getElementById('pets').checked == true) {
   for(i=46; i<=51; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=46; i<=51; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 8) {
 if (document.getElementById('plants').checked == true) {
   for(i=52; i<=57; i++) {  
      document.getElementById('cat_'+i).checked=true;
   }
 } else {
    for(i=52; i<=57; i++) {
      document.getElementById('cat_'+i).checked=false;
   } 
 }
}

if (group == 9) {
  if (document.getElementById('other_misc').checked) {
    for (i = 58; i <= 58; i++) {
      document.getElementById('cat_'+i).checked=true;
    }
  }
  else {
    for (i = 58; i <= 58; i++) {
      document.getElementById('cat_'+i).checked=false;
    }
  }
}

}

function clear_results(){
 document.getElementById('search_results').innerHTML = '<p style="text-align:center;width:100%"><b>'+window.locale.trs.CALCULATING+'...</b><p><img src="/images/working.gif"></p>';
 window.location = 'search.php?s=0&search_type=1';
}

function showhide_search(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
document.getElementById(id+'_expand').innerHTML = '<img src="/images/expand2.gif">';
} else {
obj.style.display = "none";
document.getElementById(id+'_expand').innerHTML = '<img src="/images/expand1.gif">';
}
}
}

function showhide_nav(arrow){
if (document.getElementById){
obj = document.getElementById(arrow);
if (obj.style.display == "none"){
document.getElementById(arrow).innerHTML = '<img src="/images/search_arrow.gif">';
} else {
document.getElementById(arrow).innerHTML = '<img src="/images/search_arrow_right.gif">';
}
}
}

function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}

function show2(id){
obj = document.getElementById(id);
obj.style.display = "";
}

function hide2(id){
obj = document.getElementById(id);
obj.style.display = "none";
}

function CngClass(id){
  if (id == 'snav1') {
    document.getElementById(id).className='highlight';
    document.getElementById('snav2').className='';
    document.getElementById('snav3').className='';
	document.getElementById('snav4').className='';
	document.getElementById('snav5').className='';
  }
  if (id == 'snav2') {
    document.getElementById(id).className='highlight';
    document.getElementById('snav1').className='';
    document.getElementById('snav3').className='';
	document.getElementById('snav4').className='';
	document.getElementById('snav5').className='';
  }
  if (id == 'snav3') {
    document.getElementById(id).className='highlight';
    document.getElementById('snav1').className='';
    document.getElementById('snav2').className='';
	document.getElementById('snav4').className='';
	document.getElementById('snav5').className='';
  }
  if (id == 'snav4') {
    document.getElementById(id).className='highlight';
    document.getElementById('snav1').className='';
    document.getElementById('snav2').className='';
	document.getElementById('snav3').className='';
	document.getElementById('snav5').className='';
	
  }
  if (id == 'snav5') {
	  document.getElementById(id).className='highlight';
	  document.getElementById('snav1').className='';
	  document.getElementById('snav2').className='';
	  document.getElementById('snav3').className='';
	  document.getElementById('snav4').className='';
  }
}

function pu_specify(choice) {
  if (choice == 'on' || choice == 'before') {
    show2('pbox1');
    hide2('pbox2');
  } else if (choice == 'between') {
    show2('pbox1');
    show2('pbox2');
  }
}

function del_specify(choice) {
  if (choice == 'on' || choice == 'before') {
    show2('dbox1');
    hide2('dbox2');
  } else if (choice == 'between') {
    show2('dbox1');
    show2('dbox2');
  }
}

function change_arrow(id){
  if (document.getElementById(id).className == 'header2_on') {
    document.getElementById(id).className='header2';
  }
  else if (document.getElementById(id).className == 'header2') {
    document.getElementById(id).className='header2_on';
  }
}