/* Begin: Function Definitions */

 	/**
 	 * Purpose: returns a parameter's value from a given URL query string.  For instance,
 	 * aQueryString=a=b&c=d
 	 * aKey=a
 	 *
 	 * return value would be: b
 	 *
 	 * Do not include the query string identifier '?'
 	 *
 	 * @param aQueryString	URL query string
 	 * @param aKey					parameter to look for
 	 *
 	 * @return returns the value of the query parameter requested
   */
	function getValue(aQueryString, aKey){
		if (aKey == null || aKey == ""){
			return "";
		}
		
		var keyIndex = aQueryString.indexOf(aKey + "=");
		
		if (keyIndex < 0){
			return "";
		}
		
		// increment the keyIndex to the equals sign
		keyIndex += aKey.length;
		
		var separatorIndex = aQueryString.indexOf("&", (keyIndex + 1));
		
		if (separatorIndex < 0){
			// key/value is at the end of the queryString
			return aQueryString.substring((keyIndex + 1), aQueryString.length);
		}else{
			return aQueryString.substring((keyIndex + 1), separatorIndex);
		}
	}	

	/**
		* Purpose:  Acts like Java's: String.startsWith() method.  That is, compares the 'src' value with the
		* 					'compareString' starting from the beginning of 'src'.
		*
		* @param src	string to search
		* @param compareString	string to compare from
		*
		* @return returns true if compareString is at the beginning of src, false otherwise.
		*/
	function isStartsWith(src, compareString){
		if (src.indexOf(compareString) == 0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * Purpose: Returns either the value or empty string (if null or empty).
	 *
	 * @param	aValue	value to use
	 *
	 * @return returns the value given or empty string (instead of null)
	 */
	function getEmptyForValue(aValue){
		if (aValue == null || aValue == ""){
			return "";
		}
		return aValue;
	}
	
	/**
	 * Purpose:  Determines if the given value is empty (null or empty string)
	 *
	 * @param aValue	value to compare
	 *
	 * @return returns true if aValue is null or empty, false otherise (meaning - the value is not-empty)
	 */
	function isEmpty(aValue){
		if (aValue == null || aValue == ""){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Purpose: Specific function to return the URI (in a sense).  Assumes the value
	 * 					starts with http://www. as this function is specifically utilized by
	 *					the searchEngineId determination scheme below.
	 *
	 * @param aValue	fully qualified URL to check (must start with http://www for now)
	 *
	 * @return returns the URI up to the query string character (not including) '?' or the
	 *				first slash after the http://www URI (ie: everything but the URI path).
	 */
	function getUrlUpToSlashOrQuery(aValue){
		// remove the http:// and everything up to the first period
		aValue = aValue.substring("http://".length, aValue.length);
		
		var domainIndex = aValue.indexOf(".");
		
		if (domainIndex >= 0){
			if (domainIndex == 0){
				if (aValue.length() > 1){
					aValue = aValue.substring(1, aValue.length);
				}else{
					return "";
				}
			}else{
				aValue = aValue.substring((domainIndex + 1), aValue.length);
			}
		}
		
		// look for '/'
		var slashIndex = aValue.indexOf("/");
		
		if (slashIndex >= 0){
			return aValue.substring(0, slashIndex);
		}
		else
		{
			// look for query
			var queryIndex = aValue.indexOf("?");
			
			if (queryIndex >= 0){
				return aValue.substring(0, queryIndex);
			}else{
				return aValue.substring(0, aValue.length);
			}
		}
	}//end function

/*	End: Function definitions */	

/*  Begin: main functionality */
	
	/**
	 * Purpose: Carry data from page to page within MNGi web properties without impact to underlying applications
	 *
	 * Deconstructs the referrer, current URL location, and cookies to determine search 
	 * engines being used as well as keywords.
	 *
	 * After all checks are performed, records each page's values as cookies (for any non-empty values).
	 *
	 * @author Erik Sahl
	 * @version 1.0	2006-01-23
	 */
	 
	// Determine the initial values to utilize from the referrer and current URL.
	var referer = document.referrer;
	var currUrl = location.href;
	var urlQuery = "";
	var queryTokenIndex = currUrl.indexOf("?");

	// define cookie variables which will be recorded as cookies.
	var searchEngineId = "";
	var searchEngineKeywords = "";

	// Retrieve the query string from the URL (if any were present).  Query string starts with a '?' character.	
	if (queryTokenIndex >= 0 && (queryTokenIndex + 1) < currUrl.length){
		urlQuery = currUrl.substring(queryTokenIndex + 1);
	}

	// Handle setting of the search engine ID (if we can determine it's one we want to track and we actually came
	// from a search engine).  
	if (referer == null || referer == ""){}
	else{
		// Search Engine ID
		var searchEngineId = "";
		var appendPaidString = "";

		// Determine if we're coming from a paid ad		
		if (currUrl.indexOf("CREF") >= 0 || currUrl.indexOf("cref") >= 0 || currUrl.indexOf("EADID") >= 0 || currUrl.indexOf("eadid") >= 0){
			// append to searchEngineId
			appendPaidString = " - PAID";	
		}
		 
		// see if the referer falls into one of the test cases...
		if (isStartsWith(referer, "http://www.google.")) { 
			searchEngineId = getUrlUpToSlashOrQuery(referer) + appendPaidString;
		}else if (isStartsWith(referer, "http://search.yahoo.")) { 
			searchEngineId = getUrlUpToSlashOrQuery(referer) + appendPaidString;
		}
		else if (isStartsWith(referer, "http://search.msn.com/")) { searchEngineId = "msn"+appendPaidString; }	
		else if (isStartsWith(referer, "http://search.aol.com/")) { searchEngineId = "aol"+appendPaidString; }
		else if (isStartsWith(referer, "http://www.ask.com/")) { searchEngineId = "ask.com"+appendPaidString; }
		else if (isStartsWith(referer, "http://cnet.search.com/")) { searchEngineId = "cnet"+appendPaidString; }
		else if (isStartsWith(referer, "http://search.netscape.com/")) { searchEngineId = "netscape"+appendPaidString; }
		else if (isStartsWith(referer, "http://mysearch.myway.com/")) { searchEngineId = "myway"+appendPaidString; }
		else if (isStartsWith(referer, "http://www.dogpile.com/info.dogpl/search/")) { searchEngineId = "dogpile"+appendPaidString; }
		else if (isStartsWith(referer, "http://www.overture.com/d/search/")) { searchEngineId = "overture"+appendPaidString; }
		else if (referer.indexOf("foo=info.dogpl") >= 0){ searchEngineId = "dogPile"+appendPaidString; }
	
		// Handle setting of searchEngineKeywords variable.  
		if (searchEngineId != "" && searchEngineId != appendPaidString){
			if (isStartsWith(searchEngineId, "localhost")){
				searchEngineKeywords = getValue(referer, "foo");
			}
			if (isStartsWith(searchEngineId, "mng")){
				searchEngineKeywords = getValue(referer, "foo");
			}
			if (isStartsWith(searchEngineId, "google")){
				searchEngineKeywords = getValue(referer, "q");	
			}
			if (isStartsWith(searchEngineId, "yahoo"))
			{
				searchEngineKeywords = getValue(referer, "p");
			}

			if (searchEngineId == "msn"){	searchEngineKeywords = getValue(referer, "q");}
			else if (searchEngineId == "aol") { searchEngineKeywords = getValue(referer, "encquery"); }
			else if (searchEngineId == "ask.com") { searchEngineKeywords = getValue(referer, "q"); }
			else if (searchEngineId == "cnet") { searchEngineKeywords = getValue(referer, "q"); } 
			else if (searchEngineId == "netscape") { searchEngineKeywords = getValue(referer, "query"); }
			else if (searchEngineId == "myway") { searchEngineKeywords = getValue(referer, "type"); }
			else if (searchEngineId == "dogPile") {
			keywordEndIndex = "http://www.dogpile.com/info.dogpl/search/web/".length;
	
			keywordSearchStringEndIndex = referer.indexOf("/", keywordEndIndex);
	
				if (keywordSearchStringEndIndex < 0){
					searchEngineKeywords = ""; 
				}else{
					searchEngineKeywords = referer.substring(keywordEndIndex, keywordSearchStringEndIndex);
				}
			}
			else if (searchEngineId == "overture") { searchEngineKeywords = getValue(referer, "Keywords"); }
			//if we're here it means we just came from a search engine we want to track set prop and seperately 
			//set cookie to track search engine info persistantly (per ken 4/7/06)
			prop39CookieVal = "" + searchEngineId + " / " + getEmptyForValue(searchEngineKeywords);
			s.prop39 =  prop39CookieVal + " / " + getEmptyForValue(s.pageName);
			setCookie("prop39", prop39CookieVal);
			
			prop41CookieVal = "" + searchEngineId;
			s.prop41 =  prop41CookieVal + " / " + getEmptyForValue(s.pageName);
			setCookie("prop41", prop41CookieVal);
			
		} else {
			//if we're here there was a refferer but it was not a search engine.  So we'll check cookies and see if 
			//there is one there, if there is use it and display new pagename (per ken 4/7/06)
			prop39CookieVal = getCookie("prop39");
			if (getEmptyForValue(prop39CookieVal)!=""){
				s.prop39 =  prop39CookieVal + " / " + getEmptyForValue(s.pageName);
			}

			prop41CookieVal = getCookie("prop41");
			if (getEmptyForValue(prop41CookieVal)!=""){
				s.prop41 =  prop41CookieVal + " / " + getEmptyForValue(s.pageName);
			}
		}			
	} // end if referrer was not empty


	
// END: search engine checks
/*  End: main functionality */
