var mbplatform="";
var mbbrowser="";
var formchecks= new Array();
var formfields = new Array();
var doctabs = new Array();
var docbuttons = new Array();
var activetab = "";
var pagedirty=false;
var oldpopup,oldtarget,oldaction,oldmbAction;
docbuttons["menu"]=new Array();
docbuttons["menu"]["activebutton"]="";
docbuttons["userbutton"]=new Array();
docbuttons["userbutton"]["activebutton"]="";
docbuttons["actionbutton"]=new Array();
docbuttons["actionbutton"]["activebutton"]="";

function reloadstatus()
{
	if (top.mbstatus)
		top.mbstatus.document.forms[0].submit();
}

function findframe(name)
{
	var f;
	if (window.frames) {
		for (i=0;i<window.frames.length;i++) {
			if (window.frames[i].name==name) 
				f=window.frames[i];
			else if (window.frames[i].findframe)
				f=window.frames[i].findframe(name);
			if (f)
				return f;
		}
	}
	return 0;
}

function restrictchars(whichchars,errmsg)
{
	if (!whichchars) return;
	if (whichchars=="filesystem")
		whichchars=":\"/\\<>*?|";
	if (whichchars.indexOf(unescape("%"+window.event.keyCode.toString(16)))!= -1 || window.event.keyCode<32) {
		window.event.returnValue=false;
		alert(errmsg + "\n" + whichchars);
	}
/* deze tekens weigeren in veldnamen
:
"
/
\
<
>
*
?
|
. aan eind
spaties aan eind
tekens onder hex20
*/
}

function delayedalert(msg)
{
	setTimeout("alert('" + msg + "');",200);
}

function delayedtask(task,delay)
{
	setTimeout(task,delay);
}

function formfield(field,jscript,errmsg,tab,tabgroup)
{
	this.field=field;
	this.jscript=jscript;
	this.errormsg=errmsg;
	this.tab=tab;
	this.tabgroup=tabgroup;
}

function addformcheck(str,fld)
{
	var i;
	i=formchecks.length;
	formchecks[i]=str;
	formfields[i]=fld;
}

function checkform()
{
	var result=true;
	for (f=0;f<formchecks.length;f++) {
		if (formchecks[f]!='')
			result = result & eval(formchecks[f]);
		if (!result) break;
	}
	if (!result) {
		//show error
		if (formfields[f] && formfields[f]!='') {
			if (formfields[f].tab!='' && formfields[f].tabgroup!='') {
				button_click(formfields[f].tab,formfields[f].tabgroup);
				showtab(formfields[f].tab,formfields[f].tabgroup);
			}
			if (!document.forms[0][formfields[f].field].disabled)
				document.forms[0][formfields[f].field].focus();
			if (formfields[f].errormsg!='') alert(formfields[f].errormsg);
		}
	}
	return result;
}

function showtab(tabname,group) 
{
	var tabfield=doctabs[group][tabname]["focus"];
	tabbut="";
	for (t in doctabs[group]) {
		if (document.getElementById(["div_"+t]))
			document.getElementById(["div_"+t]).style.display=((t==tabname)?"":"none");
	}
	activetab=tabname;
	docbuttons[group]["activebutton"]=tabname;

	//patch voor explorer mac
	if (mbplatform=="MAC") {
		for (t in document.all.tags("SELECT")) {
			if (t=="length") continue;
			if (document.forms[0][t].tagName=="SELECT" && !document.forms[0][t].multiple) {
				document.forms[0][t].focus();
			}
		}
	}
	//end patch

	if (tabfield)
		eval("if ("+tabfield+" && !"+tabfield + ".disabled) "+tabfield+".focus()");
}


function resetfind(field,how,findval)
{
	var skip=true;
	var val='';

	for (i=findval.length-1;i>=0;i--) {
		if (skip && findval.substring(i,i+1)==' ') 
			continue;
		else
			skip=false;
		val=findval.substring(i,i+1)+val;
	}
	if (val=="none") val='';
	if (document.forms[0].mediaff1) {
		setselectoption(document.forms[0].mediaff1,field);
		setselectoption(document.forms[0].mediafh1,how);
		document.forms[0].mediafv1.value=val;
	} else {
		setselectoption(document.forms[0].rs_1ff1,field);
		setselectoption(document.forms[0].rs_1fh1,how);
		document.forms[0].rs_1fv1.value=val;
	}
	document.forms[0].submit();
}

function setselectoption(s,val)
{
	for (i=0;i<s.options.length;i++)
		if (s.options[i].value==val) {
			s.selectedIndex=i;
			s.options[i].selected=true;
			return;
		}
}

function button_click(menu,group)
{
	docbuttons[group]["activebutton"]=menu;
	if (group=="menu") 
		document.forms[0].mbMenuid.value=menu;
	else if (group=="tab")
		document.forms[0].mbTabid.value=menu;
	for (but in docbuttons[group]) {
		if (but=="activebutton") continue;
		for (img in docbuttons[group][but]) {
			if (but==menu) {
				document.images[img].src = docbuttons[group][but][img]["on"];
			} else {
				document.images[img].src = docbuttons[group][but][img]["off"];
			}
		}
	}
}

function button_change(menu,group,active)
{
	actbut=docbuttons[group]["activebutton"];
	if (menu==actbut) return;
	for (img in docbuttons[group][menu]) {
		document.images[img].src = ((active)?docbuttons[group][menu][img]["roll"]:docbuttons[group][menu][img]["off"]);
	}
}

function selectallrecs(selnum,active)
{
	eval("document.forms[0].rsSelected" + selnum + ".value='';");
	if (document.getElementById(["selrecs"+selnum])) {
		for (i=0;i < document.forms[0].elements.length; i++) {
			if (document.forms[0].elements[i].name=="selrecs"+selnum)
				document.forms[0].elements[i].checked=active;
		}
	}
}

function selectrec(selnum,recid,active)
{
	var j = 0;
	currecs=eval("document.forms[0].rsSelected" + selnum + ".value");
	newrecs="";
	curlist=currecs.split(",");
	for (i=0;i<curlist.length;i++) {
		if (curlist[i]!=recid && curlist[i]) {
			newrecs+=","+curlist[i];
			j++;
		}
	}
	if (active) {
		newrecs+=","+recid
		j++;
	} 
	window.status="Total " + j + " items selected";
	eval("document.forms[0].rsSelected" + selnum + ".value='" + newrecs.substring(1,newrecs.length) + "'");
}

function openFormPopup(newhref,newtarget,list)
{
	return openformpopup(newhref,newtarget,list);
}

function openformpopup(newhref,newtarget,list)
{
	var sourceframe=document.forms[0];
	if (list=='1') sourceframe=parent.mediaview.document.forms[0];
	if (newtarget=='') newtarget='_blank';
	with (sourceframe) {
		oldpopup=mbPopup.value;
		oldaction=action;
		oldtarget=target;
		oldmbAction=mbAction.value;
		mbPopup.value='1';
		action=newhref;
		target=newtarget;
		setTimeout("restorevalues();",500); //via timeout ivm explorer mac
		submit();
		if (list=='1') {
			target=oldtarget;
			action=oldaction;
			mbPopup.value=oldpopup;
			
		}
	}
}

function restorevalues()
{
	with (document.forms[0]) {
		mbPopup.value=oldpopup;
		action=oldaction;
		target=oldtarget;
		mbAction.value=oldmbAction;
	}
}

function fieldcheck(field,required,checktype,usercheck,errmsg,tab,tabgroup)
{
	var jscript='';
	switch(checktype) {
		case "":
			jscript=""; break;
		case "date":
			jscript="checkdate(document.forms[0]."+field+".value);";
			break;
		case "number":
			jscript="checknumber(document.forms[0]."+field+".value);";
			break;
		case "float":
			jscript="checkfloat(document.forms[0]."+field+".value);";
			break;
		case "chars":
			jscript="checkchars(document.forms[0]."+field+".value,'"+usercheck+"');";
			break;
		case "user":
			jscript=replacestring(replacestring(usercheck,"[",""),"]",".value");
	}
	if (required=='1') {
		jscript=field+".value.length>0"+((jscript!='')?" && "+jscript:"");
	}
	if (jscript!='') {
		jscript="with (document.forms[0]) {"+jscript+"}";
		addformcheck(jscript, new formfield(field,jscript,errmsg,tab,tabgroup));
	}
}


function checknumber(val)
{
	var ch;
	ch = parseInt(val);
	return !isNaN(ch) & ch.toString().length==val.length;
}

function checkfloat(val)
{
	var ch;
	val=replacestring(val,",","."); //moeten we dit nog opslaan in het field.value?
	ch = parseFloat(val);
	return !isNaN(ch) & ch.toString().length==val.length;
}

function checkchars(val,validchars)
{
	var ch;
	for (var i=0; i<val.length; i++) {
		if (validchars.indexOf(val.substring(i,i+1)) == -1) return false;
	}
	return true;
}

function checkdate(val)
{
	//split value in date separators and check (with date object?)
	return true;
}

function replacestring(fullS,oldS,newS) 
{// Replaces oldS with newS in the string fullS
	for (var i=0; i<fullS.length; i++) {
		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
	return fullS;
}



