//hierin zitten de rechten van de 'granted' users/groups
var accessrights=new Array();
accessrights["usersgranted"]=new Array();
accessrights["groupsgranted"]=new Array();
accessrights["mediausersgranted"]=new Array();
accessrights["mediagroupsgranted"]=new Array();
accessrights["adminusersgranted"]=new Array();
accessrights["admingroupsgranted"]=new Array();

//hierin zitten de checkboxen die beschikbaar zijn voor de selectboxen
var accesscontrols=new Array();
accesscontrols["usersgranted"]=new Array();
accesscontrols["groupsgranted"]=new Array();
accesscontrols["mediausersgranted"]=new Array();
accesscontrols["mediagroupsgranted"]=new Array();
accesscontrols["adminusersgranted"]=new Array();
accesscontrols["admingroupsgranted"]=new Array();


function setaccessrights(slctbox,chkbox)
{ //click op checkbox
	//update rechten voor de geselecteerde items in slctbox in accessrights[slctbox][slctbox.options.value]
	lst=document.forms[0][slctbox];
	chk=document.forms[0][chkbox];
	for (i=0;i<lst.options.length;i++) {
		if (lst.options[i].selected) {
			v=accessrights[slctbox][lst.options[i].value];
			pos=v.indexOf(chk.value)+1;
			len=v.length;
			if (pos)
				v=v.substring(0,pos-1)+v.substring(pos,len);
			if (chk.checked) v=v+chk.value;
			accessrights[slctbox][lst.options[i].value]=v;
		}
	}
}

function getaccessrights(sl)
{ //click op selectbox
	//haal uit de accesscontrols[slctbox] array de checkboxen
	//zet de rechten van accessrights[slctbox][usergroupid] hierin
	slctbox = sl.name;
	multi="";
	for (i=0;i<sl.options.length;i++) {
		if (sl.options[i].selected) {
			if (multi!="") {
				multi="";
				break;
			} else
				multi=sl.options[i].value;
		}
	}
	//multi=selected value of leeg
	for (i=0;i<accesscontrols[slctbox].length;i++) {
		chk=accesscontrols[slctbox][i];
		v=document.forms[0][chk].value;
//alert(chk+"="+v);
		if (multi!="")
			document.forms[0][chk].checked= (accessrights[slctbox][multi].indexOf(v)!=-1);
		else
			document.forms[0][chk].checked= false;
	}
}

function moverights(from,to)
{ //accessrights toevoegen of verwijderen 
	if (from.indexOf("granted")!= -1) {
		//verwijderen
		lst=document.forms[0][from];
		for (i=0;i<lst.options.length;i++) {
			if (lst.options[i].selected) {
				accessrights[from][lst.options[i].value]=null;
			}
		}
//		setTimeout("getaccessrights(document.forms[0]["+from+"]",1000);
	} else {
		//toevoegen
		lst=document.forms[0][from];
		for (i=0;i<lst.options.length;i++) {
			if (lst.options[i].selected) {
				accessrights[to][lst.options[i].value]="";
			}
		}
//		setTimeout("getaccessrights(document.forms[0]["+to+"]",1000);
	}
}

function selectoptions(thelist)
{
	var ltype="";
	var listvalue="";
	q="";
	if (thelist.name.indexOf("groupsgranted")!= -1) {
		ltype=replacestring(thelist.name,"groupsgranted","");
		str=ltype+"gr";
	} else if (thelist.name.indexOf("usersgranted")!= -1) {
		ltype=replacestring(thelist.name,"usersgranted","");
		str=ltype+"ur";
	} else
		str="";
	var i;

	for (i=0;i<thelist.options.length;i++) {
		v=thelist.options[i].value;
		if (mbplatform=="mac") {
			//patch voor explorer mac
			if (thelist.options[i].selected!=true)
				listvalue+=((listvalue=="")?v:","+v);
		} else
			thelist.options[i].selected=true;
		if (str!="" && accessrights[thelist.name] && accessrights[thelist.name][v]) {
			q+=","+v+"="+accessrights[thelist.name][v];
		}
	} 

	if (str!="") {
		document.forms[0][str].value=q.substring(1,q.length);
	}

	if (listvalue!="" || thelist.options.length==0) {
		 //patch voor explorer mac: niet geselecteerde meegeven via het form
		document.forms[0].action+=((document.forms[0].action.indexOf("?") == -1)?"?":"&")+thelist.name+"="+listvalue;
	}
	return true;
}

function moveoptions(from,to,movetype)
{
	var i=0;
	while(i<from.options.length) {
		if (from.options[i].selected) {
			if (movetype!=1) {//move of copy
				op= new Option();
				op.text=from.options[i].text;
				op.value=from.options[i].value;
				op.selected=true;
				to.options[to.options.length]=op;
			}
			if (movetype<2) 
				from.options[i]=null; //move of delete
			else i++; //copy
		} else
			i++;
	}
}

function upoptions(source)
{
	var i=0;
	while(i<source.options.length) {
		if (i>0 && source.options[i].selected) { //omwisselen met vorige
			op= new Option();
			op.text=source.options[i-1].text;
			op.value=source.options[i-1].value;
			op.selected=source.options[i-1].selected;
			source.options[i-1].text=source.options[i].text;
			source.options[i-1].value=source.options[i].value;
			source.options[i-1].selected=source.options[i].selected;

			source.options[i].text=op.text;
			source.options[i].value=op.value;
			source.options[i].selected=op.selected;
		}
		i=i+1;
	}
}

function downoptions(source)
{
	var i=source.options.length-1;
	while(i>=0) {
		if (i<source.options.length-1 && source.options[i].selected) { //omwisselen met volgende
			op= new Option();
			op.text=source.options[i+1].text;
			op.value=source.options[i+1].value;
			op.selected=source.options[i+1].selected;

			source.options[i+1].text=source.options[i].text;
			source.options[i+1].value=source.options[i].value;
			source.options[i+1].selected=source.options[i].selected;

			source.options[i].text=op.text;
			source.options[i].value=op.value;
			source.options[i].selected=op.selected;
		}
		i=i-1;
	}
}



