/*
*	Description: spss_tag_page
*/

var _site_id=null;
var _session_id=null;
var _customer_id=null;
var _profile_id=null;
var _crm_mode=null;
var _hpc_mode=null;
var _tracking_id=null;
var _campaign_id=null;
var _value=null;
var _search_hits=null;
var _search_keywords=null;
var search_keywords=null;
var _cust_seg=null;

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	var expire_date = new Date();
	expire_date.setTime(expire_date.getTime()-(1*60*1000));
	var expires = "; expires="+expire_date.toGMTString();
	document.cookie = name+"=; expires="+expires+"; path=/";
}

function spss_tag_page() {
var pd_ver=001;
//var pd_LogProtocol="https://";
//var pd_LogPort="";
//var pd_LogHost="test.komplett-analytics.com";
//var pd_GifPath="/";
//var pd_Gif="1px.gif";

/** First we make the url-path to the 1px.gif **/
//First check the global override variable. This variable is supposed to be set in calling pages,
//dependent on U/T/P environment.
var pd_LogURL;
if(_SPSSGlobalServerUrlOverride) {
    pd_LogURL = _SPSSGlobalServerUrlOverride;
}
else {
    pd_LogURL = "https://www.komplett-analytics.com/1px.gif";  //Default to prod, just in case.
}
//pd_LogProtocol + 
//		pd_LogHost +
//		pd_LogPort +
//		pd_GifPath +
//		pd_Gif;

/*** Then we add information to the pd_LogURL ***/

/* Create a timestamp in ms for use as an id - time calculated as time since 01/01/1970 */
var mydate = new Date();
var click_time = mydate.getMilliseconds();
click_time += Date.parse(mydate);
pd_LogURL += "?click_time=" + click_time;

/**** From the naviagtor object */
var n_name=navigator.appName;
var n_version=navigator.appVersion;
var n_cookieenabled=navigator.cookieEnabled;
var n_platform=navigator.platform;

pd_LogURL += "&n_name=" + n_name;
pd_LogURL += "&n_version=" + n_version;
pd_LogURL += "&n_cookieenabled=" + n_cookieenabled;
pd_LogURL += "&n_platform=" + n_platform;

/**** Then the document object */
var d_ref=document.referrer;

pd_LogURL += "&d_ref=" + d_ref;

/**** Then the location object */
var l_search=location.search;
var l_hostname=location.hostname;
var l_pathname=location.pathname;

pd_LogURL += "&l_hostname=" + l_hostname;
pd_LogURL += "&l_pathname=" + l_pathname;
pd_LogURL += "&l_search=" + l_search;

/**** Then the screen object */
var s_height=screen.height;
var s_width=screen.width;
var s_colorDepth=screen.colorDepth;

pd_LogURL += "&s_height=" + s_height;
pd_LogURL += "&s_width=" + s_width;
pd_LogURL += "&s_colorDepth=" + s_colorDepth;

/** Set Komplett-specific variables */
pd_LogURL += "&_site_id=" + ((_site_id != null) ? _site_id : "");
pd_LogURL += "&_session_id=" + ((_session_id != null) ? _session_id : "");
pd_LogURL += "&_customer_id=" + ((_customer_id != null) ? _customer_id : "");
pd_LogURL += "&_profile_id=" + ((_profile_id != null) ? _profile_id : "");
pd_LogURL += "&_crm_mode=" + ((_crm_mode != null) ? _crm_mode : "");
pd_LogURL += "&_hpc_mode=" + ((_hpc_mode != null) ? _hpc_mode : "");
pd_LogURL += "&_tracking_id=" + ((_tracking_id != null) ? _tracking_id : "");
pd_LogURL += "&_search_hits=" + ((_search_hits != null) ? _search_hits : "");
pd_LogURL += "&_campaign_id=" + ((_campaign_id != null) ? _campaign_id : "");
pd_LogURL += "&_value=" + ((_value != null) ? _value : "");

// by default set search keywords to value from results page
search_keywords = _search_keywords;

/** Get click-source & search info from cookies */
var click_source = null;
click_source = readCookie("click_source" + location.pathname);
if (click_source != null) { eraseCookie("click_source" + location.pathname) };

var my_search_keywords = null;
my_search_keywords = readCookie("search_keywords" + location.pathname);
if (my_search_keywords != null) { search_keywords = my_search_keywords; eraseCookie("search_keywords" + location.pathname) };

var search_rank = null;
search_rank = readCookie("search_rank" + location.pathname);
if (search_rank != null) { eraseCookie("search_rank" + location.pathname) };

/* Set click-source and search info */
pd_LogURL += "&click_source=" + ((click_source != null) ? click_source : "");
pd_LogURL += "&search_rank=" + ((search_rank != null) ? search_rank : "");
pd_LogURL += "&search_keywords=" + ((search_keywords != null) ? search_keywords : "");

// Permanent cookie for visitor id
var visitor_id = null;
visitor_id = readCookie("visitor_id");
if (visitor_id == null) { 
	visitor_id = _session_id;
}
createPermCookie("visitor_id", visitor_id);

pd_LogURL += "&visitor_id=" + visitor_id;

pd_LogURL += "&_cust_seg=" + ((_cust_seg != null) ? _cust_seg : "");


/**** Now we makes the call to the page dot */
var image = new Image();
image.src = pd_LogURL;
}

function createTempCookie(name,value) {
	var expire_date = new Date();
	expire_date.setTime(expire_date.getTime()+(1*60*1000));
	var expires = "; expires="+expire_date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function createPermCookie(name,value) {
	var expire_date = new Date();
	expire_date.setDate(expire_date.getDate()+365);
	var expires = "; expires="+expire_date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function spss_tag_click_source(path, click_source) {
createTempCookie("click_source" + path, click_source);
}

function spss_tag_search(path, search_keywords, search_rank) {
createTempCookie("search_keywords" + path, search_keywords);
createTempCookie("search_rank" + path, search_rank);
}
