function ft_query(box, parentID) {
	//alert("ft_query::  " + box);
	xmlhttp = getAjaxObject();
	xmlhttp.open("POST", "fileTreeComponent.php?fileTreeList=" + parentID, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			ft_fill(box, ajaxResponseArray(xmlhttp.responseText), parentID);
		}
	}
	xmlhttp.send(""); 
	ft_loading(box);
}

function ft_loading(boxt) {
	var cbox = document.getElementById(boxt + "_d");
	
	cbox.innerHTML = '<img src="'+ajaxLoadImage.src+'" />  Loading...';
}

function ft_fill(boxt, res, id) {
	//a small note on admin...  the controls CAN be hacked to be shown publically, but they will not function
	//because the AJAX php scripts check for a valid username.  sorry, hackers!
		
	var resnum = parseInt(res["numRes"]);
	var dv = new String();
	var box = document.getElementById(boxt);
	var cbox = document.getElementById(boxt + "_d");
	
	var roi = 'style="cursor: pointer; opacity: .5; filter:alpha(opacity=50);"' + UTIL_HL;
	
	box.onmouseup = function() {
		ft_destroy(boxt, id);
	}	
	
	if(box.innerHTML != "") {
		//box.getElementsByTagName("img")[0].src = "images/retract.gif";
	}

	for (i = 0; i < resnum; i++) {
		link = ((res['file_'+i] == "dir") ? "#" : res['file_'+i]);
		 
		dv += '<img alt="" src="images/tree-icons/info.gif" onmouseup="ft_showDesc(\'ft_desc\', '+res['id_'+i]+', this)" style="cursor: pointer;" />'
		if (link == "#") {
			dv += '<a href="'+link+'" id="'+boxt+'_'+res['id_'+i]+'" onmouseup="ft_query(\''+boxt+'_'+res['id_'+i]+'\', '+res['id_'+i]+');">';
			dv += '<img alt="" src="images/expand.gif" style="display: none;"> ';
			dv += '<img alt="" src="images/tree-icons/folder.gif"> ';
		}else{
			dv += '<a href="'+link+'">';
			dv += '<img alt="" src="images/tree-icons/file.gif"> ';
		}
		dv += res['title_'+i] + '</a>';
		
		if (ADMINMODE) {
			dv += '&nbsp;&nbsp;&nbsp;<img alt="Edit this File" src="images/tree-icons/editfile.gif" '+roi+'> ';
			dv += '<img alt="Delete this File" src="images/tree-icons/deletefile.gif" '+roi+' onmouseup="ft_deleteFile('+res['id_'+i]+', \''+boxt+'\', '+id+')"> ';
		}
		
		dv += '<div id="'+boxt+'_'+res['id_'+i]+'_d" class="tree_indent"></div>';
		
	}
	if (ADMINMODE) {
		dv += '<div><a href="#" style="border-top: dashed 1px #CCCCCC;" onmouseup="ft_createNewFile(this, '+id+', \''+boxt+'\');">';
		dv += '<img src="images/tree-icons/addfile.gif" /> New...';
		dv += '</a></div>';
	}
	
	cbox.innerHTML = dv;
}

function ft_destroy(boxt, id) {
	//alert("ft_destroy::  " + boxt);
	var box = document.getElementById(boxt);
	var cbox = document.getElementById(boxt + "_d");
	
	box.onmouseup = function() {
		ft_query(boxt, id);
	}
	
	//box.getElementsByTagName("img")[0].src = "images/expand.gif";
	
	cbox.innerHTML = "";
}

//give the parent box of the data box... not x_x_d
function ft_refresh(parent_box, id) {
	ft_destroy(parent_box, id);
	
	ft_query(parent_box, id);
}

function ft_showDesc(boxt, id, giver) {
	var box = document.getElementById(boxt);

	pos = findPos(giver)
	box.style.opacity = 0.85;
	box.style.filter = "alpha(opacity=85)";
	box.style.display = "block";
	box.style.top =  pos[1] +"px";
	box.style.left =  pos[0] + "px";
	
	ajax("fileTreeComponent.php?getDesc="+id, ft_finalDesc, boxt);
	
	box.getElementsByTagName("div")[0].innerHTML = '<img src="'+ajaxLoadImage.src+'" />  Loading...';
}

function ft_finalDesc(res, boxt) {
	var box = document.getElementById(boxt);
	res = ajaxResponseArray(res);
	
	box.getElementsByTagName("div")[0].innerHTML = res['desc'];
}

function ft_hideDesc(boxt) {
	var box = document.getElementById(boxt);
	var opacity = parseFloat(box.style.opacity);
	
	if (opacity <= 0.08) {
		box.style.display = "none";
	}else{
		opacity -= 0.2;
		box.style.opacity = opacity;
		box.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
		window.setTimeout(function(){ft_hideDesc(boxt)}, 50);
	}
}

function ft_createNewFile(giver, parentID, parentBoxID) {
	var pos = findPos(giver);
	var tt = new String();
	var cc = "font-family: sans-serif; font-size: 11px; border: solid 1px #AAAAAA; width: 100%;";
	
	//form
	var addDiv = document.createElement("div");
	var addForm = document.createElement("form");
	addDiv.appendChild(addForm);
	
	tt = 'Add a new File';
	tt += '<input name="title" value="Title" style="'+cc+'" /><br>';
	tt += '<input name="fileRef" value="File Reference" style="'+cc+'" />';
	tt += '<input type="checkbox" name="isDir" onclick="this.previousSibling.disabled=!this.previousSibling.disabled;">Folder'
	tt += '<textarea name="desc" style="'+cc+'">Description</textarea>';
	tt += '<center><input type="button" value="Add" onmouseup="ft_postData(this, '+parentID+', \''+parentBoxID+'\')" /></center><br>';
	tt += '<input type="hidden" name="parentID" value="'+parentID+'" />'
	tt += '<input type="hidden" name="addFile" value="yes" />'
	addForm.innerHTML = tt;
	
	addDiv.className = "tree_desc";
	addDiv.style.left = pos[0] + "px";
	addDiv.style.top = pos[1] + "px";
	addDiv.style.opacity = 0.85;
	addDiv.style.filter = "alpha(opacity=" + (0.85 * 100) + ")";
	
	addDiv.innerHTML += '<div style="background-color: #CCCCCC; border: solid 1px #AAAAAA; text-align: center; font-size: 9px; cursor: pointer;" onmouseup="ft_destroyCNF(this);">[close]</div>';
	
	document.body.appendChild(addDiv);
}

function ft_postData(child, parentID, parentBoxID) {
	form = child.parentNode.parentNode;	
	
	ajaxSend("fileTreeComponent.php", formToURI(form), function(res) {
		ft_prepareDestroy(res, form, parentID, parentBoxID);
	});
	
	form.innerHTML = '<img src="'+ajaxLoadImage.src+'" />  Sending...'
}

function ft_prepareDestroy(res, form, parentID, parentBoxID) {
	box = form.parentNode;
	
	if (res != "") {
		alert(res);
	}
	
	ft_destroyCNF(form);
	ft_refresh(parentBoxID, parentID);
}

function ft_destroyCNF(child) {
	var box = child.parentNode;
	var opacity = parseFloat(box.style.opacity);
	
	
	if (opacity <= 0.08) {
		box.parentNode.removeChild(box);
	}else{
		opacity -= 0.2;
		box.style.opacity = opacity;
		box.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
		window.setTimeout(function(){ft_destroyCNF(child)}, 50);
	}
	
}

//both parameters are also passed to ft_refresh
function ft_deleteFile(id, parentID, parentID_n) {
	if (confirm("Do you really want to delete this file?\r\nThis action cannot be undone!")) {
		ajax("fileTreeComponent.php?deleteElem="+id, function(res){
			if (res == "") {
				ft_refresh(parentID, parentID_n);
			}else{
				alert(res);
			}
		});
	}
}








