/**
    megnyit egy uj ablakot, majd az adott oldal tartalmát átadja neki
*/

var windowSrc="http://www.mnv.hu/print/printsample/printsample";
var windowTitle="Print";
var printTagId=null;	//	ha ebbe a változoba van valami, akkor ennek a tartalmát teszi át a print képernyore
addLoadEvent(printInit);

function printInit()
{
    var btn=document.getElementById("print_btn");
    if(document.getElementById("ContentFunction"))
	btn.style.display="none";
    else
	btn.style.display="inline";
    btn.onclick=openPrintWindow;
}

function openPrintWindow()
{
//alert('a');
	if(window.location.href.indexOf('www.mnvzrt.hu/en/') > -1) {
windowSrc="http://www.mnvzrt.hu/en/print/printsample/printsample";
} else if(window.location.href.indexOf('new.mnv.hu/en/') > -1) {
windowSrc="http://new.mnv.hu/en/print/printsample/printsample";
} else if(window.location.href.indexOf('www.mnvzrt.hu/') > -1) {
windowSrc="http://www.mnvzrt.hu/print/printsample/printsample";
} else if(window.location.href.indexOf('mnvzrt.hu/') > -1) {
windowSrc="http://mnvzrt.hu/print/printsample/printsample";
}

var printW=window.open(windowSrc,'forward', 'width=590,height=550,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=0');

    if(!isIE)
    {
	printW.onload=windowOnload;
    }
    else
    {
	var loaded=false;
	while(!loaded)
	{
	    if(printW.document.getElementById("portal"))
	    {
		windowOnload();
		loaded=true;
	    }
	}
    }
/**
    No comment...:)
*/
    function windowOnload()
    {
	if(document.getElementById('hiddenfullpagecontent') != null)
	{
		if(printW.document.getElementById("tab_two")) {
			printW.document.getElementById("tab_two").innerHTML='';
		}
		
		var pagecontentDIV = document.getElementById('hiddenfullpagecontent');
		
		for(var j=0; j < pagecontentDIV.childNodes.length; j++) {
			if(pagecontentDIV.childNodes[j].nodeType == 3) {
				var txt = pagecontentDIV.childNodes[j].nodeValue;
				var pElement = document.createElement('P');
				pElement.appendChild(document.createTextNode(txt));
				pagecontentDIV.replaceChild(pElement, pagecontentDIV.childNodes[j]);
			}
		}
		
		while(pagecontentDIV.getElementsByTagName('pagebreak').length > 0) {
			var pagebreaks = pagecontentDIV.getElementsByTagName('pagebreak');
			for(var i=0; i < pagebreaks.length; i++) {
				var txt = pagebreaks[i].innerHTML;
				var pElement = document.createElement('P');
				var bElement = document.createElement('B');
				bElement.appendChild(document.createTextNode(txt));
				pElement.appendChild(bElement);
				pagebreaks[i].parentNode.replaceChild(pElement, pagebreaks[i]);
			}
		}
		var new_text = pagecontentDIV.innerHTML;
		
		if(isIE) {
			//alert(printW.document.getElementById("tab_one"));
			printW.document.getElementById("tab_one").firstChild.firstChild.firstChild.firstChild.innerHTML=new_text;
		} else {
			printW.document.getElementById("tab_one").firstChild.nextSibling.firstChild.nextSibling.firstChild.firstChild.nextSibling.innerHTML=new_text;
		}
		
		if(printW.document.getElementById('ForwardPrint')) {
			printW.document.getElementById('ForwardPrint').innerHTML = '';
		}
	}
	else
	{
		var divElements = document.getElementsByTagName("DIV");
		
		if(printW.document.getElementById("tab_two")) {
			printW.document.getElementById("tab_two").innerHTML='';
		}
		
		var new_text = '';
		
		for(i=0; i < divElements.length; i++) {
			if(divElements[i].className == 'c_div') {
				new_text += '<div class="c_div">' + divElements[i].innerHTML + '</div>';
			}
		}
		
		if(isIE) {
			//alert(printW.document.getElementById("tab_one"));
			printW.document.getElementById("tab_one").firstChild.firstChild.firstChild.firstChild.innerHTML=new_text;
		} else {
			printW.document.getElementById("tab_one").firstChild.nextSibling.firstChild.nextSibling.firstChild.firstChild.nextSibling.innerHTML=new_text;
		}
		
		if(printW.document.getElementById('ForwardPrint')) {
			printW.document.getElementById('ForwardPrint').innerHTML = '';
		}
	}
	disableFunctions(printW.document);
    }
}
/**
    Kiszedi a tartalomban levo linkeket, selecteket és dealtivalja az osszes gombot
*/
function disableFunctions(doc)
{
    links=doc.getElementsByTagName("A");
    for(l=0; l<links.length; l++)
    {
	links[l].onclick=function(){return false;}
    }
    
    opt=doc.getElementsByTagName("SELECT");
    for(s=0; s<opt.length; s++)
    {
	opt[s].disabled=true;
	opt[s].parentNode.parentNode.style.display="none";
    }
    var tbls=doc.getElementsByTagName("TH");

    for(t=0; t<tbls.length; t++)
    {
	if(isIE)tbls[t].style.setAttribute("cssText","");
	else tbls[t].setAttribute("STYLE","");
    }
    
    var tbls=doc.getElementsByTagName("TD");

    for(t=0; t<tbls.length; t++)
    {
	if(isIE)tbls[t].style.setAttribute("cssText","");
	else tbls[t].setAttribute("STYLE","");
    }
}

/**
Szoveg formazas XSL-el
*/

function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
      // code for Mozilla, Firefox, Opera, etc.
else if (document.implementation 
  && document.implementation.createDocument)
{
    xmlDoc=document.implementation.createDocument("","",null);
}
else
{
    alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
		  
function displayResult(nodeID)
{
    xml=loadXMLDoc("data.xml");
    xsl=loadXMLDoc("print.xsl");
    // code for IE
    if (window.ActiveXObject)
    {
	ex=xml.transformNode(xsl);
	document.getElementById("example").innerHTML=ex;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation 
	  && document.implementation.createDocument)
    {
	xsltProcessor=new XSLTProcessor();
	xsltProcessor.importStylesheet(xsl);
	resultDocument = xsltProcessor.transformToFragment(xml,document);
	document.getElementById(nodeID).appendChild(resultDocument);
    }
}
