
var myHost = "no";
var mysearch = "no";
var refType = "no";
var adStr = "no";

if(document.referrer && (document.referrer != "") ){

	refTemp = document.referrer.split("?");
	myHost = refTemp[0];
	mysearch = refTemp[1];
	refType= "referrer";

/*	
	if(mysearch && mysearch.length > 1){

		var adStart = mysearch.indexOf("&ref=");
		adStr = mysearch.substring(adStart + 5);
		var AdEnd = adStr.indexOf("&");
		adStr = adStr.substr(0,AdEnd);
	}	
*/
}


	var locationQuery = location.search.substring(1);
	//var refType= "location";

	var args = new Object();

	var mypairs = locationQuery.split("&");

	// get search string from URL
	// put in args array as name-value pairs
	for(var i = 0;i < mypairs.length;i++) {

		var pos = mypairs[i].indexOf('=');
		if(pos == -1) continue;
		var argname = mypairs[i].substring(0,pos);
		var value   = mypairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}

	// get ad used if any
	var pageRef = args["ref"];

	if( (pageRef) && ( (pageRef == "google1") ||
	(pageRef == "googlefrench") || (pageRef == "yahoo") ) ){

		adStr = pageRef;
		
		refType = "location";
	}

if(myHost && myHost.length > 6){

	var hostStart = myHost.indexOf("//");
	myHost = myHost.substring(hostStart + 1);
}
else{

	myHost = "none";
}


if(mysearch && mysearch.length > 1){

	mysearch = mysearch.replace(/-/g,"$");
	mysearch = mysearch.replace(/:/g,"$");
}
else{

	mysearch = "none";
}
//  ------------------------------------------------------
//--------------------------------------------------------
//  look for refTrack cookie

var allCookies = document.cookie;

var refIndex = allCookies.indexOf("refTrack=");

var myCookie = "";

var cookieArray = new Object();
var cookieStr;
var thisMonth;
var thisYear;
var thisDay;


if(refIndex != -1){  // refTrack (cookie) already exists ------------------

	// read cookie -----------------------------------------
	var startPos = refIndex + 9;
	var endPos = allCookies.indexOf(";",startPos);
	if(endPos == -1){ endPos = allCookies.length; }

	myCookie = allCookies.substring(startPos,endPos);

	if(myCookie){ // if cookie exists

		var pairs = myCookie.split(":");

		for(var i = 0;i < pairs.length;i++){

			var pos = pairs[i].indexOf('-');
			if(pos == -1) { continue; }

			var argname = pairs[i].substring(0,pos);
			var argVal = pairs[i].substring(pos + 1);

			cookieArray[argname] = unescape(argVal);
		}

		//get Ad for refAd  ----------------------------------
		if( (adStr) && ( (adStr == "google1") ||
		(adStr == "googlefrench") || (adStr == "yahoo") ) ){

			if(cookieArray["refAd"]){

				var cookieAd = cookieArray["refAd"];
				cookieStr = "refAd-" + cookieAd + "#" + adStr + ":";
			}
			else{

				cookieStr = "refAd-" + adStr + ":";
			}
		}
		else{

			if(cookieArray["refAd"]){

				var cookieAd = cookieArray["refAd"];
				cookieStr = "refAd-" + cookieAd + ":";
			}
			else{

				cookieStr = "refAd-none:";
			}
		}

	// if there is a host in the cookie add it to the new cookie
	// if not use what came in with the URL
		var thisRefHost = cookieArray["refHost"];
		if(thisRefHost){

			cookieStr += "refHost-" + thisRefHost + ":";
		}
		else{

			cookieStr += "refHost-" + myHost + ":";
		}

	// put search string in cookie -------------------------------
	// -----------------------------------------------------
		var thisRefQ = cookieArray["refQ"];
		if(thisRefQ){

			cookieStr += "refQ-" + thisRefQ + ":";
		}
		else{

			cookieStr += "refQ-" + mysearch + ":";
		}

	  // check to see if original entry date is in the cookie
		thisMonth = cookieArray["month"];
		thisYear = cookieArray["year"];
		thisDay = cookieArray["day"];
	}
}
else{

	cookieStr = "refAd-" + adStr + ":refHost-" + myHost + ":refQ-" + mysearch + ":";
}

cookieStr += "refType-" + refType + ":";

// put in original entry date
//
now = new Date;

if(thisMonth && thisDay && thisYear){
	;
}
else{

	nowMonth = new Date;
						// month add 1 to get normal month (not a 0)
	nowMonth.setMonth(nowMonth.getMonth() + 1);
	thisMonth = nowMonth.getMonth();
	if(thisMonth < 10){

		thisMonth = "0" + thisMonth;
	}

	thisDay = now.getDate();
	if(thisDay < 10){

		thisDay = "0" + thisDay;
	}
}

cookieStr += "year-" + now.getFullYear() + ":";
cookieStr += "month-" + thisMonth + ":";
cookieStr += "day-" + thisDay + ":";

 // set cookie   ----------------------------------------------
//
var expireDate = new Date;

expireDate.setMonth(expireDate.getMonth() + 3);

document.cookie = "refTrack=" + cookieStr + ";expires=" + expireDate.toGMTString() + ";path=/";

temp2 = document.referrer.split("?");
//window.status = temp2[1] + " .. " + adStr;