// ------------------------------------
// Application Name: phpFILEmanager
// Application Version: 0.8 ie only
// Author Name: Boris Rojkov
// Author Email: bo@six-ty-six.de
// Author Url: http://www.six-ty-six.de
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!Do not remove these lines!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// ------------------------------------

//get the selected item in the listbox
function getselecteditem(currpath) {
	var itemindex;
	for (var i=0; i<document.getElementsByTagName('option').length; i++) { 
		if (document.getElementsByTagName('option')[i].selected) {
			itemindex=i;
		}
	}
	return itemindex;
}
//refresh the filebrowser
function refreshflbrowser(currpath) {
	var slashpos;
	var selected=getselecteditem(currpath);
	if (document.getElementsByTagName('option')[selected].name=="dir"){
		if (document.getElementsByTagName('option')[selected].value!=".") {
			if (document.getElementsByTagName('option')[selected].value=="..") {
				currpath=currpath.substring(0,currpath.length-1);
				for (i=currpath.length-1; i>=0; i--){
					if(currpath.charAt(i)=="/"){
						slashpos=i;
						break;
					}
				}
				currpath=currpath.substring(0, i+1);	
				var newpath=currpath;
			} else {
				var newpath=currpath+document.getElementsByTagName('option')[selected].value+"/";
			}
			changedir(newpath);
		}
	} else {
		viewfile(currpath);
	}
}
//change folder
function changedir(newpath){
	window.location.href="index.php?site=file&folder="+newpath;
}
//make folder/file
function make(currpath){
	var newdirname=prompt("Please enter foldername","");
	window.location.href="index.php?site=file&action=createnewdir&newdirname="+newdirname+"&folder="+currpath;
}
//view file/change folder
function viewfile(currpath){
	var selected=getselecteditem(currpath);
	if (selected>=1){
		if (document.getElementsByTagName('option')[selected].name=="file"){
			var	filetoview=document.getElementsByTagName('option')[selected].value;
			window.location.href="index.php?site=file&action=viewfile&filetoview="+filetoview+"&folder="+currpath;
		} else {
			 refreshflbrowser(currpath);
		}
	}
}
//remove file/folder
function remove(currpath){
	var selected=getselecteditem(currpath);
	var check;
	if (selected>=2){
		if (document.getElementsByTagName('option')[selected].name=="dir"){
			for (var i=0; i<document.getElementsByTagName('option').length; i++) { 
				if (document.getElementsByTagName('option')[i].selected) {
					selected=i;
				}
			}
			var	removedirname=document.getElementsByTagName('option')[selected].value;

			check=confirm("Should the folder "+removedirname+" be removed?");

			if (check==true){
				window.location.href="index.php?site=file&action=removedir&removedirname="+removedirname+"&folder="+currpath;
			}
		} else {
			var	filetounlink=document.getElementsByTagName('option')[selected].value;
			check=confirm("Should the file "+filetounlink+" be removed?");

			if (check==true){
				window.location.href="index.php?site=file&action=unlink&filetounlink="+filetounlink+"&folder="+currpath;
			}
		}
	}
}
//rename file/folder
function renameitem(currpath){
	var selected=getselecteditem(currpath);
	alert(currpath);
	if (selected>=2){
		var	itemtorename=document.getElementsByTagName('option')[selected].value;
		var newitemname=prompt("Please enter new name","");
		alert(newitemname);
		if(newitemname=="false") {
		} else {
		  window.location.href="index.php?site=file&action=rename&itemtorename="+itemtorename+"&newitemname="+newitemname+"&folder="+currpath;
		}
	}
}
//get file info
function getinfo(currpath){
	var selected=getselecteditem(currpath);
	if (selected>=2){
		var	infofile=document.getElementsByTagName('option')[selected].value;
		window.location.href="index.php?site=file&action=getinfo&infofile="+infofile+"&folder="+currpath;
	}
}	
//download file
function download(currpath){
	var selected=getselecteditem(currpath);
	if (selected>=2){
		if (document.getElementsByTagName('option')[selected].name=="file"){
			var	downloadfile=document.getElementsByTagName('option')[selected].value;
			window.location.href="index.php?site=file&action=download&downloadfile="+downloadfile+"&folder="+currpath;
		}
	}
}
