function selected(td){
	document.getElementById(td).className = 'selected';
	document.getElementById(td).style.cursor = 'hand';
	document.getElementById(td).style.cursor = 'pointer';
}

function unselected(td) {
	document.getElementById(td).className = 'norm';
	document.getElementById(td).style.cursor = 'default';
}

function showfiles(user){
	var fileslisting = document.getElementById('filelistings').getElementsByTagName('ul');
	for (i=0;i<fileslisting.length;i++) {
		fileslisting[i].style.display = 'none';
	}
	document.getElementById(user+'_file_list').style.display = 'block';
}

function objXmlHttp() {
	var xmlHttp;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)	{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function populateUserTable() {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4){
			document.getElementById('update_user_inputs').innerHTML = '&nbsp;';
			document.getElementById('update_user_inputs').style.display = 'none';
			document.getElementById('userDataTable').innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("POST","includes/populate_users_listing.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(null);
}

function populateFilesUserTable() {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			document.getElementById('user_files').innerHTML = '&nbsp;';
			document.getElementById('user_files').style.display = 'none';
			document.getElementById('filesUserDataTable').innerHTML = xmlHttp.responseText;
		}
	}
										
	xmlHttp.open("POST","includes/populate_files_users_listing.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(null);
}

function retrieveUser(requesteduser) {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			document.getElementById('update_user_inputs').innerHTML = xmlHttp.responseText;
			document.getElementById('update_user_inputs').style.display = 'block';
		}
	}
										
	xmlHttp.open("POST","includes/retrieve_user.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('user='+requesteduser);
}

function updateUser() {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText != ''){
				alert(xmlHttp.responseText);
			}
			populateUserTable();
		}
	}
	xmlHttp.open("POST","includes/update_user.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('user='+document.existingUsers.update_username.value
	            +'&password='+document.existingUsers.update_password.value
	            +'&confirm='+document.existingUsers.confirm_update_password.value
	            +'&existinguser='+document.existingUsers.existing_username.value);
}

function addUser() {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText != ''){
				alert(xmlHttp.responseText);
			}
			document.forms.addnewuser.reset();
			populateUserTable();
		}
	}										
	xmlHttp.open("POST","includes/add_user.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('username='+document.getElementById('new_username').value
				+'&password='+document.getElementById('new_password').value
				+'&confirm='+document.getElementById('new_confirm').value);
}

function deleteUser(userToRemove) {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText != ''){
				alert(xmlHttp.responseText);
			}
			populateUserTable();
		}
	}
	var answer = confirm('Are you sure you wish to delete user "'+userToRemove+'" and all related files?');
	if(answer){
		xmlHttp.open("POST","includes/delete_user.php",true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send('user='+userToRemove);
	}					
}

function deleteUserFiles(userToRemove) {
	var xmlHttp = objXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText != ''){
				alert(xmlHttp.responseText);
			}
			populateUserTable();
		}
	}
	var answer = confirm('Are you sure you wish to delete all files for user "'+userToRemove+'"?');
	if(answer){
		xmlHttp.open("POST","includes/delete_user_files.php",true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send('user='+userToRemove);
	}					
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	try {
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	} catch (e) {
		setTimeout("addLoadEvent(" + func + ");", 100);
	}
}
