<script type="text/javascript" src="includes/js/ajax.js"></script>
<link href="includes/js/fancytree/skin-win8/ui.fancytree.min.css" rel="stylesheet" type="text/css">
<script src="includes/js/fancytree/jquery.fancytree.min.js" type="text/JavaScript" ></script>
<script type="text/javascript">
    <!--
    function keyEvent(event) {
        if (event.keyCode == 13) {
            switchPage(0);
        }
    }
    // -->
</script>

<form action="index.php?module=contact&amp;event=TSearchContact&amp;id=[% $contact_id %]" method="post" id="subForm">

    <div id="comboboxlist">
        <div id="comboboxres"></div>
        <div id="comboboxstatus">
            &nbsp;
            <a href="#" onclick="document.getElementById('comboboxlist').style.display='none';"><img src="images/close.gif"  width="16" height="16" alt="" border="0" /></a>
        </div>
    </div>

    <div style="margin:5px;text-align:right;white-space:nowrap;">
        <b>[% $user->gettext('Filter') %]:</b>
        <input type="text" name="search" value="" size="20" class="textbox" id="search" onkeyup="keyEvent(event)"/>
        &nbsp;
        <b>[% $user->gettext('Firmenstruktur') %]:</b>
        <input type="text" name="structure_descr" value="" id="structure_descr" class="textbox" style="width:250px;" readonly="readonly" />
        <input type="hidden" name="structure_id" value="" id="structure_id" />
        <a href="JavaScript:void(0);" onclick="showComboBoxList();"><img src="images/down.gif" width="22" height="22" alt="" border="0"  /></a>
        <a href="JavaScript:void(0);" onclick="switchPage(0);"><img src="images/reload_page.gif" width="22" height="22"  alt="" border="0" /></a>
    </div>

    <!-- Contact begin -->

    <div id="ContactList"></div>
</form>

    <script type="text/javascript">
        <!--

        var sorting = "[% $sort %]";
        var sortorder = "[% $sortorder %]";

        function showComboBoxList() {

            var txtfield = document.getElementById("structure_descr");
            var userbox  = document.getElementById("comboboxlist");

            if (userbox.style.visibility != "visible") {
                var spantop    = absTop(txtfield);
                var spanleft   = absLeft(txtfield);
                var spanwidth  = txtfield.offsetWidth;
                var spanheight = txtfield.offsetHeight;

                spantop   = spantop + spanheight;
                spanwidth = spanwidth - 2;

                userbox.style.width = spanwidth+"px";
                userbox.style.left  = spanleft+"px";
                userbox.style.top   = spantop+"px";

                userbox.style.visibility = "visible";
                userbox.style.display    = "block";
            } else {
                userbox.style.visibility = "hidden";
                userbox.style.display = "none";
            }
        }

        function setStructureValue(sid, childs, name) {

            document.getElementById('structure_descr').value = name;
            document.getElementById('structure_id').value = sid;
            document.getElementById('comboboxlist').style.display="none";
            switchPage(0);
        }

        function switchPage(pos) {

            sid = document.getElementById('structure_id').value;
            search_str = document.getElementById('search').value;

            var http_url = "index.php?module=contact&event=AJAXContactList&pos="+parseInt(pos);
            http_url += "&sid="+parseInt(sid);
            http_url += "&search="+escape(search_str);
            http_url += "&sort="+escape(sorting);
            http_url += "&sortorder="+escape(sortorder);
            makeHttpRequest(http_url, "buildContactList", true);
        }

        function getContact(contactid, contact) {
            parent.getContact(contactid, contact);
        }

        function buildContactList(xmldoc) {

            contact_id = 0;

            var navigation = xmldoc.getElementsByTagName("navigation")[0];
            var Pages = navigation.getAttribute("pages");
            var Page  = navigation.getAttribute("page");
            var pos   = navigation.getAttribute("pos");
            var EntriesPerPage = navigation.getAttribute("entriesperpage");
            var Entries        = navigation.getAttribute("entries");
            if (parseInt(pos) > 0) {posback = parseInt(pos) - 1;} else {posback = 0;}
            if (parseInt(Pages) > 0) {lastpage = parseInt(Pages) - 1;} else {lastpage = 0;}
            if (parseInt(pos) < lastpage) {nextpage = parseInt(pos) + 1;} else {nextpage = lastpage;}

            navigationString  = "<tr><td colspan=\"9\" class=\"navstring\">&nbsp;&nbsp;";
            navigationString += "<a href=\"JavaScript:void(0);\" ";
            if (parseInt(pos) > 0) {navigationString += "onclick=\"switchPage(0)\"";}
            navigationString += "><img src=\"images/gohome.gif\" height=\"16\" width=\"16\" alt=\"\" align=\"absmiddle\" border=\"0\" /></a>";
            navigationString += "<a href=\"JavaScript:void(0);\" ";
            if (parseInt(pos) > 0) {navigationString += "onclick=\"switchPage("+posback+")\"";}
            navigationString += "><img src=\"images/back.gif\" height=\"16\" width=\"16\" alt=\"\" align=\"absmiddle\" border=\"0\" /></a>";
            navigationString += "&nbsp;[% $user->gettext('Seite') %] "+Page+" / "+Pages+"&nbsp;";
            navigationString += "<a href=\"JavaScript:void(0);\" ";
            if (nextpage+1 <= parseInt(Pages)) {navigationString += "onclick=\"switchPage("+nextpage+")\"";}
            navigationString += "><img src=\"images/forward.gif\" height=\"16\" width=\"16\" alt=\"\" align=\"absmiddle\" border=\"0\" /></a>";
            navigationString += "<a href=\"JavaScript:void(0);\" ";
            if (nextpage+1 <= parseInt(Pages)) {navigationString += "onclick=\"switchPage("+lastpage+")\"";}
            navigationString += "><img src=\"images/goend.gif\" height=\"16\" width=\"16\" alt=\"\" align=\"absmiddle\" border=\"0\" /></a>&nbsp;&nbsp;";
            navigationString += "</td></tr>";

            tmporder = (sortorder == "DESC") ? "asc" : "desc";

            headerString  = "<table border=\"0\" width=\"100%\" class=\"listview\" cellspacing=\"0\"><thead><tr class=\"fields\" >";
            headerString += "<td>#</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "Name") ? "sortorder='"+tmporder+"';" : "sorting='Name';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Name') %]";
            if (sorting == "Name") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "Company") ? "sortorder='"+tmporder+"';" : "sorting='Company';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Firma') %]";
            if (sorting == "Company") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "Position") ? "sortorder='"+tmporder+"';" : "sorting='Position';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Position') %]";
            if (sorting == "Position") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "PersonalNo") ? "sortorder='"+tmporder+"';" : "sorting='PersonalNo';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Personalnr.') %]";
            if (sorting == "PersonalNo") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "Phone") ? "sortorder='"+tmporder+"';" : "sorting='Phone';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Telefon') %]";
            if (sorting == "Phone") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "Email") ? "sortorder='"+tmporder+"';" : "sorting='Email';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('E-Mail') %]";
            if (sorting == "Email") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td style=\"cursor:pointer;\" onclick=\"";
            headerString += (sorting == "UserName") ? "sortorder='"+tmporder+"';" : "sorting='UserName';";
            headerString += "switchPage("+pos+");\" >[% $user->gettext('Benutzer') %]";
            if (sorting == "UserName") headerString += "&nbsp;<img src=\"images/sort"+tmporder+".gif\" width=\"11\" height=\"11\" border=\"0\" alt=\"\" align=\"absmiddle\" />";
            headerString += "</td>";
            headerString += "<td>&nbsp;</td></tr></thead>";

            var users = xmldoc.getElementsByTagName("user");

            HTMLoutput = headerString;

            if (users.length > 0) {

                for (i=0; i < users.length; i++) {

                    if (contact_id == parseInt(users[i].getAttribute("id"))) {
                        HTMLoutput += "<tr class=\"selectedRow\" ";
                    } else {
                        if (i % 2 == 0) {
                            HTMLoutput += "<tr class=\"odd\" ";
                        } else {
                            HTMLoutput += "<tr class=\"even\" ";
                        }
                    }


                    HTMLoutput += " style=\"cursor:pointer;\" onclick=\"getContact("+users[i].getAttribute("id")+", '"+users[i].getAttribute("name");

                    if (users[i].getAttribute("company").length > 0) {
                        HTMLoutput += " ("+users[i].getAttribute("company")+")";
                    }

                    HTMLoutput += "');\" >";


                    HTMLoutput += "<td>"+(i+1)+"</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("name")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("company")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("position")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("personalno")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("phone")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("email")+"&nbsp;</td>";
                    HTMLoutput += "<td>"+users[i].getAttribute("username")+"&nbsp;</td>";
                    HTMLoutput += "<td><a href=\"JavaScript:void(0);\"><img src=\"images/add.gif\" width=\"16\" height=\"16\" alt=\"\" border=\"0\" align=\"absmiddle\"></a></td>";
                    HTMLoutput += "</tr>";

                }
            } else {
                HTMLoutput += "<tr>";
                HTMLoutput += "<td colspan=\"9\">&nbsp;[% $user->gettext('Keine Kontakte gefunden') %]&nbsp;</td>";
                HTMLoutput += "</tr>";
            }
            HTMLoutput += navigationString;
            document.getElementById("ContactList").innerHTML = HTMLoutput;
        }

        var pos  = 0;
        
        $("#comboboxres").fancytree({
            source: {
              url: "index.php?module=structure&event=JSONStructureTree&id=0"
            },
            checkbox: false,
            autoActivate: true, // Automatically activate a node when it is focused (using keys).
            selectMode: 1, // 1:single, 2:multi, 3:multi-hier
            activeVisible: true,
            activate: function(event, data){
                var node = data.node;
                setStructureValue(node.key, 0, node.title);    
            },
            lazyLoad: function(event, data){
                var node = data.node;
                data.result = {
                    url: node.data.src,
                  data: {mode: "children", parent: node.key},
                  cache: false
                };  
            }    
        }).show();
        

        makeHttpRequest("index.php?module=contact&event=AJAXContactList", "buildContactList", true);

        //-->
    </script>