function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 	} 
} 

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

window.onload = externalLinks;

// FOR THE TOWN CHECKING
returnValue = 1;
	
function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	var partialRegEx  = /[A-Z]{1,2}[0-9]{1,2}/i;
	
	return postcodeRegEx.test(p) || partialRegEx.test(p);
}
	
function overrideLoc(theLoc) {
	if ($("input[name=overrideloc]").length == 0) {
		$("<input type='hidden' name='overrideloc' value='" + theLoc + "'>").appendTo("#searchform");		
	} else {
		$("input[name=overrideloc]").val(theLoc);
	}
}

// AJAX COMPANY NAME SEARCH //
$(document).ready(function() {


	fancyInput('','SearchB');
	fancyInput('','SearchD');


	// ON FOCUS LOCATION BLANKER //
	$("#SearchB").click(function() {
		//$(this).val("");//
	});


	// LOCATION AUTOCOMPLETE
	if ($("#SearchB").length > 0) {
		$("#SearchB").autocomplete("/x-locationsearch.asp",{
			max: 1000,
			minChars: 3,
			width: 300,
			loadingClass: "greybord",
			cellSeparator: "|",
			cacheLength: 1,
			matchSubset: 1,
			matchContains: 1,
			delay: 100,
			selectFirst: false,
			extraParams: {
				thesubsite: document.getElementById("thesubsite").value,
				thetheindex: document.getElementById("thetheindex").value,
				kids: document.getElementById("kids").value,
				parent: document.getElementById("parent").value,
				schemes: document.getElementById("schemes").value,
				isredsite: document.getElementById("isRedSite").value
			}
		})
		
		$("#SearchB").focus(function() {
			//if ($(this).val() == $(this)[0].defaultValue) {
			//	$(this).val("");
			//}
			
			overrideLoc("");
		});
	}

	// ON FOCUS LOCATION BLANKER //
	$("#SearchC").click(function() {
		//$(this).val("");//
	});


	// LOCATION AUTOCOMPLETE
	if ($("#SearchC").length > 0) {
		$("#SearchC").autocomplete("/x-tradesearch.asp",{
			max: 1000,
			minChars: 3,
			width: 300,
			loadingClass: "greybord",
			cellSeparator: "|",
			cacheLength: 1,
			matchSubset: 1,
			matchContains: 1,
			delay: 100,
			selectFirst: false,
			extraParams: {
				thesubsite: document.getElementById("thesubsite").value,
				thetheindex: document.getElementById("thetheindex").value,
				kids: document.getElementById("kids").value,
				parent: document.getElementById("parent").value,
				schemes: document.getElementById("schemes").value,
				isredsite: document.getElementById("isRedSite").value
			}
		})
		
		$("#SearchC").focus(function() {
			//if ($(this).val() == $(this)[0].defaultValue) {
			//	$(this).val("");
			//}
			
			overrideLoc("");
		});
	}

	// ON FOCUS COMPANY NAME BLANKER //
	$("#SearchD").click(function() {
		//$(this).val("");//
	});


	// COMPANY NAME AUTOCOMPLETE
	if ($("#SearchD").length > 0) {
		$("#SearchD").autocomplete("/x-companysearch.asp",{
			max: 20,
			minChars: 3,
			width: 250,
			loadingClass: "greybord",
			cellSeparator: "|",
			cacheLength: 1,
			matchSubset: 1,
			matchContains: 1,
			delay: 100,
			selectFirst: false,
			extraParams: {
				thesubsite: document.getElementById("thesubsite").value,
				thetheindex: document.getElementById("thetheindex").value,
				kids: document.getElementById("kids").value,
				parent: document.getElementById("parent").value,
				schemes: document.getElementById("schemes").value,
				isredsite: document.getElementById("isRedSite").value
			}
		});
	}
	
	$("#searchform").submit(function() {
		thereturn = valSearch();
		
		if (thereturn==true) {
			// DO THE DUPLICATE SHUFFLE
			if ($("#SearchB").val() > "") {
				if (isValidPostcode($("#SearchB").val())) {
					$("#searchform")[0].submit();
				} else {					
					$.post("/x-townsearch.asp",{
						"townname": $("#SearchB").val()
					},function(responseXML) {						
						if ($("errorcode",responseXML).length > 0) {						
							switch ($("errorcode",responseXML).text()) {
								case "0": default: // IN AFD, ONLY TOWN WITH THIS NAME
									$("#searchform")[0].submit();							
								break;
								case "1": case "2": // TOWN NOT FOUND
									$.facebox("<p>We are sorry, but we cannot find a town with this name.</p><p>Please try searching for a nearby larger town.</p>");
								break;
							}
						} else {							
							// WE GOT A POSTCODE ANYWHERE RESPONSE!
							theHTML = "<p>Please choose one of the following:</p><form><p style='font-size: 12px'>";

							$("Item",responseXML).each(function(i) {
								theHTML += "<input type='radio' name='loc' id='loc" + i + "' value='" + $("List",this).text() + "' />&nbsp;<label class='ambiguity' for='loc" + i + "'>" + $("List",this).text() + "</label><br />";
							});

							theHTML += "</p></form>";

							$.facebox(theHTML);

							$("input[name=loc]").click(function() {
								// $("<input type='hidden' name='overrideloc' value='" + $(this).val() + "'>").appendTo("#searchform");
								overrideLoc($(this).val());
								$.facebox.close();
								$("#searchform")[0].submit();
							});

							$("label.ambiguity").hover(function() {
								$(this).addClass("ambiguityhover").css("cursor","pointer");
							},function() {
								$(this).removeClass("ambiguityhover");						
							}).click(function() {
								theID = "#" + $(this).attr("for");
								// $("<input type='hidden' name='overrideloc' value='" + $(theID).val() + "'>").appendTo("#searchform");
								overrideLoc($(theID).val());
								$.facebox.close();
								$("#searchform")[0].submit();
							});
						}
					},"xml");
				}

				return false;
			}
		}else{
			return false;
		}
	});
});

function toggleOverlay(thediv,thedisp) {
	document.getElementById(thediv).style.display = thedisp;
}



function fancyInput(theaction,theid) {
   if (document.getElementById(theid)) {
	if (theaction=="focus") {
		//focus//
		if (theid=='SearchB' && document.getElementById(theid).value.substr(0,4)=='e.g.') {
			document.getElementById(theid).value='';
			document.getElementById(theid).style.color="#000000";
		}
		if (theid=='SearchC' && document.getElementById(theid).value.substr(0,4)=='e.g.') {
			document.getElementById(theid).value='';
			document.getElementById(theid).style.color="#000000";
		}
		if (theid=='SearchD' && document.getElementById(theid).value.substr(0,13)=='Business name') {
			document.getElementById(theid).value='';
			document.getElementById(theid).style.color="#000000";
		}
	}else if (theaction=="") {
		//focus//
		if (theid=='SearchB' && document.getElementById(theid).value.substr(0,4)!='e.g.') {
			document.getElementById(theid).style.color="#000000";
		}
		if (theid=='SearchC' && document.getElementById(theid).value.substr(0,4)!='e.g.') {
			document.getElementById(theid).style.color="#000000";
		}
		if (theid=='SearchD' && document.getElementById(theid).value.substr(0,13)!='Business name') {
			document.getElementById(theid).style.color="#000000";
		}
	}else{
		//blur//
		if (document.getElementById(theid).value=='') {
			if (theid=='SearchB') { theval = document.getElementById('defaultsearchtext').value; }
			if (theid=='SearchC') { theval = 'e.g. plumbers, decorators'; }
			if (theid=='SearchD') { theval = 'Business name'; }
			document.getElementById(theid).style.color="#999999";
			document.getElementById(theid).value=theval;
		
		}
	}
    }
}