function queryField(opt)
{
var keyloc // The location of the start of "key=value"
var nextkey // The start of the next key
var start // The start of the value
var opts // The options specified by the search string
var optval // The value of the selected option

// Determine the options/search string
opts=self.location.search
// Most keys start after an & and are followed by an = sign
keyloc = opts.indexOf("&" + opt + "=")
// If a string isn't found, indexOf returns -1. So, we try the "first"
// key, which appears right after the initial question mark
if(keyloc == -1) {
keyloc = opts.indexOf("?" + opt + "=")
}
// If, at this point, we still haven't found the key, stop.
if (keyloc == -1) {
return ""
} 
// The value normally ends with an ampersand (which marks the start of the next key/value pair)
nextkey = opts.indexOf("&",keyloc+1) 
// But sometimes there is no next pair
if (nextkey == -1) {
nextkey = opts.length
}
// Okay, what next? Verify that it's reasonable
if (nextkey < keyloc) {
return ""
} 
// Get and return the value
sval = keyloc+2+opt.length
optval = plustospace(unescape(opts.substring(sval,nextkey)))
return optval
} // getOption()

function plustospace(txt)
/* Converts all the plus signs in a string to spaces. */
{
if (txt == "") { return txt }

// Variables
var newtxt="" // The txt without the spaces
var pos=0 // The position of the plus sign
var prev=0 // The position of the previous plus sign
var done=false // sentinel for loop
var tmp // Used for debugging

// Repeatedly find the next + sign, stopping when no more
// are found
// alert("Text is '" + txt + "'") // DEBUG
while (!done) {
pos = txt.indexOf("+",prev)
// tmp = prompt("Plus found at '" + pos + "'", "OK") // DEBUG
// if (tmp != "OK") { done = 1 }// DEBUG
if (prev >= txt.length) {
done = true
}
else if (pos == 0) {
prev=1
newtxt += " "
}
else if ((pos < 0) || (pos == "")) {
// Not found ... exit
done = true
}
else {
// Copy text
if (pos>prev) { newtxt += txt.substring(prev,pos) }
newtxt += " "
// And move on
prev=pos+1
}
}
// Get the last little bit
newtxt += txt.substring(prev,txt.length)
return newtxt 
} 

function writeCookie(cookie_name, cookie_value, cookie_life) 
{
var today = new Date()
var expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)

var cookie_string =cookie_name + "=" + escape(cookie_value)
if(cookie_life){ cookie_string += "; expires=" + expiry.toGMTString()}
cookie_string += "; path=" + "/"
document.cookie = cookie_string
}

function readCookie(NameOfCookie)
{
if (document.cookie.length > 0) 
{ 
begin = document.cookie.indexOf(NameOfCookie+"="); 
if (begin != -1) 
{ 
begin += NameOfCookie.length+1; 
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end));
} 
}
return null; 
}

function setAffiliateField()
// the affiliate field has a fixed name 'affiliate'
{
// get the affiliate parameter from the url if exists
// e.g. http://www.website.co.uk?affiliate=gurteen

affiliate = queryField("affiliate");

// now drop a transient cookie called 'affiliate' with the affiliate parameter as its value i.e. "gurteen"

if (affiliate == "") 
{ 
// do nowt
}
else
{
writeCookie("affiliate",affiliate,30);
} 

// now get the cookie and set the affiliate field 

temp = readCookie('affiliate');
if (temp == null) {affiliate = ""} else {affiliate = temp};
document.write('<input type="text" name="affiliate" value="'+affiliate+'">');
document.write("<br><br>Affiliate: " + affiliate)
}

function setHiddenAffiliateField()
// the affiliate field has a fixed name 'affiliate'
{
// get the affiliate parameter from the url if exists
// e.g. http://www.website.co.uk?affiliate=gurteen

affiliate = queryField("affiliate");

// now drop a transient cookie called 'affiliate' with the affiliate parameter as its value i.e. "gurteen"

if (affiliate == "") 
{ 
// do nowt
}
else
{
writeCookie("affiliate",affiliate,30);
} 

// now get the cookie and set the affiliate field 

temp = readCookie('affiliate');
if (temp == null) {affiliate = ""} else {affiliate = temp};
document.write('<input type="Hidden" name="affiliate" value="'+affiliate+'">');
}
