﻿$(document).ready(function() {
	// add prospectID and tspID to all links (ie. buttons, menuitems etc.)
	try {
		var query = GetQuerystring(window.location.search);
		var prospectid = query["prospectID"];
		var tspID = query["tspID"];

		$('a').each(function() {
			var href = $(this).attr("href");
			// skip javascript and page anker tags 
			if (href.indexOf("javascript:") == -1 && href.substring(0, 1) != "#") {
				// rewrite only internal links
				if (href.substring(0, 1) == "/") {
					var parameters = GetQuerystring(href);
					// skip adding prospectid param on wizard stap 7; "bevestiging"
					if (window.location.href.indexOf("bevestiging") == -1) {
						if (parameters["prospectID"] == null && prospectid != null && href.indexOf("prospectID") == -1) {
							// add '?' if applicable
							if (href.indexOf("?") == -1) { href += "?"; }
							// add '&' if applicable
							if (href.substring(href.length - 1, href.length) != "?" && href.substring(href.length - 1, href.length) != "&") { href += "&"; }
							href += "prospectID=" + prospectid;
						}
					}
					if (parameters["tspID"] == null && tspID != null && href.indexOf("tspID") == -1) {
						// add '?' if applicable
						if (href.indexOf("?") == -1) { href += "?"; }
						// add '&' if applicable
						if (href.substring(href.length - 1, href.length) != "?" && href.substring(href.length - 1, href.length) != "&") { href += "&"; }
						href += "tspID=" + tspID;
					}

					$(this).attr("href", href);
				}
			}
		})

	} catch (err) { }

});

function GetQuerystring(querystring) {
	var collection = {};
	// Empty if no query string
	if (!querystring) { return { toString: function() { return ""; } }; }
	// Decode query string and remove '?'
	querystring = decodeURI(querystring.substring(1));
	// Load the key/values of the return collection
	var pairs = querystring.split("&");

	for (var i = 0; i < pairs.length; i++) {
		// Empty pair (e.g. ?key=val&&key2=val2)
		if (!pairs[i]) { continue; }
		// Don't use split("=") in case value has "=" in it
		var seperatorPosition = pairs[i].indexOf("=");

		if (seperatorPosition == -1) { collection[pairs[i]] = ""; }
		else { collection[pairs[i].substring(0, seperatorPosition)] = pairs[i].substr(seperatorPosition + 1); }
	}

	// toString() returns the key/value pairs concatenated
	collection.toString = function() { return "?" + querystring; };

	return collection;
}
