// cookie checking script
// JLee 11/2/2004

var RlocalUID = unescape(RlocalUID);
var cookie_name = "RlocalUID=";
var cookies = document.cookie;
var RlocalUID_cookie = cookies.indexOf(cookie_name);

function check_cookie() {

	if (RlocalUID_cookie != -1) { return true; }
	if (RlocalUID_cookie == -1) { return false; }
			
}

function check_RlocalUID() {
	
	if (RlocalUID != "") { return true; }
	if (RlocalUID == "") { return false; }
	
}


function get_cookie(v) {
	
	if (v==0) {
				
		var check = check_RlocalUID();
		
		if (check == false) { return ""; } // if no cookie, return nothing
		if (check == true) { 
		
			// var start = RlocalUID_cookie + cookie_name.length;
			// var end = cookies.indexOf(";", start);
			// if (end == -1) { end = cookies.length; }
			// var value = cookies.substring(start, end);
			var data = RlocalUID;
			
			return data;
			
		}

	}
	
	if (v!=0) {
	
		var check = check_RlocalUID();
		
		if (check == false) { return ""; } // if no cookie, return nothing
		if (check == true) { // check for adid
		
			// var start = RlocalUID_cookie + cookie_name.length;
			// var end = cookies.indexOf(";", start);
			// if (end == -1) { end = cookies.length; }
			// var value = cookies.substring(start, end);
			var data = RlocalUID;
			
			var adid = "&adid="+v; // search string				
			var results = data.search(adid);
							
			if (results == -1) { return ""; } // adid doesn't match
			if (results != -1) {
			
				return data;
			
			}
		
		}
	
	}
	
}

function email_redirect(n,v) {
	
	var data = get_cookie(v);
	var check = check_cookie();
	
	var address = n;
	var domain = "@reachlocal.com";
	var true_url = "http://search.reachlocal.com/coupon/";
	var false_url = "http://"+location.host+"/rlmail/";
	
	var redir_url = "http://"+location.host;
	
	if (check == true) { // cookie exists
	
		if (data == "") { document.location.href = false_url+"?&target_email="+address+domain+"&redir="+redir_url; } //  adid mismatch, redirect to generic
		if (data != "") { document.location.href = true_url+"?"+data+"&ptt=4&target_email="+address+domain; } // adid correct
		
	}
			
	
	if (check == false) { // cookie does not exist, redirect to generic
	
		document.location.href = false_url+"?&target_email="+address+domain+"&redir="+redir_url;
		
	}
	
	// alert (location.host);

}



// alert(RlocalUID);
