function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getAffiliateId()
{
	return getCookie('fbAffiliateId');
}

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 
} 

// set Affiliate Id for 1 year
affiliate = queryField("afcode");

if (affiliate != "") {
	setCookie('fbAffiliateId',affiliate,365);
}

