/* GLOBAL PDF GENERATOR v2.0
 *
 * main.js - File including script for class.pdf_form_HTML.inc, an extension class for fpdf.php
 * ____________________________________________________________
 *
 * To run this class you need to install FPDF.
 * This version of Global PDF Generator is tested with FPDF v1.53
 * Get a copy from http://www.fpdf.de
 *
 * Developed by Dennis Ritz <ritz@global-cube.de>
 * Copyright (c) 2005-2006 Global-Cube Business Solutions, http://www.global-cube.de/
 * All rights reserved.
 *
 * You are not allowed to modify, sell, rent or lend this software
 */

/* Global PDF Generator/globalPDFgenerator.js, v2.0 2006/01/15 23:06:13 */

var pdf;


//Stammfunktion: Diese Funktion stößt die PDF generation an
function pdf_erzeugen(printElement)
{
        if(!document.all)
        {
        	alert("Dieses Version von 'Global PDF Generator' ist leider nur unter IE ab Version 4.0 lauffähig");
            return;
        }
        if(!document.all[printElement])
        {
        	//Wird ausgegeben wenn das Objekt von dem ein PDF erzeugt werden soll nicht existiert
            alert("Von dieser Ansicht kann kein PDF erzeugt werden.");
        	return;
        }
        //pdf ist ein Array
        //im index objekte werden hirachisch (HTML Hirachie) alle HTML Nodes gespeichert
        pdf = new Array();
        pdf['objects'] = new Array;
        //Set standard BGCOLOR
        var aktBgColor = new Array(255,255,255);
        //PDF Fenster aufrufen f&uuml;r Formular
        //pdf_generator.php ist nur eine temporäre Seite, die angezeigt wird, solange diese Seite braucht um die Datenstruktur aufzubauen
        PDFwin = window.open("pdf/pdf_generator.php","PDFwin","width=1000,height=700,left=10,top=10,location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes");
        //PDF Objekte sammeln

		pdf_childNodes(document.all[printElement], 0, 0, aktBgColor);
		//PDF Objekte an PDF Fenster (in Form eines Formulars) übergeben und Formular abschicken
        pdf_aufrufen();
}


//Funktion zum zugreifen auf die Kinderelemente des akt Objekt
function pdf_childNodes(obj, p_left, p_top, p_aktBgColor)
{
	var innerTop = 0;

    for(var k=0; k<obj.childNodes.length; k++)
    {
    	if(obj.childNodes[k].style)
        	if(obj.childNodes[k].style.display == 'none')
            	continue;

        var left;
        var top;

        switch(obj.childNodes[k].tagName)
    	{
        	case "TABLE":	//pdf_childNode_check(obj.childNodes[k]);
        					left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;
                            if(obj.childNodes[k].getAttribute('tablecolor'))
                            {
                            	aktBgColor = obj.childNodes[k].getAttribute('tablecolor');
                                aktBgColor = aktBgColor.split(',');
                            }else{
                            	aktBgColor = p_aktBgColor;
                            }

                    		pdf_childNodes(obj.childNodes[k], left, top, aktBgColor);
                    		break;

            case "TBODY":	//pdf_childNode_check(obj.childNodes[k]);
        					left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;
                    		pdf_childNodes(obj.childNodes[k], left, p_top, p_aktBgColor);
                    		break;

            case "DIV":		//pdf_childNode_check(obj.childNodes[k]);
        					left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;
                    		pdf_childNodes(obj.childNodes[k], p_left, p_top, p_aktBgColor);
                    		break;

            case "TR":      //pdf_childNode_check(obj.childNodes[k]);
        					left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;

                            if(obj.childNodes[k].getAttribute('rowcolor'))
                            {
                            	aktBgColor = obj.childNodes[k].getAttribute('rowcolor');
                                aktBgColor = aktBgColor.split(',');
                            }else{
                            	aktBgColor = p_aktBgColor;
                            }

                    		pdf_childNodes(obj.childNodes[k], left, top, aktBgColor);

							break;

            case "TD":      left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;
                            if(obj.childNodes[k].getAttribute('cellcolor'))
                            {
                            	aktBgColor = obj.childNodes[k].getAttribute('cellcolor');
                                aktBgColor = aktBgColor.split(',');
                            }else{
                            	aktBgColor = p_aktBgColor;
                            }
            				pdf_childNode_check(obj.childNodes[k], left, p_top, aktBgColor);
                    		pdf_childNodes(obj.childNodes[k], left, top, aktBgColor);
                            break;

            case "TH":      left = obj.childNodes[k].offsetLeft + p_left;
        					top = obj.childNodes[k].offsetTop + p_top;
                            if(obj.childNodes[k].getAttribute('cellcolor'))
                            {
                            	aktBgColor = obj.childNodes[k].getAttribute('cellcolor');
                                aktBgColor = aktBgColor.split(',');
                            }else{
                            	aktBgColor = p_aktBgColor;
                            }
            				pdf_childNode_check(obj.childNodes[k], left, p_top, aktBgColor);
                 			pdf_childNodes(obj.childNodes[k], left, top, aktBgColor);
                    		break;
        }
    }
}


function getObjectProps(obj)
{
	var ret = new Array();

	//get TEXT / Value of obj
    ret['TEXT'] = obj.innerHTML;

	ret['TEXT'] = ret['TEXT'].replace(/\n/g, "");
	ret['TEXT'] = ret['TEXT'].replace(/\t/g, "");
	ret['TEXT'] = ret['TEXT'].replace(/\r/g, "");
	ret['TEXT'] = ret['TEXT'].replace(/\v/g, "");

	ret['TEXT'] = ret['TEXT'].replace(ret['TEXT'].match(/<TABLE.*\/TABLE>/ig), "");
	ret['TEXT'] = ret['TEXT'].replace(ret['TEXT'].match(/<DIV.*\/DIV>/ig), "");
    ret['TEXT'] = ret['TEXT'].replace(ret['TEXT'].match(/<INPUT.*>/ig), "");
    ret['TEXT'] = ret['TEXT'].replace(ret['TEXT'].match(/<IMG.*>/ig), "");
    ret['TEXT'] = ret['TEXT'].replace(ret['TEXT'].match(/<A.*>/ig), "");

	//get WIDTH of obj
    ret['WIDTH'] = obj.offsetWidth;

    //get HEIGHT of obj
    ret['HEIGHT'] = obj.offsetHeight;

    for(var i=0; i < obj.childNodes.length; i++)
    {
    	if(obj.childNodes[i].tagName == "INPUT")
        	if(obj.childNodes[i].type == "text")
            	ret['TEXT'] += obj.childNodes[i].value;
    }

    //Align of TEXT
    //if it is a nummber (including '-,.') -> 'R'
    //else -> 'L'
    if(ret['TEXT'].match(/^-?\d*\.?\d*\.?\d+,?\d*$/))
    	ret['ALIGN'] = 'R';
    else
    	ret['ALIGN'] = 'L';

	return ret;
}


function pdf_childNode_check(obj, p_left, p_top, p_aktBgColor)
{

	switch(obj.tagName)
    {
    	case "TD":
                        var objProps = getObjectProps(obj);
                        pdf['objects'][pdf['objects'].length] = new Array();
                		pdf['objects'][pdf['objects'].length-1]['TEXT'] = objProps['TEXT'];
                        pdf['objects'][pdf['objects'].length-1]['TOP']	= p_top;
                        pdf['objects'][pdf['objects'].length-1]['LEFT'] = p_left;
                        pdf['objects'][pdf['objects'].length-1]['WIDTH'] = objProps['WIDTH'];
                        if(objProps['TEXT'].lenght > (objProps['WIDTH'] / 2000))
                        	objProps['HEIGHT'] = (objProps['HEIGHT'] / 2);
                        pdf['objects'][pdf['objects'].length-1]['HEIGHT'] = objProps['HEIGHT'] / 2;
                        pdf['objects'][pdf['objects'].length-1]['BGCOLOR'] = p_aktBgColor;
                       	pdf['objects'][pdf['objects'].length-1]['ALIGN'] = objProps['ALIGN'];
                        break;

        case "TH":
                        var objProps = getObjectProps(obj);
        				pdf['objects'][pdf['objects'].length] = new Array();
                		pdf['objects'][pdf['objects'].length-1]['TEXT'] = objProps['TEXT'];
                        pdf['objects'][pdf['objects'].length-1]['TOP']	= p_top;
                        pdf['objects'][pdf['objects'].length-1]['LEFT'] = p_left;
                        pdf['objects'][pdf['objects'].length-1]['WIDTH'] = objProps['WIDTH'];
                        pdf['objects'][pdf['objects'].length-1]['HEIGHT'] = objProps['HEIGHT'];
                        pdf['objects'][pdf['objects'].length-1]['BGCOLOR'] = p_aktBgColor;
                        pdf['objects'][pdf['objects'].length-1]['ALIGN'] = 'C';
                        break;
    }
}


function pdf_aufrufen()
{
        while(!PDFwin.document.all('pdf_form')){

        }
        var form='';
        for(var u in pdf){
                for(var l in pdf[u]){
                        m = parseInt(l) +1;
                        for(var t in pdf[u][l]){
                                if(pdf[u][l][t][0]){
                                        for(var f in pdf[u][l][t]){
                                                //PDFwin.document.all('pdf_form').innerHTML += "<input type='text' name='pdf["+u+"]["+m+"]["+t+"]["+f+"]' value='"+pdf[u][l][t][f]+"'></input>";
                                                form += "<input type='text' name='pdf["+u+"]["+m+"]["+t+"]["+f+"]' value='"+pdf[u][l][t][f]+"'></input>";
                                        }
                                }
                                else
                                {
                                      //PDFwin.document.all('pdf_form').innerHTML += "<input type='text' name='pdf["+u+"]["+m+"]["+t+"]' value='"+pdf[u][l][t]+"'></input>";
                                      form += "<input type='text' name='pdf["+u+"]["+m+"]["+t+"]' value='"+pdf[u][l][t]+"'></input>";
                                }
                        }
                }
        }

        if(PDFwin.document.all('pdf_form'))
        {
        	PDFwin.document.all('pdf_form').innerHTML = form;
        	PDFwin.document.all('pdf_form').submit();
        }
}

// End of script for class.pdf_form_HTML.inc