
	// charset of posting subframe and the form need to be utf-8 for IE not to screw up characters

	var pageTop = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"><head><meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /><link rel="stylesheet" href="css.css" type="text/css" /><link rel="stylesheet" href="pdf.css" type="text/css" /></head><body><div id="pdfBlock" class="block printMailPdf">';
	var pageBottom = '</div></body></html>';


	// standalone getElementsByClassName function from http://www.robertnyman.com/index.php?p=256
	function getElementsByClassName(className, tag, elm){
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}
		return returnElements;
	}


	// standalone onDomReady function from http://www.ajaxprogrammer.com/
	// only create when mootools not available
	if (!window.onDomReady)
	{
		window.onDomReady = function(fn){
			window.__ondom_functionArray.push(fn);
		};
		(function(){
			window.__ondom_functionArray = [];
			function _runFunctions(){
				for (var i in window.__ondom_functionArray){
					(window.__ondom_functionArray[i])();
				}
			};
			var _khtml = /(WebKit|khtml)/i.test(navigator.userAgent);
			if(document.addEventListener && !_khtml){
				document.addEventListener("DOMContentLoaded", _runFunctions, false);
			}else if(_khtml){
				var _timer = setInterval(function(){
					if(/loaded|complete/.test(document.readyState)){
						clearInterval(_timer);
						_runFunctions();
					}
				}, 10);
			}else{
				document.write("<script id=__ie_ondom defer src=javascript:void(0)><\/script>");
				var script = document.getElementById("__ie_ondom");
				script.onreadystatechange = function(){
					if(this.readyState == "complete"){
						_runFunctions();
					}
				};
			}
		})();
	}


	// creating a hidden iFrame to use for the button functions
//	document.write('<iframe style="z-index: 9999; top: 1px; left: 1px; position: absolute;" id="subframe" name="subframe"></iframe>');
	document.write('<iframe style="width: 0px; height: 0px; top: -999px; left: -999px; position: absolute;" id="subframe" name="subframe"></iframe>');


	// on dom ready
	onDomReady(function(){

		var buttonReageer, subframe = frames['subframe'];
		document.getElementById('buttonPDF').onclick = initPdf;
		document.getElementById('buttonPrint').onclick = initPrint;
		document.getElementById('buttonEmail').onclick = initMail;
		if (buttonReageer = document.getElementById('buttonReageer'))
		{
			buttonReageer.onclick = initReageer;
		}
	});


	// init pdf
	function initPdf()
	{
		var pdfHTML, printMailPdfBlock = getElementsByClassName('printMailPdf')[0], resultBlocks, detailImage;

		if ((resultBlocks = getElementsByClassName('occasionResult', 'a', printMailPdfBlock)).length > 0)
		{
			var spans;

			pdfHTML = document.getElementById('resultsInfo').innerHTML;

			// for each result, assemblding a table with the content of the spans, thumb first, then data02 + data01
			// the thumb and data02 are made clickable because the wrapping link gets discarted
/*
			pdfHTML += '<table cellpadding="0" cellspacing="-1" class="occasionResultTable">';
			for (i = 0; resultBlock = resultBlocks[i]; i++)
			{
				pdfHTML += '<tr>';
				pdfHTML += '<td class="thumbContainer"><a href="' + resultBlock.href + '">' + getElementsByClassName('thumbContainer', 'span', resultBlock)[0].innerHTML + '</a></td>';
				pdfHTML += '<td class="dataContainer02"><a href="' + resultBlock.href + '">' + getElementsByClassName('dataContainer02', 'span', resultBlock)[0].innerHTML + '</a></td>';
				pdfHTML += '<td class="dataContainer01">' + getElementsByClassName('dataContainer01', 'span', resultBlock)[0].innerHTML + '</td>';
				pdfHTML += '</tr>';
				pdfHTML += '<tr><td><br /></td><td></td><td></td></tr>';
			}
			pdfHTML += '</table>';
*/
/*
			for (i = 0; resultBlock = resultBlocks[i]; i++)
			{
				pdfHTML += '<a href="' + resultBlock.href + '">' + getElementsByClassName('thumbContainer', 'span', resultBlock)[0].innerHTML + '</a><br />';
				pdfHTML += '<a href="' + resultBlock.href + '">' + getElementsByClassName('dataContainer02', 'span', resultBlock)[0].innerHTML + '</a>';
				pdfHTML += getElementsByClassName('dataContainer01', 'span', resultBlock)[0].innerHTML + '<br /><br />';
			}
*/
			for (i = 0; resultBlock = resultBlocks[i]; i++)
			{
				pdfHTML += getElementsByClassName('thumbContainer', 'span', resultBlock)[0].innerHTML + '<br />';
				pdfHTML += getElementsByClassName('dataContainer02', 'span', resultBlock)[0].innerHTML;
				pdfHTML += getElementsByClassName('dataContainer01', 'span', resultBlock)[0].innerHTML + '<br /><br />';
			}
		}
		else if (detailImage = document.getElementById('detailImage'))
		{
			var thumbs = getElementsByClassName('detailThumbs', 'div', detailImage.parentNode)[0].getElementsByTagName('img'), thumb;

			pdfHTML = '<table cellspacing="0" cellpadding="0" id="detailImagesTable"><tr>';
			pdfHTML += '<td id="detailImage">' + detailImage.innerHTML + '</td>';
			pdfHTML += '<td class="detailThumbs"><div>' + getElementsByClassName('detailThumbs', 'div', detailImage.parentNode)[0].innerHTML.replace(/(<img.*?><img.*?>)/ig, '$1<br />') + '</div></td>';
			pdfHTML += '</tr></table>';
			pdfHTML += '<table cellspacing="0" cellpadding="0" id="detailImagesTable"><tr>';
			pdfHTML += '<td id="detailsContainer01">' + document.getElementById('detailsContainer01').innerHTML + '</td>';
			pdfHTML += '<td id="detailsContainer02">' + document.getElementById('detailsContainer02').innerHTML + '</td>';
			pdfHTML += '</tr></table>';
//alert(pdfHTML);
		}
		else
		{
			pdfHTML = printMailPdfBlock.innerHTML;
		}
		// remove the print/mail/PDF buttons
		pdfHTML = stripButtons(pdfHTML);
		// unescape URLs - With escaped URLs domPdf refused to work...
		pdfHTML = unescape(pdfHTML);
		// remove newlines and tabs
		pdfHTML = pdfHTML.replace(new RegExp('(' + String.fromCharCode(13, 10) + '|' + String.fromCharCode(10) + '|\t)', 'g'), '');
		// remove inline CSS overflow - CSS override didn't work
		pdfHTML = pdfHTML.replace(/overflow:\s?[^;"'\s]*/ig, '');
		// make image URLs relative. Wessel doesn't allow remote files fetching.
		var regex = new RegExp('(src="?)' + document.location.href.replace(/(^.*?\:\/\/[^\/]*\/?).*/, '$1').replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1"), 'g');
		pdfHTML = pdfHTML.replace(regex, '$1');
		// and image paths starting with '/'
		pdfHTML = pdfHTML.replace(/(src="?)\//ig, '$1');
		// rip all forms
		pdfHTML = pdfHTML.replace(/<form[^>]*>.*?<\/form>/igm, '');
		// make any relative link absolute
		pdfHTML = pdfHTML.replace(/(href=["|'])(?!(javascript|view-source|about|http|https|file|ftp|irc|mailto|news|gopher):)/ig, '$1' + document.location.href.replace(/[^\/]*$/, ''));

		pdfHTML = pageTop + pdfHTML + pageBottom;

		subframe.document.open();
		subframe.document.write('<html><body><form action="htmlToPdfAdaptor.php" method="post" id="pdfForm" accept-charset="utf-8"><textarea name="content">' + pdfHTML + '</textarea></form></body></html>');
		subframe.document.close();
		subframe.document.getElementById("pdfForm").submit();
	}


	// init print
	function initPrint()
	{
		window.print();
	}


	// init pdf
	function initMail()
	{
		if (document.getElementById('mailForm'))
		{
			document.getElementById('mailForm').style.display = 'inline';
		}
		else
		{
			// creating input layer
			var input = document.createElement('div');
			input.style.cssText = 'float: right; position: relative;';
			input.innerHTML = '<div id="mailForm" style="position: absolute; left: -284px; width: 281px; padding-left: 3px; height: 18px; padding-top: 3px; overflow: hidden; background: white;">'
										+ '<form action="scripts/sandraAdaptor/sandra.php" target="subframe" method="post" onsubmit="this.parentNode.style.display = \'none\'; if(this.receiver.value == \'\'){return false;}">'
										+ '<input type="text" name="receiver" style="width: 250px; float: left; font-size: 10px; font-family: arial;" value="' + document.getElementById('emailInstruction').innerHTML + '" onclick="if(this.value == this.defaultValue){this.value = \'\';}" onblur="if(this.value == \'\'){this.value = this.defaultValue;}" />'
										+ '<input type="image" src="images/buttonSmallEmailGo.gif" style="border: 0px; float: left; padding: 0px;" />'
// niet meer de pagina zelf, maar URL met begeleidend tekstje
//										+ '<textarea name="content" style="display: none;">' + pageTop + stripButtons(getElementsByClassName('printMailPdf')[0].innerHTML) + pageBottom + '</textarea>'
										+ '<textarea name="content" style="display: none;">' + document.getElementById('emailBody').innerHTML + '<br /><br /><a href="' + document.location.href.replace(/-language=..$/, '') + '-language=' + language + '">' + document.title + '</a></textarea>'
										+ '</form>';
										+ '</div>';
			this.parentNode.insertBefore(input, document.getElementById('buttonPDF'));
		}
	}


	// init reageer
	function initReageer()
	{
		document.location = 'contact.php'
			+ '?referentie=' + document.getElementById('detailReferentie').innerHTML
			+ '&merk=' + document.getElementById('detailMerk').innerHTML
			+ '&type=' + document.getElementById('detailType').innerHTML;
	}


	// strip pdf, print and email buttons from HTML
	function stripButtons(html)
	{
		html = html.replace(/<[^>]*id="?buttonPDF"?[^>]*>/, '');
		html = html.replace(/<[^>]*id="?buttonPrint"?[^>]*>/, '');
		html = html.replace(/<[^>]*id="?buttonEmail"?[^>]*>/, '');
		html = html.replace(/<[^>]*id="?emailInstruction"?[^>]*>.*?<\/div>/, '');
		html = html.replace(/<[^>]*id="?emailBody"?[^>]*>.*?<\/div>/, '');
		return html;
	}


