var logPath = "http://log.campaigns.nl/wta/";
var logIndex = 0;
var logPixel = new Array(10);

function getParam(paramName){
	var query, parameters, parameter;
	query = unescape(window.location.search.substring(1));
	parameters = query.split("&");
	for (var i = 0; i < parameters.length; i++){
		parameter = parameters[i].split("=");
		if(parameter[0].toUpperCase() == paramName.toUpperCase()) {
			return parameter[1];
		}
	}
	return "";
}

function getParamFrame(paramName){
	var query, parameters, parameter;
	query = unescape(top.document.location.search.substring(1));
	parameters = query.split("&");
	for (var i = 0; i < parameters.length; i++){
		parameter = parameters[i].split("=");
		if(parameter[0].toUpperCase() == paramName.toUpperCase()) {
			return parameter[1];
		}
	}
	return "";
}

function getCookie(cookieName) {
	var allCookies = document.cookie;
	var pos = allCookies.indexOf(cookieName);
	if (pos != -1) {
		var start = pos + cookieName.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		return allCookies.substring(start, end);
	} else {
		return "";
	}
}

function request(requestName) {
	var value = getParam(requestName);
	if (value == "") value = getParamFrame(requestName);
	if (value == "") value = getCookie(requestName);
	return value;
}

function getID() {
	var now = new Date();
	return now.getTime();
}

function getSessionID() {
	var sessionID = request("session_id");
	if (sessionID == "") sessionID = getID();
	document.cookie = "session_id=" + sessionID;
	return sessionID;
}

function getUserID() {
	var userID = request("user_id");
	if (userID == "") userID = getID();
	var expire = new Date();
	expire.setDate(expire.getDate() + 28); // 4 weeks
	document.cookie = "user_id=" + userID + "; expires=" + expire.toGMTString();
	return userID;
}

function WebLog(WebsiteID, Section, Target, Subtarget, TypeID, UserID, SessionID) {
	var RequestString;
	var Location = document.location.toString();
	var Referrer = document.referrer.toString();
	var Pos;

	Pos = Location.indexOf("?");
	if (Pos > -1) Location = Location.substring(0, Pos);

	Pos = Referrer.indexOf("?");
	if (Pos > -1) Referrer = Referrer.substring(0, Pos);

	RequestString = "?site_id=" + WebsiteID + "&";
	RequestString += "section=" + Section + "&";
	RequestString += "target=" + Target + "&";
	RequestString += "Subtarget=" + Subtarget + "&";
	RequestString += "type_id=" + TypeID + "&";
	RequestString += "user_id="+ UserID + "&";
	RequestString += "session_id=" + SessionID +"&";
	RequestString += "referrer="+ Referrer + "&";
	RequestString += "location=" + Location + "&";
	RequestString += "unique_code=" + Math.random();

	logPixel[logIndex] = new Image();
	logPixel[logIndex].src = logPath + 'log.asp' + RequestString;
	logIndex += 1;
	return true;
}

function WebLink(ContentID, UserID, SessionID, URL) {
	WebLoad(ContentID, UserID, SessionID);
	document.location = URL;
}

function WebLinkProp(ContentID, UserID, SessionID, URL) {
	WebLoad(ContentID, UserID, SessionID);
	document.location = URL + '?user_id=' + UserID + '&session_id=' + SessionID;
}

// Can be excluded when using language specification

function WebLoad(ContentID, UserID, SessionID) {
	var RequestString;
	var Location = document.location.toString();
	var Referrer = document.referrer.toString();
	var Pos;

	Pos = Location.indexOf("?");
	if (Pos > -1) Location = Location.substring(0, Pos);

	Pos = Referrer.indexOf("?");
	if (Pos > -1) Referrer = Referrer.substring(0, Pos);

	RequestString = "?c=" + ContentID + "&";
	RequestString += "u="+ UserID + "&";
	RequestString += "s=" + SessionID +"&";
	RequestString += "ref="+ Referrer + "&";
	RequestString += "loc=" + Location + "&";
	RequestString += "unique_code=" + Math.random();

	logPixel[logIndex] = new Image();
	logPixel[logIndex].src = logPath + 'load.asp' + RequestString;
	logIndex += 1;
	}

// Can be excluded when not using language specification

function WebLoadLan(ContentID, UserID, SessionID, LanguageID) {
	var RequestString;
	var Location = document.location.toString();
	var Referrer = document.referrer.toString();
	var Pos;

	Pos = Location.indexOf("?");
	if (Pos > -1) Location = Location.substring(0, Pos);

	Pos = Referrer.indexOf("?");
	if (Pos > -1) Referrer = Referrer.substring(0, Pos);

	RequestString = "?c=" + ContentID + "&";
	RequestString += "u="+ UserID + "&";
	RequestString += "s=" + SessionID +"&";
	RequestString += "ref="+ Referrer + "&";
	RequestString += "loc=" + Location + "&";
	RequestString += "lan=" + LanguageID + "&";
	RequestString += "unique_code=" + Math.random();

	logPixel[logIndex] = new Image();
	logPixel[logIndex].src = logPath + 'load.asp' + RequestString;
	logIndex += 1;
	return true;
}

