function initDialog() {
	EasyKat.Configuration.app.startRequest("ajax/ConfigInitialLoad");	
}


// Main Namespace
// Den ganzen Kram könnte man mit Templates lösen,
// das wäre bestimmt schneller
var EasyKat = {};

EasyKat.Configuration = {};

EasyKat.Configuration.config = {};
EasyKat.Configuration.states = {};

EasyKat.Configuration.app = function() {
	var onConfigchange = function(evt) {
		var element = Event.element(evt);
		// var table = element.up('table');
		EasyKat.Configuration.app.startRequest( "ajax/ConfigChanged", element );
	};
	
	var onCheckConfigchange = function(evt) {
		var element = Event.element(evt);
		if (element)	{
		    var elValue = element.getValue();
		    if (element.Lb) {
		        var strTmp = "(" + element.Lb.replace(/\|/g, "'") + ")";
		        var clsLb = eval(strTmp);
		        if (clsLb != null)	{
			        var bMsg = clsLb.bIncl ? elValue < clsLb.dVal : elValue <= clsLb.dVal;
			        if (bMsg && clsLb.strMsg != "")	{
				        alert(clsLb.strMsg);
				        element.focus();
				        return;
			        }
		        }
		    }
		    if (element.Ub) {
		        var clsUb = eval("(" + element.Ub.replace(/\|/g, "'") + ")");
		        if (clsUb != null)	{
			        var bMsg = clsUb.bIncl ? elValue > clsUb.dVal : elValue >= clsUb.dVal;
			        if (bMsg && clsUb.strMsg != "")	{
				        alert(clsUb.strMsg);
				        element.focus();
				        return;
			        }
		        }
		    }
	    }
	    
		// var table = element.up('table');
		EasyKat.Configuration.app.startRequest( "ajax/ConfigChanged", element );
	};
	
	var onConfigButton = function(evt) {
		var element = Event.element(evt);
		EasyKat.Configuration.app.startRequest( "ajax/ConfigChanged", element, element.cfgaction);
	};
	
	var onInfoclick = function(evt) {
		var element = Event.element(evt);
	    if (element.doc)
	        window.open(element.doc);
	};
	
	var getCtrl = function(config) {
		return EasyKat.Configuration.ctrl[config.ctrltype](config);
	};
	
	return {
		startRequest: function(url, element, strAction) 
		{
			// debugger;
			if (!strAction)
			    strAction = "CFG";
			$('EKSUBMITEVENT').setValue(strAction);
			
		    var form = Form.serialize(document.forms[0]);
		    // form = "";
		    var strTmp = typeof XMLHttpRequest; 
		    // form = "port=8089&" + form;
			
			if (element)
			{				
		        if (element.type == "radio")
				    form += "&CHANGEDCTRLID=" + element.name;
		        else
				    form += "&CHANGEDCTRLID=" + element.id;
				    
				form += "&EKSUBMITEVENTPAR2=" + element.attrid;
		        if (element.type == "checkbox")
				    form += "&EKSUBMITEVENTPAR3=" + element._value;
				else if (element.type == "button")
				    form += "&EKSUBMITEVENTPAR3=" + element.cfgvalue;
				else
				    form += "&EKSUBMITEVENTPAR3=" + element.value;
				
				// var Changed =  element.id;
			}


			//form += "&EKSUBMITEVENT=" + strAction;
			new Ajax.Request(url, {
				parameters: {
					'__eksubmit_values__' : form
				},
				
				onSuccess: function(transport) {
			        try {
					    EasyKat.Configuration.config = eval("(" + transport.responseText + ")");
					}
					catch(e)    {alert("error in onSuccess");}
							
					var conf 	= EasyKat.Configuration.config;
					var Res = "";
					if ( conf instanceof Array ) {
						for ( var nCtrl=0; nCtrl < conf.length; ++nCtrl ) {
							if ( conf[nCtrl].ctrltype !== undefined )
								Res += EasyKat.Configuration.ctrl[conf[nCtrl].ctrltype](conf[nCtrl]);
						}
					}
					else if ( typeof conf == "object" ) {
						if ( conf.ctrltype !== undefined )
							Res += EasyKat.Configuration.ctrl[conf.ctrltype](conf);
					} else if (conf === undefined){
					    Res += "&nbsp;";
					}
					else {
						Res += content;
					}
					configTab = $('IDC_CONGIG_TAB');
					configTab.update( Res );
					
					configTab.select('input[type="text"]').each( function( ctrl ) {
						ctrl.observe('change', onCheckConfigchange );
					});
					
					configTab.select('select').each( function( ctrl ) {
						ctrl.observe('change', onConfigchange );
					});
										
					configTab.select('input[type="checkbox"]').each( function( ctrl ) {
						ctrl.observe('click', onConfigchange );
						ctrl.stopObserving('change', onConfigchange );
					});
					
					configTab.select('input[type="radio"]').each( function( ctrl ) {
						ctrl.observe('click', onConfigchange );
						ctrl.stopObserving('change', onConfigchange );
					});
					
					configTab.select('button').each( function( ctrl ) {
						ctrl.observe('click', onConfigButton );
					});
					configTab.select('img.CFG_INFO_IMG').each( function( ctrl ) {
						ctrl.observe('click', onInfoclick );
					});
					// Hier noch alle inputs, selects etc in der Tabelle rausziehen und
					// mit einem Event Handler belegen.
					// top.frames['top'].$("LOADPRODUCTS_NOTICE").style.visibility = "hidden";
					
				}
			});
		}
	}
}();


EasyKat.Configuration.ctrl = {
	select : function(config) {
		var data = config.options;
		var opts = '', combo = '';
		
		if ( config.type == "ext" ) {
			combo = 'extended combo';
		} else {
			for ( var i=0; i < data.length; ++i ) {
			    if (data[i].value == config.value)
				    opts += '<option selected="true" value="' + data[i].value + '">' + data[i].text + '</option>';
			    else
				    opts += '<option value="' + data[i].value + '" class="' + data[i].style + '">' + data[i].text + '</option>';
			}

			var strDisabled = (config.disabled) ? '" disabled="true' : '';			
			combo = '<select id="' + config.id + '" name="' + config.id + '" attrid="' + config.attrid + '" class="' + config.style + strDisabled + '">' + opts + "</select>";
		}
		// alert(config.attrid + " = " + config.value);
		return combo;
	},
	
	table : function(config)    {
					var rows = config.rows;
					var table = '<table id="' + config.id + '" class="' + config.id + '" border="0" >\r\n';
					// var table = '<table id="' + config.id + '" border="0" >\r\n';
					
					try {
					for ( var i=0; i < rows.length; ++i ) {
						table += "<tr>\r\n"
						
						var cells = rows[i].cells;
						for ( var j=0; j < cells.length; ++j ) {
							var cell = cells[j];
							var content = cell.content;
							
							/* table += (cell.colspan) ? "<td colspan=\"" + cell.colspan + "\">" : "<td>"; */
							table += "<td";
							if (cell.colspan)
							    table += ' colspan="' + cell.colspan + '"';
							if (cell.tooltip)
							    table += ' title="' + cell.tooltip + '"';
							if (cell.style)
							    table += ' class="' + cell.style + '"';
							table += ">";
							
							var content = cells[j].content;
							
							if ( content instanceof Array ) {
								for ( var k=0; k < content.length; ++k ) {
									if ( content[k].ctrltype !== undefined )
										table += EasyKat.Configuration.ctrl[content[k].ctrltype](content[k]);
								}
							} 
							else if ( typeof content == "object" ) {
								if ( content.ctrltype !== undefined )
									table += EasyKat.Configuration.ctrl[content.ctrltype](content)
							} else if (content === undefined){
							    table += "&nbsp;";
							}
							else {
								table += content;
							}
							
							table += "</td>\r\n";
						}
						
						table += "</tr>\r\n";
					}
					}
					catch(e)    {
					    alert("error");
					}
					
					table += "</table>\r\n";
					
					return table;
	
	},
	
	input : function(config) {
	    if (config.updateMode)  {
	        $(config.id).setValue(config.value);
	        return "";
	    }
	    else    {

			var strDisabled = (config.disabled) ? '" disabled="true' : '';
		    config.type = config.type || 'text';
		    var ctrl = '<div>';
    		
		    if ( config.type == 'radio' ) {
			    ctrl += '<input id="' + config.id + '" name="' + config.name + '" attrid="' + config.attrid + '" type="' + config.type  + strDisabled + '" value="' + config.value;
			    if (config.checked)
			        ctrl += '" checked="true';
			    ctrl += '"/>';
		    } else if ( config.type == 'checkbox' ) {
			    ctrl += '<input id="' + config.id + '" name="' + config.id + '" type="' + config.type + strDisabled +  '" _value="' + config.value;
			    if (config.checked)
			        ctrl += '" checked="true';
			    ctrl += '"/>';
		    } else {
			    // ctrl += '<input id="' + config.id + '" name="' + config.id + '" type="' + config.type + strDisabled + '"/>';
			    ctrl += '<input id="' + config.id + '" name="' + config.id + '" type="' + config.type + '" value="' + config.value
			    if (config.Ub)
			         ctrl += '" Ub="' + Object.toJSON(config.Ub).replace(/"/g,"|");
			    if (config.Lb)
			        ctrl += '" Lb="' + Object.toJSON(config.Lb).replace(/"/g,"|");
			    ctrl += '"/>';
		    }
    		
		    if ( config.text ) {
			    var label = '<label for="' + config.id + '" class="' + config.labelstyle + '">' + config.text + '</label>';
			    ctrl = ( config.labelAlign == "right" ) ? ctrl + label : label + ctrl;
		    }
		    ctrl += '</div>';
		    return ctrl;
	    }
	},
	update : function(config)   {
	    var objCtrl = dialog[config.id] || $(config.id);
	    if (objCtrl)    {
	        switch (config.attr)    {
	        case "value":
	            var value = config.value.replace(/<br>/g, "\n");
	            value = value.replace(/\\\\n/g, "\n");
	            if ( objCtrl.setValue === undefined )
	            	objCtrl.value = value;
	            else
	            	objCtrl.setValue(value);
	            	
	            break;
   	        case "visibility":
   	            objCtrl.element.style.visibility = config.value;
   	            break;
	        }
	    }
	    return "";
	},
	
	img : function(config) {
		return config.doc ? '<img src="' + config.src + '" class="' + config.style + '" doc="' + config.doc + '"/>' : '<img src="' + config.src + '" class="' + config.style + '"/>';
	},
	button : function(config)   {
	    return '<button type="button" id="' + config.id + '" attrid="' + config.attrid + '" cfgaction="' + config.cfgaction + '" cfgvalue="' + config.value + '" class="' + config.style + '">' + config.text + '</button>';
	}
};

