
function getDomain(url) {
	var parts = url.split(/\/+/g);
	return parts[0]+"//"+parts[1];
}


function IFrame(parentElement)
{
	var iframe = document.createElement("iframe");
	iframe.src = "about:blank";
	
	if(parentElement == null) parentElement = document.body;
	parentElement.appendChild(iframe);
	
	iframe.doc = null;
	
	if(iframe.contentDocument)
	  // Firefox, Opera
	  iframe.doc = iframe.contentDocument;
	else if(iframe.contentWindow)
	  // Internet Explorer
	  iframe.doc = iframe.contentWindow.document;
	else if(iframe.document)
	  // Others?
	  iframe.doc = iframe.document;
	
	if(iframe.doc == null)
	  throw "Document not found, append the parent element to the DOM before creating the IFrame";
	
	iframe.doc.open();
	iframe.doc.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body></body></html>');
	iframe.doc.close();
	
	return iframe;
}

	
function printPdf(domId)
{
	//Evil Hack
	if (document.URL.indexOf("manufakturenliste.html") > -1) {
		window.open("/pdf_new/demo/html2ps.php?process_mode=single&URL=http%3A%2F%2Fwww.zmh.com%2Findex.php%3FeID%3DgetMF_pdfDruck%26house_name%3DManufakturen&renderimages=1&renderlinks=1&topmargin=10&leftmargin=20&&smartpagebreak=true");
		return;
	}
	
	var html = '';
	var frameId = '';
	
	if (document.title.indexOf("Startseite") > -1) {
		var content = $('maincontent');
	}
	else {
		var content_mitte = $('content_mitte');
		var content_rechts = $('content_rechts');
		if (content_rechts) {
			var content = document.createElement('div');
			content.style.width = '715px';
			content.appendChild(content_mitte.cloneNode(true));
			content.appendChild(content_rechts.cloneNode(true));
		}
		else {
			var content = content_mitte;
		}
	}
	
	if (!content) {
		throw "Content element not found";
		return;
	}
	var links = document.getElementsByTagName('link');
	var scripts = document.getElementsByTagName('script');
	
	//Write the content
	html += "<html>";
	html += "<head>";
	html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	for (var i=0; i<links.length; i++) {
		html += "<link href='"+links[i].href+"' type='"+links[i].type+"' rel='"+links[i].rel+"' />";
	}
	html += "</head>";
	html += "<body>";
	html += "<div id='maincontent'>";
	html += "<div id='"+content.id+"' class='"+content.className+"'>";
	html += content.innerHTML;
	html += "</div>";
	html += "</div>";
	
	html += "</body>";
	html += "</html>";

	var frame = new IFrame();
	frame.style.display = "none";
	var fdoc=frame.doc;

	var htmlInput = fdoc.createElement("input");
	htmlInput.type = "hidden";
	htmlInput.name = "html";
	htmlInput.value = html;
	
	var form = fdoc.createElement("form");
	form.method = "post";
	form.action = getDomain(document.URL)+"/print.php";
	form.appendChild(htmlInput);
	form.target = "_blank";
	
	if (document.title != '') {
		var filename = fdoc.createElement("input");
		filename.type = "hidden";
		filename.name = "filename";
		filename.value = document.title;
		form.appendChild(filename);
	}

	frame.doc.body.appendChild(form);

	//submit form
	form.submit();
}
