function blockError() 
{
	//if (opener) window.close();
	return true;
}
//window.onerror = blockError;


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 submitbusy=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"]="";

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 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)
	}

}

function setHourglass()
{
	document.body.style.cursor="wait";
}

function reloadstatus()
{
	if (top.mbstatus) {
		alert('hier');
		//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,evt)
{
	if(!evt) var evt = window.event;
	if (!whichchars) return;
	if (whichchars=="filesystem")
		whichchars=":\"/\\<>*?|";
  charCode = (evt.which) ? evt.which : evt.keyCode;
	if (whichchars.indexOf(String.fromCharCode(charCode)) != -1 || charCode<32) {
		alert(errmsg + "\n" + whichchars);
		return false;
	}
/* 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 setfocus(veld)
{
	if (document.forms[0][veld] && 
			!document.forms[0][veld].disabled && 
			document.forms[0][veld].type!='hidden')
		document.forms[0][veld].focus();
}

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].type!='hidden')
				document.forms[0][formfields[f].field].focus();
			if (formfields[f].errormsg!='') alert(formfields[f].errormsg);
		}
	}
	if (result && window.formSubmit)
		result=window.formSubmit(result);
	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 (window.menuClick)
		menuClick(menu);
	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)
{
	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+"'])";
			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)
{
	var ch;
	val=replacestring(val,",","."); //moeten we dit nog opslaan in het field.value?
	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 checkFormatString(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] && document.forms[0].mbDivScrolltop && document.getElementById('showmedia') && document.getElementById('showmedia').scrollTop>0) document.forms[0].mbDivScrolltop.value=document.getElementById('showmedia').scrollTop;
}


function ShowScrollbars()
{
	if (document.getElementById('showheader') && document.getElementById('showmedia')) {
		//alert(document.getElementById('showmedia').scrollTop);
		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);
		viewpropsHeight=(document.getElementById('showheader').offsetHeight);
		if ((frameHeight-viewpropsHeight)>0) document.getElementById('showmedia').style.height=frameHeight-viewpropsHeight;
	}
}

function Sjoerd()
{
}

function stripTags(htmlContent)
{
  var strippedText = htmlContent;
  strippedText = strippedText.replace(/<(br ?\/|\/?p)>/ig, "\n");
  strippedText = strippedText.replace(/(<([^>]+)>)/ig," ");
  strippedText = strippedText.replace(/&#([0-9]+);/g, function(c){var code=c.replace(/(&#|;)/g,"");return String.fromCharCode(code)});
  strippedText = strippedText.replace(/( {2,}|^\s+|\s+$)/g, "");
  strippedText = strippedText.replace(/\n/g, "<br />");
  return strippedText;
}

function removeEmptyTags(type, value)
{
  switch(type)
  {
    case "insert_to_editor":
	case "get_from_editor":
	  value = value.replace(/([\n\r]|<p>(\s|&#160;)*<\/p>)+/gi, '');
	  value = value.replace(/\s+/g, ' ');
  	  value = value.replace(/&#60;(\/?.*?)&#62;/g, "<$1>").replace(/&#38;/g, "&");
	  break;
  }
  return value;
}


function addHTMLTags(element_id, html, body)
{
  return "<html>" + html.replace(/&#60;(\/?.*?)&#62;/g, "<$1>").replace(/&#38;/g, "&") + "</html>";
}



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');
}



