// wtcontroller.js v_1.0
function setCookie(name, value, expires, path, domain, secure) { 
  var date = new Date();
  date.setTime(date.getTime()+(expires*24*60*60*1000));
  var curCookie = name + "=" + escape(value) +
  		((expires) ? "; expires=" + date.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function eraseCookie(name) {
	setCookie(name,"",-1);
}

function wtController(){	
	/* logic flow for prospect/subscriber cookie
	check for existing wtcookie
		if yes (wtcookie)
			what value (p/s)
				if wtcookie value = (s)
					write meta with value(s)
				else
					check for tivocookie
						if yes
							update wtcookie with value(s)
							write meta with value (s)
						else
							write meta with value (p)
		else (no wtcookie)
			check for tivocookie
				if yes
					write new wtcookie with value(s)
					write meta with value(s)
				else (no tivocookie)
					write new wtcookie with value(p)
					write meta with value(p)
	 end
	*/
	
	
		var w =  getCookie('tivoWebtrends'); // get the cookie
		var c =  getCookie('tivoCookie'); // get the cookie
		var tagP = '<meta name="WT.seg_1" content="Prospect"/>';
		var tagS = '<meta name="WT.seg_1" content="Subscriber"/>';
		
		if(w != null){	
			if(w == 'Subscriber'){
				document.write(tagS);
			} 
			if(w == 'Prospect'){
				if(c){
					setCookie('tivoWebtrends','Subscriber','1825','/','.tivo.com','');
					document.write(tagS);					
				} else {
					document.write(tagP);
				}
			}
		}
		else {
			if(c != null){
					setCookie('tivoWebtrends','Subscriber','1825','/','.tivo.com','');
					document.write(tagS);	
				} else {
					setCookie('tivoWebtrends','Prospect','1825','/','.tivo.com','');
					document.write(tagP);
				}
		}
				
}
	
wtController();  // auto init	
