// JavaScript Document

//get current system cookie

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" + escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

var utm_source = Get_Cookie('utm_source');
var utm_medium = Get_Cookie('utm_medium');
var utm_term = Get_Cookie('utm_term');
var utm_content = Get_Cookie('utm_content');
var utm_campaign = Get_Cookie('utm_campaign');

if (!utm_source && !utm_medium && !utm_term && !utm_content && !utm_campaign)
{
	utm_source = getURLParam('utm_source');
	utm_medium = getURLParam('utm_medium');
	utm_term = getURLParam('utm_term');
	utm_content = getURLParam('utm_content');
	utm_campaign = getURLParam('utm_campaign');
	
	if (!utm_source)
	{
		if(document.referrer)
		{
			utm_source = document.referrer;
		}
		else
		{
			utm_source = "Direct";
		}
		Set_Cookie( 'utm_source', utm_source, 365, '/', '', '' );
	}
	else
	{
	Set_Cookie( 'utm_source', utm_source, 365, '/', '', '' );
	Set_Cookie( 'utm_medium', utm_medium, 365, '/', '', '' );
	Set_Cookie( 'utm_term', utm_term, 365, '/', '', '' );
	Set_Cookie( 'utm_content', utm_content, 365, '/', '', '' );
	Set_Cookie( 'utm_campaign', utm_campaign, 365, '/', '', '' );	
	}
}

//set the session cookies

if (!Get_Cookie('utm_session'))
{
	
	if (getURLParam('utm_source'))
	{
		
		utm_session = 	getURLParam('utm_source') + "*" + 
							getURLParam('utm_medium') + "*" + 
							getURLParam('utm_term') + "*" + 
							getURLParam('utm_content') + "*" +
							getURLParam('utm_campaign');
	}
	else
	{
		if(document.referrer)
		{
			utm_session = document.referrer;
		}
		else
		{
			utm_session = "Direct";
		}
	
	}
	
	Set_Cookie( 'utm_session', utm_session, 1, '/', '', '' );
}	

