// Javascript by Specific Impulse, Inc.

// Sets cookie values (generic).

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

}



// Sets known person Broadcast cookie

function setParams(myParameters) {
	  var today = new Date();
	  var expires = new Date();
	  expires.setTime(today.getTime() + 1000*60*60*24*90); //90 days
	  setCookie("sotracking", myParameters, expires, "/");
}


// Sets anonymous Broadcast cookie

function setAnonParams(myAnonParameters) {
	  var today = new Date();
	  var expires = new Date();
	  expires.setTime(today.getTime() + 1000*60*60*24*90); //90 days
	  setCookie("soanontracking", myAnonParameters, expires, "/");
}




// Determines which landing page the visitor used to enter the promo page

function whereFrom(landing_page) {
	  var today = new Date();
	  var expires = new Date();
	  expires.setTime(today.getTime() + 1000*60*60*24*90); //90 days
	  setCookie("solanding", landing_page, expires, "/");
}


// The following function returns a cookie value, given the name of the cookie

function getCookie(Name) {
  var search = Name + "="
  if (document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search)
		if (offset != -1) { // if cookie exists
			  offset += search.length
			  // set index of beginning of value
			  end = document.cookie.indexOf(";", offset)
			  // set index of end of cookie value
			  if (end == -1)
						end = document.cookie.length
			  return unescape(document.cookie.substring(offset, end))
		}
	}
}

		///<!--- generate truly unique number for anonymous ID --->
		var gettracking
		randomnum = Math.random();
		/// var fineday = new Date();
		/// datestamp = fineday.getTime();
		///<!--- if you want complete overkill: unique_number = datestamp.toString() + randomnum.toString();	--->
		unique_number = randomnum;
		
function processCookies() {

	///<!--- check for cookie, in case we recognise them --->
	if (getCookie('sotracking')) {

		gettracking = getCookie('sotracking');

	} else {

		if (getCookie('soanontracking')) {

			gettracking = getCookie('soanontracking');

		} else {

				anonymous_id = unique_number;

				gettracking = "?sourceID=unknown&sourceTool=unknown" + "&so_anon_id=" + anonymous_id + "&thisO=" + this_O;

				setAnonParams(gettracking); ///<!--- set soanontracking cookie --->
		}

	}
}

// debug functions 

function notify() {
	if (location.search) {
		searchstring = location.search;
		alert(searchstring);
		mypage=location.href;
		alert(mypage);
	}
	else {
		alert("no string present");
	}
}

function showCookie() {
  	if (getCookie('sotracking')) {
		foo = getCookie('sotracking');
		alert("cookie is: " + foo);
	}
}




