/*
copyright Bart de Jong 2000-2008
*/





	function Advanced_list(name,listclass) {
		this.mouseover_color	= '#dddddd';
		this.updateOnSuccess	= true;
		this.updateOnComplete	= true;

		this.currentId			= false;

		this.add_item = add_item;
		this.insert_item = insert_item;


		this.addItem = addItem;
		this.insertItem = insertItem;

		this.add_summary = add_summary;
		this.add_content_var = add_content_var;
		
		this.base_content = new Object();
			this.base_content['content'] = page_content;
			this.base_content['parent_id'] = 0;
		this.buttons_dir = './aqua/jaguar/editbuttons/';
		this.selected		= new Array();
		this.summarries 	= new Array();
		this.read_rights 	= new Array();
		this.write_rights 	= new Array();
		this.delete_rights 	= new Array();
		this.set_rights 	= set_rights;
		this.name = name;
		this.listclass = listclass;
		this.walk		= walk;
		this._walk		= _walk;

		if (arguments.length>2) {
			this.localized_classname = arguments[2];
		}
		else {
			this.localized_classname = listclass;
		}
		this.set_selected = set_selected;
		this.set_unselected = set_unselected;
		this.delete_item = delete_item;
		this.delete_selected = delete_selected;
		this.edit_selected = edit_selected;
		this.has_write_rights = has_write_rights;
		this.edit = edit;
		this.edit_array = edit_array;
//		this.load = load;
		this.loadForm = loadObjectForm;
		this.reloadObject = reloadObject;
		this.read = read;
		this.sort = sort;
		this.updateList = updateList;
		this.displayAndUpdate = displayAndUpdate;
		this.switch_selected_state = switch_selected_state;
		this.in_array = in_array;
		this.select = select;
		this.clearSelected = clearSelected;
		this.view = view
		this.mouseover = mouseover;
		this.mouseout = mouseout;
		this.contentactionid = contentactionid;
		
		this.submit			= formsubmit;
		this.showselected = showselected;
		
		this.hiderows = hiderows;
		this.showrows = showrows;
		
		this.expand = expand;
		this.collapse = collapse;
		this.length = numselected;
		this.gocontent = gocontent;
		this.base_address = base_address;
		this.activate_button = activate_button;
		this.deactivate_button = deactivate_button;
		this.submit_active_state = submit_active_state;
		this.action_selected = action_selected;
		this.content_id_url = content_id_url;
		this.showhelp = showhelp;
		this.singleSelect	= singleSelect;
	}
	
	
	function showhelp() {
		alert(this.help_text);
	}


	function walk(func_name,_delay) {
		var list	= $(this.name);
		var tbody	= list.getElementsByTagName('tbody')[0];
		var rows	= tbody.getElementsByTagName('tr');
		ids			= new Array();
	
		for (var i=0;i<rows.length;i++) {
			ids[ids.length] = rows[i].id.replace(this.name,'');
		}

		this.walkDelay		= _delay;
		this.walkFunction	= func_name.bind(this);
		this.ids			= ids;
		this.walkCursor		= 0;
		this._walk();
		
//		this._walk(func_name,_delay,ids,0);
	}

	function _walk() { //_func_name,_delay,_arr,_cursor)
		
		if (this.walkCursor<this.ids.length) {
			this.walkFunction(this.ids[this.walkCursor]);
			this.walkCursor++;
		}
		return true;
		
		
		_func_name(_arr[_cursor]);

		if (_cursor<_arr.length-1) {		
			func_name 	= _func_name;
			delay		= _delay;
			arr			= _arr;
			newcursor = _cursor+1;
			setTimeout("this._walk(func_name,delay,arr,newcursor)",delay);
		}
	}

	function base_address() { // 20030502
		var address = "?";
		for (var i in this.base_content) {
			address += "&"+i+"="+this.base_content[i];
		}
//		address += '&content='+this.base_content['content'];
		return address;
	}
	
	
	function formsubmit() {
		form = document.forms[this.name+'_form'];
		form.submit();
	}

	function submit_active_state() {
		checked_values = new Array();
		unchecked_values = new Array();
		form = document.forms[this.name+'_form'];
		formelements = form.elements;
		for (i=0;i<formelements.length;i++) {
			if (formelements[i].checked) {
				checked_values[checked_values.length] = formelements[i].value;
			}
			else {
				unchecked_values[unchecked_values.length] = formelements[i].value;
			}
		}
		newaddress = this.base_address();
		for (i=0;i<checked_values.length;i++) {
			newaddress += '&'+this.name+'_checked%5B%5D='+checked_values[i];
		}
		for (i=0;i<unchecked_values.length;i++) {
			newaddress += '&'+this.name+'_unchecked%5B%5D='+unchecked_values[i];
		}
		location.href = newaddress;
	}
	
	
	function action_selected(content,action) { // 20030505
		num_selected = 0;
		newAddress = '?content='+content+'&action='+action;
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				num_selected++;
				newAddress += '&selectedvalues%5B%5D='+this.selected[i];
			}
		}
		location.href = newAddress;
	}
	
	
	function content_id_url(content,id) { // 20030508
		newAddress = '?&content='+content;
		newAddress += '&id='+id;
//		alert(newAddress);
		location.href = newAddress;
	}

	function add_content_var(varname,value) {
//		alert('!'+value);
		this.base_content[varname] = value;
	}

	function gocontent(content,id) {
		newAddress = '?content='+content+'&id='+id;
		location.href = newAddress;
	}
	
	function add_summary(id,summary) {
		this.summary[id] = summary;
	}
	
	function expand() {
		newAddress = this.base_address()+'&'+this.name+'_state=expand';
		location.href = newAddress;
	}


	function collapse() { // 20031003
		newAddress = this.base_address()+'&'+this.name+'_state=collapse';
		location.href = newAddress;
	}

	function set_rights(id,read,write,del_ete) {
		this.read_rights[id] = read;
		this.write_rights[id] = write;
		this.delete_rights[id] = del_ete;
	}


	function simpleUpdateList(originalRequest) { // 20080317
//		alert(originalRequest.responseText);

		var pars = this.base_address();
		pars += '&action=reloadList&table_id='+this.name
//		alert(this.name+' : '+url+pars);

		if ($(this.name+'list')) {
			new Effect.Opacity(this.name+'list', {duration:0.2, from:1.0, to:0.2});
			listReplace = replaceList.bind(this);
			var myAjax = new Ajax.Request(url+pars,{method: 'post', parameters: pars, onComplete: listReplace});}
//		else {alert('no list');}	
	}

	function updateList(extra_pars) {
		if (arguments.length>1) {
			var url = arguments[1];
		}
		else {
			var url = './xml.php';
		}
//		var pars = '?content='+page_content+'&action=reloadList&table_id='+this.name+'&'+extra_pars;
		var pars = this.base_address();
		pars += '&action=reloadList&table_id='+this.name+'&'+extra_pars;
//		alert(this.name+' : '+url+pars);

		if ($(this.name+'list')) {
			new Effect.Opacity(this.name+'list', {duration:0.2, from:1.0, to:0.2});
			listReplace = replaceList.bind(this);
			var myAjax = new Ajax.Request(url+pars,{method: 'post',parameters: pars,onComplete: listReplace});}
//		else {alert('no list');}	
	}


	function replaceList(originalRequest) {
		if (processErrors(originalRequest)) {return false;}
//		alert(originalRequest.responseText);

		var lists		= originalRequest.responseXML.getElementsByTagName("list");
		var content		= lists[0].getElementsByTagName("content");
		this.selected	= new Array();

//		var name = lists[0].getElementsByTagName("name");
//		name 	= name[0].firstChild.nodeValue;
		
		var name 	= this.name;
		var content	= content[0].firstChild;
		var list	= $(name+'list');

		if (list) {
			list.innerHTML = kloonhtml(content);
			new Effect.Opacity(name+'list', {duration:0.2, from:0.2, to:1.0});
		}
	}
	

	function sort(order_by,sort_method) {
		var pars = this.name+'_order_by='+order_by+'&'+this.name+'_sort_method='+sort_method;
		pars += '&'+this.name+'_id='+this.selected[0];
		this.updateList(pars);

//		newAddress = '?content='+page_content;
//		location.href = newAddress+'&'+this.name+'_order_by='+order_by+'&'+this.name+'_sort_method='+sort_method;
	}


	
	function view(content,id) {
		newAddress = '?content='+content+'&action=view&c='+this.listclass;
		location.href = newAddress+'&id='+id;
	}


	function numselected() {
		num = 0;
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				num++;
			}
		}
		return num;
	}


	function delete_item(iid) { // 20080202
		var durl = url;
		var pars = 'content='+page_content+'&action=delete&c='+this.listclass;
		pars += '&o%5B%5D='+iid;
		if (confirm("Ben je zeker dat je wilt verwijderen:\n\n1 "+this.localized_classname+' ?')) {
//				alert(durl+pars);
			displayAndUpdate = displayAndUpdate.bind(this);
			var myAjax = new Ajax.Request(durl,{method: 'post',parameters: pars,onLoading: gettingForm,onComplete:displayAndUpdate,onException: alertFailure});
			this.set_unselected(iid);
//				location.href = "?"+pars;
//				this.updateList('');
		}
	}
	
	
	function delete_selected() {
		var durl = url;
		var pars = 'content='+page_content+'&action=delete&c='+this.listclass;
		num_to_delete = 0;
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				num_to_delete++;
				pars += '&o%5B%5D='+this.selected[i];
			}
		}
		if (arguments.length>0) {
			pars += '&'+arguments[0];
		}
		if (arguments.length>1) {
			durl = arguments[1];
		}
		if (num_to_delete > 0) {
			if (confirm("Ben je zeker dat je wilt verwijderen:\n\n"+num_to_delete+' item(s)\n'+this.localized_classname+' ?')) {
//				alert(durl+pars);
				displayAndUpdate = displayAndUpdate.bind(this);
				var myAjax = new Ajax.Request(durl,{method: 'post',parameters: pars,onLoading: gettingForm,onComplete:displayAndUpdate,onException: alertFailure});
				this.selected = new Array(); // reset selection to zero
//				location.href = "?"+pars;
//				this.updateList('');
			}
		}
		else {
			alert('Je hebt geen '+this.listclass+'(s) geselecteerd');
		}
	}


	function displayAndUpdate(originalRequest) {
		var formDisplay = displayForm.bind(this);
		formDisplay(originalRequest);

		var list_url = originalRequest.responseXML.getElementsByTagName("list_url");
		if (list_url.length>0) {
//			alert(list_url[0].firstChild.nodeValue);
			this.updateList('',list_url[0].firstChild.nodeValue);}
		else {this.updateList('')};
	}


	function edit(id) {
		var pars	= '';
		if (arguments.length>1) {
			pars = arguments[1];
		}

		newAddress = this.base_address()+'&action=edit&c='+this.listclass+'&'+pars;
		location.href = newAddress+'&'+this.name+'_id='+id;
	}







	/*
	function load(id) { // id,class,content (pars),url,target
		var options = Object.extend({
			content:		page_content,
			pars:			false,
			objectClass:	this.listclass,
			oid:			id,
			requestUrl:			'./xml.php',
			submitUrl:			'./xml.php',
			targetId:		'focuswindow',
			refreshListOnLoad:	false}, arguments[1] || { });

		if (arguments.length>1) {
			options.objectClass = arguments[1];
		}
		if (arguments.length>2 && arguments[2]) {
			options.content = arguments[2];
		}else {
			options.content = this.base_content['content'];
		}

		if (arguments.length>3 && arguments[3]) {options.pars=arguments[3];}

//		if (arguments.length>4) { // target
//			var formLoad = loadForm.bind(this,pcontent,formclass,id,_url,arguments[4]);
//		}
//		else {
//			var formLoad = loadForm.bind(this,pcontent,formclass,id,_url);
//		}


		var formLoad = openObjectForm.bind(this,id,options);
		formLoad();
	} */




	function loadObjectForm(id) {
		this.currentId = id;
		var options = Object.extend({
			content:			page_content,
			pars:				false,
			objectClass:		this.listclass,
			oid:				id,
			requestUrl:			'./xml.php',
			submitUrl:			'./xml.php',
			targetId:			'focuswindow',
			refreshListOnLoad:	false}, arguments[1] || { });

//		alert(options.content+','+options.objectClass+','+options.oid+','+options.url+','+options.pars+','+options.target);
//		var formLoad = openObjectForm.bind(this,options.content,options.objectClass,options.oid,options.url,options.pars);
		var formLoad = openObjectForm.bind(this,id,options);
		formLoad();
	}
	
	
	function reloadObject() {
		if (this.currentId) {
			this.loadForm(this.currentId,arguments[0]);
		}
	}


	
	function contentactionid(content,action,id) {
		newAddress = '?content='+content+'&action='+action+'&c='+this.listclass;
		location.href = newAddress+'&o='+id;
	}
	
	function read(id) {
		var pars = '';
		if (arguments.length>1) {pars=arguments[1];}
		newAddress = this.base_address()+'&action=read&c='+this.listclass+pars;
		location.href = newAddress+'&'+this.name+'_id='+id;
	}


// 20080724
	function addItem() { // arguments : extra content description, load url
		var options = Object.extend({
			content:		page_content,
			pars:			false,
			objectClass:	this.listclass,
			oid:			0,
			requestUrl:		'./xml.php',
			submitUrl:		'./xml.php',
			targetId:		'focuswindow'}, arguments[0] || { });

		var formLoad = openObjectForm.bind(this,0,options);
		formLoad();
		return false;
	}




// 20080901
	function insertItem() { // arguments : extra content description, load url
		var options = Object.extend({
			content:		page_content,
			pars:			false,
			objectClass:	this.listclass,
			oid:			0,
			requestUrl:		'./xml.php',
			submitUrl:		'./xml.php',
			targetId:		'focuswindow'}, arguments[0] || { });

		options.pars	= options.pars+'&action=insert';
		var formLoad = openObjectForm.bind(this,0,options);
		formLoad();
		return false;
	}




// old
	function add_item() { // arguments : extra content description, load url
		var pcontent = this.base_content['content'];
		if (arguments.length>0) {
			pcontent += '&'+arguments[0];
		}
		
		if (arguments.length>1) {
//			alert('add_item '+arguments[1]);
		  	var formLoad = loadForm.bind(this,pcontent,this.listclass,0,arguments[1]);
		}
		else {
	  		var formLoad = loadForm.bind(this,pcontent,this.listclass,0);
	  	}

		formLoad();
		return false;

		var newAddress = '?';
//		for (var i in this.base_content) {
//			newAddress += '&'+i+'='+this.base_content[i];
//		}

		newAddress += '&content='+this.base_content['content'];
		newAddress += "&action=add_"+this.name;
		location.href = newAddress;
	}


	function insert_item() { // arguments : extra content description, load url,formdisplay
		var pars = 'content=';
		pars += this.base_address(); //this.base_content['content'];
		pars += '&c='+this.listclass+'&o=0&';
		pars += '&action=insert';
		if (arguments.length>0) {
			pars += '&'+arguments[0];
		}
//		this.updateList();
//		alert(url+'?'+pars+this.name);
		onCompleteFunction = simpleUpdateList.bind(this,'');
		var myAjax = new Ajax.Request(url,{method: 'post',parameters: pars,onLoading: false,onComplete: onCompleteFunction,onException: alertFailure});
		return true;
		
		var pcontent = this.base_content['content'];
		pcontent += '&action=insert';
		if (arguments.length>0) {
			pcontent += '&'+arguments[0];
		}		


		var onCompleteFunction = true;
		if (arguments.length>2 && arguments[2]==false) {
//			formDisplay = false;
			onCompleteFunction = updateList.bind(this);
		}
		
		if (arguments.length>1) {
		  	var formLoad = loadForm.bind(this,pcontent,this.listclass,0,arguments[1], false,onCompleteFunction);
		}
		else {
	  		var formLoad = loadForm.bind(this,pcontent,this.listclass,0,false,false,onCompleteFunction);
	  	}

		formLoad();
		return false;
	}


	function edit_array(id) {
		newAddress = '?action=edit&c='+this.listclass;
//		for (var i in this.base_content) {
//			newAddress += '&'+i+'='+this.base_content[i];
//		}

		newAddress += '&content='+this.base_content['content'];
		newAddress += '&'+this.name+'_id='+id;
		newAddress += '&o='+id;
		location.href = newAddress; //+'&'+this.name+'_id='+id;
	}
	

	function edit_selected() {
		j = 0;
		index = 0;
		for(i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				j++;
				index = this.selected[i];
			}
		}
		if (j==0) {
			alert('nothing selected to edit');
		}
		else {
			if (j==1) {
				newAddress = '?content='+page_content+'&action=edit&c='+this.listclass;
				location.href = newAddress+'&'+this.name+'_id='+index;
			}
			else {
				alert('kies 1 persoon om te editen');
			}
		}
	}


	function singleSelect(id) {
		this.clearSelected();
		this.select(id);
	}
	
	function set_selected(id) {
		this.selected[this.selected.length] = id;
	}
	
	
	function set_unselected(id) {
		delete this.selected[id];
	}
	
	function switch_selected_state(name,id) {
		index = this.in_array(id);
		if (index != -1) {
			this.set_unselected(index);
		}
		else {
			this.set_selected(id);
		}
	}
	
	function mouseover(id) {
		if (this.in_array(id) == -1) {
//			document.getElementById(this.name+id).style.backgroundColor=list_mouseover_color; //'#DDDDDD';
		}
	}
	
	function has_write_rights() {
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				if (this.write_rights[this.selected[i]] == -1) {
					return false;
				}
			}
		}
		return true;
	}
	
	function activate_button(buttontype) { // 20030421
		if (document.getElementById(this.name+'_'+buttontype)) {
			editbutton = document.getElementById(this.name+'_'+buttontype);
			editbutton.src = this.buttons_dir+'button_'+buttontype+'.gif';
		}
	}
	
	
	function deactivate_button(buttontype) { // 20030421
		if (document.getElementById(this.name+'_'+buttontype)) {
			editbutton = document.getElementById(this.name+'_'+buttontype);
			editbutton.src = this.buttons_dir+'button_'+buttontype+'_disabled.gif';
		}
	}

	function clearSelected() { // 20061223
//		this.showselected();
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
//				$(this.name+this.selected[i]).style.backgroundColor='transparent';
				if ($(this.name+this.selected[i])) { // selected items may have been deleted meanwhile
					$(this.name+this.selected[i]).removeClassName('active');
				}
			}
//			this.set_unselected(i);
		}
		this.selected = new Array();
	}

	function select(id) {
		if (this.in_array(id) != -1) { // if this row is already selected
			index = this.in_array(id);
			this.set_unselected(index);
			if ($(this.name+id)) {
				$(this.name+id).removeClassName('active');
			}
		}
		else {
			this.set_selected(id);
			if (document.getElementById(this.name+id)) {
				$(this.name+id).addClassName('active');
			}
		}

		if (this.length()==1) {
			if (this.has_write_rights()) {
				this.activate_button('edit');
			}
			if (this.delete_rights[id] != -1) {
				this.activate_button('delete');
			}
		}
		else {
			if (this.length() == 0) {
				this.deactivate_button('delete');
			}
			this.deactivate_button('edit');
		}
	}
	
	
	function mouseout(id) {
		if (this.in_array(id) == -1) {
			document.getElementById(this.name+id).style.backgroundColor='transparent'; //'white';
		}
	}
	
	function in_array(value) {
		for (i=0;i<this.selected.length;i++) {
			if (this.selected[i] == value) {
				return i;
			}
		}
		return -1;
	}
	
	function showselected() {
		alert(this.selected);
	}
	
	function showrows() {
		list = eval(this.name+'_unselected');
		for(i=0;i<list.selected.length;i++) {
			if (list.selected[i]) {
//			alert(this.name+this.selected[i]);
				document.getElementById(this.name+list.selected[i]).style.display='block';
				this.set_selected(list.selected[i]);
//			alert(this.name+'_unselected'+this.selected[i]);
				document.getElementById(this.name+'_unselected'+list.selected[i]).style.display='none';
				list.set_unselected(list.selected[i]);
			}
		}
	}
	
	
	function hiderows() {
		list = eval(this.name+'_unselected');
		for(i=0;i<this.selected.length;i++) {
			if (this.selected[i]) {
				document.getElementById(this.name+this.selected[i]).style.display='none';
				this.set_unselected(list.selected[i]);
				document.getElementById(this.name+'_unselected'+this.selected[i]).style.display='block';
				list.set_selected(list.selected[i]);
			}
		}
	}






