var currentSelected;
users = new Array;


function addUsers() {
ulist = document.getElementsByName('usernames[]');
if (ulist) {
	for (var i = 0 ; i < ulist.length ; i++) {
		if (ulist[i].checked) {
			if(!alreadyInList(ulist[i].value)){
				window.opener.addNewUser(ulist[i].value);
			}
		}
	}
}
window.close();
self.close();
}


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) {
		if (keyname != 'has4ValidElms') {
	document.formm.users.options[i++] = new Option(users[keyname]['username'], users[keyname]['username']);
		}
	}
}

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


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 onSendCheckShareName() {
	for(keyname in users) {
	if (keyname != 'has4ValidElms') {
			if (users[keyname]['sharename'].length < 1) {
				alert('<?$lang->display("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('discard_button').style.visibility='hidden';
	}
}

function showInfo() {
	selectedUser = document.formm.users.value;	
	hideDetails();
	for(keyname in users) {
	if (keyname != 'has4ValidElms') {
		if (keyname == selectedUser) {
userInfo = users[keyname];
document.formm.sharename.value = userInfo['sharename'];
document.formm.writee.checked = userInfo['write'];
document.getElementById('discard_button').style.visibility='visible';
		}
		}
	}
}

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

function dumpData() {
	data = '';
	for(keyname in users) {
		if (keyname != 'has4ValidElms') {
			data += users[keyname]['username'];
			data += ',';
			data += users[keyname]['sharename'];
			data += ',';
			data += users[keyname]['write'];
			data += '\n';
		}
	}
	document.submitform.dump.value=data;
}

function showAddUser() {
	/*centeredMenu('popmenu-afian_add_user');*/
	popup('?file=share_select_users', 'share_select_users', '330', '514', 'yes');
}
