/*&L(0,"*",21);*/

/* 
 * AD: i choosed groups to be shown at first config click. 
 * My choiche, i don't want use BCL vars and wait app to start (crappy).
 */
var a=new Array();
var i=0;
var sel;
var gGroup;

/* load group values */
a[i++]=&LSetup(1,"%lu",900,D);;a[i++]=&LSetup(1,"%lu",904,D);;a[i++]=&LSetup(1,"%lu",908,D);;a[i++]=&LSetup(1,"%lu",912,D);;
a[i++]=&LSetup(1,"%lu",916,D);;a[i++]=&LSetup(1,"%lu",920,D);;a[i++]=&LSetup(1,"%lu",924,D);;a[i++]=&LSetup(1,"%lu",928,D);;
a[i++]=&LSetup(1,"%lu",932,D);;a[i++]=&LSetup(1,"%lu",936,D);;a[i++]=&LSetup(1,"%lu",940,D);;a[i++]=&LSetup(1,"%lu",944,D);;
a[i++]=&LSetup(1,"%lu",948,D);;a[i++]=&LSetup(1,"%lu",952,D);;a[i++]=&LSetup(1,"%lu",956,D);;a[i++]=&LSetup(1,"%lu",960,D);;
a[i++]=&LSetup(1,"%lu",964,D);;a[i++]=&LSetup(1,"%lu",968,D);;a[i++]=&LSetup(1,"%lu",972,D);;a[i++]=&LSetup(1,"%lu",976,D);;
a[i++]=&LSetup(1,"%lu",980,D);;a[i++]=&LSetup(1,"%lu",984,D);;a[i++]=&LSetup(1,"%lu",988,D);;a[i++]=&LSetup(1,"%lu",992,D);;
a[i++]=&LSetup(1,"%lu",996,D);;a[i++]=&LSetup(1,"%lu",1000,D);;a[i++]=&LSetup(1,"%lu",1004,D);;a[i++]=&LSetup(1,"%lu",1008,D);;
a[i++]=&LSetup(1,"%lu",1012,D);;a[i++]=&LSetup(1,"%lu",1016,D);;a[i++]=&LSetup(1,"%lu",1020,D);;a[i++]=&LSetup(1,"%lu",1024,D);;

function qs(search_for) {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0  && search_for == parms[i].substring(0,pos)) {
			return parms[i].substring(pos+1);
		}
	}
	return "";
}
function CreateGroupPanel(g)
{
	var x=1,y,z,q,abyte,bit,id,addr;
	var txt="<table>";
	sel=0;
	
	/* store group for next usage */ 
	gGroup=g;
	this.panel = document.createElement('div');
	this.panel.setAttribute('id', 'panel');
	y=(g-1)*4;
	for(z=y;z<(y+4);++z){
		for(q=0;q<4;++q){
			abyte=(a[z]>>(q*8))&0xff;
			bit=7;
			addr=900+(z<<2)+q;
			if (q==0||q==2)txt+="<tr>";
			while(bit>=0){
				id=addr+'b'+bit;
				txt+="<td class='s'>";
				txt+="<input id="+id+" OnChange=m(this) type=checkbox value='1'";
				if(abyte&(1<<bit--)) {sel++; txt+="checked >";} else txt+=">";
				txt+="</input><br>"+(x++);
			}
		}
		txt+="<input name=D"+(900+(z<<2))+" value="+a[z]+" type=hidden />";
	}
	// ICPaging need "selected clients"
	txt+="<input id='sel' name=B"+(858+(g-1))+" value="+sel+" type=hidden />";
	txt+="</table>";
	this.panel.innerHTML=txt;
	document.getElementById('matrix').appendChild(this.panel);
}
function SetupGroupMatrix()
{
	var page=qs('page');
	var grp=parseInt(qs('group'),10);
	
	if (!grp) { // after apply
		grp =parent.mnu.document.getElementById('slot1').value;
	}
	else
	{
		parent.mnu.document.getElementById('slot1').value = grp;
		parent.mnu.document.getElementById('slot2').value = page;
	}
	document.getElementById('title').innerHTML="GROUP <span style='background-color:#cccccc; color:red'>&nbsp;"+grp+"&nbsp;</span> SETUP";
	CreateGroupPanel(grp);
}
function ChkUnchkAll (check)
{
	var q,x,i=0,val;
	
	x=((gGroup-1)<<2);
	for (;i<document.forms['grpfrm'].elements.length;i++)
	{
		document.forms['grpfrm'].elements[i].checked=(check==0)?false:true;
	}
	q=x;
	val=check?0xffffffff:0;
	for (i=0;i<4;++i,q++) a[q]=val;
	q=(900+(x<<2));
	for (i=0;i<4;++i,q+=4) document.getElementsByName('D'+q)[0].value=check?0xffffffff:0;
	
	// update ICPaging selected clients
	sel=check?128:0;
	document.getElementById('sel').value=sel;
}
function m(b)
{
	var cid=(b.id);
	var n=cid.split("b"); 
	var idx=n[0];
	var bit=n[1];
	var apos=Math.floor((idx-900)/4);
	var boff=Math.floor((idx-900)%4);
	var shift=(boff<<3)+parseInt(bit,10);
	var mask =(1<<shift);

	if (b.checked==true) {sel++;a[apos]|=mask;} else {sel--;a[apos]&=(~mask);}
	// update ICPaging selected clients
	document.getElementById('sel').value=sel;

	var hf = document.getElementsByName('D'+(900+(apos<<2)))[0];
	hf.value = a[apos];
}
function exit()
{
	var page=qs('page');
	// no query, after apply
	if (page=="") page=parent.mnu.document.getElementById('slot2').value;
	// store section to re-open at uiadvanced reload
	parent.parent.menu_frame.document.getElementById('slot1').value=5;
	// setup advanced subsection to open (ignored on basic)
	parent.location.href='uif'+page+'.html';
}