// JavaScript Document

// prevent submission of wrong form-button (MemberFilterButton)

ProductTableField = Class.create();
ProductTableField.applyTo('#Form_EditForm_ProductsComplexTableField');
ProductTableField.prototype = {
	initialize: function() {
		Behaviour.register({
			weekSearch: function(e) {
				// IE6 doesnt send an event-object with onkeypress
				var event = (e) ? e : window.event;
				var keyCode = (event.keyCode) ? event.keyCode : event.which;
				
				if(keyCode == Event.KEY_RETURN) {
					var el = Event.element(event);
					$('ProductFilterButton').onclick(event);
					Event.stop(event);
					return false;
				}
			}
	   });
	}
}

ProductFilterButton = Class.create();
ProductFilterButton.applyTo('#Form_EditForm #ProductsComplexTableFieldFilterButton');
ProductFilterButton.prototype = {
	initialize: function() {
		this.inputFields = new Array();
		
		var childNodes = this.parentNode.getElementsByTagName('input');
		
		for( var index = 0; index < childNodes.length; index++ ) {
			if( childNodes[index].tagName ) {
				childNodes[index].resetChanged = function() { return false; }
				childNodes[index].isChanged = function() { return false; }
				this.inputFields.push( childNodes[index] );
			}
		}
		
		childNodes = this.parentNode.getElementsByTagName('select');
		
		for( var index = 0; index < childNodes.length; index++ ) {
			if( childNodes[index].tagName ) {
				childNodes[index].resetChanged = function() { return false; }
				childNodes[index].field_changed = function() { return false; }
				this.inputFields.push( childNodes[index] );
			}
		}
	},
	
	isChanged: function() {
		return false;
	},
	
	onclick: function(e) {
		if(!$('ctf-ID') || !$('ProductFieldName')) {
			return false;
		}
		
		var updateURL = "";
		updateURL += Event.findElement(e,"form").action;
		// we can't set "fieldName" as a HiddenField because there might be multiple ComplexTableFields in a single EditForm-container
		updateURL += "&fieldName=ProductsComplexTableField";
		updateURL += "&action_callfieldmethod&&methodName=ajax_refresh&";
		for( var index = 0; index < this.inputFields.length; index++ ) {
			if( this.inputFields[index].tagName ) {
				updateURL += this.inputFields[index].name + '=' + encodeURIComponent( this.inputFields[index].value ) + '&';
			}
		}
		updateURL += 'ajax=1' + ($('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '');

		new Ajax.Request( updateURL, {
			onSuccess: Ajax.Evaluator,
			onFailure: function( response ) {
				errorMessage('Could not filter results: ' + response.responseText );
			}.bind(this)
		});
		
		return false;	
	}
}

