
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Client side function to make sure all links to external sites are
// redirected through BT gateway
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function normaliseLinks() {
	var sHostUrl	= window.location.protocol + "//" + window.location.host;
	var iLength		= sHostUrl.length;
	var oLinks		= document.getElementsByTagName("a");

	//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
	// Loop through and check all links in the page
	//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
	for (var ii=0; ii<oLinks.length; ii++) {
		var oLink	= oLinks[ii];
		var sHref	= oLink["href"];
		if (sHref.substring(0, 4) == "http" && sHref.substring(0, iLength) != sHostUrl) {
			//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
			// This link points to an external url, reformat to that
			// it gets directed through exit page first
			//:::::::::::::::::::::::::::::::::::::::::::::::::::::://
			oLink["href"] = "/publisha.system/redirect.aspx?url=" + encodeURIComponent(sHref);
		}
	}
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Used to initialise search form elements with default text
// as in "e.g. Florist" and also adds events to make sure boxes are
// cleared when user clicks in box.  
//
// Note that this will overwrite any onfocus/onblur functions if the 
// element value is empty when this is called.
//
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function initFormElement(sElementId, sDefaultText) {
	var oFormInput = document.getElementById(sElementId);
	oFormInput.defaultText = sDefaultText;
	if (oFormInput.value == "") {
		oFormInput.value = sDefaultText;
		oFormInput.onfocus = function() {
			if (this.value == this.defaultText) {
				this.value = "";
			}
		}
		oFormInput.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultText;
			}
		}
	}
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// When a search form is submitted, make sure we tidy up the default
// values we may have inserted (i.e. make sure we don't search for
// "e.g. florist"
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function validateSearchForm(oForm) {
	var oInputs = oForm.getElementsByTagName("input");
	for (var ii=0; ii<oInputs.length; ii++) {
		var oInput = oInputs[ii];
		if (oInput.defaultText != null && oInput.value == oInput.defaultText) {
			oInput.value = "";
		}
	}
	return true;
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Make sure that the advanced search form only has a 
// town XOR postcode.  
//
// A hack for now.
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

function validateAdvancedSearch() {
	try {
		var town = document.getElementById("Town");
		var postcode = document.getElementById("Postcode");
	
		if (town.value != "" && postcode.value != "") {
			document.getElementById("OnlyOneError").style.visibility = "visible";
			return false;
		}
	}
	catch (e) {}	
	
	return true;
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Load the printer friendly css
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function showPrinterFriendler() {
	try {
		var oCss = document.getElementById("MainCss");
		oCss.href = "/media/css/print.css";
	}
	catch (e) {}
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Back to standard css again
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function showStandardCss() {
	try {
		var oCss = document.getElementById("MainCss");
		oCss.href = "/media/css/main.css";
	}
	catch (e) {}
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Priority listings code
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

function togglePriorityListingsDetails(plid,plaction,plnow)
{
	if (plaction == "show")
	{
		if (plnow)
		{
			$('#' + plid + 'Details').show();
		}
		else
		{
			$('#' + plid + 'Details').slideDown('fast')
		}
		$('#' + plid + 'ToggleOff').show();
		$('#' + plid + 'ToggleOn').hide();		
	}
	else if (plaction == "hide")
	{
		if (plnow)
		{
			$('#' + plid + 'Details').hide();
		}
		else
		{
			$('#' + plid + 'Details').slideUp('fast')
		}
		$('#' + plid + 'ToggleOff').hide();
		$('#' + plid + 'ToggleOn').show();
	}
	else
	{
		if (plnow)
		{
			$('#' + plid + 'Details').toggle();
		}
		else
		{
			$('#' + plid + 'Details').slideToggle('fast')
		}
		$('#' + plid + 'ToggleOff').toggle();
		$('#' + plid + 'ToggleOn').toggle();
	}
}


//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// modal screen for search results
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#


function addModalBlock(content, clickfunc) {
    $("#Content").append("<div id=\"overlay\"></div>");
    clickfunc = clickfunc || (function() {removeModalBlock(content);});
    $("#overlay").bind('click', clickfunc);
    hilightContent(content); 
	$('#selService').hide();
}

function removeModalBlock(content) {
    $("#overlay").remove();
    removeContent(content);
	$('#selService').show();
}

function hilightContent(content) {
    $(content).css('z-index', '100');
}

function removeContent(content) {
    $(content).fadeOut(400);
}

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// cookie crud
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#


function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
    if (results)
        return (unescape(results[2]));
    else
        return null;
}


function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
    var cookie_string = name + "=" + escape(value);

    if (exp_y) {
        var expires = new Date(exp_y, exp_m, exp_d);
        cookie_string += "; expires=" + expires.toGMTString();
    }

    if (path)
        cookie_string += "; path=" + escape(path);

    if (domain)
        cookie_string += "; domain=" + escape(domain);

    if (secure)
        cookie_string += "; secure";

    document.cookie = cookie_string;
}


//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// url encode/decode
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

$.extend({ URLEncode: function(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        }
    } return o;
},
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});

//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
// Show Region directory
// Call from a link, then the resturn value will decide whether the
// link is followed or not
//#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
function ShowRegions()
{

	var regions = document.getElementById("SearchFormHomePageLinks");
	if (regions)
	{
		regions.style.display = "block";
		return false;
	}
	else
	{
		return true;
	}
}
