//Javascript Document
var newlocation=new String(location.href);
var origlocation=new String(location.href);
var lastpage=new String(document.referrer);
//document.write("ScriptLAST: "+document.referrer+" :LASTScript");

//check for a qeustion mark at the end of the lastpage and if there chop off
if ((lastpage.length-1)==lastpage.lastIndexOf("?"))
{
   lastpage=lastpage.slice(0,(lastpage.length-1));
}

var lastpieces=lastpage.split("?");
//document.write("here: "+lastpieces[0]+"here: "+lastpieces[0].match("http://mcdlife.com"));
// if first term of lastpieces is mcdlife and it's not the only term then chop off first term
if (lastpieces[0].match("http://www.mcdlife.com")=="http://www.mcdlife.com"||lastpieces[0].match("http://mcdlife.com")=="http://mcdlife.com")
{
	if (lastpieces.length!=1)
	{
		lastpieces.shift();
	}
}

// add terms to site here and if lastpieces is longer than one term rejoin the pieces with question marks
if (lastpieces.length!=1)
{
	lastpage=lastpieces.join("?");
}
else
{
	lastpage=lastpieces[0];
}

//check if came from adwords and chop off gclid and trash
var origlocationpara=origlocation.split("?")
if (origlocationpara.length>1)
{
	if (origlocationpara[1].slice(0,5)=="gclid"&&origlocationpara.length==2)
	{
		origlocation=origlocationpara[0];
	}
}
newlocation=origlocation+"?"+lastpage;

// if last page hasn't been added yet and there is one add it
if (origlocation.match("\\?")==null&&lastpage!="")
{
	location.href=newlocation;
}