/**
 * @copyright		(c) 2009, Viktor Steinke. All rights reserved.
 * @author			Viktor Steinke <steinke@brainworxx.de>
**/

var Helpworxx = Class.create({
	initialize: function() {
		this.cookie = new Cookies();

		this.attachNumElements = 0;
		this.attachElementRowUniqueId = 0;
		this.additionNumElements = 0;
		this.additionElementRowUniqueId = 0;
		this.noTickets = 0;
		this.quickInt = 0;
		this.quickActive = 0;

		if (window.cp.helpworxxData != undefined) {
			if (window.cp.helpworxxData.mode != undefined) {
				this.mode = window.cp.helpworxxData.mode;
                        }else {
				this.mode = null;
                        }
			if (window.cp.helpworxxData.siteUrl != undefined) 
				this.siteUrl = cp.helpworxxData.siteUrl;

			if (window.cp.helpworxxData.mode != undefined) 
				this.admin = cp.helpworxxData.admin;
				
			if (window.cp.helpworxxData.isFilterAraeOpen != undefined) 
				this.isFilterAraeOpen = cp.helpworxxData.isFilterAraeOpen;

			if (window.cp.helpworxxData.displayClosedTickets != undefined) 
				this.displayClosedTickets = cp.helpworxxData.displayClosedTickets;
		}
		
		this.numTextArea = 0;
		this.maxTextArea = 1;
		this.textAreaRowUniqueId = 0;

		this.toolbox();
		this.addListeners()

		if($('user')) { $('user').focus(); }
		if(this.mode == "new" || (this.mode == "edit" && $('toSelect'))) {  this.initValidator(); }
	},

	toolbox: function(){
		this.vitalCases = {
			attachmentsFields: function(){
				$('attachButton').observe('click', function(event){
					var contentBeginn = '<div style="float:left; position: relative;"><input type="file" name="tx_xxhelpworxx_pi1[appendix][]" size="1" class="inputfile" /><div class="inputFileExtra"><div class="inputFileButton"></div><div class="inputFileLabel"></div></div></div><div style="float: right"><a href="Javascript:cp.helpworxx.removeElement(';
					var contentEnd = ')" class="removeAttachmentButton">Remove</a></div>';
	
					if (!$('attachButtonHint').visible()) {
						new Effect.toggle($('attachButtonHint'));
					}

					cp.helpworxx.addElement('attachments', 'attachmentsInputfile', 6, 'inputfileRow', contentBeginn, contentEnd, 'attachButtonHint');
				});
			},
			
			updateSelect: function(id, callBack){
				var local = this;
				for (var i = 0; i < id.length; i++) {
					if($(id[i])){
						if ($(id[i]).identify() == "statusSelect" && !local.sI) { local.sI = $(id[i]).selectedIndex }
						
						$(id[i]).observe('change', function(event){
							if(this.identify() == "statusSelect") {
								ticketClosed = false;
								if(cp.helpworxxData.whoIs != cp.helpworxxData.assigned){
									if(this.value == "6"){
										var answer = confirm("Sind Sie sicher, dass Sie dieses Ticket schließen möchten?\r\n\r\nWichtig!\r\nDas Ticket darf nur von der Person geschlossen werden, der auch das Ticket zugewiesen wurde.");
										if (answer) {
											var e = this.previous('.selectLabel');
											$(e.identify()).update(local.getContentFromOption(this, null))
											local.sI = this.selectedIndex;
										}else{
											ticketClosed = true;
											var lastOption = $A($("statusSelect").getElementsByTagName('option'));
											lastOption[local.sI].selected = true;
										}
									}							
								}
								
								if(this.value == "5" && !ticketClosed){
									var answer = confirm("Sind Sie sicher, dass Sie dieses Ticket beenden möchten?");
									if (answer) {
										var e = this.previous('.selectLabel');
										$(e.identify()).update(local.getContentFromOption(this, null))
										local.sI = this.selectedIndex;
									}else{
										var lastOption = $A($("statusSelect").getElementsByTagName('option'));
										lastOption[local.sI].selected = true;
									}
								}else{
									var e = this.previous('.selectLabel');
									$(e.identify()).update(local.getContentFromOption(this, null))
									
									local.sI = this.selectedIndex;
								}
							}else{
								var e = this.previous('.selectLabel');
								$(e.identify()).update(local.getContentFromOption(this, null))
							}

							if (callBack) eval(callBack);
						});
					}
				}
			},
			
			initCalendar: function(id){
				var local = this;
				for (var i = 0; i < id.length; i++) {
					if($(id[i])){
						$(id[i]).observe('click', function(event){
							var e = this.ancestors()[0].next('div.calendarSkin');
							$(e.identify()).show();

							cp.helpworxx.makeCalendar(this.identify(), e.identify());
						});
					}
				}
			},

			getContentFromOption: function(selectObj, labelObj){
				var selectOptions = $A(selectObj.getElementsByTagName('option'));
				for (var i = 0; i < selectOptions.length; i++) {
					if(!labelObj){
						if($(selectOptions[i]).selected){
							return $(selectOptions[i]).innerHTML;
						}
					}else{
						if (labelObj.innerHTML == $(selectOptions[i]).value) {
							$(selectOptions[i]).selected = true;
						}
					}
				}
			},
			
			setClickAndBack: function(type, beginFrom, e, className, callFunc, callFuncClick, callFuncBack){
				switch(type){
					case "default":
						if($(e).length == undefined){
							Event.observe($(e), 'click', function(event) {
								if (!this.c) {
									this.addClassName(className);
									this.c = true;
		
									if(callFuncClick) eval(callFuncClick);
								}else if (this.c) {
									this.removeClassName(className);
									this.c = false;
		
									if(callFuncBack) eval(callFuncBack);
								}
								if (callFunc) eval(callFunc);
							});
						}else{
							for (var j = beginFrom; j < e.length; j++) {
								if(true){
									Event.observe($(e[j]), 'click', function(event) {
										if (!this.c) {
											this.addClassName(className);
											this.c = true;
				
											if(callFuncClick) eval(callFuncClick);
										}else if (this.c) {
											this.removeClassName(className);
											this.c = false;
				
											if(callFuncBack) eval(callFuncBack);
										}
										if (callFunc) eval(callFunc);
									});
								}
							}
						}
					break;

					case "tr":
						$$('.listTicketContainerItem').each( function ( tr ) {
							tr.childElements().each( function ( td, index ) {
								if(td.readAttribute('noselect') == "false"){
									Event.observe(td, 'click', function(event) {
										if (!this.c) {
											this.c = true;
										}else if (this.c) {
											this.c = false;
										}
	
										this.up('tr').toggleClassName('select');
										this.up('tr').down('.listTicketContainerItemAktionSelect').toggleClassName('setSelectMarker');
										this.up('tr').down('.listTicketContainerItemPriority').toggleClassName('noSelect'); 
										this.up('tr').down('.listTicketContainerItemStatus').toggleClassName('noSelect');
										
										if(this.up('tr').hasClassName('select'))
											this.up('tr').down('.listTicketContainerItemId').writeAttribute('print','true');
										else
											this.up('tr').down('.listTicketContainerItemId').removeAttribute('print','true');
	
										$('printInfoText').hide();
									});
								}
							});
						});
					break;

					case "attachFileDetail":
						$$('.dataPoolContainer').each( function ( element, index ) {		
							var container = element.down(0);
							var detailContainer = $(container.identify()+"_Container");
				
							Event.observe(container, 'click', function(event) {
								if(!detailContainer.visible()){
									$$('.dataPoolContainer').invoke('removeClassName','dataPoolContainerIsOpen');
									$$('.appendixDetailContainer').invoke('removeClassName','detailContainerIsOpen');									
									$$('.appendixDetailContainer').invoke('hide');
									element.addClassName('dataPoolContainerIsOpen');
									detailContainer.addClassName('detailContainerIsOpen');
									detailContainer.show();
								}
							});		
						});
					break;
					
					case "quickAddition":		
						$$('.addQuickAdditionalFromListView').each( function ( element, index ) {					
							var container = $(element).up(0).down('.addQuickAdditionalConatiner');

							Event.observe($(element), 'click', function(event) {
								cp.helpworxx.quickInt = 1;

								if(!$(container).visible()){
									$$('.addQuickAdditionalFromListView').invoke('removeClassName','quickAdditinalAreaIsOpen');
									$$('.addQuickAdditionalConatiner').invoke('hide'); 
					
									$(element).addClassName('quickAdditinalAreaIsOpen');
									$(container).show();
								}
					
								$(container).down('input.qucikTicketID').value = $(container).up(0).next('td.listTicketContainerItemId').innerHTML;
								Event.observe($(container), 'click', function(event){ cp.helpworxx.quickInt = 1;});
								Event.observe($(container).down('button.quickHideButton'), 'click', function(event){ $$('.addQuickAdditionalConatiner').invoke('hide'); });
							});		
						});					
					break;
				}
			},
			
			setOverandOut: function(t, e, mouseoverFunc, mousemoveFunc, mouseoutFunc, extraFunc){
				for (var j = 0; j < $(e).length; j++) {
					switch(t){
						case 'default':
							Event.observe($(e[j]), 'mouseover', function(event){ if(mouseoverFunc) eval(mouseoverFunc) });
							Event.observe($(e[j]), 'mousemove', function(event){ if(mousemoveFunc) eval(mousemoveFunc) });
							Event.observe($(e[j]), 'mouseout', function(event){ if(mouseoutFunc) eval(mouseoutFunc) });
							
							if(extraFunc) eval(extraFunc);
						break;
							
						case 'whois':
							if ($(e[j]).down(0).down(4).innerHTML != '') {
								if(j=="0" || j=="1" || j=="2" || j=="3"){ $(e[j]).addClassName('theFirst4TD') }
								
								Event.observe($(e[j]), 'mouseover', function(event){ if(mouseoverFunc) eval(mouseoverFunc) });
								Event.observe($(e[j]), 'mousemove', function(event){ if(mousemoveFunc) eval(mousemoveFunc) });
								Event.observe($(e[j]), 'mouseout', function(event){ if(mouseoutFunc) eval(mouseoutFunc) });
								
								if(extraFunc) eval(extraFunc);
							}	
							

						break
					}
				}
			},
			
			setMyAttributte: function(e, attributeName, attributeValue){
				for (var j = 0; j < $(e).length; j++) { $(e[j]).writeAttribute(attributeName, attributeValue) };
			},
			
			setPrintButton: function(e){
				Event.observe($(e), 'click', function(event){
					if(window.cp.helpworxxData.showUid != undefined){
						this.writeAttribute("href",cp.helpworxxData.siteUrl+"&no_cache=1&tx_xxhelpworxx_pi1[print]=1&tx_xxhelpworxx_pi1[printTicket]="+cp.helpworxxData.showUid);
					}else{
						var isTicketSelected = false;
						var ticketIDs = "";
						var td = $A($('listTicketContainerItemsTable').getElementsByTagName('td'));
						for (var j = 0; j < $(td).length; j++) {
							if($(td[j]).readAttribute('print')){
								ticketIDs += $(td[j]).innerHTML+",";
								isTicketSelected = true;
								if ($('printInfoText').show()) { $('printInfoText').hide(); };
							}else{
								this.removeAttribute("href");
								if(!isTicketSelected)
									if ($('printInfoText').hide()) { $('printInfoText').show(); };
							}
						}
		
						if(isTicketSelected){
							this.writeAttribute("href",cp.helpworxxData.siteUrl+"&no_cache=1&tx_xxhelpworxx_pi1[print]=1&tx_xxhelpworxx_pi1[printTicket]="+ticketIDs.slice(0,-1));
						}
					}
				});
			},
			
			manageSelection: function(cookie){
				if (cookie.get('id')) {
					$('saveSelection').hide();
					$('refreshSelection').show();
					$('delSelection').show();
					
					var cookieArray = cookie.get('id').split(',');
					cookieArray.each(function(id){
						$$('.listTicketContainerItem').each(function(tr){
							tr.childElements().each(function(td){
								if (td.innerHTML == id) {
									td.up('tr').toggleClassName('select');
									td.up('tr').down('.listTicketContainerItemAktionSelect').toggleClassName('setSelectMarker');
									td.up('tr').down('.listTicketContainerItemPriority').toggleClassName('noSelect');
									td.up('tr').down('.listTicketContainerItemStatus').toggleClassName('noSelect');
									
									if (td.up('tr').hasClassName('select')) 
										td.writeAttribute('print', 'true');
									else 
										td.removeAttribute('print', 'true');
									
									$('printInfoText').hide();
								}
							});
						});
					});
				}else {
					$('saveSelection').show();
					Event.observe($('saveSelection'), 'click', function(event){
						var ticketIDs = "";
						var td = $A($('listTicketContainerItemsTable').getElementsByTagName('td'));
						var goToSaveData = false;
						for (var j = 0; j < td.length; j++) {
							if ($(td[j]).readAttribute('print')) {
								ticketIDs += $(td[j]).innerHTML + ",";
								goToSaveData = true;
							}
						}
						
						if (goToSaveData) {
							if (ticketIDs.slice(0, -1)) {
								cookie.set('id', ticketIDs.slice(0, -1), 31);
							}
							$('saveSelection').hide();
							$('refreshSelection').show();
							$('delSelection').show();
							
							goToSaveData = false;
						}else{
							if ($('printInfoText').hide()) { $('printInfoText').show(); };
						}
					});
				}
				
				Event.observe($('refreshSelection'), 'click', function(event){
					var ticketIDs = "";
					var td = $A($('listTicketContainerItemsTable').getElementsByTagName('td'));
					for (var j = 0; j < td.length; j++) {
						if ($(td[j]).readAttribute('print')) {
							ticketIDs += $(td[j]).innerHTML + ",";
						}
					}
					
					if(ticketIDs.slice(0, -1)){
						cookie.set('id', ticketIDs.slice(0, -1), 31);
					}
				});
				
				Event.observe($('delSelection'), 'click', function(event){
					var cookieArray = cookie.get('id').split(',');
					cookieArray.each(function(id){
						$$('.listTicketContainerItem').each(function(tr){
							tr.childElements().each(function(td){
								if (td.innerHTML == id && td.up('tr').down('.listTicketContainerItemAktionSelect').hasClassName('setSelectMarker')) {
									td.up('tr').toggleClassName('select');
									td.up('tr').down('.listTicketContainerItemAktionSelect').toggleClassName('setSelectMarker');
									td.up('tr').down('.listTicketContainerItemPriority').toggleClassName('noSelect');
									td.up('tr').down('.listTicketContainerItemStatus').toggleClassName('noSelect');
									$('printInfoText').hide();
									
									if (td.up('tr').hasClassName('select')) 
										td.writeAttribute('print', 'true');
									else 
										td.removeAttribute('print', 'true');
								}
							});
						});					
					});
					
					cookie.clear('id');
					$('saveSelection').show();
					$('refreshSelection').hide();
					$('delSelection').hide();
				});				
			}
		};
	},
	
	addListeners: function(){
		if(this.mode == "list"){		
			this.vitalCases.manageSelection(this.cookie);
			this.makeSearchAndFilterArea();
				
			Event.observe(document.body, 'click', function(event){
				if (cp.helpworxx != null) {
					if (!cp.helpworxx.quickInt) {
						$$('.addQuickAdditionalConatiner').invoke('hide');
						$$('.addQuickAdditionalFromListView').invoke('removeClassName', 'quickAdditinalAreaIsOpen');
					}
					cp.helpworxx.quickInt = 0;
				}
			});
			
			/*
			if (this.cookie.get('v3')) {
			}else{
				$('helpworxxV3').show();
				if($('helpworxxV3Close')) $('helpworxxV3Close').observe('click', function(event){ $('helpworxxV3').hide(); cp.helpworxx.cookie.set('v3', 'true') });
				if($('helpworxxV3Help')) $('helpworxxV3Help').observe('click', function(event){ $('helpworxxHelpList').show() });
			}		
			
			*/
			
			$('helpTicket').show();
			$('helpTicket').observe('click', function(event){ $('helpworxxHelpList').show() });
			if($('helpworxxHelpListClose')) $('helpworxxHelpListClose').observe('click', function(event){ $('helpworxxHelpList').hide() });
			
			$('printTicket').show();
			this.vitalCases.setPrintButton($('printTicketButton'));
			
			this.vitalCases.setClickAndBack("quickAddition", null, null, null, null, null, null);
			this.vitalCases.setClickAndBack("tr", null, null, null, null, null, null);
			this.vitalCases.setOverandOut('default', $A($('listTicketContainerItemsTable').getElementsByTagName('tr')), "var t = this.childElements(); t[0].addClassName('addSelectMarkerOver');", "var t = this.childElements(); t[0].addClassName('addSelectMarkerOver');", "var t = this.childElements(); t[0].removeClassName('addSelectMarkerOver');", null);
			this.vitalCases.setOverandOut('default', $$('.attachListIcon'), "this.up(0).down(1).show()", "this.up(0).down(1).show()", "if (this.up(0).down(1).show()) { this.up(0).down(1).hide() }", "Event.observe($$('.attachListContent')[j], 'mousemove', function(event){ this.show() }); Event.observe($$('.attachListContent')[j], 'mouseout', function(event){ this.hide() })");
		
			if(this.admin)
				this.vitalCases.setOverandOut('whois',$$('.listTicketContainerItemFromPers'),"this.down(0).show()","this.down(0).show()","if (this.down(0).show()) { this.down(0).hide() }", "Event.observe($$('.whoIsInformation')[j], 'mousemove', function(event){ this.show() })");
			else
				this.vitalCases.setOverandOut('whois',$$('.listTicketContainerItemAssTo'),"this.down(0).show()","this.down(0).show()","if (this.down(0).show()) { this.down(0).hide() }", "Event.observe($$('.whoIsInformation')[j], 'mousemove', function(event){ this.show() })");
		}

		if(this.mode == "new"){ 
			if(this.admin){
				$('holder').setStyle({
					height: $('formAreaNew').getHeight()+'px'
				});

				var f = $$('.formAreaAdminClass')[0];
				f.setOpacity(0.5);
				this.vitalCases.updateSelect($w('customerSelect'), "$('formAreaAdmin').submit()");
			}else{
				this.selectMultiple();
				this.vitalCases.attachmentsFields();						
				this.vitalCases.updateSelect($w('selectPrio category area1 area2 area3 area4'), null);
				this.vitalCases.initCalendar($w('formAreaNewInputReleasedate'));
			}
		}
		
		if(this.mode == "edit"){
			this.ticketNavigationTabs = new TabView("ticketNavigation");
			if ($$('.newTicketContainerCenterContentLeftAuthPers')[0].style.display != 'none') this.selectMultiple();
			this.vitalCases.attachmentsFields();
			this.vitalCases.setPrintButton($('printTicketButton'));
			this.vitalCases.updateSelect($w('statusSelect toSelect selectPrio category area1 area2 area3 area4'), null);
			this.vitalCases.setClickAndBack("attachFileDetail", null, null, null, null, null, null);
                        //edit rm calender auch im edit modus
                        if(this.admin || window.cp.helpworxxData.adminRM != undefined){
                            this.vitalCases.initCalendar($w('formAreaNewInputReleasedate'));
                        }

			if ($('Open_Log_Large_Container')) {
				$('error').setStyle({
					marginLeft: '-147px'
				});
				this.vitalCases.setClickAndBack("default", 0, $('Open_Log_Large_Container'), "logContainerIsOpened", "Effect.toggle($('Log_Large_Container'), 'blind');", null, null);
			}

			$('additionButton').observe('click', function(event){
				var contentBeginn = '<div><textarea name="tx_xxhelpworxx_pi1[addition]" class="inputfile"></textarea></div><a href="Javascript:cp.helpworxx.removeElement(';
				var contentEnd = ')" class="removeAdditionsButton">Remove</a>';

				cp.helpworxx.addElement('addition', 'additionTextArea', 1, 'textareaAdditionsRow', contentBeginn, contentEnd, '');
			});
		}
		
		if(this.mode == "print"){
			for (var j = 0; j < $$('.whoIsInformation').length; j++) {
				$$('.whoIsInformation')[j].show();
			}
		}
	},

	initValidator: function() {
		for(var i = 0; i < document.forms.length; i++) {
			var f = document.forms[i];
			var needsValidation = false;

			for(var j = 0; j < f.elements.length; j++) {
				var e = f.elements[j];

				if (e.type != "text" && e.type != "textarea" && e.type != "select-one") { continue; }
				var pattern = "";

				if(Element.hasClassName(e, 'required')){ pattern = "\\S";}
				if (pattern !== "") {
					e.setAttribute("pattern", pattern);
					e.onchange = this.validateOnChange;
					needsValidation = true;
				}
			}
			if (needsValidation) { f.onsubmit = this.validateOnSubmit; }
		}
	},

	validateOnChange: function() {
		var e = this;
		var pattern = e.getAttribute("pattern");
		var value = this.value;
		var label = $A(this.parentNode.parentNode.getElementsByTagName('label'));

		if ((value.search(pattern) == -1)) {
			Element.addClassName(label[0], 'invalid');
			Element.addClassName(this, 'invalid');
		}else {
			Element.removeClassName(label[0], 'invalid');
			Element.removeClassName(this, 'invalid');
		}
	},
	
	validateOnSubmit: function() {
		var invalid = false;
		var formID = "formArea" + cp.helpworxxData.mode.capitalize();

		for(var i = 0; i < this.elements.length; i++) {
			var e = this.elements[i];

			if ((e.type == "text" || e.type == "textarea" || e.type == "select-one") && e.hasAttribute('pattern')) {
				e.onchange();
				if(Element.hasClassName(e, 'invalid')) {
					if ($(formID)) {
						if (!$('error').visible()) {
							new Effect.toggle($('error'));
						}
					}

					invalid = true;
				}
			}
		}

		if(this.id == formID) {
			if (invalid) {
				return false;
			}else {
				return true;
			}
		}
	},

	makeSearchAndFilterArea: function(){
		this.setFilterPropeteries();
		
		if (this.cookie.get('searchValue')) {
			$('flt2_listTicketContainerItemsTable').value = this.cookie.get('searchValue');
			$('resetSearchInput').show();
		}
		if (this.cookie.get('resultPageValue')) $('slcResultsTxt_listTicketContainerItemsTable').innerHTML = this.cookie.get('resultPageValue');

		$('formAreaSearch').observe('submit', function(event){ cp.helpworxx.cookie.set('searchValue', $('flt2_listTicketContainerItemsTable').value) });		
		$('slcResults_listTicketContainerItemsTable').observe('change', function(event){ cp.helpworxx.cookie.set('resultPageValue', this.value) });

		this.vitalCases.updateSelect($w('flt4_listTicketContainerItemsTable flt5_listTicketContainerItemsTable flt6_listTicketContainerItemsTable flt7_listTicketContainerItemsTable'), null);
		this.vitalCases.updateSelect($w('slcResults_listTicketContainerItemsTable'), null);
	},
	
	setFilterPropeteries: function(){
		var local = this;
		var noResult = '<div id="ErrorMSGFilter" style="padding-left: 5px; font-weight: bold; color: red;">Es wurde keine Tickets gefunden</div>';
				
		if(this.admin){
			var filter_ids = ['', '', 'slcTitle','slcID','slcStart','slcEnde', 'slcKunde','slcKategorie','slcAn','slcVon','slcPrio','slcStatus'];
		}else{
			var filter_ids = ['', '', 'slcTitle','slcID','slcStart','slcEnde', '','slcKategorie','slcAn','','slcPrio','slcStatus'];
		}
		
		
		var props = {
			filters_row_index: 1,
			remember_grid_values: true,  
			remember_page_number: true,  
			remember_page_length: true,			
			paging: true,
			paging_length: 15,
			page_selector_type: "input",
			btn_next_page_text: '',
			btn_prev_page_text: '',			
			results_per_page: ['15',[15,25,50,100]],
			alternate_rows: true,
			display_all_text: '',
			rows_counter: true,
			rows_counter_text: "Zeige Ticket: ",
			loader: true,
			loader_html: '<img src="fileadmin/layout/spinner.gif" alt="" style="vertical-align:middle;" />',
			loader_target_id: 'statusDiv',
			status: true,
			set_cookie_duration: 250000,
						 
			status_bar_target_id: 'statusDiv',
			msg_filter: '<img src="fileadmin/layout/spinner.gif" alt="" style="vertical-align:middle;" />',
			msg_clear: '<img src="fileadmin/layout/spinner.gif" alt="" style="vertical-align:middle;" />',
			col_0: "none",
			col_1: "none",
			col_2: "input",
			col_3: "input",
			col_4: "select",
			col_5: "select",
			col_6: "select",
			col_7: "select",
			col_8: "multiple",
			col_9: "multiple",
			col_10: "none",
			col_11: "multiple",
			on_after_filter: function(){
				if ($('getNumItems').innerHTML == "0") {
					$('listTicketContainerItemsTable').hide();
					$('rdiv_listTicketContainerItemsTable').hide();

					if (local.noTickets == 0) {
						new Insertion.Before($('inf_listTicketContainerItemsTable'), noResult);
						if($('printTicket').show()){$('printTicket').hide();};
						if($('helpTicket').show()){$('helpTicket').hide();};
						if($('printInfoText').show()){$('printInfoText').hide();};
						if($('saveSelection').show()){$('saveSelection').hide();};
						local.noTickets++;
					}
				}
				else {
					$('listTicketContainerItemsTable').show();
					$('rdiv_listTicketContainerItemsTable').show();
					$('printTicket').show();
					$('helpTicket').show();
					$('saveSelection').show();
					local.noTickets = 0
					if ($('ErrorMSGFilter')) 
						$('ErrorMSGFilter').remove();
				}
			},
			external_flt_grid: true,
			external_flt_grid_ids: filter_ids
		}

		setFilterGrid("listTicketContainerItemsTable",props);

		$('slcResultsTxt_listTicketContainerItemsTable').addClassName('selectLabel');
		$('loaderListContent').hide();
		if($('loaderListContent').hide()){ $('listTicketContainerItemsTable').show(); }
		tf_listTicketContainerItemsTable.onSlcChange = false;

		if(!this.admin){
			if($A($('slcKunde').getElementsByTagName('select')) == ""){ $('slcKunde').up(0).remove() }
			if($A($('slcVon').getElementsByTagName('select')) == ""){ $('slcVon').up(0).remove() }
		}

		if(this.displayClosedTickets){$('showClosedTickets').addClassName('showClosedTicketsAktive') }
		if (this.isFilterAraeOpen) {
			$('filterContainer').show();
			$('openFilterArea').addClassName('filterContainerIsOpened');
			this.vitalCases.setClickAndBack("default", 0, $('openFilterArea'), "filterContainerIsOpened", "Effect.toggle($('filterContainer'), 'blind');", "$('openFilterArea').removeClassName('filterContainerIsOpened')", "$('openFilterArea').addClassName('filterContainerIsOpened')");
		}else {
			this.vitalCases.setClickAndBack("default", 0, $('openFilterArea'), "filterContainerIsOpened", "Effect.toggle($('filterContainer'), 'blind');", null, null);
		}

		var cookie = document.cookie;
		var result = cookie.search(/tf_flts_listTicketContainerItemsTable=/);
		if (result == -1){
			tf_listTicketContainerItemsTable.ClearFilters();
			this.clearAllFilterValues();	
		}
	},

	clearAllFilterValues: function(){
		for(var y=0; y < $$('.selectLabel').length; y++){
			if(!$$('.selectLabel')[y].up(0).identify() == "whereMustSearch"){ $$('.selectLabel')[y].update(''); }
		}
	},

	makeCalendar: function(id, calendar){
		Object.extend(Date.prototype, {
			monthnames: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
			daynames: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]
		});

		var calendarOptions = Object.extend({
			prevbutton: '<div class="prev"><div></div></div>',
			nextbutton: '<div class="next"><div></div></div>',
			closebutton: '<div class="close"><div></div></div>',
			closeeffect: Element.hide,
			weekdaystart: 1
		});

		var calendar = new scal(calendar, id, calendarOptions);
	},

	addElement: function(type, id, maxElements, className, contentBeginn, contentEnd, hint) {
		if(type == 'attachments'){
			if(this.attachNumElements < maxElements) {
				if(this.attachNumElements === 0) { className = className+' first'; }
	
				var uniqueId = className+'_'+this.attachElementRowUniqueId;
				var removeElementsParams = "'"+type+"', '"+id+"', '"+uniqueId+"', '"+hint+"', '"+className.sub(' first', '')+"'"
				var div = '<div id="' + uniqueId + '" class="' + className + '">' + contentBeginn + removeElementsParams + contentEnd + '<div class="clear">&nbsp;</div></div>';
				$(id).insert(div);
								
				this.attachNumElements ++;
				this.attachElementRowUniqueId ++;
			}
			
			$$('.inputfile').each(function(input){
				input.observe('change', function(event) {
					var label = input.up(0).down(1).down(1);
					label.innerHTML = this.value;
				});
			});
		}else{
			if(this.additionNumElements < maxElements) {
				if(this.additionNumElements === 0) { className = className+' first'; }
	
				var uniqueId = className+'_'+this.additionElementRowUniqueId;
				var removeElementsParams = "'"+type+"', '"+id+"', '"+uniqueId+"', '"+hint+"', '"+className.sub(' first', '')+"'"
				var div = '<div id="' + uniqueId + '" ><div class="textareaAdditionsTopRow"></div><div class="' + className + '">' + contentBeginn + removeElementsParams + contentEnd + '<div class="clear">&nbsp;</div></div><div class="textareaAdditionsBottomRow"></div><div class="clear">&nbsp;</div></div>';
				$(id).insert(div);

				this.additionNumElements ++;
				this.additionElementRowUniqueId ++;
			}
		}
	},

	removeElement: function(type, id, uniqueId, hint, className) {
		$(uniqueId).remove();

		var firstRow = $(id).select('div.'+className).first();
		if(firstRow !== undefined) {
			firstRow.addClassName('first');
		}
		
		if (type == 'attachments') {
			if (this.attachNumElements == "1") {
				if (hint) 
					new Effect.toggle($(hint));
			}
			
			this.attachNumElements--;
		}else{
			if (this.additionNumElements == "1") {
				if (hint) 
					new Effect.toggle($(hint));
			}
			
			this.additionNumElements--;
		}
	},

	selectMultiple: function() {
		var local = this;
		
		var authPerson = new Control.SelectMultiple('authPerson', 'authPersonOptions', {
			checkboxSelector: 'div.authPersonItem div input[type=checkbox]',
			nameSelector: 'div.authPersonItem div.authPersonItemName',
			overflowLength: 30,
			afterChange: function(){
				if (authPerson && authPerson.setSelectedRows) {
					authPerson.setSelectedRows();
					
				}
			}
		});

		if (window.cp.helpworxxData.authPersonDefaultValue != undefined) {
			authPerson.setValue(window.cp.helpworxxData.authPersonDefaultValue);

			var value = this.vitalCases.getContentFromOption($('authPerson'), null);
			$('authPersonLabel').update(value.truncate(24));
		}

		$('authPersonOpen').observe('click', function(event){
			$(this.select).style.visibility = 'hidden';
			$(this.container).show();
			Event.stop(event);
			
			return false;
		}.bindAsEventListener(authPerson));

		authPerson.setSelectedRows = function(){  
			authPerson.checkboxes.each(function(checkbox){  
				var check = authPerson.checkboxes;
				var count = 0;
		
				for (var i = 0; i < check.length; i++) {
					if (!check[i].checked) { count++ }			
					if(check[i].checked){
						var value = local.vitalCases.getContentFromOption($('authPerson'), null);
						$('authPersonLabel').update(value.truncate(25));
					}
				}
				
				if(count == check.length){
					$('authPersonLabel').update("");
					var input = new Element('input', {
						type: "hidden",
						value: "0",
						name: "tx_xxhelpworxx_pi1[auth_persons]"
					});
					$('xcd').insert(input, "bottom");
				}
			});
			
			
		}.bind(authPerson);

		$('authPersonClose').observe('click', function(event){
			$(this.select).style.visibility = 'visible';
			$(this.container).hide();
			Event.stop(event);

			return false;
		}.bindAsEventListener(authPerson));
	}
});

function initHelpworxx(){
	cp.helpworxx = new Helpworxx();
}
document.observe("dom:loaded",initHelpworxx);
