
var currentSelected;
users = new Array;


function addUsers() {
hideMenu('afian_add_user');
if (document.add_user_form.add_text_user.value.length > 0) {
	if(!alreadyInList(document.add_user_form.add_text_user.value)){
		addNewUser(document.add_user_form.add_text_user.value);
	}
}
hist = document.getElementsByName('usernameHistory[]');
if (hist) {
	for (var i = 0 ; i < hist.length ; i++) {
		if (hist[i].getAttribute('chk') == "1") {
			if(!alreadyInList(hist[i].value)){
				addNewUser(hist[i].value);
			}
		}
	}
}
}


function addNewUser(username) {
users[username] = new Array;
users[username]['username'] = username;
users[username]['sharename'] = '<?echo $str->safeJS($str->fromHTML($_GET['sharename']))?>';
users[username]['write'] = '';

drawList();
	for (var i = 0 ; i < document.formm.users.options.length; i++) {
		if (document.formm.users.options[i].value==username) {
			document.formm.users.options[i].selected = true;
		}
	}
	showInfo();
	document.formm.users.focus();
}



function drawList() {
	i = 0;
	for(keyname in users) {
	document.formm.users.options[i++] = new Option(users[keyname]['username'], users[keyname]['username']);
	}
}

function deleteOption(value) {
	for (var i = 0 ; i < document.formm.users.options.length ; i++) {
		if (document.formm.users.options[i].value == value) {
			delete users[value];
			document.formm.users.options[i] = null;
		}		
	}
	hideDetails();
	document.formm.users.focus();
}

function alreadyInList(username) {
	if (users[username] == undefined) {
		return false;
	} else {
		return true;
	}
}

/*
function checkShareName(sharename) {
	for(keyname in users) {
		if (users[keyname]['sharename'] == sharename && keyname != selectedUser) {
			return keyname;
		}
	}
	return false;
}
*/

function onSendCheckShareName() {
	for(keyname in users) {
			if (users[keyname]['sharename'].length < 1) {
				alert('Sharename empty for user '+keyname);
				return false;
			}
	}
	return true;
}

function hideDetails() {
	if (document.formm.users.value != '') {
	document.getElementById('detailsNoneSelected').style.display='none';
	document.getElementById('details').style.display='block';
	} else {
	document.getElementById('detailsNoneSelected').style.display='block';
	document.getElementById('details').style.display='none';
	document.getElementById('optcell').innerHTML='';
	}
}

function showInfo() {
	selectedUser = document.formm.users.value;	
	hideDetails();
	for(keyname in users) {
		if (keyname == selectedUser) {
userInfo = users[keyname];
document.formm.sharename.value = userInfo['sharename'];
document.formm.writee.checked = userInfo['write'];
document.getElementById('optcell').innerHTML = '<?
$tpl->btn(
ucfirst($locale['sharing']['discard_selected']), 
"deleteOption(''+selectedUser+'')",
"",
true
)
?>';
		}
	}
}

function updateValue(variable, value){
	if (document.formm.users.value != '') {
		users[selectedUser][variable] = value;
	}
}

function dumpData() {
data = '';
for(keyname in users) {
	data += users[keyname]['username'];
	data += ',';
	data += users[keyname]['sharename'];
	data += ',';
	data += users[keyname]['write'];
	data += '\n';
}

document.submitform.dump.value=data;
}

function showAddUser() {
	popupMenu('afian_add_user');
}
