String.prototype.startsWith = function(str)
{
	return (this.match("^"+str)==str);
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++)
	{
		if (vars[i].startsWith(variable+"="))
		{
			// this ignores subsequent "=" characters allowing for return of composite fields
			return vars[i].substr(variable.length + 1);
		}
	}
}

function getReturnLink()
{
	// need lrcConfig.js loaded to read variables
	var rt;  // Return Text
	var ru;  // Return URL
	var sc;  // Site Context
	var branding;
	var lan; // Language

	// read the config from the cookie
	branding = readCookie("xgovk3w");

	// override: read the config from the url
	temp = getQueryVariable("xgovk3w");	// is this correct?  branding may be set by another variable in xgovf0p
	if(temp) branding = temp;

	// get the language
	lan = readCookie("xgovd2v");
	temp3 = getQueryVariable("xgovd2v");
	if(temp3) lan = temp3;
	
	// select the correct return link text depending on the text
	// can also change the actual link here if required
	if (lan == "cy" & branding == "bl230") branding="bl230w";
	
	rt = returnText[branding];

	if(rt == null) rt = returnText["bl1000"];	// default branding if undefined
	ru = returnLinks[branding];
	if(ru == null) ru = returnLinks["bl1000"];

	// now pull out the siteContext and append that too.
	sc = readCookie("xgovf0p");
	temp2 = getQueryVariable("xgovf0p");

	if(temp2) sc = temp2;			// again, override cookie with querystring
	if(sc != null) ru += "&xgovf0p=" + sc;
	
	if(lan != null) ru += "&xgovd2v=" + lan; 
	
	
	
	return "<a href=\"" + ru + "\">" + rt + "</a>";
}

// This check the language parameter and returns English or Welsh - include on page to verify correct interpretation
// Returns 'Default' if no languuage parameter passed

function checkLanguage()
{
	var lang;
	var rl; //Return Language
	
	rl = "Default";
	
	lang = readCookie("xgovd2v");
	temp4 = getQueryVariable("xgovd2v");
	if(temp4) lang = temp4;
	if(lang == "en") rl = "English";
	if (lang == "cy") rl = "Welsh";
	
	return rl;
}

//Custom function for swapping styles
function getCustomClass()
{
	// need lrcConfig.js loaded to read variables
	var rt;  // Return Text
	var sc;  // Site Context
	var branding;
	var lan; // Language

	// read the config from the cookie
	branding = readCookie("xgovk3w");

	// override: read the config from the url
	temp = getQueryVariable("xgovk3w");	// is this correct?  branding may be set by another variable in xgovf0p
	if(temp) branding = temp;

	// get the language
	lan = readCookie("xgovd2v");
	temp3 = getQueryVariable("xgovd2v");
	if(temp3) lan = temp3;
	
	// select the correct return link text depending on the text
	// can also change the actual link here if required
	if (lan == "cy" & branding == "bl230") branding="bl230w";
	
	rt = returnCustomClass[branding];
	if(rt == null) rt = returnCustomClass["bl1000"];	// default branding if undefined
		
	return "<div class=\"" + rt + "\">";
}
