var AttachmentList = false;
var objAttachmentList;

function getAttachments() {
	AttachmentList = true;	
	objAttachmentList = document.getElementById("AttachmentList");	
	objAttachmentList.innerHTML = "";
			makeHttpRequest("index.php?module=upload&event=AJAXUploadList&kbid="+kbid, "buildAttachmentList", true);	
}

function buildAttachmentList(xmldoc) {

	var attachments   = xmldoc.getElementsByTagName("attachment");

    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;}

	if (Pages > 0) {
    	navigationString  = "<tr><td colspan=\"8\" >&nbsp;&nbsp;";					
		navigationString += "<a class= 'btn btn-outline-secondary' href=\"JavaScript:void(0);\" ";
		if (parseInt(pos) > 0) {navigationString += "onclick=\"switchPage(0)\"";}
		navigationString += "><span class='fa fa-home'></span></a>";
		navigationString += "<a class= 'btn btn-outline-secondary' href=\"JavaScript:void(0);\" ";
		if (parseInt(pos) > 0) {navigationString += "onclick=\"switchPage("+posback+")\"";}
		navigationString += "><span class='fa fa-chevron-left'></span></a>";
 		navigationString += "&nbsp;"+navigation.getAttribute("descr")+" "+Page+" / "+Pages+"&nbsp;";
   		navigationString += "<a class= 'btn btn-outline-secondary' href=\"JavaScript:void(0);\" ";
   		if (nextpage+1 <= parseInt(Pages)) {navigationString += "onclick=\"switchPage("+nextpage+")\"";}
   		navigationString += "><span class='fa fa-chevron-right'></span></a>";
   		navigationString += "<a class= 'btn btn-outline-secondary' href=\"JavaScript:void(0);\" ";
   		if (nextpage+1 <= parseInt(Pages)) {navigationString += "onclick=\"switchPage("+lastpage+")\"";}
   		navigationString += "><span class='fa fa-forward'></a>&nbsp;&nbsp;";                    
   		navigationString += "</td></tr>";
	} else {
		navigationString = "";
	}
	
	HTMLOutput = "<table border=\"0\" width=\"100%\" class=\"listview\" cellspacing=\"0\">\n";
	HTMLOutput += "  <thead>\n";
    HTMLOutput += '  <tr class="fields" >\n';
	HTMLOutput += "	   <td>"+DocHeaders[0]+"</td>\n	   <td>"+DocHeaders[1]+"</td>\n";
	HTMLOutput += "    <td>"+DocHeaders[2]+"</td>\n	   <td>"+DocHeaders[3]+"</td>\n";
	HTMLOutput += "	   <td>"+DocHeaders[4]+"</td>\n    <td>"+DocHeaders[5]+"</td>\n";
	HTMLOutput += "    <td>"+DocHeaders[6]+"</td>\n    <td>&nbsp;</td>\n";
	HTMLOutput += "  </tr>\n";
	HTMLOutput += "  </thead><tbody>\n";
    console.log(attachments);
 	if (attachments.length > 0) {        
        for (i=0; i < attachments.length; i++) {

			row = attachments[i];
			typ = row.getAttribute("typ");
			
        	HTMLOutput += "  <tr class=\"even\">\n";
        	if (typ == 1 || row.getAttribute("fname")) {

    	        	if(row.getAttribute("thumbnail") ){
HTMLOutput += "    <td>&nbsp;<a href=\"index.php?module=upload&class=download&event=ShowImage&name="+row.getAttribute("tmpname")+"\" target=\"top\">"+row.getAttribute("fname")+"</a></td>\n";
                    } else {
HTMLOutput += "    <td>&nbsp;<a href=\"index.php?module=upload&class=download&id="+row.getAttribute("id")+"\" target=\"top\">"+row.getAttribute("fname")+"</a></td>\n";
                    }
                    
        	} else if (typ == 2 || row.getAttribute("url")) {
        		HTMLOutput += "    <td>&nbsp;<a href=\""+row.getAttribute("url")+"\" target=\"top\">"+row.getAttribute("url")+"</a></td>\n";
        	} else {
        		HTMLOutput += "    <td>&nbsp;&nbsp;</td>\n";
        	}
        	if(row.getAttribute("mime") != ''){
				HTMLOutput += "    <td><img src=\""+row.getAttribute("mime")+"\" /></td>\n";
			}
			else{
				HTMLOutput += "    <td><span class='fa fa-file'></span></td>\n";
			}
        	
        	HTMLOutput += (row.getAttribute("size")) ?
        		"    <td>"+row.getAttribute("size")+" KB</td>\n" : "    <td>&nbsp;</td>\n";
        	
        	HTMLOutput += (row.getAttribute("shortdescr")) ?
        		"    <td>"+row.getAttribute("shortdescr")+"</td>\n" : "    <td>&nbsp;</td>\n";
        	
    		HTMLOutput += (row.getAttribute("thumbnail") ) ?
    			"    <td><a href=\"index.php?module=upload&class=download&event=ShowImage&name="+row.getAttribute("tmpname")+"\" target=\"top\"><img src=\"index.php?module=upload&event=ShowImage&name="+row.getAttribute("thumbnail")+"\" border=\"0\" title=\"("+row.getAttribute("width")+"x"+row.getAttribute("height")+")\" /></td>\n" :
				"    <td>&nbsp;</td>\n";
        	
        	HTMLOutput += "    <td>"+row.getAttribute("cdate")+"</td>\n";
        	HTMLOutput += "    <td>"+row.getAttribute("cuser")+"</td>\n";
        	
        	HTMLOutput += "    <td width=\"50\" style=\"text-align:right !important;white-space:nowrap;\">";
        	
        	if (typ == 3 || typ == 2 && row.getAttribute("fname")) {
				HTMLOutput += "<a href=\"JavaScript:void(0);\" onclick=\"showWindow('index.php?module=upload&event=FileDetails&id="+row.getAttribute("id")+"', 'Upload', 900, 350);\"><img src=\"images/details.gif\" width=\"16\" height=\"16\" alt=\"\" border=\"0\" align=\"absmiddle\" /></a>";
        	}
			HTMLOutput += "&nbsp;<a class= 'btn btn-outline-secondary' href=\"JavaScript:void(0);\" onclick=\"RemoveAttachment("+row.getAttribute("id")+");\"><span class='fa fa-trash'></span></a>";
					
			HTMLOutput += "&nbsp;</td>\n";
        	
        	// HTMLOutput += "    <td>"+row.getAttribute("cuserid")+"</td>\n";
        	HTMLOutput += "  </tr>\n"
        	
    	}
    } else {
    
    	HTMLOutput += "  <tr class=\"even\"><td colspan=\"8\" align=\"center\">"+DocHeaders[7]+"</td></tr>\n";

    }
    
    HTMLOutput += navigationString + "</tbody></table>\n";
    
    objAttachmentList.innerHTML = HTMLOutput;
}

function closeAttachmentPopUp() {	
	setTimeout('closePopup()', 1000);
	$.fancybox.close();	
}

function closePopup() {
	getAttachments();	
}

