// Check the query string of the URL; if it contains the "campaign_id" value
// then insert a new script to track the campaign impressions and visits
// Call this script by adding
//   <script type="text/javascript" src="http://www.pimpmansion.com/code/fhg.js"></script>
// just before the </head> of each page in each FHG

// Embed everything in a try/catch block to avoid displaying error messages 
try {
	// Embed everything in an anonymous function (and call it) to avoid
	// polluting the global context with our temporary variables
	(function () {
		// call this script
		var u = 'http://www.pimpmansion.com/fhg_js.php';
// Remove these three lines on the production server
		// with these parameters:
		var qc = 'campaign_id=';				
		var qk = 'keyword=';					// 
		var gclid = 'gclid=';
		var r = 'r=' + Math.random();			// some randomness to avoid caching
	
		var s = document.location.search;
		if (s.substring(0, 1) == '?') {
			s = s.substring(1);
		}
		var a = s.split('&');	// split query string to segments
		var o = [];						// filter the segments to pass along
		var ci = false;					// is the campaign_id set?
		for (var i in a) {
			// check campaign Id
			if (a[i].substring(0, qc.length) == qc) {
				o[o.length] = a[i];
				ci = true;				// campaign_id is set
			} else
			// check keywords
			if (a[i].substring(0, qk.length) == qk && a[i] != qk) {
				o[o.length] = a[i];
			} else
			// check gclid
			if (a[i].substring(0, gclid.length) == gclid && a[i] != gclid) {
				o[o.length] = a[i];
			}
		}
		// If the campaign Id is set then invoke the script which counts the impression and rewrites the links
		if (ci) {
			o[o.length] = r;
			// use unescape() to avoid using <script> or </script> in the code because it can confuse some browsers
			// DO NOT unescape() the result of o.join() because those values where not escape()-ed before and
			// unescape()-ing them can break them
			document.write(unescape('%3Cscript src="'+u+'?')+o.join('&')+unescape('" type="text/javascript"%3E%3C/script%3E'));
		}
	})();
} catch (err) {}		// swallow all the errors (hopefully none happens)
// This is the end of the script

