/*
addSourceInfo - this function examines the Google Analytics cookie to add the 
visitor source as a hidden field to the form.
To enable for a form, add this to the onclick event: return addSourceInfo() (ex. onclick="return addSourceInfo();")
parameters:
1) strFieldName: the name attribute of the hidden field whose value will be set to the GA cookie
*/
function addSourceInfo( strFieldName )
{
 // get the field object
 arrObjField = document.getElementsByName ( strFieldName );

 // check if the field is a valid object
 if ( !arrObjField )
 {
    // failed, no valid field object retrieved
    return false;
 }

 // check that we got at least one element
 if ( arrObjField.length < 1 )
 {
    return false;
 }
 
 // :TODO: Should we check if the length of the array of objects is longer than one?
 // We may have problems if that's the case. Discuss with team lead
 var z = _uGC(document.cookie, '__utmz=', ';'); 

 z = unescape(z);
 z = removeFrontDigits(z);
 z = detemineGAMedium(z);

 // set the fields's value to the string we got from the cookie
 arrObjField[0].value = z;

 return true;
}

/*
removeFrontDigits - removes the front digits from the standard __utmz cookie value (ie. '82146323.1192204049.1.1.')
parameters:
1) string_a - the string to manipulate
*/
function removeFrontDigits(string_a)
{
  j1 = splitString(string_a,'.','back');
	j1 = splitString(j1,'.','back');
  j1 = splitString(j1,'.','back');
  j1 = splitString(j1,'.','back');
	
	return j1;
}

/*
CombineSourceAndMedium - combines the two values into one string
*/
function CombineSourceAndMedium(medium, ga_source)
{
  return medium + " / " + ga_source;
}

/*
getGASource - given the medium and the utm string, this function returns the medium & source of the visit
parameters:
1) medium: the medium of the visit (already set)
2) astring: the rest of the visit string to parse
*/
function getGASource(medium, astring)
{
	var visitor_source, end_loc;
	var utmcsr_loc = astring.indexOf("utmcsr");

	//login here to get the source
	if(astring.indexOf("gclid")>-1)
	  visitor_source = "google";
	else if(utmcsr_loc > -1)
	{
	  end_loc = astring.indexOf("|",utmcsr_loc);//checks for the pipe char
		visitor_source = astring.substring(utmcsr_loc,end_loc);
		visitor_source = splitString(visitor_source, "=", "back"); //splits at the equal sign
	}
	else
	  visitor_source = "not set";
	
	return CombineSourceAndMedium(medium, visitor_source);
}

/*
determineGAMedium - parses the given string to determine the medium of the visit (ppc, email, organic, etc)
parameters:
1) astring - the visitor string to parse
*/
function detemineGAMedium(astring)
{
  var final_string;
	
	if(astring.indexOf("gclid")>-1)
	  final_string = getGASource("paid search",astring);
	else if(astring.indexOf("ppc")>-1)
	  final_string = getGASource("paid search",astring);
	else if(astring.indexOf("cpc")>-1)
	  final_string = getGASource("paid search",astring);
	else if(astring.indexOf("organic")>-1)
	  final_string = getGASource("organic search",astring);
	else if(astring.indexOf("referral")>-1)
	  final_string = getGASource("referral",astring);
	else if(astring.indexOf("direct")>-1)
	  final_string = getGASource("direct",astring);
		
	return final_string;
}

/*
splitString - splits a string at the specified character and returns the front or back
parameters:
1) astring - the string to split
2) char_to_split - where to split the string
3) front_or_back - whether to return the front end or back end of the string after splitting
*/
function splitString(astring, char_to_split, front_or_back)
{
  var s1, s2;
	s1 = astring.substr(0, astring.indexOf(char_to_split));
	s2 = astring.substr(astring.indexOf(char_to_split)+1);
	
	if(front_or_back == "front")
	  return s1;
	else
	  return s2;
}


