function blockError() 
{
	//if (opener) window.close();
	return true;
}
//window.onerror = blockError;


function maxLines(naam,aantal,funcerror){
	var mbObject;
	var pos=-1;
	var countrules = 0;
	if (document.forms[0][naam]) {
		mbObject=document.forms[0][naam];
	} else if (document.getElementById('ufields_' + naam)) {
		mbObject=document.getElementById('ufields_' + naam);
	} else {return false;}
	
	if (mbObject.value.indexOf("\r")>0) {
		pos = mbObject.value.indexOf("\r");postype='CrLf';
	} else { pos = mbObject.value.indexOf("\n");}
	
	while ( pos != -1 ) {
		countrules++;
		if (countrules==aantal) {
			if (funcerror) {
				alert(funcerror);
			} else {
				alert('max lines:' + aantal);
			}

			mbObject.value=mbObject.value.substring(0,pos);return;

		};
		
		if (postype=='CrLf') {
			pos = mbObject.value.indexOf("\r",pos+2);
		} else  {
			pos = mbObject.value.indexOf("\n",pos+1);
		}

	}
						
}

function maxChars(naam,aantal,funcerror){
	var mbObject;
	if (document.forms[0][naam]) {
		mbObject=document.forms[0][naam];
	} else if (document.getElementById('ufields_' + naam)) {
		mbObject=document.getElementById('ufields_' + naam);
	} else { return false; }
	
	if (mbObject.value.replace(/\r|\n|\r\n/g, "").length > aantal) {
		if (funcerror) {alert(funcerror);} else {alert('max characters:' + aantal);}
		for (x=mbObject.value.length;mbObject.value.replace(/\r|\n|\r\n/g, "").length > aantal;x--) {
			mbObject.value=mbObject.value.substring(0,x);
		}
	}
						
}

function correctPNG() {
	//alert('Aanroepen correctPNG');
	var blankImgUrl = "images/blank.gif";
	var browserVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);
	if ((browserVersion >= 5.5) && (browserVersion < 7) && (document.body.filters)){
		
		
		var imgs = document.getElementsByTagName("IMG");
		for (var idx = 0; idx < imgs.length; idx++){
			if (imgs[idx].src.indexOf('.png') > 0){
				var w = imgs[idx].offsetWidth;
				var h = imgs[idx].offsetHeight;
				var src = imgs[idx].src;
				imgs[idx].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enable=true, sizingMethod=scale src='" + src + "')";
				imgs[idx].style.width = w;
				imgs[idx].style.height = h;
				imgs[idx].src = blankImgUrl;
			}
		}
		
	}
}


var mbplatform="";
var mbbrowser="";
var formchecks= new Array();
var formfields = new Array();
var doctabs = new Array();
var docbuttons = new Array();
var activetab = "";
var lastgroup="";
var lastmenu="";
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 mbSubmit(result)
		{
		//kan overruled worden in je eigen script
			if (!window.mbPageLoaded) return false;

			var dosubmit=false;
			if (result==undefined) {
				result=true;
				dosubmit=true;
			}

			if (result) {
				//alle buttons disablen
				var b=document.getElementsByTagName("INPUT");
				for (i=0;i<b.length;i++) {
					if (b[i].type && b[i].type=="button")
						b[i].disabled=true;
				}
				var b=document.getElementsByTagName("IMG");
				for (i=0;i<b.length;i++) {
					b[i].disabled=true;
					if (b[i].parentElement && b[i].parentElement.tagName=="A" && b[i].parentElement.onclick)
						b[i].parentElement.onclick="return false";
				}
				var b=document.getElementsByTagName("SELECT");
				for (i=0;i<b.length;i++) {
					b[i].onmousedown="stopEvent();"
				}
				setscrolltop();

				//setTimeout("showBusyMessage();",2000);
			}

			if (dosubmit) { 
				submitbusy=true; 
				mbPageLoaded=false;
				document.forms[0].submit();	
			}
			return result;
		}

if (!window.formSubmit)
	window.formSubmit=mbSubmit;
else {
	window.tempSubmit=formSubmit;
	window.formSubmit=function(result) {tempSubmit(result); return mbSubmit(result);}
}


function RefreshPage(queueid) {
	if (queueid && parent.refreshpage) {
			parent.refreshpage.location.href='refresh.asp?queueid=' + queueid;
	}
	else if (parent.mainbrowser && parent.mainbrowser.mainframeset && parent.mainbrowser.mainframeset.menu && parent.mainbrowser.mainframeset.menu.mbstatus) {
		//we hebben een mainbrowser, mbstatus verversen
		setTimeout("parent.mainbrowser.mainframeset.menu.mbstatus.location.href='mbstatus.asp?queueid=" + queueid  + "';",1500)
	}

}

if (typeof(loadScript) != "function"){
	function loadScript(url){
		var head          = document.getElementsByTagName("HEAD")[0];
		var loadedScripts = head.getElementsByTagName("SCRIPT");
		var script        = document.createElement("SCRIPT");
		for (var idx = 0; idx < loadedScripts.length; idx++){
			if (loadedScripts[idx].src && loadedScripts[idx].src == url)
			return;
		}
		script.type = "text/javascript";
		script.src  = url;
		if (loadedScripts.length > 0)
			head.insertBefore(script, loadedScripts[0]);
		else
			head.appendChild(script);
			
	}	
}



loadScript('AjaxRequest.js');

function setHourglass()
{
	document.body.style.cursor="wait";
}

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 validchars(whichchars,errmsg)
{
	if (!whichchars) return;

	if (whichchars.indexOf(unescape("%"+window.event.keyCode.toString(16))) == -1 || window.event.keyCode<32) {
		window.event.returnValue=false;
		alert(errmsg + "\n" + whichchars);
	}

}


function restrictchars(whichchars,errmsg)
{
	if (!whichchars) whichchars="\"";
	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]!='')
			try {result = result && eval(formchecks[f]);} catch(e) {};
			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].type!='hidden')
				try {document.forms[0][formfields[f].field].focus();} catch(e) {};
			if (formfields[f].errormsg!='') alert(formfields[f].errormsg);
		}
	}

	if (result && window.formSubmit)
		result=window.formSubmit(result);
	return result;
}

function checkformProof()
{
	var result=true;
	
	for (f=0;f<formchecks.length;f++) {
		if (formchecks[f]!='')
			try {result = result && eval(formchecks[f]);} catch(e) {};
		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].type!='hidden')
				try {document.forms[0][formfields[f].field].focus();} catch(e) {};
			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" && mbbrowser.indexOf('MSIE',0)!=-1) {
		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)
{
	if (lastmenu==menu && lastgroup==group) return;
	
	docbuttons[group]["activebutton"]=menu;
	if (group=="menu") 
		document.forms[0].mbMenuid.value=menu;
	else if (group=="tab")
		document.forms[0].mbTabid.value=menu;
		
	if (lastmenu!="") {
		if (docbuttons[lastgroup] && docbuttons[lastgroup][lastmenu]) {
			for (img in docbuttons[lastgroup][lastmenu]) {
				document.images[img].src = docbuttons[lastgroup][lastmenu][img]["off"];
			}
		}
		//achtergrond veranderen
		if (document.getElementById([lastmenu])) {
			if (lastmenu.substr(0,3)!="mb_")
				document.getElementById([lastmenu]).className="";
		}
	}

	if (docbuttons[group] && docbuttons[group][menu]) {
		for (img in docbuttons[group][menu]) {
			document.images[img].src = docbuttons[group][menu][img]["on"];
		}
		//achtergrond veranderen
		if (document.getElementById([menu])) {
			if (menu.substr(0,3)!="mb_")
				document.getElementById([menu]).className="selectcolor";
		}
	}

/*
	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"];
			}
		}		
		//achtergrond veranderen
		if (document.getElementById([but])) {
			if (but.substr(0,3)!="mb_")
			document.getElementById([but]).className=((but==menu)?"selectcolor":"");
//			document.getElementById([but]).style.color=((but==menu)?"FFFFFF":"");
		}
	}
*/

	if (menu && document.forms[0].activemenu)
		document.forms[0].activemenu.value=menu;
		
	lastmenu=menu;
	lastgroup=group;
}

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;
		mbCmd.value="";
	}
}

function fieldcheck(field,required,checktype,usercheck,errmsg,tab,tabgroup)
{
//alert(field);
	var jscript='';
	switch(checktype) {
		case "":
			jscript=""; break;
		case "date":
			jscript="checkdate(elements['"+field+"'].value)";
			break;
		case "number":
			jscript="checknumber(elements['"+field+"'].value)";
			break;
		case "float":
			jscript="checkfloat(elements['"+field+"'].value,'"+field+"')";
			break;
		case "chars":
			jscript="checkFormatString(elements['"+field+"'].value,'"+usercheck+"')";
			break;
		case "user":
			jscript=replacestring(replacestring(usercheck,"[",""),"]",".value");
		case "script":
			jscript=usercheck;
	}

	if (required=='1') {
		jscript="elements['"+field+"'].value.length>0"+((jscript!='')?" && "+jscript:"");
	} else if (jscript!='') {
		jscript="elements['"+field+"'].value.length==0 || ("+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,field)
{
	var ch,valb;
	valb='';
	whichchars="1234567890.,-";
	for (var i=0; i<val.length; i++) {
		if (whichchars.indexOf(val.substring(i,i+1)) >=0 ) {
			valb = valb + val.substring(i,i+1);
		}
		else {
			return !isNaN();
		}
	}
	val=valb;
	val=replacestring(val,",","."); //moeten we dit nog opslaan in het field.value?
	if (document.getElementById(field)) {
		document.getElementById(field).value=val;
	}
	else {
		document.forms[0].elements[field].value=val;
	}
	ch = parseFloat(val);
	return !isNaN(ch);
}



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;
}


function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function checkFormatString(val, inputformat)
{
	var formats = inputformat.split(";");
	for (var i=0; i<formats.length; i++) {
		var thisformat = trim(formats[i]);
		if (thisformat=="") 
			continue;
		var regformat = (thisformat.indexOf("!")==0)?thisformat.substring(1):thisformat;
		
		if (regformat.charAt(0)!="/" || regformat.charAt(regformat.length-1)!="/") {
			//vertalen, is geen regexp
			regformat = regformat.replace(/\\/g,"\\\\");
			regformat = regformat.replace(/\+/g,"\\+");
			regformat = regformat.replace(/\(/g,"\\(");
			regformat = regformat.replace(/\)/g,"\\)");
			regformat = regformat.replace(/\[/g,"\\[");
			regformat = regformat.replace(/\]/g,"\\]");
			regformat = regformat.replace(/\{/g,"\\{");
			regformat = regformat.replace(/\}/g,"\\}");
			regformat = regformat.replace(/\^/g,"\\^");
			regformat = regformat.replace(/\$/g,"\\$");
			regformat = regformat.replace(/\//g,"\\/");
			regformat = regformat.replace(/\./g,"\\.");
			regformat = regformat.replace(/\*/g,".*");
			regformat = regformat.replace(/\?/g,".");
			regformat = regformat.replace(/a/g,"[a-z]");
			regformat = regformat.replace(/A/g,"[A-Z]");
			regformat = regformat.replace(/#/g,"[0-9]");
		} else {
			regformat = regformat.substring(1, regformat.length-1);
		}

		var reg = new RegExp("^" + regformat + "$");
		var m = reg.exec(val);
		var result = m!=null;
		if (thisformat.indexOf("!")==0) {
			result = !result;
		}
/*		
		var s = "No match";
		if (m) {
			s = "Match at position " + m.index + ":\n";
			for (var j = 0; j < m.length; j++) {
			  s = s + m[j] + "\n";
			}
		}
		alert(regformat + ": " + s + "\nresult:" + result);
*/		
		if (result)
			return true;
	}
	
	return false;
}

function checkFormatStringOUD(val,inputformat)
{
	var wildcard=false;
	var result=false;
	var i,j,k,ch,chform;
	var thisformat;
	var formats;

	formats=inputformat.split(";");
	for (i=0;i<formats.length;i++) {
		thisformat=formats[i];
		result=true;
		k=(thisformat.charAt(0)=='!')?1:0;
		for (j=0;j<val.length && k<thisformat.length;j++) {
			ch=val.charAt(j);
			chform=thisformat.charAt(k);
			if (chform=='*') {
				wildcard=true;
				if (++k==thisformat.length) {
					j=val.length
					break;
				}
				chform=thisformat.charAt(k);
			}
			if (!checkCharFormat(ch,chform)) {
				if (!wildcard) {
					result=false;
					break;
				}
			} else {
				wildcard=false;
				k++;
			}
		}
		if (k<thisformat.length || j<val.length)
			result=false;
		if (result)
			break;
	}
	if (thisformat.charAt(0)=='!')
		return !result;
	else
		return result;
}

function checkCharFormat(ch,format)
{
	switch (format) {
		case '#':
			return (ch>='0' && ch<='9');
		case 'A':
			return (ch>='A' && ch<='Z');
		case 'a':
			return (ch>='A' && ch<='z');
		case '?':
			return true;
		case '*':
			return true;
		default:
			return (ch==format);
	}
}

function holdPosition()
{
	if (document.forms[0].mbDivScrolltop && document.getElementById('showmedia') && document.getElementById('showmedia').scrollTop>0) document.forms[0].mbDivScrolltop.value=document.getElementById('showmedia').scrollTop;
}

function ShowScrollbars(whichdiv)
{
	if (!whichdiv) {whichdiv='showmedia';}
	
	//alert(whichdiv);
	if (document.getElementById(whichdiv)) {
		//alert(document.getElementById('showmedia').scrollTop);
		var frameHeight=0;
		
		if (parent.document.getElementById('mediaview')) parent.document.getElementById('mediaview').scrolling="no";

		if (self.innerWidth) {
		 frameHeight = self.innerHeight;
		 }
		 else if (document.documentElement && document.documentElement.clientWidth) {
		 	frameHeight = document.documentElement.clientHeight;
		 }
		 else if (document.body) {
		 	frameHeight = document.body.clientHeight;
		 }
		//alert(document.getElementById('showheader').offsetHeight);
		var showheaderHeight=0;
		var showfooterHeight=0;
		var showThumbsHorHeight=0;
		if (document.getElementById('showheader')) showheaderHeight=(document.getElementById('showheader').offsetHeight);
		if (document.getElementById('showfooter')) showfooterHeight=(document.getElementById('showfooter').offsetHeight);
		if (document.getElementById('showThumbsHor')) showThumbsHorHeight=(document.getElementById('showThumbsHor').offsetHeight);
		//alert(frameHeight);

		if (whichdiv=='showblockeditor') {
			document.getElementById(whichdiv).style.height=frameHeight-showheaderHeight-showfooterHeight-showThumbsHorHeight-14;
		}
		else if ((frameHeight-showheaderHeight-showfooterHeight)>0) {
			document.getElementById(whichdiv).style.height=frameHeight-showheaderHeight-showfooterHeight;
		}
	}
}


function loadUrl(href,parameterlist,successcallback,errorcallback,sync)
{
	return AjaxRequest.get ( { 	'async'        		: (sync)?false:true,
											'method'       	: "GET",
											'parameters'	: (parameterlist)?parameterlist:null,
											'url'          		: href,
											'onSuccess'	: (successcallback)?successcallback:function (){},
											'onError'		: (errorcallback)?errorcallback:function (){}
										}
									);
}

function getXML(tagname,xmlstring)
{
//TODO: dit gaat fout bij result uit html pagina frame...
	if (!xmlstring || !tagname) return "";
	var start=xmlstring.indexOf("<"+tagname+">");
	var eind=xmlstring.indexOf("</" + tagname + ">");
	if (start>-1 && eind>-1) 
		return xmlstring.substring(start+tagname.length+2,eind).replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
}
