var kk = document.cookie;
function Get_Cookie(name) { // use: Get_Cookie("name");
	var index = kk.indexOf(name + "=");
	if (index == -1) return null;
	index = kk.indexOf("=", index) + 1;
	var endstr = kk.indexOf(";", index);
	if (endstr == -1) endstr = kk.length;
	var xxx = unescape(kk.substring(index, endstr));
	return xxx.toLowerCase();
}
function Get_mID(tURL){
     
	if (tURL.indexOf(".google.") > 0){
	    return 'genericSearchEngineGoogle';    				        
	}
	if (tURL.indexOf("search.msn.com") > 0){
	    return 'genericSearchEngineMSN';
	}
	if (tURL.indexOf("ask.com") > 0) {
    	return 'genericSearchEngineAsk';
    }
	if (tURL.indexOf("search.yahoo.com") > 0){
        return 'genericSearchEngineYahoo';
    }
    if (tURL.indexOf("search.aol.com") > 0 || tURL.indexOf("aolsearch.aol.com") > 0){
        return 'genericSearchEngineAOL';
    }
    if (tURL.indexOf("websearch.cs.com") > 0){
        return 'genericSearchEngineCompuServe';
    }
    if (tURL.indexOf("search.netscape.com") > 0){
        return 'genericSearchEngineNetscape';
    }
    if (tURL.indexOf("search.lycos.com") > 0){
        return 'genericSearchEngineLycos';
    }
    if (tURL.indexOf("dmoz.org") > 0) {
    	return 'genericSearchEngineDmoz';
    }
    if (tURL.indexOf("Hidden-Referrer") > 0){
	    return 'none';    				        
	}
    if (tURL.indexOf("yp.yahoo.com") > 0){
        return 'yahooYellowPages';
    }
    if (tURL.indexOf("yp.yahoo.com") > 0){
        return 'yahooYellowPages';
    }
    if (tURL.indexOf("dir.yahoo.com") > 0){
        return 'yahooDirectory';
    }
    if (tURL.indexOf("cgi.ebay.com") > 0){
        return 'ebayemail';
    }
    return Get_Server_Name(tURL); 		
    
}
function Get_Server_Name(tURL){

	var begSN;
	var endSN = tURL.indexOf("/",8);
	// if we didnt find a / after the 8th char (http(s)://, look for a question mark -->
	if (endSN < 1){
        endSN = tURL.indexOf("?");
    }
    // ok,no slash, no question mark, just get the last char position -->
    if (endSN < 1){
        endSN = tURL.length;
    }
    if (tURL.indexOf("https") > 0){
        begSN = 8;
    }
    else{
        begSN = 7;
    }
	return(tURL.substring(begSN, endSN));
}
function Get_Search_Words(refLn){
    // see if we can pluck off search words -->
    var x;
    if (refLn.indexOf(".google.") > 0){
		locStartSW = refLn.indexOf("&q=");
		x = 3;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?q=");
		    x = 3;
		}
		if (locStartSW > 0){
		    <!-- now that we know there are search words and we know where they start, -->
		    <!-- we need to determone where they end. they may end at the next occurance -->
		    <!-- of '&' or at the end of the referer line so...... -->
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		}
	}
	if (refLn.indexOf("search.msn.com") > 0){
		locStartSW = refLn.indexOf("&q=")
		x = 3;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?q=");
		    x = 3;
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
	}
	if (refLn.indexOf("search.yahoo.com") > 0){
		locStartSW = refLn.indexOf("&p=");
		x = 3;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?p=");
		    x = 3;
		    if (locStartSW < 1){
			    locStartSW = refLn.indexOf("&va=");
			    x = 4;
				if (locStartSW < 1){
				    locStartSW = refLn.indexOf("?va=");
				    x = 4;
				}
			}
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
    }
    if (refLn.indexOf("search.aol.com") > 0){
		locStartSW = refLn.indexOf("&encquery=");
		x = 10;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?encquery=");
		    x = 10;
		}
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("&query=");
		    x = 7;
		}
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?query=");
		    x = 7;
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
    }
    if (refLn.indexOf("websearch.cs.com") > 0){
		locStartSW = refLn.indexOf("&query=");
		x = 7;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?query=");
		    x = 7;
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
    }
    if (refLn.indexOf("search.netscape.com") > 0){
		locStartSW = refLn.indexOf("&query=");
		x = 7;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?query=");
		    x = 7;
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
    }
    if (refLn.indexOf("search.lycos.com") > 0){
		locStartSW = refLn.indexOf("&query=");
		x = 7;
		if (locStartSW < 1){
		    locStartSW = refLn.indexOf("?query=");
		    x = 7;
		}
		if (locStartSW > 0){
		    locEndSW = refLn.indexOf("&",locStartSW + 1);
		    if (locEndSW < 1){
		        locEndSW = refLn.length;
		    }
		    return refLn.substring(locStartSW+x, locEndSW);
		} 
    }
    // if I'm here, I still didn't find a sw, so take drastic measures -->
    locStartSW = refLn.indexOf("&query=");
    x = 7;
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("?query=");
	    x = 7;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("&encquery=");
	    x = 10;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("?encquery=");
	    x = 10;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("?q=");
	    x = 3;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("&q=");
	    x = 3;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("?p=");
	    x = 3;
	}
	if (locStartSW < 1){
	    locStartSW = refLn.indexOf("&p=");
	    x = 3;
	}
	if (locStartSW > 0){
	    locEndSW = refLn.indexOf("&",locStartSW + 1);
	    if (locEndSW < 1){
	        locEndSW = refLn.length;
	    }
	    return refLn.substring(locStartSW+x, locEndSW);
	} 
    return 'none';
}
var today = new Date();
var expiry = new Date(today.getTime() + 2 * 60 * 60 * 1000); // plus 2 hours
function SetCookie(name, value) { // use: setCookie("name", value);
	if (value != null && value != "")
		document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
	kk = document.cookie; // update kk
}
var cameFrom = window.document.referrer; 
var tURL = document.URL;
var stMID = document.URL.indexOf("marketingID=");
var mID;
var sw;
if (stMID > 0){
	<!-- user has arrived with a mID so set a cookie  -->
	stMID = stMID + 12; //add the length of mID=
	endMID = document.URL.indexOf("&", stMID );
	if (endMID < 1){
	    endMID = tURL.length;
	}
	mID = tURL.substring(stMID, endMID);
	sw = Get_Search_Words(tURL);
	if (sw.length < 1){
	    <!--I might be able to pull search works if we're comming from Google -->
	    sw = Get_Search_Words(cameFrom); 
	}
	if (sw.length < 1){
	    sw = Get_Cookie('searchWords');
	}
}
else{
	<!-- since no mID value was passed in, figure one out -->
	if (cameFrom.length == 0){
		<!-- user literally typed in www.a1-auto.com or www.online-auto-transport.com -->
		<!-- so propagate the current cookie  -->
		mID = Get_Cookie('marketingID');
		//if (! mID){
		//#    alert('100 I give up, making marketingID a1autotransport');
		//    mID = 'a1autotransport';
		//}
		sw = Get_Cookie('searchWords');
		if (! sw){
		    sw = 'none';
		}
	}
	else{
	    <!-- if user came from an steve crim page ...  -->
	    if (cameFrom.indexOf("a1terms") > 0 || 
	        cameFrom.indexOf("giber") > 0 ) {
	        mID = 'gs_steve1';
	    }
	    else{
	    
    	    <!-- if user came from an A1 page or a webmail page ...  -->
    		if( (cameFrom.indexOf("a1auto1.") > 0) || 
    		    (cameFrom.indexOf("a1-auto.") > 0) ||
    		    (cameFrom.indexOf("online-auto-transport.") > 0) ||
    		    (cameFrom.indexOf("mail.yahoo.com") > 0) ||
    		    (cameFrom.indexOf("hotmail") > 0) ||
    		    (cameFrom.indexOf("webmail") > 0) ||
    		    (cameFrom.indexOf("a1autotransport.") > 0)
    		    ){
    			mID = Get_Cookie('marketingID');
    			sw = Get_Cookie('searchWords');
    			<!--alert(mID); -->
    		}
    		else{
    			<!--  if we got here, this means that the user got to us via a non a1 site -->
    			<!--  that did mot supply a marketingID -->
    			mID = Get_mID(cameFrom); <!-- try to pluck the server name off -->
    			sw = Get_Search_Words(cameFrom); <!--  try to find some search words -->
    	    }
        }
	}    	
}
//if (mID == null){
//    alert('200 I give up, making marketingID a1autotransport');
//    mID = 'a1autotransport';
//}

SetCookie('marketingID',mID);
if (sw != null){
    if (sw.indexOf('none') < 1 && sw.length > 0 ){
        SetCookie('searchWords',sw);
    }
}
