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())) {
					
					//---This for URL Rewritting STARTS----
	                //alert("1");
	                CreateURLISAPI();
	                //---This for URL Rewritting ENDS----
					$("#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
								    
								    //---This for URL Rewritting STARTS----
					                //alert("2");
					                CreateURLISAPI();
					                //---This for URL Rewritting ENDS----
									$("#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();
								
								//---This for URL Rewritting STARTS----
				                //alert("3");
				                CreateURLISAPI();
				                //---This for URL Rewritting ENDS----
								$("#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();
								
								//---This for URL Rewritting STARTS----
				                //alert("4");
				                CreateURLISAPI();
				                //---This for URL Rewritting ENDS----
								$("#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.substr(0,3)=='All')) {
			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).value.substr(0,3)!='All')) {
			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') 
			{
			    if($("#thesubsite").val() == "1")
			        theval = 'All Agents';
			    else if($("#thesubsite").val() == "2")
			        theval = 'All Removers';
			    else if($("#thesubsite").val() == "6")
			        theval = 'All Compressed Air';
			    else
			        theval = 'e.g. plumbers, decorators'; 
			}
			if (theid=='SearchD') { theval = 'Business name'; }
			document.getElementById(theid).style.color="#999999";
			document.getElementById(theid).value=theval;
		
		}
	}
    }
}


function CreateURLISAPI()
{
    var sAction = ""
    
    //---This for URL Rewritting STARTS----
	//SearchB=Of(place or postcode)
	var SearchB = $("#SearchB").val();
	if(SearchB != "")
	{
	    var s1_SearchB = SearchB.substring(0,4)
	    if(s1_SearchB=="e.g.")
	        SearchB="";
	    else
	    {
	        SearchB = SearchB.replace("'", "");
	        SearchB = SearchB.replace("'", "");
	        SearchB = SearchB.replace(".", "");
	        SearchB = SearchB.replace(".", "");
	        SearchB = SearchB.replace("@", "");
	        SearchB = SearchB.replace(" ", "_");
	        SearchB = SearchB.replace(/\s/g, "_");
	        SearchB = SearchB.replace("&", "_");
	        SearchB = SearchB.replace("&", "_");
	        SearchB = SearchB.replace("&", "_");
	        SearchB = SearchB.replace("&", "_");
	        SearchB = SearchB.replace(",", "_");
	        SearchB = SearchB.replace(",", "_");
	        SearchB = SearchB.replace("/", "_");
	        SearchB = SearchB.replace("/", "_");
	        SearchB = SearchB.replace("/", "_");
	        SearchB = SearchB.replace("/", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("-", "_");
	        SearchB = SearchB.replace("___", "_");
	        SearchB = SearchB.replace("___", "_");
	        SearchB = SearchB.replace("___", "_");
	        SearchB = SearchB.replace("___", "_");
	        SearchB = SearchB.replace("__", "_");
	        SearchB = SearchB.replace("__", "_");
	        SearchB = SearchB.replace("__", "_");
	        SearchB = SearchB.replace("__", "_");
	        SearchB = SearchB.toLowerCase();
	    }
	}
	//SeachD=Business name
	var SearchD = $("#SearchD").val();
	if(SearchD != "")
	{
	    var s1_SearchD = SearchD.substring(0,8)
	    if(s1_SearchD=="Business")
	        SearchD="";
	    else
	    {
	        SearchD = SearchD.replace(/\s/g, "");
	        SearchD = SearchD.replace("-", "");
	        SearchD = SearchD.replace("-", "");
	        SearchD = SearchD.replace("-", "");
	        SearchD = SearchD.replace("&", "");
	        SearchD = SearchD.replace("&", "");
	        SearchD = SearchD.replace("&", "");
	        SearchD = SearchD.replace("'", "");
	        SearchD = SearchD.replace("'", "");
	        SearchD = SearchD.replace("'", "");
	        SearchD = SearchD.replace("(", "");
	        SearchD = SearchD.replace("(", "");
	        SearchD = SearchD.replace(")", "");
	        SearchD = SearchD.replace(")", "");
	        SearchD = SearchD.replace("/", "");
	        SearchD = SearchD.replace("/", "");
	        SearchD = SearchD.replace("/", "");
	        SearchD = SearchD.replace(".", "");
	        SearchD = SearchD.replace(".", "");
	        SearchD = SearchD.replace(".", "");
	        SearchD = SearchD.replace(",", "");
	        SearchD = SearchD.replace(",", "");
	        SearchD = SearchD.replace("@", "");
	        SearchD = SearchD.toLowerCase();
	    }
	}
	//SearchC = Of(like carpenters, plumbers, etc)
	var SearchC = $("#SearchC").val();
	if(SearchC != "")
	{
	    var s1_SearchC = SearchC.substring(0,4)
	    if(s1_SearchC=="e.g.")
	    {
	        SearchC="";
	    }
	    else if(s1_SearchC.substring(0,3)=="All")
	    {
	        SearchC="";
	    }
	    else
	    {
	        SearchC = SearchC.toLowerCase();
	        SearchC = SearchC.replace(" ", "_");
	        SearchC = SearchC.replace(" ", "_");
	        SearchC = SearchC.replace(" ", "_");
	        SearchC = SearchC.replace(" ", "_");
	        SearchC = SearchC.replace(" ", "_");
	        SearchC = SearchC.replace(/\s/g, "_");
	        SearchC = SearchC.replace("@", "");
	        SearchC = SearchC.replace("&", "_");
	        SearchC = SearchC.replace("&", "_");
	        SearchC = SearchC.replace("&", "_");
	        SearchC = SearchC.replace("&", "_");
	        SearchC = SearchC.replace(",", "_");
	        SearchC = SearchC.replace(",", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("/", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("-", "_");
	        SearchC = SearchC.replace("___", "_");
	        SearchC = SearchC.replace("___", "_");
	        SearchC = SearchC.replace("___", "_");
	        SearchC = SearchC.replace("___", "_");
	        SearchC = SearchC.replace("__", "_");
	        SearchC = SearchC.replace("__", "_");
	        SearchC = SearchC.replace("__", "_");
	        SearchC = SearchC.replace("__", "_");
	    }
	}
	
	if(SearchB!="" && SearchD != "" && SearchC == "")
	{
	    //alert("1");
	    sAction=SearchD+"-in-"+SearchB;
	}
	else if(SearchB!="" && SearchD == "" && SearchC == "")
	{
	    //alert("2");
	    sAction=SearchB;
	}
	else if(SearchB=="" && SearchD != "" && SearchC == "")
	{
	    //alert("3");
	    sAction=SearchD;
	}
	else if(SearchB!="" && SearchD == "" && SearchC != "")
	{
	    //alert("4");
	    sAction=SearchC + "-in-" + SearchB;
	}
	else if(SearchB == "" && SearchD == "" && SearchC != "")
	{
	    //alert("5");
	    if($("#thesubsite").val() != "")
	    {
	        var subsite = "";
	        switch ($("#thesubsite").val())
	        {
	            case "3":
	                sAction=SearchC + "-in-dundee";
	                break;
	            
	            case "4":
	                sAction=SearchC + "-in-durham";
	                break;
	            
	            case "14":
	                sAction=SearchC + "-in-angus";
	                break;
	            
	            case "15":
	                sAction=SearchC + "-in-norfolk";
	                break;
	            
	            case "20":
	                sAction=SearchC + "-in-fife";
	                break;
	            
	            case "21":
	                sAction=SearchC + "-in-suffolk";
	                break;
	            
	            case "0":
	                sAction = SearchC + "-in-london";
	                document.getElementById('SearchB').value="london";
	                break;
	            
	            case "1":
	                sAction = SearchC + "-in-london";
	                document.getElementById('SearchB').value="london";
	                break;
	                
	            case "2":
	                sAction = SearchC + "-in-london";
	                document.getElementById('SearchB').value="london";
	                break;
	                
	            case "6":
	                sAction = SearchC + "-in-london";
	                document.getElementById('SearchB').value="london";
	                break;
	                
	            case "27":
	                sAction = SearchC + "-in-london";
	                document.getElementById('SearchB').value="london";
	                break;
	                
	            default:
	                sAction="search.asp";
	            break;
	        }
	    }
	}
	else if(SearchB != "" && SearchD != "" && SearchC != "")
	{
	    //alert("6");
	    if($("#thesubsite").val() != "")
	    {
	        sAction=SearchC + "-in-" + SearchB + "-" + SearchD + "-search";
	    }
	}
	else if(SearchB == "" && SearchD != "" && SearchC != "")
	{
	    //alert("7");
	    if($("#thesubsite").val() != "")
	    {
	        var subsite = "";
	        switch ($("#thesubsite").val())
	        {
	            case "3":
	                sAction=SearchC + "-in-dundee-" + SearchD + "-search";
	            break;
	            
	            case "4":
	                sAction=SearchC + "-in-durham-" + SearchD + "-search";
	            break;
	            
	            case "14":
	                sAction=SearchC + "-in-angus-" + SearchD + "-search";
	            break;
	            
	            case "15":
	                sAction=SearchC + "-in-norfolk-" + SearchD + "-search";
	            break;
	            
	            case "20":
	                sAction=SearchC + "-in-fife-" + SearchD + "-search";
	            break;
	            
	            case "21":
	                sAction=SearchC + "-in-suffolk-" + SearchD + "-search";
	            break;
	            
	            case "1":
	                sAction=SearchC + "-in-london-" + SearchD + "-search";
	                document.getElementById('SearchB').value="london";
	            break;
	            
	            case "2":
	                sAction=SearchC + "-in-london-" + SearchD + "-search";
	                document.getElementById('SearchB').value="london";
	            break;
	            
	            case "6":
	                sAction=SearchC + "-in-london-" + SearchD + "-search";
	                document.getElementById('SearchB').value="london";
	            break;
	            
	            case "27":
	                sAction=SearchC + "-in-london-" + SearchD + "-search";
	                document.getElementById('SearchB').value="london";
	            break;
	            
	            default:
	                sAction=SearchC + "-in-london-" + SearchD + "-search";
	                document.getElementById('SearchB').value="london";
	                //sAction="search.asp";
	            break;
	        }
	    }
	}
	else
	{
	    //alert("8");
	    sAction="search.asp"
	}
	//---This for URL Rewritting ENDS----
	
	var sPath = "";
    var finalPath = ""
    var finalAction = "";
	
	sPath = window.location.pathname;
    var arrsPath = sPath.split('/');
    
    if(arrsPath[1]=="applications" || arrsPath[1]=="bar" || arrsPath[1]=="bcas" || arrsPath[1]=="bsikitemark" || arrsPath[1]=="BSIKitemark" || arrsPath[1]=="cle" || arrsPath[1]=="fsb" || arrsPath[1]=="jec" || arrsPath[1]=="mla" || arrsPath[1]=="naea" || arrsPath[1]=="oea" || arrsPath[1]=="sdf" || arrsPath[1]=="select" || arrsPath[1]=="snipef" || arrsPath[1]=="tpos" || arrsPath[1]=="trustedbusiness")
    {
        if(arrsPath[1]=="applications")
        {
            finalAction = "/" + arrsPath[1] + "/search.asp";
        }
        else if(arrsPath[1]=="bar")
        {
            if(arrsPath[2]=="britannia")
            {
                finalAction = "/" + arrsPath[1] + "/britannia/search.asp";
            }
            else if(arrsPath[2]=="gbliners")
            {
                finalAction = "/" + arrsPath[1] + "/gbliners/search.asp";
            }
            else
            {
                finalAction = "/" + arrsPath[1] + "/search.asp";
            }
        }
        else if(arrsPath[1]=="bcas")
        {
            if(arrsPath[2]=="airchannel")
            {
                finalAction = "/" + arrsPath[1] + "/airchannel/search.asp";
            }
            else
            {
                finalAction = "/" + arrsPath[1] + "/search.asp";
            }
        }
        else if(arrsPath[1]=="tpos")
        {
            if(arrsPath[2]=="beaumontresidential")
            {
                finalAction = "/" + arrsPath[1] + "/beaumontresidential/search.asp";
            }
            else if(arrsPath[2]=="colebrooksturrock")
            {
                finalAction = "/" + arrsPath[1] + "/colebrooksturrock/search.asp";
            }
            else if(arrsPath[2]=="kingsturge")
            {
                finalAction = "/" + arrsPath[1] + "/kingsturge/search.asp";
            }
            else
            {
                finalAction = "/" + arrsPath[1] + "/search.asp";
            }
        }
        else
        {
            finalAction = "/" + arrsPath[1] + "/search.asp";
        }
    }
    else if (arrsPath[1]=="tradingstandards")
    {
        if(arrsPath[2]=="angus" || arrsPath[2]=="fife" || arrsPath[2]=="dundee" || arrsPath[2]=="durham" || arrsPath[2]=="norfolk" || arrsPath[2]=="suffolk")
        {
            finalAction = "/" + arrsPath[1] + "/" + arrsPath[2] + "/search.asp";
        }
        else
        {
            finalAction = "/"+ sAction;
        }
    }
    else
    {
        //alert(document.getElementById("isRedSite").value);
        if(document.getElementById("isRedSite").value == "false" || document.getElementById("isRedSite").value == "False")
        {
            //alert(document.getElementById("hpid").value);
            switch( document.getElementById("hpid").value)
            {
                case "76":
                    finalAction = "/applications/search.asp";
                    break;                    
            
                case "5":
                    finalAction = "/bar/search.asp";
                    break;
                
                case "18":
                    finalAction = "/bar/gbliners/search.asp";
                    break;
                
                case "19":
                    finalAction = "/bar/britannia/search.asp";
                    break;
                
                case "16":
                    finalAction = "/bcas/search.asp";
                    break;
                    
                case "17":
                    finalAction = "/bcas/airchannel/search.asp";
                    break;
                    
                case "59":
                    finalAction = "/BSIKitemark/search.asp";
                    break;
                    
                case "44":
                    finalAction = "/cle/search.asp";
                    break;
                    
                case "66":
                    finalAction = "/fsb/search.asp";
                    break;
                    
                case "15":
                    finalAction = "/jec/search.asp";
                    break;
                    
                case "58":
                    finalAction = "/mla/search.asp";
                    break;
                    
                case "63":
                    finalAction = "/naea/search.asp";
                    break;
                    
                case "42":
                    finalAction = "/sdf/search.asp";
                    break;
                    
                case "43":
                    finalAction = "/select/search.asp";
                    break;
                    
                case "45":
                    finalAction = "/snipef/search.asp";
                    break;
                    
                case "2":
                    finalAction = "/tpos/search.asp";
                    break;
                
                case "84":
                    finalAction = "/trustedbusiness/search.asp";
                    break;
                
                case "20":
                    finalAction = "/tpos/beaumontresidential/search.asp";
                    break;
                
                case "21":
                    finalAction = "/tpos/colebrooksturrock/search.asp";
                    break;
                
                case "22":
                    finalAction = "/tpos/kingsturge/search.asp";
                    break;
                
                case "32":
                    finalAction = "/tradingstandards/angus/search.asp";
                    break;
                
                case "25":
                    finalAction = "/tradingstandards/dundee/search.asp";
                    break;
                
                case "26":
                    finalAction = "/tradingstandards/durham/search.asp";
                    break;
                
                case "46":
                    finalAction = "/tradingstandards/fife/search.asp";
                    break;
                
                case "34":
                    finalAction = "/tradingstandards/norfolk/search.asp";
                    break;
                
                case "49":
                    finalAction = "/tradingstandards/suffolk/search.asp";
                    break;
                    
            }
        }
        else
        {
            finalAction = "/"+ sAction;
        }
    }
        
	$("#searchform")[0].action = finalAction;
}
