if (typeof(MAXRECORDS)=="undefined") var MAXRECORDS=25;
if (typeof(LISTVIEWSIZE)=="undefined") var LISTVIEWSIZE="170px";
if (typeof(AUTOSHOWNOTES)=="undefined") var AUTOSHOWNOTES=true;

function showTab (tab) {
	var block = $('tab_'+tab);
	var li = $('tab_li_'+tab);
	if (block) {
		var container = block.parentNode;
		var ul = li.parentNode;

		// tabs verbergen
		for (var c=0; c<container.childNodes.length; c++) {
			var child = container.childNodes[c];
			if (child && child.tagName && child.tagName.toLowerCase() == 'div') {
				child.style.display = "none";
			}
		}

		// links als niet-actief bestempelen
		for (var c=0; c<ul.childNodes.length; c++) {
			var child = ul.childNodes[c];
			if (child && child.tagName && child.tagName.toLowerCase() == 'li') {
				child.className = "";
			}
		}

		li.className = "active";
		block.style.display = "";

		var tabcontainers = document.getElementsByClassName("tabcontainer");
		for (var t=0; t<tabcontainers.length; t++) {
			tabcontainers[t].style.display = document.all ? "inline" : "block";
		}
		var tabs = document.getElementsByClassName("tabs");
		for (var t=0; t<tabs.length; t++) {
			tabs[t].style.display = document.all ? "inline" : "block";
		}

	}
}

function init_tabs () {
	// Bij de li een link toevoegen en de containers verbergen
	var tabs = document.getElementsByClassName("tabs");
	for (var t=0; t<tabs.length; t++) {
		var lis = tabs[t].getElementsByTagName("li");
		for (var l=0; l<lis.length; l++) {
			// link toevoegen
			var li = lis[l];
			var text = li.innerHTML.replace(/ /,"");
			li.id = "tab_li_" + text;
			li.innerHTML = "<a href='javascript:showTab(\"" + text + "\");'>" + li.innerHTML + "</a>";
			li.onmouseover = function() {
				if (!/active/.test(this.className)) {
					this.className = "hover";
				}
			}
			li.onmouseout = function() {
				if (!/active/.test(this.className)) {
					this.className = "";
				}
			}

			// container vergergen
			var tab = $('tab_' + text);
			if (tab) {
				tab.style.display = "none";
				if (tab.className!="") {
					tab.className = tab.className + " tab";
				} else {
					tab.className = "tab";
				}
			}
		}
	}
}

function findObjectPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function renderdropdown(e, ctrl,strUrl) {
	if (!e) var evt = event; else var evt=e;
	var strVal=ctrl.value;
	if (ctrl.boundnode) {
		var strBoundValue=GetNodeValue(GetBoundNode(ctrl));
	} else {
		var strBoundValue="";
	}
	if (strVal!="" && strVal != strBoundValue) {
		if (!$("dropdown")) new Insertion.Top(document.body, '<div id="dropdown" class="dropdown">&nbsp;</div>');
		if ($("dropdown").targetctrl!=ctrl) {
			$("dropdown").targetctrl = ctrl;
			var arPos = findObjectPos(ctrl);
			$("dropdown").style.left = parseInt(arPos[0]);
			$("dropdown").style.top = parseInt(arPos[1])+20;
			if (ctrl.size) $("dropdown").style.width=ctrl.size * 7; else $("dropdown").style.width="250px;"
			$("dropdown").style.display="block";
		}
		switch (evt.keyCode) {
		case 38: //up
			var currow=$A(document.getElementsByClassName("dropdowncurrent"));
			if (currow.length==0) {
				currow=$A(document.getElementsByClassName("dropdownitem"));
				currow[currow.length-1].className="dropdowncurrent";
			} else {
				currow=currow[0];
				currow.className="dropdownitem";
				var arRows = $A(document.getElementsByClassName("dropdownitem"));
				for (var r=arRows.length; r>0; r--) {
					if (arRows[r]==currow) {
						arRows[r-1].className="dropdowncurrent"; 
						r=0;
					} 
				}
			} 
			break;
		case 40: //down
			var currow=$A(document.getElementsByClassName("dropdowncurrent"));
			if (currow.length==0) {
				currow=$A(document.getElementsByClassName("dropdownitem"))[0];
				currow.className="dropdowncurrent";
			} else {
				currow=currow[0];
				currow.className="dropdownitem";
				var arRows = $A(document.getElementsByClassName("dropdownitem"));
				for (var r=0; r<arRows.length-1; r++) {
					if (arRows[r]==currow) {
						arRows[r+1].className="dropdowncurrent"; 
						r=arRows.length;
					} 
				}
			} 
			break;
		default:
			var ud = new Ajax.Updater(
			$("dropdown"),
			strUrl,
			{
				method: 'get', 
				parameters: "find="+strVal
			});
		}
	}
}

function dropdownselect (strID, strName) {
	if ($("dropdown").targetctrl) {
		if ($("dropdown").targetctrl.boundnode) {
			bind_SetLookupValue($("dropdown").targetctrl,strID,strName);
		} else {
			var strFieldName = $("dropdown").targetctrl.id;
			if ($(strFieldName+"id").tagName) { 
				$(strFieldName+"id").value=strID;
				$("dropdown").targetctrl.value=strName;
			} else {
				$("dropdown").targetctrl.value=strID;
			}
			ValidateControl($("dropdown").targetctrl);
		}
		$("dropdown").targetctrl=null;
	}
	$("dropdown").style.display="none";
}

function checkdropdown(ctrl, bDisallowNull) {
	var currow=$A(document.getElementsByClassName("dropdowncurrent"));
	if (ctrl.value=="" && !bDisallowNull) {
		bind_SetLookupValue(ctrl,"","");
		if ($("dropdown")) {
			$("dropdown").style.display="none";
			$("dropdown").targetctrl=null;
		}
	} else if (currow.length>0) {
		currow[0].onclick();
	} else if (!bind_QuickCheckControl(ctrl)) {
		ctrl.value=ctrl.getAttribute("backupvalue");
		if ($("dropdown")) {
			$("dropdown").style.display="none";
			$("dropdown").targetctrl=null;
		}
	}
}

function showZoekPopup (ctrl, strURL) {
	var win = window.open(strURL, "zoekpopup", "toolbar=0,width=800, height=600, top=100, left=100,menubar=1,resizable=1");
	win.focus();
	window.setLookupValue = function(strID, strNaam) { bind_SetLookupValue(ctrl, strID,strNaam);}
}
/*****************************/
/* Begin stuk over calendars */
/*****************************/
// Globale variabelen voor de calendar-functies
var Maanden = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'];
var MaandenKort = ['Jan','Feb','Mrt','Apr','Mei','Jun','Jul','Aug','Sep','Okt','Nov','Dec'];
var Dagen = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag']
var DagenKort = ['Zo','Ma','Di','Wo','Do','Vr','Za']

function showCalendar(ctrl, fieldDate, strOnClick) {
	if ($("calendar") && $("calendar").style.display=="block")
    $("calendar").style.display="none";
  else {
  	if (!$("calendar")) new Insertion.Top(document.body, '<div class="popupshadow"><div id="calendar" class="calendar">&nbsp;</div></div>');

		var arPos = findObjectPos(ctrl);
		$("calendar").parentNode.style.left = parseInt(arPos[0])-95;
		$("calendar").parentNode.style.top = parseInt(arPos[1])+26;
    $("calendar").style.width="120px";
    $("calendar").style.display="block";
    
    var myDate;
    if (fieldDate.value == "")
      myDate = new Date();
    else
      myDate = new Date(fieldDate.value.substr(6,4),Number(fieldDate.value.substr(3,2))-1,fieldDate.value.substr(0,2));

    strViewMonth = "" + myDate.getFullYear() + ((myDate.getMonth()<9)?"0"+(myDate.getMonth()+1):(myDate.getMonth()+1));

  	DrawCalendar($("calendar"),fieldDate, strViewMonth, strOnClick);
  }
}

function DrawCalendar(divCalendar, fieldDate, strViewMonth, strOnClick) {
  var myDate, tmpDate, strDate, displayDate, curDate;
  var strTDClass, strSelected;
  var myCalendar;
  var firstDay, preDays, monthDays;
  
  var viewMonth = new Date(strViewMonth.substr(0,4),Number(strViewMonth.substr(4,2))-1);
  
  if (fieldDate.value == "")
    displayDate = new Date();
  else
    displayDate = new Date(fieldDate.value.substr(6,4),Number(fieldDate.value.substr(3,2))-1,fieldDate.value.substr(0,2));

  curDate = new Date();
  
	firstDay = new Date(viewMonth.getFullYear(),viewMonth.getMonth(),1);
	preDays = firstDay.getDay() - 1;
	if (preDays<0) preDays=6;
	monthDays = new Date((viewMonth.getMonth()<11?viewMonth.getFullYear():viewMonth.getFullYear()+1),(viewMonth.getMonth()<11?viewMonth.getMonth()+1:1),0).getDate();
	
	var strNextMonth = strViewMonth.substr(0,4) + ((Number(strViewMonth.substr(4,2))+1)<10?"0"+(Number(strViewMonth.substr(4,2))+1):(Number(strViewMonth.substr(4,2))+1));
	var strPrevMonth = strViewMonth.substr(0,4) + ((Number(strViewMonth.substr(4,2))-1)<10?"0"+(Number(strViewMonth.substr(4,2))-1):(Number(strViewMonth.substr(4,2))-1));

  myCalendar  = "<table class=calendarTitle width=100% cellspacing=0 cellpadding=0>";
  myCalendar += "  <tr>";
  myCalendar += "    <td style='width:16px'><img src='../images/previous-16.gif' class=button onClick='DrawCalendar($(\"" + divCalendar.id + "\"),$(\"" + fieldDate.id + "\"),\"" + strPrevMonth + "\",\"" + strOnClick + "\");'></td><td style='width:*;'>" + MaandenKort[viewMonth.getMonth()] + " " + viewMonth.getFullYear()+ "</td><td style='width:16px'><img src='../images/next-16.gif' class=button onClick='DrawCalendar($(\"" + divCalendar.id + "\"),$(\"" + fieldDate.id + "\"),\"" + strNextMonth + "\",\"" + strOnClick + "\");'></td>";
  myCalendar += "  </tr>";
  myCalendar += " </table>";
  myCalendar += "<table class='calendarTable' width=100% cellspacing=1 cellpadding=1>";
  myCalendar += "  <tr>";
  myCalendar += "    <th class='calendarTableTH'>M</th><th class='calendarTableTH'>D</th><th class='calendarTableTH'>W</th><th class='calendarTableTH'>D</th><th class='calendarTableTH'>V</th><th class='calendarTableTH'>Z</th><th class='calendarTableTH'>Z</th>";
  myCalendar += "  </tr>";
  myCalendar += "  <tr>";
  for (var i=0; i<preDays; i++) {
    myCalendar += "   <td class='calendarTableWeekday'>&nbsp;</td>";
  }

  for (var i=1; i<=monthDays; i++) {
    tmpDate = new Date(viewMonth.getFullYear(),viewMonth.getMonth(),i);
    if (tmpDate.getDay() == 1) {
      myCalendar += "  </tr>";
      myCalendar += "  <tr>";
    }
    
    strDate  = "";
    strDate += ((tmpDate.getDate()<10)?"0"+tmpDate.getDate():tmpDate.getDate());
    strDate += "-" + ((tmpDate.getMonth()<9)?"0"+(tmpDate.getMonth()+1):(tmpDate.getMonth()+1));
    strDate += "-" + tmpDate.getFullYear();

    
    if (tmpDate.getDay() == 0 || tmpDate.getDay() == 6)
      strTDClass = "calendarTableWeekendday";
    else
      strTDClass = "calendarTableWeekday";

    if (tmpDate.getFullYear() == displayDate.getFullYear() && tmpDate.getMonth() == displayDate.getMonth() && tmpDate.getDate() == displayDate.getDate())
      strSelected = "style='border: 1px solid red;";
    else
      strSelected = "style='";

    if (tmpDate.getFullYear() == curDate.getFullYear() && tmpDate.getMonth() == curDate.getMonth() && tmpDate.getDate() == curDate.getDate())
      strSelected += " font-weight: bold;' ";
    else
      strSelected += "' ";
      
    myCalendar += "   <td class='" + strTDClass + "' " + strSelected + "onClick=calendarSelect('"+divCalendar.id+"','"+fieldDate.id+"','"+strDate+"','"+strOnClick+"')>" + i +"</td>";
      
    if (i == monthDays && tmpDate.getDay() > 0) {
      for (var j = tmpDate.getDay(); j<=6; j++)
        myCalendar += "   <td class='calendarTableWeekday'>&nbsp;</td>";
    }
  }
  myCalendar += "  </tr>";
  myCalendar += "</table>";
  
  //alert (myCalendar);
  divCalendar.innerHTML = myCalendar;
}
function calendarSelect (calendarid, fieldid, strdate, stronclick) {
	$(fieldid).value=strdate;
	$(fieldid).select();
	$(calendarid).style.display="none";
	if (stronclick) eval(stronclick);
}
/*****************************/
/* Eind stuk over calendars  */
/*****************************/


//listform functions
function GetFilter (frm) {
	var strFilter = "";
	if (frm) {
		var strFormat;
		Form.getElements($(frm)).each(function(item) {
			if (item.value!="" && item.name !="") {
				strFormat=item.getAttribute("format");
				strFilter += "<"+item.name+">" + bind_stringDeformat(item.value,strFormat) + "</"+item.name+">"; 
			}
		});
	}
	strFilter = "<root><filter>" + strFilter + "</filter>"+"<sorting><column>"+sortColumn+"</column></sorting><pageinfo><pagenumber>"+targetPage+"</pagenumber><pagesize>"+pageSize+"</pagesize></pageinfo></root>";
	return strFilter;
}

var targetPage=1;
function gotopage(nPage) {
	targetPage=nPage;
	ApplyFilter($("listform"),"", true);
}

var pageSize=32;
function setPageSize(newSize) { pageSize=newSize; }

var sortColumn="";
function sort(strColumn) {
	if (strColumn) {
		if (sortColumn==strColumn) sortColumn += " DESC"; else sortColumn = strColumn;
	}
	ApplyFilter($("listform"),"");
}

function selectrow(row, strIndex) {
	var rownode = selectNodes(row.parentNode.parentNode.getAttribute("boundpath"),MainDoc)[strIndex];
	var allrows=$A(document.getElementsByClassName("oddrowselected"));
	allrows.each(function(node) {node.className="oddrow";});
	allrows=$A(document.getElementsByClassName("evenrowselected"));
	allrows.each(function(node) {node.className="evenrow";});
	bind_SelectNode(rownode, false);
	row.className +="selected";

	//bind_SelectNode(rownode);
	//BindControls(false,row.parentNode.parentNode); 
}



function showspinner() {
	document.getElementById("spinner").style.display="block";
}
function hidespinner() {
	document.getElementById("spinner").style.display="none";
}


function addCommandButton(strName, strIcon, strOnClick) {
	var button = document.createElement("TD");
	button.title = strName;
	button.className="commandbutton";
	eval("button.onclick=function() {"+strOnClick+"};");
	if (strIcon.indexOf(".")<0) strIcon=strIcon+"-24.png";
	button.innerHTML="<img src='../images/"+strIcon+"' />";
	$("buttoncontainer").insertBefore(button,$("endofbuttons"));
}

function hideDetail() {	if (window.top.document.getElementById("mainframeset")) window.top.document.getElementById("mainframeset").rows="*,0";}
function showDetail() {	if (window.top.document.getElementById("mainframeset")) window.top.document.getElementById("mainframeset").rows=LISTVIEWSIZE+",*"; }

function setMainPage(strUrl) {
	try {window.top.document.getElementById("mainframeset").rows="*,0";} catch(e) {}
	if (window.top.frames["main"]) 
		window.top.frames["main"].document.location=strUrl;
	else
		window.location = strUrl;
}
function setLeftMenu(strUrl) {
	if (window.top.frames["leftmenu"]) window.top.frames["leftmenu"].document.location=strUrl;
}
function setDetailPage(strUrl) {
	if (window.top.document.getElementById("mainframeset")) {
		window.top.document.getElementById("mainframeset").rows=LISTVIEWSIZE+",*";
		window.top.frames["detail"].document.location=strUrl;
	} else {
		var win = window.open(strUrl, "detailpopup", "toolbar=0,width=1000, height=600, top=50, left=100,menubar=1,resizable=1");
		win.focus();
	}
}

function RefreshList() {
	if (window.top.frames["main"]) {
		window.top.frames["main"].ApplyFilter("listform");
	} else if (window.opener) {
		window.opener.ApplyFilter("listform");
	}
}
function CheckDirty(xmldoc) {
	if (bSelectSingleNode("//@dirty",xmldoc)) return "De wijzigingen zijn nog niet opgeslagen. Weet u zeker dat u dit scherm wilt verlaten?"
}



function resizeWindow (nWidth, nHeight) {
	if (!window.top.frames.length>0) {
		window.resizeTo(nWidth,nHeight);
	}
}
