function getDomain(str){   
	if (str == null || str.length == 0)      
		return "";    
	str = str.replace("http://","").toLowerCase();    
	var i = str.indexOf("/");   
	if (i > -1)      
		str = str.substring(0, i);    
	var parts = str.split('.');    
	var len = parts.length;    
	if (len < 3)      
		return str;    
	var lastPart = parts[len-1];   
	var secondPart;    
	secondPart = parts[len-2];    
	var two = 2;    
	if (lastPart == "uk" && secondPart == "co")      
		++two;    
	if (len >= 0)      
		return parts.splice(len-two, two).join('.');    
	return "";
}

function queryparam( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null ) return "";  
	else return results[1];
}

function referrerqueryparam( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( document.referrer );  
	if( results == null ) return "";  
	else return results[1];
}



function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function GetSource ()
{
	var asource = "DIRECT";
	var refdomain = getDomain(document.referrer);
	if (refdomain + "" != "")
		asource = refdomain
	if (queryparam("CSREF") != "") 
		asource = queryparam("CSREF");
	if (queryparam("csref") != "") 
		asource = queryparam("csref");
	if (queryparam("utm_source") != "") 
		asource = queryparam("utm_source");
	return asource;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{

  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}


function check_cookie (mon, svalue, prefix, blankval) {

	var current_date = new Date;
	var future_date = current_date;
	var yearadd = 0;
	var cookie_year ;
	var cookie_month;
	var cookie_day ;
	
	future_date.Month = current_date.getMonth() + mon;
	if (future_date.Month > 11) {
		yearadd = 1;
		
		future_date.Month = future_date.Month - 12;
	}
	
	cookie_year = future_date.getFullYear ( ) + yearadd;
	cookie_month = future_date.Month;
	cookie_day = future_date.getDate ( );
	if (cookie_day > 28) {
		cookie_day = 28;
	}
	
	
	if ( ! get_cookie ( prefix + "path" + mon) )
	{
		set_cookie ( "first" + prefix + mon, svalue,  cookie_year, cookie_month, cookie_day);
		set_cookie ( prefix + "path" + mon, "/BOL/" + svalue + "/EOL/",  cookie_year, cookie_month, cookie_day);
		set_cookie ( "last" + prefix + mon, svalue,  cookie_year, cookie_month, cookie_day);
	}
	else
	{
		var lastsource = get_cookie ( "last" + prefix + mon);
		if (currentsource != blankval)
			lastval = svalue;
		var sourcepath = get_cookie ( prefix + "path" + mon);
		set_cookie ( prefix + "path" + mon, sourcepath.replace("/EOL/","") + "/" + svalue + "/EOL/",  cookie_year, cookie_month, cookie_day);
		set_cookie ( "last" + prefix + mon, lastval,  cookie_year, cookie_month, cookie_day);

	}
	  
}
	

if (! get_cookie ( "firstimpression" ) )
{
	var currentsource = GetSource();
	check_cookie(1, currentsource, "source", "DIRECT");
	check_cookie(6, currentsource, "source", "DIRECT");
	
	var term = "none";
	
	if (currentsource.indexOf("google") >= 0 ) {
		term = referrerqueryparam("q");
	}
	else if (currentsource.indexOf("bing") >= 0 ) {
		term = referrerqueryparam("q");
	}
	else if (currentsource.indexOf("yahoo") >= 0 ) {
		term = referrerqueryparam("p");
	}
	else {
		term = "none";
	}

	check_cookie(1, term, "keyword", "none");
	check_cookie(6, term, "keyword", "none");
	
	
	set_cookie ( "firstimpression", "no" );
}
else

{
}
