/*

	SOLVE CLIENT SIDE FUNCTIONS
	Copyright (c) 2001-2006 Elements Interactive bv

*/

state = 0;

var fn2 = new Array('folder_block', 'folder_none');
var im2 = new Array();
im2 = preloadimages(fn2, 1);

var fn3 = new Array('folder_small_block', 'folder_small_none');
var im3 = new Array();
im3 = preloadimages(fn3, 1);

if(typeof(PHP) != 'string')
{
	PHP = location.href;
	if(PHP.indexOf("?") >= 0) PHP = PHP.substring(0, PHP.indexOf("?"));
	if(PHP.substring(0, 7).toLowerCase() == 'http://')
	{
		PHP = PHP.substring(7, PHP.length);
		PHP = PHP.substring(PHP.indexOf('/'), PHP.length);
	}
}

function siv(name)
{
	return(document.forms[0].elements[name].options[document.forms[0].elements[name].selectedIndex].value);
}

function radio_selected(fieldname)
{
	found = false;
	with(document.forms[0])
	{
		for(i=0; i < elements[fieldname].length; i++)
			if(elements[fieldname][i].checked)
			{
				found = true;
				break;
			}
	}
	return found;
}

function sprintf(template, value1, value2, value3)
{
	var count = 0;
	var str = '';
	for(i=0; i<template.length; i++)
		str += template.charAt(i) == '%' ? eval('value' + ++count) : template.charAt(i);
	return(str);
}

function getelement(n)
{
	return document.getElementById ? document.getElementById(n) : (document.all ? document.all[n] : undefined);
}

function parent_getelement(n)
{
	return document.getElementById ? parent.document.getElementById(n) : (document.all ? parent.document.all[n] : undefined);
}

function blockdisplay_switch(name, imageobj, switchto)
{
	var obj = document.getElementById ? document.getElementById(name) : (document.all ? document.all[name] : undefined);
	if(obj)
		with(obj.style)
		{
			document.images[name + '_img'].src = imageobj[display == 'block' ? 1 : 0].src;
			if(switchto)
				display = switchto == 1 ? 'block' : 'none';
			else
				display = display == 'none' ? 'block' : 'none';
		}
}


function preloadimages(fn, mode, altpath)
{
	var im = new Array();
	if(mode == 0)
	{
		var num = 1;
		for(i=0; i<fn.length*2; i++)
		{
			var filename = altpath + '/' + fn[Math.round((i-1)/2)] + "_" + (num=1-num) + '.gif';
			im[i] = new Image();
			im[i].src = filename;
			//document.writeln('<LI>' + filename);
		}
	}
	else
	{
		for(i=0; i<fn.length; i++)
		{
			im[i] = new Image();
			im[i].src = 'images/' + fn[i] + '.gif';
		}
	}
	return(im);
}
function roll(button, mouseOver) 
{
	if(document.images) document.images['button' + button].src = im[button*2+mouseOver].src;
	//window.status = mouseOver ? str[button] : "";
	return true;
}


function getDaysInMonth(month, year)
{
	var d = new Date();
	d.setYear(year + (year < 1900 ? 1900 : 0));
	d.setMonth(month - 1);		// index base 0 in stead of 1
	d.setDate(32);
	return 32 - d.getDate();
}

function fixdate(mydate)
{
	var d = check_date(mydate);
	ret = false;
	if(d)
	{
		if(d.m < 10) d.m = '0' + d.m;
		if(d.d < 10) d.d = '0' + d.d;
		ret = d.y + '-' + d.m + '-' + d.d;
	}
	else
		ret = '';
	return ret;
}

function check_date(mydate)
{	
	if(mydate.indexOf("-") > -1)		delimeter = "-";
	else if(mydate.indexOf("/") > -1)	delimeter = "/";
	else if(mydate.indexOf(".") > -1)	delimeter = ".";
	else if(mydate.indexOf(" ") > -1)	delimeter = " ";
	else if(mydate.indexOf("\\") > -1)	delimeter = "\\";
	else if(mydate.indexOf(",") > -1)	delimeter = ",";
	else if(mydate.indexOf("_") > -1)	delimeter = "_";
	else return false;

	var val = new Array();
	var index = 0;
	for(var i=0; i<mydate.length; i++)
	{                       
		token = mydate.charAt(i);
		if(token == delimeter)
			index++
		else
		{
			if(!val[index])
				val[index] = '';
			val[index] += token
		}
	}
	
	if(index == 1)
	{
		// 2 parameters; add year, get from client
		var d = new Date();
		val[++index] = d.getYear();
	}

	if(index != 2) return false;
	
	m = val[1];

	/*
		if(isNaN(m))
		{
			var maandnaam = new Array(
				new Array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'),
				new Array('jan', 'feb', 'mrt', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'),
				new Array('   ', '   ', 'maa', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ')
			);
			var mstr = '' + m;
			mstr = mstr.substring(0, 3).toLowerCase();
			for(i=0; i<3; i++)
				for(j=0; j<12; j++)
					if(mstr == maandnaam[i][j])
					{
						m = j + 1;
						i = 3;
						break;
					}
			
		}
	*/

	m = parseInt(parseFloat(m));

	if(val[0] >= 1970)
	{
		y = parseInt(parseFloat(val[0]));
		d = parseInt(parseFloat(val[2]));
	}
	else
	{
		y = parseInt(parseFloat(val[2]));
		d = parseInt(parseFloat(val[0]));
		if(val[2] < 1970)
			y += y >= 70 && y < 100 ? 1900 : 2000;
	}
	
	if(isNaN(y) || isNaN(m) || isNaN(d)) return false;

	//alert(y + '-' + m + '-' + d);
	if(y < 1970 | y > 2099) return false;
	else if(m < 1 | m > 12) return false;
	else if(d < 1 | d > getDaysInMonth(m, y)) return false;

	var ret = new Object();
	ret.m = m;
	ret.d = d;
	ret.y = y;

	return(ret);
}


function opendatepicker(name, title, formname)
{
	if(typeof(formname) == 'undefined' || formname == '' || formname == false)
	{
		formname = 'myform';
	}

	with(document.forms[formname].elements[name])
	{
		select();
		focus();
		mydate = check_date(value);
	}
	mystr = mydate ? '&y=' + mydate.y + '&m=' + mydate.m + '&d=' + mydate.d : '';
	datepickerwin = window.open(PHP + '?ac=popup_datepicker&element=' + name + '&title=' + escape(title) + '&formname=' + escape(formname) + mystr, 'Datepicker', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=0,resizable=0,width=310,height=335');
}

function sendmailform(PHP, user, subject, body)
{
	sendmailformwin = window.open(PHP + '?ac=sendmailform&user=' + user + '&subject=' + escape(subject) + '&body=' + escape(body), 'SendMailForm', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=630,height=500');
}

function openuser(user)
{
	sendmailformwin = window.open(PHP + '?ac=popup_user&user=' + user, 'UserWin', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=400,height=350');
}


function mOvr(src,clrOver)
{ 
	if(!src.contains(event.fromElement))
	{ 
		src.style.cursor = 'hand'; 
		src.bgColor = clrOver; 
	} 
} 
function mOut(src,clrIn){ 
	if (!src.contains(event.toElement))
	{
		src.style.cursor = 'default'; 
		src.bgColor = clrIn; 
	} 
} 
function mClk(src){ 
	if(event.srcElement.tagName=='TD')
		src.children.tags('A')[0].click();
}










function get_cookie(findname)
{
	var retcookie = "";
	var curname = "";
	var mode = 0;
	var str = document.cookie + ";";
	for(var i=0; i < str.length; i++)
	{                       
		var token = str.charAt(i);
		if(token == ";")
		{
			if(mode == 2)
				break;
			curname = "";
			mode = 0;
		}
		else if(token == "=")
		{
			mode++;
			if(curname == findname)
			{
				curname = "";
				retcookie = "";
				mode++;
			}
			else
				curname = "";
		}
		else
			if(mode == 2)
				retcookie += token;
			else
				if(mode == 0 && token != " ")
					curname += token;
	}

	return(unescape(retcookie));
}

function set_cookie(name, value)
{
	var date_expire = new Date();
	date_expire.setTime(date_expire.getTime() + (1000 * 60 * 60 * 24 * 31));		// 1000 msec * 60 sec * 60 min * 24 uur * 31 dagen
	document.cookie = name + "=" + escape(value) + "; expires=" + date_expire.toGMTString() + "; path=/;";
}

function clipboard_get()
{
	str = get_cookie('solve_clipboard');
	//alert(str);

	var rec = new Array();
	var my = "";
	var count = 0;
	if(str.length > 0)
	{
		str += '&';
		for(var i=0; i < str.length; i++)
		{
			var token = str.charAt(i);
			if(token == '&')		// pair seperator
			{
				//alert(count + ' --> ' + my);
				rec[count++] = my;
				my = "";
			}
			else
				my += token;
		}
	}
	return(rec);
}

		
function clipboard_delete(obj)
{
	// Get Clipboard data
	var jsobj = new Array();
	jsobj = clipboard_get();
	
	var jsobj2 = new Array();
	for(j=0; j < jsobj.length; j++)
	{
		var found = 0;
		with(obj)
			for(var i=0; i<elements.length; i++)
				if(elements[i].name == '_cb')
					if(elements[i].checked)
						if(jsobj[j] == elements[i].value)
						{
							found = true
							break;
						}
		if(!found)
			jsobj2[jsobj2.length] = jsobj[j];
	}
	clipboard_apply(jsobj2);
	location.href += '';
}

function clipboard_copyinto()
{
	if(!document.forms['cset'])
		return;

	// Get Clipboard data
	var jsobj = new Array();
	jsobj = clipboard_get();

	// Add new Objects to Clipboard
	var newobj = 0;
	with(document.forms['cset'])
		for(var i=0; i<elements.length; i++)
			if(elements[i].name == '_cb')
				if(elements[i].type == 'hidden' || elements[i].checked)
				{
					found = 0;
					for(j=0; j < jsobj.length; j++)
						if(jsobj[j] == elements[i].value)
						{
							found = true
							break;
						}
					if(!found)
					{
						jsobj[jsobj.length] = elements[i].value;
						newobj++;
					}
				}
	clipboard_apply(jsobj);

	if(newobj > 0)
		alert('Aantal objecten toegevoegd aan klembord: ' + newobj);
}

function clipboard_apply(jsobj)
{
	// Create new cookiestring
	var str = '';
	for(i=0; i < jsobj.length; i++)
		str += jsobj[i] + '&';
	str = str.substring(0, str.length-1);

	// Store cookie
	set_cookie('solve_clipboard', str)
}

function clipboard_view()
{
	var clipboardwin = window.open(PHP + '?ac=clipboard_view', 'ClipBoard', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=580,height=450');
	clipboardwin.focus();
}

function clipboard_cset(state, checkboxname)
{
	if(typeof(checkboxname) == 'undefined' || !checkboxname)
		checkboxname = '_cb';
	

	if(document.forms['cset'])
		with(document.forms['cset'])
			for(i=0; i<elements.length; i++)
				if(elements[i].name == checkboxname)
					elements[i].checked = state;
}

function scrolldiv_resizehandler()
{
	if(document.getElementById('scrolldiv'))
		document.getElementById('scrolldiv').style.width = document.body.clientWidth - 35;
	else
		for(i=0; i<7; i++)
			if(document.getElementById('scrolldiv' + i))
				document.getElementById('scrolldiv' + i).style.width = document.body.clientWidth - 35;
}

function calendar_copyinto(useopener)
{
	if(!document.forms['cset'])
		return;

	// Collect checked objects and relocate to the Calendar
	var ids = "";
	with(document.forms['cset'])
		for(var i=0; i<elements.length; i++)
			if(elements[i].name == '_cb')
			{
				checkboxcounter++;
				if(elements[i].type == 'hidden' || elements[i].checked)
					ids += elements[i].value + ',';
			}
	if(checkboxcounter == 0)
	{
		alert('Er zijn geen objecten gevonden om te exporteren.');
		return false;
	}
	ids = ids.substring(0, ids.length-1);

	if(ids == '')
	{
		if(confirm('U heeft geen enkel object geselecteerd. Allen selecteren en openen in de agenda?'))
		{
			clipboard_cset(true);
			calendar_copyinto(useopener);
		}
	}
	else
	{
		loc = useopener ? opener.location : location;
		loc.href = PHP + '?ac=overview_calendar&objsel=-2&ids=' + escape(ids);
		if(useopener)
			self.close();
	}
}


function able(obj, state, bgcolor)
{
	if(obj)
	{
		obj.disabled = !state;
		if(obj.type == 'radio' || obj.type == 'checkbox') return;
		if(state && !bgcolor) bgcolor = '#FFFFFF';
		if(obj.style) obj.style.backgroundColor = state ? bgcolor : '#DDDDDD';
	}
}

function openhelp(url)
{
	//if(document.body.clientWidth > 0) myWidth = document.body.clientWidth;
	//else if(window.innerWidth > 0) myWidth = document.body.clientWidth;
	//var url = myWidth > 800 ? 'solvedoc/solvedoc.html' : 'solvedoc/solvedoc.swf';
	//popupWin = window.open(url , 'remote','toolbar=0,location=0,directories=0,menubar=0,scrollbars=no,resizeable=0,fullscreen=1');
	if(typeof(screen.width) == 'number')
	{
		xleft = 100;
		xtop = 50;
		width = screen.width - 200;
		height = screen.height - 200;
	}
	else
	{
		xleft = 0;
		xtop = 0;
		width = 550;
		height = 400;
	}
	popupWin = window.open(url, 'help','toolbar=1,location=1,directories=1,menubar=1,scrollbars=1,status=1,resizable=1,fullscreen=0,width=' + width + ',height=' + height + ',left=' + xleft + ',top=' + xtop);
}


function open_config_ci_detail(id)
{
	configwin = window.open(PHP + '?ac=popup_config_ci_detail&id=' + id, 'ConfigWin', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=550,height=400');
}

function open_config_citype(id)
{
	configwin = window.open(PHP + '?ac=popup_config_citype&id=' + id, 'ConfigWin', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=550,height=400');
}

function sortselect_compareOptionText(a,b)
{
	return a.text != b.text.toLowerCase() ? a.text.toLowerCase() < b.text.toLowerCase() ? -1 : 1 : 0;		// textual comparison
	//return a.text - b.text;								// numerical comparison
}

function sortselect(list)
{
	var items = list.options.length;
	if(items > 1)
	{
		var tmpArray = new Array(items);
		for(i=0; i<items; i++)
			tmpArray[i] = new Option(list.options[i].text, list.options[i].value);
		tmpArray.sort(sortselect_compareOptionText);
		for(i=0; i<items; i++)
	    	list.options[i] = new Option(tmpArray[i].text, tmpArray[i].value);
	 }
}

function ci_click(obj)
{
	with(parent.document.forms[0].elements['ci[]'])
	{
		if(!obj.checked)
		{
			for(i=0; i<options.length; i++)
			{
				if(options[i].value == obj.value)
				{
					for(j=i; j<options.length-1; j++)
					{
						//options[j] = options[j+1];
						options[j].value = options[j+1].value;
						options[j].text = options[j+1].text;
					}
					optionid = options.length--;
					break;
				}
			}
		}
		else
		{
			var found = -1;
			for(i=0; i<options.length; i++)
				if(options[i].value == obj.value)
				{
					found = i;
					break;
				}

			if(found == -1)
			{
				optionid = options.length++;
				options[optionid].text = citext[obj.value];
				options[optionid].value = obj.value;
			}
			parent.sortselect(document.forms[0].elements['ci[]'])
		}
	}
}

function ci_check()
{
	for(j=0; j < parent.document.forms[0].elements['ci[]'].options.length; j++)
	{
		var xid = parent.document.forms[0].elements['ci[]'].options[j].value;
		with(document.forms['cilist'])
			if(elements['ci' + xid] && elements['ci' + xid].type == 'checkbox')
				elements['ci' + xid].checked = true;
	}

	/*
	if(document.forms['cilist'])
		with(document.forms['cilist'])
			for(i=0; i<elements.length; i++)
				if(elements[i].type == 'checkbox')
					with(parent.document.forms[0].elements['ci[]'])
						for(j=0; j<options.length; j++)
							if(options[j].value == elements[i].value)
							{
								elements[i].checked = true;
								break;
							}
	*/
}

function excel_copyinto_postform()
{
	if(!document.forms['cset'])
		return;

	var found = false;
	with(document.forms['cset'])
		for(var i=0; i<elements.length; i++)
			if(elements[i].name == 'i[]')
				if(elements[i].checked)
				{
					found = true;
					break;
				}

	if(!found)
	{
		if(confirm('U heeft niets geselecteerd. Alles selecteren en openen in Excel?'))
		{
			clipboard_cset(true, 'i[]');
			excel_copyinto_postform();
		}
	}
	else
	{
		// Submit Form
		with(document.forms['cset'])
			submit();
	}
}

function excel_copyinto(ac, ticket_type, postform)
{
	if(typeof(ac) == 'undefined')
		ac = 'export_excel_multiobj';

	if(typeof(ticket_type) == 'undefined')
		ticket_type = 0;

	if(!document.forms['cset'])
		return;

	// Collect checked objects
	var ids = "";
	var checkboxcounter = 0;
	with(document.forms['cset'])
		for(var i=0; i<elements.length; i++)
			if(elements[i].name == '_cb')
			{
				checkboxcounter++;
				if(elements[i].type == 'hidden' || elements[i].checked)
					ids += elements[i].value + ',';
			}
	if(checkboxcounter == 0)
	{
		alert('Er zijn geen ' + (ac == 'export_excel_multiticket' ? 'tickets' : 'objecten') + ' gevonden om te exporteren.');
		return false;
	}
	ids = ids.substring(0, ids.length-1);

	if(ids == '')
	{
		if(confirm('U heeft geen enkel ' + (ac == 'export_excel_multiticket' ? 'ticket' : 'object') + ' geselecteerd. Allen selecteren en openen in Excel?'))
		{
			clipboard_cset(true);
			excel_copyinto(ac, ticket_type, postform);
		}
	}
	else
	{
		var url = PHP + '?ac=' + ac + '&ids=' + escape(ids) + '&ticket_type=' + ticket_type;
		//alert(url);
		//return;
		export_excel_iframe.document.location.href = url;
		//window.open(PHP + '?ac=export_excel_multiobj&ids=' + escape(ids));
	}
}

function excel_invoke_iframe(url)
{
	url = PHP + '?ac=export_excel_obj&obj=$obj&curtab=2';
	document.frames['export_excel_iframe'].location.replace=url
}

function open_cipicker(findci, elementname, rt, personal_ci, cur_user, user)
{
	ciwin = window.open(PHP + '?ac=config_popup_cipicker&findci=' + findci + '&elementname=' + elementname + '&rt=' + rt + '&personal_ci=' + personal_ci + '&cur_user=' + cur_user + '&user=' + user, 'copicker', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=420,height=400');
	ciwin.focus();
}

function clear_ci(fn)
{
	with(document.forms['myform'])
	{
		if(elements[fn].value > 0)
		{
			elements[fn].value = '';
			elements[fn + '_CITYPE'].value = '';
			elements[fn + '_TEXT'].value = '';
			if(typeof(onchange_ci) == 'function')
				onchange_ci(1);
		}
	}
}

function rand(max)
{
	return(Math.round(Math.random(1) * max));
}

function obj_new(newwin)
{
	var abc = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	var url = PHP + '?ac=obj_new&session=';
	for(i=0; i<24; i++)
		url += rand(1) ? abc[rand(25)] : abc[rand(25)].toLowerCase();
	
	if(navigator.appVersion.indexOf('MSIE') > -1 && event.shiftKey)
	{
		void window.open(url);
	}
	else
		location.href = url;
}

function openuserpicker(name, id, title_fieldname, title_blockname, bofd, type, obj)
{
	url = PHP + '?ac=popup_userpicker&element=' + name + '&title_blockname=' + escape(title_blockname) + '&title_fieldname=' + escape(title_fieldname) + (isNaN(id) ? '' : '&id=' + id) + (isNaN(bofd) ? '' : '&bofd=' + bofd + (isNaN(type) ? '' : '&type=' + type) + (isNaN(obj) ? '' : '&obj=' + obj));
	userpickerwin = window.open(url, 'Userpicker', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=450,height=325');
	//showModal(url,450,300,1,1,1);
}

function optionsearch(obj, heading)
{
	if(typeof(keyword) == 'undefined')
		keyword = '';
	if(heading == '')
		heading = 'Zoeken';

	if(document.all && typeof(vbprompt) != 'undefined')
		keyword = vbprompt(heading, keyword);
	else
		keyword = prompt(heading, keyword);

	if(keyword == null)
		return;

	keyword = keyword.toLowerCase();

	if(keyword.substring(0, 1) == '*' && keyword.substring(keyword.length - 1, keyword.length) == '*')
	{
		mode = 0;
		searchfor = keyword.substring(1, keyword.length-1);
	}
	else if(keyword.substring(0, 1) == '*')
	{
		mode = 1;
		searchfor = keyword.substring(1, keyword.length);
	}
	else if(keyword.substring(keyword.length - 1, keyword.length) == '*')
	{
		mode = 2;
		searchfor = keyword.substring(0, keyword.length-1);
	}
	else
	{
		mode = 2;
		searchfor = keyword;
		keyword += '*';
	}

	if(searchfor)
		with(obj)
		{
			for(i=selectedIndex+1; i<length; i++)
				if(check(obj,i)) return;
			for(i=0; i<=selectedIndex; i++)
				if(check(obj,i)) return;

			if(mode == 1)
				str = "Geen optie gevonden eindigend op trefwoord '" + (searchfor) + "'."
			else if(mode == 2)
				str = "Geen optie gevonden beginnend met trefwoord '" + (searchfor) + "'."
			else
				str = "Geen optie gevonden met trefwoord '" + (searchfor) + "'."
			
			optionsearch(obj, str);
		}

	function check(obj,i)
	{
		with(obj)
		{
			check_text = options[i].text.toLowerCase();
			if	(
					(mode == 0 && check_text.indexOf(searchfor) >= 0)
					||
					(mode == 1 && check_text.substring(check_text.length-searchfor.length, check_text.length) == searchfor)
					||
					(mode == 2 && check_text.indexOf(searchfor) == 0)
					
				)
			{
				selectedIndex = i;
				obj.focus();
				return(1);
			}
		}
	}
}

with(document)
{
	writeln('<SCRIPT LANGUAGE="VBScript">');
	writeln('<!--');
	writeln('function vbprompt(heading, keyword)');
	writeln('	vbprompt = InputBox(heading, heading, keyword)');
	writeln('end function');
	writeln('\' -->');
	writeln('</SCRIPT>');
}

function selectrow(id)
{
	with(document.getElementById('row' + id))
		style.backgroundColor = style.backgroundColor == '' ? (className == 'wl-bg0' ? '#C0D7F4' : '#B0C7E4') : '';
}



function blinkelement(fieldname, counter, nofocus, origcol)
{
	if(isNaN(counter) || counter == false || counter <= 0) counter = 5;
	with(document.forms.myform)
	{
		if(elements[fieldname].type == 'textarea' || elements[fieldname].type == 'select-one' || elements[fieldname].type == 'text')
		{
			if(elements[fieldname].style)
			{
				elements[fieldname].style.backgroundColor = counter-- % 2 ? origcol : '#FF0000';			// '#FFFFFF'

				if(counter > 0)
				{
					mytimer = setTimeout('blinkelement(\'' + fieldname + '\',' + counter + ',' + (nofocus ? 1 : 0) + ',\'' + origcol + '\')', 125);
					return;
				}
			}
		}

		if(!nofocus && !elements[fieldname].disabled)
		{
			elements[fieldname].focus();
			if(elements[fieldname].select)
				elements[fieldname].select();
		}
	}
}


//	datepickerwin = window.open(PHP + '?ac=popup_datepicker&element=' + name + '&title=' + escape(title) + mystr, 'Datepicker', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=0,resizable=0,width=310,height=335');

function showModal(url, width, height, resizable, status, scroll)
{
	if(resizable != '0' && resizable != '1')	resizable = 1;
	if(status != '0' && status != '1')			status = 0;
	if(scroll != '0' && scroll != '1')			scroll = 1;

	var args = {
		opener: window,
		str: 'Popup',
		x: 375,
		y: 410
	};
	
	var features = '';
	features += 'dialogHeight: ' + height + 'px;';
	features += 'dialogWidth: ' + width + 'px;';
	features += 'scroll: ' + scroll + ';';
	features += 'edge: Raised;';
	features += 'center: 1;';
	features += 'help: 0;';
	features += 'resizable: ' + resizable + ';';
	features += 'status: ' + (status ? 'yes' : 'no') + ';';

  	return window.showModalDialog(url, args, features);
}

function openRespWin(ofd)
{
	respwin = window.open(PHP + '?ac=popup_respwin&ofd=' + ofd, 'RespWin', 'toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1,width=300,height=300');
}


document.onclick = clickhandler;
cur_divmenu = null;
popup_hover = null;
function clickhandler()
{
	if(!popup_hover && cur_divmenu > 0)
		open_divmenu(0);
}

function open_divmenu(nr, hide_quickselector)
{
	if(typeof(hide_quickselector) == 'undefined')
		hide_quickselector = false;
	
	q = getelement('quickselector');
	if(typeof(cur_divmenu) == 'number' && cur_divmenu > 0)
	{
		p = getelement('divmenu' + cur_divmenu);
		if(p)
			p.style.visibility = 'hidden';
		cur_divmenu = 0;

		if(q)		
			q.style.visibility = 'visible';
	}

	if(nr > 0)
	{
		p = getelement('divmenu' + nr);
		if(p)
			p.style.visibility = 'visible';
		cur_divmenu = nr;

		if(hide_quickselector)
			if(q)		
				q.style.visibility = 'hidden';
	}
}

function checkMaxLength(textarea, evt, maxLength)
{
	/*
	special_chars = 0;
	for(i=0; i<textarea.value.length; i++)
		if(textarea.value.substring(i, 1) == "'" || textarea.value.substring(i, 1) == "|")
			special_chars++
	maxLength -= special_chars;
	*/

	if(textarea.value.length > maxLength)
	{
		complete_length = textarea.value.length;

		textarea.value = textarea.value.substring(0, maxLength);
		if(complete_length > maxLength + 1)
			alert('U heeft teveel tekens ingevoerd: de tekst wordt afgebroken.');
		return false;
	}

	var allowKey = false;
	if(document.selection && document.selection.createRange().text.length > 0)
		allowKey = true;
	else
	{
		var keyCode = document.layers ? evt.which : evt.keyCode;
		if(keyCode < 32 && keyCode != 13)
			allowKey = true;
		else if(keyCode >= 33 && keyCode <= 40)		// PgUp, PgDn, Home, End, Cursor Keys
			allowKey = true;
		else if(evt.ctrlKey && (keyCode == 65 || keyCode == 67 || keyCode == 86 || keyCode == 88))		// Ctrl-A Ctrl-C Ctrl-V Ctrl-X
			allowKey = true;
		else if(keyCode == 46 || keyCode == 45)		// Del, Ins
			allowKey = true;
		else
			allowKey = textarea.value.length < maxLength;
	}

	return allowKey;
}

function trim(str)	{ return ltrim(rtrim(str)) }
function ltrim(str)	{ return str.replace(/^\s+/,'') }
function rtrim(str)	{ return str.replace(/\s+$/,'') }

function image_highlight(o, state)
{
	if(typeof(o) != 'object')
		o = getelement(o);
	o.style.filter = 'alpha(opacity=' + (state ? 50 : 100) + ')';
}


function popup_attrci(is_ci, attrdef_id, prefix, element_name, auto_searchbox_open)
{
	
	if(typeof(element_name) != 'string')
	{
		element_name = 'ATTR_' + attrdef_id;
	}
	
	val = document.forms.myform.elements[element_name].value;
	url = PHP + '?ac=config_popup_attrci&is_ci=' + is_ci + '&attrdef_id=' + attrdef_id + '&val=' + escape(val) + '&auto_searchbox_open=' + (auto_searchbox_open ? 1 : 0);
	ret = showModal(url, 500, 400, 1, 0, 1);
	//window.open(url);
	
	if(ret)
	{
		with(document.forms.myform)
		{
			elements[element_name].value = ret.id;
			elements[element_name + '_VALUE'].value = ret.id != '' && ret.id != '0' ? (typeof(prefix) == 'string' && prefix.length > 0 ? prefix + ': ' : '') + ret.text : '';
			elements[element_name + '_VALUE'].title = elements[element_name + '_VALUE'].value;
		}
	}
}

function set_approvestatus_picklist(ofd, status)
{
	if(mysubmit(document.forms.myform, true, true))
	{
		with(document.forms.myform)
		{
			if(elements['approvestatus_' + ofd])
				elements['approvestatus_' + ofd].value = status;
			submit();
		}
	}
}

function block_button_group(o, fieldname, new_value)
{
	if(mysubmit(document.forms.myform, true, true))
	{
		with(document.forms.myform)
		{
			elements[fieldname].value = new_value;
			submit();
		}
	}
}

function obj_newci(dsd_id, s, citype_write_counter, citype_write_first, previous_data)
{
	citype = false;
	if(citype_write_counter > 1)
	{
		url = PHP + '?ac=popup_obj_newci_pickcitype&dsd=' + dsd_id;
		citype = showModal(url, 600, 300, 1, 0, 1);
		if(citype == false || typeof(citype) == 'undefined')
			return false;
	}
	else
	{
		citype = citype_write_first;
	}

	url = PHP + '?ac=popup_obj_newci&citype=' + citype;
	if(previous_data)
	{
		url += '&data=' + escape(previous_data) + '&reedit=1';
	}

	var data = showModal(url, 780, 450, 1, 0, 1);
	if(data == false || typeof(data) == 'undefined')
	{
		return false;
	}
	else
	{
		url = PHP + '?ac=popup_obj_newci&citype=' + citype + '&data=' + escape(data);
		ret = showModal(url, 780, 450, 1, 0, 1);
		if(typeof(ret) == 'object')
		{
			if(ret.error)
			{
				alert(ret.error);
				return obj_newci(dsd_id, s, 1, citype, data)		// Try again
			}
			else
			{
				if(s.type == 'select')
				{
					s.options.length++;
					i = s.options.length-1;
					s.options[i].value = ret.id;
					s.options[i].text = ret.text;
					s.selectedIndex = i;
					//break_loop = true;
				}
				else
				{
					// text/popup
					s.value = ret.id;
					document.forms.myform.elements[s.name + '_text'].value = ret.text;
				}
			}
		}
		else
		{
			return false;
		}
	}
}

function widget_getuser(o_id, o_text, type, caption, empty_option)
{
	url = PHP + '?ac=popup_userpicker';
	url += '&id=' + o_id.value;
	url += '&type=' + type;
	url += '&title=' + escape(caption);
	url += '&modal=1';
	url += '&empty_option=' + (typeof(empty_option) == 'undefined' ? 1 : empty_option);
	ret = showModal(url, 450, 375, 1, 0, 1);

	if(typeof(ret) == 'undefined')
	{
		return false;
	}
	else
	{
		o_id.value		= ret.id;
		o_text.value	= ret.text;
	}
}

function widget_getticket(multisel, o_sel, o_text, ticket_type, ticket_id, problem_id)
{
	url = PHP + '?ac=popup_ticketpicker';
	url += '&multisel=' + (multisel ? 1 : 0);
	url += '&sel=' + o_sel.value;
	if(ticket_id > 0) url += '&ticket_id=' + ticket_id;
	if(problem_id > 0) url += '&problem_id=' + problem_id;
	url += '&ticket_type=' + ticket_type;
	
	//window.open(url);
	ret = showModal(url, 780, 460, 1, 0, 1);

	if(typeof(ret) == 'undefined')
		return false;
	else if(ret && typeof(ret) == 'object')
	{
		str_sel = '';
		str_text = '';
		for(var i in ret)
		{
			str_sel += '' + parseInt(i,10) + ',';
			//str_text += '' + i + '. ' + ret[i] + ', ';
			str_text += ret[i] + ', ';
		}

		if(str_sel) str_sel = str_sel.substr(0, str_sel.length - 1);
		if(str_text) str_text = str_text.substr(0, str_text.length - 2);

		o_sel.value = str_sel;
		o_text.value = str_text;
	}
}

function widget_getmultici(multisel, ci_sel, ci_text, js_onchange, js_reltype_param, configmode, filter_datasetdata_citype, open_searchbox_at_startup, debug)
{
	url = PHP + '?ac=popup_multicipicker';
	url += '&multisel=' + (multisel ? 1 : 0);
	url += '&sel=' + ci_sel.value;
	if(js_reltype_param)
		url += '&reltype=' + js_reltype_param;
	
	if(configmode)
		url += '&configmode=' + configmode;

	if(filter_datasetdata_citype > 0)
		url += '&filter_datasetdata_citype=' + filter_datasetdata_citype;
	
	if(open_searchbox_at_startup)
		url  += '&open_searchbox_at_startup=' + (open_searchbox_at_startup ? 1 : 0);
	
	if(debug)
		url += '&debug=1';

	sWidth = parseInt(screen.width / 100 * 90, 10);		// 780;
	sHeight = 510;
	if(debug)
		window.open(url);
	else
		ret = showModal(url, sWidth, sHeight, 1, 0, 1);

	if(typeof(ret) == 'undefined')
	{
		return false;
	}
	else if(ret && typeof(ret) == 'object')
	{
		str_sel = '';
		str_text = '';
		for(var i in ret)
		{
			//str_sel += '' + parseInt(i,10) + ',';
			str_sel += i + ',';			// denk aan $u:999
			str_text += ret[i] + ', ';
		}

		if(str_sel) str_sel = str_sel.substr(0, str_sel.length - 1);
		if(str_text) str_text = str_text.substr(0, str_text.length - 2);

		ci_sel.value = str_sel;
		ci_text.value = str_text;
		
		//alert(ci_sel.name + '=' + ci_sel.value);
		
		if(typeof(js_onchange) == 'string' && js_onchange.length > 0)
		{
			eval(js_onchange + '(' + str_sel + ')');
		}
	}
}

function widget_userset_deptonly(field_value, field_text, ticket_id, ticket_type)
{
	widget_userset(
						field_value,
						field_text,
						'',			// defaultevent
						'',			// obj_id
						'',			// event_id
						'',			// adminflow_id
						'',			// is_incident
						ticket_id,	// ticket_id
						2,			// widget_type 2 = dept only
						0,			// width
						460,		// height
						ticket_type	// tickettype
					);
}

function widget_userset_useronly(field_value, field_text, ticket_id, ticket_type)
{
	widget_userset(
						field_value,
						field_text,
						'',			// defaultevent
						'',			// obj_id
						'',			// event_id
						'',			// adminflow_id
						'',			// is_incident
						ticket_id,	// ticket_id
						3,			// widget_type 3 = user only
						0,			// width
						490,		// height
						ticket_type	// tickettype
					);
}

function widget_userset(field_value, field_text, defaultevent, obj_id, event_id, adminflow_id, /*new*/ is_incident, ticket_id, widget_type, wwidth, wheight, ticket_type)
{
	if(isNaN(wwidth) || wwidth <= 0) wwidth = 780;
	if(isNaN(wheight) || wheight <= 0) wheight = 510;
	
	url = PHP + '?ac=popup_comm_userset';
	url += '&defaultevent=' + defaultevent;
	url += '&obj_id=' + obj_id;
	url += '&event_id=' + event_id;
	url += '&sel=' + escape(field_value.value);
	url += '&flow=' + escape(adminflow_id);
	url += '&is_incident=' + (is_incident ? 1 : 0);
	
	if(typeof(ticket_id) != 'undefined')
		url += '&ticket_id=' + ticket_id;

	if(typeof(ticket_type) != 'undefined')
		url += '&ticket_type=' + ticket_type;

	if(widget_type)
		url += '&widget_type=' + widget_type;
	//alert(url);

	ret = showModal(url, wwidth, wheight, 1, 0, 1);
	//window.open(url);

	if(typeof(ret) == 'undefined')
	{
		return false;
	}
	else
	{
		str_sel = '';
		str_text = '';
		for(var i in ret)
		{
			str_sel += '' + i + ',';
			str_text += ret[i] + "\n";	//', ';
		}
		if(str_sel) str_sel = str_sel.substr(0, str_sel.length - 1);
		//if(str_text) str_text = str_text.substr(0, str_text.length - 2);

		field_value.value = str_sel;
		field_text.value = str_text;
	}
}

function trim(str)	{ return ltrim(rtrim(str)) }
function ltrim(str)	{ return str.replace(/^\s+/,'') }
function rtrim(str)	{ return str.replace(/\s+$/,'') }


function charcount(str, chr)
{
	counter = 0;
	for(i=0; i<str.length; i++)
		if(str.charAt(i) == chr)
			counter++;
	return(counter);
}

function is_valid_numeric_value(str)
{
	var DEBUG = false;
	
	str = trim(str);
	len = str.length;
	if(len == 0)
		return true;

	minus = false;
	if(str.charAt(0) == '-')
	{
		// *** Chop off minus sign
		minus = true;
		str = str.substring(1, str.length);
	}


	count_dot = charcount(str, '.');
	count_comma = charcount(str, ',');

	//if(count_comma == 0 && count_dot == 1 && str.charAt(0) == '.')
	//alert(str.substring(str.length - 4, str.length - 3));
	if	(
			count_comma == 0
			&& count_dot == 1
			&&
			(
				str.length < 4
				|| str.substring(str.length - 4, str.length - 3) != '.'
				|| str.charAt(0) == '.'
			)
		)
	{
		// Speciaal geval: geen komma, wel een punt (en niet op plaats waar duizendtalscheidingsteken hoort te staan
		// Verander punt in komma
		if(DEBUG) alert('SPECIAL');
		str = str.replace(/\./, ',');
		count_comma = 1;
		count_dot = 0;
	}


	first_dot = str.indexOf('.');
	first_comma = str.indexOf(',');

	if(str.indexOf('-') > -1)
	{
		if(DEBUG) alert('001: Invalid minus sign');
		return false;
	}
	else if(count_comma > 1)
	{
		// Meer dan 1 komma
		if(DEBUG) alert('003: More than one comma encountered');
		return false;
	}
	else if(count_dot > 0 && count_comma > 0 && first_comma < first_dot)
	{
		// Minstens 1 punt en minstens 1 komma, maar de komma komt eerder voor dan de punt
		// 1,000.00
		// 1,0.1.22
		if(DEBUG) alert('004: Comma used earlier than dot');
		return false
	}

	major = false;
	minor = false;
	if(count_comma == 1)
	{
		// DECIMALEN!
		major = str.substring(0, first_comma);
		minor = str.substring(first_comma + 1, str.length);
		if(major.length == 0)
			major = '0';
	}
	else
	{
		// GEEN DECIMALEN
		major = str;
	}

	if(count_dot > 0)
	{
		if(major.charAt(0) == '.')
		{
			if(DEBUG) alert('009: Starts with thousand seperator');
			return false;
		}

		// DUIZENDTALSCHEIDINGSTEKEN(S)
		// 12.000.000.001
		// 100.000.000
		// 1.999.999
		if(major.length < 5)
		{
			// Major-gedeelte moet uit minstens 5 tekens bestaan als we duizendtalscheidingstekens willen gebruiken
			// '1.000'
			if(DEBUG) alert('005: Major-part needs at least 5 characters to use thousand seperators');
			return false;
		}
		
		rcount = 0;
		for(i=major.length - 1; i>=0; i--)
		{
			token = major.charAt(i);
			//alert(major + ' - ' + (i) + ': ' + token);
			//alert(i + ' - [' + token + '] ---- ' + (rcount % 4));

			if(rcount % 4 == 3 && token != '.')		// Ander teken op plaats duizendtalscheidingsteken
			{
				// Duizendtalscheidingsteken is niet consequent een punt (.)
				if(DEBUG) alert('005: Non-dot as thousand seperator (' + token + ')');
				return false;
			}
			else if(rcount % 4 != 3 && token == '.')		// Duizendtalscheidingsteken op de verkeerde plaats
			{
				// Duizendtalscheidingsteken is niet consequent een punt (.)
				if(DEBUG) alert('010: Thousand seperator not in place (pos. ' + i + ')');
				return false;
			}

			rcount++;
		}
		major = major.replace(/\./g, '');
	}

	if(minus == true && major <= 0)
	{
		if(DEBUG) alert('009: Invalid value: -0');
		return false;
	}

	if(major.match(/\D/))
	{
		if(DEBUG) alert('007: Non-digit in major-part');
		return false;
	}
	else if(minor != false && minor.match(/\D/))
	{
		if(DEBUG) alert('008: Non-digit in minor-part');
		return false;
	}

	if(minus)
		major = '-' + major;
	if(minor == false)
	{
		ret = major;
	}
	else
	{
		ret = major + '.' + minor
	}
	
	if(DEBUG)
	{
		alert('OK: ' + ret);
		return false;
	}
	return true;
}

function widget_pickconfig(o_id, o_text, type, caption)
{
	url = PHP + '?ac=widget_pickconfig_popup';
	url += '&id=' + o_id.value;
	url += '&type=' + type;
	url += '&caption=' + escape(caption);
	ret = showModal(url, 400, 400, 1, 0, 1);

	if(typeof(ret) == 'undefined')
	{
		return false;
	}
	else
	{
		o_id.value		= ret.id;
		o_text.value	= ret.text;
	}
}

