window.onload = fn_onload;

function fn_onload (event) {

	fn_ahref();

}

function fn_ahref() {

	if (!document.getElementsByTagName) return;
	arr_nodes = document.getElementsByTagName('a');

	for (i = 0; i < arr_nodes.length; i++) {
		obj_node = arr_nodes[i];
		if (obj_node.getAttribute('href') && obj_node.getAttribute('rel') == 'external')
		obj_node.target = '_blank';
	}

}






/**
 * PPC PHONE NUMBER CODE
 *
 * Gets the value of the given parameter from the query string
 *
 * name            The parameter name
 *
 * Return value    The value of the parameter
 */
function getParameter(name)
{
// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

// parse out name/value pairs separated via &
	var args = querystring.split('&');

	var toReturn = "default"

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		currentName = unescape(pair[0]);

// Fix broken unescaping, and remove exclamation mark and anything after
		temp = unescape(pair[1]).split('!');
		currentValue = temp[0];

    if (name == currentName)
    {
        toReturn = currentValue;
    }
	}

	return toReturn;
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */

function setPPCCookie(name, value, expires, path, domain, secure)
{
    if(value!="default")
    {
    	var defaultExpireDate = new Date();
    	defaultExpireDate.setDate(defaultExpireDate.getDate()+30);

    	document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "; expires=" + defaultExpireDate.toGMTString()) +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    }
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getPPCCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function ChangePPCPhoneNo()
{	
    setPPCCookie("phone", getParameter("phone"));    
    if (getPPCCookie("phone") == "ppc")
    {
        document.getElementById("ppctelnochange").style.backgroundImage = "url(/images/ppc_phonenumber.gif)";

	var arrayOfSpans = document.getElementsByTagName("span"); 
	for(var i=0;i<arrayOfSpans.length;i++) 
        {
	    if(arrayOfSpans[i].className == "ppctelephoneno") 
            {
		arrayOfSpans[i].innerHTML = "0845 863 0526";
	    }
	}
    }	
}






function getAgentCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function CheckAgentLoggedIn(){
	if(document.getElementById('liAgentLogin') != null)
	{
		var agentname;
		agentname =getAgentCookie('agentloginname'); 
		if(agentname != null)
		{
			document.getElementById('liAgentLogin').innerHTML = agentname + ' logged in';
		}
	}
}